blob: 6e0f67b67296fb2124f4762d75546f9788093949 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Loop Rotation Pass.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "loop-rotate"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/Transforms/Scalar.h"
16#include "llvm/Function.h"
Devang Patel398911e2009-03-06 03:51:30 +000017#include "llvm/IntrinsicInst.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Analysis/LoopPass.h"
20#include "llvm/Analysis/Dominators.h"
21#include "llvm/Analysis/ScalarEvolution.h"
22#include "llvm/Transforms/Utils/Local.h"
23#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Dan Gohmanf3c12362009-10-24 23:19:52 +000024#include "llvm/Transforms/Utils/SSAUpdater.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029using namespace llvm;
30
31#define MAX_HEADER_SIZE 16
32
33STATISTIC(NumRotated, "Number of loops rotated");
34namespace {
35
Chris Lattnerfa2d1ba2009-09-02 06:11:42 +000036 class LoopRotate : public LoopPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 public:
38 static char ID; // Pass ID, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000039 LoopRotate() : LoopPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
41 // Rotate Loop L as many times as possible. Return true if
42 // loop is rotated at least once.
43 bool runOnLoop(Loop *L, LPPassManager &LPM);
44
45 // LCSSA form makes instruction renaming easier.
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Devang Patel54f13c12008-02-15 01:24:49 +000047 AU.addRequiredID(LoopSimplifyID);
48 AU.addPreservedID(LoopSimplifyID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 AU.addRequiredID(LCSSAID);
50 AU.addPreservedID(LCSSAID);
51 AU.addPreserved<ScalarEvolution>();
52 AU.addPreserved<LoopInfo>();
Devang Pateld6bf3d52007-07-30 20:22:53 +000053 AU.addPreserved<DominatorTree>();
54 AU.addPreserved<DominanceFrontier>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
Dan Gohmanf17a25c2007-07-18 16:29: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
70 private:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 Loop *L;
72 BasicBlock *OrigHeader;
73 BasicBlock *OrigPreHeader;
74 BasicBlock *OrigLatch;
75 BasicBlock *NewHeader;
76 BasicBlock *Exit;
77 LPPassManager *LPM_Ptr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079}
Dan Gohman089efff2008-05-13 00:00:25 +000080
81char LoopRotate::ID = 0;
82static RegisterPass<LoopRotate> X("loop-rotate", "Rotate Loops");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083
Daniel Dunbar163555a2008-10-22 23:32:42 +000084Pass *llvm::createLoopRotatePass() { return new LoopRotate(); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085
86/// Rotate Loop L as many times as possible. Return true if
Dan Gohmanfb0239f2009-06-25 00:22:44 +000087/// the loop is rotated at least once.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088bool LoopRotate::runOnLoop(Loop *Lp, LPPassManager &LPM) {
89
90 bool RotatedOneLoop = false;
91 initialize();
92 LPM_Ptr = &LPM;
93
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
103/// Rotate loop LP. Return true if the loop is rotated.
104bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 L = Lp;
106
107 OrigHeader = L->getHeader();
108 OrigPreHeader = L->getLoopPreheader();
109 OrigLatch = L->getLoopLatch();
110
Dan Gohmanfb0239f2009-06-25 00:22:44 +0000111 // If the loop has only one block then there is not much to rotate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 if (L->getBlocks().size() == 1)
113 return false;
114
lattner34e85d82009-01-26 01:38:24 +0000115 assert(OrigHeader && OrigLatch && OrigPreHeader &&
116 "Loop is not in canonical form");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117
Dan Gohmanfb0239f2009-06-25 00:22:44 +0000118 // If the loop header is not one of the loop exiting blocks then
119 // either this loop is already rotated or it is not
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 // suitable for loop rotation transformations.
Dan Gohman3a175212009-10-24 23:34:26 +0000121 if (!L->isLoopExiting(OrigHeader))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 return false;
123
124 BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
125 if (!BI)
126 return false;
lattner34e85d82009-01-26 01:38:24 +0000127 assert(BI->isConditional() && "Branch Instruction is not conditional");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128
129 // Updating PHInodes in loops with multiple exits adds complexity.
130 // Keep it simple, and restrict loop rotation to loops with one exit only.
131 // In future, lift this restriction and support for multiple exits if
132 // required.
Devang Patel02451fa2007-08-21 00:31:24 +0000133 SmallVector<BasicBlock*, 8> ExitBlocks;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 L->getExitBlocks(ExitBlocks);
135 if (ExitBlocks.size() > 1)
136 return false;
137
138 // Check size of original header and reject
139 // loop if it is very big.
Devang Patel398911e2009-03-06 03:51:30 +0000140 unsigned Size = 0;
141
142 // FIXME: Use common api to estimate size.
143 for (BasicBlock::const_iterator OI = OrigHeader->begin(),
144 OE = OrigHeader->end(); OI != OE; ++OI) {
145 if (isa<PHINode>(OI))
146 continue; // PHI nodes don't count.
147 if (isa<DbgInfoIntrinsic>(OI))
148 continue; // Debug intrinsics don't count as size.
149 Size++;
150 }
151
152 if (Size > MAX_HEADER_SIZE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 return false;
154
155 // Now, this loop is suitable for rotation.
156
Dan Gohman7936cb82009-09-27 15:37:03 +0000157 // Anything ScalarEvolution may know about this loop or the PHI nodes
158 // in its header will soon be invalidated.
159 if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
160 SE->forgetLoopBackedgeTakenCount(L);
161
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 // Find new Loop header. NewHeader is a Header's one and only successor
lattner8a925842009-01-26 01:57:01 +0000163 // that is inside loop. Header's other successor is outside the
164 // loop. Otherwise loop is not suitable for rotation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 Exit = BI->getSuccessor(0);
166 NewHeader = BI->getSuccessor(1);
167 if (L->contains(Exit))
168 std::swap(Exit, NewHeader);
lattner34e85d82009-01-26 01:38:24 +0000169 assert(NewHeader && "Unable to determine new loop header");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 assert(L->contains(NewHeader) && !L->contains(Exit) &&
171 "Unable to determine loop header and exit blocks");
lattnercb011ab2009-01-26 02:11:30 +0000172
Dan Gohmanfb0239f2009-06-25 00:22:44 +0000173 // This code assumes that the new header has exactly one predecessor.
174 // Remove any single-entry PHI nodes in it.
lattnercb011ab2009-01-26 02:11:30 +0000175 assert(NewHeader->getSinglePredecessor() &&
176 "New header doesn't have one pred!");
177 FoldSingleEntryPHINodes(NewHeader);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178
Dan Gohmanf3c12362009-10-24 23:19:52 +0000179 // Begin by walking OrigHeader and populating ValueMap with an entry for
180 // each Instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
Dan Gohmanf3c12362009-10-24 23:19:52 +0000182 DenseMap<const Value *, Value *> ValueMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183
Dan Gohmanf3c12362009-10-24 23:19:52 +0000184 // For PHI nodes, the value available in OldPreHeader is just the
185 // incoming value from OldPreHeader.
186 for (; PHINode *PN = dyn_cast<PHINode>(I); ++I)
187 ValueMap[PN] = PN->getIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188
Dan Gohmanf3c12362009-10-24 23:19:52 +0000189 // For the rest of the instructions, create a clone in the OldPreHeader.
190 TerminatorInst *LoopEntryBranch = OrigPreHeader->getTerminator();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 for (; I != E; ++I) {
Dan Gohmanf3c12362009-10-24 23:19:52 +0000192 Instruction *C = I->clone();
193 C->setName(I->getName());
194 C->insertBefore(LoopEntryBranch);
195 ValueMap[I] = C;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 }
197
Dan Gohmanf3c12362009-10-24 23:19:52 +0000198 // Along with all the other instructions, we just cloned OrigHeader's
199 // terminator into OrigPreHeader. Fix up the PHI nodes in each of OrigHeader's
200 // successors by duplicating their incoming values for OrigHeader.
201 TerminatorInst *TI = OrigHeader->getTerminator();
202 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
203 for (BasicBlock::iterator BI = TI->getSuccessor(i)->begin();
204 PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
205 PN->addIncoming(PN->getIncomingValueForBlock(OrigHeader), OrigPreHeader);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206
Dan Gohmanf3c12362009-10-24 23:19:52 +0000207 // Now that OrigPreHeader has a clone of OrigHeader's terminator, remove
208 // OrigPreHeader's old terminator (the original branch into the loop), and
209 // remove the corresponding incoming values from the PHI nodes in OrigHeader.
210 LoopEntryBranch->eraseFromParent();
211 for (I = OrigHeader->begin(); PHINode *PN = dyn_cast<PHINode>(I); ++I)
212 PN->removeIncomingValue(PN->getBasicBlockIndex(OrigPreHeader));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213
Dan Gohmanf3c12362009-10-24 23:19:52 +0000214 // Now fix up users of the instructions in OrigHeader, insertting PHI nodes
215 // as necessary.
216 SSAUpdater SSA;
217 for (I = OrigHeader->begin(); I != E; ++I) {
218 Value *OrigHeaderVal = I;
219 Value *OrigPreHeaderVal = ValueMap[OrigHeaderVal];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220
Dan Gohmanf3c12362009-10-24 23:19:52 +0000221 // The value now exits in two versions: the initial value in the preheader
222 // and the loop "next" value in the original header.
223 SSA.Initialize(OrigHeaderVal);
224 SSA.AddAvailableValue(OrigHeader, OrigHeaderVal);
225 SSA.AddAvailableValue(OrigPreHeader, OrigPreHeaderVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226
Dan Gohmanf3c12362009-10-24 23:19:52 +0000227 // Visit each use of the OrigHeader instruction.
228 for (Value::use_iterator UI = OrigHeaderVal->use_begin(),
229 UE = OrigHeaderVal->use_end(); UI != UE; ) {
230 // Grab the use before incrementing the iterator.
231 Use &U = UI.getUse();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
Dan Gohmanf3c12362009-10-24 23:19:52 +0000233 // Increment the iterator before removing the use from the list.
234 ++UI;
235
236 // SSAUpdater can't handle a non-PHI use in the same block as an
237 // earlier def. We can easily handle those cases manually.
238 Instruction *UserInst = cast<Instruction>(U.getUser());
239 if (!isa<PHINode>(UserInst)) {
240 BasicBlock *UserBB = UserInst->getParent();
241
242 // The original users in the OrigHeader are already using the
243 // original definitions.
244 if (UserBB == OrigHeader)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 continue;
246
Dan Gohmanf3c12362009-10-24 23:19:52 +0000247 // Users in the OrigPreHeader need to use the value to which the
248 // original definitions are mapped.
249 if (UserBB == OrigPreHeader) {
250 U = OrigPreHeaderVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 continue;
Dan Gohmanf3c12362009-10-24 23:19:52 +0000252 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 }
254
Dan Gohmanf3c12362009-10-24 23:19:52 +0000255 // Anything else can be handled by SSAUpdater.
256 SSA.RewriteUse(U);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 }
258 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259
Dan Gohmanf3c12362009-10-24 23:19:52 +0000260 // NewHeader is now the header of the loop.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 L->moveToHeader(NewHeader);
262
263 preserveCanonicalLoopForm(LPM);
264
265 NumRotated++;
266 return true;
267}
268
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269/// Initialize local data
270void LoopRotate::initialize() {
271 L = NULL;
272 OrigHeader = NULL;
273 OrigPreHeader = NULL;
274 NewHeader = NULL;
275 Exit = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276}
277
278/// After loop rotation, loop pre-header has multiple sucessors.
279/// Insert one forwarding basic block to ensure that loop pre-header
280/// has only one successor.
281void LoopRotate::preserveCanonicalLoopForm(LPPassManager &LPM) {
282
283 // Right now original pre-header has two successors, new header and
284 // exit block. Insert new block between original pre-header and
285 // new header such that loop's new pre-header has only one successor.
Owen Anderson35b47072009-08-13 21:58:54 +0000286 BasicBlock *NewPreHeader = BasicBlock::Create(OrigHeader->getContext(),
287 "bb.nph",
Gabor Greifb91ea9d2008-05-15 10:04:30 +0000288 OrigHeader->getParent(),
Gabor Greifd6da1d02008-04-06 20:25:17 +0000289 NewHeader);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 LoopInfo &LI = LPM.getAnalysis<LoopInfo>();
291 if (Loop *PL = LI.getLoopFor(OrigPreHeader))
Owen Andersonca0b9d42007-11-27 03:43:35 +0000292 PL->addBasicBlockToLoop(NewPreHeader, LI.getBase());
Gabor Greifd6da1d02008-04-06 20:25:17 +0000293 BranchInst::Create(NewHeader, NewPreHeader);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294
295 BranchInst *OrigPH_BI = cast<BranchInst>(OrigPreHeader->getTerminator());
296 if (OrigPH_BI->getSuccessor(0) == NewHeader)
297 OrigPH_BI->setSuccessor(0, NewPreHeader);
298 else {
lattner34e85d82009-01-26 01:38:24 +0000299 assert(OrigPH_BI->getSuccessor(1) == NewHeader &&
300 "Unexpected original pre-header terminator");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 OrigPH_BI->setSuccessor(1, NewPreHeader);
302 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303
Dan Gohmana70d7572009-06-27 21:23:40 +0000304 PHINode *PN;
305 for (BasicBlock::iterator I = NewHeader->begin();
306 (PN = dyn_cast<PHINode>(I)); ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 int index = PN->getBasicBlockIndex(OrigPreHeader);
lattner34e85d82009-01-26 01:38:24 +0000308 assert(index != -1 && "Expected incoming value from Original PreHeader");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 PN->setIncomingBlock(index, NewPreHeader);
lattner34e85d82009-01-26 01:38:24 +0000310 assert(PN->getBasicBlockIndex(OrigPreHeader) == -1 &&
311 "Expected only one incoming value from Original PreHeader");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 }
313
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000314 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 DT->addNewBlock(NewPreHeader, OrigPreHeader);
316 DT->changeImmediateDominator(L->getHeader(), NewPreHeader);
317 DT->changeImmediateDominator(Exit, OrigPreHeader);
318 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
319 BI != BE; ++BI) {
320 BasicBlock *B = *BI;
321 if (L->getHeader() != B) {
322 DomTreeNode *Node = DT->getNode(B);
323 if (Node && Node->getBlock() == OrigHeader)
324 DT->changeImmediateDominator(*BI, L->getHeader());
325 }
326 }
327 DT->changeImmediateDominator(OrigHeader, OrigLatch);
328 }
329
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000330 if (DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 // New Preheader's dominance frontier is Exit block.
332 DominanceFrontier::DomSetType NewPHSet;
333 NewPHSet.insert(Exit);
334 DF->addBasicBlock(NewPreHeader, NewPHSet);
335
336 // New Header's dominance frontier now includes itself and Exit block
337 DominanceFrontier::iterator HeadI = DF->find(L->getHeader());
338 if (HeadI != DF->end()) {
339 DominanceFrontier::DomSetType & HeaderSet = HeadI->second;
340 HeaderSet.clear();
341 HeaderSet.insert(L->getHeader());
342 HeaderSet.insert(Exit);
343 } else {
344 DominanceFrontier::DomSetType HeaderSet;
345 HeaderSet.insert(L->getHeader());
346 HeaderSet.insert(Exit);
347 DF->addBasicBlock(L->getHeader(), HeaderSet);
348 }
349
350 // Original header (new Loop Latch)'s dominance frontier is Exit.
351 DominanceFrontier::iterator LatchI = DF->find(L->getLoopLatch());
352 if (LatchI != DF->end()) {
353 DominanceFrontier::DomSetType &LatchSet = LatchI->second;
354 LatchSet = LatchI->second;
355 LatchSet.clear();
356 LatchSet.insert(Exit);
357 } else {
358 DominanceFrontier::DomSetType LatchSet;
359 LatchSet.insert(Exit);
360 DF->addBasicBlock(L->getHeader(), LatchSet);
361 }
362
Devang Patel9522c802009-08-07 17:16:44 +0000363 // If a loop block dominates new loop latch then add to its frontiers
364 // new header and Exit and remove new latch (which is equal to original
365 // header).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 BasicBlock *NewLatch = L->getLoopLatch();
Devang Patel9522c802009-08-07 17:16:44 +0000367
368 assert(NewLatch == OrigHeader && "NewLatch is inequal to OrigHeader");
369
370 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
371 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
372 BI != BE; ++BI) {
373 BasicBlock *B = *BI;
374 if (DT->dominates(B, NewLatch)) {
375 DominanceFrontier::iterator BDFI = DF->find(B);
376 if (BDFI != DF->end()) {
377 DominanceFrontier::DomSetType &BSet = BDFI->second;
378 BSet.erase(NewLatch);
379 BSet.insert(L->getHeader());
380 BSet.insert(Exit);
381 } else {
382 DominanceFrontier::DomSetType BSet;
383 BSet.insert(L->getHeader());
384 BSet.insert(Exit);
385 DF->addBasicBlock(B, BSet);
386 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 }
388 }
389 }
390 }
391
392 // Preserve canonical loop form, which means Exit block should
393 // have only one predecessor.
Dan Gohmanf06a9ba2009-09-09 18:18:18 +0000394 SplitEdge(L->getLoopLatch(), Exit, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395
lattner34e85d82009-01-26 01:38:24 +0000396 assert(NewHeader && L->getHeader() == NewHeader &&
397 "Invalid loop header after loop rotation");
398 assert(NewPreHeader && L->getLoopPreheader() == NewPreHeader &&
399 "Invalid loop preheader after loop rotation");
400 assert(L->getLoopLatch() &&
401 "Invalid loop latch after loop rotation");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402}