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