Minor code simplification.
[oota-llvm.git] / lib / Transforms / Scalar / LoopRotation.cpp
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.
11 //
12 //===----------------------------------------------------------------------===//
13
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;
29
30 #define MAX_HEADER_SIZE 16
31
32 STATISTIC(NumRotated, "Number of loops rotated");
33 namespace {
34
35   class VISIBILITY_HIDDEN 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
43   };
44   
45   class VISIBILITY_HIDDEN LoopRotate : public LoopPass {
46
47   public:
48     static char ID; // Pass ID, replacement for typeid
49     LoopRotate() : LoopPass(&ID) {}
50
51     // Rotate Loop L as many times as possible. Return true if
52     // loop is rotated at least once.
53     bool runOnLoop(Loop *L, LPPassManager &LPM);
54
55     // LCSSA form makes instruction renaming easier.
56     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57       AU.addRequiredID(LoopSimplifyID);
58       AU.addPreservedID(LoopSimplifyID);
59       AU.addRequiredID(LCSSAID);
60       AU.addPreservedID(LCSSAID);
61       AU.addPreserved<ScalarEvolution>();
62       AU.addPreserved<LoopInfo>();
63       AU.addPreserved<DominatorTree>();
64       AU.addPreserved<DominanceFrontier>();
65     }
66
67     // Helper functions
68
69     /// Do actual work
70     bool rotateLoop(Loop *L, LPPassManager &LPM);
71     
72     /// Initialize local data
73     void initialize();
74
75     /// Make sure all Exit block PHINodes have required incoming values.
76     /// If incoming value is constant or defined outside the loop then
77     /// PHINode may not have an entry for original pre-header. 
78     void  updateExitBlock();
79
80     /// Return true if this instruction is used outside original header.
81     bool usedOutsideOriginalHeader(Instruction *In);
82
83     /// Find Replacement information for instruction. Return NULL if it is
84     /// not available.
85     const RenameData *findReplacementData(Instruction *I);
86
87     /// After loop rotation, loop pre-header has multiple sucessors.
88     /// Insert one forwarding basic block to ensure that loop pre-header
89     /// has only one successor.
90     void preserveCanonicalLoopForm(LPPassManager &LPM);
91
92   private:
93
94     Loop *L;
95     BasicBlock *OrigHeader;
96     BasicBlock *OrigPreHeader;
97     BasicBlock *OrigLatch;
98     BasicBlock *NewHeader;
99     BasicBlock *Exit;
100     LPPassManager *LPM_Ptr;
101     SmallVector<RenameData, MAX_HEADER_SIZE> LoopHeaderInfo;
102   };
103 }
104   
105 char LoopRotate::ID = 0;
106 static RegisterPass<LoopRotate> X("loop-rotate", "Rotate Loops");
107
108 Pass *llvm::createLoopRotatePass() { return new LoopRotate(); }
109
110 /// Rotate Loop L as many times as possible. Return true if
111 /// the loop is rotated at least once.
112 bool LoopRotate::runOnLoop(Loop *Lp, LPPassManager &LPM) {
113
114   bool RotatedOneLoop = false;
115   initialize();
116   LPM_Ptr = &LPM;
117
118   // One loop can be rotated multiple times.
119   while (rotateLoop(Lp,LPM)) {
120     RotatedOneLoop = true;
121     initialize();
122   }
123
124   return RotatedOneLoop;
125 }
126
127 /// Rotate loop LP. Return true if the loop is rotated.
128 bool LoopRotate::rotateLoop(Loop *Lp, LPPassManager &LPM) {
129   L = Lp;
130
131   OrigHeader =  L->getHeader();
132   OrigPreHeader = L->getLoopPreheader();
133   OrigLatch = L->getLoopLatch();
134
135   // If the loop has only one block then there is not much to rotate.
136   if (L->getBlocks().size() == 1)
137     return false;
138
139   assert(OrigHeader && OrigLatch && OrigPreHeader &&
140          "Loop is not in canonical form");
141
142   // If the loop header is not one of the loop exiting blocks then
143   // either this loop is already rotated or it is not
144   // suitable for loop rotation transformations.
145   if (!L->isLoopExit(OrigHeader))
146     return false;
147
148   BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
149   if (!BI)
150     return false;
151   assert(BI->isConditional() && "Branch Instruction is not conditional");
152
153   // Updating PHInodes in loops with multiple exits adds complexity. 
154   // Keep it simple, and restrict loop rotation to loops with one exit only.
155   // In future, lift this restriction and support for multiple exits if
156   // required.
157   SmallVector<BasicBlock*, 8> ExitBlocks;
158   L->getExitBlocks(ExitBlocks);
159   if (ExitBlocks.size() > 1)
160     return false;
161
162   // Check size of original header and reject
163   // loop if it is very big.
164   unsigned Size = 0;
165   
166   // FIXME: Use common api to estimate size.
167   for (BasicBlock::const_iterator OI = OrigHeader->begin(), 
168          OE = OrigHeader->end(); OI != OE; ++OI) {
169       if (isa<PHINode>(OI)) 
170         continue;           // PHI nodes don't count.
171       if (isa<DbgInfoIntrinsic>(OI))
172         continue;  // Debug intrinsics don't count as size.
173       Size++;
174   }
175
176   if (Size > MAX_HEADER_SIZE)
177     return false;
178
179   // Now, this loop is suitable for rotation.
180
181   // Find new Loop header. NewHeader is a Header's one and only successor
182   // that is inside loop.  Header's other successor is outside the
183   // loop.  Otherwise loop is not suitable for rotation.
184   Exit = BI->getSuccessor(0);
185   NewHeader = BI->getSuccessor(1);
186   if (L->contains(Exit))
187     std::swap(Exit, NewHeader);
188   assert(NewHeader && "Unable to determine new loop header");
189   assert(L->contains(NewHeader) && !L->contains(Exit) && 
190          "Unable to determine loop header and exit blocks");
191   
192   // This code assumes that the new header has exactly one predecessor.
193   // Remove any single-entry PHI nodes in it.
194   assert(NewHeader->getSinglePredecessor() &&
195          "New header doesn't have one pred!");
196   FoldSingleEntryPHINodes(NewHeader);
197
198   // Copy PHI nodes and other instructions from the original header
199   // into the original pre-header. Unlike the original header, the original
200   // pre-header is not a member of the loop.
201   //
202   // The new loop header is the one and only successor of original header that
203   // is inside the loop. All other original header successors are outside 
204   // the loop. Copy PHI Nodes from the original header into the new loop header.
205   // Add second incoming value, from original loop pre-header into these phi 
206   // nodes. If a value defined in original header is used outside original 
207   // header then new loop header will need new phi nodes with two incoming 
208   // values, one definition from original header and second definition is 
209   // from original loop pre-header.
210
211   // Remove terminator from Original pre-header. Original pre-header will
212   // receive a clone of original header terminator as a new terminator.
213   OrigPreHeader->getInstList().pop_back();
214   BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
215   PHINode *PN = 0;
216   for (; (PN = dyn_cast<PHINode>(I)); ++I) {
217     // PHI nodes are not copied into original pre-header. Instead their values
218     // are directly propagated.
219     Value *NPV = PN->getIncomingValueForBlock(OrigPreHeader);
220
221     // Create a new PHI node with two incoming values for NewHeader.
222     // One incoming value is from OrigLatch (through OrigHeader) and the
223     // second incoming value is from original pre-header.
224     PHINode *NH = PHINode::Create(PN->getType(), PN->getName(),
225                                   NewHeader->begin());
226     NH->addIncoming(PN->getIncomingValueForBlock(OrigLatch), OrigHeader);
227     NH->addIncoming(NPV, OrigPreHeader);
228     
229     // "In" can be replaced by NH at various places.
230     LoopHeaderInfo.push_back(RenameData(PN, NPV, NH));
231   }
232
233   // Now, handle non-phi instructions.
234   for (; I != E; ++I) {
235     Instruction *In = I;
236     assert(!isa<PHINode>(In) && "PHINode is not expected here");
237     
238     // This is not a PHI instruction. Insert its clone into original pre-header.
239     // If this instruction is using a value from same basic block then
240     // update it to use value from cloned instruction.
241     Instruction *C = In->clone();
242     C->setName(In->getName());
243     OrigPreHeader->getInstList().push_back(C);
244
245     for (unsigned opi = 0, e = In->getNumOperands(); opi != e; ++opi) {
246       Instruction *OpInsn = dyn_cast<Instruction>(In->getOperand(opi));
247       if (!OpInsn) continue;  // Ignore non-instruction values.
248       if (const RenameData *D = findReplacementData(OpInsn))
249         C->setOperand(opi, D->PreHeader);
250     }
251
252     // If this instruction is used outside this basic block then
253     // create new PHINode for this instruction.
254     Instruction *NewHeaderReplacement = NULL;
255     if (usedOutsideOriginalHeader(In)) {
256       PHINode *PN = PHINode::Create(In->getType(), In->getName(),
257                                     NewHeader->begin());
258       PN->addIncoming(In, OrigHeader);
259       PN->addIncoming(C, OrigPreHeader);
260       NewHeaderReplacement = PN;
261     }
262     LoopHeaderInfo.push_back(RenameData(In, C, NewHeaderReplacement));
263   }
264
265   // Rename uses of original header instructions to reflect their new
266   // definitions (either from original pre-header node or from newly created
267   // new header PHINodes.
268   //
269   // Original header instructions are used in
270   // 1) Original header:
271   //
272   //    If instruction is used in non-phi instructions then it is using
273   //    defintion from original heder iteself. Do not replace this use
274   //    with definition from new header or original pre-header.
275   //
276   //    If instruction is used in phi node then it is an incoming 
277   //    value. Rename its use to reflect new definition from new-preheader
278   //    or new header.
279   //
280   // 2) Inside loop but not in original header
281   //
282   //    Replace this use to reflect definition from new header.
283   for (unsigned LHI = 0, LHI_E = LoopHeaderInfo.size(); LHI != LHI_E; ++LHI) {
284     const RenameData &ILoopHeaderInfo = LoopHeaderInfo[LHI];
285
286     if (!ILoopHeaderInfo.Header)
287       continue;
288
289     Instruction *OldPhi = ILoopHeaderInfo.Original;
290     Instruction *NewPhi = ILoopHeaderInfo.Header;
291
292     // Before replacing uses, collect them first, so that iterator is
293     // not invalidated.
294     SmallVector<Instruction *, 16> AllUses;
295     for (Value::use_iterator UI = OldPhi->use_begin(), UE = OldPhi->use_end();
296          UI != UE; ++UI)
297       AllUses.push_back(cast<Instruction>(UI));
298
299     for (SmallVector<Instruction *, 16>::iterator UI = AllUses.begin(), 
300            UE = AllUses.end(); UI != UE; ++UI) {
301       Instruction *U = *UI;
302       BasicBlock *Parent = U->getParent();
303
304       // Used inside original header
305       if (Parent == OrigHeader) {
306         // Do not rename uses inside original header non-phi instructions.
307         PHINode *PU = dyn_cast<PHINode>(U);
308         if (!PU)
309           continue;
310
311         // Do not rename uses inside original header phi nodes, if the
312         // incoming value is for new header.
313         if (PU->getBasicBlockIndex(NewHeader) != -1
314             && PU->getIncomingValueForBlock(NewHeader) == U)
315           continue;
316         
317        U->replaceUsesOfWith(OldPhi, NewPhi);
318        continue;
319       }
320
321       // Used inside loop, but not in original header.
322       if (L->contains(U->getParent())) {
323         if (U != NewPhi)
324           U->replaceUsesOfWith(OldPhi, NewPhi);
325         continue;
326       }
327       
328       // Used inside Exit Block. Since we are in LCSSA form, U must be PHINode.
329       if (U->getParent() == Exit) {
330         assert(isa<PHINode>(U) && "Use in Exit Block that is not PHINode");
331         
332         PHINode *UPhi = cast<PHINode>(U);
333         // UPhi already has one incoming argument from original header. 
334         // Add second incoming argument from new Pre header.
335         UPhi->addIncoming(ILoopHeaderInfo.PreHeader, OrigPreHeader);
336       } else {
337         // Used outside Exit block. Create a new PHI node in the exit block
338         // to receive the value from the new header and pre-header.
339         PHINode *PN = PHINode::Create(U->getType(), U->getName(),
340                                       Exit->begin());
341         PN->addIncoming(ILoopHeaderInfo.PreHeader, OrigPreHeader);
342         PN->addIncoming(OldPhi, OrigHeader);
343         U->replaceUsesOfWith(OldPhi, PN);
344       }
345     }
346   }
347   
348   /// Make sure all Exit block PHINodes have required incoming values.
349   updateExitBlock();
350
351   // Update CFG
352
353   // Removing incoming branch from loop preheader to original header.
354   // Now original header is inside the loop.
355   for (BasicBlock::iterator I = OrigHeader->begin(), E = OrigHeader->end();
356        I != E; ++I)
357     if (PHINode *PN = dyn_cast<PHINode>(I))
358       PN->removeIncomingValue(OrigPreHeader);
359
360   // Make NewHeader as the new header for the loop.
361   L->moveToHeader(NewHeader);
362
363   preserveCanonicalLoopForm(LPM);
364
365   NumRotated++;
366   return true;
367 }
368
369 /// Make sure all Exit block PHINodes have required incoming values.
370 /// If an incoming value is constant or defined outside the loop then
371 /// PHINode may not have an entry for the original pre-header.
372 void LoopRotate::updateExitBlock() {
373
374   for (BasicBlock::iterator I = Exit->begin();
375        PHINode *PN = dyn_cast<PHINode>(I); ++I) {
376
377     // There is already one incoming value from original pre-header block.
378     if (PN->getBasicBlockIndex(OrigPreHeader) != -1)
379       continue;
380
381     const RenameData *ILoopHeaderInfo;
382     Value *V = PN->getIncomingValueForBlock(OrigHeader);
383     if (isa<Instruction>(V) &&
384         (ILoopHeaderInfo = findReplacementData(cast<Instruction>(V)))) {
385       assert(ILoopHeaderInfo->PreHeader && "Missing New Preheader Instruction");
386       PN->addIncoming(ILoopHeaderInfo->PreHeader, OrigPreHeader);
387     } else {
388       PN->addIncoming(V, OrigPreHeader);
389     }
390   }
391 }
392
393 /// Initialize local data
394 void LoopRotate::initialize() {
395   L = NULL;
396   OrigHeader = NULL;
397   OrigPreHeader = NULL;
398   NewHeader = NULL;
399   Exit = NULL;
400
401   LoopHeaderInfo.clear();
402 }
403
404 /// Return true if this instruction is used by any instructions in the loop that
405 /// aren't in original header.
406 bool LoopRotate::usedOutsideOriginalHeader(Instruction *In) {
407   for (Value::use_iterator UI = In->use_begin(), UE = In->use_end();
408        UI != UE; ++UI) {
409     BasicBlock *UserBB = cast<Instruction>(UI)->getParent();
410     if (UserBB != OrigHeader && L->contains(UserBB))
411       return true;
412   }
413
414   return false;
415 }
416
417 /// Find Replacement information for instruction. Return NULL if it is
418 /// not available.
419 const RenameData *LoopRotate::findReplacementData(Instruction *In) {
420
421   // Since LoopHeaderInfo is small, linear walk is OK.
422   for (unsigned LHI = 0, LHI_E = LoopHeaderInfo.size(); LHI != LHI_E; ++LHI) {
423     const RenameData &ILoopHeaderInfo = LoopHeaderInfo[LHI];
424     if (ILoopHeaderInfo.Original == In)
425       return &ILoopHeaderInfo;
426   }
427   return NULL;
428 }
429
430 /// After loop rotation, loop pre-header has multiple sucessors.
431 /// Insert one forwarding basic block to ensure that loop pre-header
432 /// has only one successor.
433 void LoopRotate::preserveCanonicalLoopForm(LPPassManager &LPM) {
434
435   // Right now original pre-header has two successors, new header and
436   // exit block. Insert new block between original pre-header and
437   // new header such that loop's new pre-header has only one successor.
438   BasicBlock *NewPreHeader = BasicBlock::Create("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);
445   
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);
453   }
454   
455   for (BasicBlock::iterator I = NewHeader->begin(), E = NewHeader->end();
456        I != E; ++I) {
457     PHINode *PN = dyn_cast<PHINode>(I);
458     if (!PN)
459       break;
460
461     int index = PN->getBasicBlockIndex(OrigPreHeader);
462     assert(index != -1 && "Expected incoming value from Original PreHeader");
463     PN->setIncomingBlock(index, NewPreHeader);
464     assert(PN->getBasicBlockIndex(OrigPreHeader) == -1 && 
465            "Expected only one incoming value from Original PreHeader");
466   }
467
468   if (DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>()) {
469     DT->addNewBlock(NewPreHeader, OrigPreHeader);
470     DT->changeImmediateDominator(L->getHeader(), NewPreHeader);
471     DT->changeImmediateDominator(Exit, OrigPreHeader);
472     for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
473          BI != BE; ++BI) {
474       BasicBlock *B = *BI;
475       if (L->getHeader() != B) {
476         DomTreeNode *Node = DT->getNode(B);
477         if (Node && Node->getBlock() == OrigHeader)
478           DT->changeImmediateDominator(*BI, L->getHeader());
479       }
480     }
481     DT->changeImmediateDominator(OrigHeader, OrigLatch);
482   }
483
484   if (DominanceFrontier *DF = getAnalysisIfAvailable<DominanceFrontier>()) {
485     // New Preheader's dominance frontier is Exit block.
486     DominanceFrontier::DomSetType NewPHSet;
487     NewPHSet.insert(Exit);
488     DF->addBasicBlock(NewPreHeader, NewPHSet);
489
490     // New Header's dominance frontier now includes itself and Exit block
491     DominanceFrontier::iterator HeadI = DF->find(L->getHeader());
492     if (HeadI != DF->end()) {
493       DominanceFrontier::DomSetType & HeaderSet = HeadI->second;
494       HeaderSet.clear();
495       HeaderSet.insert(L->getHeader());
496       HeaderSet.insert(Exit);
497     } else {
498       DominanceFrontier::DomSetType HeaderSet;
499       HeaderSet.insert(L->getHeader());
500       HeaderSet.insert(Exit);
501       DF->addBasicBlock(L->getHeader(), HeaderSet);
502     }
503
504     // Original header (new Loop Latch)'s dominance frontier is Exit.
505     DominanceFrontier::iterator LatchI = DF->find(L->getLoopLatch());
506     if (LatchI != DF->end()) {
507       DominanceFrontier::DomSetType &LatchSet = LatchI->second;
508       LatchSet = LatchI->second;
509       LatchSet.clear();
510       LatchSet.insert(Exit);
511     } else {
512       DominanceFrontier::DomSetType LatchSet;
513       LatchSet.insert(Exit);
514       DF->addBasicBlock(L->getHeader(), LatchSet);
515     }
516
517     // If a loop block dominates new loop latch then its frontier is
518     // new header and Exit.
519     BasicBlock *NewLatch = L->getLoopLatch();
520     DominatorTree *DT = getAnalysisIfAvailable<DominatorTree>();
521     for (Loop::block_iterator BI = L->block_begin(), BE = L->block_end();
522          BI != BE; ++BI) {
523       BasicBlock *B = *BI;
524       if (DT->dominates(B, NewLatch)) {
525         DominanceFrontier::iterator BDFI = DF->find(B);
526         if (BDFI != DF->end()) {
527           DominanceFrontier::DomSetType &BSet = BDFI->second;
528           BSet = BDFI->second;
529           BSet.clear();
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);
537         }
538       }
539     }
540   }
541
542   // Preserve canonical loop form, which means Exit block should
543   // have only one predecessor.
544   BasicBlock *NExit = SplitEdge(L->getLoopLatch(), Exit, this);
545
546   // Preserve LCSSA.
547   BasicBlock::iterator I = Exit->begin(), E = Exit->end();
548   PHINode *PN = NULL;
549   for (; (PN = dyn_cast<PHINode>(I)); ++I) {
550     unsigned N = PN->getNumIncomingValues();
551     for (unsigned index = 0; index < N; ++index)
552       if (PN->getIncomingBlock(index) == NExit) {
553         PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName(),
554                                          NExit->begin());
555         NewPN->addIncoming(PN->getIncomingValue(index), L->getLoopLatch());
556         PN->setIncomingValue(index, NewPN);
557         PN->setIncomingBlock(index, NExit);
558         break;
559       }
560   }
561
562   assert(NewHeader && L->getHeader() == NewHeader &&
563          "Invalid loop header after loop rotation");
564   assert(NewPreHeader && L->getLoopPreheader() == NewPreHeader &&
565          "Invalid loop preheader after loop rotation");
566   assert(L->getLoopLatch() &&
567          "Invalid loop latch after loop rotation");
568 }