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