Checkin stub for Misha to implement
[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
36   // contains - Return true of the specified basic block is in this loop
37   bool contains(const BasicBlock *BB) const;
38
39   // getSubLoops - Return the loops contained entirely within this loop
40   inline const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
41   inline const std::vector<BasicBlock*> &getBlocks() const { return Blocks; }
42
43   // isLoopExit - True if terminator in the block can branch to another block
44   // that is outside of the current loop.
45   bool isLoopExit(BasicBlock *BB) const;
46
47   void print(std::ostream &O) const;
48 private:
49   friend class LoopInfo;
50   inline Loop(BasicBlock *BB) { Blocks.push_back(BB); LoopDepth = 0; }
51   ~Loop() {
52     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
53       delete SubLoops[i];
54   }
55   
56   void setLoopDepth(unsigned Level) {
57     LoopDepth = Level;
58     for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
59       SubLoops[i]->setLoopDepth(Level+1);
60   }
61 };
62
63
64
65 //===----------------------------------------------------------------------===//
66 // LoopInfo - This class builds and contains all of the top level loop
67 // structures in the specified function.
68 //
69 class LoopInfo : public FunctionPass {
70   // BBMap - Mapping of basic blocks to the inner most loop they occur in
71   std::map<BasicBlock*, Loop*> BBMap;
72   std::vector<Loop*> TopLevelLoops;
73 public:
74   // LoopInfo ctor - Calculate the natural loop information for a CFG
75   ~LoopInfo() { releaseMemory(); }
76
77   const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
78
79   // getLoopFor - Return the inner most loop that BB lives in.  If a basic block
80   // is in no loop (for example the entry node), null is returned.
81   //
82   const Loop *getLoopFor(const BasicBlock *BB) const {
83     std::map<BasicBlock *, Loop*>::const_iterator I=BBMap.find((BasicBlock*)BB);
84     return I != BBMap.end() ? I->second : 0;
85   }
86   inline const Loop *operator[](const BasicBlock *BB) const {
87     return getLoopFor(BB);
88   }
89
90   // getLoopDepth - Return the loop nesting level of the specified block...
91   unsigned getLoopDepth(const BasicBlock *BB) const {
92     const Loop *L = getLoopFor(BB);
93     return L ? L->getLoopDepth() : 0;
94   }
95
96 #if 0
97   // isLoopHeader - True if the block is a loop header node
98   bool isLoopHeader(BasicBlock *BB) const {
99     return getLoopFor(BB)->getHeader() == BB;
100   }
101   // isLoopEnd - True if block jumps to loop entry
102   bool isLoopEnd(BasicBlock *BB) const;
103 #endif
104
105   // runOnFunction - Pass framework implementation
106   virtual bool runOnFunction(Function &F);
107
108   virtual void releaseMemory();
109   void print(std::ostream &O) const;
110
111   // getAnalysisUsage - Provide loop info, require dominator set
112   //
113   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
114
115   static void stub();  // Noop
116 private:
117   void Calculate(const DominatorSet &DS);
118   Loop *ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS);
119 };
120
121
122 // Make sure that any clients of this file link in PostDominators.cpp
123 static IncludeFile
124 LOOP_INFO_INCLUDE_FILE((void*)&LoopInfo::stub);
125
126 #endif