Chris Lattner | 67a9801 | 2003-10-12 21:44:18 +0000 | [diff] [blame] | 1 | //===- LoopSimplify.cpp - Loop Canonicalization Pass ----------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 10 | // This pass performs several transformations to transform natural loops into a |
| 11 | // simpler form, which makes subsequent analyses and transformations simpler and |
| 12 | // more effective. |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 13 | // |
| 14 | // Loop pre-header insertion guarantees that there is a single, non-critical |
| 15 | // entry edge from outside of the loop to the loop header. This simplifies a |
| 16 | // number of analyses and transformations, such as LICM. |
| 17 | // |
| 18 | // Loop exit-block insertion guarantees that all exit blocks from the loop |
| 19 | // (blocks which are outside of the loop that have predecessors inside of the |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 20 | // loop) only have predecessors from inside of the loop (and are thus dominated |
| 21 | // by the loop header). This simplifies transformations such as store-sinking |
| 22 | // that are built into LICM. |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 23 | // |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 24 | // This pass also guarantees that loops will have exactly one backedge. |
| 25 | // |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 26 | // Note that the simplifycfg pass will clean up blocks which are split out but |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 27 | // end up being unnecessary, so usage of this pass should not pessimize |
| 28 | // generated code. |
| 29 | // |
| 30 | // This pass obviously modifies the CFG, but updates loop information and |
| 31 | // dominator information. |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 32 | // |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | d216e8b | 2006-12-19 22:17:40 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "loopsimplify" |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 2ef703e | 2004-03-14 03:59:22 +0000 | [diff] [blame] | 37 | #include "llvm/Constant.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 38 | #include "llvm/Instructions.h" |
Chris Lattner | 2ef703e | 2004-03-14 03:59:22 +0000 | [diff] [blame] | 39 | #include "llvm/Function.h" |
| 40 | #include "llvm/Type.h" |
Chris Lattner | cec5b88 | 2005-03-25 06:37:22 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/Dominators.h" |
| 43 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 44 | #include "llvm/Support/CFG.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SetOperations.h" |
| 47 | #include "llvm/ADT/SetVector.h" |
| 48 | #include "llvm/ADT/Statistic.h" |
| 49 | #include "llvm/ADT/DepthFirstIterator.h" |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 50 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 51 | |
Chris Lattner | d216e8b | 2006-12-19 22:17:40 +0000 | [diff] [blame] | 52 | STATISTIC(NumInserted, "Number of pre-header or exit blocks inserted"); |
| 53 | STATISTIC(NumNested , "Number of nested loops split out"); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 54 | |
Chris Lattner | d216e8b | 2006-12-19 22:17:40 +0000 | [diff] [blame] | 55 | namespace { |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 56 | struct VISIBILITY_HIDDEN LoopSimplify : public FunctionPass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 57 | static char ID; // Pass identifcation, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 58 | LoopSimplify() : FunctionPass((intptr_t)&ID) {} |
| 59 | |
Chris Lattner | cec5b88 | 2005-03-25 06:37:22 +0000 | [diff] [blame] | 60 | // AA - If we have an alias analysis object to update, this is it, otherwise |
| 61 | // this is null. |
| 62 | AliasAnalysis *AA; |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 63 | LoopInfo *LI; |
Chris Lattner | cec5b88 | 2005-03-25 06:37:22 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 65 | virtual bool runOnFunction(Function &F); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 67 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 68 | // We need loop information to identify the loops... |
| 69 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 786c564 | 2004-03-13 22:01:26 +0000 | [diff] [blame] | 70 | AU.addRequired<DominatorTree>(); |
Devang Patel | 3b57b6f | 2007-03-20 20:18:12 +0000 | [diff] [blame] | 71 | AU.addRequired<ETForest>(); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 72 | |
| 73 | AU.addPreserved<LoopInfo>(); |
Chris Lattner | baec98d | 2006-01-09 08:03:08 +0000 | [diff] [blame] | 74 | AU.addPreserved<ETForest>(); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 75 | AU.addPreserved<DominatorTree>(); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 76 | AU.addPreserved<DominanceFrontier>(); |
Chris Lattner | 94f4032 | 2005-08-10 02:07:32 +0000 | [diff] [blame] | 77 | AU.addPreservedID(BreakCriticalEdgesID); // No critical edges added. |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 78 | } |
| 79 | private: |
| 80 | bool ProcessLoop(Loop *L); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 81 | BasicBlock *SplitBlockPredecessors(BasicBlock *BB, const char *Suffix, |
| 82 | const std::vector<BasicBlock*> &Preds); |
Chris Lattner | 59fb87d | 2004-04-18 22:27:10 +0000 | [diff] [blame] | 83 | BasicBlock *RewriteLoopExitBlock(Loop *L, BasicBlock *Exit); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 84 | void InsertPreheaderForLoop(Loop *L); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 85 | Loop *SeparateNestedLoop(Loop *L); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 86 | void InsertUniqueBackedgeBlock(Loop *L); |
Chris Lattner | 120fce5 | 2006-09-23 08:19:21 +0000 | [diff] [blame] | 87 | void PlaceSplitBlockCarefully(BasicBlock *NewBB, |
| 88 | std::vector<BasicBlock*> &SplitPreds, |
| 89 | Loop *L); |
| 90 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 91 | void UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, |
| 92 | std::vector<BasicBlock*> &PredBlocks); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 95 | char LoopSimplify::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 96 | RegisterPass<LoopSimplify> |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 97 | X("loopsimplify", "Canonicalize natural loops", true); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Publically exposed interface to pass... |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 101 | const PassInfo *llvm::LoopSimplifyID = X.getPassInfo(); |
Chris Lattner | 4b50156 | 2004-09-20 04:43:15 +0000 | [diff] [blame] | 102 | FunctionPass *llvm::createLoopSimplifyPass() { return new LoopSimplify(); } |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 104 | /// runOnFunction - Run down all loops in the CFG (recursively, but we could do |
| 105 | /// it in any convenient order) inserting preheaders... |
| 106 | /// |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 107 | bool LoopSimplify::runOnFunction(Function &F) { |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 108 | bool Changed = false; |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 109 | LI = &getAnalysis<LoopInfo>(); |
Chris Lattner | cec5b88 | 2005-03-25 06:37:22 +0000 | [diff] [blame] | 110 | AA = getAnalysisToUpdate<AliasAnalysis>(); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 111 | |
Chris Lattner | fa78946 | 2006-08-12 04:51:20 +0000 | [diff] [blame] | 112 | // Check to see that no blocks (other than the header) in loops have |
| 113 | // predecessors that are not in loops. This is not valid for natural loops, |
| 114 | // but can occur if the blocks are unreachable. Since they are unreachable we |
| 115 | // can just shamelessly destroy their terminators to make them not branch into |
| 116 | // the loop! |
| 117 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 118 | // This case can only occur for unreachable blocks. Blocks that are |
| 119 | // unreachable can't be in loops, so filter those blocks out. |
| 120 | if (LI->getLoopFor(BB)) continue; |
| 121 | |
| 122 | bool BlockUnreachable = false; |
| 123 | TerminatorInst *TI = BB->getTerminator(); |
| 124 | |
| 125 | // Check to see if any successors of this block are non-loop-header loops |
| 126 | // that are not the header. |
| 127 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 128 | // If this successor is not in a loop, BB is clearly ok. |
| 129 | Loop *L = LI->getLoopFor(TI->getSuccessor(i)); |
| 130 | if (!L) continue; |
| 131 | |
| 132 | // If the succ is the loop header, and if L is a top-level loop, then this |
| 133 | // is an entrance into a loop through the header, which is also ok. |
| 134 | if (L->getHeader() == TI->getSuccessor(i) && L->getParentLoop() == 0) |
| 135 | continue; |
| 136 | |
| 137 | // Otherwise, this is an entrance into a loop from some place invalid. |
| 138 | // Either the loop structure is invalid and this is not a natural loop (in |
| 139 | // which case the compiler is buggy somewhere else) or BB is unreachable. |
| 140 | BlockUnreachable = true; |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | // If this block is ok, check the next one. |
| 145 | if (!BlockUnreachable) continue; |
| 146 | |
| 147 | // Otherwise, this block is dead. To clean up the CFG and to allow later |
| 148 | // loop transformations to ignore this case, we delete the edges into the |
| 149 | // loop by replacing the terminator. |
| 150 | |
| 151 | // Remove PHI entries from the successors. |
| 152 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 153 | TI->getSuccessor(i)->removePredecessor(BB); |
| 154 | |
| 155 | // Add a new unreachable instruction. |
| 156 | new UnreachableInst(TI); |
| 157 | |
| 158 | // Delete the dead terminator. |
| 159 | if (AA) AA->deleteValue(&BB->back()); |
| 160 | BB->getInstList().pop_back(); |
| 161 | Changed |= true; |
| 162 | } |
| 163 | |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 164 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
Chris Lattner | 329c1c6 | 2004-01-08 00:09:44 +0000 | [diff] [blame] | 165 | Changed |= ProcessLoop(*I); |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 166 | |
| 167 | return Changed; |
| 168 | } |
| 169 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 170 | /// ProcessLoop - Walk the loop structure in depth first order, ensuring that |
| 171 | /// all loops have preheaders. |
| 172 | /// |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 173 | bool LoopSimplify::ProcessLoop(Loop *L) { |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 174 | bool Changed = false; |
Chris Lattner | 3bb4657 | 2006-08-12 05:25:00 +0000 | [diff] [blame] | 175 | ReprocessLoop: |
| 176 | |
Chris Lattner | 0ab9f96 | 2006-02-14 23:06:02 +0000 | [diff] [blame] | 177 | // Canonicalize inner loops before outer loops. Inner loop canonicalization |
| 178 | // can provide work for the outer loop to canonicalize. |
| 179 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 180 | Changed |= ProcessLoop(*I); |
| 181 | |
Chris Lattner | 2ef703e | 2004-03-14 03:59:22 +0000 | [diff] [blame] | 182 | assert(L->getBlocks()[0] == L->getHeader() && |
| 183 | "Header isn't first block in loop?"); |
Chris Lattner | 2ef703e | 2004-03-14 03:59:22 +0000 | [diff] [blame] | 184 | |
Chris Lattner | fa78946 | 2006-08-12 04:51:20 +0000 | [diff] [blame] | 185 | // Does the loop already have a preheader? If so, don't insert one. |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 186 | if (L->getLoopPreheader() == 0) { |
| 187 | InsertPreheaderForLoop(L); |
| 188 | NumInserted++; |
| 189 | Changed = true; |
| 190 | } |
| 191 | |
Chris Lattner | 66ea98e | 2003-12-10 17:20:35 +0000 | [diff] [blame] | 192 | // Next, check to make sure that all exit nodes of the loop only have |
| 193 | // predecessors that are inside of the loop. This check guarantees that the |
| 194 | // loop preheader/header will dominate the exit blocks. If the exit block has |
Chris Lattner | ee628cf | 2006-02-12 01:59:10 +0000 | [diff] [blame] | 195 | // predecessors from outside of the loop, split the edge now. |
| 196 | std::vector<BasicBlock*> ExitBlocks; |
| 197 | L->getExitBlocks(ExitBlocks); |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 198 | |
Chris Lattner | ee628cf | 2006-02-12 01:59:10 +0000 | [diff] [blame] | 199 | SetVector<BasicBlock*> ExitBlockSet(ExitBlocks.begin(), ExitBlocks.end()); |
Chris Lattner | fed22aa | 2004-07-15 08:20:22 +0000 | [diff] [blame] | 200 | for (SetVector<BasicBlock*>::iterator I = ExitBlockSet.begin(), |
| 201 | E = ExitBlockSet.end(); I != E; ++I) { |
| 202 | BasicBlock *ExitBlock = *I; |
Chris Lattner | de7aee7 | 2004-07-15 05:36:31 +0000 | [diff] [blame] | 203 | for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock); |
| 204 | PI != PE; ++PI) |
Chris Lattner | 8587eb3 | 2006-02-11 02:13:17 +0000 | [diff] [blame] | 205 | // Must be exactly this loop: no subloops, parent loops, or non-loop preds |
| 206 | // allowed. |
Chris Lattner | ee628cf | 2006-02-12 01:59:10 +0000 | [diff] [blame] | 207 | if (!L->contains(*PI)) { |
Chris Lattner | fed22aa | 2004-07-15 08:20:22 +0000 | [diff] [blame] | 208 | RewriteLoopExitBlock(L, ExitBlock); |
Chris Lattner | de7aee7 | 2004-07-15 05:36:31 +0000 | [diff] [blame] | 209 | NumInserted++; |
| 210 | Changed = true; |
| 211 | break; |
| 212 | } |
Chris Lattner | fed22aa | 2004-07-15 08:20:22 +0000 | [diff] [blame] | 213 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 215 | // If the header has more than two predecessors at this point (from the |
| 216 | // preheader and from multiple backedges), we must adjust the loop. |
Chris Lattner | 3bb4657 | 2006-08-12 05:25:00 +0000 | [diff] [blame] | 217 | unsigned NumBackedges = L->getNumBackEdges(); |
| 218 | if (NumBackedges != 1) { |
| 219 | // If this is really a nested loop, rip it out into a child loop. Don't do |
| 220 | // this for loops with a giant number of backedges, just factor them into a |
| 221 | // common backedge instead. |
| 222 | if (NumBackedges < 8) { |
| 223 | if (Loop *NL = SeparateNestedLoop(L)) { |
| 224 | ++NumNested; |
| 225 | // This is a big restructuring change, reprocess the whole loop. |
| 226 | ProcessLoop(NL); |
| 227 | Changed = true; |
| 228 | // GCC doesn't tail recursion eliminate this. |
| 229 | goto ReprocessLoop; |
| 230 | } |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | 3bb4657 | 2006-08-12 05:25:00 +0000 | [diff] [blame] | 233 | // If we either couldn't, or didn't want to, identify nesting of the loops, |
| 234 | // insert a new block that all backedges target, then make it jump to the |
| 235 | // loop header. |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 236 | InsertUniqueBackedgeBlock(L); |
| 237 | NumInserted++; |
| 238 | Changed = true; |
| 239 | } |
| 240 | |
Chris Lattner | 94f4032 | 2005-08-10 02:07:32 +0000 | [diff] [blame] | 241 | // Scan over the PHI nodes in the loop header. Since they now have only two |
| 242 | // incoming values (the loop is canonicalized), we may have simplified the PHI |
| 243 | // down to 'X = phi [X, Y]', which should be replaced with 'Y'. |
| 244 | PHINode *PN; |
Chris Lattner | 94f4032 | 2005-08-10 02:07:32 +0000 | [diff] [blame] | 245 | for (BasicBlock::iterator I = L->getHeader()->begin(); |
| 246 | (PN = dyn_cast<PHINode>(I++)); ) |
Chris Lattner | 98599ba | 2005-08-10 17:15:20 +0000 | [diff] [blame] | 247 | if (Value *V = PN->hasConstantValue()) { |
Chris Lattner | 94f4032 | 2005-08-10 02:07:32 +0000 | [diff] [blame] | 248 | PN->replaceAllUsesWith(V); |
| 249 | PN->eraseFromParent(); |
| 250 | } |
| 251 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 252 | return Changed; |
| 253 | } |
| 254 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 255 | /// SplitBlockPredecessors - Split the specified block into two blocks. We want |
| 256 | /// to move the predecessors specified in the Preds list to point to the new |
| 257 | /// block, leaving the remaining predecessors pointing to BB. This method |
| 258 | /// updates the SSA PHINode's, but no other analyses. |
| 259 | /// |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 260 | BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, |
| 261 | const char *Suffix, |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 262 | const std::vector<BasicBlock*> &Preds) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 263 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 264 | // Create new basic block, insert right before the original block... |
Chris Lattner | c24a076 | 2004-02-04 03:58:28 +0000 | [diff] [blame] | 265 | BasicBlock *NewBB = new BasicBlock(BB->getName()+Suffix, BB->getParent(), BB); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 266 | |
| 267 | // The preheader first gets an unconditional branch to the loop header... |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 268 | BranchInst *BI = new BranchInst(BB, NewBB); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 269 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 270 | // For every PHI node in the block, insert a PHI node into NewBB where the |
| 271 | // incoming values from the out of loop edges are moved to NewBB. We have two |
| 272 | // possible cases here. If the loop is dead, we just insert dummy entries |
| 273 | // into the PHI nodes for the new edge. If the loop is not dead, we move the |
| 274 | // incoming edges in BB into new PHI nodes in NewBB. |
| 275 | // |
| 276 | if (!Preds.empty()) { // Is the loop not obviously dead? |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 277 | // Check to see if the values being merged into the new block need PHI |
| 278 | // nodes. If so, insert them. |
Alkis Evlogimenos | 200a360 | 2004-09-28 02:40:37 +0000 | [diff] [blame] | 279 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ) { |
| 280 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 281 | ++I; |
| 282 | |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 283 | // Check to see if all of the values coming in are the same. If so, we |
| 284 | // don't need to create a new PHI node. |
| 285 | Value *InVal = PN->getIncomingValueForBlock(Preds[0]); |
| 286 | for (unsigned i = 1, e = Preds.size(); i != e; ++i) |
| 287 | if (InVal != PN->getIncomingValueForBlock(Preds[i])) { |
| 288 | InVal = 0; |
| 289 | break; |
| 290 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 291 | |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 292 | // If the values coming into the block are not the same, we need a PHI. |
| 293 | if (InVal == 0) { |
Chris Lattner | 010ba10 | 2003-12-09 23:12:55 +0000 | [diff] [blame] | 294 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 295 | PHINode *NewPHI = new PHINode(PN->getType(), PN->getName()+".ph", BI); |
Chris Lattner | cec5b88 | 2005-03-25 06:37:22 +0000 | [diff] [blame] | 296 | if (AA) AA->copyValue(PN, NewPHI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 010ba10 | 2003-12-09 23:12:55 +0000 | [diff] [blame] | 298 | // Move all of the edges from blocks outside the loop to the new PHI |
| 299 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 300 | Value *V = PN->removeIncomingValue(Preds[i], false); |
Chris Lattner | 010ba10 | 2003-12-09 23:12:55 +0000 | [diff] [blame] | 301 | NewPHI->addIncoming(V, Preds[i]); |
| 302 | } |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 303 | InVal = NewPHI; |
| 304 | } else { |
| 305 | // Remove all of the edges coming into the PHI nodes from outside of the |
| 306 | // block. |
| 307 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) |
| 308 | PN->removeIncomingValue(Preds[i], false); |
Chris Lattner | 010ba10 | 2003-12-09 23:12:55 +0000 | [diff] [blame] | 309 | } |
Chris Lattner | 0f98e75 | 2003-12-19 06:27:08 +0000 | [diff] [blame] | 310 | |
| 311 | // Add an incoming value to the PHI node in the loop for the preheader |
| 312 | // edge. |
| 313 | PN->addIncoming(InVal, NewBB); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 314 | |
| 315 | // Can we eliminate this phi node now? |
Chris Lattner | 5e1b231 | 2005-08-05 00:57:45 +0000 | [diff] [blame] | 316 | if (Value *V = PN->hasConstantValue(true)) { |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 317 | Instruction *I = dyn_cast<Instruction>(V); |
Owen Anderson | 558fc74 | 2007-04-09 00:52:49 +0000 | [diff] [blame] | 318 | // If I is in NewBB, the ETForest call will fail, because NewBB isn't |
| 319 | // registered in ETForest yet. Handle this case explicitly. |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 320 | if (!I || (I->getParent() != NewBB && |
| 321 | getAnalysis<ETForest>().dominates(I, PN))) { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 322 | PN->replaceAllUsesWith(V); |
Chris Lattner | cec5b88 | 2005-03-25 06:37:22 +0000 | [diff] [blame] | 323 | if (AA) AA->deleteValue(PN); |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 324 | BB->getInstList().erase(PN); |
| 325 | } |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 327 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 328 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 329 | // Now that the PHI nodes are updated, actually move the edges from |
| 330 | // Preds to point to NewBB instead of BB. |
| 331 | // |
| 332 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) { |
| 333 | TerminatorInst *TI = Preds[i]->getTerminator(); |
| 334 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) |
| 335 | if (TI->getSuccessor(s) == BB) |
| 336 | TI->setSuccessor(s, NewBB); |
| 337 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 338 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 339 | } else { // Otherwise the loop is dead... |
Alkis Evlogimenos | 200a360 | 2004-09-28 02:40:37 +0000 | [diff] [blame] | 340 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) { |
| 341 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 342 | // Insert dummy values as the incoming value... |
| 343 | PN->addIncoming(Constant::getNullValue(PN->getType()), NewBB); |
Alkis Evlogimenos | 200a360 | 2004-09-28 02:40:37 +0000 | [diff] [blame] | 344 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 346 | return NewBB; |
| 347 | } |
| 348 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 349 | /// InsertPreheaderForLoop - Once we discover that a loop doesn't have a |
| 350 | /// preheader, this method is called to insert one. This method has two phases: |
| 351 | /// preheader insertion and analysis updating. |
| 352 | /// |
Chris Lattner | ee2c50c | 2003-10-12 21:43:28 +0000 | [diff] [blame] | 353 | void LoopSimplify::InsertPreheaderForLoop(Loop *L) { |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 354 | BasicBlock *Header = L->getHeader(); |
| 355 | |
| 356 | // Compute the set of predecessors of the loop that are not in the loop. |
| 357 | std::vector<BasicBlock*> OutsideBlocks; |
| 358 | for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header); |
| 359 | PI != PE; ++PI) |
Chris Lattner | 8587eb3 | 2006-02-11 02:13:17 +0000 | [diff] [blame] | 360 | if (!L->contains(*PI)) // Coming in from outside the loop? |
| 361 | OutsideBlocks.push_back(*PI); // Keep track of it... |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 362 | |
Chris Lattner | c398457 | 2006-09-23 07:40:52 +0000 | [diff] [blame] | 363 | // Split out the loop pre-header. |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 364 | BasicBlock *NewBB = |
| 365 | SplitBlockPredecessors(Header, ".preheader", OutsideBlocks); |
Chris Lattner | c398457 | 2006-09-23 07:40:52 +0000 | [diff] [blame] | 366 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 368 | //===--------------------------------------------------------------------===// |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 369 | // Update analysis results now that we have performed the transformation |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 370 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 372 | // We know that we have loop information to update... update it now. |
| 373 | if (Loop *Parent = L->getParentLoop()) |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 374 | Parent->addBasicBlockToLoop(NewBB, *LI); |
Chris Lattner | 9f879cf | 2003-02-27 22:48:57 +0000 | [diff] [blame] | 375 | |
Chris Lattner | c398457 | 2006-09-23 07:40:52 +0000 | [diff] [blame] | 376 | UpdateDomInfoForRevectoredPreds(NewBB, OutsideBlocks); |
Chris Lattner | 120fce5 | 2006-09-23 08:19:21 +0000 | [diff] [blame] | 377 | |
| 378 | // Make sure that NewBB is put someplace intelligent, which doesn't mess up |
| 379 | // code layout too horribly. |
| 380 | PlaceSplitBlockCarefully(NewBB, OutsideBlocks, L); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 383 | /// RewriteLoopExitBlock - Ensure that the loop preheader dominates all exit |
| 384 | /// blocks. This method is used to split exit blocks that have predecessors |
| 385 | /// outside of the loop. |
Chris Lattner | 59fb87d | 2004-04-18 22:27:10 +0000 | [diff] [blame] | 386 | BasicBlock *LoopSimplify::RewriteLoopExitBlock(Loop *L, BasicBlock *Exit) { |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 387 | std::vector<BasicBlock*> LoopBlocks; |
| 388 | for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit); I != E; ++I) |
| 389 | if (L->contains(*I)) |
| 390 | LoopBlocks.push_back(*I); |
| 391 | |
Chris Lattner | 7e7ad49 | 2003-02-27 22:31:07 +0000 | [diff] [blame] | 392 | assert(!LoopBlocks.empty() && "No edges coming in from outside the loop?"); |
| 393 | BasicBlock *NewBB = SplitBlockPredecessors(Exit, ".loopexit", LoopBlocks); |
| 394 | |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 395 | // Update Loop Information - we know that the new block will be in whichever |
| 396 | // loop the Exit block is in. Note that it may not be in that immediate loop, |
| 397 | // if the successor is some other loop header. In that case, we continue |
| 398 | // walking up the loop tree to find a loop that contains both the successor |
| 399 | // block and the predecessor block. |
| 400 | Loop *SuccLoop = LI->getLoopFor(Exit); |
| 401 | while (SuccLoop && !SuccLoop->contains(L->getHeader())) |
| 402 | SuccLoop = SuccLoop->getParentLoop(); |
| 403 | if (SuccLoop) |
| 404 | SuccLoop->addBasicBlockToLoop(NewBB, *LI); |
Chris Lattner | 74cd04e | 2003-02-28 03:07:54 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 406 | // Update dominator information (set, immdom, domtree, and domfrontier) |
| 407 | UpdateDomInfoForRevectoredPreds(NewBB, LoopBlocks); |
Chris Lattner | 59fb87d | 2004-04-18 22:27:10 +0000 | [diff] [blame] | 408 | return NewBB; |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 411 | /// AddBlockAndPredsToSet - Add the specified block, and all of its |
| 412 | /// predecessors, to the specified set, if it's not already in there. Stop |
| 413 | /// predecessor traversal when we reach StopBlock. |
Devang Patel | 58d7fbf | 2007-04-20 20:04:37 +0000 | [diff] [blame] | 414 | static void AddBlockAndPredsToSet(BasicBlock *InputBB, BasicBlock *StopBlock, |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 415 | std::set<BasicBlock*> &Blocks) { |
Devang Patel | 58d7fbf | 2007-04-20 20:04:37 +0000 | [diff] [blame] | 416 | std::vector<BasicBlock *> WorkList; |
| 417 | WorkList.push_back(InputBB); |
| 418 | do { |
| 419 | BasicBlock *BB = WorkList.back(); WorkList.pop_back(); |
| 420 | if (Blocks.insert(BB).second && BB != StopBlock) |
| 421 | // If BB is not already processed and it is not a stop block then |
| 422 | // insert its predecessor in the work list |
| 423 | for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) { |
| 424 | BasicBlock *WBB = *I; |
| 425 | WorkList.push_back(WBB); |
| 426 | } |
| 427 | } while(!WorkList.empty()); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 430 | /// FindPHIToPartitionLoops - The first part of loop-nestification is to find a |
| 431 | /// PHI node that tells us how to partition the loops. |
Owen Anderson | ad19014 | 2007-04-09 22:54:50 +0000 | [diff] [blame] | 432 | static PHINode *FindPHIToPartitionLoops(Loop *L, ETForest *EF, |
| 433 | AliasAnalysis *AA) { |
Alkis Evlogimenos | 200a360 | 2004-09-28 02:40:37 +0000 | [diff] [blame] | 434 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ) { |
| 435 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 436 | ++I; |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 437 | if (Value *V = PN->hasConstantValue()) |
Devang Patel | 3b57b6f | 2007-03-20 20:18:12 +0000 | [diff] [blame] | 438 | if (!isa<Instruction>(V) || EF->dominates(cast<Instruction>(V), PN)) { |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 439 | // This is a degenerate PHI already, don't modify it! |
| 440 | PN->replaceAllUsesWith(V); |
Chris Lattner | cec5b88 | 2005-03-25 06:37:22 +0000 | [diff] [blame] | 441 | if (AA) AA->deleteValue(PN); |
Chris Lattner | fee3411 | 2005-03-06 21:35:38 +0000 | [diff] [blame] | 442 | PN->eraseFromParent(); |
Chris Lattner | c30bda7 | 2004-10-17 21:22:38 +0000 | [diff] [blame] | 443 | continue; |
| 444 | } |
| 445 | |
| 446 | // Scan this PHI node looking for a use of the PHI node by itself. |
| 447 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 448 | if (PN->getIncomingValue(i) == PN && |
| 449 | L->contains(PN->getIncomingBlock(i))) |
| 450 | // We found something tasty to remove. |
| 451 | return PN; |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 452 | } |
| 453 | return 0; |
| 454 | } |
| 455 | |
Chris Lattner | 120fce5 | 2006-09-23 08:19:21 +0000 | [diff] [blame] | 456 | // PlaceSplitBlockCarefully - If the block isn't already, move the new block to |
| 457 | // right after some 'outside block' block. This prevents the preheader from |
| 458 | // being placed inside the loop body, e.g. when the loop hasn't been rotated. |
| 459 | void LoopSimplify::PlaceSplitBlockCarefully(BasicBlock *NewBB, |
| 460 | std::vector<BasicBlock*>&SplitPreds, |
| 461 | Loop *L) { |
| 462 | // Check to see if NewBB is already well placed. |
| 463 | Function::iterator BBI = NewBB; --BBI; |
| 464 | for (unsigned i = 0, e = SplitPreds.size(); i != e; ++i) { |
| 465 | if (&*BBI == SplitPreds[i]) |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | // If it isn't already after an outside block, move it after one. This is |
| 470 | // always good as it makes the uncond branch from the outside block into a |
| 471 | // fall-through. |
| 472 | |
| 473 | // Figure out *which* outside block to put this after. Prefer an outside |
| 474 | // block that neighbors a BB actually in the loop. |
| 475 | BasicBlock *FoundBB = 0; |
| 476 | for (unsigned i = 0, e = SplitPreds.size(); i != e; ++i) { |
| 477 | Function::iterator BBI = SplitPreds[i]; |
| 478 | if (++BBI != NewBB->getParent()->end() && |
| 479 | L->contains(BBI)) { |
| 480 | FoundBB = SplitPreds[i]; |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // If our heuristic for a *good* bb to place this after doesn't find |
| 486 | // anything, just pick something. It's likely better than leaving it within |
| 487 | // the loop. |
| 488 | if (!FoundBB) |
| 489 | FoundBB = SplitPreds[0]; |
| 490 | NewBB->moveAfter(FoundBB); |
| 491 | } |
| 492 | |
| 493 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 494 | /// SeparateNestedLoop - If this loop has multiple backedges, try to pull one of |
| 495 | /// them out into a nested loop. This is important for code that looks like |
| 496 | /// this: |
| 497 | /// |
| 498 | /// Loop: |
| 499 | /// ... |
| 500 | /// br cond, Loop, Next |
| 501 | /// ... |
| 502 | /// br cond2, Loop, Out |
| 503 | /// |
| 504 | /// To identify this common case, we look at the PHI nodes in the header of the |
| 505 | /// loop. PHI nodes with unchanging values on one backedge correspond to values |
| 506 | /// that change in the "outer" loop, but not in the "inner" loop. |
| 507 | /// |
| 508 | /// If we are able to separate out a loop, return the new outer loop that was |
| 509 | /// created. |
| 510 | /// |
| 511 | Loop *LoopSimplify::SeparateNestedLoop(Loop *L) { |
Devang Patel | 3b57b6f | 2007-03-20 20:18:12 +0000 | [diff] [blame] | 512 | ETForest *EF = getAnalysisToUpdate<ETForest>(); |
| 513 | PHINode *PN = FindPHIToPartitionLoops(L, EF, AA); |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 514 | if (PN == 0) return 0; // No known way to partition. |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 516 | // Pull out all predecessors that have varying values in the loop. This |
| 517 | // handles the case when a PHI node has multiple instances of itself as |
| 518 | // arguments. |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 519 | std::vector<BasicBlock*> OuterLoopPreds; |
Chris Lattner | 1f62f82 | 2004-04-13 15:21:18 +0000 | [diff] [blame] | 520 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 521 | if (PN->getIncomingValue(i) != PN || |
| 522 | !L->contains(PN->getIncomingBlock(i))) |
| 523 | OuterLoopPreds.push_back(PN->getIncomingBlock(i)); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 524 | |
Chris Lattner | 4b66242 | 2004-04-13 16:23:25 +0000 | [diff] [blame] | 525 | BasicBlock *Header = L->getHeader(); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 526 | BasicBlock *NewBB = SplitBlockPredecessors(Header, ".outer", OuterLoopPreds); |
| 527 | |
| 528 | // Update dominator information (set, immdom, domtree, and domfrontier) |
| 529 | UpdateDomInfoForRevectoredPreds(NewBB, OuterLoopPreds); |
| 530 | |
Chris Lattner | 120fce5 | 2006-09-23 08:19:21 +0000 | [diff] [blame] | 531 | // Make sure that NewBB is put someplace intelligent, which doesn't mess up |
| 532 | // code layout too horribly. |
| 533 | PlaceSplitBlockCarefully(NewBB, OuterLoopPreds, L); |
| 534 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 535 | // Create the new outer loop. |
| 536 | Loop *NewOuter = new Loop(); |
| 537 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 538 | // Change the parent loop to use the outer loop as its child now. |
| 539 | if (Loop *Parent = L->getParentLoop()) |
| 540 | Parent->replaceChildLoopWith(L, NewOuter); |
| 541 | else |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 542 | LI->changeTopLevelLoop(L, NewOuter); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 543 | |
| 544 | // This block is going to be our new header block: add it to this loop and all |
| 545 | // parent loops. |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 546 | NewOuter->addBasicBlockToLoop(NewBB, *LI); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 547 | |
| 548 | // L is now a subloop of our outer loop. |
| 549 | NewOuter->addChildLoop(L); |
| 550 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 551 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) |
| 552 | NewOuter->addBlockEntry(L->getBlocks()[i]); |
| 553 | |
| 554 | // Determine which blocks should stay in L and which should be moved out to |
| 555 | // the Outer loop now. |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 556 | std::set<BasicBlock*> BlocksInL; |
| 557 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); PI!=E; ++PI) |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 558 | if (EF->dominates(Header, *PI)) |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 559 | AddBlockAndPredsToSet(*PI, Header, BlocksInL); |
| 560 | |
| 561 | |
| 562 | // Scan all of the loop children of L, moving them to OuterLoop if they are |
| 563 | // not part of the inner loop. |
| 564 | for (Loop::iterator I = L->begin(); I != L->end(); ) |
| 565 | if (BlocksInL.count((*I)->getHeader())) |
| 566 | ++I; // Loop remains in L |
| 567 | else |
| 568 | NewOuter->addChildLoop(L->removeChildLoop(I)); |
| 569 | |
| 570 | // Now that we know which blocks are in L and which need to be moved to |
| 571 | // OuterLoop, move any blocks that need it. |
| 572 | for (unsigned i = 0; i != L->getBlocks().size(); ++i) { |
| 573 | BasicBlock *BB = L->getBlocks()[i]; |
| 574 | if (!BlocksInL.count(BB)) { |
| 575 | // Move this block to the parent, updating the exit blocks sets |
| 576 | L->removeBlockFromLoop(BB); |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 577 | if ((*LI)[BB] == L) |
| 578 | LI->changeLoopFor(BB, NewOuter); |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 579 | --i; |
| 580 | } |
| 581 | } |
| 582 | |
Chris Lattner | 529b28d | 2004-04-13 05:05:33 +0000 | [diff] [blame] | 583 | return NewOuter; |
| 584 | } |
| 585 | |
| 586 | |
| 587 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 588 | /// InsertUniqueBackedgeBlock - This method is called when the specified loop |
| 589 | /// has more than one backedge in it. If this occurs, revector all of these |
| 590 | /// backedges to target a new basic block and have that block branch to the loop |
| 591 | /// header. This ensures that loops have exactly one backedge. |
| 592 | /// |
| 593 | void LoopSimplify::InsertUniqueBackedgeBlock(Loop *L) { |
| 594 | assert(L->getNumBackEdges() > 1 && "Must have > 1 backedge!"); |
| 595 | |
| 596 | // Get information about the loop |
| 597 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 598 | BasicBlock *Header = L->getHeader(); |
| 599 | Function *F = Header->getParent(); |
| 600 | |
| 601 | // Figure out which basic blocks contain back-edges to the loop header. |
| 602 | std::vector<BasicBlock*> BackedgeBlocks; |
| 603 | for (pred_iterator I = pred_begin(Header), E = pred_end(Header); I != E; ++I) |
| 604 | if (*I != Preheader) BackedgeBlocks.push_back(*I); |
| 605 | |
| 606 | // Create and insert the new backedge block... |
| 607 | BasicBlock *BEBlock = new BasicBlock(Header->getName()+".backedge", F); |
Chris Lattner | 108e4ab | 2003-11-21 16:52:05 +0000 | [diff] [blame] | 608 | BranchInst *BETerminator = new BranchInst(Header, BEBlock); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 609 | |
| 610 | // Move the new backedge block to right after the last backedge block. |
| 611 | Function::iterator InsertPos = BackedgeBlocks.back(); ++InsertPos; |
| 612 | F->getBasicBlockList().splice(InsertPos, F->getBasicBlockList(), BEBlock); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 613 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 614 | // Now that the block has been inserted into the function, create PHI nodes in |
| 615 | // the backedge block which correspond to any PHI nodes in the header block. |
Alkis Evlogimenos | 200a360 | 2004-09-28 02:40:37 +0000 | [diff] [blame] | 616 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 617 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 618 | PHINode *NewPN = new PHINode(PN->getType(), PN->getName()+".be", |
| 619 | BETerminator); |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 620 | NewPN->reserveOperandSpace(BackedgeBlocks.size()); |
Chris Lattner | cec5b88 | 2005-03-25 06:37:22 +0000 | [diff] [blame] | 621 | if (AA) AA->copyValue(PN, NewPN); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 622 | |
| 623 | // Loop over the PHI node, moving all entries except the one for the |
| 624 | // preheader over to the new PHI node. |
| 625 | unsigned PreheaderIdx = ~0U; |
| 626 | bool HasUniqueIncomingValue = true; |
| 627 | Value *UniqueValue = 0; |
| 628 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 629 | BasicBlock *IBB = PN->getIncomingBlock(i); |
| 630 | Value *IV = PN->getIncomingValue(i); |
| 631 | if (IBB == Preheader) { |
| 632 | PreheaderIdx = i; |
| 633 | } else { |
| 634 | NewPN->addIncoming(IV, IBB); |
| 635 | if (HasUniqueIncomingValue) { |
| 636 | if (UniqueValue == 0) |
| 637 | UniqueValue = IV; |
| 638 | else if (UniqueValue != IV) |
| 639 | HasUniqueIncomingValue = false; |
| 640 | } |
| 641 | } |
| 642 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 644 | // Delete all of the incoming values from the old PN except the preheader's |
| 645 | assert(PreheaderIdx != ~0U && "PHI has no preheader entry??"); |
| 646 | if (PreheaderIdx != 0) { |
| 647 | PN->setIncomingValue(0, PN->getIncomingValue(PreheaderIdx)); |
| 648 | PN->setIncomingBlock(0, PN->getIncomingBlock(PreheaderIdx)); |
| 649 | } |
Chris Lattner | 5551706 | 2005-01-29 00:39:08 +0000 | [diff] [blame] | 650 | // Nuke all entries except the zero'th. |
| 651 | for (unsigned i = 0, e = PN->getNumIncomingValues()-1; i != e; ++i) |
| 652 | PN->removeIncomingValue(e-i, false); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 653 | |
| 654 | // Finally, add the newly constructed PHI node as the entry for the BEBlock. |
| 655 | PN->addIncoming(NewPN, BEBlock); |
| 656 | |
| 657 | // As an optimization, if all incoming values in the new PhiNode (which is a |
| 658 | // subset of the incoming values of the old PHI node) have the same value, |
| 659 | // eliminate the PHI Node. |
| 660 | if (HasUniqueIncomingValue) { |
| 661 | NewPN->replaceAllUsesWith(UniqueValue); |
Chris Lattner | cec5b88 | 2005-03-25 06:37:22 +0000 | [diff] [blame] | 662 | if (AA) AA->deleteValue(NewPN); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 663 | BEBlock->getInstList().erase(NewPN); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // Now that all of the PHI nodes have been inserted and adjusted, modify the |
| 668 | // backedge blocks to just to the BEBlock instead of the header. |
| 669 | for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) { |
| 670 | TerminatorInst *TI = BackedgeBlocks[i]->getTerminator(); |
| 671 | for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op) |
| 672 | if (TI->getSuccessor(Op) == Header) |
| 673 | TI->setSuccessor(Op, BEBlock); |
| 674 | } |
| 675 | |
| 676 | //===--- Update all analyses which we must preserve now -----------------===// |
| 677 | |
| 678 | // Update Loop Information - we know that this block is now in the current |
| 679 | // loop and all parent loops. |
Chris Lattner | c27e056 | 2006-02-14 22:34:08 +0000 | [diff] [blame] | 680 | L->addBasicBlockToLoop(BEBlock, *LI); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 682 | // Update dominator information (set, immdom, domtree, and domfrontier) |
| 683 | UpdateDomInfoForRevectoredPreds(BEBlock, BackedgeBlocks); |
| 684 | } |
| 685 | |
Owen Anderson | 17cba6d | 2007-04-09 22:25:09 +0000 | [diff] [blame] | 686 | // Returns true if BasicBlock A dominates at least one block in vector B |
| 687 | // Helper function for UpdateDomInfoForRevectoredPreds |
Owen Anderson | cc221cd | 2007-04-09 23:38:18 +0000 | [diff] [blame] | 688 | static bool BlockDominatesAny(BasicBlock* A, const std::vector<BasicBlock*>& B, |
| 689 | ETForest& ETF) { |
| 690 | for (std::vector<BasicBlock*>::const_iterator BI = B.begin(), BE = B.end(); |
| 691 | BI != BE; ++BI) { |
Owen Anderson | 0cd0461 | 2007-04-09 22:31:43 +0000 | [diff] [blame] | 692 | if (ETF.dominates(A, *BI)) |
| 693 | return true; |
| 694 | } |
| 695 | return false; |
Owen Anderson | 17cba6d | 2007-04-09 22:25:09 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 698 | /// UpdateDomInfoForRevectoredPreds - This method is used to update the four |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 699 | /// different kinds of dominator information (immediate dominators, |
| 700 | /// dominator trees, et-forest and dominance frontiers) after a new block has |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 701 | /// been added to the CFG. |
| 702 | /// |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 703 | /// This only supports the case when an existing block (known as "NewBBSucc"), |
| 704 | /// had some of its predecessors factored into a new basic block. This |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 705 | /// transformation inserts a new basic block ("NewBB"), with a single |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 706 | /// unconditional branch to NewBBSucc, and moves some predecessors of |
| 707 | /// "NewBBSucc" to now branch to NewBB. These predecessors are listed in |
| 708 | /// PredBlocks, even though they are the same as |
| 709 | /// pred_begin(NewBB)/pred_end(NewBB). |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 710 | /// |
| 711 | void LoopSimplify::UpdateDomInfoForRevectoredPreds(BasicBlock *NewBB, |
| 712 | std::vector<BasicBlock*> &PredBlocks) { |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 713 | assert(!PredBlocks.empty() && "No predblocks??"); |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 714 | assert(succ_begin(NewBB) != succ_end(NewBB) && |
| 715 | ++succ_begin(NewBB) == succ_end(NewBB) && |
| 716 | "NewBB should have a single successor!"); |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 717 | BasicBlock *NewBBSucc = *succ_begin(NewBB); |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 718 | ETForest& ETF = getAnalysis<ETForest>(); |
| 719 | |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 720 | // The newly inserted basic block will dominate existing basic blocks iff the |
| 721 | // PredBlocks dominate all of the non-pred blocks. If all predblocks dominate |
| 722 | // the non-pred blocks, then they all must be the same block! |
Chris Lattner | 4f303bd | 2004-04-01 19:06:07 +0000 | [diff] [blame] | 723 | // |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 724 | bool NewBBDominatesNewBBSucc = true; |
| 725 | { |
| 726 | BasicBlock *OnePred = PredBlocks[0]; |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 727 | unsigned i = 1, e = PredBlocks.size(); |
Owen Anderson | 558fc74 | 2007-04-09 00:52:49 +0000 | [diff] [blame] | 728 | for (i = 1; !ETF.isReachableFromEntry(OnePred); ++i) { |
Chris Lattner | c398457 | 2006-09-23 07:40:52 +0000 | [diff] [blame] | 729 | assert(i != e && "Didn't find reachable pred?"); |
| 730 | OnePred = PredBlocks[i]; |
| 731 | } |
| 732 | |
| 733 | for (; i != e; ++i) |
Owen Anderson | 558fc74 | 2007-04-09 00:52:49 +0000 | [diff] [blame] | 734 | if (PredBlocks[i] != OnePred && ETF.isReachableFromEntry(OnePred)){ |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 735 | NewBBDominatesNewBBSucc = false; |
| 736 | break; |
| 737 | } |
| 738 | |
| 739 | if (NewBBDominatesNewBBSucc) |
| 740 | for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc); |
| 741 | PI != E; ++PI) |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 742 | if (*PI != NewBB && !ETF.dominates(NewBBSucc, *PI)) { |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 743 | NewBBDominatesNewBBSucc = false; |
| 744 | break; |
| 745 | } |
| 746 | } |
| 747 | |
Chris Lattner | 4f303bd | 2004-04-01 19:06:07 +0000 | [diff] [blame] | 748 | // The other scenario where the new block can dominate its successors are when |
| 749 | // all predecessors of NewBBSucc that are not NewBB are dominated by NewBBSucc |
| 750 | // already. |
| 751 | if (!NewBBDominatesNewBBSucc) { |
| 752 | NewBBDominatesNewBBSucc = true; |
| 753 | for (pred_iterator PI = pred_begin(NewBBSucc), E = pred_end(NewBBSucc); |
| 754 | PI != E; ++PI) |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 755 | if (*PI != NewBB && !ETF.dominates(NewBBSucc, *PI)) { |
Chris Lattner | 4f303bd | 2004-04-01 19:06:07 +0000 | [diff] [blame] | 756 | NewBBDominatesNewBBSucc = false; |
| 757 | break; |
| 758 | } |
| 759 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 760 | |
Owen Anderson | e9ed445 | 2007-04-07 18:23:27 +0000 | [diff] [blame] | 761 | BasicBlock *NewBBIDom = 0; |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 762 | |
| 763 | // Update DominatorTree information if it is active. |
| 764 | if (DominatorTree *DT = getAnalysisToUpdate<DominatorTree>()) { |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 765 | // If we don't have ImmediateDominator info around, calculate the idom as |
| 766 | // above. |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 767 | if (!NewBBIDom) { |
| 768 | unsigned i = 0; |
| 769 | for (i = 0; i < PredBlocks.size(); ++i) |
| 770 | if (ETF.dominates(&PredBlocks[i]->getParent()->getEntryBlock(), PredBlocks[i])) { |
| 771 | NewBBIDom = PredBlocks[i]; |
| 772 | break; |
| 773 | } |
| 774 | assert(i != PredBlocks.size() && "No reachable preds?"); |
| 775 | for (i = i + 1; i < PredBlocks.size(); ++i) { |
| 776 | if (ETF.dominates(&PredBlocks[i]->getParent()->getEntryBlock(), PredBlocks[i])) |
| 777 | NewBBIDom = ETF.nearestCommonDominator(NewBBIDom, PredBlocks[i]); |
Chris Lattner | c398457 | 2006-09-23 07:40:52 +0000 | [diff] [blame] | 778 | } |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 779 | assert(NewBBIDom && "No immediate dominator found??"); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 780 | } |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 781 | DominatorTree::Node *NewBBIDomNode = DT->getNode(NewBBIDom); |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 782 | |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 783 | // Create the new dominator tree node... and set the idom of NewBB. |
| 784 | DominatorTree::Node *NewBBNode = DT->createNewNode(NewBB, NewBBIDomNode); |
| 785 | |
| 786 | // If NewBB strictly dominates other blocks, then it is now the immediate |
| 787 | // dominator of NewBBSucc. Update the dominator tree as appropriate. |
| 788 | if (NewBBDominatesNewBBSucc) { |
| 789 | DominatorTree::Node *NewBBSuccNode = DT->getNode(NewBBSucc); |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 790 | DT->changeImmediateDominator(NewBBSuccNode, NewBBNode); |
| 791 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Chris Lattner | baec98d | 2006-01-09 08:03:08 +0000 | [diff] [blame] | 794 | // Update ET-Forest information if it is active. |
| 795 | if (ETForest *EF = getAnalysisToUpdate<ETForest>()) { |
| 796 | EF->addNewBlock(NewBB, NewBBIDom); |
| 797 | if (NewBBDominatesNewBBSucc) |
| 798 | EF->setImmediateDominator(NewBBSucc, NewBB); |
| 799 | } |
| 800 | |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 801 | // Update dominance frontier information... |
| 802 | if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) { |
Chris Lattner | 4b66242 | 2004-04-13 16:23:25 +0000 | [diff] [blame] | 803 | // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the |
| 804 | // DF(PredBlocks[0]) without the stuff that the new block does not dominate |
| 805 | // a predecessor of. |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 806 | if (NewBBDominatesNewBBSucc) { |
| 807 | DominanceFrontier::iterator DFI = DF->find(PredBlocks[0]); |
| 808 | if (DFI != DF->end()) { |
| 809 | DominanceFrontier::DomSetType Set = DFI->second; |
| 810 | // Filter out stuff in Set that we do not dominate a predecessor of. |
| 811 | for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(), |
| 812 | E = Set.end(); SetI != E;) { |
| 813 | bool DominatesPred = false; |
| 814 | for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI); |
| 815 | PI != E; ++PI) |
Nick Lewycky | a397ce1 | 2007-04-08 01:04:30 +0000 | [diff] [blame] | 816 | if (ETF.dominates(NewBB, *PI)) |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 817 | DominatesPred = true; |
| 818 | if (!DominatesPred) |
| 819 | Set.erase(SetI++); |
| 820 | else |
| 821 | ++SetI; |
| 822 | } |
Chris Lattner | dbf3cd7 | 2003-02-27 20:27:08 +0000 | [diff] [blame] | 823 | |
Chris Lattner | 4f02fc2 | 2004-02-05 21:12:24 +0000 | [diff] [blame] | 824 | DF->addBasicBlock(NewBB, Set); |
| 825 | } |
| 826 | |
| 827 | } else { |
| 828 | // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate |
| 829 | // NewBBSucc, but it does dominate itself (and there is an edge (NewBB -> |
| 830 | // NewBBSucc)). NewBBSucc is the single successor of NewBB. |
| 831 | DominanceFrontier::DomSetType NewDFSet; |
| 832 | NewDFSet.insert(NewBBSucc); |
| 833 | DF->addBasicBlock(NewBB, NewDFSet); |
Chris Lattner | 4b66242 | 2004-04-13 16:23:25 +0000 | [diff] [blame] | 834 | } |
Chris Lattner | 2ab6a73 | 2003-10-13 00:37:13 +0000 | [diff] [blame] | 835 | |
Chris Lattner | 4b66242 | 2004-04-13 16:23:25 +0000 | [diff] [blame] | 836 | // Now we must loop over all of the dominance frontiers in the function, |
| 837 | // replacing occurrences of NewBBSucc with NewBB in some cases. All |
| 838 | // blocks that dominate a block in PredBlocks and contained NewBBSucc in |
| 839 | // their dominance frontier must be updated to contain NewBB instead. |
| 840 | // |
Owen Anderson | 17cba6d | 2007-04-09 22:25:09 +0000 | [diff] [blame] | 841 | for (Function::iterator FI = NewBB->getParent()->begin(), |
Owen Anderson | 0cd0461 | 2007-04-09 22:31:43 +0000 | [diff] [blame] | 842 | FE = NewBB->getParent()->end(); FI != FE; ++FI) { |
| 843 | DominanceFrontier::iterator DFI = DF->find(FI); |
| 844 | if (DFI == DF->end()) continue; // unreachable block. |
| 845 | |
| 846 | // Only consider dominators of NewBBSucc |
| 847 | if (!DFI->second.count(NewBBSucc)) continue; |
Owen Anderson | ad19014 | 2007-04-09 22:54:50 +0000 | [diff] [blame] | 848 | |
Owen Anderson | 0cd0461 | 2007-04-09 22:31:43 +0000 | [diff] [blame] | 849 | if (BlockDominatesAny(FI, PredBlocks, ETF)) { |
| 850 | // If NewBBSucc should not stay in our dominator frontier, remove it. |
| 851 | // We remove it unless there is a predecessor of NewBBSucc that we |
| 852 | // dominate, but we don't strictly dominate NewBBSucc. |
| 853 | bool ShouldRemove = true; |
| 854 | if ((BasicBlock*)FI == NewBBSucc || !ETF.dominates(FI, NewBBSucc)) { |
| 855 | // Okay, we know that PredDom does not strictly dominate NewBBSucc. |
| 856 | // Check to see if it dominates any predecessors of NewBBSucc. |
| 857 | for (pred_iterator PI = pred_begin(NewBBSucc), |
| 858 | E = pred_end(NewBBSucc); PI != E; ++PI) |
| 859 | if (ETF.dominates(FI, *PI)) { |
| 860 | ShouldRemove = false; |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | if (ShouldRemove) |
| 865 | DF->removeFromFrontier(DFI, NewBBSucc); |
| 866 | DF->addToFrontier(DFI, NewBB); |
| 867 | |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | } |
Chris Lattner | 38acf9e | 2002-09-26 16:17:31 +0000 | [diff] [blame] | 873 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 874 | |
Owen Anderson | 17cba6d | 2007-04-09 22:25:09 +0000 | [diff] [blame] | 875 | |