- Add new methods to LoopInfo: getLoopPreheader, addBasicBlockToLoop.
[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(BasicBlock *BB) const;
48
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.
54   ///
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).
58   ///
59   BasicBlock *getLoopPreheader() const;
60
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.
66   ///
67   void addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI);
68
69   void print(std::ostream &O) const;
70 private:
71   friend class LoopInfo;
72   inline Loop(BasicBlock *BB) { Blocks.push_back(BB); LoopDepth = 0; }
73   ~Loop() {
74     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
75       delete SubLoops[i];
76   }
77   
78   void setLoopDepth(unsigned Level) {
79     LoopDepth = Level;
80     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
81       SubLoops[i]->setLoopDepth(Level+1);
82   }
83 };
84
85
86
87 //===----------------------------------------------------------------------===//
88 /// LoopInfo - This class builds and contains all of the top level loop
89 /// structures in the specified function.
90 ///
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;
95   friend class Loop;
96 public:
97   ~LoopInfo() { releaseMemory(); }
98
99   const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
100
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.
103   ///
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;
107   }
108
109   /// operator[] - same as getLoopFor...
110   ///
111   inline const Loop *operator[](const BasicBlock *BB) const {
112     return getLoopFor(BB);
113   }
114
115   /// getLoopDepth - Return the loop nesting level of the specified block...
116   ///
117   unsigned getLoopDepth(const BasicBlock *BB) const {
118     const Loop *L = getLoopFor(BB);
119     return L ? L->getLoopDepth() : 0;
120   }
121
122 #if 0
123   // isLoopHeader - True if the block is a loop header node
124   bool isLoopHeader(BasicBlock *BB) const {
125     return getLoopFor(BB)->getHeader() == BB;
126   }
127   // isLoopEnd - True if block jumps to loop entry
128   bool isLoopEnd(BasicBlock *BB) const;
129 #endif
130
131   /// runOnFunction - Calculate the natural loop information.
132   ///
133   virtual bool runOnFunction(Function &F);
134
135   virtual void releaseMemory();
136   void print(std::ostream &O) const;
137
138   /// getAnalysisUsage - Requires dominator sets
139   ///
140   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
141
142   static void stub();  // Noop
143 private:
144   void Calculate(const DominatorSet &DS);
145   Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
146 };
147
148
149 // Make sure that any clients of this file link in LoopInfo.cpp
150 static IncludeFile
151 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);
152
153 #endif