Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 1 | //===- ADCE.cpp - Code to perform aggressive dead code elimination --------===// |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 3 | // This file implements "aggressive" dead code elimination. ADCE is DCe where |
Chris Lattner | b28986f | 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 | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 9 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 10 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 11 | #include "llvm/Type.h" |
Chris Lattner | 8024bde | 2001-07-06 16:32:07 +0000 | [diff] [blame] | 12 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 8024bde | 2001-07-06 16:32:07 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/Writer.h" |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 14 | #include "llvm/iTerminators.h" |
Chris Lattner | fb5ae02 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 15 | #include "llvm/iPHINode.h" |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CFG.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 17 | #include "Support/STLExtras.h" |
| 18 | #include "Support/DepthFirstIterator.h" |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 20 | #include <iostream> |
| 21 | using std::cerr; |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 22 | |
Chris Lattner | b271be3 | 2001-09-28 00:06:42 +0000 | [diff] [blame] | 23 | #define DEBUG_ADCE 1 |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | // ADCE Class |
| 29 | // |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 30 | // This class does all of the work of Aggressive Dead Code Elimination. |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 31 | // It's public interface consists of a constructor and a doADCE() method. |
| 32 | // |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 33 | class ADCE : public FunctionPass { |
| 34 | Function *Func; // The function that we are working on |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 35 | std::vector<Instruction*> WorkList; // Instructions that just became live |
| 36 | std::set<Instruction*> LiveSet; // The set of live instructions |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 37 | bool MadeChanges; |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 38 | |
| 39 | //===--------------------------------------------------------------------===// |
| 40 | // The public interface for this class |
| 41 | // |
| 42 | public: |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 43 | const char *getPassName() const { return "Aggressive Dead Code Elimination"; } |
| 44 | |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 45 | // doADCE - Execute the Aggressive Dead Code Elimination Algorithm |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 46 | // |
| 47 | virtual bool runOnFunction(Function *F) { |
| 48 | Func = F; MadeChanges = false; |
| 49 | doADCE(getAnalysis<DominanceFrontier>(DominanceFrontier::PostDomID)); |
| 50 | assert(WorkList.empty()); |
| 51 | LiveSet.clear(); |
| 52 | return MadeChanges; |
| 53 | } |
| 54 | // getAnalysisUsage - We require post dominance frontiers (aka Control |
| 55 | // Dependence Graph) |
| 56 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 57 | AU.addRequired(DominanceFrontier::PostDomID); |
| 58 | } |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 59 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 60 | |
| 61 | //===--------------------------------------------------------------------===// |
| 62 | // The implementation of this class |
| 63 | // |
| 64 | private: |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 65 | // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 66 | // true if the function was modified. |
| 67 | // |
| 68 | void doADCE(DominanceFrontier &CDG); |
| 69 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 70 | inline void markInstructionLive(Instruction *I) { |
| 71 | if (LiveSet.count(I)) return; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 72 | #ifdef DEBUG_ADCE |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 73 | cerr << "Insn Live: " << I; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 74 | #endif |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 75 | LiveSet.insert(I); |
| 76 | WorkList.push_back(I); |
| 77 | } |
| 78 | |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 79 | inline void markTerminatorLive(const BasicBlock *BB) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 80 | #ifdef DEBUG_ADCE |
| 81 | cerr << "Terminat Live: " << BB->getTerminator(); |
| 82 | #endif |
| 83 | markInstructionLive((Instruction*)BB->getTerminator()); |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 85 | |
| 86 | // fixupCFG - Walk the CFG in depth first order, eliminating references to |
| 87 | // dead blocks. |
| 88 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 89 | BasicBlock *fixupCFG(BasicBlock *Head, std::set<BasicBlock*> &VisitedBlocks, |
| 90 | const std::set<BasicBlock*> &AliveBlocks); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 93 | } // End of anonymous namespace |
| 94 | |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 95 | Pass *createAggressiveDCEPass() { |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 96 | return new ADCE(); |
| 97 | } |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 98 | |
| 99 | |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 100 | // doADCE() - Run the Aggressive Dead Code Elimination algorithm, returning |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 101 | // true if the function was modified. |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 102 | // |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 103 | void ADCE::doADCE(DominanceFrontier &CDG) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 104 | #ifdef DEBUG_ADCE |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 105 | cerr << "Function: " << Func; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 108 | // Iterate over all of the instructions in the function, eliminating trivially |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 109 | // dead instructions, and marking instructions live that are known to be |
| 110 | // needed. Perform the walk in depth first order so that we avoid marking any |
| 111 | // instructions live in basic blocks that are unreachable. These blocks will |
| 112 | // be eliminated later, along with the instructions inside. |
| 113 | // |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 114 | for (df_iterator<Function*> BBI = df_begin(Func), BBE = df_end(Func); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 115 | BBI != BBE; ++BBI) { |
| 116 | BasicBlock *BB = *BBI; |
| 117 | for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) { |
| 118 | Instruction *I = *II; |
| 119 | |
| 120 | if (I->hasSideEffects() || I->getOpcode() == Instruction::Ret) { |
| 121 | markInstructionLive(I); |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 122 | ++II; // Increment the inst iterator if the inst wasn't deleted |
| 123 | } else if (isInstructionTriviallyDead(I)) { |
| 124 | // Remove the instruction from it's basic block... |
| 125 | delete BB->getInstList().remove(II); |
| 126 | MadeChanges = true; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 127 | } else { |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 128 | ++II; // Increment the inst iterator if the inst wasn't deleted |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 129 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | #ifdef DEBUG_ADCE |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 134 | cerr << "Processing work list\n"; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 135 | #endif |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 136 | |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 137 | // AliveBlocks - Set of basic blocks that we know have instructions that are |
| 138 | // alive in them... |
| 139 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 140 | std::set<BasicBlock*> AliveBlocks; |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 141 | |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 142 | // Process the work list of instructions that just became live... if they |
| 143 | // became live, then that means that all of their operands are neccesary as |
| 144 | // well... make them live as well. |
| 145 | // |
| 146 | while (!WorkList.empty()) { |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 147 | Instruction *I = WorkList.back(); // Get an instruction that became live... |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 148 | WorkList.pop_back(); |
| 149 | |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 150 | BasicBlock *BB = I->getParent(); |
| 151 | if (AliveBlocks.count(BB) == 0) { // Basic block not alive yet... |
| 152 | // Mark the basic block as being newly ALIVE... and mark all branches that |
| 153 | // this block is control dependant on as being alive also... |
| 154 | // |
| 155 | AliveBlocks.insert(BB); // Block is now ALIVE! |
Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 156 | DominanceFrontier::const_iterator It = CDG.find(BB); |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 157 | if (It != CDG.end()) { |
| 158 | // Get the blocks that this node is control dependant on... |
Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 159 | const DominanceFrontier::DomSetType &CDB = It->second; |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 160 | for_each(CDB.begin(), CDB.end(), // Mark all their terminators as live |
| 161 | bind_obj(this, &ADCE::markTerminatorLive)); |
| 162 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 163 | |
| 164 | // If this basic block is live, then the terminator must be as well! |
| 165 | markTerminatorLive(BB); |
Chris Lattner | fb8ed0c | 2001-07-08 18:38:36 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 168 | // Loop over all of the operands of the live instruction, making sure that |
| 169 | // they are known to be alive as well... |
| 170 | // |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 171 | for (unsigned op = 0, End = I->getNumOperands(); op != End; ++op) |
Chris Lattner | 4b717c0 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 172 | if (Instruction *Operand = dyn_cast<Instruction>(I->getOperand(op))) |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 173 | markInstructionLive(Operand); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 176 | #ifdef DEBUG_ADCE |
Chris Lattner | f8e4dc3 | 2002-04-08 22:03:00 +0000 | [diff] [blame] | 177 | cerr << "Current Function: X = Live\n"; |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 178 | for (Function::iterator I = Func->begin(), E = Func->end(); I != E; ++I) |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 179 | for (BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end(); |
| 180 | BI != BE; ++BI) { |
| 181 | if (LiveSet.count(*BI)) cerr << "X "; |
| 182 | cerr << *BI; |
| 183 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 184 | #endif |
| 185 | |
| 186 | // After the worklist is processed, recursively walk the CFG in depth first |
| 187 | // order, patching up references to dead blocks... |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 188 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 189 | std::set<BasicBlock*> VisitedBlocks; |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 190 | BasicBlock *EntryBlock = fixupCFG(Func->front(), VisitedBlocks, AliveBlocks); |
| 191 | if (EntryBlock && EntryBlock != Func->front()) { |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 192 | // We need to move the new entry block to be the first bb of the function |
| 193 | Function::iterator EBI = find(Func->begin(), Func->end(), EntryBlock); |
| 194 | std::swap(*EBI, *Func->begin()); // Exchange old location with start of fn |
| 195 | |
| 196 | while (PHINode *PN = dyn_cast<PHINode>(EntryBlock->front())) { |
| 197 | assert(PN->getNumIncomingValues() == 1 && |
| 198 | "Can only have a single incoming value at this point..."); |
| 199 | // The incoming value must be outside of the scope of the function, a |
| 200 | // global variable, constant or parameter maybe... |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 201 | // |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 202 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
Chris Lattner | b271be3 | 2001-09-28 00:06:42 +0000 | [diff] [blame] | 203 | |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 204 | // Nuke the phi node... |
| 205 | delete EntryBlock->getInstList().remove(EntryBlock->begin()); |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 209 | // Now go through and tell dead blocks to drop all of their references so they |
| 210 | // can be safely deleted. |
| 211 | // |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 212 | for (Function::iterator BI = Func->begin(), BE = Func->end(); BI != BE; ++BI){ |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 213 | BasicBlock *BB = *BI; |
| 214 | if (!AliveBlocks.count(BB)) { |
| 215 | BB->dropAllReferences(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Now loop through all of the blocks and delete them. We can safely do this |
| 220 | // now because we know that there are no references to dead blocks (because |
| 221 | // they have dropped all of their references... |
| 222 | // |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 223 | for (Function::iterator BI = Func->begin(); BI != Func->end();) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 224 | if (!AliveBlocks.count(*BI)) { |
Chris Lattner | 019f364 | 2002-05-06 17:27:57 +0000 | [diff] [blame] | 225 | delete Func->getBasicBlocks().remove(BI); |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 226 | MadeChanges = true; |
| 227 | continue; // Don't increment iterator |
| 228 | } |
| 229 | ++BI; // Increment iterator... |
| 230 | } |
Chris Lattner | b28986f | 2001-06-30 06:39:11 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 234 | // fixupCFG - Walk the CFG in depth first order, eliminating references to |
| 235 | // dead blocks: |
| 236 | // If the BB is alive (in AliveBlocks): |
| 237 | // 1. Eliminate all dead instructions in the BB |
| 238 | // 2. Recursively traverse all of the successors of the BB: |
| 239 | // - If the returned successor is non-null, update our terminator to |
| 240 | // reference the returned BB |
| 241 | // 3. Return 0 (no update needed) |
| 242 | // |
| 243 | // If the BB is dead (not in AliveBlocks): |
| 244 | // 1. Add the BB to the dead set |
| 245 | // 2. Recursively traverse all of the successors of the block: |
| 246 | // - Only one shall return a nonnull value (or else this block should have |
| 247 | // been in the alive set). |
| 248 | // 3. Return the nonnull child, or 0 if no non-null children. |
| 249 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 250 | BasicBlock *ADCE::fixupCFG(BasicBlock *BB, std::set<BasicBlock*> &VisitedBlocks, |
| 251 | const std::set<BasicBlock*> &AliveBlocks) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 252 | if (VisitedBlocks.count(BB)) return 0; // Revisiting a node? No update. |
| 253 | VisitedBlocks.insert(BB); // We have now visited this node! |
| 254 | |
| 255 | #ifdef DEBUG_ADCE |
| 256 | cerr << "Fixing up BB: " << BB; |
| 257 | #endif |
| 258 | |
| 259 | if (AliveBlocks.count(BB)) { // Is the block alive? |
| 260 | // Yes it's alive: loop through and eliminate all dead instructions in block |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 261 | for (BasicBlock::iterator II = BB->begin(); II != BB->end()-1; ) |
| 262 | if (!LiveSet.count(*II)) { // Is this instruction alive? |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 263 | // Nope... remove the instruction from it's basic block... |
| 264 | delete BB->getInstList().remove(II); |
| 265 | MadeChanges = true; |
Chris Lattner | c1496bda | 2002-05-07 22:11:39 +0000 | [diff] [blame^] | 266 | } else { |
| 267 | ++II; |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 269 | |
| 270 | // Recursively traverse successors of this basic block. |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 271 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 272 | BasicBlock *Succ = *SI; |
| 273 | BasicBlock *Repl = fixupCFG(Succ, VisitedBlocks, AliveBlocks); |
| 274 | if (Repl && Repl != Succ) { // We have to replace the successor |
| 275 | Succ->replaceAllUsesWith(Repl); |
| 276 | MadeChanges = true; |
| 277 | } |
| 278 | } |
| 279 | return BB; |
| 280 | } else { // Otherwise the block is dead... |
| 281 | BasicBlock *ReturnBB = 0; // Default to nothing live down here |
| 282 | |
| 283 | // Recursively traverse successors of this basic block. |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 284 | for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) { |
Chris Lattner | acfd27d | 2001-09-09 22:26:47 +0000 | [diff] [blame] | 285 | BasicBlock *RetBB = fixupCFG(*SI, VisitedBlocks, AliveBlocks); |
| 286 | if (RetBB) { |
| 287 | assert(ReturnBB == 0 && "One one live child allowed!"); |
| 288 | ReturnBB = RetBB; |
| 289 | } |
| 290 | } |
| 291 | return ReturnBB; // Return the result of traversal |
| 292 | } |
| 293 | } |
| 294 | |