MachineBasicBlock.h revision 61d3d5c06b279cce1f2b68af4de82f635910578a
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 "Support/ilist"
19#include <iosfwd>
20
21namespace llvm {
22  class MachineFunction;
23
24// ilist_traits
25template <>
26class ilist_traits<MachineInstr> {
27  // this is only set by the MachineBasicBlock owning the ilist
28  friend class MachineBasicBlock;
29  MachineBasicBlock* parent;
30
31public:
32  ilist_traits<MachineInstr>() : parent(0) { }
33
34  static MachineInstr* getPrev(MachineInstr* N) { return N->prev; }
35  static MachineInstr* getNext(MachineInstr* N) { return N->next; }
36
37  static const MachineInstr*
38  getPrev(const MachineInstr* N) { return N->prev; }
39
40  static const MachineInstr*
41  getNext(const MachineInstr* N) { return N->next; }
42
43  static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; }
44  static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; }
45
46  static MachineInstr* createNode();
47  void addNodeToList(MachineInstr* N);
48  void removeNodeFromList(MachineInstr* N);
49  void transferNodesFromList(
50      iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
51      ilist_iterator<MachineInstr> first,
52      ilist_iterator<MachineInstr> last);
53};
54
55class BasicBlock;
56
57class MachineBasicBlock {
58public:
59  typedef ilist<MachineInstr> Instructions;
60  Instructions Insts;
61  MachineBasicBlock *Prev, *Next;
62  const BasicBlock *BB;
63  std::vector<MachineBasicBlock *> Predecessors;
64  std::vector<MachineBasicBlock *> Successors;
65
66public:
67  MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb) {
68    Insts.parent = this;
69  }
70  ~MachineBasicBlock() {}
71
72  /// getBasicBlock - Return the LLVM basic block that this instance
73  /// corresponded to originally.
74  ///
75  const BasicBlock *getBasicBlock() const { return BB; }
76
77  /// getParent - Return the MachineFunction containing this basic block.
78  ///
79  const MachineFunction *getParent() const;
80
81  typedef ilist<MachineInstr>::iterator                       iterator;
82  typedef ilist<MachineInstr>::const_iterator           const_iterator;
83  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
84  typedef std::reverse_iterator<iterator>             reverse_iterator;
85
86  unsigned size() const { return Insts.size(); }
87  bool empty() const { return Insts.empty(); }
88
89  MachineInstr& front() { return Insts.front(); }
90  MachineInstr& back()  { return Insts.back(); }
91
92  iterator                begin()       { return Insts.begin();  }
93  const_iterator          begin() const { return Insts.begin();  }
94  iterator                  end()       { return Insts.end();    }
95  const_iterator            end() const { return Insts.end();    }
96  reverse_iterator       rbegin()       { return Insts.rbegin(); }
97  const_reverse_iterator rbegin() const { return Insts.rbegin(); }
98  reverse_iterator       rend  ()       { return Insts.rend();   }
99  const_reverse_iterator rend  () const { return Insts.rend();   }
100
101  // Machine-CFG iterators
102  typedef std::vector<MachineBasicBlock *>::iterator       pred_iterator;
103  typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
104  typedef std::vector<MachineBasicBlock *>::iterator       succ_iterator;
105  typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
106
107  pred_iterator        pred_begin()       { return Predecessors.begin (); }
108  const_pred_iterator  pred_begin() const { return Predecessors.begin (); }
109  pred_iterator        pred_end()         { return Predecessors.end ();   }
110  const_pred_iterator  pred_end()   const { return Predecessors.end ();   }
111  succ_iterator        succ_begin()       { return Successors.begin ();   }
112  const_succ_iterator  succ_begin() const { return Successors.begin ();   }
113  succ_iterator        succ_end()         { return Successors.end ();     }
114  const_succ_iterator  succ_end()   const { return Successors.end ();     }
115
116  // Machine-CFG mutators
117
118  /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
119  /// The Predecessors list of succ is automatically updated.
120  ///
121  void addSuccessor (MachineBasicBlock *succ) {
122    assert (std::find (Successors.begin (), Successors.end (), succ)
123            == Successors.end ()
124            && "Trying to addSuccessor a MBB which is already my successor");
125    Successors.push_back (succ);
126    succ->addPredecessor (this);
127  }
128
129  /// removeSuccessor - Remove succ from the successors list of this
130  /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
131  ///
132  void removeSuccessor (MachineBasicBlock *succ) {
133    succ->removePredecessor (this);
134    std::vector<MachineBasicBlock *>::iterator goner =
135      std::find (Successors.begin(), Successors.end (), succ);
136    assert (goner != Successors.end ()
137            && "Trying to removeSuccessor a MBB which isn't my successor");
138    Successors.erase (goner);
139  }
140
141private:
142  /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
143  /// Don't do this unless you know what you're doing, because it doesn't
144  /// update pred's successors list. Use pred->addSuccessor instead.
145  ///
146  void addPredecessor (MachineBasicBlock *pred) {
147    assert(std::find (Predecessors.begin (), Predecessors.end (), pred)
148           == Predecessors.end ()
149           && "Trying to addPredecessor a MBB which is already my predecessor");
150    Predecessors.push_back (pred);
151  }
152
153  /// removePredecessor - Remove pred as a predecessor of this
154  /// MachineBasicBlock. Don't do this unless you know what you're
155  /// doing, because it doesn't update pred's successors list. Use
156  /// pred->removeSuccessor instead.
157  ///
158  void removePredecessor (MachineBasicBlock *pred) {
159    std::vector<MachineBasicBlock *>::iterator goner =
160      std::find (Predecessors.begin(), Predecessors.end (), pred);
161    assert (goner != Predecessors.end ()
162            && "Trying to removePredecessor a MBB which isn't my predecessor");
163    Predecessors.erase (goner);
164  }
165
166public:
167  /// getFirstTerminator - returns an iterator to the first terminator
168  /// instruction of this basic block. If a terminator does not exist,
169  /// it returns end()
170  iterator getFirstTerminator();
171
172  void push_back(MachineInstr *MI) { Insts.push_back(MI); }
173  template<typename IT>
174  void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
175  iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
176
177  // erase - Remove the specified element or range from the instruction list.
178  // These functions delete any instructions removed.
179  //
180  iterator erase(iterator I)             { return Insts.erase(I); }
181  iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
182  MachineInstr *remove(MachineInstr *I)  { return Insts.remove(I); }
183  void clear()                           { Insts.clear(); }
184
185  // Debugging methods.
186  void dump() const;
187  void print(std::ostream &OS) const;
188
189private:   // Methods used to maintain doubly linked list of blocks...
190  friend class ilist_traits<MachineBasicBlock>;
191
192  MachineBasicBlock *getPrev() const { return Prev; }
193  MachineBasicBlock *getNext() const { return Next; }
194  void setPrev(MachineBasicBlock *P) { Prev = P; }
195  void setNext(MachineBasicBlock *N) { Next = N; }
196};
197
198} // End llvm namespace
199
200#endif
201