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