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/GraphTraits.h"
37 #include "llvm/ADT/SmallPtrSet.h"
38 #include "llvm/ADT/SmallVector.h"
39 #include "llvm/Support/CFG.h"
40 #include "llvm/Support/Streams.h"
45 static void RemoveFromVector(std::vector<T*> &V, T *N) {
46 typename std::vector<T*>::iterator I = std::find(V.begin(), V.end(), N);
47 assert(I != V.end() && "N is not in this list!");
58 //===----------------------------------------------------------------------===//
59 /// LoopBase class - Instances of this class are used to represent loops that are
60 /// detected in the flow graph
62 template<class BlockT>
64 LoopBase<BlockT> *ParentLoop;
65 std::vector<LoopBase<BlockT>*> SubLoops; // Loops contained entirely within this one
66 std::vector<BlockT*> Blocks; // First entry is the header node
68 LoopBase(const LoopBase<BlockT> &); // DO NOT IMPLEMENT
69 const LoopBase<BlockT> &operator=(const LoopBase<BlockT> &); // DO NOT IMPLEMENT
71 /// Loop ctor - This creates an empty loop.
72 LoopBase() : ParentLoop(0) {}
74 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
78 unsigned getLoopDepth() const {
80 for (const LoopBase<BlockT> *CurLoop = this; CurLoop;
81 CurLoop = CurLoop->ParentLoop)
85 BlockT *getHeader() const { return Blocks.front(); }
86 LoopBase<BlockT> *getParentLoop() const { return ParentLoop; }
88 /// contains - Return true of the specified basic block is in this loop
90 bool contains(const BlockT *BB) const {
91 return std::find(Blocks.begin(), Blocks.end(), BB) != Blocks.end();
94 /// iterator/begin/end - Return the loops contained entirely within this loop.
96 const std::vector<LoopBase<BlockT>*> &getSubLoops() const { return SubLoops; }
97 typedef typename std::vector<LoopBase<BlockT>*>::const_iterator iterator;
98 iterator begin() const { return SubLoops.begin(); }
99 iterator end() const { return SubLoops.end(); }
100 bool empty() const { return SubLoops.empty(); }
102 /// getBlocks - Get a list of the basic blocks which make up this loop.
104 const std::vector<BlockT*> &getBlocks() const { return Blocks; }
105 typedef typename std::vector<BlockT*>::const_iterator block_iterator;
106 block_iterator block_begin() const { return Blocks.begin(); }
107 block_iterator block_end() const { return Blocks.end(); }
109 /// isLoopExit - True if terminator in the block can branch to another block
110 /// that is outside of the current loop.
112 bool isLoopExit(const BlockT *BB) const {
113 for (succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
121 /// getNumBackEdges - Calculate the number of back edges to the loop header
123 unsigned getNumBackEdges() const {
124 unsigned NumBackEdges = 0;
125 BlockT *H = getHeader();
127 for (pred_iterator I = pred_begin(H), E = pred_end(H); I != E; ++I)
134 /// isLoopInvariant - Return true if the specified value is loop invariant
136 bool isLoopInvariant(Value *V) const {
137 if (Instruction *I = dyn_cast<Instruction>(V))
138 return !contains(I->getParent());
139 return true; // All non-instructions are loop invariant
142 //===--------------------------------------------------------------------===//
143 // APIs for simple analysis of the loop.
145 // Note that all of these methods can fail on general loops (ie, there may not
146 // be a preheader, etc). For best success, the loop simplification and
147 // induction variable canonicalization pass should be used to normalize loops
148 // for easy analysis. These methods assume canonical loops.
150 /// getExitingBlocks - Return all blocks inside the loop that have successors
151 /// outside of the loop. These are the blocks _inside of the current loop_
152 /// which branch out. The returned list is always unique.
154 void getExitingBlocks(SmallVectorImpl<BlockT *> &ExitingBlocks) const {
155 // Sort the blocks vector so that we can use binary search to do quick
157 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
158 std::sort(LoopBBs.begin(), LoopBBs.end());
160 for (typename std::vector<BlockT*>::const_iterator BI = Blocks.begin(),
161 BE = Blocks.end(); BI != BE; ++BI)
162 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I)
163 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I)) {
164 // Not in current loop? It must be an exit block.
165 ExitingBlocks.push_back(*BI);
170 /// getExitBlocks - Return all of the successor blocks of this loop. These
171 /// are the blocks _outside of the current loop_ which are branched to.
173 void getExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
174 // Sort the blocks vector so that we can use binary search to do quick
176 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
177 std::sort(LoopBBs.begin(), LoopBBs.end());
179 for (typename std::vector<BlockT*>::const_iterator BI = Blocks.begin(),
180 BE = Blocks.end(); BI != BE; ++BI)
181 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I)
182 if (!std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
183 // Not in current loop? It must be an exit block.
184 ExitBlocks.push_back(*I);
187 /// getUniqueExitBlocks - Return all unique successor blocks of this loop.
188 /// These are the blocks _outside of the current loop_ which are branched to.
189 /// This assumes that loop is in canonical form.
191 void getUniqueExitBlocks(SmallVectorImpl<BlockT*> &ExitBlocks) const {
192 // Sort the blocks vector so that we can use binary search to do quick
194 SmallVector<BlockT*, 128> LoopBBs(block_begin(), block_end());
195 std::sort(LoopBBs.begin(), LoopBBs.end());
197 std::vector<BlockT*> switchExitBlocks;
199 for (typename std::vector<BlockT*>::const_iterator BI = Blocks.begin(),
200 BE = Blocks.end(); BI != BE; ++BI) {
202 BlockT *current = *BI;
203 switchExitBlocks.clear();
205 for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I) {
206 if (std::binary_search(LoopBBs.begin(), LoopBBs.end(), *I))
207 // If block is inside the loop then it is not a exit block.
210 pred_iterator PI = pred_begin(*I);
211 BlockT *firstPred = *PI;
213 // If current basic block is this exit block's first predecessor
214 // then only insert exit block in to the output ExitBlocks vector.
215 // This ensures that same exit block is not inserted twice into
216 // ExitBlocks vector.
217 if (current != firstPred)
220 // If a terminator has more then two successors, for example SwitchInst,
221 // then it is possible that there are multiple edges from current block
222 // to one exit block.
223 if (current->getTerminator()->getNumSuccessors() <= 2) {
224 ExitBlocks.push_back(*I);
228 // In case of multiple edges from current block to exit block, collect
229 // only one edge in ExitBlocks. Use switchExitBlocks to keep track of
231 if (std::find(switchExitBlocks.begin(), switchExitBlocks.end(), *I)
232 == switchExitBlocks.end()) {
233 switchExitBlocks.push_back(*I);
234 ExitBlocks.push_back(*I);
240 /// getLoopPreheader - If there is a preheader for this loop, return it. A
241 /// loop has a preheader if there is only one edge to the header of the loop
242 /// from outside of the loop. If this is the case, the block branching to the
243 /// header of the loop is the preheader node.
245 /// This method returns null if there is no preheader for the loop.
247 BlockT *getLoopPreheader() const {
248 // Keep track of nodes outside the loop branching to the header...
251 // Loop over the predecessors of the header node...
252 BlockT *Header = getHeader();
253 for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
255 if (!contains(*PI)) { // If the block is not in the loop...
256 if (Out && Out != *PI)
257 return 0; // Multiple predecessors outside the loop
261 // Make sure there is only one exit out of the preheader.
262 assert(Out && "Header of loop has no predecessors from outside loop?");
263 succ_iterator SI = succ_begin(Out);
265 if (SI != succ_end(Out))
266 return 0; // Multiple exits from the block, must not be a preheader.
268 // If there is exactly one preheader, return it. If there was zero, then Out
273 /// getLoopLatch - If there is a latch block for this loop, return it. A
274 /// latch block is the canonical backedge for a loop. A loop header in normal
275 /// form has two edges into it: one from a preheader and one from a latch
277 BlockT *getLoopLatch() const {
278 BlockT *Header = getHeader();
279 pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
280 if (PI == PE) return 0; // no preds?
286 if (PI == PE) return 0; // only one pred?
289 if (Latch) return 0; // multiple backedges
293 if (PI != PE) return 0; // more than two preds
298 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
299 /// induction variable: an integer recurrence that starts at 0 and increments
300 /// by one each time through the loop. If so, return the phi node that
301 /// corresponds to it.
303 PHINode *getCanonicalInductionVariable() const {
304 BlockT *H = getHeader();
306 BlockT *Incoming = 0, *Backedge = 0;
307 pred_iterator PI = pred_begin(H);
308 assert(PI != pred_end(H) && "Loop must have at least one backedge!");
310 if (PI == pred_end(H)) return 0; // dead loop
312 if (PI != pred_end(H)) return 0; // multiple backedges?
314 if (contains(Incoming)) {
315 if (contains(Backedge))
317 std::swap(Incoming, Backedge);
318 } else if (!contains(Backedge))
321 // Loop over all of the PHI nodes, looking for a canonical indvar.
322 for (typename BlockT::iterator I = H->begin(); isa<PHINode>(I); ++I) {
323 PHINode *PN = cast<PHINode>(I);
324 if (Instruction *Inc =
325 dyn_cast<Instruction>(PN->getIncomingValueForBlock(Backedge)))
326 if (Inc->getOpcode() == Instruction::Add && Inc->getOperand(0) == PN)
327 if (ConstantInt *CI = dyn_cast<ConstantInt>(Inc->getOperand(1)))
328 if (CI->equalsInt(1))
334 /// getCanonicalInductionVariableIncrement - Return the LLVM value that holds
335 /// the canonical induction variable value for the "next" iteration of the
336 /// loop. This always succeeds if getCanonicalInductionVariable succeeds.
338 Instruction *getCanonicalInductionVariableIncrement() const {
339 if (PHINode *PN = getCanonicalInductionVariable()) {
340 bool P1InLoop = contains(PN->getIncomingBlock(1));
341 return cast<Instruction>(PN->getIncomingValue(P1InLoop));
346 /// getTripCount - Return a loop-invariant LLVM value indicating the number of
347 /// times the loop will be executed. Note that this means that the backedge
348 /// of the loop executes N-1 times. If the trip-count cannot be determined,
349 /// this returns null.
351 Value *getTripCount() const {
352 // Canonical loops will end with a 'cmp ne I, V', where I is the incremented
353 // canonical induction variable and V is the trip count of the loop.
354 Instruction *Inc = getCanonicalInductionVariableIncrement();
355 if (Inc == 0) return 0;
356 PHINode *IV = cast<PHINode>(Inc->getOperand(0));
358 BlockT *BackedgeBlock =
359 IV->getIncomingBlock(contains(IV->getIncomingBlock(1)));
361 if (BranchInst *BI = dyn_cast<BranchInst>(BackedgeBlock->getTerminator()))
362 if (BI->isConditional()) {
363 if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
364 if (ICI->getOperand(0) == Inc)
365 if (BI->getSuccessor(0) == getHeader()) {
366 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
367 return ICI->getOperand(1);
368 } else if (ICI->getPredicate() == ICmpInst::ICMP_EQ) {
369 return ICI->getOperand(1);
377 /// isLCSSAForm - Return true if the Loop is in LCSSA form
378 bool isLCSSAForm() const {
379 // Sort the blocks vector so that we can use binary search to do quick
381 SmallPtrSet<BlockT*, 16> LoopBBs(block_begin(), block_end());
383 for (block_iterator BI = block_begin(), E = block_end(); BI != E; ++BI) {
385 for (typename BlockT::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
386 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
388 BlockT *UserBB = cast<Instruction>(*UI)->getParent();
389 if (PHINode *P = dyn_cast<PHINode>(*UI)) {
390 unsigned OperandNo = UI.getOperandNo();
391 UserBB = P->getIncomingBlock(OperandNo/2);
394 // Check the current block, as a fast-path. Most values are used in the
395 // same block they are defined in.
396 if (UserBB != BB && !LoopBBs.count(UserBB))
404 //===--------------------------------------------------------------------===//
405 // APIs for updating loop information after changing the CFG
408 /// addBasicBlockToLoop - This method is used by other analyses to update loop
409 /// information. NewBB is set to be a new member of the current loop.
410 /// Because of this, it is added as a member of all parent loops, and is added
411 /// to the specified LoopInfo object as being in the current basic block. It
412 /// is not valid to replace the loop header with this method.
414 void addBasicBlockToLoop(BlockT *NewBB, LoopInfo &LI);
416 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
417 /// the OldChild entry in our children list with NewChild, and updates the
418 /// parent pointer of OldChild to be null and the NewChild to be this loop.
419 /// This updates the loop depth of the new child.
420 void replaceChildLoopWith(LoopBase<BlockT> *OldChild,
421 LoopBase<BlockT> *NewChild) {
422 assert(OldChild->ParentLoop == this && "This loop is already broken!");
423 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
424 typename std::vector<LoopBase<BlockT>*>::iterator I =
425 std::find(SubLoops.begin(), SubLoops.end(), OldChild);
426 assert(I != SubLoops.end() && "OldChild not in loop!");
428 OldChild->ParentLoop = 0;
429 NewChild->ParentLoop = this;
432 /// addChildLoop - Add the specified loop to be a child of this loop. This
433 /// updates the loop depth of the new child.
435 void addChildLoop(LoopBase<BlockT> *NewChild) {
436 assert(NewChild->ParentLoop == 0 && "NewChild already has a parent!");
437 NewChild->ParentLoop = this;
438 SubLoops.push_back(NewChild);
441 /// removeChildLoop - This removes the specified child from being a subloop of
442 /// this loop. The loop is not deleted, as it will presumably be inserted
443 /// into another loop.
444 LoopBase<BlockT> *removeChildLoop(iterator I) {
445 assert(I != SubLoops.end() && "Cannot remove end iterator!");
446 LoopBase<BlockT> *Child = *I;
447 assert(Child->ParentLoop == this && "Child is not a child of this loop!");
448 SubLoops.erase(SubLoops.begin()+(I-begin()));
449 Child->ParentLoop = 0;
453 /// addBlockEntry - This adds a basic block directly to the basic block list.
454 /// This should only be used by transformations that create new loops. Other
455 /// transformations should use addBasicBlockToLoop.
456 void addBlockEntry(BlockT *BB) {
457 Blocks.push_back(BB);
460 /// moveToHeader - This method is used to move BB (which must be part of this
461 /// loop) to be the loop header of the loop (the block that dominates all
463 void moveToHeader(BlockT *BB) {
464 if (Blocks[0] == BB) return;
465 for (unsigned i = 0; ; ++i) {
466 assert(i != Blocks.size() && "Loop does not contain BB!");
467 if (Blocks[i] == BB) {
468 Blocks[i] = Blocks[0];
475 /// removeBlockFromLoop - This removes the specified basic block from the
476 /// current loop, updating the Blocks as appropriate. This does not update
477 /// the mapping in the LoopInfo class.
478 void removeBlockFromLoop(BlockT *BB) {
479 RemoveFromVector(Blocks, BB);
482 /// verifyLoop - Verify loop structure
483 void verifyLoop() const {
485 assert (getHeader() && "Loop header is missing");
486 assert (getLoopPreheader() && "Loop preheader is missing");
487 assert (getLoopLatch() && "Loop latch is missing");
488 for (typename std::vector<LoopBase<BlockT>*>::const_iterator I =
489 SubLoops.begin(), E = SubLoops.end(); I != E; ++I)
494 void print(std::ostream &OS, unsigned Depth = 0) const {
495 OS << std::string(Depth*2, ' ') << "Loop Containing: ";
497 for (unsigned i = 0; i < getBlocks().size(); ++i) {
499 WriteAsOperand(OS, getBlocks()[i], false);
503 for (iterator I = begin(), E = end(); I != E; ++I)
504 (*I)->print(OS, Depth+2);
507 void print(std::ostream *O, unsigned Depth = 0) const {
508 if (O) print(*O, Depth);
516 friend class LoopInfo;
517 LoopBase(BlockT *BB) : ParentLoop(0) {
518 Blocks.push_back(BB);
522 typedef LoopBase<BasicBlock> Loop;
525 //===----------------------------------------------------------------------===//
526 /// LoopInfo - This class builds and contains all of the top level loop
527 /// structures in the specified function.
529 class LoopInfo : public FunctionPass {
530 // BBMap - Mapping of basic blocks to the inner most loop they occur in
531 std::map<BasicBlock*, Loop*> BBMap;
532 std::vector<Loop*> TopLevelLoops;
533 friend class LoopBase<BasicBlock>;
535 static char ID; // Pass identification, replacement for typeid
537 LoopInfo() : FunctionPass(intptr_t(&ID)) {}
538 ~LoopInfo() { releaseMemory(); }
540 /// iterator/begin/end - The interface to the top-level loops in the current
543 typedef std::vector<Loop*>::const_iterator iterator;
544 iterator begin() const { return TopLevelLoops.begin(); }
545 iterator end() const { return TopLevelLoops.end(); }
547 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
548 /// block is in no loop (for example the entry node), null is returned.
550 Loop *getLoopFor(const BasicBlock *BB) const {
551 std::map<BasicBlock *, Loop*>::const_iterator I=
552 BBMap.find(const_cast<BasicBlock*>(BB));
553 return I != BBMap.end() ? I->second : 0;
556 /// operator[] - same as getLoopFor...
558 const Loop *operator[](const BasicBlock *BB) const {
559 return getLoopFor(BB);
562 /// getLoopDepth - Return the loop nesting level of the specified block...
564 unsigned getLoopDepth(const BasicBlock *BB) const {
565 const Loop *L = getLoopFor(BB);
566 return L ? L->getLoopDepth() : 0;
569 // isLoopHeader - True if the block is a loop header node
570 bool isLoopHeader(BasicBlock *BB) const {
571 const Loop *L = getLoopFor(BB);
572 return L && L->getHeader() == BB;
575 /// runOnFunction - Calculate the natural loop information.
577 virtual bool runOnFunction(Function &F);
579 virtual void releaseMemory();
581 void print(std::ostream &O, const Module* = 0) const;
582 void print(std::ostream *O, const Module* M = 0) const {
586 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
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 Loop *removeLoop(iterator I);
593 /// changeLoopFor - Change the top-level loop that contains BB to the
594 /// specified loop. This should be used by transformations that restructure
595 /// the loop hierarchy tree.
596 void changeLoopFor(BasicBlock *BB, Loop *L);
598 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
599 /// list with the indicated loop.
600 void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop);
602 /// addTopLevelLoop - This adds the specified loop to the collection of
604 void addTopLevelLoop(Loop *New) {
605 assert(New->getParentLoop() == 0 && "Loop already in subloop!");
606 TopLevelLoops.push_back(New);
609 /// removeBlock - This method completely removes BB from all data structures,
610 /// including all of the Loop objects it is nested in and our mapping from
611 /// BasicBlocks to loops.
612 void removeBlock(BasicBlock *BB);
615 void Calculate(DominatorTree &DT);
616 Loop *ConsiderForLoop(BasicBlock *BB, DominatorTree &DT);
617 void MoveSiblingLoopInto(Loop *NewChild, Loop *NewParent);
618 void InsertLoopInto(Loop *L, Loop *Parent);
622 // Allow clients to walk the list of nested loops...
623 template <> struct GraphTraits<const Loop*> {
624 typedef const Loop NodeType;
625 typedef std::vector<Loop*>::const_iterator ChildIteratorType;
627 static NodeType *getEntryNode(const Loop *L) { return L; }
628 static inline ChildIteratorType child_begin(NodeType *N) {
631 static inline ChildIteratorType child_end(NodeType *N) {
636 template <> struct GraphTraits<Loop*> {
637 typedef Loop NodeType;
638 typedef std::vector<Loop*>::const_iterator ChildIteratorType;
640 static NodeType *getEntryNode(Loop *L) { return L; }
641 static inline ChildIteratorType child_begin(NodeType *N) {
644 static inline ChildIteratorType child_end(NodeType *N) {
649 template<class BlockT>
650 void LoopBase<BlockT>::addBasicBlockToLoop(BlockT *NewBB, LoopInfo &LI) {
651 assert((Blocks.empty() || LI[getHeader()] == this) &&
652 "Incorrect LI specified for this loop!");
653 assert(NewBB && "Cannot add a null basic block to the loop!");
654 assert(LI[NewBB] == 0 && "BasicBlock already in the loop!");
656 // Add the loop mapping to the LoopInfo object...
657 LI.BBMap[NewBB] = this;
659 // Add the basic block to this loop and all parent loops...
660 LoopBase<BlockT> *L = this;
662 L->Blocks.push_back(NewBB);
663 L = L->getParentLoop();
667 } // End llvm namespace
669 // Make sure that any clients of this file link in LoopInfo.cpp
670 FORCE_DEFINING_FILE_TO_BE_LINKED(LoopInfo)