blob: 4ca422e62097d21a6cc85f3663d6ec3734bb8458 [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 Patelc4625da2007-04-07 01:25:15 +000017#include "llvm/Analysis/LoopPass.h"
Chris Lattner9fc5cdf2011-01-02 22:09:33 +000018#include "llvm/Analysis/DominanceFrontier.h"
Chris Lattnerd9e07972011-01-02 07:35:53 +000019#include "llvm/Analysis/CodeMetrics.h"
Devang Patel990e8662007-07-11 23:47:28 +000020#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"
Chris Lattner6ccb3652011-01-08 07:21:31 +000024#include "llvm/Transforms/Utils/ValueMapper.h"
Devang Patelc4625da2007-04-07 01:25:15 +000025#include "llvm/Support/Debug.h"
26#include "llvm/ADT/Statistic.h"
Devang Patelc4625da2007-04-07 01:25:15 +000027using namespace llvm;
28
29#define MAX_HEADER_SIZE 16
30
31STATISTIC(NumRotated, "Number of loops rotated");
32namespace {
33
Chris Lattner3e8b6632009-09-02 06:11:42 +000034 class LoopRotate : public LoopPass {
Devang Patelc4625da2007-04-07 01:25:15 +000035 public:
Devang Patel19974732007-05-03 01:11:54 +000036 static char ID; // Pass ID, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000037 LoopRotate() : LoopPass(ID) {
38 initializeLoopRotatePass(*PassRegistry::getPassRegistry());
39 }
Devang Patel794fd752007-05-01 21:15:47 +000040
Devang Patel32231332007-04-09 16:11:48 +000041 // Rotate Loop L as many times as possible. Return true if
42 // loop is rotated at least once.
Devang Patelc4625da2007-04-07 01:25:15 +000043 bool runOnLoop(Loop *L, LPPassManager &LPM);
Devang Patel32231332007-04-09 16:11:48 +000044
45 // LCSSA form makes instruction renaming easier.
Devang Patelc4625da2007-04-07 01:25:15 +000046 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman1e381fc2010-07-16 17:58:45 +000047 AU.addPreserved<DominatorTree>();
48 AU.addPreserved<DominanceFrontier>();
49 AU.addRequired<LoopInfo>();
50 AU.addPreserved<LoopInfo>();
Devang Patel9d9b2042008-02-15 01:24:49 +000051 AU.addRequiredID(LoopSimplifyID);
52 AU.addPreservedID(LoopSimplifyID);
Devang Patelc4625da2007-04-07 01:25:15 +000053 AU.addRequiredID(LCSSAID);
54 AU.addPreservedID(LCSSAID);
Devang Patel990e8662007-07-11 23:47:28 +000055 AU.addPreserved<ScalarEvolution>();
Devang Patelc4625da2007-04-07 01:25:15 +000056 }
57
58 // Helper functions
59
60 /// Do actual work
61 bool rotateLoop(Loop *L, LPPassManager &LPM);
62
63 /// Initialize local data
64 void initialize();
65
Devang Patel5464b962007-04-09 20:19:46 +000066 /// After loop rotation, loop pre-header has multiple sucessors.
67 /// Insert one forwarding basic block to ensure that loop pre-header
68 /// has only one successor.
69 void preserveCanonicalLoopForm(LPPassManager &LPM);
70
Devang Patelc4625da2007-04-07 01:25:15 +000071 private:
Devang Patelc4625da2007-04-07 01:25:15 +000072 Loop *L;
73 BasicBlock *OrigHeader;
74 BasicBlock *OrigPreHeader;
75 BasicBlock *OrigLatch;
76 BasicBlock *NewHeader;
Devang Patelc4625da2007-04-07 01:25:15 +000077 BasicBlock *Exit;
Devang Patel990e8662007-07-11 23:47:28 +000078 LPPassManager *LPM_Ptr;
Devang Patelc4625da2007-04-07 01:25:15 +000079 };
Devang Patelc4625da2007-04-07 01:25:15 +000080}
Dan Gohman844731a2008-05-13 00:00:25 +000081
82char LoopRotate::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000083INITIALIZE_PASS_BEGIN(LoopRotate, "loop-rotate", "Rotate Loops", false, false)
Owen Anderson2ab36d32010-10-12 19:48:12 +000084INITIALIZE_PASS_DEPENDENCY(LoopInfo)
85INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
86INITIALIZE_PASS_DEPENDENCY(LCSSA)
Owen Anderson2ab36d32010-10-12 19:48:12 +000087INITIALIZE_PASS_END(LoopRotate, "loop-rotate", "Rotate Loops", false, false)
Devang Patelc4625da2007-04-07 01:25:15 +000088
Daniel Dunbar394f0442008-10-22 23:32:42 +000089Pass *llvm::createLoopRotatePass() { return new LoopRotate(); }
Devang Patelc4625da2007-04-07 01:25:15 +000090
Devang Patel32231332007-04-09 16:11:48 +000091/// Rotate Loop L as many times as possible. Return true if
Dan Gohmancc4e6052009-06-25 00:22:44 +000092/// the loop is rotated at least once.
Devang Patelc4625da2007-04-07 01:25:15 +000093bool LoopRotate::runOnLoop(Loop *Lp, LPPassManager &LPM) {
Devang Patel990e8662007-07-11 23:47:28 +000094
Devang Patelc4625da2007-04-07 01:25:15 +000095 bool RotatedOneLoop = false;
96 initialize();
Devang Patel990e8662007-07-11 23:47:28 +000097 LPM_Ptr = &LPM;
Devang Patelc4625da2007-04-07 01:25:15 +000098
99 // One loop can be rotated multiple times.
100 while (rotateLoop(Lp,LPM)) {
101 RotatedOneLoop = true;
102 initialize();
103 }
104
105 return RotatedOneLoop;
106}
107
Dan Gohman23d9d272007-05-11 21:10:54 +0000108/// Rotate loop LP. Return true if the loop is rotated.
Devang Patelc4625da2007-04-07 01:25:15 +0000109bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
Devang Patelc4625da2007-04-07 01:25:15 +0000110 L = Lp;
Devang Patelc4625da2007-04-07 01:25:15 +0000111
Devang Patelc4625da2007-04-07 01:25:15 +0000112 OrigPreHeader = L->getLoopPreheader();
Dan Gohman03e896b2009-11-05 21:11:53 +0000113 if (!OrigPreHeader) return false;
114
Devang Patelc4625da2007-04-07 01:25:15 +0000115 OrigLatch = L->getLoopLatch();
Dan Gohman03e896b2009-11-05 21:11:53 +0000116 if (!OrigLatch) return false;
117
118 OrigHeader = L->getHeader();
Devang Patelc4625da2007-04-07 01:25:15 +0000119
Dan Gohmancc4e6052009-06-25 00:22:44 +0000120 // If the loop has only one block then there is not much to rotate.
Devang Patel32231332007-04-09 16:11:48 +0000121 if (L->getBlocks().size() == 1)
Devang Patelc4625da2007-04-07 01:25:15 +0000122 return false;
123
Dan Gohmancc4e6052009-06-25 00:22:44 +0000124 // If the loop header is not one of the loop exiting blocks then
125 // either this loop is already rotated or it is not
Devang Patelc4625da2007-04-07 01:25:15 +0000126 // suitable for loop rotation transformations.
Dan Gohman32663b72009-10-24 23:34:26 +0000127 if (!L->isLoopExiting(OrigHeader))
Devang Patelc4625da2007-04-07 01:25:15 +0000128 return false;
129
130 BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
131 if (!BI)
132 return false;
Chris Lattner2ba25432009-01-26 01:38:24 +0000133 assert(BI->isConditional() && "Branch Instruction is not conditional");
Devang Patelc4625da2007-04-07 01:25:15 +0000134
Devang Patel32231332007-04-09 16:11:48 +0000135 // Updating PHInodes in loops with multiple exits adds complexity.
136 // Keep it simple, and restrict loop rotation to loops with one exit only.
137 // In future, lift this restriction and support for multiple exits if
138 // required.
Devang Patelb7211a22007-08-21 00:31:24 +0000139 SmallVector<BasicBlock*, 8> ExitBlocks;
Devang Patelc4625da2007-04-07 01:25:15 +0000140 L->getExitBlocks(ExitBlocks);
141 if (ExitBlocks.size() > 1)
142 return false;
143
Chris Lattnerd9e07972011-01-02 07:35:53 +0000144 // Check size of original header and reject loop if it is very big.
145 {
146 CodeMetrics Metrics;
147 Metrics.analyzeBasicBlock(OrigHeader);
148 if (Metrics.NumInsts > MAX_HEADER_SIZE)
149 return false;
Devang Patel3f43a702009-03-06 03:51:30 +0000150 }
151
Devang Patel990e8662007-07-11 23:47:28 +0000152 // Now, this loop is suitable for rotation.
153
Dan Gohmane6fe67b2009-09-27 15:37:03 +0000154 // Anything ScalarEvolution may know about this loop or the PHI nodes
155 // in its header will soon be invalidated.
156 if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
Dan Gohman4c7279a2009-10-31 15:04:55 +0000157 SE->forgetLoop(L);
Dan Gohmane6fe67b2009-09-27 15:37:03 +0000158
Devang Patelc4625da2007-04-07 01:25:15 +0000159 // Find new Loop header. NewHeader is a Header's one and only successor
Chris Lattnerf6784a32009-01-26 01:57:01 +0000160 // that is inside loop. Header's other successor is outside the
161 // loop. Otherwise loop is not suitable for rotation.
Devang Patel32231332007-04-09 16:11:48 +0000162 Exit = BI->getSuccessor(0);
163 NewHeader = BI->getSuccessor(1);
164 if (L->contains(Exit))
165 std::swap(Exit, NewHeader);
Chris Lattner2ba25432009-01-26 01:38:24 +0000166 assert(NewHeader && "Unable to determine new loop header");
Devang Patel32231332007-04-09 16:11:48 +0000167 assert(L->contains(NewHeader) && !L->contains(Exit) &&
168 "Unable to determine loop header and exit blocks");
Chris Lattner3796a262009-01-26 02:11:30 +0000169
Dan Gohmancc4e6052009-06-25 00:22:44 +0000170 // This code assumes that the new header has exactly one predecessor.
171 // Remove any single-entry PHI nodes in it.
Chris Lattner3796a262009-01-26 02:11:30 +0000172 assert(NewHeader->getSinglePredecessor() &&
173 "New header doesn't have one pred!");
174 FoldSingleEntryPHINodes(NewHeader);
Devang Patelc4625da2007-04-07 01:25:15 +0000175
Dan Gohmane6e37b92009-10-24 23:19:52 +0000176 // Begin by walking OrigHeader and populating ValueMap with an entry for
177 // each Instruction.
Devang Patel32231332007-04-09 16:11:48 +0000178 BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
Chris Lattner6ccb3652011-01-08 07:21:31 +0000179 ValueToValueMapTy ValueMap;
Devang Patele9881542007-04-09 19:04:21 +0000180
Dan Gohmane6e37b92009-10-24 23:19:52 +0000181 // For PHI nodes, the value available in OldPreHeader is just the
182 // incoming value from OldPreHeader.
183 for (; PHINode *PN = dyn_cast<PHINode>(I); ++I)
184 ValueMap[PN] = PN->getIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
Devang Patelc4625da2007-04-07 01:25:15 +0000185
Chris Lattner50fb4692010-09-06 01:10:22 +0000186 // For the rest of the instructions, either hoist to the OrigPreheader if
187 // possible or create a clone in the OldPreHeader if not.
Dan Gohmane6e37b92009-10-24 23:19:52 +0000188 TerminatorInst *LoopEntryBranch = OrigPreHeader->getTerminator();
Chris Lattner50fb4692010-09-06 01:10:22 +0000189 while (I != E) {
190 Instruction *Inst = I++;
191
192 // If the instruction's operands are invariant and it doesn't read or write
193 // memory, then it is safe to hoist. Doing this doesn't change the order of
194 // execution in the preheader, but does prevent the instruction from
195 // executing in each iteration of the loop. This means it is safe to hoist
196 // something that might trap, but isn't safe to hoist something that reads
197 // memory (without proving that the loop doesn't write).
198 if (L->hasLoopInvariantOperands(Inst) &&
199 !Inst->mayReadFromMemory() && !Inst->mayWriteToMemory() &&
200 !isa<TerminatorInst>(Inst)) {
201 Inst->moveBefore(LoopEntryBranch);
202 continue;
203 }
204
205 // Otherwise, create a duplicate of the instruction.
206 Instruction *C = Inst->clone();
Chris Lattnerb5fa5fc2011-01-08 08:15:20 +0000207
Chris Lattner50fb4692010-09-06 01:10:22 +0000208 C->setName(Inst->getName());
Dan Gohmane6e37b92009-10-24 23:19:52 +0000209 C->insertBefore(LoopEntryBranch);
Chris Lattner50fb4692010-09-06 01:10:22 +0000210 ValueMap[Inst] = C;
Devang Patelc4625da2007-04-07 01:25:15 +0000211 }
212
Dan Gohmane6e37b92009-10-24 23:19:52 +0000213 // Along with all the other instructions, we just cloned OrigHeader's
214 // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's
215 // successors by duplicating their incoming values for OrigHeader.
216 TerminatorInst *TI = OrigHeader->getTerminator();
217 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
218 for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin();
219 PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
220 PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreHeader);
Devang Patelc4625da2007-04-07 01:25:15 +0000221
Dan Gohmane6e37b92009-10-24 23:19:52 +0000222 // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove
223 // OrigPreHeader's old terminator (the original branch into the loop), and
224 // remove the corresponding incoming values from the PHI nodes in OrigHeader.
225 LoopEntryBranch->eraseFromParent();
226 for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I)
227 PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
Devang Patelc4625da2007-04-07 01:25:15 +0000228
Dan Gohman440e2512009-10-26 15:55:24 +0000229 // Now fix up users of the instructions in OrigHeader, inserting PHI nodes
Dan Gohmane6e37b92009-10-24 23:19:52 +0000230 // as necessary.
231 SSAUpdater SSA;
232 for (I = OrigHeader->begin(); I != E; ++I) {
233 Value *OrigHeaderVal = I;
234 Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal];
Devang Patelc4625da2007-04-07 01:25:15 +0000235
Chris Lattner6ccb3652011-01-08 07:21:31 +0000236 // If there are no uses of the value (e.g. because it returns void), there
237 // is nothing to rewrite.
238 if (OrigHeaderVal->use_empty() && OrigPreHeaderVal->use_empty())
239 continue;
240
Dan Gohmane6e37b92009-10-24 23:19:52 +0000241 // The value now exits in two versions: the initial value in the preheader
242 // and the loop "next" value in the original header.
Duncan Sandsfc6e29d2010-09-02 08:14:03 +0000243 SSA.Initialize(OrigHeaderVal->getType(), OrigHeaderVal->getName());
Dan Gohmane6e37b92009-10-24 23:19:52 +0000244 SSA.AddAvailableValue(OrigHeader, OrigHeaderVal);
245 SSA.AddAvailableValue(OrigPreHeader, OrigPreHeaderVal);
Devang Patelc4625da2007-04-07 01:25:15 +0000246
Dan Gohmane6e37b92009-10-24 23:19:52 +0000247 // Visit each use of the OrigHeader instruction.
248 for (Value::use_iterator UI = OrigHeaderVal->use_begin(),
249 UE = OrigHeaderVal->use_end(); UI != UE; ) {
250 // Grab the use before incrementing the iterator.
251 Use &U = UI.getUse();
Devang Patelc4625da2007-04-07 01:25:15 +0000252
Dan Gohmane6e37b92009-10-24 23:19:52 +0000253 // Increment the iterator before removing the use from the list.
254 ++UI;
255
256 // SSAUpdater can't handle a non-PHI use in the same block as an
257 // earlier def. We can easily handle those cases manually.
258 Instruction *UserInst = cast<Instruction>(U.getUser());
259 if (!isa<PHINode>(UserInst)) {
260 BasicBlock *UserBB = UserInst->getParent();
261
262 // The original users in the OrigHeader are already using the
263 // original definitions.
264 if (UserBB == OrigHeader)
Devang Patel24a1c492007-04-09 16:21:29 +0000265 continue;
266
Dan Gohmane6e37b92009-10-24 23:19:52 +0000267 // Users in the OrigPreHeader need to use the value to which the
268 // original definitions are mapped.
269 if (UserBB == OrigPreHeader) {
270 U = OrigPreHeaderVal;
Devang Patelc4625da2007-04-07 01:25:15 +0000271 continue;
Dan Gohmane6e37b92009-10-24 23:19:52 +0000272 }
Devang Patelc4625da2007-04-07 01:25:15 +0000273 }
274
Dan Gohmane6e37b92009-10-24 23:19:52 +0000275 // Anything else can be handled by SSAUpdater.
276 SSA.RewriteUse(U);
Devang Patelc4625da2007-04-07 01:25:15 +0000277 }
278 }
Devang Patelc4625da2007-04-07 01:25:15 +0000279
Dan Gohmane6e37b92009-10-24 23:19:52 +0000280 // NewHeader is now the header of the loop.
Devang Patelc4625da2007-04-07 01:25:15 +0000281 L->moveToHeader(NewHeader);
282
Dan Gohmanfc8042a2010-08-17 17:39:21 +0000283 // Move the original header to the bottom of the loop, where it now more
284 // naturally belongs. This isn't necessary for correctness, and CodeGen can
285 // usually reorder blocks on its own to fix things like this up, but it's
286 // still nice to keep the IR readable.
287 //
288 // The original header should have only one predecessor at this point, since
289 // we checked that the loop had a proper preheader and unique backedge before
290 // we started.
291 assert(OrigHeader->getSinglePredecessor() &&
292 "Original loop header has too many predecessors after loop rotation!");
293 OrigHeader->moveAfter(OrigHeader->getSinglePredecessor());
294
295 // Also, since this original header only has one predecessor, zap its
296 // PHI nodes, which are now trivial.
297 FoldSingleEntryPHINodes(OrigHeader);
298
299 // TODO: We could just go ahead and merge OrigHeader into its predecessor
300 // at this point, if we don't mind updating dominator info.
301
302 // Establish a new preheader, update dominators, etc.
Devang Patel5464b962007-04-09 20:19:46 +0000303 preserveCanonicalLoopForm(LPM);
304
Dan Gohmanfe601042010-06-22 15:08:57 +0000305 ++NumRotated;
Devang Patelc4625da2007-04-07 01:25:15 +0000306 return true;
307}
308
Devang Patelc4625da2007-04-07 01:25:15 +0000309/// Initialize local data
310void LoopRotate::initialize() {
311 L = NULL;
312 OrigHeader = NULL;
313 OrigPreHeader = NULL;
314 NewHeader = NULL;
Devang Patelc4625da2007-04-07 01:25:15 +0000315 Exit = NULL;
Devang Patelc4625da2007-04-07 01:25:15 +0000316}
Devang Patel5464b962007-04-09 20:19:46 +0000317
318/// After loop rotation, loop pre-header has multiple sucessors.
319/// Insert one forwarding basic block to ensure that loop pre-header
320/// has only one successor.
321void LoopRotate::preserveCanonicalLoopForm(LPPassManager &LPM) {
322
323 // Right now original pre-header has two successors, new header and
324 // exit block. Insert new block between original pre-header and
325 // new header such that loop's new pre-header has only one successor.
Owen Anderson1d0be152009-08-13 21:58:54 +0000326 BasicBlock *NewPreHeader = BasicBlock::Create(OrigHeader->getContext(),
327 "bb.nph",
Gabor Greifb1dbcd82008-05-15 10:04:30 +0000328 OrigHeader->getParent(),
Gabor Greif051a9502008-04-06 20:25:17 +0000329 NewHeader);
Dan Gohman6a02fc32009-11-05 19:43:25 +0000330 LoopInfo &LI = getAnalysis<LoopInfo>();
Devang Patel5464b962007-04-09 20:19:46 +0000331 if (Loop *PL = LI.getLoopFor(OrigPreHeader))
Owen Andersond735ee82007-11-27 03:43:35 +0000332 PL->addBasicBlockToLoop(NewPreHeader, LI.getBase());
Gabor Greif051a9502008-04-06 20:25:17 +0000333 BranchInst::Create(NewHeader, NewPreHeader);
Devang Patel5464b962007-04-09 20:19:46 +0000334
335 BranchInst *OrigPH_BI = cast<BranchInst>(OrigPreHeader->getTerminator());
336 if (OrigPH_BI->getSuccessor(0) == NewHeader)
337 OrigPH_BI->setSuccessor(0, NewPreHeader);
338 else {
Chris Lattner2ba25432009-01-26 01:38:24 +0000339 assert(OrigPH_BI->getSuccessor(1) == NewHeader &&
340 "Unexpected original pre-header terminator");
Devang Patel5464b962007-04-09 20:19:46 +0000341 OrigPH_BI->setSuccessor(1, NewPreHeader);
342 }
Devang Patel5464b962007-04-09 20:19:46 +0000343
Dan Gohmancfb32202009-06-27 21:23:40 +0000344 PHINode *PN;
345 for (BasicBlock::iterator I = NewHeader->begin();
346 (PN = dyn_cast<PHINode>(I)); ++I) {
Devang Patel5464b962007-04-09 20:19:46 +0000347 int index = PN->getBasicBlockIndex(OrigPreHeader);
Chris Lattner2ba25432009-01-26 01:38:24 +0000348 assert(index != -1 && "Expected incoming value from Original PreHeader");
Devang Patel5464b962007-04-09 20:19:46 +0000349 PN->setIncomingBlock(index, NewPreHeader);
Chris Lattner2ba25432009-01-26 01:38:24 +0000350 assert(PN->getBasicBlockIndex(OrigPreHeader) == -1 &&
351 "Expected only one incoming value from Original PreHeader");
Devang Patel5464b962007-04-09 20:19:46 +0000352 }
353
Duncan Sands1465d612009-01-28 13:14:17 +0000354 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
Devang Patel990e8662007-07-11 23:47:28 +0000355 DT->addNewBlock(NewPreHeader, OrigPreHeader);
356 DT->changeImmediateDominator(L->getHeader(), NewPreHeader);
357 DT->changeImmediateDominator(Exit, OrigPreHeader);
358 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
359 BI != BE; ++BI) {
360 BasicBlock *B = *BI;
361 if (L->getHeader() != B) {
362 DomTreeNode *Node = DT->getNode(B);
363 if (Node && Node->getBlock() == OrigHeader)
364 DT->changeImmediateDominator(*BI, L->getHeader());
365 }
366 }
367 DT->changeImmediateDominator(OrigHeader, OrigLatch);
368 }
369
Duncan Sands1465d612009-01-28 13:14:17 +0000370 if (DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>()) {
Devang Patel990e8662007-07-11 23:47:28 +0000371 // New Preheader's dominance frontier is Exit block.
372 DominanceFrontier::DomSetType NewPHSet;
373 NewPHSet.insert(Exit);
374 DF->addBasicBlock(NewPreHeader, NewPHSet);
375
376 // New Header's dominance frontier now includes itself and Exit block
377 DominanceFrontier::iterator HeadI = DF->find(L->getHeader());
378 if (HeadI != DF->end()) {
379 DominanceFrontier::DomSetType & HeaderSet = HeadI->second;
380 HeaderSet.clear();
381 HeaderSet.insert(L->getHeader());
382 HeaderSet.insert(Exit);
383 } else {
384 DominanceFrontier::DomSetType HeaderSet;
385 HeaderSet.insert(L->getHeader());
386 HeaderSet.insert(Exit);
387 DF->addBasicBlock(L->getHeader(), HeaderSet);
388 }
389
390 // Original header (new Loop Latch)'s dominance frontier is Exit.
391 DominanceFrontier::iterator LatchI = DF->find(L->getLoopLatch());
392 if (LatchI != DF->end()) {
393 DominanceFrontier::DomSetType &LatchSet = LatchI->second;
394 LatchSet = LatchI->second;
395 LatchSet.clear();
396 LatchSet.insert(Exit);
397 } else {
398 DominanceFrontier::DomSetType LatchSet;
399 LatchSet.insert(Exit);
400 DF->addBasicBlock(L->getHeader(), LatchSet);
401 }
402
Devang Patelb7f40c12009-08-07 17:16:44 +0000403 // If a loop block dominates new loop latch then add to its frontiers
404 // new header and Exit and remove new latch (which is equal to original
405 // header).
Devang Patel990e8662007-07-11 23:47:28 +0000406 BasicBlock *NewLatch = L->getLoopLatch();
Devang Patelb7f40c12009-08-07 17:16:44 +0000407
408 assert(NewLatch == OrigHeader && "NewLatch is inequal to OrigHeader");
409
410 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
411 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
412 BI != BE; ++BI) {
413 BasicBlock *B = *BI;
414 if (DT->dominates(B, NewLatch)) {
415 DominanceFrontier::iterator BDFI = DF->find(B);
416 if (BDFI != DF->end()) {
417 DominanceFrontier::DomSetType &BSet = BDFI->second;
418 BSet.erase(NewLatch);
419 BSet.insert(L->getHeader());
420 BSet.insert(Exit);
421 } else {
422 DominanceFrontier::DomSetType BSet;
423 BSet.insert(L->getHeader());
424 BSet.insert(Exit);
425 DF->addBasicBlock(B, BSet);
426 }
Devang Patel990e8662007-07-11 23:47:28 +0000427 }
428 }
429 }
430 }
431
432 // Preserve canonical loop form, which means Exit block should
433 // have only one predecessor.
Dan Gohmanc292caf2009-09-09 18:18:18 +0000434 SplitEdge(L->getLoopLatch(), Exit, this);
Devang Patel990e8662007-07-11 23:47:28 +0000435
Chris Lattner2ba25432009-01-26 01:38:24 +0000436 assert(NewHeader && L->getHeader() == NewHeader &&
437 "Invalid loop header after loop rotation");
438 assert(NewPreHeader && L->getLoopPreheader() == NewPreHeader &&
439 "Invalid loop preheader after loop rotation");
440 assert(L->getLoopLatch() &&
441 "Invalid loop latch after loop rotation");
Devang Patel5464b962007-04-09 20:19:46 +0000442}