Change behavior of changeExitBlock function to replace all instances of exit block
[oota-llvm.git] / include / llvm / Analysis / LoopInfo.h
1 //===- llvm/Analysis/LoopInfo.h - Natural Loop Calculator --------*- C++ -*--=//
2 //
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...
6 //
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.
11 //
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.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
19 #define LLVM_ANALYSIS_LOOP_INFO_H
20
21 #include "llvm/Pass.h"
22 #include <set>
23
24 class DominatorSet;
25 class LoopInfo;
26
27 //===----------------------------------------------------------------------===//
28 /// Loop class - Instances of this class are used to represent loops that are 
29 /// detected in the flow graph 
30 ///
31 class Loop {
32   Loop *ParentLoop;
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
37
38   Loop(const Loop &);                  // DO NOT IMPLEMENT
39   const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
40 public:
41
42   inline unsigned getLoopDepth() const { return LoopDepth; }
43   inline BasicBlock *getHeader() const { return Blocks.front(); }
44   inline Loop *getParentLoop() const { return ParentLoop; }
45
46   /// contains - Return true of the specified basic block is in this loop
47   bool contains(const BasicBlock *BB) const;
48
49   /// getSubLoops - Return the loops contained entirely within this loop
50   ///
51   const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
52
53   /// getBlocks - Get a list of the basic blocks which make up this loop.
54   ///
55   const std::vector<BasicBlock*> &getBlocks() const { return Blocks; }
56
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.
59   ///
60   const std::vector<BasicBlock*> &getExitBlocks() const { return ExitBlocks; }
61
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
64   /// ExitBlocks list.
65   ///
66   bool isLoopExit(const BasicBlock *BB) const;
67
68   /// getNumBackEdges - Calculate the number of back edges to the loop header
69   ///
70   unsigned getNumBackEdges() const;
71
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.
77   ///
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).
81   ///
82   BasicBlock *getLoopPreheader() const;
83
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.
89   ///
90   void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI);
91
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.
95   ///
96   void changeExitBlock(BasicBlock *Old, BasicBlock *New);
97
98   void print(std::ostream &O) const;
99 private:
100   friend class LoopInfo;
101   inline Loop(BasicBlock *BB) : ParentLoop(0) {
102     Blocks.push_back(BB); LoopDepth = 0;
103   }
104   ~Loop() {
105     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
106       delete SubLoops[i];
107   }
108   
109   void setLoopDepth(unsigned Level) {
110     LoopDepth = Level;
111     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
112       SubLoops[i]->setLoopDepth(Level+1);
113   }
114 };
115
116
117
118 //===----------------------------------------------------------------------===//
119 /// LoopInfo - This class builds and contains all of the top level loop
120 /// structures in the specified function.
121 ///
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;
126   friend class Loop;
127 public:
128   ~LoopInfo() { releaseMemory(); }
129
130   const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
131
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.
134   ///
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;
138   }
139
140   /// operator[] - same as getLoopFor...
141   ///
142   inline const Loop *operator[](const BasicBlock *BB) const {
143     return getLoopFor(BB);
144   }
145
146   /// getLoopDepth - Return the loop nesting level of the specified block...
147   ///
148   unsigned getLoopDepth(const BasicBlock *BB) const {
149     const Loop *L = getLoopFor(BB);
150     return L ? L->getLoopDepth() : 0;
151   }
152
153   // isLoopHeader - True if the block is a loop header node
154   bool isLoopHeader(BasicBlock *BB) const {
155     return getLoopFor(BB)->getHeader() == BB;
156   }
157
158   /// runOnFunction - Calculate the natural loop information.
159   ///
160   virtual bool runOnFunction(Function &F);
161
162   virtual void releaseMemory();
163   void print(std::ostream &O) const;
164
165   /// getAnalysisUsage - Requires dominator sets
166   ///
167   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
168
169   static void stub();  // Noop
170 private:
171   void Calculate(const DominatorSet &DS);
172   Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
173 };
174
175
176 // Make sure that any clients of this file link in LoopInfo.cpp
177 static IncludeFile
178 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);
179
180 #endif