Induction variables must be phi nodes
[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 <vector>
14 #include <map>
15 #include <set>
16 class BasicBlock;
17
18 namespace cfg {
19   class DominatorSet;
20   class LoopInfo;
21
22 //===----------------------------------------------------------------------===//
23 // Loop class - Instances of this class are used to represent loops that are 
24 // detected in the flow graph 
25 //
26 class Loop {
27   Loop *ParentLoop;
28   vector<const BasicBlock *> Blocks; // First entry is the header node
29   vector<Loop*> SubLoops;            // Loops contained entirely within this one
30   unsigned LoopDepth;                // Nesting depth of this loop
31
32   Loop(const Loop &);                  // DO NOT IMPLEMENT
33   const Loop &operator=(const Loop &); // DO NOT IMPLEMENT
34 public:
35
36   inline unsigned getLoopDepth() const { return LoopDepth; }
37   inline const BasicBlock *getHeader() const { return Blocks.front(); }
38
39   // contains - Return true of the specified basic block is in this loop
40   bool contains(const BasicBlock *BB) const;
41
42   // getSubLoops - Return the loops contained entirely within this loop
43   inline const vector<Loop*> &getSubLoops() const { return SubLoops; }
44   inline const vector<const BasicBlock*> &getBlocks() const { return Blocks; }
45
46 private:
47   friend class LoopInfo;
48   inline Loop(const BasicBlock *BB) { Blocks.push_back(BB); LoopDepth = 0; }
49   
50   void setLoopDepth(unsigned Level) {
51     LoopDepth = Level;
52     for (unsigned i = 0; i < SubLoops.size(); ++i)
53       SubLoops[i]->setLoopDepth(Level+1);
54   }
55 };
56
57
58
59 //===----------------------------------------------------------------------===//
60 // LoopInfo - This class builds and contains all of the top level loop
61 // structures in the specified method.
62 //
63 class LoopInfo {
64   // BBMap - Mapping of basic blocks to the inner most loop they occur in
65   map<const BasicBlock *, Loop*> BBMap;
66   vector<Loop*> TopLevelLoops;
67 public:
68   // LoopInfo ctor - Calculate the natural loop information for a CFG
69   LoopInfo(const DominatorSet &DS);
70
71   const vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
72
73   // getLoopFor - Return the inner most loop that BB lives in.  If a basic block
74   // is in no loop (for example the entry node), null is returned.
75   //
76   const Loop *getLoopFor(const BasicBlock *BB) const {
77     map<const BasicBlock *, Loop*>::const_iterator I = BBMap.find(BB);
78     return I != BBMap.end() ? I->second : 0;
79   }
80   inline const Loop *operator[](const BasicBlock *BB) const {
81     return getLoopFor(BB);
82   }
83
84   // getLoopDepth - Return the loop nesting level of the specified block...
85   unsigned getLoopDepth(const BasicBlock *BB) const {
86     const Loop *L = getLoopFor(BB);
87     return L ? L->getLoopDepth() : 0;
88   }
89
90 #if 0
91   // isLoopHeader - True if the block is a loop header node
92   bool isLoopHeader(const BasicBlock *BB) const {
93     return getLoopFor(BB)->getHeader() == BB;
94   }
95   // isLoopEnd - True if block jumps to loop entry
96   bool isLoopEnd(const BasicBlock *BB) const;
97   // isLoopExit - True if block is the loop exit
98   bool isLoopExit(const BasicBlock *BB) const;
99 #endif
100
101 private:
102   Loop *ConsiderForLoop(const BasicBlock *BB, const DominatorSet &DS);
103 };
104
105 }  // End namespace cfg
106
107 #endif