Add DebugLoc to the getNode() methods.
[oota-llvm.git] / include / llvm / CodeGen / MachineBasicBlock.h
1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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/Support/Streams.h"
20
21 namespace llvm {
22
23 class BasicBlock;
24 class MachineFunction;
25
26 template <>
27 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
28 private:
29   mutable MachineInstr Sentinel;
30
31   // this is only set by the MachineBasicBlock owning the LiveList
32   friend class MachineBasicBlock;
33   MachineBasicBlock* Parent;
34
35 public:
36   MachineInstr *createSentinel() const { return &Sentinel; }
37   void destroySentinel(MachineInstr *) const {}
38
39   void addNodeToList(MachineInstr* N);
40   void removeNodeFromList(MachineInstr* N);
41   void transferNodesFromList(ilist_traits &SrcTraits,
42                              ilist_iterator<MachineInstr> first,
43                              ilist_iterator<MachineInstr> last);
44   void deleteNode(MachineInstr *N);
45 private:
46   void createNode(const MachineInstr &);
47 };
48
49 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
50   typedef ilist<MachineInstr> Instructions;
51   Instructions Insts;
52   const BasicBlock *BB;
53   int Number;
54   MachineFunction *xParent;
55   
56   /// Predecessors/Successors - Keep track of the predecessor / successor
57   /// basicblocks.
58   std::vector<MachineBasicBlock *> Predecessors;
59   std::vector<MachineBasicBlock *> Successors;
60
61   /// LiveIns - Keep track of the physical registers that are livein of
62   /// the basicblock.
63   std::vector<unsigned> LiveIns;
64
65   /// Alignment - Alignment of the basic block. Zero if the basic block does
66   /// not need to be aligned.
67   unsigned Alignment;
68   
69   /// IsLandingPad - Indicate that this basic block is entered via an
70   /// exception handler.
71   bool IsLandingPad;
72
73   // Intrusive list support
74   friend struct ilist_sentinel_traits<MachineBasicBlock>;
75   MachineBasicBlock() {}
76
77   explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
78
79   ~MachineBasicBlock();
80
81   // MachineBasicBlocks are allocated and owned by MachineFunction.
82   friend class MachineFunction;
83
84 public:
85   /// getBasicBlock - Return the LLVM basic block that this instance
86   /// corresponded to originally.
87   ///
88   const BasicBlock *getBasicBlock() const { return BB; }
89
90   /// getParent - Return the MachineFunction containing this basic block.
91   ///
92   const MachineFunction *getParent() const { return xParent; }
93   MachineFunction *getParent() { return xParent; }
94
95   typedef Instructions::iterator                              iterator;
96   typedef Instructions::const_iterator                  const_iterator;
97   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
98   typedef std::reverse_iterator<iterator>             reverse_iterator;
99
100   unsigned size() const { return (unsigned)Insts.size(); }
101   bool empty() const { return Insts.empty(); }
102
103   MachineInstr& front() { return Insts.front(); }
104   MachineInstr& back()  { return Insts.back(); }
105   const MachineInstr& front() const { return Insts.front(); }
106   const MachineInstr& back()  const { return Insts.back(); }
107
108   iterator                begin()       { return Insts.begin();  }
109   const_iterator          begin() const { return Insts.begin();  }
110   iterator                  end()       { return Insts.end();    }
111   const_iterator            end() const { return Insts.end();    }
112   reverse_iterator       rbegin()       { return Insts.rbegin(); }
113   const_reverse_iterator rbegin() const { return Insts.rbegin(); }
114   reverse_iterator       rend  ()       { return Insts.rend();   }
115   const_reverse_iterator rend  () const { return Insts.rend();   }
116
117   // Machine-CFG iterators
118   typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
119   typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
120   typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
121   typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
122   typedef std::vector<MachineBasicBlock *>::reverse_iterator
123                                                          pred_reverse_iterator;
124   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
125                                                    const_pred_reverse_iterator;
126   typedef std::vector<MachineBasicBlock *>::reverse_iterator
127                                                          succ_reverse_iterator;
128   typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
129                                                    const_succ_reverse_iterator;
130
131   pred_iterator        pred_begin()       { return Predecessors.begin(); }
132   const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
133   pred_iterator        pred_end()         { return Predecessors.end();   }
134   const_pred_iterator  pred_end()   const { return Predecessors.end();   }
135   pred_reverse_iterator        pred_rbegin()
136                                           { return Predecessors.rbegin();}
137   const_pred_reverse_iterator  pred_rbegin() const
138                                           { return Predecessors.rbegin();}
139   pred_reverse_iterator        pred_rend()
140                                           { return Predecessors.rend();  }
141   const_pred_reverse_iterator  pred_rend()   const
142                                           { return Predecessors.rend();  }
143   unsigned             pred_size()  const {
144     return (unsigned)Predecessors.size();
145   }
146   bool                 pred_empty() const { return Predecessors.empty(); }
147   succ_iterator        succ_begin()       { return Successors.begin();   }
148   const_succ_iterator  succ_begin() const { return Successors.begin();   }
149   succ_iterator        succ_end()         { return Successors.end();     }
150   const_succ_iterator  succ_end()   const { return Successors.end();     }
151   succ_reverse_iterator        succ_rbegin()
152                                           { return Successors.rbegin();  }
153   const_succ_reverse_iterator  succ_rbegin() const
154                                           { return Successors.rbegin();  }
155   succ_reverse_iterator        succ_rend()
156                                           { return Successors.rend();    }
157   const_succ_reverse_iterator  succ_rend()   const
158                                           { return Successors.rend();    }
159   unsigned             succ_size()  const {
160     return (unsigned)Successors.size();
161   }
162   bool                 succ_empty() const { return Successors.empty();   }
163
164   // LiveIn management methods.
165
166   /// addLiveIn - Add the specified register as a live in.  Note that it
167   /// is an error to add the same register to the same set more than once.
168   void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
169
170   /// removeLiveIn - Remove the specified register from the live in set.
171   ///
172   void removeLiveIn(unsigned Reg);
173
174   /// isLiveIn - Return true if the specified register is in the live in set.
175   ///
176   bool isLiveIn(unsigned Reg) const;
177
178   // Iteration support for live in sets.  These sets are kept in sorted
179   // order by their register number.
180   typedef std::vector<unsigned>::iterator       livein_iterator;
181   typedef std::vector<unsigned>::const_iterator const_livein_iterator;
182   livein_iterator       livein_begin()       { return LiveIns.begin(); }
183   const_livein_iterator livein_begin() const { return LiveIns.begin(); }
184   livein_iterator       livein_end()         { return LiveIns.end(); }
185   const_livein_iterator livein_end()   const { return LiveIns.end(); }
186   bool            livein_empty() const { return LiveIns.empty(); }
187
188   /// getAlignment - Return alignment of the basic block.
189   ///
190   unsigned getAlignment() const { return Alignment; }
191
192   /// setAlignment - Set alignment of the basic block.
193   ///
194   void setAlignment(unsigned Align) { Alignment = Align; }
195
196   /// isLandingPad - Returns true if the block is a landing pad. That is
197   /// this basic block is entered via an exception handler.
198   bool isLandingPad() const { return IsLandingPad; }
199
200   /// setIsLandingPad - Indicates the block is a landing pad.  That is
201   /// this basic block is entered via an exception handler.
202   void setIsLandingPad() { IsLandingPad = true; }
203
204   // Code Layout methods.
205   
206   /// moveBefore/moveAfter - move 'this' block before or after the specified
207   /// block.  This only moves the block, it does not modify the CFG or adjust
208   /// potential fall-throughs at the end of the block.
209   void moveBefore(MachineBasicBlock *NewAfter);
210   void moveAfter(MachineBasicBlock *NewBefore);
211   
212   // Machine-CFG mutators
213   
214   /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
215   /// The Predecessors list of succ is automatically updated.
216   ///
217   void addSuccessor(MachineBasicBlock *succ);
218
219   /// removeSuccessor - Remove successor from the successors list of this
220   /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
221   ///
222   void removeSuccessor(MachineBasicBlock *succ);
223
224   /// removeSuccessor - Remove specified successor from the successors list of
225   /// this MachineBasicBlock. The Predecessors list of succ is automatically
226   /// updated.  Return the iterator to the element after the one removed.
227   ///
228   succ_iterator removeSuccessor(succ_iterator I);
229   
230   /// transferSuccessors - Transfers all the successors from MBB to this
231   /// machine basic block (i.e., copies all the successors fromMBB and
232   /// remove all the successors fromBB).
233   void transferSuccessors(MachineBasicBlock *fromMBB);
234   
235   /// isSuccessor - Return true if the specified MBB is a successor of this
236   /// block.
237   bool isSuccessor(MachineBasicBlock *MBB) const;
238
239   /// isLayoutSuccessor - Return true if the specified MBB will be emitted
240   /// immediately after this block, such that if this block exits by
241   /// falling through, control will transfer to the specified MBB. Note
242   /// that MBB need not be a successor at all, for example if this block
243   /// ends with an unconditional branch to some other block.
244   bool isLayoutSuccessor(MachineBasicBlock *MBB) const;
245
246   /// getFirstTerminator - returns an iterator to the first terminator
247   /// instruction of this basic block. If a terminator does not exist,
248   /// it returns end()
249   iterator getFirstTerminator();
250
251   void pop_front() { Insts.pop_front(); }
252   void pop_back() { Insts.pop_back(); }
253   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
254   template<typename IT>
255   void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
256   iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
257
258   // erase - Remove the specified element or range from the instruction list.
259   // These functions delete any instructions removed.
260   //
261   iterator erase(iterator I)             { return Insts.erase(I); }
262   iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
263   MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
264   void clear()                           { Insts.clear(); }
265
266   /// splice - Take an instruction from MBB 'Other' at the position From,
267   /// and insert it into this MBB right before 'where'.
268   void splice(iterator where, MachineBasicBlock *Other, iterator From) {
269     Insts.splice(where, Other->Insts, From);
270   }
271
272   /// splice - Take a block of instructions from MBB 'Other' in the range [From,
273   /// To), and insert them into this MBB right before 'where'.
274   void splice(iterator where, MachineBasicBlock *Other, iterator From,
275               iterator To) {
276     Insts.splice(where, Other->Insts, From, To);
277   }
278
279   /// removeFromParent - This method unlinks 'this' from the containing
280   /// function, and returns it, but does not delete it.
281   MachineBasicBlock *removeFromParent();
282   
283   /// eraseFromParent - This method unlinks 'this' from the containing
284   /// function and deletes it.
285   void eraseFromParent();
286
287   /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
288   /// 'Old', change the code and CFG so that it branches to 'New' instead.
289   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
290
291   /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
292   /// the CFG to be inserted.  If we have proven that MBB can only branch to
293   /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
294   /// DestB can be null. Besides DestA and DestB, retain other edges leading
295   /// to LandingPads (currently there can be only one; we don't check or require
296   /// that here). Note it is possible that DestA and/or DestB are LandingPads.
297   bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
298                             MachineBasicBlock *DestB,
299                             bool isCond);
300
301   // Debugging methods.
302   void dump() const;
303   void print(std::ostream &OS) const;
304   void print(std::ostream *OS) const { if (OS) print(*OS); }
305
306   /// getNumber - MachineBasicBlocks are uniquely numbered at the function
307   /// level, unless they're not in a MachineFunction yet, in which case this
308   /// will return -1.
309   ///
310   int getNumber() const { return Number; }
311   void setNumber(int N) { Number = N; }
312
313 private:   // Methods used to maintain doubly linked list of blocks...
314   friend struct ilist_traits<MachineBasicBlock>;
315
316   // Machine-CFG mutators
317
318   /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
319   /// Don't do this unless you know what you're doing, because it doesn't
320   /// update pred's successors list. Use pred->addSuccessor instead.
321   ///
322   void addPredecessor(MachineBasicBlock *pred);
323
324   /// removePredecessor - Remove pred as a predecessor of this
325   /// MachineBasicBlock. Don't do this unless you know what you're
326   /// doing, because it doesn't update pred's successors list. Use
327   /// pred->removeSuccessor instead.
328   ///
329   void removePredecessor(MachineBasicBlock *pred);
330 };
331
332 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
333
334 //===--------------------------------------------------------------------===//
335 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
336 //===--------------------------------------------------------------------===//
337
338 // Provide specializations of GraphTraits to be able to treat a
339 // MachineFunction as a graph of MachineBasicBlocks...
340 //
341
342 template <> struct GraphTraits<MachineBasicBlock *> {
343   typedef MachineBasicBlock NodeType;
344   typedef MachineBasicBlock::succ_iterator ChildIteratorType;
345
346   static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
347   static inline ChildIteratorType child_begin(NodeType *N) {
348     return N->succ_begin();
349   }
350   static inline ChildIteratorType child_end(NodeType *N) {
351     return N->succ_end();
352   }
353 };
354
355 template <> struct GraphTraits<const MachineBasicBlock *> {
356   typedef const MachineBasicBlock NodeType;
357   typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
358
359   static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
360   static inline ChildIteratorType child_begin(NodeType *N) {
361     return N->succ_begin();
362   }
363   static inline ChildIteratorType child_end(NodeType *N) {
364     return N->succ_end();
365   }
366 };
367
368 // Provide specializations of GraphTraits to be able to treat a
369 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
370 // in inverse order.  Inverse order for a function is considered
371 // to be when traversing the predecessor edges of a MBB
372 // instead of the successor edges.
373 //
374 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
375   typedef MachineBasicBlock NodeType;
376   typedef MachineBasicBlock::pred_iterator ChildIteratorType;
377   static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
378     return G.Graph;
379   }
380   static inline ChildIteratorType child_begin(NodeType *N) {
381     return N->pred_begin();
382   }
383   static inline ChildIteratorType child_end(NodeType *N) {
384     return N->pred_end();
385   }
386 };
387
388 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
389   typedef const MachineBasicBlock NodeType;
390   typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
391   static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
392     return G.Graph;
393   }
394   static inline ChildIteratorType child_begin(NodeType *N) {
395     return N->pred_begin();
396   }
397   static inline ChildIteratorType child_end(NodeType *N) {
398     return N->pred_end();
399   }
400 };
401
402 } // End llvm namespace
403
404 #endif