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 the
5 // loops identified may actually be several natural loops that share the same
6 // header node... not just a single natural loop.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
11 #define LLVM_ANALYSIS_LOOP_INFO_H
13 #include "llvm/Pass.h"
19 //===----------------------------------------------------------------------===//
20 /// Loop class - Instances of this class are used to represent loops that are
21 /// detected in the flow graph
25 std::vector<BasicBlock *> Blocks; // First entry is the header node
26 std::vector<Loop*> SubLoops; // Loops contained entirely within this one
27 unsigned LoopDepth; // Nesting depth of this loop
29 Loop(const Loop &); // DO NOT IMPLEMENT
30 const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
33 inline unsigned getLoopDepth() const { return LoopDepth; }
34 inline BasicBlock *getHeader() const { return Blocks.front(); }
35 inline Loop *getParentLoop() const { return ParentLoop; }
37 /// contains - Return true of the specified basic block is in this loop
38 bool contains(const BasicBlock *BB) const;
40 /// getSubLoops - Return the loops contained entirely within this loop
42 inline const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
43 inline const std::vector<BasicBlock*> &getBlocks() const { return Blocks; }
45 /// isLoopExit - True if terminator in the block can branch to another block
46 /// that is outside of the current loop.
47 bool isLoopExit(const BasicBlock *BB) const;
49 /// Find number of back edges
50 unsigned getNumBackEdges() const;
52 /// getLoopPreheader - If there is a preheader for this loop, return it. A
53 /// loop has a preheader if there is only one edge to the header of the loop
54 /// from outside of the loop. If this is the case, the block branching to the
55 /// header of the loop is the preheader node. The "preheaders" pass can be
56 /// "Required" to ensure that there is always a preheader node for every loop.
58 /// This method returns null if there is no preheader for the loop (either
59 /// because the loop is dead or because multiple blocks branch to the header
60 /// node of this loop).
62 BasicBlock *getLoopPreheader() const;
64 /// addBasicBlockToLoop - This function is used by other analyses to update
65 /// loop information. NewBB is set to be a new member of the current loop.
66 /// Because of this, it is added as a member of all parent loops, and is added
67 /// to the specified LoopInfo object as being in the current basic block. It
68 /// is not valid to replace the loop header with this method.
70 void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI);
72 void print(std::ostream &O) const;
74 friend class LoopInfo;
75 inline Loop(BasicBlock *BB) : ParentLoop(0) {
76 Blocks.push_back(BB); LoopDepth = 0;
79 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
83 void setLoopDepth(unsigned Level) {
85 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
86 SubLoops[i]->setLoopDepth(Level+1);
92 //===----------------------------------------------------------------------===//
93 /// LoopInfo - This class builds and contains all of the top level loop
94 /// structures in the specified function.
96 class LoopInfo : public FunctionPass {
97 // BBMap - Mapping of basic blocks to the inner most loop they occur in
98 std::map<BasicBlock*, Loop*> BBMap;
99 std::vector<Loop*> TopLevelLoops;
102 ~LoopInfo() { releaseMemory(); }
104 const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
106 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
107 /// block is in no loop (for example the entry node), null is returned.
109 const Loop *getLoopFor(const BasicBlock *BB) const {
110 std::map<BasicBlock *, Loop*>::const_iterator I=BBMap.find((BasicBlock*)BB);
111 return I != BBMap.end() ? I->second : 0;
114 /// operator[] - same as getLoopFor...
116 inline const Loop *operator[](const BasicBlock *BB) const {
117 return getLoopFor(BB);
120 /// getLoopDepth - Return the loop nesting level of the specified block...
122 unsigned getLoopDepth(const BasicBlock *BB) const {
123 const Loop *L = getLoopFor(BB);
124 return L ? L->getLoopDepth() : 0;
128 // isLoopHeader - True if the block is a loop header node
129 bool isLoopHeader(BasicBlock *BB) const {
130 return getLoopFor(BB)->getHeader() == BB;
132 // isLoopEnd - True if block jumps to loop entry
133 bool isLoopEnd(BasicBlock *BB) const;
136 /// runOnFunction - Calculate the natural loop information.
138 virtual bool runOnFunction(Function &F);
140 virtual void releaseMemory();
141 void print(std::ostream &O) const;
143 /// getAnalysisUsage - Requires dominator sets
145 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
147 static void stub(); // Noop
149 void Calculate(const DominatorSet &DS);
150 Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
154 // Make sure that any clients of this file link in LoopInfo.cpp
156 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);