MachineBasicBlock.h revision c7f6b8c5d40e17bf43fd3a1549d7d89c9da735e1
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/Support/Streams.h"
20
21namespace llvm {
22
23class BasicBlock;
24class MachineFunction;
25
26template <>
27struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
28private:
29  mutable ilist_node<MachineInstr> Sentinel;
30
31  // this is only set by the MachineBasicBlock owning the LiveList
32  friend class MachineBasicBlock;
33  MachineBasicBlock* Parent;
34
35public:
36  MachineInstr *createSentinel() const {
37    return static_cast<MachineInstr*>(&Sentinel);
38  }
39  void destroySentinel(MachineInstr *) const {}
40
41  void addNodeToList(MachineInstr* N);
42  void removeNodeFromList(MachineInstr* N);
43  void transferNodesFromList(ilist_traits &SrcTraits,
44                             ilist_iterator<MachineInstr> first,
45                             ilist_iterator<MachineInstr> last);
46  void deleteNode(MachineInstr *N);
47private:
48  void createNode(const MachineInstr &);
49};
50
51class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
52  typedef ilist<MachineInstr> Instructions;
53  Instructions Insts;
54  const BasicBlock *BB;
55  int Number;
56  MachineFunction *xParent;
57
58  /// Predecessors/Successors - Keep track of the predecessor / successor
59  /// basicblocks.
60  std::vector<MachineBasicBlock *> Predecessors;
61  std::vector<MachineBasicBlock *> Successors;
62
63  /// LiveIns - Keep track of the physical registers that are livein of
64  /// the basicblock.
65  std::vector<unsigned> LiveIns;
66
67  /// Alignment - Alignment of the basic block. Zero if the basic block does
68  /// not need to be aligned.
69  unsigned Alignment;
70
71  /// IsLandingPad - Indicate that this basic block is entered via an
72  /// exception handler.
73  bool IsLandingPad;
74
75  // Intrusive list support
76  friend struct ilist_sentinel_traits<MachineBasicBlock>;
77  MachineBasicBlock() {}
78
79  explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
80
81  ~MachineBasicBlock();
82
83  // MachineBasicBlocks are allocated and owned by MachineFunction.
84  friend class MachineFunction;
85
86public:
87  /// getBasicBlock - Return the LLVM basic block that this instance
88  /// corresponded to originally.
89  ///
90  const BasicBlock *getBasicBlock() const { return BB; }
91
92  /// getParent - Return the MachineFunction containing this basic block.
93  ///
94  const MachineFunction *getParent() const { return xParent; }
95  MachineFunction *getParent() { return xParent; }
96
97  typedef Instructions::iterator                              iterator;
98  typedef Instructions::const_iterator                  const_iterator;
99  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
100  typedef std::reverse_iterator<iterator>             reverse_iterator;
101
102  unsigned size() const { return (unsigned)Insts.size(); }
103  bool empty() const { return Insts.empty(); }
104
105  MachineInstr& front() { return Insts.front(); }
106  MachineInstr& back()  { return Insts.back(); }
107  const MachineInstr& front() const { return Insts.front(); }
108  const MachineInstr& back()  const { return Insts.back(); }
109
110  iterator                begin()       { return Insts.begin();  }
111  const_iterator          begin() const { return Insts.begin();  }
112  iterator                  end()       { return Insts.end();    }
113  const_iterator            end() const { return Insts.end();    }
114  reverse_iterator       rbegin()       { return Insts.rbegin(); }
115  const_reverse_iterator rbegin() const { return Insts.rbegin(); }
116  reverse_iterator       rend  ()       { return Insts.rend();   }
117  const_reverse_iterator rend  () const { return Insts.rend();   }
118
119  // Machine-CFG iterators
120  typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
121  typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
122  typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
123  typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
124  typedef std::vector<MachineBasicBlock *>::reverse_iterator
125                                                         pred_reverse_iterator;
126  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
127                                                   const_pred_reverse_iterator;
128  typedef std::vector<MachineBasicBlock *>::reverse_iterator
129                                                         succ_reverse_iterator;
130  typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
131                                                   const_succ_reverse_iterator;
132
133  pred_iterator        pred_begin()       { return Predecessors.begin(); }
134  const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
135  pred_iterator        pred_end()         { return Predecessors.end();   }
136  const_pred_iterator  pred_end()   const { return Predecessors.end();   }
137  pred_reverse_iterator        pred_rbegin()
138                                          { return Predecessors.rbegin();}
139  const_pred_reverse_iterator  pred_rbegin() const
140                                          { return Predecessors.rbegin();}
141  pred_reverse_iterator        pred_rend()
142                                          { return Predecessors.rend();  }
143  const_pred_reverse_iterator  pred_rend()   const
144                                          { return Predecessors.rend();  }
145  unsigned             pred_size()  const {
146    return (unsigned)Predecessors.size();
147  }
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 {
162    return (unsigned)Successors.size();
163  }
164  bool                 succ_empty() const { return Successors.empty();   }
165
166  // LiveIn management methods.
167
168  /// addLiveIn - Add the specified register as a live in.  Note that it
169  /// is an error to add the same register to the same set more than once.
170  void addLiveIn(unsigned Reg)  { LiveIns.push_back(Reg); }
171
172  /// removeLiveIn - Remove the specified register from the live in set.
173  ///
174  void removeLiveIn(unsigned Reg);
175
176  /// isLiveIn - Return true if the specified register is in the live in set.
177  ///
178  bool isLiveIn(unsigned Reg) const;
179
180  // Iteration support for live in sets.  These sets are kept in sorted
181  // order by their register number.
182  typedef std::vector<unsigned>::iterator       livein_iterator;
183  typedef std::vector<unsigned>::const_iterator const_livein_iterator;
184  livein_iterator       livein_begin()       { return LiveIns.begin(); }
185  const_livein_iterator livein_begin() const { return LiveIns.begin(); }
186  livein_iterator       livein_end()         { return LiveIns.end(); }
187  const_livein_iterator livein_end()   const { return LiveIns.end(); }
188  bool            livein_empty() const { return LiveIns.empty(); }
189
190  /// getAlignment - Return alignment of the basic block.
191  ///
192  unsigned getAlignment() const { return Alignment; }
193
194  /// setAlignment - Set alignment of the basic block.
195  ///
196  void setAlignment(unsigned Align) { Alignment = Align; }
197
198  /// isLandingPad - Returns true if the block is a landing pad. That is
199  /// this basic block is entered via an exception handler.
200  bool isLandingPad() const { return IsLandingPad; }
201
202  /// setIsLandingPad - Indicates the block is a landing pad.  That is
203  /// this basic block is entered via an exception handler.
204  void setIsLandingPad() { IsLandingPad = true; }
205
206  // Code Layout methods.
207
208  /// moveBefore/moveAfter - move 'this' block before or after the specified
209  /// block.  This only moves the block, it does not modify the CFG or adjust
210  /// potential fall-throughs at the end of the block.
211  void moveBefore(MachineBasicBlock *NewAfter);
212  void moveAfter(MachineBasicBlock *NewBefore);
213
214  // Machine-CFG mutators
215
216  /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
217  /// The Predecessors list of succ is automatically updated.
218  ///
219  void addSuccessor(MachineBasicBlock *succ);
220
221  /// removeSuccessor - Remove successor from the successors list of this
222  /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
223  ///
224  void removeSuccessor(MachineBasicBlock *succ);
225
226  /// removeSuccessor - Remove specified successor from the successors list of
227  /// this MachineBasicBlock. The Predecessors list of succ is automatically
228  /// updated.  Return the iterator to the element after the one removed.
229  ///
230  succ_iterator removeSuccessor(succ_iterator I);
231
232  /// transferSuccessors - Transfers all the successors from MBB to this
233  /// machine basic block (i.e., copies all the successors fromMBB and
234  /// remove all the successors fromBB).
235  void transferSuccessors(MachineBasicBlock *fromMBB);
236
237  /// isSuccessor - Return true if the specified MBB is a successor of this
238  /// block.
239  bool isSuccessor(MachineBasicBlock *MBB) const;
240
241  /// isLayoutSuccessor - Return true if the specified MBB will be emitted
242  /// immediately after this block, such that if this block exits by
243  /// falling through, control will transfer to the specified MBB. Note
244  /// that MBB need not be a successor at all, for example if this block
245  /// ends with an unconditional branch to some other block.
246  bool isLayoutSuccessor(MachineBasicBlock *MBB) const;
247
248  /// getFirstTerminator - returns an iterator to the first terminator
249  /// instruction of this basic block. If a terminator does not exist,
250  /// it returns end()
251  iterator getFirstTerminator();
252
253  void pop_front() { Insts.pop_front(); }
254  void pop_back() { Insts.pop_back(); }
255  void push_back(MachineInstr *MI) { Insts.push_back(MI); }
256  template<typename IT>
257  void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
258  iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
259
260  // erase - Remove the specified element or range from the instruction list.
261  // These functions delete any instructions removed.
262  //
263  iterator erase(iterator I)             { return Insts.erase(I); }
264  iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
265  MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
266  void clear()                           { Insts.clear(); }
267
268  /// splice - Take an instruction from MBB 'Other' at the position From,
269  /// and insert it into this MBB right before 'where'.
270  void splice(iterator where, MachineBasicBlock *Other, iterator From) {
271    Insts.splice(where, Other->Insts, From);
272  }
273
274  /// splice - Take a block of instructions from MBB 'Other' in the range [From,
275  /// To), and insert them into this MBB right before 'where'.
276  void splice(iterator where, MachineBasicBlock *Other, iterator From,
277              iterator To) {
278    Insts.splice(where, Other->Insts, From, To);
279  }
280
281  /// removeFromParent - This method unlinks 'this' from the containing
282  /// function, and returns it, but does not delete it.
283  MachineBasicBlock *removeFromParent();
284
285  /// eraseFromParent - This method unlinks 'this' from the containing
286  /// function and deletes it.
287  void eraseFromParent();
288
289  /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
290  /// 'Old', change the code and CFG so that it branches to 'New' instead.
291  void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
292
293  /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
294  /// the CFG to be inserted.  If we have proven that MBB can only branch to
295  /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
296  /// DestB can be null. Besides DestA and DestB, retain other edges leading
297  /// to LandingPads (currently there can be only one; we don't check or require
298  /// that here). Note it is possible that DestA and/or DestB are LandingPads.
299  bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
300                            MachineBasicBlock *DestB,
301                            bool isCond);
302
303  // Debugging methods.
304  void dump() const;
305  void print(std::ostream &OS) const;
306  void print(std::ostream *OS) const { if (OS) print(*OS); }
307
308  /// getNumber - MachineBasicBlocks are uniquely numbered at the function
309  /// level, unless they're not in a MachineFunction yet, in which case this
310  /// will return -1.
311  ///
312  int getNumber() const { return Number; }
313  void setNumber(int N) { Number = N; }
314
315private:   // Methods used to maintain doubly linked list of blocks...
316  friend struct ilist_traits<MachineBasicBlock>;
317
318  // Machine-CFG mutators
319
320  /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
321  /// Don't do this unless you know what you're doing, because it doesn't
322  /// update pred's successors list. Use pred->addSuccessor instead.
323  ///
324  void addPredecessor(MachineBasicBlock *pred);
325
326  /// removePredecessor - Remove pred as a predecessor of this
327  /// MachineBasicBlock. Don't do this unless you know what you're
328  /// doing, because it doesn't update pred's successors list. Use
329  /// pred->removeSuccessor instead.
330  ///
331  void removePredecessor(MachineBasicBlock *pred);
332};
333
334std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
335
336//===--------------------------------------------------------------------===//
337// GraphTraits specializations for machine basic block graphs (machine-CFGs)
338//===--------------------------------------------------------------------===//
339
340// Provide specializations of GraphTraits to be able to treat a
341// MachineFunction as a graph of MachineBasicBlocks...
342//
343
344template <> struct GraphTraits<MachineBasicBlock *> {
345  typedef MachineBasicBlock NodeType;
346  typedef MachineBasicBlock::succ_iterator ChildIteratorType;
347
348  static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
349  static inline ChildIteratorType child_begin(NodeType *N) {
350    return N->succ_begin();
351  }
352  static inline ChildIteratorType child_end(NodeType *N) {
353    return N->succ_end();
354  }
355};
356
357template <> struct GraphTraits<const MachineBasicBlock *> {
358  typedef const MachineBasicBlock NodeType;
359  typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
360
361  static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
362  static inline ChildIteratorType child_begin(NodeType *N) {
363    return N->succ_begin();
364  }
365  static inline ChildIteratorType child_end(NodeType *N) {
366    return N->succ_end();
367  }
368};
369
370// Provide specializations of GraphTraits to be able to treat a
371// MachineFunction as a graph of MachineBasicBlocks... and to walk it
372// in inverse order.  Inverse order for a function is considered
373// to be when traversing the predecessor edges of a MBB
374// instead of the successor edges.
375//
376template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
377  typedef MachineBasicBlock NodeType;
378  typedef MachineBasicBlock::pred_iterator ChildIteratorType;
379  static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
380    return G.Graph;
381  }
382  static inline ChildIteratorType child_begin(NodeType *N) {
383    return N->pred_begin();
384  }
385  static inline ChildIteratorType child_end(NodeType *N) {
386    return N->pred_end();
387  }
388};
389
390template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
391  typedef const MachineBasicBlock NodeType;
392  typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
393  static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
394    return G.Graph;
395  }
396  static inline ChildIteratorType child_begin(NodeType *N) {
397    return N->pred_begin();
398  }
399  static inline ChildIteratorType child_end(NodeType *N) {
400    return N->pred_end();
401  }
402};
403
404} // End llvm namespace
405
406#endif
407