BDCE.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //===---- BDCE.cpp - Bit-tracking 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 the Bit-Tracking Dead Code Elimination pass. Some
  10. // instructions (shifts, some ands, ors, etc.) kill some of their input bits.
  11. // We track these dead bits and remove instructions that compute only these
  12. // dead bits. We also simplify sext that generates unused extension bits,
  13. // converting it to a zext.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/Scalar/BDCE.h"
  17. #include "llvm/ADT/SmallPtrSet.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/Analysis/DemandedBits.h"
  21. #include "llvm/Analysis/GlobalsModRef.h"
  22. #include "llvm/IR/IRBuilder.h"
  23. #include "llvm/IR/InstIterator.h"
  24. #include "llvm/IR/Instructions.h"
  25. #include "llvm/InitializePasses.h"
  26. #include "llvm/Pass.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include "llvm/Transforms/Scalar.h"
  30. #include "llvm/Transforms/Utils/Local.h"
  31. using namespace llvm;
  32. #define DEBUG_TYPE "bdce"
  33. STATISTIC(NumRemoved, "Number of instructions removed (unused)");
  34. STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)");
  35. STATISTIC(NumSExt2ZExt,
  36. "Number of sign extension instructions converted to zero extension");
  37. /// If an instruction is trivialized (dead), then the chain of users of that
  38. /// instruction may need to be cleared of assumptions that can no longer be
  39. /// guaranteed correct.
  40. static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) {
  41. assert(I->getType()->isIntOrIntVectorTy() &&
  42. "Trivializing a non-integer value?");
  43. // Initialize the worklist with eligible direct users.
  44. SmallPtrSet<Instruction *, 16> Visited;
  45. SmallVector<Instruction *, 16> WorkList;
  46. for (User *JU : I->users()) {
  47. // If all bits of a user are demanded, then we know that nothing below that
  48. // in the def-use chain needs to be changed.
  49. auto *J = dyn_cast<Instruction>(JU);
  50. if (J && J->getType()->isIntOrIntVectorTy() &&
  51. !DB.getDemandedBits(J).isAllOnes()) {
  52. Visited.insert(J);
  53. WorkList.push_back(J);
  54. }
  55. // Note that we need to check for non-int types above before asking for
  56. // demanded bits. Normally, the only way to reach an instruction with an
  57. // non-int type is via an instruction that has side effects (or otherwise
  58. // will demand its input bits). However, if we have a readnone function
  59. // that returns an unsized type (e.g., void), we must avoid asking for the
  60. // demanded bits of the function call's return value. A void-returning
  61. // readnone function is always dead (and so we can stop walking the use/def
  62. // chain here), but the check is necessary to avoid asserting.
  63. }
  64. // DFS through subsequent users while tracking visits to avoid cycles.
  65. while (!WorkList.empty()) {
  66. Instruction *J = WorkList.pop_back_val();
  67. // NSW, NUW, and exact are based on operands that might have changed.
  68. J->dropPoisonGeneratingFlags();
  69. // We do not have to worry about llvm.assume or range metadata:
  70. // 1. llvm.assume demands its operand, so trivializing can't change it.
  71. // 2. range metadata only applies to memory accesses which demand all bits.
  72. for (User *KU : J->users()) {
  73. // If all bits of a user are demanded, then we know that nothing below
  74. // that in the def-use chain needs to be changed.
  75. auto *K = dyn_cast<Instruction>(KU);
  76. if (K && Visited.insert(K).second && K->getType()->isIntOrIntVectorTy() &&
  77. !DB.getDemandedBits(K).isAllOnes())
  78. WorkList.push_back(K);
  79. }
  80. }
  81. }
  82. static bool bitTrackingDCE(Function &F, DemandedBits &DB) {
  83. SmallVector<Instruction*, 128> Worklist;
  84. bool Changed = false;
  85. for (Instruction &I : instructions(F)) {
  86. // If the instruction has side effects and no non-dbg uses,
  87. // skip it. This way we avoid computing known bits on an instruction
  88. // that will not help us.
  89. if (I.mayHaveSideEffects() && I.use_empty())
  90. continue;
  91. // Remove instructions that are dead, either because they were not reached
  92. // during analysis or have no demanded bits.
  93. if (DB.isInstructionDead(&I) ||
  94. (I.getType()->isIntOrIntVectorTy() && DB.getDemandedBits(&I).isZero() &&
  95. wouldInstructionBeTriviallyDead(&I))) {
  96. Worklist.push_back(&I);
  97. Changed = true;
  98. continue;
  99. }
  100. // Convert SExt into ZExt if none of the extension bits is required
  101. if (SExtInst *SE = dyn_cast<SExtInst>(&I)) {
  102. APInt Demanded = DB.getDemandedBits(SE);
  103. const uint32_t SrcBitSize = SE->getSrcTy()->getScalarSizeInBits();
  104. auto *const DstTy = SE->getDestTy();
  105. const uint32_t DestBitSize = DstTy->getScalarSizeInBits();
  106. if (Demanded.countLeadingZeros() >= (DestBitSize - SrcBitSize)) {
  107. clearAssumptionsOfUsers(SE, DB);
  108. IRBuilder<> Builder(SE);
  109. I.replaceAllUsesWith(
  110. Builder.CreateZExt(SE->getOperand(0), DstTy, SE->getName()));
  111. Worklist.push_back(SE);
  112. Changed = true;
  113. NumSExt2ZExt++;
  114. continue;
  115. }
  116. }
  117. for (Use &U : I.operands()) {
  118. // DemandedBits only detects dead integer uses.
  119. if (!U->getType()->isIntOrIntVectorTy())
  120. continue;
  121. if (!isa<Instruction>(U) && !isa<Argument>(U))
  122. continue;
  123. if (!DB.isUseDead(&U))
  124. continue;
  125. LLVM_DEBUG(dbgs() << "BDCE: Trivializing: " << U << " (all bits dead)\n");
  126. clearAssumptionsOfUsers(&I, DB);
  127. // FIXME: In theory we could substitute undef here instead of zero.
  128. // This should be reconsidered once we settle on the semantics of
  129. // undef, poison, etc.
  130. U.set(ConstantInt::get(U->getType(), 0));
  131. ++NumSimplified;
  132. Changed = true;
  133. }
  134. }
  135. for (Instruction *&I : llvm::reverse(Worklist)) {
  136. salvageDebugInfo(*I);
  137. I->dropAllReferences();
  138. }
  139. for (Instruction *&I : Worklist) {
  140. ++NumRemoved;
  141. I->eraseFromParent();
  142. }
  143. return Changed;
  144. }
  145. PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) {
  146. auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
  147. if (!bitTrackingDCE(F, DB))
  148. return PreservedAnalyses::all();
  149. PreservedAnalyses PA;
  150. PA.preserveSet<CFGAnalyses>();
  151. return PA;
  152. }
  153. namespace {
  154. struct BDCELegacyPass : public FunctionPass {
  155. static char ID; // Pass identification, replacement for typeid
  156. BDCELegacyPass() : FunctionPass(ID) {
  157. initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry());
  158. }
  159. bool runOnFunction(Function &F) override {
  160. if (skipFunction(F))
  161. return false;
  162. auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
  163. return bitTrackingDCE(F, DB);
  164. }
  165. void getAnalysisUsage(AnalysisUsage &AU) const override {
  166. AU.setPreservesCFG();
  167. AU.addRequired<DemandedBitsWrapperPass>();
  168. AU.addPreserved<GlobalsAAWrapperPass>();
  169. }
  170. };
  171. }
  172. char BDCELegacyPass::ID = 0;
  173. INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce",
  174. "Bit-Tracking Dead Code Elimination", false, false)
  175. INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
  176. INITIALIZE_PASS_END(BDCELegacyPass, "bdce",
  177. "Bit-Tracking Dead Code Elimination", false, false)
  178. FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); }