1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
49 static void RemoveFromVector(std::vector<T*> &V, T *N) {
50 typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
51 assert(I != V.end() && "N is not in this list!");
57 template<class N> class LoopInfoBase;
58 template<class N> class LoopBase;
60 typedef LoopBase<BasicBlock> Loop;
62 //===----------------------------------------------------------------------===//
63 /// LoopBase class - Instances of this class are used to represent loops that
64 /// are detected in the flow graph
66 template<class BlockT>
68 LoopBase<BlockT> *ParentLoop;
69 // SubLoops - Loops contained entirely within this one.
70 std::vector<LoopBase<BlockT>*> SubLoops;
72 // Blocks - The list of blocks in this loop. First entry is the header node.
73 std::vector<BlockT*> Blocks;
75 LoopBase(const LoopBase<BlockT> &); // DO NOT IMPLEMENT
76 const LoopBase<BlockT>&operator=(const LoopBase<BlockT> &);// DO NOT IMPLEMENT
78 /// Loop ctor - This creates an empty loop.
79 LoopBase() : ParentLoop(0) {}
81 for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
85 /// getLoopDepth - Return the nesting level of this loop. An outer-most
86 /// loop has depth 1, for consistency with loop depth values used for basic
87 /// blocks, where depth 0 is used for blocks not inside any loops.
88 unsigned getLoopDepth() const {
90 for (const LoopBase<BlockT> *CurLoop = ParentLoop; CurLoop;
91 CurLoop = CurLoop->ParentLoop)
95 BlockT *getHeader() const { return Blocks.front(); }
96 LoopBase<BlockT> *getParentLoop() const { return ParentLoop; }
98 /// contains - Return true if the specified basic block is in this loop
100 bool contains(const BlockT *BB) const {
101 return std::find(block_begin(), block_end(), BB) != block_end();
104 /// iterator/begin/end - Return the loops contained entirely within this loop.
106 const std::vector<LoopBase<BlockT>*> &getSubLoops() const { return SubLoops; }
107 typedef typename std::vector<LoopBase<BlockT>*>::const_iterator iterator;
108 iterator begin() const { return SubLoops.begin(); }
109 iterator end() const { return SubLoops.end(); }
110 bool empty() const { return SubLoops.empty(); }
112 /// getBlocks - Get a list of the basic blocks which make up this loop.
114 const std::vector<BlockT*> &getBlocks() const { return Blocks; }
115 typedef typename std::vector<BlockT*>::const_iterator block_iterator;
116 block_iterator block_begin() const { return Blocks.begin(); }
117 block_iterator block_end() const { return Blocks.end(); }
119 /// isLoopExit - True if terminator in the block can branch to another block
120 /// that is outside of the current loop.
122 bool isLoopExit(const BlockT *BB) const {
123 typedef GraphTraits<BlockT*> BlockTraits;
124 for (typename BlockTraits::ChildIteratorType SI =
125 BlockTraits::child_begin(const_cast<BlockT*>(BB)),
126 SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
133 /// getNumBackEdges - Calculate the number of back edges to the loop header
135 unsigned getNumBackEdges() const {
136 unsigned NumBackEdges = 0;
137 BlockT *H = getHeader();
139 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
140 for (typename InvBlockTraits::ChildIteratorType I =
141 InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
142 E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
149 /// isLoopInvariant - Return true if the specified value is loop invariant
151 inline bool isLoopInvariant(Value *V) const {
152 if (Instruction *I = dyn_cast<Instruction>(V))
153 return !contains(I->getParent());
154 return true; // All non-instructions are loop invariant
157 //===--------------------------------------------------------------------===//
158 // APIs for simple analysis of the loop.
160 // Note that all of these methods can fail on general loops (ie, there may not
161 // be a preheader, etc). For best success, the loop simplification and
162 // induction variable canonicalization pass should be used to normalize loops
163 // for easy analysis. These methods assume canonical loops.
165 /// getExitingBlocks - Return all blocks inside the loop that have successors
166 /// outside of the loop. These are the blocks _inside of the current loop_
167 /// which branch out. The returned list is always unique.
169 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
170 // Sort the blocks vector so that we can use binary search to do quick
172 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
173 std::sort(LoopBBs.begin(), LoopBBs.end());
175 typedef GraphTraits<BlockT*> BlockTraits;
176 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
177 for (typename BlockTraits::ChildIteratorType I =
178 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
180 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
181 // Not in current loop? It must be an exit block.
182 ExitingBlocks.push_back(*BI);
187 /// getExitingBlock - If getExitingBlocks would return exactly one block,
188 /// return that block. Otherwise return null.
189 BlockT *getExitingBlock() const {
190 SmallVector<BlockT*, 8> ExitingBlocks;
191 getExitingBlocks(ExitingBlocks);
192 if (ExitingBlocks.size() == 1)
193 return ExitingBlocks[0];
197 /// getExitBlocks - Return all of the successor blocks of this loop. These
198 /// are the blocks _outside of the current loop_ which are branched to.
200 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
201 // Sort the blocks vector so that we can use binary search to do quick
203 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
204 std::sort(LoopBBs.begin(), LoopBBs.end());
206 typedef GraphTraits<BlockT*> BlockTraits;
207 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
208 for (typename BlockTraits::ChildIteratorType I =
209 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
211 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
212 // Not in current loop? It must be an exit block.
213 ExitBlocks.push_back(*I);
216 /// getExitBlock - If getExitBlocks would return exactly one block,
217 /// return that block. Otherwise return null.
218 BlockT *getExitBlock() const {
219 SmallVector<BlockT*, 8> ExitBlocks;
220 getExitBlocks(ExitBlocks);
221 if (ExitBlocks.size() == 1)
222 return ExitBlocks[0];
226 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
227 /// These are the blocks _outside of the current loop_ which are branched to.
228 /// This assumes that loop is in canonical form.
230 void getUniqueExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
231 // Sort the blocks vector so that we can use binary search to do quick
233 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
234 std::sort(LoopBBs.begin(), LoopBBs.end());
236 std::vector<BlockT*> switchExitBlocks;
238 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI) {
240 BlockT *current = *BI;
241 switchExitBlocks.clear();
243 typedef GraphTraits<BlockT*> BlockTraits;
244 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
245 for (typename BlockTraits::ChildIteratorType I =
246 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
248 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
249 // If block is inside the loop then it is not a exit block.
252 typename InvBlockTraits::ChildIteratorType PI =
253 InvBlockTraits::child_begin(*I);
254 BlockT *firstPred = *PI;
256 // If current basic block is this exit block's first predecessor
257 // then only insert exit block in to the output ExitBlocks vector.
258 // This ensures that same exit block is not inserted twice into
259 // ExitBlocks vector.
260 if (current != firstPred)
263 // If a terminator has more then two successors, for example SwitchInst,
264 // then it is possible that there are multiple edges from current block
265 // to one exit block.
266 if (std::distance(BlockTraits::child_begin(current),
267 BlockTraits::child_end(current)) <= 2) {
268 ExitBlocks.push_back(*I);
272 // In case of multiple edges from current block to exit block, collect
273 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
275 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
276 == switchExitBlocks.end()) {
277 switchExitBlocks.push_back(*I);
278 ExitBlocks.push_back(*I);
284 /// getLoopPreheader - If there is a preheader for this loop, return it. A
285 /// loop has a preheader if there is only one edge to the header of the loop
286 /// from outside of the loop. If this is the case, the block branching to the
287 /// header of the loop is the preheader node.
289 /// This method returns null if there is no preheader for the loop.
291 BlockT *getLoopPreheader() const {
292 // Keep track of nodes outside the loop branching to the header...
295 // Loop over the predecessors of the header node...
296 BlockT *Header = getHeader();
297 typedef GraphTraits<BlockT*> BlockTraits;
298 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
299 for (typename InvBlockTraits::ChildIteratorType PI =
300 InvBlockTraits::child_begin(Header),
301 PE = InvBlockTraits::child_end(Header); PI != PE; ++PI)
302 if (!contains(*PI)) { // If the block is not in the loop...
303 if (Out && Out != *PI)
304 return 0; // Multiple predecessors outside the loop
308 // Make sure there is only one exit out of the preheader.
309 assert(Out && "Header of loop has no predecessors from outside loop?");
310 typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
312 if (SI != BlockTraits::child_end(Out))
313 return 0; // Multiple exits from the block, must not be a preheader.
315 // If there is exactly one preheader, return it. If there was zero, then
316 // Out is still null.
320 /// getLoopLatch - If there is a single latch block for this loop, return it.
321 /// A latch block is a block that contains a branch back to the header.
322 /// A loop header in normal form has two edges into it: one from a preheader
323 /// and one from a latch block.
324 BlockT *getLoopLatch() const {
325 BlockT *Header = getHeader();
326 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
327 typename InvBlockTraits::ChildIteratorType PI =
328 InvBlockTraits::child_begin(Header);
329 typename InvBlockTraits::ChildIteratorType PE =
330 InvBlockTraits::child_end(Header);
331 if (PI == PE) return 0; // no preds?
337 if (PI == PE) return 0; // only one pred?
340 if (Latch) return 0; // multiple backedges
344 if (PI != PE) return 0; // more than two preds
349 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
350 /// induction variable: an integer recurrence that starts at 0 and increments
351 /// by one each time through the loop. If so, return the phi node that
352 /// corresponds to it.
354 inline PHINode *getCanonicalInductionVariable() const {
355 BlockT *H = getHeader();
357 BlockT *Incoming = 0, *Backedge = 0;
358 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
359 typename InvBlockTraits::ChildIteratorType PI =
360 InvBlockTraits::child_begin(H);
361 assert(PI != InvBlockTraits::child_end(H) &&
362 "Loop must have at least one backedge!");
364 if (PI == InvBlockTraits::child_end(H)) return 0; // dead loop
366 if (PI != InvBlockTraits::child_end(H)) return 0; // multiple backedges?
368 if (contains(Incoming)) {
369 if (contains(Backedge))
371 std::swap(Incoming, Backedge);
372 } else if (!contains(Backedge))
375 // Loop over all of the PHI nodes, looking for a canonical indvar.
376 for (typename BlockT::iterator I = H->begin(); isa<PHINode>(I); ++I) {
377 PHINode *PN = cast<PHINode>(I);
378 if (ConstantInt *CI =
379 dyn_cast<ConstantInt>(PN->getIncomingValueForBlock(Incoming)))
380 if (CI->isNullValue())
381 if (Instruction *Inc =
382 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
383 if (Inc->getOpcode() == Instruction::Add &&
384 Inc->getOperand(0) == PN)
385 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
386 if (CI->equalsInt(1))
392 /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds
393 /// the canonical induction variable value for the "next" iteration of the
394 /// loop. This always succeeds if getCanonicalInductionVariable succeeds.
396 inline Instruction *getCanonicalInductionVariableIncrement() const {
397 if (PHINode *PN = getCanonicalInductionVariable()) {
398 bool P1InLoop = contains(PN->getIncomingBlock(1));
399 return cast<Instruction>(PN->getIncomingValue(P1InLoop));
404 /// getTripCount - Return a loop-invariant LLVM value indicating the number of
405 /// times the loop will be executed. Note that this means that the backedge
406 /// of the loop executes N-1 times. If the trip-count cannot be determined,
407 /// this returns null.
409 inline Value *getTripCount() const {
410 // Canonical loops will end with a 'cmp ne I, V', where I is the incremented
411 // canonical induction variable and V is the trip count of the loop.
412 Instruction *Inc = getCanonicalInductionVariableIncrement();
413 if (Inc == 0) return 0;
414 PHINode *IV = cast<PHINode>(Inc->getOperand(0));
416 BlockT *BackedgeBlock =
417 IV->getIncomingBlock(contains(IV->getIncomingBlock(1)));
419 if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator()))
420 if (BI->isConditional()) {
421 if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
422 if (ICI->getOperand(0) == Inc) {
423 if (BI->getSuccessor(0) == getHeader()) {
424 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
425 return ICI->getOperand(1);
426 } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) {
427 return ICI->getOperand(1);
436 /// getSmallConstantTripCount - Returns the trip count of this loop as a
437 /// normal unsigned value, if possible. Returns 0 if the trip count is unknown
438 /// of not constant. Will also return 0 if the trip count is very large
440 inline unsigned getSmallConstantTripCount() const {
441 Value* TripCount = this->getTripCount();
443 if (ConstantInt *TripCountC = dyn_cast<ConstantInt>(TripCount)) {
444 // Guard against huge trip counts.
445 if (TripCountC->getValue().getActiveBits() <= 32) {
446 return (unsigned)TripCountC->getZExtValue();
453 /// getSmallConstantTripMultiple - Returns the largest constant divisor of the
454 /// trip count of this loop as a normal unsigned value, if possible. This
455 /// means that the actual trip count is always a multiple of the returned
456 /// value (don't forget the trip count could very well be zero as well!).
458 /// Returns 1 if the trip count is unknown or not guaranteed to be the
459 /// multiple of a constant (which is also the case if the trip count is simply
460 /// constant, use getSmallConstantTripCount for that case), Will also return 1
461 /// if the trip count is very large (>= 2^32).
462 inline unsigned getSmallConstantTripMultiple() const {
463 Value* TripCount = this->getTripCount();
464 // This will hold the ConstantInt result, if any
465 ConstantInt *Result = NULL;
467 // See if the trip count is constant itself
468 Result = dyn_cast<ConstantInt>(TripCount);
469 // if not, see if it is a multiplication
471 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TripCount)) {
472 switch (BO->getOpcode()) {
473 case BinaryOperator::Mul:
474 Result = dyn_cast<ConstantInt>(BO->getOperand(1));
481 // Guard against huge trip counts.
482 if (Result && Result->getValue().getActiveBits() <= 32) {
483 return (unsigned)Result->getZExtValue();
489 /// isLCSSAForm - Return true if the Loop is in LCSSA form
490 inline bool isLCSSAForm() const {
491 // Sort the blocks vector so that we can use binary search to do quick
493 SmallPtrSet<BlockT*, 16> LoopBBs(block_begin(), block_end());
495 for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) {
497 for (typename BlockT::iterator I = BB->begin(), E = BB->end(); I != E;++I)
498 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
500 BlockT *UserBB = cast<Instruction>(*UI)->getParent();
501 if (PHINode *P = dyn_cast<PHINode>(*UI)) {
502 UserBB = P->getIncomingBlock(UI);
505 // Check the current block, as a fast-path. Most values are used in
506 // the same block they are defined in.
507 if (UserBB != BB && !LoopBBs.count(UserBB))
515 //===--------------------------------------------------------------------===//
516 // APIs for updating loop information after changing the CFG
519 /// addBasicBlockToLoop - This method is used by other analyses to update loop
520 /// information. NewBB is set to be a new member of the current loop.
521 /// Because of this, it is added as a member of all parent loops, and is added
522 /// to the specified LoopInfo object as being in the current basic block. It
523 /// is not valid to replace the loop header with this method.
525 void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT> &LI);
527 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
528 /// the OldChild entry in our children list with NewChild, and updates the
529 /// parent pointer of OldChild to be null and the NewChild to be this loop.
530 /// This updates the loop depth of the new child.
531 void replaceChildLoopWith(LoopBase<BlockT> *OldChild,
532 LoopBase<BlockT> *NewChild) {
533 assert(OldChild->ParentLoop == this && "This loop is already broken!");
534 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
535 typename std::vector<LoopBase<BlockT>*>::iterator I =
536 std::find(SubLoops.begin(), SubLoops.end(), OldChild);
537 assert(I != SubLoops.end() && "OldChild not in loop!");
539 OldChild->ParentLoop = 0;
540 NewChild->ParentLoop = this;
543 /// addChildLoop - Add the specified loop to be a child of this loop. This
544 /// updates the loop depth of the new child.
546 void addChildLoop(LoopBase<BlockT> *NewChild) {
547 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
548 NewChild->ParentLoop = this;
549 SubLoops.push_back(NewChild);
552 /// removeChildLoop - This removes the specified child from being a subloop of
553 /// this loop. The loop is not deleted, as it will presumably be inserted
554 /// into another loop.
555 LoopBase<BlockT> *removeChildLoop(iterator I) {
556 assert(I != SubLoops.end() && "Cannot remove end iterator!");
557 LoopBase<BlockT> *Child = *I;
558 assert(Child->ParentLoop == this && "Child is not a child of this loop!");
559 SubLoops.erase(SubLoops.begin()+(I-begin()));
560 Child->ParentLoop = 0;
564 /// addBlockEntry - This adds a basic block directly to the basic block list.
565 /// This should only be used by transformations that create new loops. Other
566 /// transformations should use addBasicBlockToLoop.
567 void addBlockEntry(BlockT *BB) {
568 Blocks.push_back(BB);
571 /// moveToHeader - This method is used to move BB (which must be part of this
572 /// loop) to be the loop header of the loop (the block that dominates all
574 void moveToHeader(BlockT *BB) {
575 if (Blocks[0] == BB) return;
576 for (unsigned i = 0; ; ++i) {
577 assert(i != Blocks.size() && "Loop does not contain BB!");
578 if (Blocks[i] == BB) {
579 Blocks[i] = Blocks[0];
586 /// removeBlockFromLoop - This removes the specified basic block from the
587 /// current loop, updating the Blocks as appropriate. This does not update
588 /// the mapping in the LoopInfo class.
589 void removeBlockFromLoop(BlockT *BB) {
590 RemoveFromVector(Blocks, BB);
593 /// verifyLoop - Verify loop structure
594 void verifyLoop() const {
596 assert (getHeader() && "Loop header is missing");
597 assert (getLoopPreheader() && "Loop preheader is missing");
598 assert (getLoopLatch() && "Loop latch is missing");
599 for (iterator I = SubLoops.begin(), E = SubLoops.end(); I != E; ++I)
604 void print(std::ostream &OS, unsigned Depth = 0) const {
605 OS << std::string(Depth*2, ' ') << "Loop at depth " << getLoopDepth()
608 for (unsigned i = 0; i < getBlocks().size(); ++i) {
610 BlockT *BB = getBlocks()[i];
611 WriteAsOperand(OS, BB, false);
612 if (BB == getHeader()) OS << "<header>";
613 if (BB == getLoopLatch()) OS << "<latch>";
614 if (isLoopExit(BB)) OS << "<exit>";
618 for (iterator I = begin(), E = end(); I != E; ++I)
619 (*I)->print(OS, Depth+2);
622 void print(std::ostream *O, unsigned Depth = 0) const {
623 if (O) print(*O, Depth);
631 friend class LoopInfoBase<BlockT>;
632 explicit LoopBase(BlockT *BB) : ParentLoop(0) {
633 Blocks.push_back(BB);
638 //===----------------------------------------------------------------------===//
639 /// LoopInfo - This class builds and contains all of the top level loop
640 /// structures in the specified function.
643 template<class BlockT>
645 // BBMap - Mapping of basic blocks to the inner most loop they occur in
646 std::map<BlockT*, LoopBase<BlockT>*> BBMap;
647 std::vector<LoopBase<BlockT>*> TopLevelLoops;
648 friend class LoopBase<BlockT>;
652 ~LoopInfoBase() { releaseMemory(); }
654 void releaseMemory() {
655 for (typename std::vector<LoopBase<BlockT>* >::iterator I =
656 TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
657 delete *I; // Delete all of the loops...
659 BBMap.clear(); // Reset internal state of analysis
660 TopLevelLoops.clear();
663 /// iterator/begin/end - The interface to the top-level loops in the current
666 typedef typename std::vector<LoopBase<BlockT>*>::const_iterator iterator;
667 iterator begin() const { return TopLevelLoops.begin(); }
668 iterator end() const { return TopLevelLoops.end(); }
669 bool empty() const { return TopLevelLoops.empty(); }
671 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
672 /// block is in no loop (for example the entry node), null is returned.
674 LoopBase<BlockT> *getLoopFor(const BlockT *BB) const {
675 typename std::map<BlockT *, LoopBase<BlockT>*>::const_iterator I=
676 BBMap.find(const_cast<BlockT*>(BB));
677 return I != BBMap.end() ? I->second : 0;
680 /// operator[] - same as getLoopFor...
682 const LoopBase<BlockT> *operator[](const BlockT *BB) const {
683 return getLoopFor(BB);
686 /// getLoopDepth - Return the loop nesting level of the specified block. A
687 /// depth of 0 means the block is not inside any loop.
689 unsigned getLoopDepth(const BlockT *BB) const {
690 const LoopBase<BlockT> *L = getLoopFor(BB);
691 return L ? L->getLoopDepth() : 0;
694 // isLoopHeader - True if the block is a loop header node
695 bool isLoopHeader(BlockT *BB) const {
696 const LoopBase<BlockT> *L = getLoopFor(BB);
697 return L && L->getHeader() == BB;
700 /// removeLoop - This removes the specified top-level loop from this loop info
701 /// object. The loop is not deleted, as it will presumably be inserted into
703 LoopBase<BlockT> *removeLoop(iterator I) {
704 assert(I != end() && "Cannot remove end iterator!");
705 LoopBase<BlockT> *L = *I;
706 assert(L->getParentLoop() == 0 && "Not a top-level loop!");
707 TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
711 /// changeLoopFor - Change the top-level loop that contains BB to the
712 /// specified loop. This should be used by transformations that restructure
713 /// the loop hierarchy tree.
714 void changeLoopFor(BlockT *BB, LoopBase<BlockT> *L) {
715 LoopBase<BlockT> *&OldLoop = BBMap[BB];
716 assert(OldLoop && "Block not in a loop yet!");
720 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
721 /// list with the indicated loop.
722 void changeTopLevelLoop(LoopBase<BlockT> *OldLoop,
723 LoopBase<BlockT> *NewLoop) {
724 typename std::vector<LoopBase<BlockT>*>::iterator I =
725 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
726 assert(I != TopLevelLoops.end() && "Old loop not at top level!");
728 assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
729 "Loops already embedded into a subloop!");
732 /// addTopLevelLoop - This adds the specified loop to the collection of
734 void addTopLevelLoop(LoopBase<BlockT> *New) {
735 assert(New->getParentLoop() == 0 && "Loop already in subloop!");
736 TopLevelLoops.push_back(New);
739 /// removeBlock - This method completely removes BB from all data structures,
740 /// including all of the Loop objects it is nested in and our mapping from
741 /// BasicBlocks to loops.
742 void removeBlock(BlockT *BB) {
743 typename std::map<BlockT *, LoopBase<BlockT>*>::iterator I = BBMap.find(BB);
744 if (I != BBMap.end()) {
745 for (LoopBase<BlockT> *L = I->second; L; L = L->getParentLoop())
746 L->removeBlockFromLoop(BB);
754 static bool isNotAlreadyContainedIn(const LoopBase<BlockT> *SubLoop,
755 const LoopBase<BlockT> *ParentLoop) {
756 if (SubLoop == 0) return true;
757 if (SubLoop == ParentLoop) return false;
758 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
761 void Calculate(DominatorTreeBase<BlockT> &DT) {
762 BlockT *RootNode = DT.getRootNode()->getBlock();
764 for (df_iterator<BlockT*> NI = df_begin(RootNode),
765 NE = df_end(RootNode); NI != NE; ++NI)
766 if (LoopBase<BlockT> *L = ConsiderForLoop(*NI, DT))
767 TopLevelLoops.push_back(L);
770 LoopBase<BlockT> *ConsiderForLoop(BlockT *BB, DominatorTreeBase<BlockT> &DT) {
771 if (BBMap.find(BB) != BBMap.end()) return 0;// Haven't processed this node?
773 std::vector<BlockT *> TodoStack;
775 // Scan the predecessors of BB, checking to see if BB dominates any of
776 // them. This identifies backedges which target this node...
777 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
778 for (typename InvBlockTraits::ChildIteratorType I =
779 InvBlockTraits::child_begin(BB), E = InvBlockTraits::child_end(BB);
781 if (DT.dominates(BB, *I)) // If BB dominates it's predecessor...
782 TodoStack.push_back(*I);
784 if (TodoStack.empty()) return 0; // No backedges to this block...
786 // Create a new loop to represent this basic block...
787 LoopBase<BlockT> *L = new LoopBase<BlockT>(BB);
790 BlockT *EntryBlock = BB->getParent()->begin();
792 while (!TodoStack.empty()) { // Process all the nodes in the loop
793 BlockT *X = TodoStack.back();
794 TodoStack.pop_back();
796 if (!L->contains(X) && // As of yet unprocessed??
797 DT.dominates(EntryBlock, X)) { // X is reachable from entry block?
798 // Check to see if this block already belongs to a loop. If this occurs
799 // then we have a case where a loop that is supposed to be a child of
800 // the current loop was processed before the current loop. When this
801 // occurs, this child loop gets added to a part of the current loop,
802 // making it a sibling to the current loop. We have to reparent this
804 if (LoopBase<BlockT> *SubLoop =
805 const_cast<LoopBase<BlockT>*>(getLoopFor(X)))
806 if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)){
807 // Remove the subloop from it's current parent...
808 assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L);
809 LoopBase<BlockT> *SLP = SubLoop->ParentLoop; // SubLoopParent
810 typename std::vector<LoopBase<BlockT>*>::iterator I =
811 std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop);
812 assert(I != SLP->SubLoops.end() &&"SubLoop not a child of parent?");
813 SLP->SubLoops.erase(I); // Remove from parent...
815 // Add the subloop to THIS loop...
816 SubLoop->ParentLoop = L;
817 L->SubLoops.push_back(SubLoop);
820 // Normal case, add the block to our loop...
821 L->Blocks.push_back(X);
823 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
825 // Add all of the predecessors of X to the end of the work stack...
826 TodoStack.insert(TodoStack.end(), InvBlockTraits::child_begin(X),
827 InvBlockTraits::child_end(X));
831 // If there are any loops nested within this loop, create them now!
832 for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
833 E = L->Blocks.end(); I != E; ++I)
834 if (LoopBase<BlockT> *NewLoop = ConsiderForLoop(*I, DT)) {
835 L->SubLoops.push_back(NewLoop);
836 NewLoop->ParentLoop = L;
839 // Add the basic blocks that comprise this loop to the BBMap so that this
840 // loop can be found for them.
842 for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
843 E = L->Blocks.end(); I != E; ++I) {
844 typename std::map<BlockT*, LoopBase<BlockT>*>::iterator BBMI =
846 if (BBMI == BBMap.end()) // Not in map yet...
847 BBMap.insert(BBMI, std::make_pair(*I, L)); // Must be at this level
850 // Now that we have a list of all of the child loops of this loop, check to
851 // see if any of them should actually be nested inside of each other. We
852 // can accidentally pull loops our of their parents, so we must make sure to
853 // organize the loop nests correctly now.
855 std::map<BlockT*, LoopBase<BlockT>*> ContainingLoops;
856 for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
857 LoopBase<BlockT> *Child = L->SubLoops[i];
858 assert(Child->getParentLoop() == L && "Not proper child loop?");
860 if (LoopBase<BlockT> *ContainingLoop =
861 ContainingLoops[Child->getHeader()]) {
862 // If there is already a loop which contains this loop, move this loop
863 // into the containing loop.
864 MoveSiblingLoopInto(Child, ContainingLoop);
865 --i; // The loop got removed from the SubLoops list.
867 // This is currently considered to be a top-level loop. Check to see
868 // if any of the contained blocks are loop headers for subloops we
869 // have already processed.
870 for (unsigned b = 0, e = Child->Blocks.size(); b != e; ++b) {
871 LoopBase<BlockT> *&BlockLoop = ContainingLoops[Child->Blocks[b]];
872 if (BlockLoop == 0) { // Child block not processed yet...
874 } else if (BlockLoop != Child) {
875 LoopBase<BlockT> *SubLoop = BlockLoop;
876 // Reparent all of the blocks which used to belong to BlockLoops
877 for (unsigned j = 0, e = SubLoop->Blocks.size(); j != e; ++j)
878 ContainingLoops[SubLoop->Blocks[j]] = Child;
880 // There is already a loop which contains this block, that means
881 // that we should reparent the loop which the block is currently
882 // considered to belong to to be a child of this loop.
883 MoveSiblingLoopInto(SubLoop, Child);
884 --i; // We just shrunk the SubLoops list.
894 /// MoveSiblingLoopInto - This method moves the NewChild loop to live inside
895 /// of the NewParent Loop, instead of being a sibling of it.
896 void MoveSiblingLoopInto(LoopBase<BlockT> *NewChild,
897 LoopBase<BlockT> *NewParent) {
898 LoopBase<BlockT> *OldParent = NewChild->getParentLoop();
899 assert(OldParent && OldParent == NewParent->getParentLoop() &&
900 NewChild != NewParent && "Not sibling loops!");
902 // Remove NewChild from being a child of OldParent
903 typename std::vector<LoopBase<BlockT>*>::iterator I =
904 std::find(OldParent->SubLoops.begin(), OldParent->SubLoops.end(),
906 assert(I != OldParent->SubLoops.end() && "Parent fields incorrect??");
907 OldParent->SubLoops.erase(I); // Remove from parent's subloops list
908 NewChild->ParentLoop = 0;
910 InsertLoopInto(NewChild, NewParent);
913 /// InsertLoopInto - This inserts loop L into the specified parent loop. If
914 /// the parent loop contains a loop which should contain L, the loop gets
915 /// inserted into L instead.
916 void InsertLoopInto(LoopBase<BlockT> *L, LoopBase<BlockT> *Parent) {
917 BlockT *LHeader = L->getHeader();
918 assert(Parent->contains(LHeader) &&
919 "This loop should not be inserted here!");
921 // Check to see if it belongs in a child loop...
922 for (unsigned i = 0, e = static_cast<unsigned>(Parent->SubLoops.size());
924 if (Parent->SubLoops[i]->contains(LHeader)) {
925 InsertLoopInto(L, Parent->SubLoops[i]);
929 // If not, insert it here!
930 Parent->SubLoops.push_back(L);
931 L->ParentLoop = Parent;
936 void print(std::ostream &OS, const Module* ) const {
937 for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
938 TopLevelLoops[i]->print(OS);
940 for (std::map<BasicBlock*, Loop*>::const_iterator I = BBMap.begin(),
941 E = BBMap.end(); I != E; ++I)
942 OS << "BB '" << I->first->getName() << "' level = "
943 << I->second->getLoopDepth() << "\n";
948 class LoopInfo : public FunctionPass {
949 LoopInfoBase<BasicBlock>* LI;
950 friend class LoopBase<BasicBlock>;
953 static char ID; // Pass identification, replacement for typeid
955 LoopInfo() : FunctionPass(&ID) {
956 LI = new LoopInfoBase<BasicBlock>();
959 ~LoopInfo() { delete LI; }
961 LoopInfoBase<BasicBlock>& getBase() { return *LI; }
963 /// iterator/begin/end - The interface to the top-level loops in the current
966 typedef std::vector<Loop*>::const_iterator iterator;
967 inline iterator begin() const { return LI->begin(); }
968 inline iterator end() const { return LI->end(); }
969 bool empty() const { return LI->empty(); }
971 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
972 /// block is in no loop (for example the entry node), null is returned.
974 inline Loop *getLoopFor(const BasicBlock *BB) const {
975 return LI->getLoopFor(BB);
978 /// operator[] - same as getLoopFor...
980 inline const Loop *operator[](const BasicBlock *BB) const {
981 return LI->getLoopFor(BB);
984 /// getLoopDepth - Return the loop nesting level of the specified block. A
985 /// depth of 0 means the block is not inside any loop.
987 inline unsigned getLoopDepth(const BasicBlock *BB) const {
988 return LI->getLoopDepth(BB);
991 // isLoopHeader - True if the block is a loop header node
992 inline bool isLoopHeader(BasicBlock *BB) const {
993 return LI->isLoopHeader(BB);
996 /// runOnFunction - Calculate the natural loop information.
998 virtual bool runOnFunction(Function &F);
1000 virtual void releaseMemory() { LI->releaseMemory(); }
1002 virtual void print(std::ostream &O, const Module* M = 0) const {
1003 if (O) LI->print(O, M);
1006 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
1008 /// removeLoop - This removes the specified top-level loop from this loop info
1009 /// object. The loop is not deleted, as it will presumably be inserted into
1011 inline Loop *removeLoop(iterator I) { return LI->removeLoop(I); }
1013 /// changeLoopFor - Change the top-level loop that contains BB to the
1014 /// specified loop. This should be used by transformations that restructure
1015 /// the loop hierarchy tree.
1016 inline void changeLoopFor(BasicBlock *BB, Loop *L) {
1017 LI->changeLoopFor(BB, L);
1020 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
1021 /// list with the indicated loop.
1022 inline void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop) {
1023 LI->changeTopLevelLoop(OldLoop, NewLoop);
1026 /// addTopLevelLoop - This adds the specified loop to the collection of
1027 /// top-level loops.
1028 inline void addTopLevelLoop(Loop *New) {
1029 LI->addTopLevelLoop(New);
1032 /// removeBlock - This method completely removes BB from all data structures,
1033 /// including all of the Loop objects it is nested in and our mapping from
1034 /// BasicBlocks to loops.
1035 void removeBlock(BasicBlock *BB) {
1036 LI->removeBlock(BB);
1041 // Allow clients to walk the list of nested loops...
1042 template <> struct GraphTraits<const Loop*> {
1043 typedef const Loop NodeType;
1044 typedef std::vector<Loop*>::const_iterator ChildIteratorType;
1046 static NodeType *getEntryNode(const Loop *L) { return L; }
1047 static inline ChildIteratorType child_begin(NodeType *N) {
1050 static inline ChildIteratorType child_end(NodeType *N) {
1055 template <> struct GraphTraits<Loop*> {
1056 typedef Loop NodeType;
1057 typedef std::vector<Loop*>::const_iterator ChildIteratorType;
1059 static NodeType *getEntryNode(Loop *L) { return L; }
1060 static inline ChildIteratorType child_begin(NodeType *N) {
1063 static inline ChildIteratorType child_end(NodeType *N) {
1068 template<class BlockT>
1069 void LoopBase<BlockT>::addBasicBlockToLoop(BlockT *NewBB,
1070 LoopInfoBase<BlockT> &LIB) {
1071 assert((Blocks.empty() || LIB[getHeader()] == this) &&
1072 "Incorrect LI specified for this loop!");
1073 assert(NewBB && "Cannot add a null basic block to the loop!");
1074 assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
1076 // Add the loop mapping to the LoopInfo object...
1077 LIB.BBMap[NewBB] = this;
1079 // Add the basic block to this loop and all parent loops...
1080 LoopBase<BlockT> *L = this;
1082 L->Blocks.push_back(NewBB);
1083 L = L->getParentLoop();
1087 } // End llvm namespace