DEBUG got moved to Support/Debug.h
[oota-llvm.git] / lib / Transforms / Scalar / ADCE.cpp
1 //===- ADCE.cpp - Code to perform aggressive dead code elimination --------===//
2 //
3 // This file implements "aggressive" dead code elimination.  ADCE is DCe where
4 // values are assumed to be dead until proven otherwise.  This is similar to 
5 // SCCP, except applied to the liveness of values.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Transforms/Scalar.h"
10 #include "llvm/Transforms/Utils/Local.h"
11 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
12 #include "llvm/Type.h"
13 #include "llvm/Analysis/PostDominators.h"
14 #include "llvm/iTerminators.h"
15 #include "llvm/iPHINode.h"
16 #include "llvm/Constant.h"
17 #include "llvm/Support/CFG.h"
18 #include "Support/Debug.h"
19 #include "Support/DepthFirstIterator.h"
20 #include "Support/Statistic.h"
21 #include "Support/STLExtras.h"
22 #include <algorithm>
23
24 namespace {
25   Statistic<> NumBlockRemoved("adce", "Number of basic blocks removed");
26   Statistic<> NumInstRemoved ("adce", "Number of instructions removed");
27
28 //===----------------------------------------------------------------------===//
29 // ADCE Class
30 //
31 // This class does all of the work of Aggressive Dead Code Elimination.
32 // It's public interface consists of a constructor and a doADCE() method.
33 //
34 class ADCE : public FunctionPass {
35   Function *Func;                       // The function that we are working on
36   std::vector<Instruction*> WorkList;   // Instructions that just became live
37   std::set<Instruction*>    LiveSet;    // The set of live instructions
38
39   //===--------------------------------------------------------------------===//
40   // The public interface for this class
41   //
42 public:
43   // Execute the Aggressive Dead Code Elimination Algorithm
44   //
45   virtual bool runOnFunction(Function &F) {
46     Func = &F;
47     bool Changed = doADCE();
48     assert(WorkList.empty());
49     LiveSet.clear();
50     return Changed;
51   }
52   // getAnalysisUsage - We require post dominance frontiers (aka Control
53   // Dependence Graph)
54   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55     AU.addRequired<PostDominatorTree>();
56     AU.addRequired<PostDominanceFrontier>();
57   }
58
59
60   //===--------------------------------------------------------------------===//
61   // The implementation of this class
62   //
63 private:
64   // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning
65   // true if the function was modified.
66   //
67   bool doADCE();
68
69   void markBlockAlive(BasicBlock *BB);
70
71
72   // dropReferencesOfDeadInstructionsInLiveBlock - Loop over all of the
73   // instructions in the specified basic block, dropping references on
74   // instructions that are dead according to LiveSet.
75   bool dropReferencesOfDeadInstructionsInLiveBlock(BasicBlock *BB);
76
77   TerminatorInst *convertToUnconditionalBranch(TerminatorInst *TI);
78
79   inline void markInstructionLive(Instruction *I) {
80     if (LiveSet.count(I)) return;
81     DEBUG(std::cerr << "Insn Live: " << I);
82     LiveSet.insert(I);
83     WorkList.push_back(I);
84   }
85
86   inline void markTerminatorLive(const BasicBlock *BB) {
87     DEBUG(std::cerr << "Terminat Live: " << BB->getTerminator());
88     markInstructionLive((Instruction*)BB->getTerminator());
89   }
90 };
91
92   RegisterOpt<ADCE> X("adce", "Aggressive Dead Code Elimination");
93 } // End of anonymous namespace
94
95 Pass *createAggressiveDCEPass() { return new ADCE(); }
96
97 void ADCE::markBlockAlive(BasicBlock *BB) {
98   // Mark the basic block as being newly ALIVE... and mark all branches that
99   // this block is control dependant on as being alive also...
100   //
101   PostDominanceFrontier &CDG = getAnalysis<PostDominanceFrontier>();
102
103   PostDominanceFrontier::const_iterator It = CDG.find(BB);
104   if (It != CDG.end()) {
105     // Get the blocks that this node is control dependant on...
106     const PostDominanceFrontier::DomSetType &CDB = It->second;
107     for_each(CDB.begin(), CDB.end(),   // Mark all their terminators as live
108              bind_obj(this, &ADCE::markTerminatorLive));
109   }
110   
111   // If this basic block is live, and it ends in an unconditional branch, then
112   // the branch is alive as well...
113   if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))
114     if (BI->isUnconditional())
115       markTerminatorLive(BB);
116 }
117
118 // dropReferencesOfDeadInstructionsInLiveBlock - Loop over all of the
119 // instructions in the specified basic block, dropping references on
120 // instructions that are dead according to LiveSet.
121 bool ADCE::dropReferencesOfDeadInstructionsInLiveBlock(BasicBlock *BB) {
122   bool Changed = false;
123   for (BasicBlock::iterator I = BB->begin(), E = --BB->end(); I != E; )
124     if (!LiveSet.count(I)) {              // Is this instruction alive?
125       I->dropAllReferences();             // Nope, drop references... 
126       if (PHINode *PN = dyn_cast<PHINode>(I)) {
127         // We don't want to leave PHI nodes in the program that have
128         // #arguments != #predecessors, so we remove them now.
129         //
130         PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
131         
132         // Delete the instruction...
133         I = BB->getInstList().erase(I);
134         Changed = true;
135       } else {
136         ++I;
137       }
138     } else {
139       ++I;
140     }
141   return Changed;
142 }
143
144
145 /// convertToUnconditionalBranch - Transform this conditional terminator
146 /// instruction into an unconditional branch because we don't care which of the
147 /// successors it goes to.  This eliminate a use of the condition as well.
148 ///
149 TerminatorInst *ADCE::convertToUnconditionalBranch(TerminatorInst *TI) {
150   BranchInst *NB = new BranchInst(TI->getSuccessor(0), TI);
151   BasicBlock *BB = TI->getParent();
152
153   // Remove entries from PHI nodes to avoid confusing ourself later...
154   for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
155     TI->getSuccessor(i)->removePredecessor(BB);
156   
157   // Delete the old branch itself...
158   BB->getInstList().erase(TI);
159   return NB;
160 }
161
162
163 // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning
164 // true if the function was modified.
165 //
166 bool ADCE::doADCE() {
167   bool MadeChanges = false;
168
169   // Iterate over all of the instructions in the function, eliminating trivially
170   // dead instructions, and marking instructions live that are known to be 
171   // needed.  Perform the walk in depth first order so that we avoid marking any
172   // instructions live in basic blocks that are unreachable.  These blocks will
173   // be eliminated later, along with the instructions inside.
174   //
175   for (df_iterator<Function*> BBI = df_begin(Func), BBE = df_end(Func);
176        BBI != BBE; ++BBI) {
177     BasicBlock *BB = *BBI;
178     for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) {
179       if (II->mayWriteToMemory() || II->getOpcode() == Instruction::Ret) {
180         markInstructionLive(II);
181         ++II;  // Increment the inst iterator if the inst wasn't deleted
182       } else if (isInstructionTriviallyDead(II)) {
183         // Remove the instruction from it's basic block...
184         II = BB->getInstList().erase(II);
185         ++NumInstRemoved;
186         MadeChanges = true;
187       } else {
188         ++II;  // Increment the inst iterator if the inst wasn't deleted
189       }
190     }
191   }
192
193   // Check to ensure we have an exit node for this CFG.  If we don't, we won't
194   // have any post-dominance information, thus we cannot perform our
195   // transformations safely.
196   //
197   PostDominatorTree &DT = getAnalysis<PostDominatorTree>();
198   if (DT[&Func->getEntryNode()] == 0) {
199     WorkList.clear();
200     return MadeChanges;
201   }
202
203   DEBUG(std::cerr << "Processing work list\n");
204
205   // AliveBlocks - Set of basic blocks that we know have instructions that are
206   // alive in them...
207   //
208   std::set<BasicBlock*> AliveBlocks;
209
210   // Process the work list of instructions that just became live... if they
211   // became live, then that means that all of their operands are neccesary as
212   // well... make them live as well.
213   //
214   while (!WorkList.empty()) {
215     Instruction *I = WorkList.back(); // Get an instruction that became live...
216     WorkList.pop_back();
217
218     BasicBlock *BB = I->getParent();
219     if (!AliveBlocks.count(BB)) {     // Basic block not alive yet...
220       AliveBlocks.insert(BB);         // Block is now ALIVE!
221       markBlockAlive(BB);             // Make it so now!
222     }
223
224     // PHI nodes are a special case, because the incoming values are actually
225     // defined in the predecessor nodes of this block, meaning that the PHI
226     // makes the predecessors alive.
227     //
228     if (PHINode *PN = dyn_cast<PHINode>(I))
229       for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI)
230         if (!AliveBlocks.count(*PI)) {
231           AliveBlocks.insert(BB);         // Block is now ALIVE!
232           markBlockAlive(*PI);
233         }
234
235     // Loop over all of the operands of the live instruction, making sure that
236     // they are known to be alive as well...
237     //
238     for (unsigned op = 0, End = I->getNumOperands(); op != End; ++op)
239       if (Instruction *Operand = dyn_cast<Instruction>(I->getOperand(op)))
240         markInstructionLive(Operand);
241   }
242
243   DEBUG(
244     std::cerr << "Current Function: X = Live\n";
245     for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I){
246       std::cerr << I->getName() << ":\t"
247                 << (AliveBlocks.count(I) ? "LIVE\n" : "DEAD\n");
248       for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI){
249         if (LiveSet.count(BI)) std::cerr << "X ";
250         std::cerr << *BI;
251       }
252     });
253
254   // Find the first postdominator of the entry node that is alive.  Make it the
255   // new entry node...
256   //
257   if (AliveBlocks.size() == Func->size()) {  // No dead blocks?
258     for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) {
259       // Loop over all of the instructions in the function, telling dead
260       // instructions to drop their references.  This is so that the next sweep
261       // over the program can safely delete dead instructions without other dead
262       // instructions still refering to them.
263       //
264       dropReferencesOfDeadInstructionsInLiveBlock(I);
265
266       // Check to make sure the terminator instruction is live.  If it isn't,
267       // this means that the condition that it branches on (we know it is not an
268       // unconditional branch), is not needed to make the decision of where to
269       // go to, because all outgoing edges go to the same place.  We must remove
270       // the use of the condition (because it's probably dead), so we convert
271       // the terminator to a conditional branch.
272       //
273       TerminatorInst *TI = I->getTerminator();
274       if (!LiveSet.count(TI))
275         convertToUnconditionalBranch(TI);
276     }
277     
278   } else {                                   // If there are some blocks dead...
279     // If the entry node is dead, insert a new entry node to eliminate the entry
280     // node as a special case.
281     //
282     if (!AliveBlocks.count(&Func->front())) {
283       BasicBlock *NewEntry = new BasicBlock();
284       NewEntry->getInstList().push_back(new BranchInst(&Func->front()));
285       Func->getBasicBlockList().push_front(NewEntry);
286       AliveBlocks.insert(NewEntry);    // This block is always alive!
287       LiveSet.insert(NewEntry->getTerminator());  // The branch is live
288     }
289     
290     // Loop over all of the alive blocks in the function.  If any successor
291     // blocks are not alive, we adjust the outgoing branches to branch to the
292     // first live postdominator of the live block, adjusting any PHI nodes in
293     // the block to reflect this.
294     //
295     for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I)
296       if (AliveBlocks.count(I)) {
297         BasicBlock *BB = I;
298         TerminatorInst *TI = BB->getTerminator();
299       
300         // If the terminator instruction is alive, but the block it is contained
301         // in IS alive, this means that this terminator is a conditional branch
302         // on a condition that doesn't matter.  Make it an unconditional branch
303         // to ONE of the successors.  This has the side effect of dropping a use
304         // of the conditional value, which may also be dead.
305         if (!LiveSet.count(TI))
306           TI = convertToUnconditionalBranch(TI);
307
308         // Loop over all of the successors, looking for ones that are not alive.
309         // We cannot save the number of successors in the terminator instruction
310         // here because we may remove them if we don't have a postdominator...
311         //
312         for (unsigned i = 0; i != TI->getNumSuccessors(); ++i)
313           if (!AliveBlocks.count(TI->getSuccessor(i))) {
314             // Scan up the postdominator tree, looking for the first
315             // postdominator that is alive, and the last postdominator that is
316             // dead...
317             //
318             PostDominatorTree::Node *LastNode = DT[TI->getSuccessor(i)];
319
320             // There is a special case here... if there IS no post-dominator for
321             // the block we have no owhere to point our branch to.  Instead,
322             // convert it to a return.  This can only happen if the code
323             // branched into an infinite loop.  Note that this may not be
324             // desirable, because we _are_ altering the behavior of the code.
325             // This is a well known drawback of ADCE, so in the future if we
326             // choose to revisit the decision, this is where it should be.
327             //
328             if (LastNode == 0) {        // No postdominator!
329               // Call RemoveSuccessor to transmogrify the terminator instruction
330               // to not contain the outgoing branch, or to create a new
331               // terminator if the form fundementally changes (ie unconditional
332               // branch to return).  Note that this will change a branch into an
333               // infinite loop into a return instruction!
334               //
335               RemoveSuccessor(TI, i);
336
337               // RemoveSuccessor may replace TI... make sure we have a fresh
338               // pointer... and e variable.
339               //
340               TI = BB->getTerminator();
341
342               // Rescan this successor...
343               --i;
344             } else {
345               PostDominatorTree::Node *NextNode = LastNode->getIDom();
346
347               while (!AliveBlocks.count(NextNode->getNode())) {
348                 LastNode = NextNode;
349                 NextNode = NextNode->getIDom();
350               }
351             
352               // Get the basic blocks that we need...
353               BasicBlock *LastDead = LastNode->getNode();
354               BasicBlock *NextAlive = NextNode->getNode();
355
356               // Make the conditional branch now go to the next alive block...
357               TI->getSuccessor(i)->removePredecessor(BB);
358               TI->setSuccessor(i, NextAlive);
359
360               // If there are PHI nodes in NextAlive, we need to add entries to
361               // the PHI nodes for the new incoming edge.  The incoming values
362               // should be identical to the incoming values for LastDead.
363               //
364               for (BasicBlock::iterator II = NextAlive->begin();
365                    PHINode *PN = dyn_cast<PHINode>(II); ++II)
366                 if (LiveSet.count(PN)) {  // Only modify live phi nodes
367                   // Get the incoming value for LastDead...
368                   int OldIdx = PN->getBasicBlockIndex(LastDead);
369                   assert(OldIdx != -1 &&"LastDead is not a pred of NextAlive!");
370                   Value *InVal = PN->getIncomingValue(OldIdx);
371                   
372                   // Add an incoming value for BB now...
373                   PN->addIncoming(InVal, BB);
374                 }
375             }
376           }
377
378         // Now loop over all of the instructions in the basic block, telling
379         // dead instructions to drop their references.  This is so that the next
380         // sweep over the program can safely delete dead instructions without
381         // other dead instructions still refering to them.
382         //
383         dropReferencesOfDeadInstructionsInLiveBlock(BB);
384       }
385   }
386
387   // We make changes if there are any dead blocks in the function...
388   if (unsigned NumDeadBlocks = Func->size() - AliveBlocks.size()) {
389     MadeChanges = true;
390     NumBlockRemoved += NumDeadBlocks;
391   }
392
393   // Loop over all of the basic blocks in the function, removing control flow
394   // edges to live blocks (also eliminating any entries in PHI functions in
395   // referenced blocks).
396   //
397   for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB)
398     if (!AliveBlocks.count(BB)) {
399       // Remove all outgoing edges from this basic block and convert the
400       // terminator into a return instruction.
401       std::vector<BasicBlock*> Succs(succ_begin(BB), succ_end(BB));
402       
403       if (!Succs.empty()) {
404         // Loop over all of the successors, removing this block from PHI node
405         // entries that might be in the block...
406         while (!Succs.empty()) {
407           Succs.back()->removePredecessor(BB);
408           Succs.pop_back();
409         }
410         
411         // Delete the old terminator instruction...
412         BB->getInstList().pop_back();
413         const Type *RetTy = Func->getReturnType();
414         BB->getInstList().push_back(new ReturnInst(RetTy != Type::VoidTy ?
415                                            Constant::getNullValue(RetTy) : 0));
416       }
417     }
418
419
420   // Loop over all of the basic blocks in the function, dropping references of
421   // the dead basic blocks.  We must do this after the previous step to avoid
422   // dropping references to PHIs which still have entries...
423   //
424   for (Function::iterator BB = Func->begin(), E = Func->end(); BB != E; ++BB)
425     if (!AliveBlocks.count(BB))
426       BB->dropAllReferences();
427
428   // Now loop through all of the blocks and delete the dead ones.  We can safely
429   // do this now because we know that there are no references to dead blocks
430   // (because they have dropped all of their references...  we also remove dead
431   // instructions from alive blocks.
432   //
433   for (Function::iterator BI = Func->begin(); BI != Func->end(); )
434     if (!AliveBlocks.count(BI)) {                // Delete dead blocks...
435       BI = Func->getBasicBlockList().erase(BI);
436     } else {                                     // Scan alive blocks...
437       for (BasicBlock::iterator II = BI->begin(); II != --BI->end(); )
438         if (!LiveSet.count(II)) {             // Is this instruction alive?
439           // Nope... remove the instruction from it's basic block...
440           II = BI->getInstList().erase(II);
441           ++NumInstRemoved;
442           MadeChanges = true;
443         } else {
444           ++II;
445         }
446
447       ++BI;                                           // Increment iterator...
448     }
449
450   return MadeChanges;
451 }