blob: 34e15a18ce19edc3ee5424e4ab8f3678f050b8aa [file] [log] [blame]
Justin Bogner0638b7ba2015-09-25 21:03:46 +00001//===- ADCE.cpp - Code to perform dead code elimination -------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb28986f2001-06-30 06:39:11 +00009//
Owen Anderson7686b552008-05-29 08:45:13 +000010// This file implements the Aggressive Dead Code Elimination pass. This pass
11// optimistically assumes that all instructions are dead until proven otherwise,
Nadav Rotem465834c2012-07-24 10:51:42 +000012// allowing it to eliminate dead computations that other DCE passes do not
Owen Anderson7686b552008-05-29 08:45:13 +000013// catch, particularly involving loop computations.
Chris Lattnerb28986f2001-06-30 06:39:11 +000014//
15//===----------------------------------------------------------------------===//
16
Justin Bogner19b67992015-10-30 23:13:18 +000017#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/DepthFirstIterator.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/Statistic.h"
James Molloyefbba722015-09-10 10:22:12 +000022#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/BasicBlock.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000024#include "llvm/IR/CFG.h"
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +000025#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth83948572014-03-04 10:30:26 +000026#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Instructions.h"
28#include "llvm/IR/IntrinsicInst.h"
Andrew Kaylorf0f27922016-04-21 17:58:54 +000029#include "llvm/IR/OptBisect.h"
Owen Anderson7686b552008-05-29 08:45:13 +000030#include "llvm/Pass.h"
Betul Buyukkurtbf8554c2016-04-13 18:52:19 +000031#include "llvm/ProfileData/InstrProf.h"
Justin Bogner19b67992015-10-30 23:13:18 +000032#include "llvm/Transforms/Scalar.h"
Chris Lattnerfc7bdac2003-12-19 09:08:34 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chandler Carruth964daaa2014-04-22 02:55:47 +000035#define DEBUG_TYPE "adce"
36
Owen Anderson7686b552008-05-29 08:45:13 +000037STATISTIC(NumRemoved, "Number of instructions removed");
Chris Lattner019f3642002-05-06 17:27:57 +000038
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +000039static void collectLiveScopes(const DILocalScope &LS,
40 SmallPtrSetImpl<const Metadata *> &AliveScopes) {
41 if (!AliveScopes.insert(&LS).second)
42 return;
43
44 if (isa<DISubprogram>(LS))
45 return;
46
47 // Tail-recurse through the scope chain.
48 collectLiveScopes(cast<DILocalScope>(*LS.getScope()), AliveScopes);
49}
50
51static void collectLiveScopes(const DILocation &DL,
52 SmallPtrSetImpl<const Metadata *> &AliveScopes) {
53 // Even though DILocations are not scopes, shove them into AliveScopes so we
54 // don't revisit them.
55 if (!AliveScopes.insert(&DL).second)
56 return;
57
58 // Collect live scopes from the scope chain.
59 collectLiveScopes(*DL.getScope(), AliveScopes);
60
61 // Tail-recurse through the inlined-at chain.
62 if (const DILocation *IA = DL.getInlinedAt())
63 collectLiveScopes(*IA, AliveScopes);
64}
65
Betul Buyukkurtbf8554c2016-04-13 18:52:19 +000066// Check if this instruction is a runtime call for value profiling and
67// if it's instrumenting a constant.
68static bool isInstrumentsConstant(Instruction &I) {
69 if (CallInst *CI = dyn_cast<CallInst>(&I))
70 if (Function *Callee = CI->getCalledFunction())
71 if (Callee->getName().equals(getInstrProfValueProfFuncName()))
72 if (isa<Constant>(CI->getArgOperand(0)))
73 return true;
74 return false;
75}
76
Justin Bogner19b67992015-10-30 23:13:18 +000077static bool aggressiveDCE(Function& F) {
Matthias Braunb30f2f512016-01-30 01:24:31 +000078 SmallPtrSet<Instruction*, 32> Alive;
Hal Finkel75901292015-02-15 15:45:28 +000079 SmallVector<Instruction*, 128> Worklist;
Nadav Rotem465834c2012-07-24 10:51:42 +000080
Owen Anderson7686b552008-05-29 08:45:13 +000081 // Collect the set of "root" instructions that are known live.
Nico Rieck78199512015-08-06 19:10:45 +000082 for (Instruction &I : instructions(F)) {
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +000083 if (isa<TerminatorInst>(I) || I.isEHPad() || I.mayHaveSideEffects()) {
Betul Buyukkurtbf8554c2016-04-13 18:52:19 +000084 // Skip any value profile instrumentation calls if they are
85 // instrumenting constants.
86 if (isInstrumentsConstant(I))
87 continue;
Hal Finkel92fb2d32015-02-15 15:51:23 +000088 Alive.insert(&I);
89 Worklist.push_back(&I);
Owen Andersonc7d6ece2008-05-16 04:34:51 +000090 }
Hal Finkel92fb2d32015-02-15 15:51:23 +000091 }
Nadav Rotem465834c2012-07-24 10:51:42 +000092
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +000093 // Propagate liveness backwards to operands. Keep track of live debug info
94 // scopes.
95 SmallPtrSet<const Metadata *, 32> AliveScopes;
Hal Finkel75901292015-02-15 15:45:28 +000096 while (!Worklist.empty()) {
Hal Finkelc6035cf2015-02-15 15:47:52 +000097 Instruction *Curr = Worklist.pop_back_val();
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +000098
99 // Collect the live debug info scopes attached to this instruction.
100 if (const DILocation *DL = Curr->getDebugLoc())
101 collectLiveScopes(*DL, AliveScopes);
102
Hal Finkel8626ed22015-02-15 15:51:25 +0000103 for (Use &OI : Curr->operands()) {
Hal Finkelc6035cf2015-02-15 15:47:52 +0000104 if (Instruction *Inst = dyn_cast<Instruction>(OI))
Hal Finkel75901292015-02-15 15:45:28 +0000105 if (Alive.insert(Inst).second)
106 Worklist.push_back(Inst);
Hal Finkel8626ed22015-02-15 15:51:25 +0000107 }
Owen Anderson7686b552008-05-29 08:45:13 +0000108 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000109
Owen Anderson7686b552008-05-29 08:45:13 +0000110 // The inverse of the live set is the dead set. These are those instructions
111 // which have no side effects and do not influence the control flow or return
112 // value of the function, and may therefore be deleted safely.
Hal Finkel75901292015-02-15 15:45:28 +0000113 // NOTE: We reuse the Worklist vector here for memory efficiency.
Nico Rieck78199512015-08-06 19:10:45 +0000114 for (Instruction &I : instructions(F)) {
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000115 // Check if the instruction is alive.
116 if (Alive.count(&I))
117 continue;
118
119 if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) {
120 // Check if the scope of this variable location is alive.
121 if (AliveScopes.count(DII->getDebugLoc()->getScope()))
122 continue;
123
124 // Fallthrough and drop the intrinsic.
125 DEBUG({
126 // If intrinsic is pointing at a live SSA value, there may be an
127 // earlier optimization bug: if we know the location of the variable,
128 // why isn't the scope of the location alive?
129 if (Value *V = DII->getVariableLocation())
130 if (Instruction *II = dyn_cast<Instruction>(V))
131 if (Alive.count(II))
132 dbgs() << "Dropping debug info for " << *DII << "\n";
133 });
Chris Lattneracfd27d2001-09-09 22:26:47 +0000134 }
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000135
136 // Prepare to delete.
137 Worklist.push_back(&I);
138 I.dropAllReferences();
Hal Finkel92fb2d32015-02-15 15:51:23 +0000139 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000140
Hal Finkel92fb2d32015-02-15 15:51:23 +0000141 for (Instruction *&I : Worklist) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000142 ++NumRemoved;
Hal Finkel92fb2d32015-02-15 15:51:23 +0000143 I->eraseFromParent();
Chris Lattneracfd27d2001-09-09 22:26:47 +0000144 }
Devang Patel53b39b52008-11-11 00:54:10 +0000145
Hal Finkel75901292015-02-15 15:45:28 +0000146 return !Worklist.empty();
Chris Lattnerb28986f2001-06-30 06:39:11 +0000147}
Owen Anderson7686b552008-05-29 08:45:13 +0000148
Justin Bogner19b67992015-10-30 23:13:18 +0000149PreservedAnalyses ADCEPass::run(Function &F) {
Andrew Kaylorf0f27922016-04-21 17:58:54 +0000150 if (skipPassForFunction(name(), F))
151 return PreservedAnalyses::all();
152
Justin Bogner19b67992015-10-30 23:13:18 +0000153 if (aggressiveDCE(F))
154 return PreservedAnalyses::none();
155 return PreservedAnalyses::all();
Duncan Sands9e064a22008-05-29 14:38:23 +0000156}
Justin Bogner19b67992015-10-30 23:13:18 +0000157
158namespace {
159struct ADCELegacyPass : public FunctionPass {
160 static char ID; // Pass identification, replacement for typeid
161 ADCELegacyPass() : FunctionPass(ID) {
162 initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
163 }
164
165 bool runOnFunction(Function& F) override {
Andrew Kaylorf0f27922016-04-21 17:58:54 +0000166 if (skipFunction(F))
Justin Bogner19b67992015-10-30 23:13:18 +0000167 return false;
168 return aggressiveDCE(F);
169 }
170
171 void getAnalysisUsage(AnalysisUsage& AU) const override {
172 AU.setPreservesCFG();
173 AU.addPreserved<GlobalsAAWrapperPass>();
174 }
175};
176}
177
178char ADCELegacyPass::ID = 0;
179INITIALIZE_PASS(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination",
180 false, false)
181
182FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); }