blob: 1cec336d712c434450f1bc3a3097569bd82c7b0f [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"
David Callahancc5cd4d2016-08-03 04:28:39 +000018
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/DepthFirstIterator.h"
20#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/Statistic.h"
James Molloyefbba722015-09-10 10:22:12 +000023#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/BasicBlock.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000025#include "llvm/IR/CFG.h"
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +000026#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth83948572014-03-04 10:30:26 +000027#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Instructions.h"
29#include "llvm/IR/IntrinsicInst.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
David Callahan947be0f2016-08-16 14:31:51 +000039// This is a tempoary option until we change the interface
40// to this pass based on optimization level.
41static cl::opt<bool> RemoveControlFlowFlag("adce-remove-control-flow",
42 cl::init(false), cl::Hidden);
43
David Callahancc5cd4d2016-08-03 04:28:39 +000044namespace {
David Callahan947be0f2016-08-16 14:31:51 +000045/// Information about Instructions
46struct InstInfoType {
47 /// True if the associated instruction is live.
48 bool Live = false;
49 /// Quick access to information for block containing associated Instruction.
50 struct BlockInfoType *Block = nullptr;
51};
52
53/// Information about basic blocks relevant to dead code elimination.
54struct BlockInfoType {
55 /// True when this block contains a live instructions.
56 bool Live = false;
57 /// True when this block ends in an unconditional branch.
58 bool UnconditionalBranch = false;
59
60 /// Quick access to the LiveInfo for the terminator,
61 /// holds the value &InstInfo[Terminator]
62 InstInfoType *TerminatorLiveInfo = nullptr;
63
64 bool terminatorIsLive() const { return TerminatorLiveInfo->Live; }
65
66 /// Corresponding BasicBlock.
67 BasicBlock *BB = nullptr;
68
69 /// Cache of BB->getTerminator()
70 TerminatorInst *Terminator = nullptr;
71};
72
David Callahan45e442e2016-08-05 19:38:11 +000073class AggressiveDeadCodeElimination {
David Callahancc5cd4d2016-08-03 04:28:39 +000074 Function &F;
David Callahan947be0f2016-08-16 14:31:51 +000075
76 /// Mapping of blocks to associated information, an element in BlockInfoVec.
77 DenseMap<BasicBlock *, BlockInfoType> BlockInfo;
78 bool isLive(BasicBlock *BB) { return BlockInfo[BB].Live; }
79
80 /// Mapping of instructions to associated information.
81 DenseMap<Instruction *, InstInfoType> InstInfo;
82 bool isLive(Instruction *I) { return InstInfo[I].Live; }
83
David Callahan45e442e2016-08-05 19:38:11 +000084 /// Instructions known to be live where we need to mark
85 /// reaching definitions as live.
David Callahancc5cd4d2016-08-03 04:28:39 +000086 SmallVector<Instruction *, 128> Worklist;
David Callahan45e442e2016-08-05 19:38:11 +000087 /// Debug info scopes around a live instruction.
David Callahancc5cd4d2016-08-03 04:28:39 +000088 SmallPtrSet<const Metadata *, 32> AliveScopes;
David Callahan45e442e2016-08-05 19:38:11 +000089
David Callahan947be0f2016-08-16 14:31:51 +000090 /// Set of blocks with not known to have live terminators.
91 SmallPtrSet<BasicBlock *, 16> BlocksWithDeadTerminators;
92
93 /// The set of blocks which we have determined are live in the
94 /// most recent iteration of propagating liveness.
95 SmallPtrSet<BasicBlock *, 16> NewLiveBlocks;
96
97 /// Set up auxiliary data structures for Instructions and BasicBlocks and
98 /// initialize the Worklist to the set of must-be-live Instruscions.
David Callahan45e442e2016-08-05 19:38:11 +000099 void initialize();
David Callahan947be0f2016-08-16 14:31:51 +0000100 /// Return true for operations which are always treated as live.
David Callahan45e442e2016-08-05 19:38:11 +0000101 bool isAlwaysLive(Instruction &I);
David Callahan947be0f2016-08-16 14:31:51 +0000102 /// Return true for instrumentation instructions for value profiling.
David Callahan45e442e2016-08-05 19:38:11 +0000103 bool isInstrumentsConstant(Instruction &I);
David Callahan45e442e2016-08-05 19:38:11 +0000104
105 /// Propagate liveness to reaching definitions.
106 void markLiveInstructions();
107 /// Mark an instruction as live.
David Callahan947be0f2016-08-16 14:31:51 +0000108 void markLive(Instruction *I);
109
110 /// Record the Debug Scopes which surround live debug information.
David Callahancc5cd4d2016-08-03 04:28:39 +0000111 void collectLiveScopes(const DILocalScope &LS);
112 void collectLiveScopes(const DILocation &DL);
David Callahan947be0f2016-08-16 14:31:51 +0000113
114 /// Analyze dead branches to find those whose branches are the sources
115 /// of control dependences impacting a live block. Those branches are
116 /// marked live.
117 void markLiveBranchesFromControlDependences();
David Callahan45e442e2016-08-05 19:38:11 +0000118
119 /// Remove instructions not marked live, return if any any instruction
120 /// was removed.
121 bool removeDeadInstructions();
122
David Callahancc5cd4d2016-08-03 04:28:39 +0000123public:
David Callahan45e442e2016-08-05 19:38:11 +0000124 AggressiveDeadCodeElimination(Function &F) : F(F) {}
125 bool performDeadCodeElimination();
David Callahancc5cd4d2016-08-03 04:28:39 +0000126};
127}
128
David Callahan45e442e2016-08-05 19:38:11 +0000129bool AggressiveDeadCodeElimination::performDeadCodeElimination() {
130 initialize();
131 markLiveInstructions();
132 return removeDeadInstructions();
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000133}
134
David Callahan947be0f2016-08-16 14:31:51 +0000135static bool isUnconditionalBranch(TerminatorInst *Term) {
136 auto BR = dyn_cast<BranchInst>(Term);
137 return BR && BR->isUnconditional();
138}
139
David Callahan45e442e2016-08-05 19:38:11 +0000140void AggressiveDeadCodeElimination::initialize() {
David Callahan947be0f2016-08-16 14:31:51 +0000141
142 auto NumBlocks = F.size();
143
144 // We will have an entry in the map for each block so we grow the
145 // structure to twice that size to keep the load factor low in the hash table.
146 BlockInfo.reserve(NumBlocks);
147 size_t NumInsts = 0;
148
149 // Iterate over blocks and initialize BlockInfoVec entries, count
150 // instructions to size the InstInfo hash table.
151 for (auto &BB : F) {
152 NumInsts += BB.size();
153 auto &Info = BlockInfo[&BB];
154 Info.BB = &BB;
155 Info.Terminator = BB.getTerminator();
156 Info.UnconditionalBranch = isUnconditionalBranch(Info.Terminator);
157 }
158
159 // Initialize instruction map and set pointers to block info.
160 InstInfo.reserve(NumInsts);
161 for (auto &BBInfo : BlockInfo)
162 for (Instruction &I : *BBInfo.second.BB)
163 InstInfo[&I].Block = &BBInfo.second;
164
165 // Since BlockInfoVec holds pointers into InstInfo and vice-versa, we may not
166 // add any more elements to either after this point.
167 for (auto &BBInfo : BlockInfo)
168 BBInfo.second.TerminatorLiveInfo = &InstInfo[BBInfo.second.Terminator];
169
David Callahan45e442e2016-08-05 19:38:11 +0000170 // Collect the set of "root" instructions that are known live.
171 for (Instruction &I : instructions(F))
172 if (isAlwaysLive(I))
David Callahan947be0f2016-08-16 14:31:51 +0000173 markLive(&I);
174
175 if (!RemoveControlFlowFlag)
176 return;
177
178 // This is temporary: will update with post order traveral to
179 // find loop bottoms
180 SmallPtrSet<BasicBlock *, 16> Seen;
181 for (auto &BB : F) {
182 Seen.insert(&BB);
183 TerminatorInst *Term = BB.getTerminator();
184 if (isLive(Term))
185 continue;
186
187 for (auto Succ : successors(&BB))
188 if (Seen.count(Succ)) {
189 // back edge....
190 markLive(Term);
191 break;
192 }
193 }
194 // end temporary handling of loops
195
196 // Treat the entry block as always live
197 auto *BB = &F.getEntryBlock();
198 auto &EntryInfo = BlockInfo[BB];
199 EntryInfo.Live = true;
200 if (EntryInfo.UnconditionalBranch)
201 markLive(EntryInfo.Terminator);
202
203 // Build initial collection of blocks with dead terminators
204 for (auto &BBInfo : BlockInfo)
205 if (!BBInfo.second.terminatorIsLive())
206 BlocksWithDeadTerminators.insert(BBInfo.second.BB);
David Callahan45e442e2016-08-05 19:38:11 +0000207}
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000208
David Callahan45e442e2016-08-05 19:38:11 +0000209bool AggressiveDeadCodeElimination::isAlwaysLive(Instruction &I) {
David Callahan45e442e2016-08-05 19:38:11 +0000210 // TODO -- use llvm::isInstructionTriviallyDead
David Callahan947be0f2016-08-16 14:31:51 +0000211 if (I.isEHPad() || I.mayHaveSideEffects()) {
David Callahan45e442e2016-08-05 19:38:11 +0000212 // Skip any value profile instrumentation calls if they are
213 // instrumenting constants.
David Callahan947be0f2016-08-16 14:31:51 +0000214 if (isInstrumentsConstant(I))
215 return false;
216 return true;
David Callahan45e442e2016-08-05 19:38:11 +0000217 }
David Callahan947be0f2016-08-16 14:31:51 +0000218 if (!isa<TerminatorInst>(I))
219 return false;
220 if (RemoveControlFlowFlag && (isa<BranchInst>(I) || isa<SwitchInst>(I)))
221 return false;
222 return true;
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000223}
224
Betul Buyukkurtbf8554c2016-04-13 18:52:19 +0000225// Check if this instruction is a runtime call for value profiling and
226// if it's instrumenting a constant.
David Callahan45e442e2016-08-05 19:38:11 +0000227bool AggressiveDeadCodeElimination::isInstrumentsConstant(Instruction &I) {
228 // TODO -- move this test into llvm::isInstructionTriviallyDead
Betul Buyukkurtbf8554c2016-04-13 18:52:19 +0000229 if (CallInst *CI = dyn_cast<CallInst>(&I))
230 if (Function *Callee = CI->getCalledFunction())
231 if (Callee->getName().equals(getInstrProfValueProfFuncName()))
232 if (isa<Constant>(CI->getArgOperand(0)))
233 return true;
234 return false;
235}
236
David Callahan45e442e2016-08-05 19:38:11 +0000237void AggressiveDeadCodeElimination::markLiveInstructions() {
Nadav Rotem465834c2012-07-24 10:51:42 +0000238
David Callahan947be0f2016-08-16 14:31:51 +0000239 // Propagate liveness backwards to operands.
240 do {
241 // Worklist holds newly discovered live instructions
242 // where we need to mark the inputs as live.
243 while (!Worklist.empty()) {
244 Instruction *LiveInst = Worklist.pop_back_val();
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000245
David Callahan947be0f2016-08-16 14:31:51 +0000246 // Collect the live debug info scopes attached to this instruction.
247 if (const DILocation *DL = LiveInst->getDebugLoc())
248 collectLiveScopes(*DL);
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000249
David Callahan947be0f2016-08-16 14:31:51 +0000250 DEBUG(dbgs() << "work live: "; LiveInst->dump(););
251 for (Use &OI : LiveInst->operands())
252 if (Instruction *Inst = dyn_cast<Instruction>(OI))
253 markLive(Inst);
Hal Finkel8626ed22015-02-15 15:51:25 +0000254 }
David Callahan947be0f2016-08-16 14:31:51 +0000255 markLiveBranchesFromControlDependences();
256 // TODO -- handle PhiNodes
257 } while (!Worklist.empty());
258
259 // temporary until control dependences are implemented
260 assert(BlocksWithDeadTerminators.empty());
261}
262
263void AggressiveDeadCodeElimination::markLive(Instruction *I) {
264
265 auto &Info = InstInfo[I];
266 if (Info.Live)
267 return;
268
269 DEBUG(dbgs() << "mark live: "; I->dump());
270 Info.Live = true;
271 Worklist.push_back(I);
272
273 // Mark the containing block live
274 auto &BBInfo = *Info.Block;
275 if (BBInfo.Terminator == I)
276 BlocksWithDeadTerminators.erase(BBInfo.BB);
277 if (BBInfo.Live)
278 return;
279
280 DEBUG(dbgs() << "mark block live: " << BBInfo.BB->getName() << '\n');
281 BBInfo.Live = true;
282 NewLiveBlocks.insert(BBInfo.BB);
283
284 // Mark unconditional branches at the end of live
285 // blocks as live since there is no work to do for them later
286 if (BBInfo.UnconditionalBranch && I != BBInfo.Terminator)
287 markLive(BBInfo.Terminator);
David Callahan45e442e2016-08-05 19:38:11 +0000288}
289
290void AggressiveDeadCodeElimination::collectLiveScopes(const DILocalScope &LS) {
291 if (!AliveScopes.insert(&LS).second)
292 return;
David Callahan947be0f2016-08-16 14:31:51 +0000293
David Callahan45e442e2016-08-05 19:38:11 +0000294 if (isa<DISubprogram>(LS))
295 return;
David Callahan947be0f2016-08-16 14:31:51 +0000296
David Callahan45e442e2016-08-05 19:38:11 +0000297 // Tail-recurse through the scope chain.
298 collectLiveScopes(cast<DILocalScope>(*LS.getScope()));
299}
300
301void AggressiveDeadCodeElimination::collectLiveScopes(const DILocation &DL) {
302 // Even though DILocations are not scopes, shove them into AliveScopes so we
303 // don't revisit them.
304 if (!AliveScopes.insert(&DL).second)
305 return;
David Callahan947be0f2016-08-16 14:31:51 +0000306
David Callahan45e442e2016-08-05 19:38:11 +0000307 // Collect live scopes from the scope chain.
308 collectLiveScopes(*DL.getScope());
David Callahan947be0f2016-08-16 14:31:51 +0000309
David Callahan45e442e2016-08-05 19:38:11 +0000310 // Tail-recurse through the inlined-at chain.
311 if (const DILocation *IA = DL.getInlinedAt())
312 collectLiveScopes(*IA);
313}
314
David Callahan947be0f2016-08-16 14:31:51 +0000315void AggressiveDeadCodeElimination::markLiveBranchesFromControlDependences() {
316
317 // This is a place holder, mark all read operations live
318 // The next patch will replace this with using iterated dominance
319 // frontier to compute branches that need to be live because they
320 // control live blocks with live operations
321 SmallVector<TerminatorInst *, 16> DeadTerminators;
322 for (auto *BB : BlocksWithDeadTerminators)
323 DeadTerminators.push_back(BB->getTerminator());
324 for (auto I : DeadTerminators)
325 markLive(I);
326 NewLiveBlocks.clear();
David Callahan45e442e2016-08-05 19:38:11 +0000327}
328
329bool AggressiveDeadCodeElimination::removeDeadInstructions() {
Nadav Rotem465834c2012-07-24 10:51:42 +0000330
Owen Anderson7686b552008-05-29 08:45:13 +0000331 // The inverse of the live set is the dead set. These are those instructions
332 // which have no side effects and do not influence the control flow or return
333 // value of the function, and may therefore be deleted safely.
Hal Finkel75901292015-02-15 15:45:28 +0000334 // NOTE: We reuse the Worklist vector here for memory efficiency.
Nico Rieck78199512015-08-06 19:10:45 +0000335 for (Instruction &I : instructions(F)) {
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000336 // Check if the instruction is alive.
David Callahan947be0f2016-08-16 14:31:51 +0000337 if (isLive(&I))
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000338 continue;
339
David Callahan947be0f2016-08-16 14:31:51 +0000340 assert(!I.isTerminator() && "NYI: Removing Control Flow");
341
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000342 if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) {
343 // Check if the scope of this variable location is alive.
344 if (AliveScopes.count(DII->getDebugLoc()->getScope()))
345 continue;
346
347 // Fallthrough and drop the intrinsic.
348 DEBUG({
349 // If intrinsic is pointing at a live SSA value, there may be an
350 // earlier optimization bug: if we know the location of the variable,
351 // why isn't the scope of the location alive?
352 if (Value *V = DII->getVariableLocation())
353 if (Instruction *II = dyn_cast<Instruction>(V))
David Callahan947be0f2016-08-16 14:31:51 +0000354 if (isLive(II))
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000355 dbgs() << "Dropping debug info for " << *DII << "\n";
356 });
Chris Lattneracfd27d2001-09-09 22:26:47 +0000357 }
Duncan P. N. Exon Smithe8eb94a2016-03-29 22:57:12 +0000358
359 // Prepare to delete.
360 Worklist.push_back(&I);
361 I.dropAllReferences();
Hal Finkel92fb2d32015-02-15 15:51:23 +0000362 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000363
Hal Finkel92fb2d32015-02-15 15:51:23 +0000364 for (Instruction *&I : Worklist) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000365 ++NumRemoved;
Hal Finkel92fb2d32015-02-15 15:51:23 +0000366 I->eraseFromParent();
Chris Lattneracfd27d2001-09-09 22:26:47 +0000367 }
Devang Patel53b39b52008-11-11 00:54:10 +0000368
Hal Finkel75901292015-02-15 15:45:28 +0000369 return !Worklist.empty();
Chris Lattnerb28986f2001-06-30 06:39:11 +0000370}
Owen Anderson7686b552008-05-29 08:45:13 +0000371
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000372PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &) {
David Callahan45e442e2016-08-05 19:38:11 +0000373 if (!AggressiveDeadCodeElimination(F).performDeadCodeElimination())
Davide Italiano688616f2016-05-31 17:39:39 +0000374 return PreservedAnalyses::all();
375
Michael Kuperstein835facd2016-06-28 00:54:12 +0000376 // FIXME: This should also 'preserve the CFG'.
Davide Italiano688616f2016-05-31 17:39:39 +0000377 auto PA = PreservedAnalyses();
378 PA.preserve<GlobalsAA>();
379 return PA;
Duncan Sands9e064a22008-05-29 14:38:23 +0000380}
Justin Bogner19b67992015-10-30 23:13:18 +0000381
382namespace {
383struct ADCELegacyPass : public FunctionPass {
384 static char ID; // Pass identification, replacement for typeid
385 ADCELegacyPass() : FunctionPass(ID) {
386 initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
387 }
388
David Callahancc5cd4d2016-08-03 04:28:39 +0000389 bool runOnFunction(Function &F) override {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000390 if (skipFunction(F))
Justin Bogner19b67992015-10-30 23:13:18 +0000391 return false;
David Callahan45e442e2016-08-05 19:38:11 +0000392 return AggressiveDeadCodeElimination(F).performDeadCodeElimination();
Justin Bogner19b67992015-10-30 23:13:18 +0000393 }
394
David Callahancc5cd4d2016-08-03 04:28:39 +0000395 void getAnalysisUsage(AnalysisUsage &AU) const override {
Justin Bogner19b67992015-10-30 23:13:18 +0000396 AU.setPreservesCFG();
397 AU.addPreserved<GlobalsAAWrapperPass>();
398 }
399};
400}
401
402char ADCELegacyPass::ID = 0;
403INITIALIZE_PASS(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination",
404 false, false)
405
406FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); }