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