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