1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the LoopInfo class that is used to identify natural loops
11 // and determine the loop depth of various nodes of the CFG. Note that natural
12 // loops may actually be several loops that share the same header node.
14 // This analysis calculates the nesting structure of loops in a function. For
15 // each natural loop identified, this analysis identifies natural loops
16 // contained entirely within the loop and the basic blocks the make up the loop.
18 // It can calculate on the fly various bits of information, for example:
20 // * whether there is a preheader for the loop
21 // * the number of back edges to the header
22 // * whether or not a particular block branches out of the loop
23 // * the successor blocks of the loop
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
31 #define LLVM_ANALYSIS_LOOP_INFO_H
33 #include "llvm/Pass.h"
34 #include "llvm/Constants.h"
35 #include "llvm/Instructions.h"
36 #include "llvm/ADT/DepthFirstIterator.h"
37 #include "llvm/ADT/GraphTraits.h"
38 #include "llvm/ADT/SmallPtrSet.h"
39 #include "llvm/ADT/SmallVector.h"
40 #include "llvm/Analysis/Dominators.h"
41 #include "llvm/Support/CFG.h"
42 #include "llvm/Support/Streams.h"
47 static void RemoveFromVector(std::vector<T*> &V, T *N) {
48 typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
49 assert(I != V.end() && "N is not in this list!");
59 template<class N> class LoopInfoBase;
61 //===----------------------------------------------------------------------===//
62 /// LoopBase class - Instances of this class are used to represent loops that are
63 /// detected in the flow graph
65 template<class BlockT>
67 LoopBase<BlockT> *ParentLoop;
68 std::vector<LoopBase<BlockT>*> SubLoops; // Loops contained entirely within this one
69 std::vector<BlockT*> Blocks; // First entry is the header node
71 LoopBase(const LoopBase<BlockT> &); // DO NOT IMPLEMENT
72 const LoopBase<BlockT> &operator=(const LoopBase<BlockT> &); // DO NOT IMPLEMENT
74 /// Loop ctor - This creates an empty loop.
75 LoopBase() : ParentLoop(0) {}
77 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
81 unsigned getLoopDepth() const {
83 for (const LoopBase<BlockT> *CurLoop = this; CurLoop;
84 CurLoop = CurLoop->ParentLoop)
88 BlockT *getHeader() const { return Blocks.front(); }
89 LoopBase<BlockT> *getParentLoop() const { return ParentLoop; }
91 /// contains - Return true of the specified basic block is in this loop
93 bool contains(const BlockT *BB) const {
94 return std::find(Blocks.begin(), Blocks.end(), BB) != Blocks.end();
97 /// iterator/begin/end - Return the loops contained entirely within this loop.
99 const std::vector<LoopBase<BlockT>*> &getSubLoops() const { return SubLoops; }
100 typedef typename std::vector<LoopBase<BlockT>*>::const_iterator iterator;
101 iterator begin() const { return SubLoops.begin(); }
102 iterator end() const { return SubLoops.end(); }
103 bool empty() const { return SubLoops.empty(); }
105 /// getBlocks - Get a list of the basic blocks which make up this loop.
107 const std::vector<BlockT*> &getBlocks() const { return Blocks; }
108 typedef typename std::vector<BlockT*>::const_iterator block_iterator;
109 block_iterator block_begin() const { return Blocks.begin(); }
110 block_iterator block_end() const { return Blocks.end(); }
112 /// isLoopExit - True if terminator in the block can branch to another block
113 /// that is outside of the current loop.
115 bool isLoopExit(const BlockT *BB) const {
116 for (succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
124 /// getNumBackEdges - Calculate the number of back edges to the loop header
126 unsigned getNumBackEdges() const {
127 unsigned NumBackEdges = 0;
128 BlockT *H = getHeader();
130 for (pred_iterator I = pred_begin(H), E = pred_end(H); I != E; ++I)
137 /// isLoopInvariant - Return true if the specified value is loop invariant
139 bool isLoopInvariant(Value *V) const {
140 if (Instruction *I = dyn_cast<Instruction>(V))
141 return !contains(I->getParent());
142 return true; // All non-instructions are loop invariant
145 //===--------------------------------------------------------------------===//
146 // APIs for simple analysis of the loop.
148 // Note that all of these methods can fail on general loops (ie, there may not
149 // be a preheader, etc). For best success, the loop simplification and
150 // induction variable canonicalization pass should be used to normalize loops
151 // for easy analysis. These methods assume canonical loops.
153 /// getExitingBlocks - Return all blocks inside the loop that have successors
154 /// outside of the loop. These are the blocks _inside of the current loop_
155 /// which branch out. The returned list is always unique.
157 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
158 // Sort the blocks vector so that we can use binary search to do quick
160 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
161 std::sort(LoopBBs.begin(), LoopBBs.end());
163 for (typename std::vector<BlockT*>::const_iterator BI = Blocks.begin(),
164 BE = Blocks.end(); BI != BE; ++BI)
165 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I)
166 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
167 // Not in current loop? It must be an exit block.
168 ExitingBlocks.push_back(*BI);
173 /// getExitBlocks - Return all of the successor blocks of this loop. These
174 /// are the blocks _outside of the current loop_ which are branched to.
176 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
177 // Sort the blocks vector so that we can use binary search to do quick
179 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
180 std::sort(LoopBBs.begin(), LoopBBs.end());
182 for (typename std::vector<BlockT*>::const_iterator BI = Blocks.begin(),
183 BE = Blocks.end(); BI != BE; ++BI)
184 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I)
185 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
186 // Not in current loop? It must be an exit block.
187 ExitBlocks.push_back(*I);
190 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
191 /// These are the blocks _outside of the current loop_ which are branched to.
192 /// This assumes that loop is in canonical form.
194 void getUniqueExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
195 // Sort the blocks vector so that we can use binary search to do quick
197 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
198 std::sort(LoopBBs.begin(), LoopBBs.end());
200 std::vector<BlockT*> switchExitBlocks;
202 for (typename std::vector<BlockT*>::const_iterator BI = Blocks.begin(),
203 BE = Blocks.end(); BI != BE; ++BI) {
205 BlockT *current = *BI;
206 switchExitBlocks.clear();
208 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) {
209 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
210 // If block is inside the loop then it is not a exit block.
213 pred_iterator PI = pred_begin(*I);
214 BlockT *firstPred = *PI;
216 // If current basic block is this exit block's first predecessor
217 // then only insert exit block in to the output ExitBlocks vector.
218 // This ensures that same exit block is not inserted twice into
219 // ExitBlocks vector.
220 if (current != firstPred)
223 // If a terminator has more then two successors, for example SwitchInst,
224 // then it is possible that there are multiple edges from current block
225 // to one exit block.
226 if (current->getTerminator()->getNumSuccessors() <= 2) {
227 ExitBlocks.push_back(*I);
231 // In case of multiple edges from current block to exit block, collect
232 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
234 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
235 == switchExitBlocks.end()) {
236 switchExitBlocks.push_back(*I);
237 ExitBlocks.push_back(*I);
243 /// getLoopPreheader - If there is a preheader for this loop, return it. A
244 /// loop has a preheader if there is only one edge to the header of the loop
245 /// from outside of the loop. If this is the case, the block branching to the
246 /// header of the loop is the preheader node.
248 /// This method returns null if there is no preheader for the loop.
250 BlockT *getLoopPreheader() const {
251 // Keep track of nodes outside the loop branching to the header...
254 // Loop over the predecessors of the header node...
255 BlockT *Header = getHeader();
256 for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
258 if (!contains(*PI)) { // If the block is not in the loop...
259 if (Out && Out != *PI)
260 return 0; // Multiple predecessors outside the loop
264 // Make sure there is only one exit out of the preheader.
265 assert(Out && "Header of loop has no predecessors from outside loop?");
266 succ_iterator SI = succ_begin(Out);
268 if (SI != succ_end(Out))
269 return 0; // Multiple exits from the block, must not be a preheader.
271 // If there is exactly one preheader, return it. If there was zero, then Out
276 /// getLoopLatch - If there is a latch block for this loop, return it. A
277 /// latch block is the canonical backedge for a loop. A loop header in normal
278 /// form has two edges into it: one from a preheader and one from a latch
280 BlockT *getLoopLatch() const {
281 BlockT *Header = getHeader();
282 pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
283 if (PI == PE) return 0; // no preds?
289 if (PI == PE) return 0; // only one pred?
292 if (Latch) return 0; // multiple backedges
296 if (PI != PE) return 0; // more than two preds
301 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
302 /// induction variable: an integer recurrence that starts at 0 and increments
303 /// by one each time through the loop. If so, return the phi node that
304 /// corresponds to it.
306 PHINode *getCanonicalInductionVariable() const {
307 BlockT *H = getHeader();
309 BlockT *Incoming = 0, *Backedge = 0;
310 pred_iterator PI = pred_begin(H);
311 assert(PI != pred_end(H) && "Loop must have at least one backedge!");
313 if (PI == pred_end(H)) return 0; // dead loop
315 if (PI != pred_end(H)) return 0; // multiple backedges?
317 if (contains(Incoming)) {
318 if (contains(Backedge))
320 std::swap(Incoming, Backedge);
321 } else if (!contains(Backedge))
324 // Loop over all of the PHI nodes, looking for a canonical indvar.
325 for (typename BlockT::iterator I = H->begin(); isa<PHINode>(I); ++I) {
326 PHINode *PN = cast<PHINode>(I);
327 if (Instruction *Inc =
328 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
329 if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
330 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
331 if (CI->equalsInt(1))
337 /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds
338 /// the canonical induction variable value for the "next" iteration of the
339 /// loop. This always succeeds if getCanonicalInductionVariable succeeds.
341 Instruction *getCanonicalInductionVariableIncrement() const {
342 if (PHINode *PN = getCanonicalInductionVariable()) {
343 bool P1InLoop = contains(PN->getIncomingBlock(1));
344 return cast<Instruction>(PN->getIncomingValue(P1InLoop));
349 /// getTripCount - Return a loop-invariant LLVM value indicating the number of
350 /// times the loop will be executed. Note that this means that the backedge
351 /// of the loop executes N-1 times. If the trip-count cannot be determined,
352 /// this returns null.
354 Value *getTripCount() const {
355 // Canonical loops will end with a 'cmp ne I, V', where I is the incremented
356 // canonical induction variable and V is the trip count of the loop.
357 Instruction *Inc = getCanonicalInductionVariableIncrement();
358 if (Inc == 0) return 0;
359 PHINode *IV = cast<PHINode>(Inc->getOperand(0));
361 BlockT *BackedgeBlock =
362 IV->getIncomingBlock(contains(IV->getIncomingBlock(1)));
364 if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator()))
365 if (BI->isConditional()) {
366 if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
367 if (ICI->getOperand(0) == Inc)
368 if (BI->getSuccessor(0) == getHeader()) {
369 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
370 return ICI->getOperand(1);
371 } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) {
372 return ICI->getOperand(1);
380 /// isLCSSAForm - Return true if the Loop is in LCSSA form
381 bool isLCSSAForm() const {
382 // Sort the blocks vector so that we can use binary search to do quick
384 SmallPtrSet<BlockT*, 16> LoopBBs(block_begin(), block_end());
386 for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) {
388 for (typename BlockT::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
389 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
391 BlockT *UserBB = cast<Instruction>(*UI)->getParent();
392 if (PHINode *P = dyn_cast<PHINode>(*UI)) {
393 unsigned OperandNo = UI.getOperandNo();
394 UserBB = P->getIncomingBlock(OperandNo/2);
397 // Check the current block, as a fast-path. Most values are used in the
398 // same block they are defined in.
399 if (UserBB != BB && !LoopBBs.count(UserBB))
407 //===--------------------------------------------------------------------===//
408 // APIs for updating loop information after changing the CFG
411 /// addBasicBlockToLoop - This method is used by other analyses to update loop
412 /// information. NewBB is set to be a new member of the current loop.
413 /// Because of this, it is added as a member of all parent loops, and is added
414 /// to the specified LoopInfo object as being in the current basic block. It
415 /// is not valid to replace the loop header with this method.
417 void addBasicBlockToLoop(BlockT *NewBB, LoopInfo &LI);
419 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
420 /// the OldChild entry in our children list with NewChild, and updates the
421 /// parent pointer of OldChild to be null and the NewChild to be this loop.
422 /// This updates the loop depth of the new child.
423 void replaceChildLoopWith(LoopBase<BlockT> *OldChild,
424 LoopBase<BlockT> *NewChild) {
425 assert(OldChild->ParentLoop == this && "This loop is already broken!");
426 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
427 typename std::vector<LoopBase<BlockT>*>::iterator I =
428 std::find(SubLoops.begin(), SubLoops.end(), OldChild);
429 assert(I != SubLoops.end() && "OldChild not in loop!");
431 OldChild->ParentLoop = 0;
432 NewChild->ParentLoop = this;
435 /// addChildLoop - Add the specified loop to be a child of this loop. This
436 /// updates the loop depth of the new child.
438 void addChildLoop(LoopBase<BlockT> *NewChild) {
439 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
440 NewChild->ParentLoop = this;
441 SubLoops.push_back(NewChild);
444 /// removeChildLoop - This removes the specified child from being a subloop of
445 /// this loop. The loop is not deleted, as it will presumably be inserted
446 /// into another loop.
447 LoopBase<BlockT> *removeChildLoop(iterator I) {
448 assert(I != SubLoops.end() && "Cannot remove end iterator!");
449 LoopBase<BlockT> *Child = *I;
450 assert(Child->ParentLoop == this && "Child is not a child of this loop!");
451 SubLoops.erase(SubLoops.begin()+(I-begin()));
452 Child->ParentLoop = 0;
456 /// addBlockEntry - This adds a basic block directly to the basic block list.
457 /// This should only be used by transformations that create new loops. Other
458 /// transformations should use addBasicBlockToLoop.
459 void addBlockEntry(BlockT *BB) {
460 Blocks.push_back(BB);
463 /// moveToHeader - This method is used to move BB (which must be part of this
464 /// loop) to be the loop header of the loop (the block that dominates all
466 void moveToHeader(BlockT *BB) {
467 if (Blocks[0] == BB) return;
468 for (unsigned i = 0; ; ++i) {
469 assert(i != Blocks.size() && "Loop does not contain BB!");
470 if (Blocks[i] == BB) {
471 Blocks[i] = Blocks[0];
478 /// removeBlockFromLoop - This removes the specified basic block from the
479 /// current loop, updating the Blocks as appropriate. This does not update
480 /// the mapping in the LoopInfo class.
481 void removeBlockFromLoop(BlockT *BB) {
482 RemoveFromVector(Blocks, BB);
485 /// verifyLoop - Verify loop structure
486 void verifyLoop() const {
488 assert (getHeader() && "Loop header is missing");
489 assert (getLoopPreheader() && "Loop preheader is missing");
490 assert (getLoopLatch() && "Loop latch is missing");
491 for (typename std::vector<LoopBase<BlockT>*>::const_iterator I =
492 SubLoops.begin(), E = SubLoops.end(); I != E; ++I)
497 void print(std::ostream &OS, unsigned Depth = 0) const {
498 OS << std::string(Depth*2, ' ') << "Loop Containing: ";
500 for (unsigned i = 0; i < getBlocks().size(); ++i) {
502 WriteAsOperand(OS, getBlocks()[i], false);
506 for (iterator I = begin(), E = end(); I != E; ++I)
507 (*I)->print(OS, Depth+2);
510 void print(std::ostream *O, unsigned Depth = 0) const {
511 if (O) print(*O, Depth);
519 friend class LoopInfoBase<BlockT>;
520 LoopBase(BlockT *BB) : ParentLoop(0) {
521 Blocks.push_back(BB);
525 typedef LoopBase<BasicBlock> Loop;
528 //===----------------------------------------------------------------------===//
529 /// LoopInfo - This class builds and contains all of the top level loop
530 /// structures in the specified function.
533 template<class BlockT>
535 // BBMap - Mapping of basic blocks to the inner most loop they occur in
536 std::map<BlockT*, Loop*> BBMap;
537 std::vector<LoopBase<BlockT>*> TopLevelLoops;
538 friend class LoopBase<BlockT>;
542 ~LoopInfoBase() { releaseMemory(); }
544 void releaseMemory() {
545 for (typename std::vector<LoopBase<BlockT>* >::iterator I =
546 TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
547 delete *I; // Delete all of the loops...
549 BBMap.clear(); // Reset internal state of analysis
550 TopLevelLoops.clear();
553 /// iterator/begin/end - The interface to the top-level loops in the current
556 typedef typename std::vector<LoopBase<BlockT>*>::const_iterator iterator;
557 iterator begin() const { return TopLevelLoops.begin(); }
558 iterator end() const { return TopLevelLoops.end(); }
560 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
561 /// block is in no loop (for example the entry node), null is returned.
563 LoopBase<BlockT> *getLoopFor(const BlockT *BB) const {
564 typename std::map<BlockT *, LoopBase<BlockT>*>::const_iterator I=
565 BBMap.find(const_cast<BasicBlock*>(BB));
566 return I != BBMap.end() ? I->second : 0;
569 /// operator[] - same as getLoopFor...
571 const LoopBase<BlockT> *operator[](const BlockT *BB) const {
572 return getLoopFor(BB);
575 /// getLoopDepth - Return the loop nesting level of the specified block...
577 unsigned getLoopDepth(const BlockT *BB) const {
578 const Loop *L = getLoopFor(BB);
579 return L ? L->getLoopDepth() : 0;
582 // isLoopHeader - True if the block is a loop header node
583 bool isLoopHeader(BlockT *BB) const {
584 const Loop *L = getLoopFor(BB);
585 return L && L->getHeader() == BB;
588 /// removeLoop - This removes the specified top-level loop from this loop info
589 /// object. The loop is not deleted, as it will presumably be inserted into
591 LoopBase<BlockT> *removeLoop(iterator I) {
592 assert(I != end() && "Cannot remove end iterator!");
593 LoopBase<BlockT> *L = *I;
594 assert(L->getParentLoop() == 0 && "Not a top-level loop!");
595 TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
599 /// changeLoopFor - Change the top-level loop that contains BB to the
600 /// specified loop. This should be used by transformations that restructure
601 /// the loop hierarchy tree.
602 void changeLoopFor(BlockT *BB, LoopBase<BlockT> *L) {
603 LoopBase<BlockT> *&OldLoop = BBMap[BB];
604 assert(OldLoop && "Block not in a loop yet!");
608 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
609 /// list with the indicated loop.
610 void changeTopLevelLoop(LoopBase<BlockT> *OldLoop,
611 LoopBase<BlockT> *NewLoop) {
612 typename std::vector<LoopBase<BlockT>*>::iterator I =
613 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
614 assert(I != TopLevelLoops.end() && "Old loop not at top level!");
616 assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
617 "Loops already embedded into a subloop!");
620 /// addTopLevelLoop - This adds the specified loop to the collection of
622 void addTopLevelLoop(LoopBase<BlockT> *New) {
623 assert(New->getParentLoop() == 0 && "Loop already in subloop!");
624 TopLevelLoops.push_back(New);
627 /// removeBlock - This method completely removes BB from all data structures,
628 /// including all of the Loop objects it is nested in and our mapping from
629 /// BasicBlocks to loops.
630 void removeBlock(BlockT *BB) {
631 typename std::map<BlockT *, LoopBase<BlockT>*>::iterator I = BBMap.find(BB);
632 if (I != BBMap.end()) {
633 for (Loop *L = I->second; L; L = L->getParentLoop())
634 L->removeBlockFromLoop(BB);
642 static bool isNotAlreadyContainedIn(Loop *SubLoop, Loop *ParentLoop) {
643 if (SubLoop == 0) return true;
644 if (SubLoop == ParentLoop) return false;
645 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
648 void Calculate(DominatorTree &DT) {
649 BlockT *RootNode = DT.getRootNode()->getBlock();
651 for (df_iterator<BlockT*> NI = df_begin(RootNode),
652 NE = df_end(RootNode); NI != NE; ++NI)
653 if (LoopBase<BlockT> *L = ConsiderForLoop(*NI, DT))
654 TopLevelLoops.push_back(L);
657 LoopBase<BlockT> *ConsiderForLoop(BlockT *BB, DominatorTree &DT) {
658 if (BBMap.find(BB) != BBMap.end()) return 0;// Haven't processed this node?
660 std::vector<BlockT *> TodoStack;
662 // Scan the predecessors of BB, checking to see if BB dominates any of
663 // them. This identifies backedges which target this node...
664 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I)
665 if (DT.dominates(BB, *I)) // If BB dominates it's predecessor...
666 TodoStack.push_back(*I);
668 if (TodoStack.empty()) return 0; // No backedges to this block...
670 // Create a new loop to represent this basic block...
671 LoopBase<BlockT> *L = new LoopBase<BlockT>(BB);
674 BlockT *EntryBlock = &BB->getParent()->getEntryBlock();
676 while (!TodoStack.empty()) { // Process all the nodes in the loop
677 BlockT *X = TodoStack.back();
678 TodoStack.pop_back();
680 if (!L->contains(X) && // As of yet unprocessed??
681 DT.dominates(EntryBlock, X)) { // X is reachable from entry block?
682 // Check to see if this block already belongs to a loop. If this occurs
683 // then we have a case where a loop that is supposed to be a child of the
684 // current loop was processed before the current loop. When this occurs,
685 // this child loop gets added to a part of the current loop, making it a
686 // sibling to the current loop. We have to reparent this loop.
687 if (LoopBase<BlockT> *SubLoop =
688 const_cast<LoopBase<BlockT>*>(getLoopFor(X)))
689 if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)) {
690 // Remove the subloop from it's current parent...
691 assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L);
692 LoopBase<BlockT> *SLP = SubLoop->ParentLoop; // SubLoopParent
693 typename std::vector<LoopBase<BlockT>*>::iterator I =
694 std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop);
695 assert(I != SLP->SubLoops.end() && "SubLoop not a child of parent?");
696 SLP->SubLoops.erase(I); // Remove from parent...
698 // Add the subloop to THIS loop...
699 SubLoop->ParentLoop = L;
700 L->SubLoops.push_back(SubLoop);
703 // Normal case, add the block to our loop...
704 L->Blocks.push_back(X);
706 // Add all of the predecessors of X to the end of the work stack...
707 TodoStack.insert(TodoStack.end(), pred_begin(X), pred_end(X));
711 // If there are any loops nested within this loop, create them now!
712 for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
713 E = L->Blocks.end(); I != E; ++I)
714 if (LoopBase<BlockT> *NewLoop = ConsiderForLoop(*I, DT)) {
715 L->SubLoops.push_back(NewLoop);
716 NewLoop->ParentLoop = L;
719 // Add the basic blocks that comprise this loop to the BBMap so that this
720 // loop can be found for them.
722 for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
723 E = L->Blocks.end(); I != E; ++I) {
724 typename std::map<BlockT*, LoopBase<BlockT>*>::iterator BBMI =
725 BBMap.lower_bound(*I);
726 if (BBMI == BBMap.end() || BBMI->first != *I) // Not in map yet...
727 BBMap.insert(BBMI, std::make_pair(*I, L)); // Must be at this level
730 // Now that we have a list of all of the child loops of this loop, check to
731 // see if any of them should actually be nested inside of each other. We can
732 // accidentally pull loops our of their parents, so we must make sure to
733 // organize the loop nests correctly now.
735 std::map<BlockT*, LoopBase<BlockT>*> ContainingLoops;
736 for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
737 LoopBase<BlockT> *Child = L->SubLoops[i];
738 assert(Child->getParentLoop() == L && "Not proper child loop?");
740 if (LoopBase<BlockT> *ContainingLoop =
741 ContainingLoops[Child->getHeader()]) {
742 // If there is already a loop which contains this loop, move this loop
743 // into the containing loop.
744 MoveSiblingLoopInto(Child, ContainingLoop);
745 --i; // The loop got removed from the SubLoops list.
747 // This is currently considered to be a top-level loop. Check to see if
748 // any of the contained blocks are loop headers for subloops we have
749 // already processed.
750 for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) {
751 LoopBase<BlockT> *&BlockLoop = ContainingLoops[Child->Blocks[b]];
752 if (BlockLoop == 0) { // Child block not processed yet...
754 } else if (BlockLoop != Child) {
755 LoopBase<BlockT> *SubLoop = BlockLoop;
756 // Reparent all of the blocks which used to belong to BlockLoops
757 for (unsigned j = 0, e = SubLoop->Blocks.size(); j != e; ++j)
758 ContainingLoops[SubLoop->Blocks[j]] = Child;
760 // There is already a loop which contains this block, that means
761 // that we should reparent the loop which the block is currently
762 // considered to belong to to be a child of this loop.
763 MoveSiblingLoopInto(SubLoop, Child);
764 --i; // We just shrunk the SubLoops list.
774 /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside of
775 /// the NewParent Loop, instead of being a sibling of it.
776 void MoveSiblingLoopInto(LoopBase<BlockT> *NewChild,
777 LoopBase<BlockT> *NewParent) {
778 LoopBase<BlockT> *OldParent = NewChild->getParentLoop();
779 assert(OldParent && OldParent == NewParent->getParentLoop() &&
780 NewChild != NewParent && "Not sibling loops!");
782 // Remove NewChild from being a child of OldParent
783 typename std::vector<LoopBase<BlockT>*>::iterator I =
784 std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(), NewChild);
785 assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??");
786 OldParent->SubLoops.erase(I); // Remove from parent's subloops list
787 NewChild->ParentLoop = 0;
789 InsertLoopInto(NewChild, NewParent);
792 /// InsertLoopInto - This inserts loop L into the specified parent loop. If the
793 /// parent loop contains a loop which should contain L, the loop gets inserted
795 void InsertLoopInto(LoopBase<BlockT> *L, LoopBase<BlockT> *Parent) {
796 BlockT *LHeader = L->getHeader();
797 assert(Parent->contains(LHeader) && "This loop should not be inserted here!");
799 // Check to see if it belongs in a child loop...
800 for (unsigned i = 0, e = Parent->SubLoops.size(); i != e; ++i)
801 if (Parent->SubLoops[i]->contains(LHeader)) {
802 InsertLoopInto(L, Parent->SubLoops[i]);
806 // If not, insert it here!
807 Parent->SubLoops.push_back(L);
808 L->ParentLoop = Parent;
813 void print(std::ostream &OS, const Module* ) const {
814 for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
815 TopLevelLoops[i]->print(OS);
817 for (std::map<BasicBlock*, Loop*>::const_iterator I = BBMap.begin(),
818 E = BBMap.end(); I != E; ++I)
819 OS << "BB '" << I->first->getName() << "' level = "
820 << I->second->getLoopDepth() << "\n";
825 class LoopInfo : public FunctionPass {
826 LoopInfoBase<BasicBlock>* LI;
827 friend class LoopBase<BasicBlock>;
829 LoopInfoBase<BasicBlock>& getBase() { return *LI; }
831 static char ID; // Pass identification, replacement for typeid
833 LoopInfo() : FunctionPass(intptr_t(&ID)) {
834 LI = new LoopInfoBase<BasicBlock>();
837 ~LoopInfo() { LI->releaseMemory(); }
839 /// iterator/begin/end - The interface to the top-level loops in the current
842 typedef std::vector<Loop*>::const_iterator iterator;
843 inline iterator begin() const { return LI->begin(); }
844 inline iterator end() const { return LI->end(); }
846 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
847 /// block is in no loop (for example the entry node), null is returned.
849 inline Loop *getLoopFor(const BasicBlock *BB) const {
850 return LI->getLoopFor(BB);
853 /// operator[] - same as getLoopFor...
855 inline const Loop *operator[](const BasicBlock *BB) const {
856 return LI->getLoopFor(BB);
859 /// getLoopDepth - Return the loop nesting level of the specified block...
861 inline unsigned getLoopDepth(const BasicBlock *BB) const {
862 return LI->getLoopDepth(BB);
865 // isLoopHeader - True if the block is a loop header node
866 inline bool isLoopHeader(BasicBlock *BB) const {
867 return LI->isLoopHeader(BB);
870 /// runOnFunction - Calculate the natural loop information.
872 virtual bool runOnFunction(Function &F);
874 virtual void releaseMemory() { LI->releaseMemory(); }
876 virtual void print(std::ostream &O, const Module* M = 0) const {
877 if (O) LI->print(O, M);
880 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
882 /// removeLoop - This removes the specified top-level loop from this loop info
883 /// object. The loop is not deleted, as it will presumably be inserted into
885 inline Loop *removeLoop(iterator I) { return LI->removeLoop(I); }
887 /// changeLoopFor - Change the top-level loop that contains BB to the
888 /// specified loop. This should be used by transformations that restructure
889 /// the loop hierarchy tree.
890 inline void changeLoopFor(BasicBlock *BB, Loop *L) {
891 LI->changeLoopFor(BB, L);
894 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
895 /// list with the indicated loop.
896 inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
897 LI->changeTopLevelLoop(OldLoop, NewLoop);
900 /// addTopLevelLoop - This adds the specified loop to the collection of
902 inline void addTopLevelLoop(Loop *New) {
903 LI->addTopLevelLoop(New);
906 /// removeBlock - This method completely removes BB from all data structures,
907 /// including all of the Loop objects it is nested in and our mapping from
908 /// BasicBlocks to loops.
909 void removeBlock(BasicBlock *BB) {
915 // Allow clients to walk the list of nested loops...
916 template <> struct GraphTraits<const Loop*> {
917 typedef const Loop NodeType;
918 typedef std::vector<Loop*>::const_iterator ChildIteratorType;
920 static NodeType *getEntryNode(const Loop *L) { return L; }
921 static inline ChildIteratorType child_begin(NodeType *N) {
924 static inline ChildIteratorType child_end(NodeType *N) {
929 template <> struct GraphTraits<Loop*> {
930 typedef Loop NodeType;
931 typedef std::vector<Loop*>::const_iterator ChildIteratorType;
933 static NodeType *getEntryNode(Loop *L) { return L; }
934 static inline ChildIteratorType child_begin(NodeType *N) {
937 static inline ChildIteratorType child_end(NodeType *N) {
942 template<class BlockT>
943 void LoopBase<BlockT>::addBasicBlockToLoop(BlockT *NewBB,
945 assert((Blocks.empty() || LI[getHeader()] == this) &&
946 "Incorrect LI specified for this loop!");
947 assert(NewBB && "Cannot add a null basic block to the loop!");
948 assert(LI[NewBB] == 0 && "BasicBlock already in the loop!");
950 LoopInfoBase<BasicBlock>& LIB = LI.getBase();
952 // Add the loop mapping to the LoopInfo object...
953 LIB.BBMap[NewBB] = this;
955 // Add the basic block to this loop and all parent loops...
956 LoopBase<BlockT> *L = this;
958 L->Blocks.push_back(NewBB);
959 L = L->getParentLoop();
963 } // End llvm namespace
965 // Make sure that any clients of this file link in LoopInfo.cpp
966 FORCE_DEFINING_FILE_TO_BE_LINKED(LoopInfo)