Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 1 | //===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===// |
| 2 | // |
| 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. |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements Loop Rotation Pass. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Devang Patel | 3223133 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "loop-rotate" |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar.h" |
| 16 | #include "llvm/Function.h" |
Devang Patel | 3fc178f | 2011-02-14 23:03:23 +0000 | [diff] [blame] | 17 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | d9e0797 | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/CodeMetrics.h" |
Chris Lattner | d9ec357 | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopPass.h" |
| 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Devang Patel | 990e866 | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ScalarEvolution.h" |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/Local.h" |
Devang Patel | 990e866 | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Dan Gohman | e6e37b9 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Chris Lattner | 6ccb365 | 2011-01-08 07:21:31 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/ValueMapper.h" |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/ADT/Statistic.h" |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | #define MAX_HEADER_SIZE 16 |
| 31 | |
| 32 | STATISTIC(NumRotated, "Number of loops rotated"); |
| 33 | namespace { |
| 34 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 35 | class LoopRotate : public LoopPass { |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 36 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 37 | static char ID; // Pass ID, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 38 | LoopRotate() : LoopPass(ID) { |
| 39 | initializeLoopRotatePass(*PassRegistry::getPassRegistry()); |
| 40 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 41 | |
Devang Patel | 3223133 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 42 | // LCSSA form makes instruction renaming easier. |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 43 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 1e381fc | 2010-07-16 17:58:45 +0000 | [diff] [blame] | 44 | AU.addPreserved<DominatorTree>(); |
Dan Gohman | 1e381fc | 2010-07-16 17:58:45 +0000 | [diff] [blame] | 45 | AU.addRequired<LoopInfo>(); |
| 46 | AU.addPreserved<LoopInfo>(); |
Devang Patel | 9d9b204 | 2008-02-15 01:24:49 +0000 | [diff] [blame] | 47 | AU.addRequiredID(LoopSimplifyID); |
| 48 | AU.addPreservedID(LoopSimplifyID); |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 49 | AU.addRequiredID(LCSSAID); |
| 50 | AU.addPreservedID(LCSSAID); |
Devang Patel | 990e866 | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 51 | AU.addPreserved<ScalarEvolution>(); |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Chris Lattner | a1ae0c7 | 2011-01-08 18:55:50 +0000 | [diff] [blame] | 54 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
Chris Lattner | 4aefc9b | 2011-01-08 17:48:33 +0000 | [diff] [blame] | 55 | bool rotateLoop(Loop *L); |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 56 | |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 57 | private: |
Chris Lattner | 012ca94 | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 58 | LoopInfo *LI; |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 59 | }; |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 60 | } |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 61 | |
| 62 | char LoopRotate::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 63 | INITIALIZE_PASS_BEGIN(LoopRotate, "loop-rotate", "Rotate Loops", false, false) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 64 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 65 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 66 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 67 | INITIALIZE_PASS_END(LoopRotate, "loop-rotate", "Rotate Loops", false, false) |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 394f044 | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 69 | Pass *llvm::createLoopRotatePass() { return new LoopRotate(); } |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 70 | |
Devang Patel | 3223133 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 71 | /// Rotate Loop L as many times as possible. Return true if |
Dan Gohman | cc4e605 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 72 | /// the loop is rotated at least once. |
Chris Lattner | 4aefc9b | 2011-01-08 17:48:33 +0000 | [diff] [blame] | 73 | bool LoopRotate::runOnLoop(Loop *L, LPPassManager &LPM) { |
Chris Lattner | 012ca94 | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 74 | LI = &getAnalysis<LoopInfo>(); |
Devang Patel | 990e866 | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 75 | |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 76 | // One loop can be rotated multiple times. |
Chris Lattner | 012ca94 | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 77 | bool MadeChange = false; |
Chris Lattner | 4aefc9b | 2011-01-08 17:48:33 +0000 | [diff] [blame] | 78 | while (rotateLoop(L)) |
Chris Lattner | 012ca94 | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 79 | MadeChange = true; |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 012ca94 | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 81 | return MadeChange; |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | 64c24db | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 84 | /// RewriteUsesOfClonedInstructions - We just cloned the instructions from the |
| 85 | /// old header into the preheader. If there were uses of the values produced by |
| 86 | /// these instruction that were outside of the loop, we have to insert PHI nodes |
| 87 | /// to merge the two values. Do this now. |
| 88 | static void RewriteUsesOfClonedInstructions(BasicBlock *OrigHeader, |
| 89 | BasicBlock *OrigPreheader, |
| 90 | ValueToValueMapTy &ValueMap) { |
| 91 | // Remove PHI node entries that are no longer live. |
| 92 | BasicBlock::iterator I, E = OrigHeader->end(); |
| 93 | for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 94 | PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreheader)); |
| 95 | |
| 96 | // Now fix up users of the instructions in OrigHeader, inserting PHI nodes |
| 97 | // as necessary. |
| 98 | SSAUpdater SSA; |
| 99 | for (I = OrigHeader->begin(); I != E; ++I) { |
| 100 | Value *OrigHeaderVal = I; |
| 101 | |
| 102 | // If there are no uses of the value (e.g. because it returns void), there |
| 103 | // is nothing to rewrite. |
| 104 | if (OrigHeaderVal->use_empty()) |
| 105 | continue; |
| 106 | |
| 107 | Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal]; |
| 108 | |
| 109 | // The value now exits in two versions: the initial value in the preheader |
| 110 | // and the loop "next" value in the original header. |
| 111 | SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName()); |
| 112 | SSA.AddAvailableValue(OrigHeader, OrigHeaderVal); |
| 113 | SSA.AddAvailableValue(OrigPreheader, OrigPreHeaderVal); |
| 114 | |
| 115 | // Visit each use of the OrigHeader instruction. |
| 116 | for (Value::use_iterator UI = OrigHeaderVal->use_begin(), |
| 117 | UE = OrigHeaderVal->use_end(); UI != UE; ) { |
| 118 | // Grab the use before incrementing the iterator. |
| 119 | Use &U = UI.getUse(); |
| 120 | |
| 121 | // Increment the iterator before removing the use from the list. |
| 122 | ++UI; |
| 123 | |
| 124 | // SSAUpdater can't handle a non-PHI use in the same block as an |
| 125 | // earlier def. We can easily handle those cases manually. |
| 126 | Instruction *UserInst = cast<Instruction>(U.getUser()); |
| 127 | if (!isa<PHINode>(UserInst)) { |
| 128 | BasicBlock *UserBB = UserInst->getParent(); |
| 129 | |
| 130 | // The original users in the OrigHeader are already using the |
| 131 | // original definitions. |
| 132 | if (UserBB == OrigHeader) |
| 133 | continue; |
| 134 | |
| 135 | // Users in the OrigPreHeader need to use the value to which the |
| 136 | // original definitions are mapped. |
| 137 | if (UserBB == OrigPreheader) { |
| 138 | U = OrigPreHeaderVal; |
| 139 | continue; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Anything else can be handled by SSAUpdater. |
| 144 | SSA.RewriteUse(U); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Dan Gohman | 23d9d27 | 2007-05-11 21:10:54 +0000 | [diff] [blame] | 149 | /// Rotate loop LP. Return true if the loop is rotated. |
Chris Lattner | 4aefc9b | 2011-01-08 17:48:33 +0000 | [diff] [blame] | 150 | bool LoopRotate::rotateLoop(Loop *L) { |
Dan Gohman | cc4e605 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 151 | // If the loop has only one block then there is not much to rotate. |
Devang Patel | 3223133 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 152 | if (L->getBlocks().size() == 1) |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 153 | return false; |
Chris Lattner | 2aa6908 | 2011-01-08 18:06:22 +0000 | [diff] [blame] | 154 | |
| 155 | BasicBlock *OrigHeader = L->getHeader(); |
| 156 | |
| 157 | BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator()); |
| 158 | if (BI == 0 || BI->isUnconditional()) |
| 159 | return false; |
| 160 | |
Dan Gohman | cc4e605 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 161 | // If the loop header is not one of the loop exiting blocks then |
| 162 | // either this loop is already rotated or it is not |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 163 | // suitable for loop rotation transformations. |
Dan Gohman | 32663b7 | 2009-10-24 23:34:26 +0000 | [diff] [blame] | 164 | if (!L->isLoopExiting(OrigHeader)) |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 165 | return false; |
| 166 | |
Devang Patel | 3223133 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 167 | // Updating PHInodes in loops with multiple exits adds complexity. |
| 168 | // Keep it simple, and restrict loop rotation to loops with one exit only. |
| 169 | // In future, lift this restriction and support for multiple exits if |
| 170 | // required. |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 171 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 172 | L->getExitBlocks(ExitBlocks); |
| 173 | if (ExitBlocks.size() > 1) |
| 174 | return false; |
| 175 | |
Chris Lattner | d9e0797 | 2011-01-02 07:35:53 +0000 | [diff] [blame] | 176 | // Check size of original header and reject loop if it is very big. |
| 177 | { |
| 178 | CodeMetrics Metrics; |
| 179 | Metrics.analyzeBasicBlock(OrigHeader); |
| 180 | if (Metrics.NumInsts > MAX_HEADER_SIZE) |
| 181 | return false; |
Devang Patel | 3f43a70 | 2009-03-06 03:51:30 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Devang Patel | 990e866 | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 184 | // Now, this loop is suitable for rotation. |
Chris Lattner | 64c24db | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 185 | BasicBlock *OrigPreheader = L->getLoopPreheader(); |
Chris Lattner | 2aa6908 | 2011-01-08 18:06:22 +0000 | [diff] [blame] | 186 | BasicBlock *OrigLatch = L->getLoopLatch(); |
Chris Lattner | 64c24db | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 187 | assert(OrigPreheader && OrigLatch && "Loop not in canonical form?"); |
Devang Patel | 990e866 | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 188 | |
Dan Gohman | e6fe67b | 2009-09-27 15:37:03 +0000 | [diff] [blame] | 189 | // Anything ScalarEvolution may know about this loop or the PHI nodes |
| 190 | // in its header will soon be invalidated. |
| 191 | if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>()) |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 192 | SE->forgetLoop(L); |
Dan Gohman | e6fe67b | 2009-09-27 15:37:03 +0000 | [diff] [blame] | 193 | |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 194 | // Find new Loop header. NewHeader is a Header's one and only successor |
Chris Lattner | f6784a3 | 2009-01-26 01:57:01 +0000 | [diff] [blame] | 195 | // that is inside loop. Header's other successor is outside the |
| 196 | // loop. Otherwise loop is not suitable for rotation. |
Chris Lattner | 4aefc9b | 2011-01-08 17:48:33 +0000 | [diff] [blame] | 197 | BasicBlock *Exit = BI->getSuccessor(0); |
| 198 | BasicBlock *NewHeader = BI->getSuccessor(1); |
Devang Patel | 3223133 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 199 | if (L->contains(Exit)) |
| 200 | std::swap(Exit, NewHeader); |
Chris Lattner | 2ba2543 | 2009-01-26 01:38:24 +0000 | [diff] [blame] | 201 | assert(NewHeader && "Unable to determine new loop header"); |
Devang Patel | 3223133 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 202 | assert(L->contains(NewHeader) && !L->contains(Exit) && |
| 203 | "Unable to determine loop header and exit blocks"); |
Chris Lattner | 3796a26 | 2009-01-26 02:11:30 +0000 | [diff] [blame] | 204 | |
Dan Gohman | cc4e605 | 2009-06-25 00:22:44 +0000 | [diff] [blame] | 205 | // This code assumes that the new header has exactly one predecessor. |
| 206 | // Remove any single-entry PHI nodes in it. |
Chris Lattner | 3796a26 | 2009-01-26 02:11:30 +0000 | [diff] [blame] | 207 | assert(NewHeader->getSinglePredecessor() && |
| 208 | "New header doesn't have one pred!"); |
| 209 | FoldSingleEntryPHINodes(NewHeader); |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 210 | |
Dan Gohman | e6e37b9 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 211 | // Begin by walking OrigHeader and populating ValueMap with an entry for |
| 212 | // each Instruction. |
Devang Patel | 3223133 | 2007-04-09 16:11:48 +0000 | [diff] [blame] | 213 | BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end(); |
Chris Lattner | 6ccb365 | 2011-01-08 07:21:31 +0000 | [diff] [blame] | 214 | ValueToValueMapTy ValueMap; |
Devang Patel | e988154 | 2007-04-09 19:04:21 +0000 | [diff] [blame] | 215 | |
Dan Gohman | e6e37b9 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 216 | // For PHI nodes, the value available in OldPreHeader is just the |
| 217 | // incoming value from OldPreHeader. |
| 218 | for (; PHINode *PN = dyn_cast<PHINode>(I); ++I) |
Chris Lattner | 64c24db | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 219 | ValueMap[PN] = PN->getIncomingValue(PN->getBasicBlockIndex(OrigPreheader)); |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 50fb469 | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 221 | // For the rest of the instructions, either hoist to the OrigPreheader if |
| 222 | // possible or create a clone in the OldPreHeader if not. |
Chris Lattner | 64c24db | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 223 | TerminatorInst *LoopEntryBranch = OrigPreheader->getTerminator(); |
Chris Lattner | 50fb469 | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 224 | while (I != E) { |
| 225 | Instruction *Inst = I++; |
| 226 | |
| 227 | // If the instruction's operands are invariant and it doesn't read or write |
| 228 | // memory, then it is safe to hoist. Doing this doesn't change the order of |
| 229 | // execution in the preheader, but does prevent the instruction from |
| 230 | // executing in each iteration of the loop. This means it is safe to hoist |
| 231 | // something that might trap, but isn't safe to hoist something that reads |
| 232 | // memory (without proving that the loop doesn't write). |
| 233 | if (L->hasLoopInvariantOperands(Inst) && |
| 234 | !Inst->mayReadFromMemory() && !Inst->mayWriteToMemory() && |
Devang Patel | 3fc178f | 2011-02-14 23:03:23 +0000 | [diff] [blame] | 235 | !isa<TerminatorInst>(Inst) && !isa<DbgInfoIntrinsic>(Inst)) { |
Chris Lattner | 50fb469 | 2010-09-06 01:10:22 +0000 | [diff] [blame] | 236 | Inst->moveBefore(LoopEntryBranch); |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | // Otherwise, create a duplicate of the instruction. |
| 241 | Instruction *C = Inst->clone(); |
Chris Lattner | b5fa5fc | 2011-01-08 08:15:20 +0000 | [diff] [blame] | 242 | |
Chris Lattner | d9ec357 | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 243 | // Eagerly remap the operands of the instruction. |
| 244 | RemapInstruction(C, ValueMap, |
| 245 | RF_NoModuleLevelChanges|RF_IgnoreMissingEntries); |
| 246 | |
| 247 | // With the operands remapped, see if the instruction constant folds or is |
| 248 | // otherwise simplifyable. This commonly occurs because the entry from PHI |
| 249 | // nodes allows icmps and other instructions to fold. |
Chris Lattner | 012ca94 | 2011-01-08 17:38:45 +0000 | [diff] [blame] | 250 | Value *V = SimplifyInstruction(C); |
| 251 | if (V && LI->replacementPreservesLCSSAForm(C, V)) { |
Chris Lattner | d9ec357 | 2011-01-08 08:24:46 +0000 | [diff] [blame] | 252 | // If so, then delete the temporary instruction and stick the folded value |
| 253 | // in the map. |
| 254 | delete C; |
| 255 | ValueMap[Inst] = V; |
| 256 | } else { |
| 257 | // Otherwise, stick the new instruction into the new block! |
| 258 | C->setName(Inst->getName()); |
| 259 | C->insertBefore(LoopEntryBranch); |
| 260 | ValueMap[Inst] = C; |
| 261 | } |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Dan Gohman | e6e37b9 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 264 | // Along with all the other instructions, we just cloned OrigHeader's |
| 265 | // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's |
| 266 | // successors by duplicating their incoming values for OrigHeader. |
| 267 | TerminatorInst *TI = OrigHeader->getTerminator(); |
| 268 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 269 | for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin(); |
| 270 | PHINode *PN = dyn_cast<PHINode>(BI); ++BI) |
Chris Lattner | 64c24db | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 271 | PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreheader); |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 272 | |
Dan Gohman | e6e37b9 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 273 | // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove |
| 274 | // OrigPreHeader's old terminator (the original branch into the loop), and |
| 275 | // remove the corresponding incoming values from the PHI nodes in OrigHeader. |
| 276 | LoopEntryBranch->eraseFromParent(); |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 64c24db | 2011-01-08 19:26:33 +0000 | [diff] [blame] | 278 | // If there were any uses of instructions in the duplicated block outside the |
| 279 | // loop, update them, inserting PHI nodes as required |
| 280 | RewriteUsesOfClonedInstructions(OrigHeader, OrigPreheader, ValueMap); |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 281 | |
Dan Gohman | e6e37b9 | 2009-10-24 23:19:52 +0000 | [diff] [blame] | 282 | // NewHeader is now the header of the loop. |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 283 | L->moveToHeader(NewHeader); |
Chris Lattner | 883401a | 2011-01-08 19:10:28 +0000 | [diff] [blame] | 284 | assert(L->getHeader() == NewHeader && "Latch block is our new header"); |
Devang Patel | c4625da | 2007-04-07 01:25:15 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 5d37370 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 286 | |
| 287 | // At this point, we've finished our major CFG changes. As part of cloning |
| 288 | // the loop into the preheader we've simplified instructions and the |
| 289 | // duplicated conditional branch may now be branching on a constant. If it is |
| 290 | // branching on a constant and if that constant means that we enter the loop, |
| 291 | // then we fold away the cond branch to an uncond branch. This simplifies the |
| 292 | // loop in cases important for nested loops, and it also means we don't have |
| 293 | // to split as many edges. |
| 294 | BranchInst *PHBI = cast<BranchInst>(OrigPreheader->getTerminator()); |
| 295 | assert(PHBI->isConditional() && "Should be clone of BI condbr!"); |
| 296 | if (!isa<ConstantInt>(PHBI->getCondition()) || |
| 297 | PHBI->getSuccessor(cast<ConstantInt>(PHBI->getCondition())->isZero()) |
| 298 | != NewHeader) { |
| 299 | // The conditional branch can't be folded, handle the general case. |
| 300 | // Update DominatorTree to reflect the CFG change we just made. Then split |
| 301 | // edges as necessary to preserve LoopSimplify form. |
| 302 | if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) { |
| 303 | // Since OrigPreheader now has the conditional branch to Exit block, it is |
| 304 | // the dominator of Exit. |
| 305 | DT->changeImmediateDominator(Exit, OrigPreheader); |
| 306 | DT->changeImmediateDominator(NewHeader, OrigPreheader); |
| 307 | |
| 308 | // Update OrigHeader to be dominated by the new header block. |
| 309 | DT->changeImmediateDominator(OrigHeader, OrigLatch); |
| 310 | } |
Chris Lattner | 0e4a154 | 2011-01-08 18:52:51 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 5d37370 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 312 | // Right now OrigPreHeader has two successors, NewHeader and ExitBlock, and |
| 313 | // thus is not a preheader anymore. Split the edge to form a real preheader. |
| 314 | BasicBlock *NewPH = SplitCriticalEdge(OrigPreheader, NewHeader, this); |
| 315 | NewPH->setName(NewHeader->getName() + ".lr.ph"); |
| 316 | |
| 317 | // Preserve canonical loop form, which means that 'Exit' should have only one |
| 318 | // predecessor. |
| 319 | BasicBlock *ExitSplit = SplitCriticalEdge(L->getLoopLatch(), Exit, this); |
| 320 | ExitSplit->moveBefore(Exit); |
| 321 | } else { |
| 322 | // We can fold the conditional branch in the preheader, this makes things |
| 323 | // simpler. The first step is to remove the extra edge to the Exit block. |
| 324 | Exit->removePredecessor(OrigPreheader, true /*preserve LCSSA*/); |
| 325 | BranchInst::Create(NewHeader, PHBI); |
| 326 | PHBI->eraseFromParent(); |
| 327 | |
| 328 | // With our CFG finalized, update DomTree if it is available. |
| 329 | if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) { |
| 330 | // Update OrigHeader to be dominated by the new header block. |
| 331 | DT->changeImmediateDominator(NewHeader, OrigPreheader); |
| 332 | DT->changeImmediateDominator(OrigHeader, OrigLatch); |
| 333 | } |
Devang Patel | 990e866 | 2007-07-11 23:47:28 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | 0e4a154 | 2011-01-08 18:52:51 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 5d37370 | 2011-01-08 19:59:06 +0000 | [diff] [blame] | 336 | assert(L->getLoopPreheader() && "Invalid loop preheader after loop rotation"); |
Chris Lattner | 0e4a154 | 2011-01-08 18:52:51 +0000 | [diff] [blame] | 337 | assert(L->getLoopLatch() && "Invalid loop latch after loop rotation"); |
Chris Lattner | a1ae0c7 | 2011-01-08 18:55:50 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 93767fd | 2011-01-11 07:47:59 +0000 | [diff] [blame] | 339 | // Now that the CFG and DomTree are in a consistent state again, try to merge |
| 340 | // the OrigHeader block into OrigLatch. This will succeed if they are |
| 341 | // connected by an unconditional branch. This is just a cleanup so the |
| 342 | // emitted code isn't too gross in this common case. |
| 343 | MergeBlockIntoPredecessor(OrigHeader, this); |
Chris Lattner | a1ae0c7 | 2011-01-08 18:55:50 +0000 | [diff] [blame] | 344 | |
| 345 | ++NumRotated; |
| 346 | return true; |
Devang Patel | 5464b96 | 2007-04-09 20:19:46 +0000 | [diff] [blame] | 347 | } |
Chris Lattner | a1ae0c7 | 2011-01-08 18:55:50 +0000 | [diff] [blame] | 348 | |