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