Be a bit more efficient when processing the active and inactive
[oota-llvm.git] / include / llvm / CodeGen / MachineBasicBlock.h
1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 // 
10 // Collect the sequence of machine instructions for a basic block.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
16
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "Support/GraphTraits.h"
19 #include "Support/ilist"
20 #include <iosfwd>
21
22 namespace llvm {
23   class MachineFunction;
24
25 // ilist_traits
26 template <>
27 class ilist_traits<MachineInstr> {
28   // this is only set by the MachineBasicBlock owning the ilist
29   friend class MachineBasicBlock;
30   MachineBasicBlock* parent;
31
32 public:
33   ilist_traits<MachineInstr>() : parent(0) { }
34
35   static MachineInstr* getPrev(MachineInstr* N) { return N->prev; }
36   static MachineInstr* getNext(MachineInstr* N) { return N->next; }
37
38   static const MachineInstr*
39   getPrev(const MachineInstr* N) { return N->prev; }
40
41   static const MachineInstr*
42   getNext(const MachineInstr* N) { return N->next; }
43
44   static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; }
45   static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; }
46
47   static MachineInstr* createNode();
48   void addNodeToList(MachineInstr* N);
49   void removeNodeFromList(MachineInstr* N);
50   void transferNodesFromList(
51       iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
52       ilist_iterator<MachineInstr> first,
53       ilist_iterator<MachineInstr> last);
54 };
55
56 class BasicBlock;
57
58 class MachineBasicBlock {
59 public:
60   typedef ilist<MachineInstr> Instructions;
61   Instructions Insts;
62   MachineBasicBlock *Prev, *Next;
63   const BasicBlock *BB;
64   std::vector<MachineBasicBlock *> Predecessors;
65   std::vector<MachineBasicBlock *> Successors;
66   int Number;
67   MachineFunction *Parent;
68
69 public:
70   MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb),
71                                                 Number(-1), Parent(0) {
72     Insts.parent = this;
73   }
74
75   ~MachineBasicBlock();
76   
77   /// getBasicBlock - Return the LLVM basic block that this instance
78   /// corresponded to originally.
79   ///
80   const BasicBlock *getBasicBlock() const { return BB; }
81
82   /// getParent - Return the MachineFunction containing this basic block.
83   ///
84   const MachineFunction *getParent() const { return Parent; }
85   MachineFunction *getParent() { return Parent; }
86
87   typedef ilist<MachineInstr>::iterator                       iterator;
88   typedef ilist<MachineInstr>::const_iterator           const_iterator;
89   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
90   typedef std::reverse_iterator<iterator>             reverse_iterator;
91
92   unsigned size() const { return Insts.size(); }
93   bool empty() const { return Insts.empty(); }
94
95   MachineInstr& front() { return Insts.front(); }
96   MachineInstr& back()  { return Insts.back(); }
97
98   iterator                begin()       { return Insts.begin();  }
99   const_iterator          begin() const { return Insts.begin();  }
100   iterator                  end()       { return Insts.end();    }
101   const_iterator            end() const { return Insts.end();    }
102   reverse_iterator       rbegin()       { return Insts.rbegin(); }
103   const_reverse_iterator rbegin() const { return Insts.rbegin(); }
104   reverse_iterator       rend  ()       { return Insts.rend();   }
105   const_reverse_iterator rend  () const { return Insts.rend();   }
106
107   // Machine-CFG iterators
108   typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
109   typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
110   typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
111   typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
112   
113   pred_iterator        pred_begin()       { return Predecessors.begin (); }
114   const_pred_iterator  pred_begin() const { return Predecessors.begin (); }
115   pred_iterator        pred_end()         { return Predecessors.end ();   }
116   const_pred_iterator  pred_end()   const { return Predecessors.end ();   }
117   unsigned             pred_size()  const { return Predecessors.size ();  }
118   bool                 pred_empty() const { return Predecessors.empty();  }
119   succ_iterator        succ_begin()       { return Successors.begin ();   }
120   const_succ_iterator  succ_begin() const { return Successors.begin ();   }
121   succ_iterator        succ_end()         { return Successors.end ();     }
122   const_succ_iterator  succ_end()   const { return Successors.end ();     }
123   unsigned             succ_size()  const { return Successors.size ();    }
124   bool                 succ_empty() const { return Successors.empty();    }
125
126   // Machine-CFG mutators
127
128   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
129   /// The Predecessors list of succ is automatically updated.
130   ///
131   void addSuccessor(MachineBasicBlock *succ) {
132     Successors.push_back(succ);
133     succ->addPredecessor(this);
134   }
135
136   /// removeSuccessor - Remove successor from the successors list of this
137   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
138   ///
139   void removeSuccessor(MachineBasicBlock *succ) {
140     succ->removePredecessor(this);
141     succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
142     assert(I != Successors.end() && "Not a current successor!");
143     Successors.erase(I);
144   }
145
146   /// removeSuccessor - Remove specified successor from the successors list of
147   /// this MachineBasicBlock. The Predecessors list of succ is automatically
148   /// updated.
149   ///
150   void removeSuccessor(succ_iterator I) {
151     assert(I != Successors.end() && "Not a current successor!");
152     (*I)->removePredecessor(this);
153     Successors.erase(I);
154   }
155
156   /// getFirstTerminator - returns an iterator to the first terminator
157   /// instruction of this basic block. If a terminator does not exist,
158   /// it returns end()
159   iterator getFirstTerminator();
160
161   void pop_front() { Insts.pop_front(); }
162   void pop_back() { Insts.pop_back(); }
163   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
164   template<typename IT>
165   void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
166   iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
167
168   // erase - Remove the specified element or range from the instruction list.
169   // These functions delete any instructions removed.
170   //
171   iterator erase(iterator I)             { return Insts.erase(I); }
172   iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
173   MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
174   void clear()                           { Insts.clear(); }
175   
176   /// splice - Take a block of instructions from MBB 'Other' in the range [From,
177   /// To), and insert them into this MBB right before 'where'.
178   void splice(iterator where, MachineBasicBlock *Other, iterator From,
179               iterator To) {
180     Insts.splice(where, Other->Insts, From, To);
181   }
182
183   // Debugging methods.
184   void dump() const;
185   void print(std::ostream &OS) const;
186
187   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
188   /// level, unless they're not in a MachineFunction yet, in which case this
189   /// will return -1.
190   ///
191   int getNumber() const { return Number; }
192
193 private:   // Methods used to maintain doubly linked list of blocks...
194   friend class ilist_traits<MachineBasicBlock>;
195
196   MachineBasicBlock *getPrev() const { return Prev; }
197   MachineBasicBlock *getNext() const { return Next; }
198   void setPrev(MachineBasicBlock *P) { Prev = P; }
199   void setNext(MachineBasicBlock *N) { Next = N; }
200
201   // Machine-CFG mutators
202
203   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
204   /// Don't do this unless you know what you're doing, because it doesn't
205   /// update pred's successors list. Use pred->addSuccessor instead.
206   ///
207   void addPredecessor (MachineBasicBlock *pred) {
208     Predecessors.push_back (pred);
209   }
210
211   /// removePredecessor - Remove pred as a predecessor of this
212   /// MachineBasicBlock. Don't do this unless you know what you're
213   /// doing, because it doesn't update pred's successors list. Use
214   /// pred->removeSuccessor instead.
215   ///
216   void removePredecessor (MachineBasicBlock *pred) {
217     std::vector<MachineBasicBlock *>::iterator goner =
218       std::find (Predecessors.begin(), Predecessors.end (), pred);
219     Predecessors.erase (goner);
220   }
221 };
222
223 // This is useful when building DenseMaps keyed on MachineBasicBlocks
224 struct MachineBasicBlock2IndexFunctor
225   : std::unary_function<const MachineBasicBlock*, unsigned> {
226   unsigned operator()(const MachineBasicBlock* MBB) const {
227     assert(MBB->getNumber() != -1 &&
228            "MachineBasicBlock does not belong to a MachineFunction");
229     return MBB->getNumber();
230   }
231 };
232
233
234 //===--------------------------------------------------------------------===//
235 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
236 //===--------------------------------------------------------------------===//
237
238 // Provide specializations of GraphTraits to be able to treat a
239 // MachineFunction as a graph of MachineBasicBlocks...
240 //
241
242 template <> struct GraphTraits<MachineBasicBlock *> {
243   typedef MachineBasicBlock NodeType;
244   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
245
246   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
247   static inline ChildIteratorType child_begin(NodeType *N) { 
248     return N->succ_begin();
249   }
250   static inline ChildIteratorType child_end(NodeType *N) { 
251     return N->succ_end();
252   }
253 };
254
255 template <> struct GraphTraits<const MachineBasicBlock *> {
256   typedef const MachineBasicBlock NodeType;
257   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
258
259   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
260   static inline ChildIteratorType child_begin(NodeType *N) { 
261     return N->succ_begin();
262   }
263   static inline ChildIteratorType child_end(NodeType *N) { 
264     return N->succ_end();
265   }
266 };
267
268 // Provide specializations of GraphTraits to be able to treat a
269 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
270 // in inverse order.  Inverse order for a function is considered
271 // to be when traversing the predecessor edges of a MBB
272 // instead of the successor edges.
273 //
274 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
275   typedef MachineBasicBlock NodeType;
276   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
277   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
278     return G.Graph;
279   }
280   static inline ChildIteratorType child_begin(NodeType *N) { 
281     return N->pred_begin();
282   }
283   static inline ChildIteratorType child_end(NodeType *N) { 
284     return N->pred_end();
285   }
286 };
287
288 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
289   typedef const MachineBasicBlock NodeType;
290   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
291   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
292     return G.Graph; 
293   }
294   static inline ChildIteratorType child_begin(NodeType *N) { 
295     return N->pred_begin();
296   }
297   static inline ChildIteratorType child_end(NodeType *N) { 
298     return N->pred_end();
299   }
300 };
301
302 } // End llvm namespace
303
304 #endif