Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 1 | //===---- BDCE.cpp - Bit-tracking dead code elimination -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 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. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Scalar/BDCE.h" |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallPtrSet.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/DemandedBits.h" |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/GlobalsModRef.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/Local.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 23 | #include "llvm/IR/InstIterator.h" |
| 24 | #include "llvm/IR/Instructions.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/raw_ostream.h" |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Scalar.h" |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | #define DEBUG_TYPE "bdce" |
| 32 | |
| 33 | STATISTIC(NumRemoved, "Number of instructions removed (unused)"); |
| 34 | STATISTIC(NumSimplified, "Number of instructions trivialized (dead bits)"); |
| 35 | |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 36 | /// If an instruction is trivialized (dead), then the chain of users of that |
| 37 | /// instruction may need to be cleared of assumptions that can no longer be |
| 38 | /// guaranteed correct. |
| 39 | static void clearAssumptionsOfUsers(Instruction *I, DemandedBits &DB) { |
Nikita Popov | 110cf05 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 40 | assert(I->getType()->isIntOrIntVectorTy() && |
| 41 | "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. |
Fangrui Song | b0f764c | 2019-03-07 06:38:03 +0000 | [diff] [blame^] | 44 | SmallPtrSet<Instruction *, 16> Visited; |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 45 | SmallVector<Instruction *, 16> WorkList; |
| 46 | for (User *JU : I->users()) { |
Sanjay Patel | a1067d9 | 2017-08-14 15:13:46 +0000 | [diff] [blame] | 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. |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 49 | auto *J = dyn_cast<Instruction>(JU); |
Nikita Popov | 110cf05 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 50 | if (J && J->getType()->isIntOrIntVectorTy() && |
Fangrui Song | b0f764c | 2019-03-07 06:38:03 +0000 | [diff] [blame^] | 51 | !DB.getDemandedBits(J).isAllOnesValue()) { |
| 52 | Visited.insert(J); |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 53 | WorkList.push_back(J); |
Fangrui Song | b0f764c | 2019-03-07 06:38:03 +0000 | [diff] [blame^] | 54 | } |
Hal Finkel | 9e54b70 | 2017-08-16 16:09:22 +0000 | [diff] [blame] | 55 | |
Nikita Popov | 110cf05 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 56 | // Note that we need to check for non-int types above before asking for |
Hal Finkel | 9e54b70 | 2017-08-16 16:09:22 +0000 | [diff] [blame] | 57 | // demanded bits. Normally, the only way to reach an instruction with an |
Nikita Popov | 110cf05 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 58 | // non-int type is via an instruction that has side effects (or otherwise |
Hal Finkel | 9e54b70 | 2017-08-16 16:09:22 +0000 | [diff] [blame] | 59 | // will demand its input bits). However, if we have a readnone function |
| 60 | // that returns an unsized type (e.g., void), we must avoid asking for the |
| 61 | // demanded bits of the function call's return value. A void-returning |
| 62 | // readnone function is always dead (and so we can stop walking the use/def |
| 63 | // chain here), but the check is necessary to avoid asserting. |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // DFS through subsequent users while tracking visits to avoid cycles. |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 67 | while (!WorkList.empty()) { |
| 68 | Instruction *J = WorkList.pop_back_val(); |
| 69 | |
| 70 | // NSW, NUW, and exact are based on operands that might have changed. |
| 71 | J->dropPoisonGeneratingFlags(); |
| 72 | |
| 73 | // We do not have to worry about llvm.assume or range metadata: |
| 74 | // 1. llvm.assume demands its operand, so trivializing can't change it. |
| 75 | // 2. range metadata only applies to memory accesses which demand all bits. |
| 76 | |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 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); |
Fangrui Song | b0f764c | 2019-03-07 06:38:03 +0000 | [diff] [blame^] | 81 | if (K && Visited.insert(K).second && K->getType()->isIntOrIntVectorTy() && |
Hal Finkel | 9e54b70 | 2017-08-16 16:09:22 +0000 | [diff] [blame] | 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 | |
Nikita Popov | cc6ef7f | 2019-01-02 20:02:14 +0000 | [diff] [blame] | 98 | // Remove instructions that are dead, either because they were not reached |
| 99 | // during analysis or have no demanded bits. |
| 100 | if (DB.isInstructionDead(&I) || |
| 101 | (I.getType()->isIntOrIntVectorTy() && |
| 102 | DB.getDemandedBits(&I).isNullValue() && |
| 103 | wouldInstructionBeTriviallyDead(&I))) { |
Nikita Popov | bc9986e | 2019-01-01 10:05:26 +0000 | [diff] [blame] | 104 | salvageDebugInfo(I); |
| 105 | Worklist.push_back(&I); |
| 106 | I.dropAllReferences(); |
| 107 | Changed = true; |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | for (Use &U : I.operands()) { |
| 112 | // DemandedBits only detects dead integer uses. |
| 113 | if (!U->getType()->isIntOrIntVectorTy()) |
| 114 | continue; |
| 115 | |
Nikita Popov | 6658fce | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 116 | if (!isa<Instruction>(U) && !isa<Argument>(U)) |
Nikita Popov | bc9986e | 2019-01-01 10:05:26 +0000 | [diff] [blame] | 117 | continue; |
| 118 | |
| 119 | if (!DB.isUseDead(&U)) |
| 120 | continue; |
| 121 | |
| 122 | LLVM_DEBUG(dbgs() << "BDCE: Trivializing: " << U << " (all bits dead)\n"); |
Sanjay Patel | fe346f9 | 2017-08-12 16:41:08 +0000 | [diff] [blame] | 123 | |
| 124 | clearAssumptionsOfUsers(&I, DB); |
| 125 | |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 126 | // FIXME: In theory we could substitute undef here instead of zero. |
| 127 | // This should be reconsidered once we settle on the semantics of |
| 128 | // undef, poison, etc. |
Nikita Popov | bc9986e | 2019-01-01 10:05:26 +0000 | [diff] [blame] | 129 | U.set(ConstantInt::get(U->getType(), 0)); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 130 | ++NumSimplified; |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 131 | Changed = true; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 132 | } |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | for (Instruction *&I : Worklist) { |
| 136 | ++NumRemoved; |
| 137 | I->eraseFromParent(); |
| 138 | } |
| 139 | |
| 140 | return Changed; |
| 141 | } |
| 142 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 143 | PreservedAnalyses BDCEPass::run(Function &F, FunctionAnalysisManager &AM) { |
| 144 | auto &DB = AM.getResult<DemandedBitsAnalysis>(F); |
Davide Italiano | bdc2971 | 2016-05-31 17:53:22 +0000 | [diff] [blame] | 145 | if (!bitTrackingDCE(F, DB)) |
| 146 | return PreservedAnalyses::all(); |
| 147 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 148 | PreservedAnalyses PA; |
| 149 | PA.preserveSet<CFGAnalyses>(); |
Davide Italiano | bdc2971 | 2016-05-31 17:53:22 +0000 | [diff] [blame] | 150 | PA.preserve<GlobalsAA>(); |
| 151 | return PA; |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 154 | namespace { |
| 155 | struct BDCELegacyPass : public FunctionPass { |
| 156 | static char ID; // Pass identification, replacement for typeid |
| 157 | BDCELegacyPass() : FunctionPass(ID) { |
| 158 | initializeBDCELegacyPassPass(*PassRegistry::getPassRegistry()); |
| 159 | } |
| 160 | |
| 161 | bool runOnFunction(Function &F) override { |
| 162 | if (skipFunction(F)) |
| 163 | return false; |
| 164 | auto &DB = getAnalysis<DemandedBitsWrapperPass>().getDemandedBits(); |
| 165 | return bitTrackingDCE(F, DB); |
| 166 | } |
| 167 | |
| 168 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 169 | AU.setPreservesCFG(); |
| 170 | AU.addRequired<DemandedBitsWrapperPass>(); |
| 171 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 172 | } |
| 173 | }; |
| 174 | } |
| 175 | |
| 176 | char BDCELegacyPass::ID = 0; |
| 177 | INITIALIZE_PASS_BEGIN(BDCELegacyPass, "bdce", |
| 178 | "Bit-Tracking Dead Code Elimination", false, false) |
| 179 | INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass) |
| 180 | INITIALIZE_PASS_END(BDCELegacyPass, "bdce", |
| 181 | "Bit-Tracking Dead Code Elimination", false, false) |
| 182 | |
| 183 | FunctionPass *llvm::createBitTrackingDCEPass() { return new BDCELegacyPass(); } |