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