blob: 6faf9bdd49a75211e2d620b12a112423af01ee06 [file] [log] [blame]
Devang Patelc4625da2007-04-07 01:25:15 +00001//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patelc4625da2007-04-07 01:25:15 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Loop Rotation Pass.
11//
12//===----------------------------------------------------------------------===//
13
Devang Patel32231332007-04-09 16:11:48 +000014#define DEBUG_TYPE "loop-rotate"
Devang Patelc4625da2007-04-07 01:25:15 +000015#include "llvm/Transforms/Scalar.h"
16#include "llvm/Function.h"
Devang Patel3f43a702009-03-06 03:51:30 +000017#include "llvm/IntrinsicInst.h"
Devang Patelc4625da2007-04-07 01:25:15 +000018#include "llvm/Analysis/LoopPass.h"
Devang Patel990e8662007-07-11 23:47:28 +000019#include "llvm/Analysis/Dominators.h"
20#include "llvm/Analysis/ScalarEvolution.h"
Devang Patelc4625da2007-04-07 01:25:15 +000021#include "llvm/Transforms/Utils/Local.h"
Devang Patel990e8662007-07-11 23:47:28 +000022#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Dan Gohmane6e37b92009-10-24 23:19:52 +000023#include "llvm/Transforms/Utils/SSAUpdater.h"
Devang Patelc4625da2007-04-07 01:25:15 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/SmallVector.h"
Devang Patelc4625da2007-04-07 01:25:15 +000028using namespace llvm;
29
30#define MAX_HEADER_SIZE 16
31
32STATISTIC(NumRotated, "Number of loops rotated");
33namespace {
34
Chris Lattner3e8b6632009-09-02 06:11:42 +000035 class LoopRotate : public LoopPass {
Devang Patelc4625da2007-04-07 01:25:15 +000036 public:
Devang Patel19974732007-05-03 01:11:54 +000037 static char ID; // Pass ID, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000038 LoopRotate() : LoopPass(ID) {
39 initializeLoopRotatePass(*PassRegistry::getPassRegistry());
40 }
Devang Patel794fd752007-05-01 21:15:47 +000041
Devang Patel32231332007-04-09 16:11:48 +000042 // Rotate Loop L as many times as possible. Return true if
43 // loop is rotated at least once.
Devang Patelc4625da2007-04-07 01:25:15 +000044 bool runOnLoop(Loop *L, LPPassManager &LPM);
Devang Patel32231332007-04-09 16:11:48 +000045
46 // LCSSA form makes instruction renaming easier.
Devang Patelc4625da2007-04-07 01:25:15 +000047 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman1e381fc2010-07-16 17:58:45 +000048 AU.addPreserved<DominatorTree>();
49 AU.addPreserved<DominanceFrontier>();
50 AU.addRequired<LoopInfo>();
51 AU.addPreserved<LoopInfo>();
Devang Patel9d9b2042008-02-15 01:24:49 +000052 AU.addRequiredID(LoopSimplifyID);
53 AU.addPreservedID(LoopSimplifyID);
Devang Patelc4625da2007-04-07 01:25:15 +000054 AU.addRequiredID(LCSSAID);
55 AU.addPreservedID(LCSSAID);
Devang Patel990e8662007-07-11 23:47:28 +000056 AU.addPreserved<ScalarEvolution>();
Devang Patelc4625da2007-04-07 01:25:15 +000057 }
58
59 // Helper functions
60
61 /// Do actual work
62 bool rotateLoop(Loop *L, LPPassManager &LPM);
63
64 /// Initialize local data
65 void initialize();
66
Devang Patel5464b962007-04-09 20:19:46 +000067 /// After loop rotation, loop pre-header has multiple sucessors.
68 /// Insert one forwarding basic block to ensure that loop pre-header
69 /// has only one successor.
70 void preserveCanonicalLoopForm(LPPassManager &LPM);
71
Devang Patelc4625da2007-04-07 01:25:15 +000072 private:
Devang Patelc4625da2007-04-07 01:25:15 +000073 Loop *L;
74 BasicBlock *OrigHeader;
75 BasicBlock *OrigPreHeader;
76 BasicBlock *OrigLatch;
77 BasicBlock *NewHeader;
Devang Patelc4625da2007-04-07 01:25:15 +000078 BasicBlock *Exit;
Devang Patel990e8662007-07-11 23:47:28 +000079 LPPassManager *LPM_Ptr;
Devang Patelc4625da2007-04-07 01:25:15 +000080 };
Devang Patelc4625da2007-04-07 01:25:15 +000081}
Dan Gohman844731a2008-05-13 00:00:25 +000082
83char LoopRotate::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000084INITIALIZE_PASS_BEGIN(LoopRotate, "loop-rotate", "Rotate Loops", false, false)
85INITIALIZE_PASS_DEPENDENCY(DominatorTree)
86INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
87INITIALIZE_PASS_DEPENDENCY(LoopInfo)
88INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
89INITIALIZE_PASS_DEPENDENCY(LCSSA)
90INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
91INITIALIZE_PASS_END(LoopRotate, "loop-rotate", "Rotate Loops", false, false)
Devang Patelc4625da2007-04-07 01:25:15 +000092
Daniel Dunbar394f0442008-10-22 23:32:42 +000093Pass *llvm::createLoopRotatePass() { return new LoopRotate(); }
Devang Patelc4625da2007-04-07 01:25:15 +000094
Devang Patel32231332007-04-09 16:11:48 +000095/// Rotate Loop L as many times as possible. Return true if
Dan Gohmancc4e6052009-06-25 00:22:44 +000096/// the loop is rotated at least once.
Devang Patelc4625da2007-04-07 01:25:15 +000097bool LoopRotate::runOnLoop(Loop *Lp, LPPassManager &LPM) {
Devang Patel990e8662007-07-11 23:47:28 +000098
Devang Patelc4625da2007-04-07 01:25:15 +000099 bool RotatedOneLoop = false;
100 initialize();
Devang Patel990e8662007-07-11 23:47:28 +0000101 LPM_Ptr = &LPM;
Devang Patelc4625da2007-04-07 01:25:15 +0000102
103 // One loop can be rotated multiple times.
104 while (rotateLoop(Lp,LPM)) {
105 RotatedOneLoop = true;
106 initialize();
107 }
108
109 return RotatedOneLoop;
110}
111
Dan Gohman23d9d272007-05-11 21:10:54 +0000112/// Rotate loop LP. Return true if the loop is rotated.
Devang Patelc4625da2007-04-07 01:25:15 +0000113bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
Devang Patelc4625da2007-04-07 01:25:15 +0000114 L = Lp;
Devang Patelc4625da2007-04-07 01:25:15 +0000115
Devang Patelc4625da2007-04-07 01:25:15 +0000116 OrigPreHeader = L->getLoopPreheader();
Dan Gohman03e896b2009-11-05 21:11:53 +0000117 if (!OrigPreHeader) return false;
118
Devang Patelc4625da2007-04-07 01:25:15 +0000119 OrigLatch = L->getLoopLatch();
Dan Gohman03e896b2009-11-05 21:11:53 +0000120 if (!OrigLatch) return false;
121
122 OrigHeader = L->getHeader();
Devang Patelc4625da2007-04-07 01:25:15 +0000123
Dan Gohmancc4e6052009-06-25 00:22:44 +0000124 // If the loop has only one block then there is not much to rotate.
Devang Patel32231332007-04-09 16:11:48 +0000125 if (L->getBlocks().size() == 1)
Devang Patelc4625da2007-04-07 01:25:15 +0000126 return false;
127
Dan Gohmancc4e6052009-06-25 00:22:44 +0000128 // If the loop header is not one of the loop exiting blocks then
129 // either this loop is already rotated or it is not
Devang Patelc4625da2007-04-07 01:25:15 +0000130 // suitable for loop rotation transformations.
Dan Gohman32663b72009-10-24 23:34:26 +0000131 if (!L->isLoopExiting(OrigHeader))
Devang Patelc4625da2007-04-07 01:25:15 +0000132 return false;
133
134 BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
135 if (!BI)
136 return false;
Chris Lattner2ba25432009-01-26 01:38:24 +0000137 assert(BI->isConditional() && "Branch Instruction is not conditional");
Devang Patelc4625da2007-04-07 01:25:15 +0000138
Devang Patel32231332007-04-09 16:11:48 +0000139 // Updating PHInodes in loops with multiple exits adds complexity.
140 // Keep it simple, and restrict loop rotation to loops with one exit only.
141 // In future, lift this restriction and support for multiple exits if
142 // required.
Devang Patelb7211a22007-08-21 00:31:24 +0000143 SmallVector<BasicBlock*, 8> ExitBlocks;
Devang Patelc4625da2007-04-07 01:25:15 +0000144 L->getExitBlocks(ExitBlocks);
145 if (ExitBlocks.size() > 1)
146 return false;
147
Devang Patel990e8662007-07-11 23:47:28 +0000148 // Check size of original header and reject
149 // loop if it is very big.
Devang Patel3f43a702009-03-06 03:51:30 +0000150 unsigned Size = 0;
151
152 // FIXME: Use common api to estimate size.
153 for (BasicBlock::const_iterator OI = OrigHeader->begin(),
154 OE = OrigHeader->end(); OI != OE; ++OI) {
Chris Lattner50fb4692010-09-06 01:10:22 +0000155 if (isa<PHINode>(OI))
156 continue; // PHI nodes don't count.
157 if (isa<DbgInfoIntrinsic>(OI))
158 continue; // Debug intrinsics don't count as size.
159 ++Size;
Devang Patel3f43a702009-03-06 03:51:30 +0000160 }
161
162 if (Size > MAX_HEADER_SIZE)
Devang Patel990e8662007-07-11 23:47:28 +0000163 return false;
164
165 // Now, this loop is suitable for rotation.
166
Dan Gohmane6fe67b2009-09-27 15:37:03 +0000167 // Anything ScalarEvolution may know about this loop or the PHI nodes
168 // in its header will soon be invalidated.
169 if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
Dan Gohman4c7279a2009-10-31 15:04:55 +0000170 SE->forgetLoop(L);
Dan Gohmane6fe67b2009-09-27 15:37:03 +0000171
Devang Patelc4625da2007-04-07 01:25:15 +0000172 // Find new Loop header. NewHeader is a Header's one and only successor
Chris Lattnerf6784a32009-01-26 01:57:01 +0000173 // that is inside loop. Header's other successor is outside the
174 // loop. Otherwise loop is not suitable for rotation.
Devang Patel32231332007-04-09 16:11:48 +0000175 Exit = BI->getSuccessor(0);
176 NewHeader = BI->getSuccessor(1);
177 if (L->contains(Exit))
178 std::swap(Exit, NewHeader);
Chris Lattner2ba25432009-01-26 01:38:24 +0000179 assert(NewHeader && "Unable to determine new loop header");
Devang Patel32231332007-04-09 16:11:48 +0000180 assert(L->contains(NewHeader) && !L->contains(Exit) &&
181 "Unable to determine loop header and exit blocks");
Chris Lattner3796a262009-01-26 02:11:30 +0000182
Dan Gohmancc4e6052009-06-25 00:22:44 +0000183 // This code assumes that the new header has exactly one predecessor.
184 // Remove any single-entry PHI nodes in it.
Chris Lattner3796a262009-01-26 02:11:30 +0000185 assert(NewHeader->getSinglePredecessor() &&
186 "New header doesn't have one pred!");
187 FoldSingleEntryPHINodes(NewHeader);
Devang Patelc4625da2007-04-07 01:25:15 +0000188
Dan Gohmane6e37b92009-10-24 23:19:52 +0000189 // Begin by walking OrigHeader and populating ValueMap with an entry for
190 // each Instruction.
Devang Patel32231332007-04-09 16:11:48 +0000191 BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
Dan Gohmane6e37b92009-10-24 23:19:52 +0000192 DenseMap<const Value *, Value *> ValueMap;
Devang Patele9881542007-04-09 19:04:21 +0000193
Dan Gohmane6e37b92009-10-24 23:19:52 +0000194 // For PHI nodes, the value available in OldPreHeader is just the
195 // incoming value from OldPreHeader.
196 for (; PHINode *PN = dyn_cast<PHINode>(I); ++I)
197 ValueMap[PN] = PN->getIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
Devang Patelc4625da2007-04-07 01:25:15 +0000198
Chris Lattner50fb4692010-09-06 01:10:22 +0000199 // For the rest of the instructions, either hoist to the OrigPreheader if
200 // possible or create a clone in the OldPreHeader if not.
Dan Gohmane6e37b92009-10-24 23:19:52 +0000201 TerminatorInst *LoopEntryBranch = OrigPreHeader->getTerminator();
Chris Lattner50fb4692010-09-06 01:10:22 +0000202 while (I != E) {
203 Instruction *Inst = I++;
204
205 // If the instruction's operands are invariant and it doesn't read or write
206 // memory, then it is safe to hoist. Doing this doesn't change the order of
207 // execution in the preheader, but does prevent the instruction from
208 // executing in each iteration of the loop. This means it is safe to hoist
209 // something that might trap, but isn't safe to hoist something that reads
210 // memory (without proving that the loop doesn't write).
211 if (L->hasLoopInvariantOperands(Inst) &&
212 !Inst->mayReadFromMemory() && !Inst->mayWriteToMemory() &&
213 !isa<TerminatorInst>(Inst)) {
214 Inst->moveBefore(LoopEntryBranch);
215 continue;
216 }
217
218 // Otherwise, create a duplicate of the instruction.
219 Instruction *C = Inst->clone();
220 C->setName(Inst->getName());
Dan Gohmane6e37b92009-10-24 23:19:52 +0000221 C->insertBefore(LoopEntryBranch);
Chris Lattner50fb4692010-09-06 01:10:22 +0000222 ValueMap[Inst] = C;
Devang Patelc4625da2007-04-07 01:25:15 +0000223 }
224
Dan Gohmane6e37b92009-10-24 23:19:52 +0000225 // Along with all the other instructions, we just cloned OrigHeader's
226 // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's
227 // successors by duplicating their incoming values for OrigHeader.
228 TerminatorInst *TI = OrigHeader->getTerminator();
229 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
230 for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin();
231 PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
232 PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreHeader);
Devang Patelc4625da2007-04-07 01:25:15 +0000233
Dan Gohmane6e37b92009-10-24 23:19:52 +0000234 // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove
235 // OrigPreHeader's old terminator (the original branch into the loop), and
236 // remove the corresponding incoming values from the PHI nodes in OrigHeader.
237 LoopEntryBranch->eraseFromParent();
238 for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I)
239 PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
Devang Patelc4625da2007-04-07 01:25:15 +0000240
Dan Gohman440e2512009-10-26 15:55:24 +0000241 // Now fix up users of the instructions in OrigHeader, inserting PHI nodes
Dan Gohmane6e37b92009-10-24 23:19:52 +0000242 // as necessary.
243 SSAUpdater SSA;
244 for (I = OrigHeader->begin(); I != E; ++I) {
245 Value *OrigHeaderVal = I;
246 Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal];
Devang Patelc4625da2007-04-07 01:25:15 +0000247
Dan Gohmane6e37b92009-10-24 23:19:52 +0000248 // The value now exits in two versions: the initial value in the preheader
249 // and the loop "next" value in the original header.
Duncan Sandsfc6e29d2010-09-02 08:14:03 +0000250 SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName());
Dan Gohmane6e37b92009-10-24 23:19:52 +0000251 SSA.AddAvailableValue(OrigHeader, OrigHeaderVal);
252 SSA.AddAvailableValue(OrigPreHeader, OrigPreHeaderVal);
Devang Patelc4625da2007-04-07 01:25:15 +0000253
Dan Gohmane6e37b92009-10-24 23:19:52 +0000254 // Visit each use of the OrigHeader instruction.
255 for (Value::use_iterator UI = OrigHeaderVal->use_begin(),
256 UE = OrigHeaderVal->use_end(); UI != UE; ) {
257 // Grab the use before incrementing the iterator.
258 Use &U = UI.getUse();
Devang Patelc4625da2007-04-07 01:25:15 +0000259
Dan Gohmane6e37b92009-10-24 23:19:52 +0000260 // Increment the iterator before removing the use from the list.
261 ++UI;
262
263 // SSAUpdater can't handle a non-PHI use in the same block as an
264 // earlier def. We can easily handle those cases manually.
265 Instruction *UserInst = cast<Instruction>(U.getUser());
266 if (!isa<PHINode>(UserInst)) {
267 BasicBlock *UserBB = UserInst->getParent();
268
269 // The original users in the OrigHeader are already using the
270 // original definitions.
271 if (UserBB == OrigHeader)
Devang Patel24a1c492007-04-09 16:21:29 +0000272 continue;
273
Dan Gohmane6e37b92009-10-24 23:19:52 +0000274 // Users in the OrigPreHeader need to use the value to which the
275 // original definitions are mapped.
276 if (UserBB == OrigPreHeader) {
277 U = OrigPreHeaderVal;
Devang Patelc4625da2007-04-07 01:25:15 +0000278 continue;
Dan Gohmane6e37b92009-10-24 23:19:52 +0000279 }
Devang Patelc4625da2007-04-07 01:25:15 +0000280 }
281
Dan Gohmane6e37b92009-10-24 23:19:52 +0000282 // Anything else can be handled by SSAUpdater.
283 SSA.RewriteUse(U);
Devang Patelc4625da2007-04-07 01:25:15 +0000284 }
285 }
Devang Patelc4625da2007-04-07 01:25:15 +0000286
Dan Gohmane6e37b92009-10-24 23:19:52 +0000287 // NewHeader is now the header of the loop.
Devang Patelc4625da2007-04-07 01:25:15 +0000288 L->moveToHeader(NewHeader);
289
Dan Gohmanfc8042a2010-08-17 17:39:21 +0000290 // Move the original header to the bottom of the loop, where it now more
291 // naturally belongs. This isn't necessary for correctness, and CodeGen can
292 // usually reorder blocks on its own to fix things like this up, but it's
293 // still nice to keep the IR readable.
294 //
295 // The original header should have only one predecessor at this point, since
296 // we checked that the loop had a proper preheader and unique backedge before
297 // we started.
298 assert(OrigHeader->getSinglePredecessor() &&
299 "Original loop header has too many predecessors after loop rotation!");
300 OrigHeader->moveAfter(OrigHeader->getSinglePredecessor());
301
302 // Also, since this original header only has one predecessor, zap its
303 // PHI nodes, which are now trivial.
304 FoldSingleEntryPHINodes(OrigHeader);
305
306 // TODO: We could just go ahead and merge OrigHeader into its predecessor
307 // at this point, if we don't mind updating dominator info.
308
309 // Establish a new preheader, update dominators, etc.
Devang Patel5464b962007-04-09 20:19:46 +0000310 preserveCanonicalLoopForm(LPM);
311
Dan Gohmanfe601042010-06-22 15:08:57 +0000312 ++NumRotated;
Devang Patelc4625da2007-04-07 01:25:15 +0000313 return true;
314}
315
Devang Patelc4625da2007-04-07 01:25:15 +0000316/// Initialize local data
317void LoopRotate::initialize() {
318 L = NULL;
319 OrigHeader = NULL;
320 OrigPreHeader = NULL;
321 NewHeader = NULL;
Devang Patelc4625da2007-04-07 01:25:15 +0000322 Exit = NULL;
Devang Patelc4625da2007-04-07 01:25:15 +0000323}
Devang Patel5464b962007-04-09 20:19:46 +0000324
325/// After loop rotation, loop pre-header has multiple sucessors.
326/// Insert one forwarding basic block to ensure that loop pre-header
327/// has only one successor.
328void LoopRotate::preserveCanonicalLoopForm(LPPassManager &LPM) {
329
330 // Right now original pre-header has two successors, new header and
331 // exit block. Insert new block between original pre-header and
332 // new header such that loop's new pre-header has only one successor.
Owen Anderson1d0be152009-08-13 21:58:54 +0000333 BasicBlock *NewPreHeader = BasicBlock::Create(OrigHeader->getContext(),
334 "bb.nph",
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000335 OrigHeader->getParent(),
Gabor Greif051a9502008-04-06 20:25:17 +0000336 NewHeader);
Dan Gohman6a02fc32009-11-05 19:43:25 +0000337 LoopInfo &LI = getAnalysis<LoopInfo>();
Devang Patel5464b962007-04-09 20:19:46 +0000338 if (Loop *PL = LI.getLoopFor(OrigPreHeader))
Owen Andersond735ee82007-11-27 03:43:35 +0000339 PL->addBasicBlockToLoop(NewPreHeader, LI.getBase());
Gabor Greif051a9502008-04-06 20:25:17 +0000340 BranchInst::Create(NewHeader, NewPreHeader);
Devang Patel5464b962007-04-09 20:19:46 +0000341
342 BranchInst *OrigPH_BI = cast<BranchInst>(OrigPreHeader->getTerminator());
343 if (OrigPH_BI->getSuccessor(0) == NewHeader)
344 OrigPH_BI->setSuccessor(0, NewPreHeader);
345 else {
Chris Lattner2ba25432009-01-26 01:38:24 +0000346 assert(OrigPH_BI->getSuccessor(1) == NewHeader &&
347 "Unexpected original pre-header terminator");
Devang Patel5464b962007-04-09 20:19:46 +0000348 OrigPH_BI->setSuccessor(1, NewPreHeader);
349 }
Devang Patel5464b962007-04-09 20:19:46 +0000350
Dan Gohmancfb32202009-06-27 21:23:40 +0000351 PHINode *PN;
352 for (BasicBlock::iterator I = NewHeader->begin();
353 (PN = dyn_cast<PHINode>(I)); ++I) {
Devang Patel5464b962007-04-09 20:19:46 +0000354 int index = PN->getBasicBlockIndex(OrigPreHeader);
Chris Lattner2ba25432009-01-26 01:38:24 +0000355 assert(index != -1 && "Expected incoming value from Original PreHeader");
Devang Patel5464b962007-04-09 20:19:46 +0000356 PN->setIncomingBlock(index, NewPreHeader);
Chris Lattner2ba25432009-01-26 01:38:24 +0000357 assert(PN->getBasicBlockIndex(OrigPreHeader) == -1 &&
358 "Expected only one incoming value from Original PreHeader");
Devang Patel5464b962007-04-09 20:19:46 +0000359 }
360
Duncan Sands1465d612009-01-28 13:14:17 +0000361 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
Devang Patel990e8662007-07-11 23:47:28 +0000362 DT->addNewBlock(NewPreHeader, OrigPreHeader);
363 DT->changeImmediateDominator(L->getHeader(), NewPreHeader);
364 DT->changeImmediateDominator(Exit, OrigPreHeader);
365 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
366 BI != BE; ++BI) {
367 BasicBlock *B = *BI;
368 if (L->getHeader() != B) {
369 DomTreeNode *Node = DT->getNode(B);
370 if (Node && Node->getBlock() == OrigHeader)
371 DT->changeImmediateDominator(*BI, L->getHeader());
372 }
373 }
374 DT->changeImmediateDominator(OrigHeader, OrigLatch);
375 }
376
Duncan Sands1465d612009-01-28 13:14:17 +0000377 if (DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>()) {
Devang Patel990e8662007-07-11 23:47:28 +0000378 // New Preheader's dominance frontier is Exit block.
379 DominanceFrontier::DomSetType NewPHSet;
380 NewPHSet.insert(Exit);
381 DF->addBasicBlock(NewPreHeader, NewPHSet);
382
383 // New Header's dominance frontier now includes itself and Exit block
384 DominanceFrontier::iterator HeadI = DF->find(L->getHeader());
385 if (HeadI != DF->end()) {
386 DominanceFrontier::DomSetType & HeaderSet = HeadI->second;
387 HeaderSet.clear();
388 HeaderSet.insert(L->getHeader());
389 HeaderSet.insert(Exit);
390 } else {
391 DominanceFrontier::DomSetType HeaderSet;
392 HeaderSet.insert(L->getHeader());
393 HeaderSet.insert(Exit);
394 DF->addBasicBlock(L->getHeader(), HeaderSet);
395 }
396
397 // Original header (new Loop Latch)'s dominance frontier is Exit.
398 DominanceFrontier::iterator LatchI = DF->find(L->getLoopLatch());
399 if (LatchI != DF->end()) {
400 DominanceFrontier::DomSetType &LatchSet = LatchI->second;
401 LatchSet = LatchI->second;
402 LatchSet.clear();
403 LatchSet.insert(Exit);
404 } else {
405 DominanceFrontier::DomSetType LatchSet;
406 LatchSet.insert(Exit);
407 DF->addBasicBlock(L->getHeader(), LatchSet);
408 }
409
Devang Patelb7f40c12009-08-07 17:16:44 +0000410 // If a loop block dominates new loop latch then add to its frontiers
411 // new header and Exit and remove new latch (which is equal to original
412 // header).
Devang Patel990e8662007-07-11 23:47:28 +0000413 BasicBlock *NewLatch = L->getLoopLatch();
Devang Patelb7f40c12009-08-07 17:16:44 +0000414
415 assert(NewLatch == OrigHeader && "NewLatch is inequal to OrigHeader");
416
417 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
418 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
419 BI != BE; ++BI) {
420 BasicBlock *B = *BI;
421 if (DT->dominates(B, NewLatch)) {
422 DominanceFrontier::iterator BDFI = DF->find(B);
423 if (BDFI != DF->end()) {
424 DominanceFrontier::DomSetType &BSet = BDFI->second;
425 BSet.erase(NewLatch);
426 BSet.insert(L->getHeader());
427 BSet.insert(Exit);
428 } else {
429 DominanceFrontier::DomSetType BSet;
430 BSet.insert(L->getHeader());
431 BSet.insert(Exit);
432 DF->addBasicBlock(B, BSet);
433 }
Devang Patel990e8662007-07-11 23:47:28 +0000434 }
435 }
436 }
437 }
438
439 // Preserve canonical loop form, which means Exit block should
440 // have only one predecessor.
Dan Gohmanc292caf2009-09-09 18:18:18 +0000441 SplitEdge(L->getLoopLatch(), Exit, this);
Devang Patel990e8662007-07-11 23:47:28 +0000442
Chris Lattner2ba25432009-01-26 01:38:24 +0000443 assert(NewHeader && L->getHeader() == NewHeader &&
444 "Invalid loop header after loop rotation");
445 assert(NewPreHeader && L->getLoopPreheader() == NewPreHeader &&
446 "Invalid loop preheader after loop rotation");
447 assert(L->getLoopLatch() &&
448 "Invalid loop latch after loop rotation");
Devang Patel5464b962007-04-09 20:19:46 +0000449}