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