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. A natural loop
12 // has exactly one entry-point, which is called the header. Note that natural
13 // loops may actually be several loops that share the same header node.
15 // This analysis calculates the nesting structure of loops in a function. For
16 // each natural loop identified, this analysis identifies natural loops
17 // contained entirely within the loop and the basic blocks the make up the loop.
19 // It can calculate on the fly various bits of information, for example:
21 // * whether there is a preheader for the loop
22 // * the number of back edges to the header
23 // * whether or not a particular block branches out of the loop
24 // * the successor blocks of the loop
29 //===----------------------------------------------------------------------===//
31 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
32 #define LLVM_ANALYSIS_LOOP_INFO_H
34 #include "llvm/Pass.h"
35 #include "llvm/ADT/DenseMap.h"
36 #include "llvm/ADT/DenseSet.h"
37 #include "llvm/ADT/DepthFirstIterator.h"
38 #include "llvm/ADT/GraphTraits.h"
39 #include "llvm/ADT/SmallVector.h"
40 #include "llvm/ADT/STLExtras.h"
41 #include "llvm/Analysis/Dominators.h"
42 #include "llvm/Support/CFG.h"
43 #include "llvm/Support/raw_ostream.h"
50 static void RemoveFromVector(std::vector<T*> &V, T *N) {
51 typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
52 assert(I != V.end() && "N is not in this list!");
60 template<class N, class M> class LoopInfoBase;
61 template<class N, class M> class LoopBase;
63 //===----------------------------------------------------------------------===//
64 /// LoopBase class - Instances of this class are used to represent loops that
65 /// are detected in the flow graph
67 template<class BlockT, class LoopT>
70 // SubLoops - Loops contained entirely within this one.
71 std::vector<LoopT *> SubLoops;
73 // Blocks - The list of blocks in this loop. First entry is the header node.
74 std::vector<BlockT*> Blocks;
77 LoopBase(const LoopBase<BlockT, LoopT> &);
79 const LoopBase<BlockT, LoopT>&operator=(const LoopBase<BlockT, LoopT> &);
81 /// Loop ctor - This creates an empty loop.
82 LoopBase() : ParentLoop(0) {}
84 for (size_t i = 0, e = SubLoops.size(); i != e; ++i)
88 /// getLoopDepth - Return the nesting level of this loop. An outer-most
89 /// loop has depth 1, for consistency with loop depth values used for basic
90 /// blocks, where depth 0 is used for blocks not inside any loops.
91 unsigned getLoopDepth() const {
93 for (const LoopT *CurLoop = ParentLoop; CurLoop;
94 CurLoop = CurLoop->ParentLoop)
98 BlockT *getHeader() const { return Blocks.front(); }
99 LoopT *getParentLoop() const { return ParentLoop; }
101 /// contains - Return true if the specified loop is contained within in
104 bool contains(const LoopT *L) const {
105 if (L == this) return true;
106 if (L == 0) return false;
107 return contains(L->getParentLoop());
110 /// contains - Return true if the specified basic block is in this loop.
112 bool contains(const BlockT *BB) const {
113 return std::find(block_begin(), block_end(), BB) != block_end();
116 /// contains - Return true if the specified instruction is in this loop.
118 template<class InstT>
119 bool contains(const InstT *Inst) const {
120 return contains(Inst->getParent());
123 /// iterator/begin/end - Return the loops contained entirely within this loop.
125 const std::vector<LoopT *> &getSubLoops() const { return SubLoops; }
126 typedef typename std::vector<LoopT *>::const_iterator iterator;
127 iterator begin() const { return SubLoops.begin(); }
128 iterator end() const { return SubLoops.end(); }
129 bool empty() const { return SubLoops.empty(); }
131 /// getBlocks - Get a list of the basic blocks which make up this loop.
133 const std::vector<BlockT*> &getBlocks() const { return Blocks; }
134 typedef typename std::vector<BlockT*>::const_iterator block_iterator;
135 block_iterator block_begin() const { return Blocks.begin(); }
136 block_iterator block_end() const { return Blocks.end(); }
138 /// getNumBlocks - Get the number of blocks in this loop in constant time.
139 unsigned getNumBlocks() const {
140 return Blocks.size();
143 /// isLoopExiting - True if terminator in the block can branch to another
144 /// block that is outside of the current loop.
146 bool isLoopExiting(const BlockT *BB) const {
147 typedef GraphTraits<BlockT*> BlockTraits;
148 for (typename BlockTraits::ChildIteratorType SI =
149 BlockTraits::child_begin(const_cast<BlockT*>(BB)),
150 SE = BlockTraits::child_end(const_cast<BlockT*>(BB)); SI != SE; ++SI) {
157 /// getNumBackEdges - Calculate the number of back edges to the loop header
159 unsigned getNumBackEdges() const {
160 unsigned NumBackEdges = 0;
161 BlockT *H = getHeader();
163 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
164 for (typename InvBlockTraits::ChildIteratorType I =
165 InvBlockTraits::child_begin(const_cast<BlockT*>(H)),
166 E = InvBlockTraits::child_end(const_cast<BlockT*>(H)); I != E; ++I)
173 //===--------------------------------------------------------------------===//
174 // APIs for simple analysis of the loop.
176 // Note that all of these methods can fail on general loops (ie, there may not
177 // be a preheader, etc). For best success, the loop simplification and
178 // induction variable canonicalization pass should be used to normalize loops
179 // for easy analysis. These methods assume canonical loops.
181 /// getExitingBlocks - Return all blocks inside the loop that have successors
182 /// outside of the loop. These are the blocks _inside of the current loop_
183 /// which branch out. The returned list is always unique.
185 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
186 // Sort the blocks vector so that we can use binary search to do quick
188 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
189 std::sort(LoopBBs.begin(), LoopBBs.end());
191 typedef GraphTraits<BlockT*> BlockTraits;
192 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
193 for (typename BlockTraits::ChildIteratorType I =
194 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
196 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
197 // Not in current loop? It must be an exit block.
198 ExitingBlocks.push_back(*BI);
203 /// getExitingBlock - If getExitingBlocks would return exactly one block,
204 /// return that block. Otherwise return null.
205 BlockT *getExitingBlock() const {
206 SmallVector<BlockT*, 8> ExitingBlocks;
207 getExitingBlocks(ExitingBlocks);
208 if (ExitingBlocks.size() == 1)
209 return ExitingBlocks[0];
213 /// getExitBlocks - Return all of the successor blocks of this loop. These
214 /// are the blocks _outside of the current loop_ which are branched to.
216 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
217 // Sort the blocks vector so that we can use binary search to do quick
219 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
220 std::sort(LoopBBs.begin(), LoopBBs.end());
222 typedef GraphTraits<BlockT*> BlockTraits;
223 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
224 for (typename BlockTraits::ChildIteratorType I =
225 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
227 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
228 // Not in current loop? It must be an exit block.
229 ExitBlocks.push_back(*I);
232 /// getExitBlock - If getExitBlocks would return exactly one block,
233 /// return that block. Otherwise return null.
234 BlockT *getExitBlock() const {
235 SmallVector<BlockT*, 8> ExitBlocks;
236 getExitBlocks(ExitBlocks);
237 if (ExitBlocks.size() == 1)
238 return ExitBlocks[0];
243 typedef std::pair<BlockT*, BlockT*> Edge;
245 /// getExitEdges - Return all pairs of (_inside_block_,_outside_block_).
246 template <typename EdgeT>
247 void getExitEdges(SmallVectorImpl<EdgeT> &ExitEdges) const {
248 // Sort the blocks vector so that we can use binary search to do quick
250 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
251 array_pod_sort(LoopBBs.begin(), LoopBBs.end());
253 typedef GraphTraits<BlockT*> BlockTraits;
254 for (block_iterator BI = block_begin(), BE = block_end(); BI != BE; ++BI)
255 for (typename BlockTraits::ChildIteratorType I =
256 BlockTraits::child_begin(*BI), E = BlockTraits::child_end(*BI);
258 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
259 // Not in current loop? It must be an exit block.
260 ExitEdges.push_back(EdgeT(*BI, *I));
263 /// getLoopPreheader - If there is a preheader for this loop, return it. A
264 /// loop has a preheader if there is only one edge to the header of the loop
265 /// from outside of the loop. If this is the case, the block branching to the
266 /// header of the loop is the preheader node.
268 /// This method returns null if there is no preheader for the loop.
270 BlockT *getLoopPreheader() const {
271 // Keep track of nodes outside the loop branching to the header...
272 BlockT *Out = getLoopPredecessor();
275 // Make sure there is only one exit out of the preheader.
276 typedef GraphTraits<BlockT*> BlockTraits;
277 typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
279 if (SI != BlockTraits::child_end(Out))
280 return 0; // Multiple exits from the block, must not be a preheader.
282 // The predecessor has exactly one successor, so it is a preheader.
286 /// getLoopPredecessor - If the given loop's header has exactly one unique
287 /// predecessor outside the loop, return it. Otherwise return null.
288 /// This is less strict that the loop "preheader" concept, which requires
289 /// the predecessor to have exactly one successor.
291 BlockT *getLoopPredecessor() 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 typename InvBlockTraits::NodeType *N = *PI;
303 if (!contains(N)) { // If the block is not in the loop...
305 return 0; // Multiple predecessors outside the loop
310 // Make sure there is only one exit out of the preheader.
311 assert(Out && "Header of loop has no predecessors from outside loop?");
315 /// getLoopLatch - If there is a single latch block for this loop, return it.
316 /// A latch block is a block that contains a branch back to the header.
317 BlockT *getLoopLatch() const {
318 BlockT *Header = getHeader();
319 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
320 typename InvBlockTraits::ChildIteratorType PI =
321 InvBlockTraits::child_begin(Header);
322 typename InvBlockTraits::ChildIteratorType PE =
323 InvBlockTraits::child_end(Header);
325 for (; PI != PE; ++PI) {
326 typename InvBlockTraits::NodeType *N = *PI;
336 //===--------------------------------------------------------------------===//
337 // APIs for updating loop information after changing the CFG
340 /// addBasicBlockToLoop - This method is used by other analyses to update loop
341 /// information. NewBB is set to be a new member of the current loop.
342 /// Because of this, it is added as a member of all parent loops, and is added
343 /// to the specified LoopInfo object as being in the current basic block. It
344 /// is not valid to replace the loop header with this method.
346 void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase<BlockT, LoopT> &LI);
348 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
349 /// the OldChild entry in our children list with NewChild, and updates the
350 /// parent pointer of OldChild to be null and the NewChild to be this loop.
351 /// This updates the loop depth of the new child.
352 void replaceChildLoopWith(LoopT *OldChild,
354 assert(OldChild->ParentLoop == this && "This loop is already broken!");
355 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
356 typename std::vector<LoopT *>::iterator I =
357 std::find(SubLoops.begin(), SubLoops.end(), OldChild);
358 assert(I != SubLoops.end() && "OldChild not in loop!");
360 OldChild->ParentLoop = 0;
361 NewChild->ParentLoop = static_cast<LoopT *>(this);
364 /// addChildLoop - Add the specified loop to be a child of this loop. This
365 /// updates the loop depth of the new child.
367 void addChildLoop(LoopT *NewChild) {
368 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
369 NewChild->ParentLoop = static_cast<LoopT *>(this);
370 SubLoops.push_back(NewChild);
373 /// removeChildLoop - This removes the specified child from being a subloop of
374 /// this loop. The loop is not deleted, as it will presumably be inserted
375 /// into another loop.
376 LoopT *removeChildLoop(iterator I) {
377 assert(I != SubLoops.end() && "Cannot remove end iterator!");
379 assert(Child->ParentLoop == this && "Child is not a child of this loop!");
380 SubLoops.erase(SubLoops.begin()+(I-begin()));
381 Child->ParentLoop = 0;
385 /// addBlockEntry - This adds a basic block directly to the basic block list.
386 /// This should only be used by transformations that create new loops. Other
387 /// transformations should use addBasicBlockToLoop.
388 void addBlockEntry(BlockT *BB) {
389 Blocks.push_back(BB);
392 /// moveToHeader - This method is used to move BB (which must be part of this
393 /// loop) to be the loop header of the loop (the block that dominates all
395 void moveToHeader(BlockT *BB) {
396 if (Blocks[0] == BB) return;
397 for (unsigned i = 0; ; ++i) {
398 assert(i != Blocks.size() && "Loop does not contain BB!");
399 if (Blocks[i] == BB) {
400 Blocks[i] = Blocks[0];
407 /// removeBlockFromLoop - This removes the specified basic block from the
408 /// current loop, updating the Blocks as appropriate. This does not update
409 /// the mapping in the LoopInfo class.
410 void removeBlockFromLoop(BlockT *BB) {
411 RemoveFromVector(Blocks, BB);
414 /// verifyLoop - Verify loop structure
415 void verifyLoop() const {
417 assert(!Blocks.empty() && "Loop header is missing");
419 // Sort the blocks vector so that we can use binary search to do quick
421 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
422 std::sort(LoopBBs.begin(), LoopBBs.end());
424 // Check the individual blocks.
425 for (block_iterator I = block_begin(), E = block_end(); I != E; ++I) {
427 bool HasInsideLoopSuccs = false;
428 bool HasInsideLoopPreds = false;
429 SmallVector<BlockT *, 2> OutsideLoopPreds;
431 typedef GraphTraits<BlockT*> BlockTraits;
432 for (typename BlockTraits::ChildIteratorType SI =
433 BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
435 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *SI)) {
436 HasInsideLoopSuccs = true;
439 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
440 for (typename InvBlockTraits::ChildIteratorType PI =
441 InvBlockTraits::child_begin(BB), PE = InvBlockTraits::child_end(BB);
443 typename InvBlockTraits::NodeType *N = *PI;
444 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), N))
445 HasInsideLoopPreds = true;
447 OutsideLoopPreds.push_back(N);
450 if (BB == getHeader()) {
451 assert(!OutsideLoopPreds.empty() && "Loop is unreachable!");
452 } else if (!OutsideLoopPreds.empty()) {
453 // A non-header loop shouldn't be reachable from outside the loop,
454 // though it is permitted if the predecessor is not itself actually
456 BlockT *EntryBB = BB->getParent()->begin();
457 for (df_iterator<BlockT *> NI = df_begin(EntryBB),
458 NE = df_end(EntryBB); NI != NE; ++NI)
459 for (unsigned i = 0, e = OutsideLoopPreds.size(); i != e; ++i)
460 assert(*NI != OutsideLoopPreds[i] &&
461 "Loop has multiple entry points!");
463 assert(HasInsideLoopPreds && "Loop block has no in-loop predecessors!");
464 assert(HasInsideLoopSuccs && "Loop block has no in-loop successors!");
465 assert(BB != getHeader()->getParent()->begin() &&
466 "Loop contains function entry block!");
469 // Check the subloops.
470 for (iterator I = begin(), E = end(); I != E; ++I)
471 // Each block in each subloop should be contained within this loop.
472 for (block_iterator BI = (*I)->block_begin(), BE = (*I)->block_end();
474 assert(std::binary_search(LoopBBs.begin(), LoopBBs.end(), *BI) &&
475 "Loop does not contain all the blocks of a subloop!");
478 // Check the parent loop pointer.
480 assert(std::find(ParentLoop->begin(), ParentLoop->end(), this) !=
482 "Loop is not a subloop of its parent!");
487 /// verifyLoop - Verify loop structure of this loop and all nested loops.
488 void verifyLoopNest(DenseSet<const LoopT*> *Loops) const {
489 Loops->insert(static_cast<const LoopT *>(this));
492 // Verify the subloops.
493 for (iterator I = begin(), E = end(); I != E; ++I)
494 (*I)->verifyLoopNest(Loops);
497 void print(raw_ostream &OS, unsigned Depth = 0) const {
498 OS.indent(Depth*2) << "Loop at depth " << getLoopDepth()
501 for (unsigned i = 0; i < getBlocks().size(); ++i) {
503 BlockT *BB = getBlocks()[i];
504 WriteAsOperand(OS, BB, false);
505 if (BB == getHeader()) OS << "<header>";
506 if (BB == getLoopLatch()) OS << "<latch>";
507 if (isLoopExiting(BB)) OS << "<exiting>";
511 for (iterator I = begin(), E = end(); I != E; ++I)
512 (*I)->print(OS, Depth+2);
516 friend class LoopInfoBase<BlockT, LoopT>;
517 explicit LoopBase(BlockT *BB) : ParentLoop(0) {
518 Blocks.push_back(BB);
522 template<class BlockT, class LoopT>
523 raw_ostream& operator<<(raw_ostream &OS, const LoopBase<BlockT, LoopT> &Loop) {
528 class Loop : public LoopBase<BasicBlock, Loop> {
532 /// isLoopInvariant - Return true if the specified value is loop invariant
534 bool isLoopInvariant(Value *V) const;
536 /// hasLoopInvariantOperands - Return true if all the operands of the
537 /// specified instruction are loop invariant.
538 bool hasLoopInvariantOperands(Instruction *I) const;
540 /// makeLoopInvariant - If the given value is an instruction inside of the
541 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
542 /// Return true if the value after any hoisting is loop invariant. This
543 /// function can be used as a slightly more aggressive replacement for
546 /// If InsertPt is specified, it is the point to hoist instructions to.
547 /// If null, the terminator of the loop preheader is used.
549 bool makeLoopInvariant(Value *V, bool &Changed,
550 Instruction *InsertPt = 0) const;
552 /// makeLoopInvariant - If the given instruction is inside of the
553 /// loop and it can be hoisted, do so to make it trivially loop-invariant.
554 /// Return true if the instruction after any hoisting is loop invariant. This
555 /// function can be used as a slightly more aggressive replacement for
558 /// If InsertPt is specified, it is the point to hoist instructions to.
559 /// If null, the terminator of the loop preheader is used.
561 bool makeLoopInvariant(Instruction *I, bool &Changed,
562 Instruction *InsertPt = 0) const;
564 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
565 /// induction variable: an integer recurrence that starts at 0 and increments
566 /// by one each time through the loop. If so, return the phi node that
567 /// corresponds to it.
569 /// The IndVarSimplify pass transforms loops to have a canonical induction
572 PHINode *getCanonicalInductionVariable() const;
574 /// getTripCount - Return a loop-invariant LLVM value indicating the number of
575 /// times the loop will be executed. Note that this means that the backedge
576 /// of the loop executes N-1 times. If the trip-count cannot be determined,
577 /// this returns null.
579 /// The IndVarSimplify pass transforms loops to have a form that this
580 /// function easily understands.
582 Value *getTripCount() const;
584 /// getSmallConstantTripCount - Returns the trip count of this loop as a
585 /// normal unsigned value, if possible. Returns 0 if the trip count is unknown
586 /// of not constant. Will also return 0 if the trip count is very large
589 /// The IndVarSimplify pass transforms loops to have a form that this
590 /// function easily understands.
592 unsigned getSmallConstantTripCount() const;
594 /// getSmallConstantTripMultiple - Returns the largest constant divisor of the
595 /// trip count of this loop as a normal unsigned value, if possible. This
596 /// means that the actual trip count is always a multiple of the returned
597 /// value (don't forget the trip count could very well be zero as well!).
599 /// Returns 1 if the trip count is unknown or not guaranteed to be the
600 /// multiple of a constant (which is also the case if the trip count is simply
601 /// constant, use getSmallConstantTripCount for that case), Will also return 1
602 /// if the trip count is very large (>= 2^32).
603 unsigned getSmallConstantTripMultiple() const;
605 /// isLCSSAForm - Return true if the Loop is in LCSSA form
606 bool isLCSSAForm(DominatorTree &DT) const;
608 /// isLoopSimplifyForm - Return true if the Loop is in the form that
609 /// the LoopSimplify form transforms loops to, which is sometimes called
611 bool isLoopSimplifyForm() const;
613 /// hasDedicatedExits - Return true if no exit block for the loop
614 /// has a predecessor that is outside the loop.
615 bool hasDedicatedExits() const;
617 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
618 /// These are the blocks _outside of the current loop_ which are branched to.
619 /// This assumes that loop exits are in canonical form.
621 void getUniqueExitBlocks(SmallVectorImpl<BasicBlock *> &ExitBlocks) const;
623 /// getUniqueExitBlock - If getUniqueExitBlocks would return exactly one
624 /// block, return that block. Otherwise return null.
625 BasicBlock *getUniqueExitBlock() const;
630 friend class LoopInfoBase<BasicBlock, Loop>;
631 explicit Loop(BasicBlock *BB) : LoopBase<BasicBlock, Loop>(BB) {}
634 //===----------------------------------------------------------------------===//
635 /// LoopInfo - This class builds and contains all of the top level loop
636 /// structures in the specified function.
639 template<class BlockT, class LoopT>
641 // BBMap - Mapping of basic blocks to the inner most loop they occur in
642 DenseMap<BlockT *, LoopT *> BBMap;
643 std::vector<LoopT *> TopLevelLoops;
644 friend class LoopBase<BlockT, LoopT>;
645 friend class LoopInfo;
647 void operator=(const LoopInfoBase &); // do not implement
648 LoopInfoBase(const LoopInfo &); // do not implement
651 ~LoopInfoBase() { releaseMemory(); }
653 void releaseMemory() {
654 for (typename std::vector<LoopT *>::iterator I =
655 TopLevelLoops.begin(), E = TopLevelLoops.end(); I != E; ++I)
656 delete *I; // Delete all of the loops...
658 BBMap.clear(); // Reset internal state of analysis
659 TopLevelLoops.clear();
662 /// iterator/begin/end - The interface to the top-level loops in the current
665 typedef typename std::vector<LoopT *>::const_iterator iterator;
666 iterator begin() const { return TopLevelLoops.begin(); }
667 iterator end() const { return TopLevelLoops.end(); }
668 bool empty() const { return TopLevelLoops.empty(); }
670 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
671 /// block is in no loop (for example the entry node), null is returned.
673 LoopT *getLoopFor(const BlockT *BB) const {
674 typename DenseMap<BlockT *, LoopT *>::const_iterator I=
675 BBMap.find(const_cast<BlockT*>(BB));
676 return I != BBMap.end() ? I->second : 0;
679 /// operator[] - same as getLoopFor...
681 const LoopT *operator[](const BlockT *BB) const {
682 return getLoopFor(BB);
685 /// getLoopDepth - Return the loop nesting level of the specified block. A
686 /// depth of 0 means the block is not inside any loop.
688 unsigned getLoopDepth(const BlockT *BB) const {
689 const LoopT *L = getLoopFor(BB);
690 return L ? L->getLoopDepth() : 0;
693 // isLoopHeader - True if the block is a loop header node
694 bool isLoopHeader(BlockT *BB) const {
695 const LoopT *L = getLoopFor(BB);
696 return L && L->getHeader() == BB;
699 /// removeLoop - This removes the specified top-level loop from this loop info
700 /// object. The loop is not deleted, as it will presumably be inserted into
702 LoopT *removeLoop(iterator I) {
703 assert(I != end() && "Cannot remove end iterator!");
705 assert(L->getParentLoop() == 0 && "Not a top-level loop!");
706 TopLevelLoops.erase(TopLevelLoops.begin() + (I-begin()));
710 /// changeLoopFor - Change the top-level loop that contains BB to the
711 /// specified loop. This should be used by transformations that restructure
712 /// the loop hierarchy tree.
713 void changeLoopFor(BlockT *BB, LoopT *L) {
715 typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
716 if (I != BBMap.end())
723 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
724 /// list with the indicated loop.
725 void changeTopLevelLoop(LoopT *OldLoop,
727 typename std::vector<LoopT *>::iterator I =
728 std::find(TopLevelLoops.begin(), TopLevelLoops.end(), OldLoop);
729 assert(I != TopLevelLoops.end() && "Old loop not at top level!");
731 assert(NewLoop->ParentLoop == 0 && OldLoop->ParentLoop == 0 &&
732 "Loops already embedded into a subloop!");
735 /// addTopLevelLoop - This adds the specified loop to the collection of
737 void addTopLevelLoop(LoopT *New) {
738 assert(New->getParentLoop() == 0 && "Loop already in subloop!");
739 TopLevelLoops.push_back(New);
742 /// removeBlock - This method completely removes BB from all data structures,
743 /// including all of the Loop objects it is nested in and our mapping from
744 /// BasicBlocks to loops.
745 void removeBlock(BlockT *BB) {
746 typename DenseMap<BlockT *, LoopT *>::iterator I = BBMap.find(BB);
747 if (I != BBMap.end()) {
748 for (LoopT *L = I->second; L; L = L->getParentLoop())
749 L->removeBlockFromLoop(BB);
757 static bool isNotAlreadyContainedIn(const LoopT *SubLoop,
758 const LoopT *ParentLoop) {
759 if (SubLoop == 0) return true;
760 if (SubLoop == ParentLoop) return false;
761 return isNotAlreadyContainedIn(SubLoop->getParentLoop(), ParentLoop);
764 void Calculate(DominatorTreeBase<BlockT> &DT) {
765 BlockT *RootNode = DT.getRootNode()->getBlock();
767 for (df_iterator<BlockT*> NI = df_begin(RootNode),
768 NE = df_end(RootNode); NI != NE; ++NI)
769 if (LoopT *L = ConsiderForLoop(*NI, DT))
770 TopLevelLoops.push_back(L);
773 LoopT *ConsiderForLoop(BlockT *BB, DominatorTreeBase<BlockT> &DT) {
774 if (BBMap.find(BB) != BBMap.end()) return 0;// Haven't processed this node?
776 std::vector<BlockT *> TodoStack;
778 // Scan the predecessors of BB, checking to see if BB dominates any of
779 // them. This identifies backedges which target this node...
780 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
781 for (typename InvBlockTraits::ChildIteratorType I =
782 InvBlockTraits::child_begin(BB), E = InvBlockTraits::child_end(BB);
784 typename InvBlockTraits::NodeType *N = *I;
785 if (DT.dominates(BB, N)) // If BB dominates its predecessor...
786 TodoStack.push_back(N);
789 if (TodoStack.empty()) return 0; // No backedges to this block...
791 // Create a new loop to represent this basic block...
792 LoopT *L = new LoopT(BB);
795 BlockT *EntryBlock = BB->getParent()->begin();
797 while (!TodoStack.empty()) { // Process all the nodes in the loop
798 BlockT *X = TodoStack.back();
799 TodoStack.pop_back();
801 if (!L->contains(X) && // As of yet unprocessed??
802 DT.dominates(EntryBlock, X)) { // X is reachable from entry block?
803 // Check to see if this block already belongs to a loop. If this occurs
804 // then we have a case where a loop that is supposed to be a child of
805 // the current loop was processed before the current loop. When this
806 // occurs, this child loop gets added to a part of the current loop,
807 // making it a sibling to the current loop. We have to reparent this
810 const_cast<LoopT *>(getLoopFor(X)))
811 if (SubLoop->getHeader() == X && isNotAlreadyContainedIn(SubLoop, L)){
812 // Remove the subloop from its current parent...
813 assert(SubLoop->ParentLoop && SubLoop->ParentLoop != L);
814 LoopT *SLP = SubLoop->ParentLoop; // SubLoopParent
815 typename std::vector<LoopT *>::iterator I =
816 std::find(SLP->SubLoops.begin(), SLP->SubLoops.end(), SubLoop);
817 assert(I != SLP->SubLoops.end() &&"SubLoop not a child of parent?");
818 SLP->SubLoops.erase(I); // Remove from parent...
820 // Add the subloop to THIS loop...
821 SubLoop->ParentLoop = L;
822 L->SubLoops.push_back(SubLoop);
825 // Normal case, add the block to our loop...
826 L->Blocks.push_back(X);
828 typedef GraphTraits<Inverse<BlockT*> > InvBlockTraits;
830 // Add all of the predecessors of X to the end of the work stack...
831 TodoStack.insert(TodoStack.end(), InvBlockTraits::child_begin(X),
832 InvBlockTraits::child_end(X));
836 // If there are any loops nested within this loop, create them now!
837 for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
838 E = L->Blocks.end(); I != E; ++I)
839 if (LoopT *NewLoop = ConsiderForLoop(*I, DT)) {
840 L->SubLoops.push_back(NewLoop);
841 NewLoop->ParentLoop = L;
844 // Add the basic blocks that comprise this loop to the BBMap so that this
845 // loop can be found for them.
847 for (typename std::vector<BlockT*>::iterator I = L->Blocks.begin(),
848 E = L->Blocks.end(); I != E; ++I)
849 BBMap.insert(std::make_pair(*I, L));
851 // Now that we have a list of all of the child loops of this loop, check to
852 // see if any of them should actually be nested inside of each other. We
853 // can accidentally pull loops our of their parents, so we must make sure to
854 // organize the loop nests correctly now.
856 std::map<BlockT *, LoopT *> ContainingLoops;
857 for (unsigned i = 0; i != L->SubLoops.size(); ++i) {
858 LoopT *Child = L->SubLoops[i];
859 assert(Child->getParentLoop() == L && "Not proper child loop?");
861 if (LoopT *ContainingLoop = 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 LoopT *&BlockLoop = ContainingLoops[Child->Blocks[b]];
872 if (BlockLoop == 0) { // Child block not processed yet...
874 } else if (BlockLoop != Child) {
875 LoopT *SubLoop = BlockLoop;
876 // Reparent all of the blocks which used to belong to BlockLoops
877 for (unsigned j = 0, f = SubLoop->Blocks.size(); j != f; ++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(LoopT *NewChild,
898 LoopT *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<LoopT *>::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(LoopT *L, LoopT *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(raw_ostream &OS) const {
937 for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
938 TopLevelLoops[i]->print(OS);
940 for (DenseMap<BasicBlock*, LoopT*>::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, Loop> LI;
950 friend class LoopBase<BasicBlock, Loop>;
952 void operator=(const LoopInfo &); // do not implement
953 LoopInfo(const LoopInfo &); // do not implement
955 static char ID; // Pass identification, replacement for typeid
957 LoopInfo() : FunctionPass(ID) {
958 initializeLoopInfoPass(*PassRegistry::getPassRegistry());
961 LoopInfoBase<BasicBlock, Loop>& getBase() { return LI; }
963 /// iterator/begin/end - The interface to the top-level loops in the current
966 typedef LoopInfoBase<BasicBlock, Loop>::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 verifyAnalysis() const;
1002 virtual void releaseMemory() { LI.releaseMemory(); }
1004 virtual void print(raw_ostream &O, const Module* M = 0) const;
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) {
1039 /// updateUnloop - Update LoopInfo after removing the last backedge from a
1040 /// loop--now the "unloop". This updates the loop forest and parent loops for
1041 /// each block so that Unloop is no longer referenced, but the caller must
1042 /// actually delete the Unloop object.
1043 void updateUnloop(Loop *Unloop);
1045 /// replacementPreservesLCSSAForm - Returns true if replacing From with To
1046 /// everywhere is guaranteed to preserve LCSSA form.
1047 bool replacementPreservesLCSSAForm(Instruction *From, Value *To) {
1048 // Preserving LCSSA form is only problematic if the replacing value is an
1050 Instruction *I = dyn_cast<Instruction>(To);
1051 if (!I) return true;
1052 // If both instructions are defined in the same basic block then replacement
1053 // cannot break LCSSA form.
1054 if (I->getParent() == From->getParent())
1056 // If the instruction is not defined in a loop then it can safely replace
1058 Loop *ToLoop = getLoopFor(I->getParent());
1059 if (!ToLoop) return true;
1060 // If the replacing instruction is defined in the same loop as the original
1061 // instruction, or in a loop that contains it as an inner loop, then using
1062 // it as a replacement will not break LCSSA form.
1063 return ToLoop->contains(getLoopFor(From->getParent()));
1068 // Allow clients to walk the list of nested loops...
1069 template <> struct GraphTraits<const Loop*> {
1070 typedef const Loop NodeType;
1071 typedef LoopInfo::iterator ChildIteratorType;
1073 static NodeType *getEntryNode(const Loop *L) { return L; }
1074 static inline ChildIteratorType child_begin(NodeType *N) {
1077 static inline ChildIteratorType child_end(NodeType *N) {
1082 template <> struct GraphTraits<Loop*> {
1083 typedef Loop NodeType;
1084 typedef LoopInfo::iterator ChildIteratorType;
1086 static NodeType *getEntryNode(Loop *L) { return L; }
1087 static inline ChildIteratorType child_begin(NodeType *N) {
1090 static inline ChildIteratorType child_end(NodeType *N) {
1095 template<class BlockT, class LoopT>
1097 LoopBase<BlockT, LoopT>::addBasicBlockToLoop(BlockT *NewBB,
1098 LoopInfoBase<BlockT, LoopT> &LIB) {
1099 assert((Blocks.empty() || LIB[getHeader()] == this) &&
1100 "Incorrect LI specified for this loop!");
1101 assert(NewBB && "Cannot add a null basic block to the loop!");
1102 assert(LIB[NewBB] == 0 && "BasicBlock already in the loop!");
1104 LoopT *L = static_cast<LoopT *>(this);
1106 // Add the loop mapping to the LoopInfo object...
1107 LIB.BBMap[NewBB] = L;
1109 // Add the basic block to this loop and all parent loops...
1111 L->Blocks.push_back(NewBB);
1112 L = L->getParentLoop();
1116 } // End llvm namespace