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