DCE.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 dead inst elimination and dead code elimination.
  11. //
  12. // Dead Inst Elimination performs a single pass over the function removing
  13. // instructions that are obviously dead. Dead Code Elimination is similar, but
  14. // it rechecks instructions that were used by removed instructions to see if
  15. // they are newly dead.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/Transforms/Scalar.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/IR/InstIterator.h"
  21. #include "llvm/IR/Instruction.h"
  22. #include "llvm/Pass.h"
  23. #include "llvm/Analysis/TargetLibraryInfo.h"
  24. #include "llvm/Transforms/Utils/Local.h"
  25. using namespace llvm;
  26. #define DEBUG_TYPE "dce"
  27. STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
  28. STATISTIC(DCEEliminated, "Number of insts removed");
  29. namespace {
  30. //===--------------------------------------------------------------------===//
  31. // DeadInstElimination pass implementation
  32. //
  33. struct DeadInstElimination : public BasicBlockPass {
  34. static char ID; // Pass identification, replacement for typeid
  35. DeadInstElimination() : BasicBlockPass(ID) {
  36. initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
  37. }
  38. bool runOnBasicBlock(BasicBlock &BB) override {
  39. if (skipOptnoneFunction(BB))
  40. return false;
  41. auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
  42. TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
  43. bool Changed = false;
  44. for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
  45. Instruction *Inst = DI++;
  46. if (isInstructionTriviallyDead(Inst, TLI)) {
  47. Inst->eraseFromParent();
  48. Changed = true;
  49. ++DIEEliminated;
  50. }
  51. }
  52. return Changed;
  53. }
  54. void getAnalysisUsage(AnalysisUsage &AU) const override {
  55. AU.setPreservesCFG();
  56. }
  57. };
  58. }
  59. char DeadInstElimination::ID = 0;
  60. INITIALIZE_PASS(DeadInstElimination, "die",
  61. "Dead Instruction Elimination", false, false)
  62. Pass *llvm::createDeadInstEliminationPass() {
  63. return new DeadInstElimination();
  64. }
  65. namespace {
  66. //===--------------------------------------------------------------------===//
  67. // DeadCodeElimination pass implementation
  68. //
  69. struct DCE : public FunctionPass {
  70. static char ID; // Pass identification, replacement for typeid
  71. DCE() : FunctionPass(ID) {
  72. initializeDCEPass(*PassRegistry::getPassRegistry());
  73. }
  74. bool runOnFunction(Function &F) override;
  75. void getAnalysisUsage(AnalysisUsage &AU) const override {
  76. AU.setPreservesCFG();
  77. }
  78. };
  79. }
  80. char DCE::ID = 0;
  81. INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false)
  82. bool DCE::runOnFunction(Function &F) {
  83. if (skipOptnoneFunction(F))
  84. return false;
  85. auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
  86. TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
  87. // Start out with all of the instructions in the worklist...
  88. std::vector<Instruction*> WorkList;
  89. for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
  90. WorkList.push_back(&*i);
  91. // Loop over the worklist finding instructions that are dead. If they are
  92. // dead make them drop all of their uses, making other instructions
  93. // potentially dead, and work until the worklist is empty.
  94. //
  95. bool MadeChange = false;
  96. while (!WorkList.empty()) {
  97. Instruction *I = WorkList.back();
  98. WorkList.pop_back();
  99. if (isInstructionTriviallyDead(I, TLI)) { // If the instruction is dead.
  100. // Loop over all of the values that the instruction uses, if there are
  101. // instructions being used, add them to the worklist, because they might
  102. // go dead after this one is removed.
  103. //
  104. for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
  105. if (Instruction *Used = dyn_cast<Instruction>(*OI))
  106. WorkList.push_back(Used);
  107. // Remove the instruction.
  108. I->eraseFromParent();
  109. // Remove the instruction from the worklist if it still exists in it.
  110. WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
  111. WorkList.end());
  112. MadeChange = true;
  113. ++DCEEliminated;
  114. }
  115. }
  116. return MadeChange;
  117. }
  118. FunctionPass *llvm::createDeadCodeEliminationPass() {
  119. return new DCE();
  120. }