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