6a96834a1581caa66af6c5e36b1a5ce006fbc97c
[oota-llvm.git] / include / llvm / Analysis / LoopInfo.h
1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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...
13 //
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.
18 //
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.
22 //
23 //===----------------------------------------------------------------------===//
24
25 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
26 #define LLVM_ANALYSIS_LOOP_INFO_H
27
28 #include "llvm/Pass.h"
29 #include "Support/GraphTraits.h"
30 #include <set>
31
32 class DominatorSet;
33 class LoopInfo;
34
35 //===----------------------------------------------------------------------===//
36 /// Loop class - Instances of this class are used to represent loops that are 
37 /// detected in the flow graph 
38 ///
39 class Loop {
40   Loop *ParentLoop;
41   std::vector<Loop*> SubLoops;       // Loops contained entirely within this one
42   std::vector<BasicBlock*> Blocks;   // First entry is the header node
43   std::vector<BasicBlock*> ExitBlocks; // Reachable blocks outside the loop
44   unsigned LoopDepth;                // Nesting depth of this loop
45
46   Loop(const Loop &);                  // DO NOT IMPLEMENT
47   const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
48 public:
49
50   unsigned getLoopDepth() const { return LoopDepth; }
51   BasicBlock *getHeader() const { return Blocks.front(); }
52   Loop *getParentLoop() const { return ParentLoop; }
53
54   /// contains - Return true of the specified basic block is in this loop
55   bool contains(const BasicBlock *BB) const;
56
57   /// getSubLoops - Return the loops contained entirely within this loop
58   ///
59   const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
60
61   /// getBlocks - Get a list of the basic blocks which make up this loop.
62   ///
63   const std::vector<BasicBlock*> &getBlocks() const { return Blocks; }
64
65   /// getExitBlocks - Return all of the successor blocks of this loop.  These
66   /// are the blocks _outside of the current loop_ which are branched to.
67   ///
68   const std::vector<BasicBlock*> &getExitBlocks() const { return ExitBlocks; }
69
70   /// isLoopExit - True if terminator in the block can branch to another block
71   /// that is outside of the current loop.  The reached block should be in the
72   /// ExitBlocks list.
73   ///
74   bool isLoopExit(const BasicBlock *BB) const;
75
76   /// getNumBackEdges - Calculate the number of back edges to the loop header
77   ///
78   unsigned getNumBackEdges() const;
79
80   /// hasExitBlock - Return true if the current loop has the specified block as
81   /// an exit block...
82   bool hasExitBlock(BasicBlock *BB) const {
83     for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
84       if (ExitBlocks[i] == BB)
85         return true;
86     return false;
87   }
88
89   /// getLoopPreheader - If there is a preheader for this loop, return it.  A
90   /// loop has a preheader if there is only one edge to the header of the loop
91   /// from outside of the loop.  If this is the case, the block branching to the
92   /// header of the loop is the preheader node.  The "preheaders" pass can be
93   /// "Required" to ensure that there is always a preheader node for every loop.
94   ///
95   /// This method returns null if there is no preheader for the loop (either
96   /// because the loop is dead or because multiple blocks branch to the header
97   /// node of this loop).
98   ///
99   BasicBlock *getLoopPreheader() const;
100
101   /// addBasicBlockToLoop - This method is used by other analyses to update loop
102   /// information.  NewBB is set to be a new member of the current loop.
103   /// Because of this, it is added as a member of all parent loops, and is added
104   /// to the specified LoopInfo object as being in the current basic block.  It
105   /// is not valid to replace the loop header with this method.
106   ///
107   void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI);
108
109   /// changeExitBlock - This method is used to update loop information.  All
110   /// instances of the specified Old basic block are removed from the exit list
111   /// and replaced with New.
112   ///
113   void changeExitBlock(BasicBlock *Old, BasicBlock *New);
114
115   void print(std::ostream &O, unsigned Depth = 0) const;
116   void dump() const;
117 private:
118   friend class LoopInfo;
119   inline Loop(BasicBlock *BB) : ParentLoop(0) {
120     Blocks.push_back(BB); LoopDepth = 0;
121   }
122   ~Loop() {
123     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
124       delete SubLoops[i];
125   }
126   
127   void setLoopDepth(unsigned Level) {
128     LoopDepth = Level;
129     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
130       SubLoops[i]->setLoopDepth(Level+1);
131   }
132 };
133
134
135
136 //===----------------------------------------------------------------------===//
137 /// LoopInfo - This class builds and contains all of the top level loop
138 /// structures in the specified function.
139 ///
140 class LoopInfo : public FunctionPass {
141   // BBMap - Mapping of basic blocks to the inner most loop they occur in
142   std::map<BasicBlock*, Loop*> BBMap;
143   std::vector<Loop*> TopLevelLoops;
144   friend class Loop;
145 public:
146   ~LoopInfo() { releaseMemory(); }
147
148   const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
149
150   /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
151   /// block is in no loop (for example the entry node), null is returned.
152   ///
153   const Loop *getLoopFor(const BasicBlock *BB) const {
154     std::map<BasicBlock *, Loop*>::const_iterator I=BBMap.find((BasicBlock*)BB);
155     return I != BBMap.end() ? I->second : 0;
156   }
157
158   /// operator[] - same as getLoopFor...
159   ///
160   inline const Loop *operator[](const BasicBlock *BB) const {
161     return getLoopFor(BB);
162   }
163
164   /// getLoopDepth - Return the loop nesting level of the specified block...
165   ///
166   unsigned getLoopDepth(const BasicBlock *BB) const {
167     const Loop *L = getLoopFor(BB);
168     return L ? L->getLoopDepth() : 0;
169   }
170
171   // isLoopHeader - True if the block is a loop header node
172   bool isLoopHeader(BasicBlock *BB) const {
173     return getLoopFor(BB)->getHeader() == BB;
174   }
175
176   /// runOnFunction - Calculate the natural loop information.
177   ///
178   virtual bool runOnFunction(Function &F);
179
180   virtual void releaseMemory();
181   void print(std::ostream &O) const;
182
183   /// getAnalysisUsage - Requires dominator sets
184   ///
185   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
186
187   static void stub();  // Noop
188 private:
189   void Calculate(const DominatorSet &DS);
190   Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
191   void MoveSiblingLoopInto(Loop *NewChild, Loop *NewParent);
192   void InsertLoopInto(Loop *L, Loop *Parent);
193 };
194
195
196 // Make sure that any clients of this file link in LoopInfo.cpp
197 static IncludeFile
198 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);
199
200 // Allow clients to walk the list of nested loops...
201 template <> struct GraphTraits<const Loop*> {
202   typedef const Loop NodeType;
203   typedef std::vector<Loop*>::const_iterator ChildIteratorType;
204
205   static NodeType *getEntryNode(const Loop *L) { return L; }
206   static inline ChildIteratorType child_begin(NodeType *N) { 
207     return N->getSubLoops().begin();
208   }
209   static inline ChildIteratorType child_end(NodeType *N) { 
210     return N->getSubLoops().end();
211   }
212 };
213
214 template <> struct GraphTraits<Loop*> {
215   typedef Loop NodeType;
216   typedef std::vector<Loop*>::const_iterator ChildIteratorType;
217
218   static NodeType *getEntryNode(Loop *L) { return L; }
219   static inline ChildIteratorType child_begin(NodeType *N) { 
220     return N->getSubLoops().begin();
221   }
222   static inline ChildIteratorType child_end(NodeType *N) { 
223     return N->getSubLoops().end();
224   }
225 };
226
227 #endif