1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator --------*- C++ -*--=//
3 // This file defines the LoopInfo class that is used to identify natural loops
4 // and determine the loop depth of various nodes of the CFG. Note that natural
5 // loops may actually be several loops that share the same header node...
7 // This analysis calculates the nesting structure of loops in a function. For
8 // each natural loop identified, this analysis identifies natural loops
9 // contained entirely within the function, the basic blocks the make up the
10 // loop, the nesting depth of the loop, and the successor blocks of the loop.
12 // It can calculate on the fly a variety of different bits of information, such
13 // as whether there is a preheader for the loop, the number of back edges to the
14 // header, and whether or not a particular block branches out of the loop.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
19 #define LLVM_ANALYSIS_LOOP_INFO_H
21 #include "llvm/Pass.h"
27 //===----------------------------------------------------------------------===//
28 /// Loop class - Instances of this class are used to represent loops that are
29 /// detected in the flow graph
33 std::vector<Loop*> SubLoops; // Loops contained entirely within this one
34 std::vector<BasicBlock *> Blocks; // First entry is the header node
35 std::vector<BasicBlock *> ExitBlocks; // Reachable blocks outside the loop
36 unsigned LoopDepth; // Nesting depth of this loop
38 Loop(const Loop &); // DO NOT IMPLEMENT
39 const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
42 inline unsigned getLoopDepth() const { return LoopDepth; }
43 inline BasicBlock *getHeader() const { return Blocks.front(); }
44 inline Loop *getParentLoop() const { return ParentLoop; }
46 /// contains - Return true of the specified basic block is in this loop
47 bool contains(const BasicBlock *BB) const;
49 /// getSubLoops - Return the loops contained entirely within this loop
51 const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
53 /// getBlocks - Get a list of the basic blocks which make up this loop.
55 const std::vector<BasicBlock*> &getBlocks() const { return Blocks; }
57 /// getExitBlocks - Return all of the successor blocks of this loop. These
58 /// are the blocks _outside of the current loop_ which are branched to.
60 const std::vector<BasicBlock*> &getExitBlocks() const { return ExitBlocks; }
62 /// isLoopExit - True if terminator in the block can branch to another block
63 /// that is outside of the current loop. The reached block should be in the
66 bool isLoopExit(const BasicBlock *BB) const;
68 /// getNumBackEdges - Calculate the number of back edges to the loop header
70 unsigned getNumBackEdges() const;
72 /// getLoopPreheader - If there is a preheader for this loop, return it. A
73 /// loop has a preheader if there is only one edge to the header of the loop
74 /// from outside of the loop. If this is the case, the block branching to the
75 /// header of the loop is the preheader node. The "preheaders" pass can be
76 /// "Required" to ensure that there is always a preheader node for every loop.
78 /// This method returns null if there is no preheader for the loop (either
79 /// because the loop is dead or because multiple blocks branch to the header
80 /// node of this loop).
82 BasicBlock *getLoopPreheader() const;
84 /// addBasicBlockToLoop - This method is used by other analyses to update loop
85 /// information. NewBB is set to be a new member of the current loop.
86 /// Because of this, it is added as a member of all parent loops, and is added
87 /// to the specified LoopInfo object as being in the current basic block. It
88 /// is not valid to replace the loop header with this method.
90 void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI);
92 /// changeExitBlock - This method is used to update loop information. All
93 /// instances of the specified Old basic block are removed from the exit list
94 /// and replaced with New.
96 void changeExitBlock(BasicBlock *Old, BasicBlock *New);
98 void print(std::ostream &O) const;
100 friend class LoopInfo;
101 inline Loop(BasicBlock *BB) : ParentLoop(0) {
102 Blocks.push_back(BB); LoopDepth = 0;
105 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
109 void setLoopDepth(unsigned Level) {
111 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
112 SubLoops[i]->setLoopDepth(Level+1);
118 //===----------------------------------------------------------------------===//
119 /// LoopInfo - This class builds and contains all of the top level loop
120 /// structures in the specified function.
122 class LoopInfo : public FunctionPass {
123 // BBMap - Mapping of basic blocks to the inner most loop they occur in
124 std::map<BasicBlock*, Loop*> BBMap;
125 std::vector<Loop*> TopLevelLoops;
128 ~LoopInfo() { releaseMemory(); }
130 const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
132 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
133 /// block is in no loop (for example the entry node), null is returned.
135 const Loop *getLoopFor(const BasicBlock *BB) const {
136 std::map<BasicBlock *, Loop*>::const_iterator I=BBMap.find((BasicBlock*)BB);
137 return I != BBMap.end() ? I->second : 0;
140 /// operator[] - same as getLoopFor...
142 inline const Loop *operator[](const BasicBlock *BB) const {
143 return getLoopFor(BB);
146 /// getLoopDepth - Return the loop nesting level of the specified block...
148 unsigned getLoopDepth(const BasicBlock *BB) const {
149 const Loop *L = getLoopFor(BB);
150 return L ? L->getLoopDepth() : 0;
153 // isLoopHeader - True if the block is a loop header node
154 bool isLoopHeader(BasicBlock *BB) const {
155 return getLoopFor(BB)->getHeader() == BB;
158 /// runOnFunction - Calculate the natural loop information.
160 virtual bool runOnFunction(Function &F);
162 virtual void releaseMemory();
163 void print(std::ostream &O) const;
165 /// getAnalysisUsage - Requires dominator sets
167 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
169 static void stub(); // Noop
171 void Calculate(const DominatorSet &DS);
172 Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
176 // Make sure that any clients of this file link in LoopInfo.cpp
178 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);