DCE.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //===- DCE.cpp - Code to perform dead code elimination --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements dead inst elimination and dead code elimination.
  10. //
  11. // Dead Inst Elimination performs a single pass over the function removing
  12. // instructions that are obviously dead. Dead Code Elimination is similar, but
  13. // it rechecks instructions that were used by removed instructions to see if
  14. // they are newly dead.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/Transforms/Scalar/DCE.h"
  18. #include "llvm/ADT/SetVector.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/Analysis/TargetLibraryInfo.h"
  21. #include "llvm/IR/InstIterator.h"
  22. #include "llvm/IR/Instruction.h"
  23. #include "llvm/InitializePasses.h"
  24. #include "llvm/Pass.h"
  25. #include "llvm/Support/DebugCounter.h"
  26. #include "llvm/Transforms/Scalar.h"
  27. #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
  28. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  29. #include "llvm/Transforms/Utils/Local.h"
  30. using namespace llvm;
  31. #define DEBUG_TYPE "dce"
  32. STATISTIC(DCEEliminated, "Number of insts removed");
  33. DEBUG_COUNTER(DCECounter, "dce-transform",
  34. "Controls which instructions are eliminated");
  35. //===--------------------------------------------------------------------===//
  36. // RedundantDbgInstElimination pass implementation
  37. //
  38. namespace {
  39. struct RedundantDbgInstElimination : public FunctionPass {
  40. static char ID; // Pass identification, replacement for typeid
  41. RedundantDbgInstElimination() : FunctionPass(ID) {
  42. initializeRedundantDbgInstEliminationPass(*PassRegistry::getPassRegistry());
  43. }
  44. bool runOnFunction(Function &F) override {
  45. if (skipFunction(F))
  46. return false;
  47. bool Changed = false;
  48. for (auto &BB : F)
  49. Changed |= RemoveRedundantDbgInstrs(&BB);
  50. return Changed;
  51. }
  52. void getAnalysisUsage(AnalysisUsage &AU) const override {
  53. AU.setPreservesCFG();
  54. }
  55. };
  56. }
  57. char RedundantDbgInstElimination::ID = 0;
  58. INITIALIZE_PASS(RedundantDbgInstElimination, "redundant-dbg-inst-elim",
  59. "Redundant Dbg Instruction Elimination", false, false)
  60. Pass *llvm::createRedundantDbgInstEliminationPass() {
  61. return new RedundantDbgInstElimination();
  62. }
  63. PreservedAnalyses
  64. RedundantDbgInstEliminationPass::run(Function &F, FunctionAnalysisManager &AM) {
  65. bool Changed = false;
  66. for (auto &BB : F)
  67. Changed |= RemoveRedundantDbgInstrs(&BB);
  68. if (!Changed)
  69. return PreservedAnalyses::all();
  70. PreservedAnalyses PA;
  71. PA.preserveSet<CFGAnalyses>();
  72. return PA;
  73. }
  74. //===--------------------------------------------------------------------===//
  75. // DeadCodeElimination pass implementation
  76. //
  77. static bool DCEInstruction(Instruction *I,
  78. SmallSetVector<Instruction *, 16> &WorkList,
  79. const TargetLibraryInfo *TLI) {
  80. if (isInstructionTriviallyDead(I, TLI)) {
  81. if (!DebugCounter::shouldExecute(DCECounter))
  82. return false;
  83. salvageDebugInfo(*I);
  84. salvageKnowledge(I);
  85. // Null out all of the instruction's operands to see if any operand becomes
  86. // dead as we go.
  87. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
  88. Value *OpV = I->getOperand(i);
  89. I->setOperand(i, nullptr);
  90. if (!OpV->use_empty() || I == OpV)
  91. continue;
  92. // If the operand is an instruction that became dead as we nulled out the
  93. // operand, and if it is 'trivially' dead, delete it in a future loop
  94. // iteration.
  95. if (Instruction *OpI = dyn_cast<Instruction>(OpV))
  96. if (isInstructionTriviallyDead(OpI, TLI))
  97. WorkList.insert(OpI);
  98. }
  99. I->eraseFromParent();
  100. ++DCEEliminated;
  101. return true;
  102. }
  103. return false;
  104. }
  105. static bool eliminateDeadCode(Function &F, TargetLibraryInfo *TLI) {
  106. bool MadeChange = false;
  107. SmallSetVector<Instruction *, 16> WorkList;
  108. // Iterate over the original function, only adding insts to the worklist
  109. // if they actually need to be revisited. This avoids having to pre-init
  110. // the worklist with the entire function's worth of instructions.
  111. for (Instruction &I : llvm::make_early_inc_range(instructions(F))) {
  112. // We're visiting this instruction now, so make sure it's not in the
  113. // worklist from an earlier visit.
  114. if (!WorkList.count(&I))
  115. MadeChange |= DCEInstruction(&I, WorkList, TLI);
  116. }
  117. while (!WorkList.empty()) {
  118. Instruction *I = WorkList.pop_back_val();
  119. MadeChange |= DCEInstruction(I, WorkList, TLI);
  120. }
  121. return MadeChange;
  122. }
  123. PreservedAnalyses DCEPass::run(Function &F, FunctionAnalysisManager &AM) {
  124. if (!eliminateDeadCode(F, &AM.getResult<TargetLibraryAnalysis>(F)))
  125. return PreservedAnalyses::all();
  126. PreservedAnalyses PA;
  127. PA.preserveSet<CFGAnalyses>();
  128. return PA;
  129. }
  130. namespace {
  131. struct DCELegacyPass : public FunctionPass {
  132. static char ID; // Pass identification, replacement for typeid
  133. DCELegacyPass() : FunctionPass(ID) {
  134. initializeDCELegacyPassPass(*PassRegistry::getPassRegistry());
  135. }
  136. bool runOnFunction(Function &F) override {
  137. if (skipFunction(F))
  138. return false;
  139. TargetLibraryInfo *TLI =
  140. &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  141. return eliminateDeadCode(F, TLI);
  142. }
  143. void getAnalysisUsage(AnalysisUsage &AU) const override {
  144. AU.addRequired<TargetLibraryInfoWrapperPass>();
  145. AU.setPreservesCFG();
  146. }
  147. };
  148. }
  149. char DCELegacyPass::ID = 0;
  150. INITIALIZE_PASS(DCELegacyPass, "dce", "Dead Code Elimination", false, false)
  151. FunctionPass *llvm::createDeadCodeEliminationPass() {
  152. return new DCELegacyPass();
  153. }