Added helper functions in LoopInfo: isLoopExit and numBackEdges.
[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 the
5 // loops identified may actually be several natural loops that share the same
6 // header node... not just a single natural loop.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_ANALYSIS_LOOP_INFO_H
11 #define LLVM_ANALYSIS_LOOP_INFO_H
12
13 #include "llvm/Pass.h"
14 #include <set>
15
16 class DominatorSet;
17 class LoopInfo;
18
19 //===----------------------------------------------------------------------===//
20 /// Loop class - Instances of this class are used to represent loops that are 
21 /// detected in the flow graph 
22 ///
23 class Loop {
24   Loop *ParentLoop;
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
28
29   Loop(const Loop &);                  // DO NOT IMPLEMENT
30   const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
31 public:
32
33   inline unsigned getLoopDepth() const { return LoopDepth; }
34   inline BasicBlock *getHeader() const { return Blocks.front(); }
35   inline Loop *getParentLoop() const { return ParentLoop; }
36
37   /// contains - Return true of the specified basic block is in this loop
38   bool contains(const BasicBlock *BB) const;
39
40   /// getSubLoops - Return the loops contained entirely within this loop
41   ///
42   inline const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
43   inline const std::vector<BasicBlock*> &getBlocks() const { return Blocks; }
44
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;
48
49   /// Find number of back edges
50   unsigned getNumBackEdges() const;
51
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.
57   ///
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).
61   ///
62   BasicBlock *getLoopPreheader() const;
63
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.
69   ///
70   void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI);
71
72   void print(std::ostream &O) const;
73 private:
74   friend class LoopInfo;
75   inline Loop(BasicBlock *BB) : ParentLoop(0) {
76     Blocks.push_back(BB); LoopDepth = 0;
77   }
78   ~Loop() {
79     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
80       delete SubLoops[i];
81   }
82   
83   void setLoopDepth(unsigned Level) {
84     LoopDepth = Level;
85     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
86       SubLoops[i]->setLoopDepth(Level+1);
87   }
88 };
89
90
91
92 //===----------------------------------------------------------------------===//
93 /// LoopInfo - This class builds and contains all of the top level loop
94 /// structures in the specified function.
95 ///
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;
100   friend class Loop;
101 public:
102   ~LoopInfo() { releaseMemory(); }
103
104   const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
105
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.
108   ///
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;
112   }
113
114   /// operator[] - same as getLoopFor...
115   ///
116   inline const Loop *operator[](const BasicBlock *BB) const {
117     return getLoopFor(BB);
118   }
119
120   /// getLoopDepth - Return the loop nesting level of the specified block...
121   ///
122   unsigned getLoopDepth(const BasicBlock *BB) const {
123     const Loop *L = getLoopFor(BB);
124     return L ? L->getLoopDepth() : 0;
125   }
126
127 #if 0
128   // isLoopHeader - True if the block is a loop header node
129   bool isLoopHeader(BasicBlock *BB) const {
130     return getLoopFor(BB)->getHeader() == BB;
131   }
132   // isLoopEnd - True if block jumps to loop entry
133   bool isLoopEnd(BasicBlock *BB) const;
134 #endif
135
136   /// runOnFunction - Calculate the natural loop information.
137   ///
138   virtual bool runOnFunction(Function &F);
139
140   virtual void releaseMemory();
141   void print(std::ostream &O) const;
142
143   /// getAnalysisUsage - Requires dominator sets
144   ///
145   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
146
147   static void stub();  // Noop
148 private:
149   void Calculate(const DominatorSet &DS);
150   Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
151 };
152
153
154 // Make sure that any clients of this file link in LoopInfo.cpp
155 static IncludeFile
156 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);
157
158 #endif