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