Justin Bogner | 0638b7ba | 2015-09-25 21:03:46 +0000 | [diff] [blame] | 1 | //===- ADCE.cpp - Code to perform dead code elimination -------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 9 | // |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 10 | // This file implements the Aggressive Dead Code Elimination pass. This pass |
| 11 | // optimistically assumes that all instructions are dead until proven otherwise, |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 12 | // allowing it to eliminate dead computations that other DCE passes do not |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 13 | // catch, particularly involving loop computations. |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar/ADCE.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DepthFirstIterator.h" |
| 19 | #include "llvm/ADT/SmallPtrSet.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
James Molloy | efbba72 | 2015-09-10 10:22:12 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 24 | #include "llvm/IR/CFG.h" |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 26 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Instructions.h" |
| 28 | #include "llvm/IR/IntrinsicInst.h" |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
Betul Buyukkurt | bf8554c | 2016-04-13 18:52:19 +0000 | [diff] [blame] | 30 | #include "llvm/ProfileData/InstrProf.h" |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | fc7bdac | 2003-12-19 09:08:34 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 34 | #define DEBUG_TYPE "adce" |
| 35 | |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 36 | STATISTIC(NumRemoved, "Number of instructions removed"); |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 37 | |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 38 | static void collectLiveScopes(const DILocalScope &LS, |
| 39 | SmallPtrSetImpl<const Metadata *> &AliveScopes) { |
| 40 | if (!AliveScopes.insert(&LS).second) |
| 41 | return; |
| 42 | |
| 43 | if (isa<DISubprogram>(LS)) |
| 44 | return; |
| 45 | |
| 46 | // Tail-recurse through the scope chain. |
| 47 | collectLiveScopes(cast<DILocalScope>(*LS.getScope()), AliveScopes); |
| 48 | } |
| 49 | |
| 50 | static void collectLiveScopes(const DILocation &DL, |
| 51 | SmallPtrSetImpl<const Metadata *> &AliveScopes) { |
| 52 | // Even though DILocations are not scopes, shove them into AliveScopes so we |
| 53 | // don't revisit them. |
| 54 | if (!AliveScopes.insert(&DL).second) |
| 55 | return; |
| 56 | |
| 57 | // Collect live scopes from the scope chain. |
| 58 | collectLiveScopes(*DL.getScope(), AliveScopes); |
| 59 | |
| 60 | // Tail-recurse through the inlined-at chain. |
| 61 | if (const DILocation *IA = DL.getInlinedAt()) |
| 62 | collectLiveScopes(*IA, AliveScopes); |
| 63 | } |
| 64 | |
Betul Buyukkurt | bf8554c | 2016-04-13 18:52:19 +0000 | [diff] [blame] | 65 | // Check if this instruction is a runtime call for value profiling and |
| 66 | // if it's instrumenting a constant. |
| 67 | static bool isInstrumentsConstant(Instruction &I) { |
| 68 | if (CallInst *CI = dyn_cast<CallInst>(&I)) |
| 69 | if (Function *Callee = CI->getCalledFunction()) |
| 70 | if (Callee->getName().equals(getInstrProfValueProfFuncName())) |
| 71 | if (isa<Constant>(CI->getArgOperand(0))) |
| 72 | return true; |
| 73 | return false; |
| 74 | } |
| 75 | |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 76 | static bool aggressiveDCE(Function& F) { |
Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 77 | SmallPtrSet<Instruction*, 32> Alive; |
Hal Finkel | 7590129 | 2015-02-15 15:45:28 +0000 | [diff] [blame] | 78 | SmallVector<Instruction*, 128> Worklist; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 79 | |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 80 | // Collect the set of "root" instructions that are known live. |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 81 | for (Instruction &I : instructions(F)) { |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 82 | if (isa<TerminatorInst>(I) || I.isEHPad() || I.mayHaveSideEffects()) { |
Betul Buyukkurt | bf8554c | 2016-04-13 18:52:19 +0000 | [diff] [blame] | 83 | // Skip any value profile instrumentation calls if they are |
| 84 | // instrumenting constants. |
| 85 | if (isInstrumentsConstant(I)) |
| 86 | continue; |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 87 | Alive.insert(&I); |
| 88 | Worklist.push_back(&I); |
Owen Anderson | c7d6ece | 2008-05-16 04:34:51 +0000 | [diff] [blame] | 89 | } |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 90 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 91 | |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 92 | // Propagate liveness backwards to operands. Keep track of live debug info |
| 93 | // scopes. |
| 94 | SmallPtrSet<const Metadata *, 32> AliveScopes; |
Hal Finkel | 7590129 | 2015-02-15 15:45:28 +0000 | [diff] [blame] | 95 | while (!Worklist.empty()) { |
Hal Finkel | c6035cf | 2015-02-15 15:47:52 +0000 | [diff] [blame] | 96 | Instruction *Curr = Worklist.pop_back_val(); |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 97 | |
| 98 | // Collect the live debug info scopes attached to this instruction. |
| 99 | if (const DILocation *DL = Curr->getDebugLoc()) |
| 100 | collectLiveScopes(*DL, AliveScopes); |
| 101 | |
Hal Finkel | 8626ed2 | 2015-02-15 15:51:25 +0000 | [diff] [blame] | 102 | for (Use &OI : Curr->operands()) { |
Hal Finkel | c6035cf | 2015-02-15 15:47:52 +0000 | [diff] [blame] | 103 | if (Instruction *Inst = dyn_cast<Instruction>(OI)) |
Hal Finkel | 7590129 | 2015-02-15 15:45:28 +0000 | [diff] [blame] | 104 | if (Alive.insert(Inst).second) |
| 105 | Worklist.push_back(Inst); |
Hal Finkel | 8626ed2 | 2015-02-15 15:51:25 +0000 | [diff] [blame] | 106 | } |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 107 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 108 | |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 109 | // The inverse of the live set is the dead set. These are those instructions |
| 110 | // which have no side effects and do not influence the control flow or return |
| 111 | // value of the function, and may therefore be deleted safely. |
Hal Finkel | 7590129 | 2015-02-15 15:45:28 +0000 | [diff] [blame] | 112 | // NOTE: We reuse the Worklist vector here for memory efficiency. |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 113 | for (Instruction &I : instructions(F)) { |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 114 | // Check if the instruction is alive. |
| 115 | if (Alive.count(&I)) |
| 116 | continue; |
| 117 | |
| 118 | if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) { |
| 119 | // Check if the scope of this variable location is alive. |
| 120 | if (AliveScopes.count(DII->getDebugLoc()->getScope())) |
| 121 | continue; |
| 122 | |
| 123 | // Fallthrough and drop the intrinsic. |
| 124 | DEBUG({ |
| 125 | // If intrinsic is pointing at a live SSA value, there may be an |
| 126 | // earlier optimization bug: if we know the location of the variable, |
| 127 | // why isn't the scope of the location alive? |
| 128 | if (Value *V = DII->getVariableLocation()) |
| 129 | if (Instruction *II = dyn_cast<Instruction>(V)) |
| 130 | if (Alive.count(II)) |
| 131 | dbgs() << "Dropping debug info for " << *DII << "\n"; |
| 132 | }); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 133 | } |
Duncan P. N. Exon Smith | e8eb94a | 2016-03-29 22:57:12 +0000 | [diff] [blame] | 134 | |
| 135 | // Prepare to delete. |
| 136 | Worklist.push_back(&I); |
| 137 | I.dropAllReferences(); |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 138 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 139 | |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 140 | for (Instruction *&I : Worklist) { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 141 | ++NumRemoved; |
Hal Finkel | 92fb2d3 | 2015-02-15 15:51:23 +0000 | [diff] [blame] | 142 | I->eraseFromParent(); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 143 | } |
Devang Patel | 53b39b5 | 2008-11-11 00:54:10 +0000 | [diff] [blame] | 144 | |
Hal Finkel | 7590129 | 2015-02-15 15:45:28 +0000 | [diff] [blame] | 145 | return !Worklist.empty(); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 146 | } |
Owen Anderson | 7686b55 | 2008-05-29 08:45:13 +0000 | [diff] [blame] | 147 | |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 148 | PreservedAnalyses ADCEPass::run(Function &F) { |
Davide Italiano | 688616f | 2016-05-31 17:39:39 +0000 | [diff] [blame^] | 149 | if (!aggressiveDCE(F)) |
| 150 | return PreservedAnalyses::all(); |
| 151 | |
| 152 | // FIXME: ADCE should also 'preserve the CFG'. |
| 153 | // The new pass manager has currently no way to do it. |
| 154 | auto PA = PreservedAnalyses(); |
| 155 | PA.preserve<GlobalsAA>(); |
| 156 | return PA; |
Duncan Sands | 9e064a2 | 2008-05-29 14:38:23 +0000 | [diff] [blame] | 157 | } |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 158 | |
| 159 | namespace { |
| 160 | struct ADCELegacyPass : public FunctionPass { |
| 161 | static char ID; // Pass identification, replacement for typeid |
| 162 | ADCELegacyPass() : FunctionPass(ID) { |
| 163 | initializeADCELegacyPassPass(*PassRegistry::getPassRegistry()); |
| 164 | } |
| 165 | |
| 166 | bool runOnFunction(Function& F) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 167 | if (skipFunction(F)) |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 168 | return false; |
| 169 | return aggressiveDCE(F); |
| 170 | } |
| 171 | |
| 172 | void getAnalysisUsage(AnalysisUsage& AU) const override { |
| 173 | AU.setPreservesCFG(); |
| 174 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 175 | } |
| 176 | }; |
| 177 | } |
| 178 | |
| 179 | char ADCELegacyPass::ID = 0; |
| 180 | INITIALIZE_PASS(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination", |
| 181 | false, false) |
| 182 | |
| 183 | FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); } |