5faf2a21a97d66f51abd8b6e6a42f074f98c989d
[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
67 public:
68   MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb) {
69     Insts.parent = this;
70   }
71   ~MachineBasicBlock() {}
72   
73   /// getBasicBlock - Return the LLVM basic block that this instance
74   /// corresponded to originally.
75   ///
76   const BasicBlock *getBasicBlock() const { return BB; }
77
78   /// getParent - Return the MachineFunction containing this basic block.
79   ///
80   const MachineFunction *getParent() const;
81
82   typedef ilist<MachineInstr>::iterator                       iterator;
83   typedef ilist<MachineInstr>::const_iterator           const_iterator;
84   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
85   typedef std::reverse_iterator<iterator>             reverse_iterator;
86
87   unsigned size() const { return Insts.size(); }
88   bool empty() const { return Insts.empty(); }
89
90   MachineInstr& front() { return Insts.front(); }
91   MachineInstr& back()  { return Insts.back(); }
92
93   iterator                begin()       { return Insts.begin();  }
94   const_iterator          begin() const { return Insts.begin();  }
95   iterator                  end()       { return Insts.end();    }
96   const_iterator            end() const { return Insts.end();    }
97   reverse_iterator       rbegin()       { return Insts.rbegin(); }
98   const_reverse_iterator rbegin() const { return Insts.rbegin(); }
99   reverse_iterator       rend  ()       { return Insts.rend();   }
100   const_reverse_iterator rend  () const { return Insts.rend();   }
101
102   // Machine-CFG iterators
103   typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
104   typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
105   typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
106   typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
107   
108   pred_iterator        pred_begin()       { return Predecessors.begin (); }
109   const_pred_iterator  pred_begin() const { return Predecessors.begin (); }
110   pred_iterator        pred_end()         { return Predecessors.end ();   }
111   const_pred_iterator  pred_end()   const { return Predecessors.end ();   }
112   unsigned             pred_size()  const { return Predecessors.size ();  }
113   succ_iterator        succ_begin()       { return Successors.begin ();   }
114   const_succ_iterator  succ_begin() const { return Successors.begin ();   }
115   succ_iterator        succ_end()         { return Successors.end ();     }
116   const_succ_iterator  succ_end()   const { return Successors.end ();     }
117   unsigned             succ_size()  const { return Successors.size ();    }
118
119   // Machine-CFG mutators
120
121   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
122   /// The Predecessors list of succ is automatically updated.
123   ///
124   void addSuccessor (MachineBasicBlock *succ) {
125     Successors.push_back (succ);
126     succ->addPredecessor (this);
127   }
128
129   /// removeSuccessor - Remove succ from the successors list of this
130   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
131   ///
132   void removeSuccessor (MachineBasicBlock *succ) {
133     succ->removePredecessor (this);
134     std::vector<MachineBasicBlock *>::iterator goner =
135       std::find (Successors.begin(), Successors.end (), succ);
136     Successors.erase (goner);
137   }
138
139   /// getFirstTerminator - returns an iterator to the first terminator
140   /// instruction of this basic block. If a terminator does not exist,
141   /// it returns end()
142   iterator getFirstTerminator();
143
144   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
145   template<typename IT>
146   void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
147   iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
148
149   // erase - Remove the specified element or range from the instruction list.
150   // These functions delete any instructions removed.
151   //
152   iterator erase(iterator I)             { return Insts.erase(I); }
153   iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
154   MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
155   void clear()                           { Insts.clear(); }
156
157   // Debugging methods.
158   void dump() const;
159   void print(std::ostream &OS) const;
160
161 private:   // Methods used to maintain doubly linked list of blocks...
162   friend class ilist_traits<MachineBasicBlock>;
163
164   MachineBasicBlock *getPrev() const { return Prev; }
165   MachineBasicBlock *getNext() const { return Next; }
166   void setPrev(MachineBasicBlock *P) { Prev = P; }
167   void setNext(MachineBasicBlock *N) { Next = N; }
168
169   // Machine-CFG mutators
170
171   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
172   /// Don't do this unless you know what you're doing, because it doesn't
173   /// update pred's successors list. Use pred->addSuccessor instead.
174   ///
175   void addPredecessor (MachineBasicBlock *pred) {
176     Predecessors.push_back (pred);
177   }
178
179   /// removePredecessor - Remove pred as a predecessor of this
180   /// MachineBasicBlock. Don't do this unless you know what you're
181   /// doing, because it doesn't update pred's successors list. Use
182   /// pred->removeSuccessor instead.
183   ///
184   void removePredecessor (MachineBasicBlock *pred) {
185     std::vector<MachineBasicBlock *>::iterator goner =
186       std::find (Predecessors.begin(), Predecessors.end (), pred);
187     Predecessors.erase (goner);
188   }
189 };
190
191
192 //===--------------------------------------------------------------------===//
193 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
194 //===--------------------------------------------------------------------===//
195
196 // Provide specializations of GraphTraits to be able to treat a
197 // MachineFunction as a graph of MachineBasicBlocks...
198 //
199
200 template <> struct GraphTraits<MachineBasicBlock *> {
201   typedef MachineBasicBlock NodeType;
202   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
203
204   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
205   static inline ChildIteratorType child_begin(NodeType *N) { 
206     return N->succ_begin();
207   }
208   static inline ChildIteratorType child_end(NodeType *N) { 
209     return N->succ_end();
210   }
211 };
212
213 template <> struct GraphTraits<const MachineBasicBlock *> {
214   typedef const MachineBasicBlock NodeType;
215   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
216
217   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
218   static inline ChildIteratorType child_begin(NodeType *N) { 
219     return N->succ_begin();
220   }
221   static inline ChildIteratorType child_end(NodeType *N) { 
222     return N->succ_end();
223   }
224 };
225
226 // Provide specializations of GraphTraits to be able to treat a
227 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
228 // in inverse order.  Inverse order for a function is considered
229 // to be when traversing the predecessor edges of a MBB
230 // instead of the successor edges.
231 //
232 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
233   typedef MachineBasicBlock NodeType;
234   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
235   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
236     return G.Graph;
237   }
238   static inline ChildIteratorType child_begin(NodeType *N) { 
239     return N->pred_begin();
240   }
241   static inline ChildIteratorType child_end(NodeType *N) { 
242     return N->pred_end();
243   }
244 };
245
246 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
247   typedef const MachineBasicBlock NodeType;
248   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
249   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
250     return G.Graph; 
251   }
252   static inline ChildIteratorType child_begin(NodeType *N) { 
253     return N->pred_begin();
254   }
255   static inline ChildIteratorType child_end(NodeType *N) { 
256     return N->pred_end();
257   }
258 };
259
260 } // End llvm namespace
261
262 #endif