MachineBasicBlock.h revision f20c1a497fe3922ac718429d65a5fe396890575e
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/ADT/ilist"
20#include "llvm/Support/Streams.h"
21
22namespace llvm {
23  class MachineFunction;
24
25// ilist_traits
26template <>
27struct ilist_traits<MachineInstr> {
28protected:
29  // this is only set by the MachineBasicBlock owning the ilist
30  friend class MachineBasicBlock;
31  MachineBasicBlock* parent;
32
33public:
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
58class BasicBlock;
59
60class MachineBasicBlock {
61  typedef ilist<MachineInstr> Instructions;
62  Instructions Insts;
63  MachineBasicBlock *Prev, *Next;
64  const BasicBlock *BB;
65  int Number;
66  MachineFunction *xParent;
67
68  void setParent(MachineFunction *P) { xParent = P; }
69
70  /// Predecessors/Successors - Keep track of the predecessor / successor
71  /// basicblocks.
72  std::vector<MachineBasicBlock *> Predecessors;
73  std::vector<MachineBasicBlock *> Successors;
74
75  /// LiveIns - Keep track of the physical registers that are livein of
76  /// the basicblock.
77  std::vector<unsigned> LiveIns;
78
79  /// IsLandingPad - Indicate that this basic block is entered via an
80  /// exception handler.
81  bool IsLandingPad;
82
83public:
84  explicit MachineBasicBlock(const BasicBlock *bb = 0)
85    : Prev(0), Next(0), BB(bb), Number(-1), xParent(0), 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 xParent; }
99  MachineFunction *getParent() { return xParent; }
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  typedef std::vector<MachineBasicBlock *>::reverse_iterator
127                                                         pred_reverse_iterator;
128  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
129                                                   const_pred_reverse_iterator;
130  typedef std::vector<MachineBasicBlock *>::reverse_iterator
131                                                         succ_reverse_iterator;
132  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
133                                                   const_succ_reverse_iterator;
134
135  pred_iterator        pred_begin()       { return Predecessors.begin(); }
136  const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
137  pred_iterator        pred_end()         { return Predecessors.end();   }
138  const_pred_iterator  pred_end()   const { return Predecessors.end();   }
139  pred_reverse_iterator        pred_rbegin()
140                                          { return Predecessors.rbegin();}
141  const_pred_reverse_iterator  pred_rbegin() const
142                                          { return Predecessors.rbegin();}
143  pred_reverse_iterator        pred_rend()
144                                          { return Predecessors.rend();  }
145  const_pred_reverse_iterator  pred_rend()   const
146                                          { return Predecessors.rend();  }
147  unsigned             pred_size()  const { return Predecessors.size();  }
148  bool                 pred_empty() const { return Predecessors.empty(); }
149  succ_iterator        succ_begin()       { return Successors.begin();   }
150  const_succ_iterator  succ_begin() const { return Successors.begin();   }
151  succ_iterator        succ_end()         { return Successors.end();     }
152  const_succ_iterator  succ_end()   const { return Successors.end();     }
153  succ_reverse_iterator        succ_rbegin()
154                                          { return Successors.rbegin();  }
155  const_succ_reverse_iterator  succ_rbegin() const
156                                          { return Successors.rbegin();  }
157  succ_reverse_iterator        succ_rend()
158                                          { return Successors.rend();    }
159  const_succ_reverse_iterator  succ_rend()   const
160                                          { return Successors.rend();    }
161  unsigned             succ_size()  const { return Successors.size();    }
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  // Iteration support for live in sets.  These sets are kept in sorted
175  // order by their register number.
176  typedef std::vector<unsigned>::iterator       livein_iterator;
177  typedef std::vector<unsigned>::const_iterator const_livein_iterator;
178  livein_iterator       livein_begin()       { return LiveIns.begin(); }
179  const_livein_iterator livein_begin() const { return LiveIns.begin(); }
180  livein_iterator       livein_end()         { return LiveIns.end(); }
181  const_livein_iterator livein_end()   const { return LiveIns.end(); }
182  bool            livein_empty() const { return LiveIns.empty(); }
183
184  /// isLandingPad - Returns true if the block is a landing pad. That is
185  /// this basic block is entered via an exception handler.
186  bool isLandingPad() const { return IsLandingPad; }
187
188  /// setIsLandingPad - Indicates the block is a landing pad.  That is
189  /// this basic block is entered via an exception handler.
190  void setIsLandingPad() { IsLandingPad = true; }
191
192  // Code Layout methods.
193
194  /// moveBefore/moveAfter - move 'this' block before or after the specified
195  /// block.  This only moves the block, it does not modify the CFG or adjust
196  /// potential fall-throughs at the end of the block.
197  void moveBefore(MachineBasicBlock *NewAfter);
198  void moveAfter(MachineBasicBlock *NewBefore);
199
200  // Machine-CFG mutators
201
202  /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
203  /// The Predecessors list of succ is automatically updated.
204  ///
205  void addSuccessor(MachineBasicBlock *succ);
206
207  /// removeSuccessor - Remove successor from the successors list of this
208  /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
209  ///
210  void removeSuccessor(MachineBasicBlock *succ);
211
212  /// removeSuccessor - Remove specified successor from the successors list of
213  /// this MachineBasicBlock. The Predecessors list of succ is automatically
214  /// updated.  Return the iterator to the element after the one removed.
215  ///
216  succ_iterator removeSuccessor(succ_iterator I);
217
218  /// isSuccessor - Return true if the specified MBB is a successor of this
219  /// block.
220  bool isSuccessor(MachineBasicBlock *MBB) const;
221
222  /// getFirstTerminator - returns an iterator to the first terminator
223  /// instruction of this basic block. If a terminator does not exist,
224  /// it returns end()
225  iterator getFirstTerminator();
226
227  void pop_front() { Insts.pop_front(); }
228  void pop_back() { Insts.pop_back(); }
229  void push_back(MachineInstr *MI) { Insts.push_back(MI); }
230  template<typename IT>
231  void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
232  iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
233
234  // erase - Remove the specified element or range from the instruction list.
235  // These functions delete any instructions removed.
236  //
237  iterator erase(iterator I)             { return Insts.erase(I); }
238  iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
239  MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
240  void clear()                           { Insts.clear(); }
241
242  /// splice - Take a block of instructions from MBB 'Other' in the range [From,
243  /// To), and insert them into this MBB right before 'where'.
244  void splice(iterator where, MachineBasicBlock *Other, iterator From,
245              iterator To) {
246    Insts.splice(where, Other->Insts, From, To);
247  }
248
249  /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
250  /// 'Old', change the code and CFG so that it branches to 'New' instead.
251  void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
252
253  /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
254  /// the CFG to be inserted.  If we have proven that MBB can only branch to
255  /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
256  /// DestB can be null. Besides DestA and DestB, retain other edges leading
257  /// to LandingPads (currently there can be only one; we don't check or require
258  /// that here). Note it is possible that DestA and/or DestB are LandingPads.
259  bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
260                            MachineBasicBlock *DestB,
261                            bool isCond);
262
263  // Debugging methods.
264  void dump() const;
265  void print(std::ostream &OS) const;
266  void print(std::ostream *OS) const { if (OS) print(*OS); }
267
268  /// getNumber - MachineBasicBlocks are uniquely numbered at the function
269  /// level, unless they're not in a MachineFunction yet, in which case this
270  /// will return -1.
271  ///
272  int getNumber() const { return Number; }
273  void setNumber(int N) { Number = N; }
274
275private:   // Methods used to maintain doubly linked list of blocks...
276  friend struct ilist_traits<MachineBasicBlock>;
277
278  MachineBasicBlock *getPrev() const { return Prev; }
279  MachineBasicBlock *getNext() const { return Next; }
280  void setPrev(MachineBasicBlock *P) { Prev = P; }
281  void setNext(MachineBasicBlock *N) { Next = N; }
282
283  // Machine-CFG mutators
284
285  /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
286  /// Don't do this unless you know what you're doing, because it doesn't
287  /// update pred's successors list. Use pred->addSuccessor instead.
288  ///
289  void addPredecessor(MachineBasicBlock *pred);
290
291  /// removePredecessor - Remove pred as a predecessor of this
292  /// MachineBasicBlock. Don't do this unless you know what you're
293  /// doing, because it doesn't update pred's successors list. Use
294  /// pred->removeSuccessor instead.
295  ///
296  void removePredecessor(MachineBasicBlock *pred);
297};
298
299std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
300
301//===--------------------------------------------------------------------===//
302// GraphTraits specializations for machine basic block graphs (machine-CFGs)
303//===--------------------------------------------------------------------===//
304
305// Provide specializations of GraphTraits to be able to treat a
306// MachineFunction as a graph of MachineBasicBlocks...
307//
308
309template <> struct GraphTraits<MachineBasicBlock *> {
310  typedef MachineBasicBlock NodeType;
311  typedef MachineBasicBlock::succ_iterator ChildIteratorType;
312
313  static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
314  static inline ChildIteratorType child_begin(NodeType *N) {
315    return N->succ_begin();
316  }
317  static inline ChildIteratorType child_end(NodeType *N) {
318    return N->succ_end();
319  }
320};
321
322template <> struct GraphTraits<const MachineBasicBlock *> {
323  typedef const MachineBasicBlock NodeType;
324  typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
325
326  static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
327  static inline ChildIteratorType child_begin(NodeType *N) {
328    return N->succ_begin();
329  }
330  static inline ChildIteratorType child_end(NodeType *N) {
331    return N->succ_end();
332  }
333};
334
335// Provide specializations of GraphTraits to be able to treat a
336// MachineFunction as a graph of MachineBasicBlocks... and to walk it
337// in inverse order.  Inverse order for a function is considered
338// to be when traversing the predecessor edges of a MBB
339// instead of the successor edges.
340//
341template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
342  typedef MachineBasicBlock NodeType;
343  typedef MachineBasicBlock::pred_iterator ChildIteratorType;
344  static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
345    return G.Graph;
346  }
347  static inline ChildIteratorType child_begin(NodeType *N) {
348    return N->pred_begin();
349  }
350  static inline ChildIteratorType child_end(NodeType *N) {
351    return N->pred_end();
352  }
353};
354
355template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
356  typedef const MachineBasicBlock NodeType;
357  typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
358  static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
359    return G.Graph;
360  }
361  static inline ChildIteratorType child_begin(NodeType *N) {
362    return N->pred_begin();
363  }
364  static inline ChildIteratorType child_end(NodeType *N) {
365    return N->pred_end();
366  }
367};
368
369} // End llvm namespace
370
371#endif
372