add some missing quotes in debug output
[llvm/avr.git] / lib / Transforms / Scalar / LoopRotation.cpp
blobca394bdc46a3ce6470bb2ababaa09efb4b1f3f82
1 //===- LoopRotation.cpp - Loop Rotation Pass ------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements Loop Rotation Pass.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "loop-rotate"
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Function.h"
17 #include "llvm/IntrinsicInst.h"
18 #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"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/ADT/SmallVector.h"
28 using namespace llvm;
30 #define MAX_HEADER_SIZE 16
32 STATISTIC(NumRotated, "Number of loops rotated");
33 namespace {
35 class RenameData {
36 public:
37 RenameData(Instruction *O, Value *P, Instruction *H)
38 : Original(O), PreHeader(P), Header(H) { }
39 public:
40 Instruction *Original; // Original instruction
41 Value *PreHeader; // Original pre-header replacement
42 Instruction *Header; // New header replacement
45 class LoopRotate : public LoopPass {
46 public:
47 static char ID; // Pass ID, replacement for typeid
48 LoopRotate() : LoopPass(&ID) {}
50 // Rotate Loop L as many times as possible. Return true if
51 // loop is rotated at least once.
52 bool runOnLoop(Loop *L, LPPassManager &LPM);
54 // LCSSA form makes instruction renaming easier.
55 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.addRequiredID(LoopSimplifyID);
57 AU.addPreservedID(LoopSimplifyID);
58 AU.addRequiredID(LCSSAID);
59 AU.addPreservedID(LCSSAID);
60 AU.addPreserved<ScalarEvolution>();
61 AU.addPreserved<LoopInfo>();
62 AU.addPreserved<DominatorTree>();
63 AU.addPreserved<DominanceFrontier>();
66 // Helper functions
68 /// Do actual work
69 bool rotateLoop(Loop *L, LPPassManager &LPM);
71 /// Initialize local data
72 void initialize();
74 /// Make sure all Exit block PHINodes have required incoming values.
75 /// If incoming value is constant or defined outside the loop then
76 /// PHINode may not have an entry for original pre-header.
77 void updateExitBlock();
79 /// Return true if this instruction is used outside original header.
80 bool usedOutsideOriginalHeader(Instruction *In);
82 /// Find Replacement information for instruction. Return NULL if it is
83 /// not available.
84 const RenameData *findReplacementData(Instruction *I);
86 /// After loop rotation, loop pre-header has multiple sucessors.
87 /// Insert one forwarding basic block to ensure that loop pre-header
88 /// has only one successor.
89 void preserveCanonicalLoopForm(LPPassManager &LPM);
91 private:
93 Loop *L;
94 BasicBlock *OrigHeader;
95 BasicBlock *OrigPreHeader;
96 BasicBlock *OrigLatch;
97 BasicBlock *NewHeader;
98 BasicBlock *Exit;
99 LPPassManager *LPM_Ptr;
100 SmallVector<RenameData, MAX_HEADER_SIZE> LoopHeaderInfo;
104 char LoopRotate::ID = 0;
105 static RegisterPass<LoopRotate> X("loop-rotate", "Rotate Loops");
107 Pass *llvm::createLoopRotatePass() { return new LoopRotate(); }
109 /// Rotate Loop L as many times as possible. Return true if
110 /// the loop is rotated at least once.
111 bool LoopRotate::runOnLoop(Loop *Lp, LPPassManager &LPM) {
113 bool RotatedOneLoop = false;
114 initialize();
115 LPM_Ptr = &LPM;
117 // One loop can be rotated multiple times.
118 while (rotateLoop(Lp,LPM)) {
119 RotatedOneLoop = true;
120 initialize();
123 return RotatedOneLoop;
126 /// Rotate loop LP. Return true if the loop is rotated.
127 bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
128 L = Lp;
130 OrigHeader = L->getHeader();
131 OrigPreHeader = L->getLoopPreheader();
132 OrigLatch = L->getLoopLatch();
134 // If the loop has only one block then there is not much to rotate.
135 if (L->getBlocks().size() == 1)
136 return false;
138 assert(OrigHeader && OrigLatch && OrigPreHeader &&
139 "Loop is not in canonical form");
141 // If the loop header is not one of the loop exiting blocks then
142 // either this loop is already rotated or it is not
143 // suitable for loop rotation transformations.
144 if (!L->isLoopExit(OrigHeader))
145 return false;
147 BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
148 if (!BI)
149 return false;
150 assert(BI->isConditional() && "Branch Instruction is not conditional");
152 // Updating PHInodes in loops with multiple exits adds complexity.
153 // Keep it simple, and restrict loop rotation to loops with one exit only.
154 // In future, lift this restriction and support for multiple exits if
155 // required.
156 SmallVector<BasicBlock*, 8> ExitBlocks;
157 L->getExitBlocks(ExitBlocks);
158 if (ExitBlocks.size() > 1)
159 return false;
161 // Check size of original header and reject
162 // loop if it is very big.
163 unsigned Size = 0;
165 // FIXME: Use common api to estimate size.
166 for (BasicBlock::const_iterator OI = OrigHeader->begin(),
167 OE = OrigHeader->end(); OI != OE; ++OI) {
168 if (isa<PHINode>(OI))
169 continue; // PHI nodes don't count.
170 if (isa<DbgInfoIntrinsic>(OI))
171 continue; // Debug intrinsics don't count as size.
172 Size++;
175 if (Size > MAX_HEADER_SIZE)
176 return false;
178 // Now, this loop is suitable for rotation.
180 // Find new Loop header. NewHeader is a Header's one and only successor
181 // that is inside loop. Header's other successor is outside the
182 // loop. Otherwise loop is not suitable for rotation.
183 Exit = BI->getSuccessor(0);
184 NewHeader = BI->getSuccessor(1);
185 if (L->contains(Exit))
186 std::swap(Exit, NewHeader);
187 assert(NewHeader && "Unable to determine new loop header");
188 assert(L->contains(NewHeader) && !L->contains(Exit) &&
189 "Unable to determine loop header and exit blocks");
191 // This code assumes that the new header has exactly one predecessor.
192 // Remove any single-entry PHI nodes in it.
193 assert(NewHeader->getSinglePredecessor() &&
194 "New header doesn't have one pred!");
195 FoldSingleEntryPHINodes(NewHeader);
197 // Copy PHI nodes and other instructions from the original header
198 // into the original pre-header. Unlike the original header, the original
199 // pre-header is not a member of the loop.
201 // The new loop header is the one and only successor of original header that
202 // is inside the loop. All other original header successors are outside
203 // the loop. Copy PHI Nodes from the original header into the new loop header.
204 // Add second incoming value, from original loop pre-header into these phi
205 // nodes. If a value defined in original header is used outside original
206 // header then new loop header will need new phi nodes with two incoming
207 // values, one definition from original header and second definition is
208 // from original loop pre-header.
210 // Remove terminator from Original pre-header. Original pre-header will
211 // receive a clone of original header terminator as a new terminator.
212 OrigPreHeader->getInstList().pop_back();
213 BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
214 PHINode *PN = 0;
215 for (; (PN = dyn_cast<PHINode>(I)); ++I) {
216 // PHI nodes are not copied into original pre-header. Instead their values
217 // are directly propagated.
218 Value *NPV = PN->getIncomingValueForBlock(OrigPreHeader);
220 // Create a new PHI node with two incoming values for NewHeader.
221 // One incoming value is from OrigLatch (through OrigHeader) and the
222 // second incoming value is from original pre-header.
223 PHINode *NH = PHINode::Create(PN->getType(), PN->getName(),
224 NewHeader->begin());
225 NH->addIncoming(PN->getIncomingValueForBlock(OrigLatch), OrigHeader);
226 NH->addIncoming(NPV, OrigPreHeader);
228 // "In" can be replaced by NH at various places.
229 LoopHeaderInfo.push_back(RenameData(PN, NPV, NH));
232 // Now, handle non-phi instructions.
233 for (; I != E; ++I) {
234 Instruction *In = I;
235 assert(!isa<PHINode>(In) && "PHINode is not expected here");
237 // This is not a PHI instruction. Insert its clone into original pre-header.
238 // If this instruction is using a value from same basic block then
239 // update it to use value from cloned instruction.
240 Instruction *C = In->clone(In->getContext());
241 C->setName(In->getName());
242 OrigPreHeader->getInstList().push_back(C);
244 for (unsigned opi = 0, e = In->getNumOperands(); opi != e; ++opi) {
245 Instruction *OpInsn = dyn_cast<Instruction>(In->getOperand(opi));
246 if (!OpInsn) continue; // Ignore non-instruction values.
247 if (const RenameData *D = findReplacementData(OpInsn))
248 C->setOperand(opi, D->PreHeader);
251 // If this instruction is used outside this basic block then
252 // create new PHINode for this instruction.
253 Instruction *NewHeaderReplacement = NULL;
254 if (usedOutsideOriginalHeader(In)) {
255 PHINode *PN = PHINode::Create(In->getType(), In->getName(),
256 NewHeader->begin());
257 PN->addIncoming(In, OrigHeader);
258 PN->addIncoming(C, OrigPreHeader);
259 NewHeaderReplacement = PN;
261 LoopHeaderInfo.push_back(RenameData(In, C, NewHeaderReplacement));
264 // Rename uses of original header instructions to reflect their new
265 // definitions (either from original pre-header node or from newly created
266 // new header PHINodes.
268 // Original header instructions are used in
269 // 1) Original header:
271 // If instruction is used in non-phi instructions then it is using
272 // defintion from original heder iteself. Do not replace this use
273 // with definition from new header or original pre-header.
275 // If instruction is used in phi node then it is an incoming
276 // value. Rename its use to reflect new definition from new-preheader
277 // or new header.
279 // 2) Inside loop but not in original header
281 // Replace this use to reflect definition from new header.
282 for (unsigned LHI = 0, LHI_E = LoopHeaderInfo.size(); LHI != LHI_E; ++LHI) {
283 const RenameData &ILoopHeaderInfo = LoopHeaderInfo[LHI];
285 if (!ILoopHeaderInfo.Header)
286 continue;
288 Instruction *OldPhi = ILoopHeaderInfo.Original;
289 Instruction *NewPhi = ILoopHeaderInfo.Header;
291 // Before replacing uses, collect them first, so that iterator is
292 // not invalidated.
293 SmallVector<Instruction *, 16> AllUses;
294 for (Value::use_iterator UI = OldPhi->use_begin(), UE = OldPhi->use_end();
295 UI != UE; ++UI)
296 AllUses.push_back(cast<Instruction>(UI));
298 for (SmallVector<Instruction *, 16>::iterator UI = AllUses.begin(),
299 UE = AllUses.end(); UI != UE; ++UI) {
300 Instruction *U = *UI;
301 BasicBlock *Parent = U->getParent();
303 // Used inside original header
304 if (Parent == OrigHeader) {
305 // Do not rename uses inside original header non-phi instructions.
306 PHINode *PU = dyn_cast<PHINode>(U);
307 if (!PU)
308 continue;
310 // Do not rename uses inside original header phi nodes, if the
311 // incoming value is for new header.
312 if (PU->getBasicBlockIndex(NewHeader) != -1
313 && PU->getIncomingValueForBlock(NewHeader) == U)
314 continue;
316 U->replaceUsesOfWith(OldPhi, NewPhi);
317 continue;
320 // Used inside loop, but not in original header.
321 if (L->contains(U->getParent())) {
322 if (U != NewPhi)
323 U->replaceUsesOfWith(OldPhi, NewPhi);
324 continue;
327 // Used inside Exit Block. Since we are in LCSSA form, U must be PHINode.
328 if (U->getParent() == Exit) {
329 assert(isa<PHINode>(U) && "Use in Exit Block that is not PHINode");
331 PHINode *UPhi = cast<PHINode>(U);
332 // UPhi already has one incoming argument from original header.
333 // Add second incoming argument from new Pre header.
334 UPhi->addIncoming(ILoopHeaderInfo.PreHeader, OrigPreHeader);
335 } else {
336 // Used outside Exit block. Create a new PHI node in the exit block
337 // to receive the value from the new header and pre-header.
338 PHINode *PN = PHINode::Create(U->getType(), U->getName(),
339 Exit->begin());
340 PN->addIncoming(ILoopHeaderInfo.PreHeader, OrigPreHeader);
341 PN->addIncoming(OldPhi, OrigHeader);
342 U->replaceUsesOfWith(OldPhi, PN);
347 /// Make sure all Exit block PHINodes have required incoming values.
348 updateExitBlock();
350 // Update CFG
352 // Removing incoming branch from loop preheader to original header.
353 // Now original header is inside the loop.
354 for (BasicBlock::iterator I = OrigHeader->begin();
355 (PN = dyn_cast<PHINode>(I)); ++I)
356 PN->removeIncomingValue(OrigPreHeader);
358 // Make NewHeader as the new header for the loop.
359 L->moveToHeader(NewHeader);
361 preserveCanonicalLoopForm(LPM);
363 NumRotated++;
364 return true;
367 /// Make sure all Exit block PHINodes have required incoming values.
368 /// If an incoming value is constant or defined outside the loop then
369 /// PHINode may not have an entry for the original pre-header.
370 void LoopRotate::updateExitBlock() {
372 PHINode *PN;
373 for (BasicBlock::iterator I = Exit->begin();
374 (PN = dyn_cast<PHINode>(I)); ++I) {
376 // There is already one incoming value from original pre-header block.
377 if (PN->getBasicBlockIndex(OrigPreHeader) != -1)
378 continue;
380 const RenameData *ILoopHeaderInfo;
381 Value *V = PN->getIncomingValueForBlock(OrigHeader);
382 if (isa<Instruction>(V) &&
383 (ILoopHeaderInfo = findReplacementData(cast<Instruction>(V)))) {
384 assert(ILoopHeaderInfo->PreHeader && "Missing New Preheader Instruction");
385 PN->addIncoming(ILoopHeaderInfo->PreHeader, OrigPreHeader);
386 } else {
387 PN->addIncoming(V, OrigPreHeader);
392 /// Initialize local data
393 void LoopRotate::initialize() {
394 L = NULL;
395 OrigHeader = NULL;
396 OrigPreHeader = NULL;
397 NewHeader = NULL;
398 Exit = NULL;
400 LoopHeaderInfo.clear();
403 /// Return true if this instruction is used by any instructions in the loop that
404 /// aren't in original header.
405 bool LoopRotate::usedOutsideOriginalHeader(Instruction *In) {
406 for (Value::use_iterator UI = In->use_begin(), UE = In->use_end();
407 UI != UE; ++UI) {
408 BasicBlock *UserBB = cast<Instruction>(UI)->getParent();
409 if (UserBB != OrigHeader && L->contains(UserBB))
410 return true;
413 return false;
416 /// Find Replacement information for instruction. Return NULL if it is
417 /// not available.
418 const RenameData *LoopRotate::findReplacementData(Instruction *In) {
420 // Since LoopHeaderInfo is small, linear walk is OK.
421 for (unsigned LHI = 0, LHI_E = LoopHeaderInfo.size(); LHI != LHI_E; ++LHI) {
422 const RenameData &ILoopHeaderInfo = LoopHeaderInfo[LHI];
423 if (ILoopHeaderInfo.Original == In)
424 return &ILoopHeaderInfo;
426 return NULL;
429 /// After loop rotation, loop pre-header has multiple sucessors.
430 /// Insert one forwarding basic block to ensure that loop pre-header
431 /// has only one successor.
432 void LoopRotate::preserveCanonicalLoopForm(LPPassManager &LPM) {
434 // Right now original pre-header has two successors, new header and
435 // exit block. Insert new block between original pre-header and
436 // new header such that loop's new pre-header has only one successor.
437 BasicBlock *NewPreHeader = BasicBlock::Create(OrigHeader->getContext(),
438 "bb.nph",
439 OrigHeader->getParent(),
440 NewHeader);
441 LoopInfo &LI = LPM.getAnalysis<LoopInfo>();
442 if (Loop *PL = LI.getLoopFor(OrigPreHeader))
443 PL->addBasicBlockToLoop(NewPreHeader, LI.getBase());
444 BranchInst::Create(NewHeader, NewPreHeader);
446 BranchInst *OrigPH_BI = cast<BranchInst>(OrigPreHeader->getTerminator());
447 if (OrigPH_BI->getSuccessor(0) == NewHeader)
448 OrigPH_BI->setSuccessor(0, NewPreHeader);
449 else {
450 assert(OrigPH_BI->getSuccessor(1) == NewHeader &&
451 "Unexpected original pre-header terminator");
452 OrigPH_BI->setSuccessor(1, NewPreHeader);
455 PHINode *PN;
456 for (BasicBlock::iterator I = NewHeader->begin();
457 (PN = dyn_cast<PHINode>(I)); ++I) {
458 int index = PN->getBasicBlockIndex(OrigPreHeader);
459 assert(index != -1 && "Expected incoming value from Original PreHeader");
460 PN->setIncomingBlock(index, NewPreHeader);
461 assert(PN->getBasicBlockIndex(OrigPreHeader) == -1 &&
462 "Expected only one incoming value from Original PreHeader");
465 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
466 DT->addNewBlock(NewPreHeader, OrigPreHeader);
467 DT->changeImmediateDominator(L->getHeader(), NewPreHeader);
468 DT->changeImmediateDominator(Exit, OrigPreHeader);
469 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
470 BI != BE; ++BI) {
471 BasicBlock *B = *BI;
472 if (L->getHeader() != B) {
473 DomTreeNode *Node = DT->getNode(B);
474 if (Node && Node->getBlock() == OrigHeader)
475 DT->changeImmediateDominator(*BI, L->getHeader());
478 DT->changeImmediateDominator(OrigHeader, OrigLatch);
481 if (DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>()) {
482 // New Preheader's dominance frontier is Exit block.
483 DominanceFrontier::DomSetType NewPHSet;
484 NewPHSet.insert(Exit);
485 DF->addBasicBlock(NewPreHeader, NewPHSet);
487 // New Header's dominance frontier now includes itself and Exit block
488 DominanceFrontier::iterator HeadI = DF->find(L->getHeader());
489 if (HeadI != DF->end()) {
490 DominanceFrontier::DomSetType & HeaderSet = HeadI->second;
491 HeaderSet.clear();
492 HeaderSet.insert(L->getHeader());
493 HeaderSet.insert(Exit);
494 } else {
495 DominanceFrontier::DomSetType HeaderSet;
496 HeaderSet.insert(L->getHeader());
497 HeaderSet.insert(Exit);
498 DF->addBasicBlock(L->getHeader(), HeaderSet);
501 // Original header (new Loop Latch)'s dominance frontier is Exit.
502 DominanceFrontier::iterator LatchI = DF->find(L->getLoopLatch());
503 if (LatchI != DF->end()) {
504 DominanceFrontier::DomSetType &LatchSet = LatchI->second;
505 LatchSet = LatchI->second;
506 LatchSet.clear();
507 LatchSet.insert(Exit);
508 } else {
509 DominanceFrontier::DomSetType LatchSet;
510 LatchSet.insert(Exit);
511 DF->addBasicBlock(L->getHeader(), LatchSet);
514 // If a loop block dominates new loop latch then add to its frontiers
515 // new header and Exit and remove new latch (which is equal to original
516 // header).
517 BasicBlock *NewLatch = L->getLoopLatch();
519 assert(NewLatch == OrigHeader && "NewLatch is inequal to OrigHeader");
521 if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
522 for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
523 BI != BE; ++BI) {
524 BasicBlock *B = *BI;
525 if (DT->dominates(B, NewLatch)) {
526 DominanceFrontier::iterator BDFI = DF->find(B);
527 if (BDFI != DF->end()) {
528 DominanceFrontier::DomSetType &BSet = BDFI->second;
529 BSet.erase(NewLatch);
530 BSet.insert(L->getHeader());
531 BSet.insert(Exit);
532 } else {
533 DominanceFrontier::DomSetType BSet;
534 BSet.insert(L->getHeader());
535 BSet.insert(Exit);
536 DF->addBasicBlock(B, BSet);
543 // Preserve canonical loop form, which means Exit block should
544 // have only one predecessor.
545 SplitEdge(L->getLoopLatch(), Exit, this);
547 assert(NewHeader && L->getHeader() == NewHeader &&
548 "Invalid loop header after loop rotation");
549 assert(NewPreHeader && L->getLoopPreheader() == NewPreHeader &&
550 "Invalid loop preheader after loop rotation");
551 assert(L->getLoopLatch() &&
552 "Invalid loop latch after loop rotation");