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 function, the basic blocks the make up the
17 // loop, the nesting depth of the loop, and the successor blocks of the loop.
19 // It can calculate on the fly a variety of different bits of information, such
20 // as whether there is a preheader for the loop, the number of back edges to the
21 // header, and whether or not a particular block branches out of the loop.
23 //===----------------------------------------------------------------------===//
25 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
26 #define LLVM_ANALYSIS_LOOP_INFO_H
28 #include "llvm/Pass.h"
29 #include "Support/GraphTraits.h"
37 //===----------------------------------------------------------------------===//
38 /// Loop class - Instances of this class are used to represent loops that are
39 /// detected in the flow graph
43 std::vector<Loop*> SubLoops; // Loops contained entirely within this one
44 std::vector<BasicBlock*> Blocks; // First entry is the header node
45 std::vector<BasicBlock*> ExitBlocks; // Reachable blocks outside the loop
46 unsigned LoopDepth; // Nesting depth of this loop
48 Loop(const Loop &); // DO NOT IMPLEMENT
49 const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
51 /// Loop ctor - This creates an empty loop.
52 Loop() : ParentLoop(0), LoopDepth(0) {
55 unsigned getLoopDepth() const { return LoopDepth; }
56 BasicBlock *getHeader() const { return Blocks.front(); }
57 Loop *getParentLoop() const { return ParentLoop; }
59 /// contains - Return true of the specified basic block is in this loop
61 bool contains(const BasicBlock *BB) const;
63 /// iterator/begin/end - Return the loops contained entirely within this loop.
65 typedef std::vector<Loop*>::const_iterator iterator;
66 iterator begin() const { return SubLoops.begin(); }
67 iterator end() const { return SubLoops.end(); }
69 /// getBlocks - Get a list of the basic blocks which make up this loop.
71 const std::vector<BasicBlock*> &getBlocks() const { return Blocks; }
73 /// getExitBlocks - Return all of the successor blocks of this loop. These
74 /// are the blocks _outside of the current loop_ which are branched to.
76 const std::vector<BasicBlock*> &getExitBlocks() const { return ExitBlocks; }
78 /// isLoopExit - True if terminator in the block can branch to another block
79 /// that is outside of the current loop. The reached block should be in the
82 bool isLoopExit(const BasicBlock *BB) const;
84 /// getNumBackEdges - Calculate the number of back edges to the loop header
86 unsigned getNumBackEdges() const;
88 /// hasExitBlock - Return true if the current loop has the specified block as
90 bool hasExitBlock(BasicBlock *BB) const {
91 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
92 if (ExitBlocks[i] == BB)
97 /// getLoopPreheader - If there is a preheader for this loop, return it. A
98 /// loop has a preheader if there is only one edge to the header of the loop
99 /// from outside of the loop. If this is the case, the block branching to the
100 /// header of the loop is the preheader node. The "preheaders" pass can be
101 /// "Required" to ensure that there is always a preheader node for every loop.
103 /// This method returns null if there is no preheader for the loop (either
104 /// because the loop is dead or because multiple blocks branch to the header
105 /// node of this loop).
107 BasicBlock *getLoopPreheader() const;
109 /// addBasicBlockToLoop - This method is used by other analyses to update loop
110 /// information. NewBB is set to be a new member of the current loop.
111 /// Because of this, it is added as a member of all parent loops, and is added
112 /// to the specified LoopInfo object as being in the current basic block. It
113 /// is not valid to replace the loop header with this method.
115 void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI);
117 /// changeExitBlock - This method is used to update loop information. All
118 /// instances of the specified Old basic block are removed from the exit list
119 /// and replaced with New.
121 void changeExitBlock(BasicBlock *Old, BasicBlock *New);
123 /// replaceChildLoopWith - This is used when splitting loops up. It replaces
124 /// the OldChild entry in our children list with NewChild, and updates the
125 /// parent pointer of OldChild to be null and the NewChild to be this loop.
126 /// This updates the loop depth of the new child.
127 void replaceChildLoopWith(Loop *OldChild, Loop *NewChild);
129 /// addChildLoop - Add the specified loop to be a child of this loop. This
130 /// updates the loop depth of the new child.
132 void addChildLoop(Loop *NewChild);
134 /// removeChildLoop - This removes the specified child from being a subloop of
135 /// this loop. The loop is not deleted, as it will presumably be inserted
136 /// into another loop.
137 Loop *removeChildLoop(iterator OldChild);
139 /// addExitBlock - Add the specified exit block to the loop.
141 void addExitBlock(BasicBlock *BB) {
142 ExitBlocks.push_back(BB);
145 /// addBlockEntry - This adds a basic block directly to the basic block list.
146 /// This should only be used by transformations that create new loops. Other
147 /// transformations should use addBasicBlockToLoop.
148 void addBlockEntry(BasicBlock *BB) {
149 Blocks.push_back(BB);
152 /// removeBlockFromLoop - This removes the specified basic block from the
153 /// current loop, updating the Blocks and ExitBlocks lists as appropriate.
154 /// This does not update the mapping in the LoopInfo class.
155 void removeBlockFromLoop(BasicBlock *BB);
157 void print(std::ostream &O, unsigned Depth = 0) const;
160 friend class LoopInfo;
161 Loop(BasicBlock *BB) : ParentLoop(0) {
162 Blocks.push_back(BB); LoopDepth = 0;
165 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
169 void setLoopDepth(unsigned Level) {
171 for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
172 SubLoops[i]->setLoopDepth(Level+1);
178 //===----------------------------------------------------------------------===//
179 /// LoopInfo - This class builds and contains all of the top level loop
180 /// structures in the specified function.
182 class LoopInfo : public FunctionPass {
183 // BBMap - Mapping of basic blocks to the inner most loop they occur in
184 std::map<BasicBlock*, Loop*> BBMap;
185 std::vector<Loop*> TopLevelLoops;
188 ~LoopInfo() { releaseMemory(); }
190 /// iterator/begin/end - The interface to the top-level loops in the current
193 typedef std::vector<Loop*>::const_iterator iterator;
194 iterator begin() const { return TopLevelLoops.begin(); }
195 iterator end() const { return TopLevelLoops.end(); }
197 /// getLoopFor - Return the inner most loop that BB lives in. If a basic
198 /// block is in no loop (for example the entry node), null is returned.
200 const Loop *getLoopFor(const BasicBlock *BB) const {
201 std::map<BasicBlock *, Loop*>::const_iterator I=BBMap.find((BasicBlock*)BB);
202 return I != BBMap.end() ? I->second : 0;
205 /// operator[] - same as getLoopFor...
207 inline const Loop *operator[](const BasicBlock *BB) const {
208 return getLoopFor(BB);
211 /// getLoopDepth - Return the loop nesting level of the specified block...
213 unsigned getLoopDepth(const BasicBlock *BB) const {
214 const Loop *L = getLoopFor(BB);
215 return L ? L->getLoopDepth() : 0;
218 // isLoopHeader - True if the block is a loop header node
219 bool isLoopHeader(BasicBlock *BB) const {
220 return getLoopFor(BB)->getHeader() == BB;
223 /// runOnFunction - Calculate the natural loop information.
225 virtual bool runOnFunction(Function &F);
227 virtual void releaseMemory();
228 void print(std::ostream &O) const;
230 /// getAnalysisUsage - Requires dominator sets
232 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
234 /// changeLoopFor - Change the top-level loop that contains BB to the
235 /// specified loop. This should be used by transformations that restructure
236 /// the loop hierarchy tree.
237 void changeLoopFor(BasicBlock *BB, Loop *L);
239 /// changeTopLevelLoop - Replace the specified loop in the top-level loops
240 /// list with the indicated loop.
241 void changeTopLevelLoop(Loop *OldLoop, Loop *NewLoop);
243 static void stub(); // Noop
245 void Calculate(const DominatorSet &DS);
246 Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
247 void MoveSiblingLoopInto(Loop *NewChild, Loop *NewParent);
248 void InsertLoopInto(Loop *L, Loop *Parent);
252 // Make sure that any clients of this file link in LoopInfo.cpp
254 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);
256 // Allow clients to walk the list of nested loops...
257 template <> struct GraphTraits<const Loop*> {
258 typedef const Loop NodeType;
259 typedef std::vector<Loop*>::const_iterator ChildIteratorType;
261 static NodeType *getEntryNode(const Loop *L) { return L; }
262 static inline ChildIteratorType child_begin(NodeType *N) {
265 static inline ChildIteratorType child_end(NodeType *N) {
270 template <> struct GraphTraits<Loop*> {
271 typedef Loop NodeType;
272 typedef std::vector<Loop*>::const_iterator ChildIteratorType;
274 static NodeType *getEntryNode(Loop *L) { return L; }
275 static inline ChildIteratorType child_begin(NodeType *N) {
278 static inline ChildIteratorType child_end(NodeType *N) {
283 } // End llvm namespace