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