2
0

DCE.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/SetVector.h"
  20. #include "llvm/ADT/Statistic.h"
  21. #include "llvm/IR/InstIterator.h"
  22. #include "llvm/IR/Instruction.h"
  23. #include "llvm/Pass.h"
  24. #include "llvm/Analysis/TargetLibraryInfo.h"
  25. #include "llvm/Transforms/Utils/Local.h"
  26. using namespace llvm;
  27. #define DEBUG_TYPE "dce"
  28. STATISTIC(DIEEliminated, "Number of insts removed by DIE pass");
  29. STATISTIC(DCEEliminated, "Number of insts removed");
  30. namespace {
  31. //===--------------------------------------------------------------------===//
  32. // DeadInstElimination pass implementation
  33. //
  34. struct DeadInstElimination : public BasicBlockPass {
  35. static char ID; // Pass identification, replacement for typeid
  36. DeadInstElimination() : BasicBlockPass(ID) {
  37. initializeDeadInstEliminationPass(*PassRegistry::getPassRegistry());
  38. }
  39. bool runOnBasicBlock(BasicBlock &BB) override {
  40. if (skipOptnoneFunction(BB))
  41. return false;
  42. auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
  43. TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
  44. bool Changed = false;
  45. for (BasicBlock::iterator DI = BB.begin(); DI != BB.end(); ) {
  46. Instruction *Inst = DI++;
  47. if (isInstructionTriviallyDead(Inst, TLI)) {
  48. Inst->eraseFromParent();
  49. Changed = true;
  50. ++DIEEliminated;
  51. }
  52. }
  53. return Changed;
  54. }
  55. void getAnalysisUsage(AnalysisUsage &AU) const override {
  56. AU.setPreservesCFG();
  57. }
  58. };
  59. }
  60. char DeadInstElimination::ID = 0;
  61. INITIALIZE_PASS(DeadInstElimination, "die",
  62. "Dead Instruction Elimination", false, false)
  63. Pass *llvm::createDeadInstEliminationPass() {
  64. return new DeadInstElimination();
  65. }
  66. namespace {
  67. //===--------------------------------------------------------------------===//
  68. // DeadCodeElimination pass implementation
  69. //
  70. struct DCE : public FunctionPass {
  71. static char ID; // Pass identification, replacement for typeid
  72. DCE() : FunctionPass(ID) {
  73. initializeDCEPass(*PassRegistry::getPassRegistry());
  74. }
  75. bool runOnFunction(Function &F) override;
  76. void getAnalysisUsage(AnalysisUsage &AU) const override {
  77. AU.setPreservesCFG();
  78. }
  79. };
  80. }
  81. char DCE::ID = 0;
  82. INITIALIZE_PASS(DCE, "dce", "Dead Code Elimination", false, false)
  83. bool DCE::runOnFunction(Function &F) {
  84. if (skipOptnoneFunction(F))
  85. return false;
  86. auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
  87. TargetLibraryInfo *TLI = TLIP ? &TLIP->getTLI() : nullptr;
  88. // Start out with all of the instructions in the worklist...
  89. SmallSetVector<Instruction*, 16> WorkList;
  90. for (inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i)
  91. WorkList.insert(&*i);
  92. // Loop over the worklist finding instructions that are dead. If they are
  93. // dead make them drop all of their uses, making other instructions
  94. // potentially dead, and work until the worklist is empty.
  95. //
  96. bool MadeChange = false;
  97. while (!WorkList.empty()) {
  98. Instruction *I = WorkList.pop_back_val();
  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.insert(Used);
  107. // Remove the instruction.
  108. I->eraseFromParent();
  109. MadeChange = true;
  110. ++DCEEliminated;
  111. }
  112. }
  113. return MadeChange;
  114. }
  115. FunctionPass *llvm::createDeadCodeEliminationPass() {
  116. return new DCE();
  117. }