xref: /llvm-project/llvm/lib/Transforms/Scalar/ADCE.cpp (revision c165a4e2154125dd209ea94055d9e41cb240439a)
1 //===- ADCE.cpp - Code to perform 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 Aggressive Dead Code Elimination pass.  This pass
11 // optimistically assumes that all instructions are dead until proven otherwise,
12 // allowing it to eliminate dead computations that other DCE passes do not
13 // catch, particularly involving loop computations.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/Transforms/Scalar/ADCE.h"
18 
19 #include "llvm/ADT/DepthFirstIterator.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Analysis/GlobalsModRef.h"
24 #include "llvm/Analysis/IteratedDominanceFrontier.h"
25 #include "llvm/Analysis/PostDominators.h"
26 #include "llvm/IR/BasicBlock.h"
27 #include "llvm/IR/CFG.h"
28 #include "llvm/IR/DebugInfoMetadata.h"
29 #include "llvm/IR/InstIterator.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/Pass.h"
33 #include "llvm/ProfileData/InstrProf.h"
34 #include "llvm/Transforms/Scalar.h"
35 using namespace llvm;
36 
37 #define DEBUG_TYPE "adce"
38 
39 STATISTIC(NumRemoved, "Number of instructions removed");
40 
41 // This is a tempoary option until we change the interface
42 // to this pass based on optimization level.
43 static cl::opt<bool> RemoveControlFlowFlag("adce-remove-control-flow",
44                                            cl::init(false), cl::Hidden);
45 
46 namespace {
47 /// Information about Instructions
48 struct InstInfoType {
49   /// True if the associated instruction is live.
50   bool Live = false;
51   /// Quick access to information for block containing associated Instruction.
52   struct BlockInfoType *Block = nullptr;
53 };
54 
55 /// Information about basic blocks relevant to dead code elimination.
56 struct BlockInfoType {
57   /// True when this block contains a live instructions.
58   bool Live = false;
59   /// True when this block ends in an unconditional branch.
60   bool UnconditionalBranch = false;
61   /// True when this block is known to have live PHI nodes.
62   bool HasLivePhiNodes = false;
63   /// Control dependence sources need to be live for this block.
64   bool CFLive = false;
65 
66   /// Quick access to the LiveInfo for the terminator,
67   /// holds the value &InstInfo[Terminator]
68   InstInfoType *TerminatorLiveInfo = nullptr;
69 
70   bool terminatorIsLive() const { return TerminatorLiveInfo->Live; }
71 
72   /// Corresponding BasicBlock.
73   BasicBlock *BB = nullptr;
74 
75   /// Cache of BB->getTerminator()
76   TerminatorInst *Terminator = nullptr;
77 };
78 
79 class AggressiveDeadCodeElimination {
80   Function &F;
81   PostDominatorTree &PDT;
82 
83   /// Mapping of blocks to associated information, an element in BlockInfoVec.
84   DenseMap<BasicBlock *, BlockInfoType> BlockInfo;
85   bool isLive(BasicBlock *BB) { return BlockInfo[BB].Live; }
86 
87   /// Mapping of instructions to associated information.
88   DenseMap<Instruction *, InstInfoType> InstInfo;
89   bool isLive(Instruction *I) { return InstInfo[I].Live; }
90 
91   /// Instructions known to be live where we need to mark
92   /// reaching definitions as live.
93   SmallVector<Instruction *, 128> Worklist;
94   /// Debug info scopes around a live instruction.
95   SmallPtrSet<const Metadata *, 32> AliveScopes;
96 
97   /// Set of blocks with not known to have live terminators.
98   SmallPtrSet<BasicBlock *, 16> BlocksWithDeadTerminators;
99 
100   /// The set of blocks which we have determined are live in the
101   /// most recent iteration of propagating liveness.
102   SmallPtrSet<BasicBlock *, 16> NewLiveBlocks;
103 
104   /// Set up auxiliary data structures for Instructions and BasicBlocks and
105   /// initialize the Worklist to the set of must-be-live Instruscions.
106   void initialize();
107   /// Return true for operations which are always treated as live.
108   bool isAlwaysLive(Instruction &I);
109   /// Return true for instrumentation instructions for value profiling.
110   bool isInstrumentsConstant(Instruction &I);
111 
112   /// Propagate liveness to reaching definitions.
113   void markLiveInstructions();
114   /// Mark an instruction as live.
115   void markLive(Instruction *I);
116 
117   /// Mark terminators of control predecessors of a PHI node live.
118   void markPhiLive(PHINode *PN);
119 
120   /// Record the Debug Scopes which surround live debug information.
121   void collectLiveScopes(const DILocalScope &LS);
122   void collectLiveScopes(const DILocation &DL);
123 
124   /// Analyze dead branches to find those whose branches are the sources
125   /// of control dependences impacting a live block. Those branches are
126   /// marked live.
127   void markLiveBranchesFromControlDependences();
128 
129   /// Remove instructions not marked live, return if any any instruction
130   /// was removed.
131   bool removeDeadInstructions();
132 
133 public:
134   AggressiveDeadCodeElimination(Function &F, PostDominatorTree &PDT)
135       : F(F), PDT(PDT) {}
136   bool performDeadCodeElimination();
137 };
138 }
139 
140 bool AggressiveDeadCodeElimination::performDeadCodeElimination() {
141   initialize();
142   markLiveInstructions();
143   return removeDeadInstructions();
144 }
145 
146 static bool isUnconditionalBranch(TerminatorInst *Term) {
147   auto BR = dyn_cast<BranchInst>(Term);
148   return BR && BR->isUnconditional();
149 }
150 
151 void AggressiveDeadCodeElimination::initialize() {
152 
153   auto NumBlocks = F.size();
154 
155   // We will have an entry in the map for each block so we grow the
156   // structure to twice that size to keep the load factor low in the hash table.
157   BlockInfo.reserve(NumBlocks);
158   size_t NumInsts = 0;
159 
160   // Iterate over blocks and initialize BlockInfoVec entries, count
161   // instructions to size the InstInfo hash table.
162   for (auto &BB : F) {
163     NumInsts += BB.size();
164     auto &Info = BlockInfo[&BB];
165     Info.BB = &BB;
166     Info.Terminator = BB.getTerminator();
167     Info.UnconditionalBranch = isUnconditionalBranch(Info.Terminator);
168   }
169 
170   // Initialize instruction map and set pointers to block info.
171   InstInfo.reserve(NumInsts);
172   for (auto &BBInfo : BlockInfo)
173     for (Instruction &I : *BBInfo.second.BB)
174       InstInfo[&I].Block = &BBInfo.second;
175 
176   // Since BlockInfoVec holds pointers into InstInfo and vice-versa, we may not
177   // add any more elements to either after this point.
178   for (auto &BBInfo : BlockInfo)
179     BBInfo.second.TerminatorLiveInfo = &InstInfo[BBInfo.second.Terminator];
180 
181   // Collect the set of "root" instructions that are known live.
182   for (Instruction &I : instructions(F))
183     if (isAlwaysLive(I))
184       markLive(&I);
185 
186   if (!RemoveControlFlowFlag)
187     return;
188 
189   // This is temporary: will update with post order traveral to
190   // find loop bottoms
191   SmallPtrSet<BasicBlock *, 16> Seen;
192   for (auto &BB : F) {
193     Seen.insert(&BB);
194     TerminatorInst *Term = BB.getTerminator();
195     if (isLive(Term))
196       continue;
197 
198     for (auto Succ : successors(&BB))
199       if (Seen.count(Succ)) {
200         // back edge....
201         markLive(Term);
202         break;
203       }
204   }
205   // End temporary handling of loops.
206 
207   // Mark blocks live if there is no path from the block to the
208   // return of the function or a successor for which this is true.
209   // This protects IDFCalculator which cannot handle such blocks.
210   for (auto &BBInfoPair : BlockInfo) {
211     auto &BBInfo = BBInfoPair.second;
212     if (BBInfo.terminatorIsLive())
213       continue;
214     auto *BB = BBInfo.BB;
215     if (!PDT.getNode(BB)) {
216       DEBUG(dbgs() << "Not post-dominated by return: " << BB->getName()
217                    << '\n';);
218       markLive(BBInfo.Terminator);
219       continue;
220     }
221     for (auto Succ : successors(BB))
222       if (!PDT.getNode(Succ)) {
223         DEBUG(dbgs() << "Successor not post-dominated by return: "
224                      << BB->getName() << '\n';);
225         markLive(BBInfo.Terminator);
226         break;
227       }
228   }
229 
230   // Treat the entry block as always live
231   auto *BB = &F.getEntryBlock();
232   auto &EntryInfo = BlockInfo[BB];
233   EntryInfo.Live = true;
234   if (EntryInfo.UnconditionalBranch)
235     markLive(EntryInfo.Terminator);
236 
237   // Build initial collection of blocks with dead terminators
238   for (auto &BBInfo : BlockInfo)
239     if (!BBInfo.second.terminatorIsLive())
240       BlocksWithDeadTerminators.insert(BBInfo.second.BB);
241 }
242 
243 bool AggressiveDeadCodeElimination::isAlwaysLive(Instruction &I) {
244   // TODO -- use llvm::isInstructionTriviallyDead
245   if (I.isEHPad() || I.mayHaveSideEffects()) {
246     // Skip any value profile instrumentation calls if they are
247     // instrumenting constants.
248     if (isInstrumentsConstant(I))
249       return false;
250     return true;
251   }
252   if (!isa<TerminatorInst>(I))
253     return false;
254   if (RemoveControlFlowFlag && (isa<BranchInst>(I) || isa<SwitchInst>(I)))
255     return false;
256   return true;
257 }
258 
259 // Check if this instruction is a runtime call for value profiling and
260 // if it's instrumenting a constant.
261 bool AggressiveDeadCodeElimination::isInstrumentsConstant(Instruction &I) {
262   // TODO -- move this test into llvm::isInstructionTriviallyDead
263   if (CallInst *CI = dyn_cast<CallInst>(&I))
264     if (Function *Callee = CI->getCalledFunction())
265       if (Callee->getName().equals(getInstrProfValueProfFuncName()))
266         if (isa<Constant>(CI->getArgOperand(0)))
267           return true;
268   return false;
269 }
270 
271 void AggressiveDeadCodeElimination::markLiveInstructions() {
272 
273   // Propagate liveness backwards to operands.
274   do {
275     // Worklist holds newly discovered live instructions
276     // where we need to mark the inputs as live.
277     while (!Worklist.empty()) {
278       Instruction *LiveInst = Worklist.pop_back_val();
279       DEBUG(dbgs() << "work live: "; LiveInst->dump(););
280 
281       // Collect the live debug info scopes attached to this instruction.
282       if (const DILocation *DL = LiveInst->getDebugLoc())
283         collectLiveScopes(*DL);
284 
285       for (Use &OI : LiveInst->operands())
286         if (Instruction *Inst = dyn_cast<Instruction>(OI))
287           markLive(Inst);
288 
289       if (auto *PN = dyn_cast<PHINode>(LiveInst))
290         markPhiLive(PN);
291     }
292     markLiveBranchesFromControlDependences();
293 
294     if (Worklist.empty()) {
295       // Temporary until we can actually delete branches.
296       SmallVector<TerminatorInst *, 16> DeadTerminators;
297       for (auto *BB : BlocksWithDeadTerminators)
298         DeadTerminators.push_back(BB->getTerminator());
299       for (auto *I : DeadTerminators)
300         markLive(I);
301       assert(BlocksWithDeadTerminators.empty());
302       // End temporary.
303     }
304   } while (!Worklist.empty());
305 
306   assert(BlocksWithDeadTerminators.empty());
307 }
308 
309 void AggressiveDeadCodeElimination::markLive(Instruction *I) {
310 
311   auto &Info = InstInfo[I];
312   if (Info.Live)
313     return;
314 
315   DEBUG(dbgs() << "mark live: "; I->dump());
316   Info.Live = true;
317   Worklist.push_back(I);
318 
319   // Mark the containing block live
320   auto &BBInfo = *Info.Block;
321   if (BBInfo.Terminator == I)
322     BlocksWithDeadTerminators.erase(BBInfo.BB);
323   if (BBInfo.Live)
324     return;
325 
326   DEBUG(dbgs() << "mark block live: " << BBInfo.BB->getName() << '\n');
327   BBInfo.Live = true;
328   if (!BBInfo.CFLive) {
329     BBInfo.CFLive = true;
330     NewLiveBlocks.insert(BBInfo.BB);
331   }
332 
333   // Mark unconditional branches at the end of live
334   // blocks as live since there is no work to do for them later
335   if (BBInfo.UnconditionalBranch && I != BBInfo.Terminator)
336     markLive(BBInfo.Terminator);
337 }
338 
339 void AggressiveDeadCodeElimination::collectLiveScopes(const DILocalScope &LS) {
340   if (!AliveScopes.insert(&LS).second)
341     return;
342 
343   if (isa<DISubprogram>(LS))
344     return;
345 
346   // Tail-recurse through the scope chain.
347   collectLiveScopes(cast<DILocalScope>(*LS.getScope()));
348 }
349 
350 void AggressiveDeadCodeElimination::collectLiveScopes(const DILocation &DL) {
351   // Even though DILocations are not scopes, shove them into AliveScopes so we
352   // don't revisit them.
353   if (!AliveScopes.insert(&DL).second)
354     return;
355 
356   // Collect live scopes from the scope chain.
357   collectLiveScopes(*DL.getScope());
358 
359   // Tail-recurse through the inlined-at chain.
360   if (const DILocation *IA = DL.getInlinedAt())
361     collectLiveScopes(*IA);
362 }
363 
364 void AggressiveDeadCodeElimination::markPhiLive(PHINode *PN) {
365   auto &Info = BlockInfo[PN->getParent()];
366   // Only need to check this once per block.
367   if (Info.HasLivePhiNodes)
368     return;
369   Info.HasLivePhiNodes = true;
370 
371   // If a predecessor block is not live, mark it as control-flow live
372   // which will trigger marking live branches upon which
373   // that block is control dependent.
374   for (auto *PredBB : predecessors(Info.BB)) {
375     auto &Info = BlockInfo[PredBB];
376     if (!Info.CFLive) {
377       Info.CFLive = true;
378       NewLiveBlocks.insert(PredBB);
379     }
380   }
381 }
382 
383 void AggressiveDeadCodeElimination::markLiveBranchesFromControlDependences() {
384 
385   if (BlocksWithDeadTerminators.empty())
386     return;
387 
388   DEBUG({
389     dbgs() << "new live blocks:\n";
390     for (auto *BB : NewLiveBlocks)
391       dbgs() << "\t" << BB->getName() << '\n';
392     dbgs() << "dead terminator blocks:\n";
393     for (auto *BB : BlocksWithDeadTerminators)
394       dbgs() << "\t" << BB->getName() << '\n';
395   });
396 
397   // The dominance frontier of a live block X in the reverse
398   // control graph is the set of blocks upon which X is control
399   // dependent. The following sequence computes the set of blocks
400   // which currently have dead terminators that are control
401   // dependence sources of a block which is in NewLiveBlocks.
402 
403   SmallVector<BasicBlock *, 32> IDFBlocks;
404   ReverseIDFCalculator IDFs(PDT);
405   IDFs.setDefiningBlocks(NewLiveBlocks);
406   IDFs.setLiveInBlocks(BlocksWithDeadTerminators);
407   IDFs.calculate(IDFBlocks);
408   NewLiveBlocks.clear();
409 
410   // Dead terminators which control live blocks are now marked live.
411   for (auto BB : IDFBlocks) {
412     DEBUG(dbgs() << "live control in: " << BB->getName() << '\n');
413     markLive(BB->getTerminator());
414   }
415 }
416 
417 //===----------------------------------------------------------------------===//
418 //
419 //  Routines to update the CFG and SSA information before removing dead code.
420 //
421 //===----------------------------------------------------------------------===//
422 bool AggressiveDeadCodeElimination::removeDeadInstructions() {
423 
424   // The inverse of the live set is the dead set.  These are those instructions
425   // which have no side effects and do not influence the control flow or return
426   // value of the function, and may therefore be deleted safely.
427   // NOTE: We reuse the Worklist vector here for memory efficiency.
428   for (Instruction &I : instructions(F)) {
429     // Check if the instruction is alive.
430     if (isLive(&I))
431       continue;
432 
433     assert(!I.isTerminator() && "NYI: Removing Control Flow");
434 
435     if (auto *DII = dyn_cast<DbgInfoIntrinsic>(&I)) {
436       // Check if the scope of this variable location is alive.
437       if (AliveScopes.count(DII->getDebugLoc()->getScope()))
438         continue;
439 
440       // Fallthrough and drop the intrinsic.
441       DEBUG({
442         // If intrinsic is pointing at a live SSA value, there may be an
443         // earlier optimization bug: if we know the location of the variable,
444         // why isn't the scope of the location alive?
445         if (Value *V = DII->getVariableLocation())
446           if (Instruction *II = dyn_cast<Instruction>(V))
447             if (isLive(II))
448               dbgs() << "Dropping debug info for " << *DII << "\n";
449       });
450     }
451 
452     // Prepare to delete.
453     Worklist.push_back(&I);
454     I.dropAllReferences();
455   }
456 
457   for (Instruction *&I : Worklist) {
458     ++NumRemoved;
459     I->eraseFromParent();
460   }
461 
462   return !Worklist.empty();
463 }
464 
465 //===----------------------------------------------------------------------===//
466 //
467 // Pass Manager integration code
468 //
469 //===----------------------------------------------------------------------===//
470 PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &FAM) {
471   auto &PDT = FAM.getResult<PostDominatorTreeAnalysis>(F);
472   if (!AggressiveDeadCodeElimination(F, PDT).performDeadCodeElimination())
473     return PreservedAnalyses::all();
474 
475   // FIXME: This should also 'preserve the CFG'.
476   auto PA = PreservedAnalyses();
477   PA.preserve<GlobalsAA>();
478   return PA;
479 }
480 
481 namespace {
482 struct ADCELegacyPass : public FunctionPass {
483   static char ID; // Pass identification, replacement for typeid
484   ADCELegacyPass() : FunctionPass(ID) {
485     initializeADCELegacyPassPass(*PassRegistry::getPassRegistry());
486   }
487 
488   bool runOnFunction(Function &F) override {
489     if (skipFunction(F))
490       return false;
491     auto &PDT = getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
492     return AggressiveDeadCodeElimination(F, PDT).performDeadCodeElimination();
493   }
494 
495   void getAnalysisUsage(AnalysisUsage &AU) const override {
496     AU.addRequired<PostDominatorTreeWrapperPass>();
497     AU.setPreservesCFG(); // TODO -- will remove when we start removing branches
498     AU.addPreserved<GlobalsAAWrapperPass>();
499   }
500 };
501 }
502 
503 char ADCELegacyPass::ID = 0;
504 INITIALIZE_PASS_BEGIN(ADCELegacyPass, "adce",
505                       "Aggressive Dead Code Elimination", false, false)
506 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
507 INITIALIZE_PASS_END(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination",
508                     false, false)
509 
510 FunctionPass *llvm::createAggressiveDCEPass() { return new ADCELegacyPass(); }
511