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(BasicBlock *BB) const;
49 /// getLoopPreheader - If there is a preheader for this loop, return it. A
50 /// loop has a preheader if there is only one edge to the header of the loop
51 /// from outside of the loop. If this is the case, the block branching to the
52 /// header of the loop is the preheader node. The "preheaders" pass can be
53 /// "Required" to ensure that there is always a preheader node for every loop.
55 /// This method returns null if there is no preheader for the loop (either
56 /// because the loop is dead or because multiple blocks branch to the header
57 /// node of this loop).
59 BasicBlock *getLoopPreheader() const;
61 /// addBasicBlockToLoop - This function is used by other analyses to update
62 /// loop information. NewBB is set to be a new member of the current loop.
63 /// Because of this, it is added as a member of all parent loops, and is added
64 /// to the specified LoopInfo object as being in the current basic block. It
65 /// is not valid to replace the loop header with this method.
67 void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI);
69 void print(std::ostream &O) const;
71 friend class LoopInfo;
72 inline Loop(BasicBlock *BB) { Blocks.push_back(BB); LoopDepth = 0; }
74 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
78 void setLoopDepth(unsigned Level) {
80 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
81 SubLoops[i]->setLoopDepth(Level+1);
87 //===----------------------------------------------------------------------===//
88 /// LoopInfo - This class builds and contains all of the top level loop
89 /// structures in the specified function.
91 class LoopInfo : public FunctionPass {
92 // BBMap - Mapping of basic blocks to the inner most loop they occur in
93 std::map<BasicBlock*, Loop*> BBMap;
94 std::vector<Loop*> TopLevelLoops;
97 ~LoopInfo() { releaseMemory(); }
99 const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
101 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
102 /// block is in no loop (for example the entry node), null is returned.
104 const Loop *getLoopFor(const BasicBlock *BB) const {
105 std::map<BasicBlock *, Loop*>::const_iterator I=BBMap.find((BasicBlock*)BB);
106 return I != BBMap.end() ? I->second : 0;
109 /// operator[] - same as getLoopFor...
111 inline const Loop *operator[](const BasicBlock *BB) const {
112 return getLoopFor(BB);
115 /// getLoopDepth - Return the loop nesting level of the specified block...
117 unsigned getLoopDepth(const BasicBlock *BB) const {
118 const Loop *L = getLoopFor(BB);
119 return L ? L->getLoopDepth() : 0;
123 // isLoopHeader - True if the block is a loop header node
124 bool isLoopHeader(BasicBlock *BB) const {
125 return getLoopFor(BB)->getHeader() == BB;
127 // isLoopEnd - True if block jumps to loop entry
128 bool isLoopEnd(BasicBlock *BB) const;
131 /// runOnFunction - Calculate the natural loop information.
133 virtual bool runOnFunction(Function &F);
135 virtual void releaseMemory();
136 void print(std::ostream &O) const;
138 /// getAnalysisUsage - Requires dominator sets
140 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
142 static void stub(); // Noop
144 void Calculate(const DominatorSet &DS);
145 Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
149 // Make sure that any clients of this file link in LoopInfo.cpp
151 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);