Don't leak all of the Loop objects created...
[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 namespace cfg {
17   class DominatorSet;
18   class LoopInfo;
19
20 //===----------------------------------------------------------------------===//
21 // Loop class - Instances of this class are used to represent loops that are 
22 // detected in the flow graph 
23 //
24 class Loop {
25   Loop *ParentLoop;
26   std::vector<const BasicBlock *> Blocks; // First entry is the header node
27   std::vector<Loop*> SubLoops;       // Loops contained entirely within this one
28   unsigned LoopDepth;                // Nesting depth of this loop
29
30   Loop(const Loop &);                  // DO NOT IMPLEMENT
31   const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
32 public:
33
34   inline unsigned getLoopDepth() const { return LoopDepth; }
35   inline const BasicBlock *getHeader() const { return Blocks.front(); }
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   inline const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
42   inline const std::vector<const BasicBlock*> &getBlocks() const {
43     return Blocks;
44   }
45
46 private:
47   friend class LoopInfo;
48   inline Loop(const BasicBlock *BB) { Blocks.push_back(BB); LoopDepth = 0; }
49   ~Loop() {
50     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
51       delete SubLoops[i];
52   }
53   
54   void setLoopDepth(unsigned Level) {
55     LoopDepth = Level;
56     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
57       SubLoops[i]->setLoopDepth(Level+1);
58   }
59 };
60
61
62
63 //===----------------------------------------------------------------------===//
64 // LoopInfo - This class builds and contains all of the top level loop
65 // structures in the specified method.
66 //
67 class LoopInfo : public MethodPass {
68   // BBMap - Mapping of basic blocks to the inner most loop they occur in
69   std::map<const BasicBlock *, Loop*> BBMap;
70   std::vector<Loop*> TopLevelLoops;
71 public:
72   static AnalysisID ID;            // cfg::LoopInfo Analysis ID 
73
74   // LoopInfo ctor - Calculate the natural loop information for a CFG
75   LoopInfo(AnalysisID id) { assert(id == ID); }
76   ~LoopInfo() { releaseMemory(); }
77
78   const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
79
80   // getLoopFor - Return the inner most loop that BB lives in.  If a basic block
81   // is in no loop (for example the entry node), null is returned.
82   //
83   const Loop *getLoopFor(const BasicBlock *BB) const {
84     std::map<const BasicBlock *, Loop*>::const_iterator I = BBMap.find(BB);
85     return I != BBMap.end() ? I->second : 0;
86   }
87   inline const Loop *operator[](const BasicBlock *BB) const {
88     return getLoopFor(BB);
89   }
90
91   // getLoopDepth - Return the loop nesting level of the specified block...
92   unsigned getLoopDepth(const BasicBlock *BB) const {
93     const Loop *L = getLoopFor(BB);
94     return L ? L->getLoopDepth() : 0;
95   }
96
97 #if 0
98   // isLoopHeader - True if the block is a loop header node
99   bool isLoopHeader(const BasicBlock *BB) const {
100     return getLoopFor(BB)->getHeader() == BB;
101   }
102   // isLoopEnd - True if block jumps to loop entry
103   bool isLoopEnd(const BasicBlock *BB) const;
104   // isLoopExit - True if block is the loop exit
105   bool isLoopExit(const BasicBlock *BB) const;
106 #endif
107
108   // runOnMethod - Pass framework implementation
109   virtual bool runOnMethod(Function *F);
110
111   virtual void releaseMemory();
112
113   // getAnalysisUsageInfo - Provide loop info, require dominator set
114   //
115   virtual void getAnalysisUsageInfo(Pass::AnalysisSet &Requires,
116                                     Pass::AnalysisSet &Destroyed,
117                                     Pass::AnalysisSet &Provided);
118 private:
119   void Calculate(const DominatorSet &DS);
120   Loop *ConsiderForLoop(const BasicBlock *BB, const DominatorSet &DS);
121 };
122
123 }  // End namespace cfg
124
125 #endif