MachineBasicBlock.h revision a971dbdde27fd4ff53dbebdd4aaf87826d081aa2
1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// License. See LICENSE.TXT for details.
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//===----------------------------------------------------------------------===//
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Collect the sequence of machine instructions for a basic block.
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//===----------------------------------------------------------------------===//
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define LLVM_CODEGEN_MACHINEBASICBLOCK_H
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/CodeGen/MachineInstr.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/ADT/GraphTraits.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/ADT/ilist"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Support/Streams.h"
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace llvm {
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  class MachineFunction;
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// ilist_traits
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)template <>
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)struct ilist_traits<MachineInstr> {
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)protected:
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // this is only set by the MachineBasicBlock owning the ilist
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  friend class MachineBasicBlock;
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  MachineBasicBlock* parent;
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)public:
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ilist_traits<MachineInstr>() : parent(0) { }
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  static MachineInstr* getPrev(MachineInstr* N) { return N->Prev; }
3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  static MachineInstr* getNext(MachineInstr* N) { return N->Next; }
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static const MachineInstr*
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  getPrev(const MachineInstr* N) { return N->Prev; }
4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static const MachineInstr*
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  getNext(const MachineInstr* N) { return N->Next; }
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
4546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  static void setPrev(MachineInstr* N, MachineInstr* prev) { N->Prev = prev; }
4646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  static void setNext(MachineInstr* N, MachineInstr* next) { N->Next = next; }
4746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static MachineInstr* createSentinel();
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static void destroySentinel(MachineInstr *MI) { delete MI; }
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void addNodeToList(MachineInstr* N);
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void removeNodeFromList(MachineInstr* N);
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  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  /// Alignment - Alignment of the basic block. Zero if the basic block does
80  /// not need to be aligned.
81  unsigned Alignment;
82
83  /// IsLandingPad - Indicate that this basic block is entered via an
84  /// exception handler.
85  bool IsLandingPad;
86
87public:
88  explicit MachineBasicBlock(const BasicBlock *bb = 0)
89    : Prev(0), Next(0), BB(bb), Number(-1), xParent(0),
90      Alignment(0), IsLandingPad(false) {
91    Insts.parent = this;
92  }
93
94  ~MachineBasicBlock();
95
96  /// getBasicBlock - Return the LLVM basic block that this instance
97  /// corresponded to originally.
98  ///
99  const BasicBlock *getBasicBlock() const { return BB; }
100
101  /// getParent - Return the MachineFunction containing this basic block.
102  ///
103  const MachineFunction *getParent() const { return xParent; }
104  MachineFunction *getParent() { return xParent; }
105
106  typedef ilist<MachineInstr>::iterator                       iterator;
107  typedef ilist<MachineInstr>::const_iterator           const_iterator;
108  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
109  typedef std::reverse_iterator<iterator>             reverse_iterator;
110
111  unsigned size() const { return Insts.size(); }
112  bool empty() const { return Insts.empty(); }
113
114  MachineInstr& front() { return Insts.front(); }
115  MachineInstr& back()  { return Insts.back(); }
116
117  iterator                begin()       { return Insts.begin();  }
118  const_iterator          begin() const { return Insts.begin();  }
119  iterator                  end()       { return Insts.end();    }
120  const_iterator            end() const { return Insts.end();    }
121  reverse_iterator       rbegin()       { return Insts.rbegin(); }
122  const_reverse_iterator rbegin() const { return Insts.rbegin(); }
123  reverse_iterator       rend  ()       { return Insts.rend();   }
124  const_reverse_iterator rend  () const { return Insts.rend();   }
125
126  // Machine-CFG iterators
127  typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
128  typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
129  typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
130  typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
131  typedef std::vector<MachineBasicBlock *>::reverse_iterator
132                                                         pred_reverse_iterator;
133  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
134                                                   const_pred_reverse_iterator;
135  typedef std::vector<MachineBasicBlock *>::reverse_iterator
136                                                         succ_reverse_iterator;
137  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
138                                                   const_succ_reverse_iterator;
139
140  pred_iterator        pred_begin()       { return Predecessors.begin(); }
141  const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
142  pred_iterator        pred_end()         { return Predecessors.end();   }
143  const_pred_iterator  pred_end()   const { return Predecessors.end();   }
144  pred_reverse_iterator        pred_rbegin()
145                                          { return Predecessors.rbegin();}
146  const_pred_reverse_iterator  pred_rbegin() const
147                                          { return Predecessors.rbegin();}
148  pred_reverse_iterator        pred_rend()
149                                          { return Predecessors.rend();  }
150  const_pred_reverse_iterator  pred_rend()   const
151                                          { return Predecessors.rend();  }
152  unsigned             pred_size()  const { return Predecessors.size();  }
153  bool                 pred_empty() const { return Predecessors.empty(); }
154  succ_iterator        succ_begin()       { return Successors.begin();   }
155  const_succ_iterator  succ_begin() const { return Successors.begin();   }
156  succ_iterator        succ_end()         { return Successors.end();     }
157  const_succ_iterator  succ_end()   const { return Successors.end();     }
158  succ_reverse_iterator        succ_rbegin()
159                                          { return Successors.rbegin();  }
160  const_succ_reverse_iterator  succ_rbegin() const
161                                          { return Successors.rbegin();  }
162  succ_reverse_iterator        succ_rend()
163                                          { return Successors.rend();    }
164  const_succ_reverse_iterator  succ_rend()   const
165                                          { return Successors.rend();    }
166  unsigned             succ_size()  const { return Successors.size();    }
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  /// isSuccessor - Return true if the specified MBB is a successor of this
236  /// block.
237  bool isSuccessor(MachineBasicBlock *MBB) const;
238
239  /// getFirstTerminator - returns an iterator to the first terminator
240  /// instruction of this basic block. If a terminator does not exist,
241  /// it returns end()
242  iterator getFirstTerminator();
243
244  void pop_front() { Insts.pop_front(); }
245  void pop_back() { Insts.pop_back(); }
246  void push_back(MachineInstr *MI) { Insts.push_back(MI); }
247  template<typename IT>
248  void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
249  iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
250
251  // erase - Remove the specified element or range from the instruction list.
252  // These functions delete any instructions removed.
253  //
254  iterator erase(iterator I)             { return Insts.erase(I); }
255  iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
256  MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
257  void clear()                           { Insts.clear(); }
258
259  /// splice - Take a block of instructions from MBB 'Other' in the range [From,
260  /// To), and insert them into this MBB right before 'where'.
261  void splice(iterator where, MachineBasicBlock *Other, iterator From,
262              iterator To) {
263    Insts.splice(where, Other->Insts, From, To);
264  }
265
266  /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
267  /// 'Old', change the code and CFG so that it branches to 'New' instead.
268  void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
269
270  /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
271  /// the CFG to be inserted.  If we have proven that MBB can only branch to
272  /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
273  /// DestB can be null. Besides DestA and DestB, retain other edges leading
274  /// to LandingPads (currently there can be only one; we don't check or require
275  /// that here). Note it is possible that DestA and/or DestB are LandingPads.
276  bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
277                            MachineBasicBlock *DestB,
278                            bool isCond);
279
280  // Debugging methods.
281  void dump() const;
282  void print(std::ostream &OS) const;
283  void print(std::ostream *OS) const { if (OS) print(*OS); }
284
285  /// getNumber - MachineBasicBlocks are uniquely numbered at the function
286  /// level, unless they're not in a MachineFunction yet, in which case this
287  /// will return -1.
288  ///
289  int getNumber() const { return Number; }
290  void setNumber(int N) { Number = N; }
291
292private:   // Methods used to maintain doubly linked list of blocks...
293  friend struct ilist_traits<MachineBasicBlock>;
294
295  MachineBasicBlock *getPrev() const { return Prev; }
296  MachineBasicBlock *getNext() const { return Next; }
297  void setPrev(MachineBasicBlock *P) { Prev = P; }
298  void setNext(MachineBasicBlock *N) { Next = N; }
299
300  // Machine-CFG mutators
301
302  /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
303  /// Don't do this unless you know what you're doing, because it doesn't
304  /// update pred's successors list. Use pred->addSuccessor instead.
305  ///
306  void addPredecessor(MachineBasicBlock *pred);
307
308  /// removePredecessor - Remove pred as a predecessor of this
309  /// MachineBasicBlock. Don't do this unless you know what you're
310  /// doing, because it doesn't update pred's successors list. Use
311  /// pred->removeSuccessor instead.
312  ///
313  void removePredecessor(MachineBasicBlock *pred);
314};
315
316std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
317
318//===--------------------------------------------------------------------===//
319// GraphTraits specializations for machine basic block graphs (machine-CFGs)
320//===--------------------------------------------------------------------===//
321
322// Provide specializations of GraphTraits to be able to treat a
323// MachineFunction as a graph of MachineBasicBlocks...
324//
325
326template <> struct GraphTraits<MachineBasicBlock *> {
327  typedef MachineBasicBlock NodeType;
328  typedef MachineBasicBlock::succ_iterator ChildIteratorType;
329
330  static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
331  static inline ChildIteratorType child_begin(NodeType *N) {
332    return N->succ_begin();
333  }
334  static inline ChildIteratorType child_end(NodeType *N) {
335    return N->succ_end();
336  }
337};
338
339template <> struct GraphTraits<const MachineBasicBlock *> {
340  typedef const MachineBasicBlock NodeType;
341  typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
342
343  static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
344  static inline ChildIteratorType child_begin(NodeType *N) {
345    return N->succ_begin();
346  }
347  static inline ChildIteratorType child_end(NodeType *N) {
348    return N->succ_end();
349  }
350};
351
352// Provide specializations of GraphTraits to be able to treat a
353// MachineFunction as a graph of MachineBasicBlocks... and to walk it
354// in inverse order.  Inverse order for a function is considered
355// to be when traversing the predecessor edges of a MBB
356// instead of the successor edges.
357//
358template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
359  typedef MachineBasicBlock NodeType;
360  typedef MachineBasicBlock::pred_iterator ChildIteratorType;
361  static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
362    return G.Graph;
363  }
364  static inline ChildIteratorType child_begin(NodeType *N) {
365    return N->pred_begin();
366  }
367  static inline ChildIteratorType child_end(NodeType *N) {
368    return N->pred_end();
369  }
370};
371
372template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
373  typedef const MachineBasicBlock NodeType;
374  typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
375  static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
376    return G.Graph;
377  }
378  static inline ChildIteratorType child_begin(NodeType *N) {
379    return N->pred_begin();
380  }
381  static inline ChildIteratorType child_end(NodeType *N) {
382    return N->pred_end();
383  }
384};
385
386} // End llvm namespace
387
388#endif
389