ADCE.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===- DCE.cpp - Code to perform dead code elimination --------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Aggressive Dead Code Elimination pass. This pass
  11. // optimistically assumes that all instructions are dead until proven otherwise,
  12. // allowing it to eliminate dead computations that other DCE passes do not
  13. // catch, particularly involving loop computations.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/Scalar.h"
  17. #include "llvm/ADT/DepthFirstIterator.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/Statistic.h"
  21. #include "llvm/IR/BasicBlock.h"
  22. #include "llvm/IR/CFG.h"
  23. #include "llvm/IR/InstIterator.h"
  24. #include "llvm/IR/Instructions.h"
  25. #include "llvm/IR/IntrinsicInst.h"
  26. #include "llvm/Pass.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "adce"
  29. STATISTIC(NumRemoved, "Number of instructions removed");
  30. namespace {
  31. struct ADCE : public FunctionPass {
  32. static char ID; // Pass identification, replacement for typeid
  33. ADCE() : FunctionPass(ID) {
  34. initializeADCEPass(*PassRegistry::getPassRegistry());
  35. }
  36. bool runOnFunction(Function& F) override;
  37. void getAnalysisUsage(AnalysisUsage& AU) const override {
  38. AU.setPreservesCFG();
  39. }
  40. };
  41. }
  42. char ADCE::ID = 0;
  43. INITIALIZE_PASS(ADCE, "adce", "Aggressive Dead Code Elimination", false, false)
  44. bool ADCE::runOnFunction(Function& F) {
  45. if (skipOptnoneFunction(F))
  46. return false;
  47. SmallPtrSet<Instruction*, 128> Alive;
  48. SmallVector<Instruction*, 128> Worklist;
  49. // Collect the set of "root" instructions that are known live.
  50. for (Instruction &I : inst_range(F)) {
  51. if (isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(I) ||
  52. isa<LandingPadInst>(I) || I.mayHaveSideEffects()) {
  53. Alive.insert(&I);
  54. Worklist.push_back(&I);
  55. }
  56. }
  57. // Propagate liveness backwards to operands.
  58. while (!Worklist.empty()) {
  59. Instruction *Curr = Worklist.pop_back_val();
  60. for (Use &OI : Curr->operands()) {
  61. if (Instruction *Inst = dyn_cast<Instruction>(OI))
  62. if (Alive.insert(Inst).second)
  63. Worklist.push_back(Inst);
  64. }
  65. }
  66. // The inverse of the live set is the dead set. These are those instructions
  67. // which have no side effects and do not influence the control flow or return
  68. // value of the function, and may therefore be deleted safely.
  69. // NOTE: We reuse the Worklist vector here for memory efficiency.
  70. for (Instruction &I : inst_range(F)) {
  71. if (!Alive.count(&I)) {
  72. Worklist.push_back(&I);
  73. I.dropAllReferences();
  74. }
  75. }
  76. for (Instruction *&I : Worklist) {
  77. ++NumRemoved;
  78. I->eraseFromParent();
  79. }
  80. return !Worklist.empty();
  81. }
  82. FunctionPass *llvm::createAggressiveDCEPass() {
  83. return new ADCE();
  84. }