blob: bdc8f4ab7b4f2d186863c3f7adf82c9299ccbf24 [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 Anderson90c579d2010-08-06 18:33:48 +000038 LoopRotate() : LoopPass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000039
Devang Patel32231332007-04-09 16:11:48 +000040 // Rotate Loop L as many times as possible. Return true if
41 // loop is rotated at least once.
Devang Patelc4625da2007-04-07 01:25:15 +000042 bool runOnLoop(Loop *L, LPPassManager &LPM);
Devang Patel32231332007-04-09 16:11:48 +000043
44 // LCSSA form makes instruction renaming easier.
Devang Patelc4625da2007-04-07 01:25:15 +000045 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman1e381fc2010-07-16 17:58:45 +000046 AU.addPreserved<DominatorTree>();
47 AU.addPreserved<DominanceFrontier>();
48 AU.addRequired<LoopInfo>();
49 AU.addPreserved<LoopInfo>();
Devang Patel9d9b2042008-02-15 01:24:49 +000050 AU.addRequiredID(LoopSimplifyID);
51 AU.addPreservedID(LoopSimplifyID);
Devang Patelc4625da2007-04-07 01:25:15 +000052 AU.addRequiredID(LCSSAID);
53 AU.addPreservedID(LCSSAID);
Devang Patel990e8662007-07-11 23:47:28 +000054 AU.addPreserved<ScalarEvolution>();
Devang Patelc4625da2007-04-07 01:25:15 +000055 }
56
57 // Helper functions
58
59 /// Do actual work
60 bool rotateLoop(Loop *L, LPPassManager &LPM);
61
62 /// Initialize local data
63 void initialize();
64
Devang Patel5464b962007-04-09 20:19:46 +000065 /// After loop rotation, loop pre-header has multiple sucessors.
66 /// Insert one forwarding basic block to ensure that loop pre-header
67 /// has only one successor.
68 void preserveCanonicalLoopForm(LPPassManager &LPM);
69
Devang Patelc4625da2007-04-07 01:25:15 +000070 private:
Devang Patelc4625da2007-04-07 01:25:15 +000071 Loop *L;
72 BasicBlock *OrigHeader;
73 BasicBlock *OrigPreHeader;
74 BasicBlock *OrigLatch;
75 BasicBlock *NewHeader;
Devang Patelc4625da2007-04-07 01:25:15 +000076 BasicBlock *Exit;
Devang Patel990e8662007-07-11 23:47:28 +000077 LPPassManager *LPM_Ptr;
Devang Patelc4625da2007-04-07 01:25:15 +000078 };
Devang Patelc4625da2007-04-07 01:25:15 +000079}
Dan Gohman844731a2008-05-13 00:00:25 +000080
81char LoopRotate::ID = 0;
Owen Andersonce665bd2010-10-07 22:25:06 +000082INITIALIZE_PASS(LoopRotate, "loop-rotate", "Rotate Loops", false, false)
Devang Patelc4625da2007-04-07 01:25:15 +000083
Daniel Dunbar394f0442008-10-22 23:32:42 +000084Pass *llvm::createLoopRotatePass() { return new LoopRotate(); }
Devang Patelc4625da2007-04-07 01:25:15 +000085
Devang Patel32231332007-04-09 16:11:48 +000086/// Rotate Loop L as many times as possible. Return true if
Dan Gohmancc4e6052009-06-25 00:22:44 +000087/// the loop is rotated at least once.
Devang Patelc4625da2007-04-07 01:25:15 +000088bool LoopRotate::runOnLoop(Loop *Lp, LPPassManager &LPM) {
Devang Patel990e8662007-07-11 23:47:28 +000089
Devang Patelc4625da2007-04-07 01:25:15 +000090 bool RotatedOneLoop = false;
91 initialize();
Devang Patel990e8662007-07-11 23:47:28 +000092 LPM_Ptr = &LPM;
Devang Patelc4625da2007-04-07 01:25:15 +000093
94 // One loop can be rotated multiple times.
95 while (rotateLoop(Lp,LPM)) {
96 RotatedOneLoop = true;
97 initialize();
98 }
99
100 return RotatedOneLoop;
101}
102
Dan Gohman23d9d272007-05-11 21:10:54 +0000103/// Rotate loop LP. Return true if the loop is rotated.
Devang Patelc4625da2007-04-07 01:25:15 +0000104bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
Devang Patelc4625da2007-04-07 01:25:15 +0000105 L = Lp;
Devang Patelc4625da2007-04-07 01:25:15 +0000106
Devang Patelc4625da2007-04-07 01:25:15 +0000107 OrigPreHeader = L->getLoopPreheader();
Dan Gohman03e896b2009-11-05 21:11:53 +0000108 if (!OrigPreHeader) return false;
109
Devang Patelc4625da2007-04-07 01:25:15 +0000110 OrigLatch = L->getLoopLatch();
Dan Gohman03e896b2009-11-05 21:11:53 +0000111 if (!OrigLatch) return false;
112
113 OrigHeader = L->getHeader();
Devang Patelc4625da2007-04-07 01:25:15 +0000114
Dan Gohmancc4e6052009-06-25 00:22:44 +0000115 // If the loop has only one block then there is not much to rotate.
Devang Patel32231332007-04-09 16:11:48 +0000116 if (L->getBlocks().size() == 1)
Devang Patelc4625da2007-04-07 01:25:15 +0000117 return false;
118
Dan Gohmancc4e6052009-06-25 00:22:44 +0000119 // If the loop header is not one of the loop exiting blocks then
120 // either this loop is already rotated or it is not
Devang Patelc4625da2007-04-07 01:25:15 +0000121 // suitable for loop rotation transformations.
Dan Gohman32663b72009-10-24 23:34:26 +0000122 if (!L->isLoopExiting(OrigHeader))
Devang Patelc4625da2007-04-07 01:25:15 +0000123 return false;
124
125 BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
126 if (!BI)
127 return false;
Chris Lattner2ba25432009-01-26 01:38:24 +0000128 assert(BI->isConditional() && "Branch Instruction is not conditional");
Devang Patelc4625da2007-04-07 01:25:15 +0000129
Devang Patel32231332007-04-09 16:11:48 +0000130 // Updating PHInodes in loops with multiple exits adds complexity.
131 // Keep it simple, and restrict loop rotation to loops with one exit only.
132 // In future, lift this restriction and support for multiple exits if
133 // required.
Devang Patelb7211a22007-08-21 00:31:24 +0000134 SmallVector<BasicBlock*, 8> ExitBlocks;
Devang Patelc4625da2007-04-07 01:25:15 +0000135 L->getExitBlocks(ExitBlocks);
136 if (ExitBlocks.size() > 1)
137 return false;
138
Devang Patel990e8662007-07-11 23:47:28 +0000139 // Check size of original header and reject
140 // loop if it is very big.
Devang Patel3f43a702009-03-06 03:51:30 +0000141 unsigned Size = 0;
142
143 // FIXME: Use common api to estimate size.
144 for (BasicBlock::const_iterator OI = OrigHeader->begin(),
145 OE = OrigHeader->end(); OI != OE; ++OI) {
Chris Lattner50fb4692010-09-06 01:10:22 +0000146 if (isa<PHINode>(OI))
147 continue; // PHI nodes don't count.
148 if (isa<DbgInfoIntrinsic>(OI))
149 continue; // Debug intrinsics don't count as size.
150 ++Size;
Devang Patel3f43a702009-03-06 03:51:30 +0000151 }
152
153 if (Size > MAX_HEADER_SIZE)
Devang Patel990e8662007-07-11 23:47:28 +0000154 return false;
155
156 // Now, this loop is suitable for rotation.
157
Dan Gohmane6fe67b2009-09-27 15:37:03 +0000158 // Anything ScalarEvolution may know about this loop or the PHI nodes
159 // in its header will soon be invalidated.
160 if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
Dan Gohman4c7279a2009-10-31 15:04:55 +0000161 SE->forgetLoop(L);
Dan Gohmane6fe67b2009-09-27 15:37:03 +0000162
Devang Patelc4625da2007-04-07 01:25:15 +0000163 // Find new Loop header. NewHeader is a Header's one and only successor
Chris Lattnerf6784a32009-01-26 01:57:01 +0000164 // that is inside loop. Header's other successor is outside the
165 // loop. Otherwise loop is not suitable for rotation.
Devang Patel32231332007-04-09 16:11:48 +0000166 Exit = BI->getSuccessor(0);
167 NewHeader = BI->getSuccessor(1);
168 if (L->contains(Exit))
169 std::swap(Exit, NewHeader);
Chris Lattner2ba25432009-01-26 01:38:24 +0000170 assert(NewHeader && "Unable to determine new loop header");
Devang Patel32231332007-04-09 16:11:48 +0000171 assert(L->contains(NewHeader) && !L->contains(Exit) &&
172 "Unable to determine loop header and exit blocks");
Chris Lattner3796a262009-01-26 02:11:30 +0000173
Dan Gohmancc4e6052009-06-25 00:22:44 +0000174 // This code assumes that the new header has exactly one predecessor.
175 // Remove any single-entry PHI nodes in it.
Chris Lattner3796a262009-01-26 02:11:30 +0000176 assert(NewHeader->getSinglePredecessor() &&
177 "New header doesn't have one pred!");
178 FoldSingleEntryPHINodes(NewHeader);
Devang Patelc4625da2007-04-07 01:25:15 +0000179
Dan Gohmane6e37b92009-10-24 23:19:52 +0000180 // Begin by walking OrigHeader and populating ValueMap with an entry for
181 // each Instruction.
Devang Patel32231332007-04-09 16:11:48 +0000182 BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
Dan Gohmane6e37b92009-10-24 23:19:52 +0000183 DenseMap<const Value *, Value *> ValueMap;
Devang Patele9881542007-04-09 19:04:21 +0000184
Dan Gohmane6e37b92009-10-24 23:19:52 +0000185 // For PHI nodes, the value available in OldPreHeader is just the
186 // incoming value from OldPreHeader.
187 for (; PHINode *PN = dyn_cast<PHINode>(I); ++I)
188 ValueMap[PN] = PN->getIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
Devang Patelc4625da2007-04-07 01:25:15 +0000189
Chris Lattner50fb4692010-09-06 01:10:22 +0000190 // For the rest of the instructions, either hoist to the OrigPreheader if
191 // possible or create a clone in the OldPreHeader if not.
Dan Gohmane6e37b92009-10-24 23:19:52 +0000192 TerminatorInst *LoopEntryBranch = OrigPreHeader->getTerminator();
Chris Lattner50fb4692010-09-06 01:10:22 +0000193 while (I != E) {
194 Instruction *Inst = I++;
195
196 // If the instruction's operands are invariant and it doesn't read or write
197 // memory, then it is safe to hoist. Doing this doesn't change the order of
198 // execution in the preheader, but does prevent the instruction from
199 // executing in each iteration of the loop. This means it is safe to hoist
200 // something that might trap, but isn't safe to hoist something that reads
201 // memory (without proving that the loop doesn't write).
202 if (L->hasLoopInvariantOperands(Inst) &&
203 !Inst->mayReadFromMemory() && !Inst->mayWriteToMemory() &&
204 !isa<TerminatorInst>(Inst)) {
205 Inst->moveBefore(LoopEntryBranch);
206 continue;
207 }
208
209 // Otherwise, create a duplicate of the instruction.
210 Instruction *C = Inst->clone();
211 C->setName(Inst->getName());
Dan Gohmane6e37b92009-10-24 23:19:52 +0000212 C->insertBefore(LoopEntryBranch);
Chris Lattner50fb4692010-09-06 01:10:22 +0000213 ValueMap[Inst] = C;
Devang Patelc4625da2007-04-07 01:25:15 +0000214 }
215
Dan Gohmane6e37b92009-10-24 23:19:52 +0000216 // Along with all the other instructions, we just cloned OrigHeader's
217 // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's
218 // successors by duplicating their incoming values for OrigHeader.
219 TerminatorInst *TI = OrigHeader->getTerminator();
220 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
221 for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin();
222 PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
223 PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreHeader);
Devang Patelc4625da2007-04-07 01:25:15 +0000224
Dan Gohmane6e37b92009-10-24 23:19:52 +0000225 // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove
226 // OrigPreHeader's old terminator (the original branch into the loop), and
227 // remove the corresponding incoming values from the PHI nodes in OrigHeader.
228 LoopEntryBranch->eraseFromParent();
229 for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I)
230 PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
Devang Patelc4625da2007-04-07 01:25:15 +0000231
Dan Gohman440e2512009-10-26 15:55:24 +0000232 // Now fix up users of the instructions in OrigHeader, inserting PHI nodes
Dan Gohmane6e37b92009-10-24 23:19:52 +0000233 // as necessary.
234 SSAUpdater SSA;
235 for (I = OrigHeader->begin(); I != E; ++I) {
236 Value *OrigHeaderVal = I;
237 Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal];
Devang Patelc4625da2007-04-07 01:25:15 +0000238
Dan Gohmane6e37b92009-10-24 23:19:52 +0000239 // The value now exits in two versions: the initial value in the preheader
240 // and the loop "next" value in the original header.
Duncan Sandsfc6e29d2010-09-02 08:14:03 +0000241 SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName());
Dan Gohmane6e37b92009-10-24 23:19:52 +0000242 SSA.AddAvailableValue(OrigHeader, OrigHeaderVal);
243 SSA.AddAvailableValue(OrigPreHeader, OrigPreHeaderVal);
Devang Patelc4625da2007-04-07 01:25:15 +0000244
Dan Gohmane6e37b92009-10-24 23:19:52 +0000245 // Visit each use of the OrigHeader instruction.
246 for (Value::use_iterator UI = OrigHeaderVal->use_begin(),
247 UE = OrigHeaderVal->use_end(); UI != UE; ) {
248 // Grab the use before incrementing the iterator.
249 Use &U = UI.getUse();
Devang Patelc4625da2007-04-07 01:25:15 +0000250
Dan Gohmane6e37b92009-10-24 23:19:52 +0000251 // Increment the iterator before removing the use from the list.
252 ++UI;
253
254 // SSAUpdater can't handle a non-PHI use in the same block as an
255 // earlier def. We can easily handle those cases manually.
256 Instruction *UserInst = cast<Instruction>(U.getUser());
257 if (!isa<PHINode>(UserInst)) {
258 BasicBlock *UserBB = UserInst->getParent();
259
260 // The original users in the OrigHeader are already using the
261 // original definitions.
262 if (UserBB == OrigHeader)
Devang Patel24a1c492007-04-09 16:21:29 +0000263 continue;
264
Dan Gohmane6e37b92009-10-24 23:19:52 +0000265 // Users in the OrigPreHeader need to use the value to which the
266 // original definitions are mapped.
267 if (UserBB == OrigPreHeader) {
268 U = OrigPreHeaderVal;
Devang Patelc4625da2007-04-07 01:25:15 +0000269 continue;
Dan Gohmane6e37b92009-10-24 23:19:52 +0000270 }
Devang Patelc4625da2007-04-07 01:25:15 +0000271 }
272
Dan Gohmane6e37b92009-10-24 23:19:52 +0000273 // Anything else can be handled by SSAUpdater.
274 SSA.RewriteUse(U);
Devang Patelc4625da2007-04-07 01:25:15 +0000275 }
276 }
Devang Patelc4625da2007-04-07 01:25:15 +0000277
Dan Gohmane6e37b92009-10-24 23:19:52 +0000278 // NewHeader is now the header of the loop.
Devang Patelc4625da2007-04-07 01:25:15 +0000279 L->moveToHeader(NewHeader);
280
Dan Gohmanfc8042a2010-08-17 17:39:21 +0000281 // Move the original header to the bottom of the loop, where it now more
282 // naturally belongs. This isn't necessary for correctness, and CodeGen can
283 // usually reorder blocks on its own to fix things like this up, but it's
284 // still nice to keep the IR readable.
285 //
286 // The original header should have only one predecessor at this point, since
287 // we checked that the loop had a proper preheader and unique backedge before
288 // we started.
289 assert(OrigHeader->getSinglePredecessor() &&
290 "Original loop header has too many predecessors after loop rotation!");
291 OrigHeader->moveAfter(OrigHeader->getSinglePredecessor());
292
293 // Also, since this original header only has one predecessor, zap its
294 // PHI nodes, which are now trivial.
295 FoldSingleEntryPHINodes(OrigHeader);
296
297 // TODO: We could just go ahead and merge OrigHeader into its predecessor
298 // at this point, if we don't mind updating dominator info.
299
300 // Establish a new preheader, update dominators, etc.
Devang Patel5464b962007-04-09 20:19:46 +0000301 preserveCanonicalLoopForm(LPM);
302
Dan Gohmanfe601042010-06-22 15:08:57 +0000303 ++NumRotated;
Devang Patelc4625da2007-04-07 01:25:15 +0000304 return true;
305}
306
Devang Patelc4625da2007-04-07 01:25:15 +0000307/// Initialize local data
308void LoopRotate::initialize() {
309 L = NULL;
310 OrigHeader = NULL;
311 OrigPreHeader = NULL;
312 NewHeader = NULL;
Devang Patelc4625da2007-04-07 01:25:15 +0000313 Exit = NULL;
Devang Patelc4625da2007-04-07 01:25:15 +0000314}
Devang Patel5464b962007-04-09 20:19:46 +0000315
316/// After loop rotation, loop pre-header has multiple sucessors.
317/// Insert one forwarding basic block to ensure that loop pre-header
318/// has only one successor.
319void LoopRotate::preserveCanonicalLoopForm(LPPassManager &LPM) {
320
321 // Right now original pre-header has two successors, new header and
322 // exit block. Insert new block between original pre-header and
323 // new header such that loop's new pre-header has only one successor.
Owen Anderson1d0be152009-08-13 21:58:54 +0000324 BasicBlock *NewPreHeader = BasicBlock::Create(OrigHeader->getContext(),
325 "bb.nph",
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000326 OrigHeader->getParent(),
Gabor Greif051a9502008-04-06 20:25:17 +0000327 NewHeader);
Dan Gohman6a02fc32009-11-05 19:43:25 +0000328 LoopInfo &LI = getAnalysis<LoopInfo>();
Devang Patel5464b962007-04-09 20:19:46 +0000329 if (Loop *PL = LI.getLoopFor(OrigPreHeader))
Owen Andersond735ee82007-11-27 03:43:35 +0000330 PL->addBasicBlockToLoop(NewPreHeader, LI.getBase());
Gabor Greif051a9502008-04-06 20:25:17 +0000331 BranchInst::Create(NewHeader, NewPreHeader);
Devang Patel5464b962007-04-09 20:19:46 +0000332
333 BranchInst *OrigPH_BI = cast<BranchInst>(OrigPreHeader->getTerminator());
334 if (OrigPH_BI->getSuccessor(0) == NewHeader)
335 OrigPH_BI->setSuccessor(0, NewPreHeader);
336 else {
Chris Lattner2ba25432009-01-26 01:38:24 +0000337 assert(OrigPH_BI->getSuccessor(1) == NewHeader &&
338 "Unexpected original pre-header terminator");
Devang Patel5464b962007-04-09 20:19:46 +0000339 OrigPH_BI->setSuccessor(1, NewPreHeader);
340 }
Devang Patel5464b962007-04-09 20:19:46 +0000341
Dan Gohmancfb32202009-06-27 21:23:40 +0000342 PHINode *PN;
343 for (BasicBlock::iterator I = NewHeader->begin();
344 (PN = dyn_cast<PHINode>(I)); ++I) {
Devang Patel5464b962007-04-09 20:19:46 +0000345 int index = PN->getBasicBlockIndex(OrigPreHeader);
Chris Lattner2ba25432009-01-26 01:38:24 +0000346 assert(index != -1 && "Expected incoming value from Original PreHeader");
Devang Patel5464b962007-04-09 20:19:46 +0000347 PN->setIncomingBlock(index, NewPreHeader);
Chris Lattner2ba25432009-01-26 01:38:24 +0000348 assert(PN->getBasicBlockIndex(OrigPreHeader) == -1 &&
349 "Expected only one incoming value from Original PreHeader");
Devang Patel5464b962007-04-09 20:19:46 +0000350 }
351
Duncan Sands1465d612009-01-28 13:14:17 +0000352 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
Devang Patel990e8662007-07-11 23:47:28 +0000353 DT->addNewBlock(NewPreHeader, OrigPreHeader);
354 DT->changeImmediateDominator(L->getHeader(), NewPreHeader);
355 DT->changeImmediateDominator(Exit, OrigPreHeader);
356 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
357 BI != BE; ++BI) {
358 BasicBlock *B = *BI;
359 if (L->getHeader() != B) {
360 DomTreeNode *Node = DT->getNode(B);
361 if (Node && Node->getBlock() == OrigHeader)
362 DT->changeImmediateDominator(*BI, L->getHeader());
363 }
364 }
365 DT->changeImmediateDominator(OrigHeader, OrigLatch);
366 }
367
Duncan Sands1465d612009-01-28 13:14:17 +0000368 if (DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>()) {
Devang Patel990e8662007-07-11 23:47:28 +0000369 // New Preheader's dominance frontier is Exit block.
370 DominanceFrontier::DomSetType NewPHSet;
371 NewPHSet.insert(Exit);
372 DF->addBasicBlock(NewPreHeader, NewPHSet);
373
374 // New Header's dominance frontier now includes itself and Exit block
375 DominanceFrontier::iterator HeadI = DF->find(L->getHeader());
376 if (HeadI != DF->end()) {
377 DominanceFrontier::DomSetType & HeaderSet = HeadI->second;
378 HeaderSet.clear();
379 HeaderSet.insert(L->getHeader());
380 HeaderSet.insert(Exit);
381 } else {
382 DominanceFrontier::DomSetType HeaderSet;
383 HeaderSet.insert(L->getHeader());
384 HeaderSet.insert(Exit);
385 DF->addBasicBlock(L->getHeader(), HeaderSet);
386 }
387
388 // Original header (new Loop Latch)'s dominance frontier is Exit.
389 DominanceFrontier::iterator LatchI = DF->find(L->getLoopLatch());
390 if (LatchI != DF->end()) {
391 DominanceFrontier::DomSetType &LatchSet = LatchI->second;
392 LatchSet = LatchI->second;
393 LatchSet.clear();
394 LatchSet.insert(Exit);
395 } else {
396 DominanceFrontier::DomSetType LatchSet;
397 LatchSet.insert(Exit);
398 DF->addBasicBlock(L->getHeader(), LatchSet);
399 }
400
Devang Patelb7f40c12009-08-07 17:16:44 +0000401 // If a loop block dominates new loop latch then add to its frontiers
402 // new header and Exit and remove new latch (which is equal to original
403 // header).
Devang Patel990e8662007-07-11 23:47:28 +0000404 BasicBlock *NewLatch = L->getLoopLatch();
Devang Patelb7f40c12009-08-07 17:16:44 +0000405
406 assert(NewLatch == OrigHeader && "NewLatch is inequal to OrigHeader");
407
408 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
409 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
410 BI != BE; ++BI) {
411 BasicBlock *B = *BI;
412 if (DT->dominates(B, NewLatch)) {
413 DominanceFrontier::iterator BDFI = DF->find(B);
414 if (BDFI != DF->end()) {
415 DominanceFrontier::DomSetType &BSet = BDFI->second;
416 BSet.erase(NewLatch);
417 BSet.insert(L->getHeader());
418 BSet.insert(Exit);
419 } else {
420 DominanceFrontier::DomSetType BSet;
421 BSet.insert(L->getHeader());
422 BSet.insert(Exit);
423 DF->addBasicBlock(B, BSet);
424 }
Devang Patel990e8662007-07-11 23:47:28 +0000425 }
426 }
427 }
428 }
429
430 // Preserve canonical loop form, which means Exit block should
431 // have only one predecessor.
Dan Gohmanc292caf2009-09-09 18:18:18 +0000432 SplitEdge(L->getLoopLatch(), Exit, this);
Devang Patel990e8662007-07-11 23:47:28 +0000433
Chris Lattner2ba25432009-01-26 01:38:24 +0000434 assert(NewHeader && L->getHeader() == NewHeader &&
435 "Invalid loop header after loop rotation");
436 assert(NewPreHeader && L->getLoopPreheader() == NewPreHeader &&
437 "Invalid loop preheader after loop rotation");
438 assert(L->getLoopLatch() &&
439 "Invalid loop latch after loop rotation");
Devang Patel5464b962007-04-09 20:19:46 +0000440}