Make Loop and MachineLoop be subclasses of LoopBase, rather than typedefs,
[oota-llvm.git] / include / llvm / CodeGen / MachineLoopInfo.h
1 //===- llvm/CodeGen/MachineLoopInfo.h - Natural Loop Calculator -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MachineLoopInfo class that is used to identify natural 
11 // loops and determine the loop depth of various nodes of the CFG.  Note that
12 // natural loops may actually be several loops that share the same header node.
13 //
14 // This analysis calculates the nesting structure of loops in a function.  For
15 // each natural loop identified, this analysis identifies natural loops
16 // contained entirely within the loop and the basic blocks the make up the loop.
17 //
18 // It can calculate on the fly various bits of information, for example:
19 //
20 //  * whether there is a preheader for the loop
21 //  * the number of back edges to the header
22 //  * whether or not a particular block branches out of the loop
23 //  * the successor blocks of the loop
24 //  * the loop depth
25 //  * the trip count
26 //  * etc...
27 //
28 //===----------------------------------------------------------------------===//
29
30 #ifndef LLVM_CODEGEN_MACHINE_LOOP_INFO_H
31 #define LLVM_CODEGEN_MACHINE_LOOP_INFO_H
32
33 #include "llvm/CodeGen/MachineFunctionPass.h"
34 #include "llvm/Analysis/LoopInfo.h"
35
36 namespace llvm {
37
38 class MachineLoop;
39
40 // Provide overrides for Loop methods that don't make sense for machine loops.
41 template<> inline
42 PHINode *
43 LoopBase<MachineBasicBlock, MachineLoop>::getCanonicalInductionVariable() const {
44   assert(0 && "getCanonicalInductionVariable not supported for machine loops!");
45   return 0;
46 }
47
48 template<> inline Instruction*
49 LoopBase<MachineBasicBlock,
50          MachineLoop>::getCanonicalInductionVariableIncrement() const {
51   assert(0 &&
52      "getCanonicalInductionVariableIncrement not supported for machine loops!");
53   return 0;
54 }
55
56 template<>
57 inline bool
58 LoopBase<MachineBasicBlock, MachineLoop>::isLoopInvariant(Value *V) const {
59   assert(0 && "isLoopInvariant not supported for machine loops!");
60   return false;
61 }
62
63 template<>
64 inline Value *
65 LoopBase<MachineBasicBlock, MachineLoop>::getTripCount() const {
66   assert(0 && "getTripCount not supported for machine loops!");
67   return 0;
68 }
69
70 template<>
71 inline bool
72 LoopBase<MachineBasicBlock, MachineLoop>::isLCSSAForm() const {
73   assert(0 && "isLCSSAForm not supported for machine loops");
74   return false;
75 }
76
77 class MachineLoop : public LoopBase<MachineBasicBlock, MachineLoop> {
78 public:
79   MachineLoop();
80 private:
81   friend class LoopInfoBase<MachineBasicBlock, MachineLoop>;
82   explicit MachineLoop(MachineBasicBlock *MBB)
83     : LoopBase<MachineBasicBlock, MachineLoop>(MBB) {}
84 };
85
86 class MachineLoopInfo : public MachineFunctionPass {
87   LoopInfoBase<MachineBasicBlock, MachineLoop> LI;
88   friend class LoopBase<MachineBasicBlock, MachineLoop>;
89
90   void operator=(const MachineLoopInfo &);  // do not implement
91   MachineLoopInfo(const MachineLoopInfo &); // do not implement
92
93   LoopInfoBase<MachineBasicBlock, MachineLoop>& getBase() { return LI; }
94
95 public:
96   static char ID; // Pass identification, replacement for typeid
97
98   MachineLoopInfo() : MachineFunctionPass(&ID) {}
99
100   /// iterator/begin/end - The interface to the top-level loops in the current
101   /// function.
102   ///
103   typedef LoopInfoBase<MachineBasicBlock, MachineLoop>::iterator iterator;
104   inline iterator begin() const { return LI.begin(); }
105   inline iterator end() const { return LI.end(); }
106   bool empty() const { return LI.empty(); }
107
108   /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
109   /// block is in no loop (for example the entry node), null is returned.
110   ///
111   inline MachineLoop *getLoopFor(const MachineBasicBlock *BB) const {
112     return LI.getLoopFor(BB);
113   }
114
115   /// operator[] - same as getLoopFor...
116   ///
117   inline const MachineLoop *operator[](const MachineBasicBlock *BB) const {
118     return LI.getLoopFor(BB);
119   }
120
121   /// getLoopDepth - Return the loop nesting level of the specified block...
122   ///
123   inline unsigned getLoopDepth(const MachineBasicBlock *BB) const {
124     return LI.getLoopDepth(BB);
125   }
126
127   // isLoopHeader - True if the block is a loop header node
128   inline bool isLoopHeader(MachineBasicBlock *BB) const {
129     return LI.isLoopHeader(BB);
130   }
131
132   /// runOnFunction - Calculate the natural loop information.
133   ///
134   virtual bool runOnMachineFunction(MachineFunction &F);
135
136   virtual void releaseMemory() { LI.releaseMemory(); }
137
138   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
139
140   /// removeLoop - This removes the specified top-level loop from this loop info
141   /// object.  The loop is not deleted, as it will presumably be inserted into
142   /// another loop.
143   inline MachineLoop *removeLoop(iterator I) { return LI.removeLoop(I); }
144
145   /// changeLoopFor - Change the top-level loop that contains BB to the
146   /// specified loop.  This should be used by transformations that restructure
147   /// the loop hierarchy tree.
148   inline void changeLoopFor(MachineBasicBlock *BB, MachineLoop *L) {
149     LI.changeLoopFor(BB, L);
150   }
151
152   /// changeTopLevelLoop - Replace the specified loop in the top-level loops
153   /// list with the indicated loop.
154   inline void changeTopLevelLoop(MachineLoop *OldLoop, MachineLoop *NewLoop) {
155     LI.changeTopLevelLoop(OldLoop, NewLoop);
156   }
157
158   /// addTopLevelLoop - This adds the specified loop to the collection of
159   /// top-level loops.
160   inline void addTopLevelLoop(MachineLoop *New) {
161     LI.addTopLevelLoop(New);
162   }
163
164   /// removeBlock - This method completely removes BB from all data structures,
165   /// including all of the Loop objects it is nested in and our mapping from
166   /// MachineBasicBlocks to loops.
167   void removeBlock(MachineBasicBlock *BB) {
168     LI.removeBlock(BB);
169   }
170 };
171
172
173 // Allow clients to walk the list of nested loops...
174 template <> struct GraphTraits<const MachineLoop*> {
175   typedef const MachineLoop NodeType;
176   typedef MachineLoopInfo::iterator ChildIteratorType;
177
178   static NodeType *getEntryNode(const MachineLoop *L) { return L; }
179   static inline ChildIteratorType child_begin(NodeType *N) {
180     return N->begin();
181   }
182   static inline ChildIteratorType child_end(NodeType *N) {
183     return N->end();
184   }
185 };
186
187 template <> struct GraphTraits<MachineLoop*> {
188   typedef MachineLoop NodeType;
189   typedef MachineLoopInfo::iterator ChildIteratorType;
190
191   static NodeType *getEntryNode(MachineLoop *L) { return L; }
192   static inline ChildIteratorType child_begin(NodeType *N) {
193     return N->begin();
194   }
195   static inline ChildIteratorType child_end(NodeType *N) {
196     return N->end();
197   }
198 };
199
200 } // End llvm namespace
201
202 #endif