ed2219b9f380c437f5315547d875b6fca1503573
[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 "llvm/Support/Streams.h"
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* createSentinel();
49   static void destroySentinel(MachineInstr *MI) { delete MI; }
50   void addNodeToList(MachineInstr* N);
51   void removeNodeFromList(MachineInstr* N);
52   void transferNodesFromList(
53       iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
54       ilist_iterator<MachineInstr> first,
55       ilist_iterator<MachineInstr> last);
56 };
57
58 class BasicBlock;
59
60 class MachineBasicBlock {
61 public:
62   typedef ilist<MachineInstr> Instructions;
63   Instructions Insts;
64   MachineBasicBlock *Prev, *Next;
65   const BasicBlock *BB;
66   int Number;
67   MachineFunction *Parent;
68
69   /// Predecessors/Successors - Keep track of the predecessor / successor
70   /// basicblocks.
71   std::vector<MachineBasicBlock *> Predecessors;
72   std::vector<MachineBasicBlock *> Successors;
73
74   /// LiveIns - Keep track of the physical registers that are livein of
75   /// the basicblock.
76   std::vector<unsigned> LiveIns;
77   
78   /// IsLandingPad - Indicate that this basic block is entered via an
79   /// exception handler.
80   bool IsLandingPad;
81
82 public:
83   MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb),
84                                                 Number(-1), Parent(0),
85                                                 IsLandingPad(false) {
86     Insts.parent = this;
87   }
88
89   ~MachineBasicBlock();
90
91   /// getBasicBlock - Return the LLVM basic block that this instance
92   /// corresponded to originally.
93   ///
94   const BasicBlock *getBasicBlock() const { return BB; }
95
96   /// getParent - Return the MachineFunction containing this basic block.
97   ///
98   const MachineFunction *getParent() const { return Parent; }
99   MachineFunction *getParent() { return Parent; }
100
101   typedef ilist<MachineInstr>::iterator                       iterator;
102   typedef ilist<MachineInstr>::const_iterator           const_iterator;
103   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
104   typedef std::reverse_iterator<iterator>             reverse_iterator;
105
106   unsigned size() const { return Insts.size(); }
107   bool empty() const { return Insts.empty(); }
108
109   MachineInstr& front() { return Insts.front(); }
110   MachineInstr& back()  { return Insts.back(); }
111
112   iterator                begin()       { return Insts.begin();  }
113   const_iterator          begin() const { return Insts.begin();  }
114   iterator                  end()       { return Insts.end();    }
115   const_iterator            end() const { return Insts.end();    }
116   reverse_iterator       rbegin()       { return Insts.rbegin(); }
117   const_reverse_iterator rbegin() const { return Insts.rbegin(); }
118   reverse_iterator       rend  ()       { return Insts.rend();   }
119   const_reverse_iterator rend  () const { return Insts.rend();   }
120
121   // Machine-CFG iterators
122   typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
123   typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
124   typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
125   typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
126
127   pred_iterator        pred_begin()       { return Predecessors.begin(); }
128   const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
129   pred_iterator        pred_end()         { return Predecessors.end();   }
130   const_pred_iterator  pred_end()   const { return Predecessors.end();   }
131   unsigned             pred_size()  const { return Predecessors.size();  }
132   bool                 pred_empty() const { return Predecessors.empty(); }
133   succ_iterator        succ_begin()       { return Successors.begin();   }
134   const_succ_iterator  succ_begin() const { return Successors.begin();   }
135   succ_iterator        succ_end()         { return Successors.end();     }
136   const_succ_iterator  succ_end()   const { return Successors.end();     }
137   unsigned             succ_size()  const { return Successors.size();    }
138   bool                 succ_empty() const { return Successors.empty();   }
139
140   // LiveIn management methods.
141
142   /// addLiveIn - Add the specified register as a live in.  Note that it
143   /// is an error to add the same register to the same set more than once.
144   void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
145
146   /// removeLiveIn - Remove the specified register from the live in set.
147   ///
148   void removeLiveIn(unsigned Reg);
149
150   // Iteration support for live in sets.  These sets are kept in sorted
151   // order by their register number.
152   typedef std::vector<unsigned>::iterator       livein_iterator;
153   typedef std::vector<unsigned>::const_iterator const_livein_iterator;
154   livein_iterator       livein_begin()       { return LiveIns.begin(); }
155   const_livein_iterator livein_begin() const { return LiveIns.begin(); }
156   livein_iterator       livein_end()         { return LiveIns.end(); }
157   const_livein_iterator livein_end()   const { return LiveIns.end(); }
158   bool            livein_empty() const { return LiveIns.empty(); }
159
160   /// isLandingPad - Returns true if the block is a landing pad. That is
161   /// this basic block is entered via an exception handler.
162   bool isLandingPad() const { return IsLandingPad; }
163
164   /// setIsLandingPad - Indicates the block is a landing pad.  That is
165   /// this basic block is entered via an exception handler.
166   void setIsLandingPad() { IsLandingPad = true; }
167
168   /// isAccessable - Returns true if the block is alive.  That is, if it has
169   /// predecessors or is an eh landing pad.
170   bool isAccessable() const { return !pred_empty() || isLandingPad(); }
171
172   // Code Layout methods.
173   
174   /// moveBefore/moveAfter - move 'this' block before or after the specified
175   /// block.  This only moves the block, it does not modify the CFG or adjust
176   /// potential fall-throughs at the end of the block.
177   void moveBefore(MachineBasicBlock *NewAfter);
178   void moveAfter(MachineBasicBlock *NewBefore);
179   
180   // Machine-CFG mutators
181   
182   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
183   /// The Predecessors list of succ is automatically updated.
184   ///
185   void addSuccessor(MachineBasicBlock *succ);
186
187   /// removeSuccessor - Remove successor from the successors list of this
188   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
189   ///
190   void removeSuccessor(MachineBasicBlock *succ);
191
192   /// removeSuccessor - Remove specified successor from the successors list of
193   /// this MachineBasicBlock. The Predecessors list of succ is automatically
194   /// updated.
195   ///
196   void removeSuccessor(succ_iterator I);
197   
198   /// isSuccessor - Return true if the specified MBB is a successor of this
199   /// block.
200   bool isSuccessor(MachineBasicBlock *MBB) const {
201     for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
202       if (*I == MBB)
203         return true;
204     return false;
205   }
206
207   /// getFirstTerminator - returns an iterator to the first terminator
208   /// instruction of this basic block. If a terminator does not exist,
209   /// it returns end()
210   iterator getFirstTerminator();
211
212   void pop_front() { Insts.pop_front(); }
213   void pop_back() { Insts.pop_back(); }
214   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
215   template<typename IT>
216   void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
217   iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
218
219   // erase - Remove the specified element or range from the instruction list.
220   // These functions delete any instructions removed.
221   //
222   iterator erase(iterator I)             { return Insts.erase(I); }
223   iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
224   MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
225   void clear()                           { Insts.clear(); }
226
227   /// splice - Take a block of instructions from MBB 'Other' in the range [From,
228   /// To), and insert them into this MBB right before 'where'.
229   void splice(iterator where, MachineBasicBlock *Other, iterator From,
230               iterator To) {
231     Insts.splice(where, Other->Insts, From, To);
232   }
233
234   // Debugging methods.
235   void dump() const;
236   void print(std::ostream &OS) const;
237   void print(std::ostream *OS) const { if (OS) print(*OS); }
238
239   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
240   /// level, unless they're not in a MachineFunction yet, in which case this
241   /// will return -1.
242   ///
243   int getNumber() const { return Number; }
244   void setNumber(int N) { Number = N; }
245
246 private:   // Methods used to maintain doubly linked list of blocks...
247   friend struct ilist_traits<MachineBasicBlock>;
248
249   MachineBasicBlock *getPrev() const { return Prev; }
250   MachineBasicBlock *getNext() const { return Next; }
251   void setPrev(MachineBasicBlock *P) { Prev = P; }
252   void setNext(MachineBasicBlock *N) { Next = N; }
253
254   // Machine-CFG mutators
255
256   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
257   /// Don't do this unless you know what you're doing, because it doesn't
258   /// update pred's successors list. Use pred->addSuccessor instead.
259   ///
260   void addPredecessor(MachineBasicBlock *pred);
261
262   /// removePredecessor - Remove pred as a predecessor of this
263   /// MachineBasicBlock. Don't do this unless you know what you're
264   /// doing, because it doesn't update pred's successors list. Use
265   /// pred->removeSuccessor instead.
266   ///
267   void removePredecessor(MachineBasicBlock *pred);
268 };
269
270 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
271
272 //===--------------------------------------------------------------------===//
273 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
274 //===--------------------------------------------------------------------===//
275
276 // Provide specializations of GraphTraits to be able to treat a
277 // MachineFunction as a graph of MachineBasicBlocks...
278 //
279
280 template <> struct GraphTraits<MachineBasicBlock *> {
281   typedef MachineBasicBlock NodeType;
282   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
283
284   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
285   static inline ChildIteratorType child_begin(NodeType *N) {
286     return N->succ_begin();
287   }
288   static inline ChildIteratorType child_end(NodeType *N) {
289     return N->succ_end();
290   }
291 };
292
293 template <> struct GraphTraits<const MachineBasicBlock *> {
294   typedef const MachineBasicBlock NodeType;
295   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
296
297   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
298   static inline ChildIteratorType child_begin(NodeType *N) {
299     return N->succ_begin();
300   }
301   static inline ChildIteratorType child_end(NodeType *N) {
302     return N->succ_end();
303   }
304 };
305
306 // Provide specializations of GraphTraits to be able to treat a
307 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
308 // in inverse order.  Inverse order for a function is considered
309 // to be when traversing the predecessor edges of a MBB
310 // instead of the successor edges.
311 //
312 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
313   typedef MachineBasicBlock NodeType;
314   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
315   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
316     return G.Graph;
317   }
318   static inline ChildIteratorType child_begin(NodeType *N) {
319     return N->pred_begin();
320   }
321   static inline ChildIteratorType child_end(NodeType *N) {
322     return N->pred_end();
323   }
324 };
325
326 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
327   typedef const MachineBasicBlock NodeType;
328   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
329   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
330     return G.Graph;
331   }
332   static inline ChildIteratorType child_begin(NodeType *N) {
333     return N->pred_begin();
334   }
335   static inline ChildIteratorType child_end(NodeType *N) {
336     return N->pred_end();
337   }
338 };
339
340 } // End llvm namespace
341
342 #endif