Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 1 | //===---- BDCE.cpp - Bit-tracking 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 Bit-Tracking Dead Code Elimination pass. Some |
| 11 | // instructions (shifts, some ands, ors, etc.) kill some of their input bits. |
| 12 | // We track these dead bits and remove instructions that compute only these |
| 13 | // dead bits. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar/BDCE.h" |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallPtrSet.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/DemandedBits.h" |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/GlobalsModRef.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame^] | 23 | #include "llvm/Transforms/Utils/Local.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 24 | #include "llvm/IR/InstIterator.h" |
| 25 | #include "llvm/IR/Instructions.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Scalar.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | #define DEBUG_TYPE "bdce" |
| 33 | |
| 34 | STATISTIC(NumRemoved, "Number of instructions removed (unused)"); |
| 35 | STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)"); |
| 36 | |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 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) { |
Sanjay Patel | a1067d9 | 2017-08-14 15:13:46 +0000 | [diff] [blame] | 41 | assert(I->getType()->isIntegerTy() && "Trivializing a non-integer value?"); |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 42 | |
| 43 | // Initialize the worklist with eligible direct users. |
| 44 | SmallVector<Instruction *, 16> WorkList; |
| 45 | for (User *JU : I->users()) { |
Sanjay Patel | a1067d9 | 2017-08-14 15:13:46 +0000 | [diff] [blame] | 46 | // If all bits of a user are demanded, then we know that nothing below that |
| 47 | // in the def-use chain needs to be changed. |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 48 | auto *J = dyn_cast<Instruction>(JU); |
Hal Finkel | 9e54b70 | 2017-08-16 16:09:22 +0000 | [diff] [blame] | 49 | if (J && J->getType()->isSized() && |
| 50 | !DB.getDemandedBits(J).isAllOnesValue()) |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 51 | WorkList.push_back(J); |
Hal Finkel | 9e54b70 | 2017-08-16 16:09:22 +0000 | [diff] [blame] | 52 | |
| 53 | // Note that we need to check for unsized types above before asking for |
| 54 | // demanded bits. Normally, the only way to reach an instruction with an |
| 55 | // unsized type is via an instruction that has side effects (or otherwise |
| 56 | // will demand its input bits). However, if we have a readnone function |
| 57 | // that returns an unsized type (e.g., void), we must avoid asking for the |
| 58 | // demanded bits of the function call's return value. A void-returning |
| 59 | // readnone function is always dead (and so we can stop walking the use/def |
| 60 | // chain here), but the check is necessary to avoid asserting. |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // DFS through subsequent users while tracking visits to avoid cycles. |
| 64 | SmallPtrSet<Instruction *, 16> Visited; |
| 65 | while (!WorkList.empty()) { |
| 66 | Instruction *J = WorkList.pop_back_val(); |
| 67 | |
| 68 | // NSW, NUW, and exact are based on operands that might have changed. |
| 69 | J->dropPoisonGeneratingFlags(); |
| 70 | |
| 71 | // We do not have to worry about llvm.assume or range metadata: |
| 72 | // 1. llvm.assume demands its operand, so trivializing can't change it. |
| 73 | // 2. range metadata only applies to memory accesses which demand all bits. |
| 74 | |
| 75 | Visited.insert(J); |
| 76 | |
| 77 | for (User *KU : J->users()) { |
Sanjay Patel | a1067d9 | 2017-08-14 15:13:46 +0000 | [diff] [blame] | 78 | // If all bits of a user are demanded, then we know that nothing below |
| 79 | // that in the def-use chain needs to be changed. |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 80 | auto *K = dyn_cast<Instruction>(KU); |
Hal Finkel | 9e54b70 | 2017-08-16 16:09:22 +0000 | [diff] [blame] | 81 | if (K && !Visited.count(K) && K->getType()->isSized() && |
| 82 | !DB.getDemandedBits(K).isAllOnesValue()) |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 83 | WorkList.push_back(K); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 88 | static bool bitTrackingDCE(Function &F, DemandedBits &DB) { |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 89 | SmallVector<Instruction*, 128> Worklist; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 90 | bool Changed = false; |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 91 | for (Instruction &I : instructions(F)) { |
Davide Italiano | 043e661 | 2016-12-06 21:52:47 +0000 | [diff] [blame] | 92 | // If the instruction has side effects and no non-dbg uses, |
Davide Italiano | 1ed5396 | 2016-12-07 21:47:32 +0000 | [diff] [blame] | 93 | // skip it. This way we avoid computing known bits on an instruction |
| 94 | // that will not help us. |
Davide Italiano | 043e661 | 2016-12-06 21:52:47 +0000 | [diff] [blame] | 95 | if (I.mayHaveSideEffects() && I.use_empty()) |
| 96 | continue; |
| 97 | |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 98 | if (I.getType()->isIntegerTy() && |
| 99 | !DB.getDemandedBits(&I).getBoolValue()) { |
| 100 | // For live instructions that have all dead bits, first make them dead by |
| 101 | // replacing all uses with something else. Then, if they don't need to |
| 102 | // remain live (because they have side effects, etc.) we can remove them. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 103 | LLVM_DEBUG(dbgs() << "BDCE: Trivializing: " << I << " (all bits dead)\n"); |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 104 | |
| 105 | clearAssumptionsOfUsers(&I, DB); |
| 106 | |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 107 | // FIXME: In theory we could substitute undef here instead of zero. |
| 108 | // This should be reconsidered once we settle on the semantics of |
| 109 | // undef, poison, etc. |
| 110 | Value *Zero = ConstantInt::get(I.getType(), 0); |
| 111 | ++NumSimplified; |
Davide Italiano | 1ed5396 | 2016-12-07 21:47:32 +0000 | [diff] [blame] | 112 | I.replaceNonMetadataUsesWith(Zero); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 113 | Changed = true; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 114 | } |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 115 | if (!DB.isInstructionDead(&I)) |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 116 | continue; |
| 117 | |
Vedant Kumar | 56492f9 | 2018-02-21 01:55:33 +0000 | [diff] [blame] | 118 | salvageDebugInfo(I); |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 119 | Worklist.push_back(&I); |
| 120 | I.dropAllReferences(); |
| 121 | Changed = true; |
| 122 | } |
| 123 | |
| 124 | for (Instruction *&I : Worklist) { |
| 125 | ++NumRemoved; |
| 126 | I->eraseFromParent(); |
| 127 | } |
| 128 | |
| 129 | return Changed; |
| 130 | } |
| 131 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 132 | PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) { |
| 133 | auto &DB = AM.getResult<DemandedBitsAnalysis>(F); |
Davide Italiano | bdc2971 | 2016-05-31 17:53:22 +0000 | [diff] [blame] | 134 | if (!bitTrackingDCE(F, DB)) |
| 135 | return PreservedAnalyses::all(); |
| 136 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 137 | PreservedAnalyses PA; |
| 138 | PA.preserveSet<CFGAnalyses>(); |
Davide Italiano | bdc2971 | 2016-05-31 17:53:22 +0000 | [diff] [blame] | 139 | PA.preserve<GlobalsAA>(); |
| 140 | return PA; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 143 | namespace { |
| 144 | struct BDCELegacyPass : public FunctionPass { |
| 145 | static char ID; // Pass identification, replacement for typeid |
| 146 | BDCELegacyPass() : FunctionPass(ID) { |
| 147 | initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry()); |
| 148 | } |
| 149 | |
| 150 | bool runOnFunction(Function &F) override { |
| 151 | if (skipFunction(F)) |
| 152 | return false; |
| 153 | auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits(); |
| 154 | return bitTrackingDCE(F, DB); |
| 155 | } |
| 156 | |
| 157 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 158 | AU.setPreservesCFG(); |
| 159 | AU.addRequired<DemandedBitsWrapperPass>(); |
| 160 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 161 | } |
| 162 | }; |
| 163 | } |
| 164 | |
| 165 | char BDCELegacyPass::ID = 0; |
| 166 | INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce", |
| 167 | "Bit-Tracking Dead Code Elimination", false, false) |
| 168 | INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass) |
| 169 | INITIALIZE_PASS_END(BDCELegacyPass, "bdce", |
| 170 | "Bit-Tracking Dead Code Elimination", false, false) |
| 171 | |
| 172 | FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); } |