1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Collect the sequence of machine instructions for a basic block.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "Support/GraphTraits.h"
19 #include "Support/ilist"
23 class MachineFunction;
27 class ilist_traits<MachineInstr> {
28 // this is only set by the MachineBasicBlock owning the ilist
29 friend class MachineBasicBlock;
30 MachineBasicBlock* parent;
33 ilist_traits<MachineInstr>() : parent(0) { }
35 static MachineInstr* getPrev(MachineInstr* N) { return N->prev; }
36 static MachineInstr* getNext(MachineInstr* N) { return N->next; }
38 static const MachineInstr*
39 getPrev(const MachineInstr* N) { return N->prev; }
41 static const MachineInstr*
42 getNext(const MachineInstr* N) { return N->next; }
44 static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; }
45 static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; }
47 static MachineInstr* createNode();
48 void addNodeToList(MachineInstr* N);
49 void removeNodeFromList(MachineInstr* N);
50 void transferNodesFromList(
51 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
52 ilist_iterator<MachineInstr> first,
53 ilist_iterator<MachineInstr> last);
58 class MachineBasicBlock {
60 typedef ilist<MachineInstr> Instructions;
62 MachineBasicBlock *Prev, *Next;
64 std::vector<MachineBasicBlock *> Predecessors;
65 std::vector<MachineBasicBlock *> Successors;
68 MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb) {
71 ~MachineBasicBlock() {}
73 /// getBasicBlock - Return the LLVM basic block that this instance
74 /// corresponded to originally.
76 const BasicBlock *getBasicBlock() const { return BB; }
78 /// getParent - Return the MachineFunction containing this basic block.
80 const MachineFunction *getParent() const;
82 typedef ilist<MachineInstr>::iterator iterator;
83 typedef ilist<MachineInstr>::const_iterator const_iterator;
84 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
85 typedef std::reverse_iterator<iterator> reverse_iterator;
87 unsigned size() const { return Insts.size(); }
88 bool empty() const { return Insts.empty(); }
90 MachineInstr& front() { return Insts.front(); }
91 MachineInstr& back() { return Insts.back(); }
93 iterator begin() { return Insts.begin(); }
94 const_iterator begin() const { return Insts.begin(); }
95 iterator end() { return Insts.end(); }
96 const_iterator end() const { return Insts.end(); }
97 reverse_iterator rbegin() { return Insts.rbegin(); }
98 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
99 reverse_iterator rend () { return Insts.rend(); }
100 const_reverse_iterator rend () const { return Insts.rend(); }
102 // Machine-CFG iterators
103 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
104 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
105 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
106 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
108 pred_iterator pred_begin() { return Predecessors.begin (); }
109 const_pred_iterator pred_begin() const { return Predecessors.begin (); }
110 pred_iterator pred_end() { return Predecessors.end (); }
111 const_pred_iterator pred_end() const { return Predecessors.end (); }
112 unsigned pred_size() const { return Predecessors.size (); }
113 succ_iterator succ_begin() { return Successors.begin (); }
114 const_succ_iterator succ_begin() const { return Successors.begin (); }
115 succ_iterator succ_end() { return Successors.end (); }
116 const_succ_iterator succ_end() const { return Successors.end (); }
117 unsigned succ_size() const { return Successors.size (); }
119 // Machine-CFG mutators
121 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
122 /// The Predecessors list of succ is automatically updated.
124 void addSuccessor (MachineBasicBlock *succ) {
125 Successors.push_back (succ);
126 succ->addPredecessor (this);
129 /// removeSuccessor - Remove succ from the successors list of this
130 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
132 void removeSuccessor (MachineBasicBlock *succ) {
133 succ->removePredecessor (this);
134 std::vector<MachineBasicBlock *>::iterator goner =
135 std::find (Successors.begin(), Successors.end (), succ);
136 Successors.erase (goner);
139 /// getFirstTerminator - returns an iterator to the first terminator
140 /// instruction of this basic block. If a terminator does not exist,
142 iterator getFirstTerminator();
144 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
145 template<typename IT>
146 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
147 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
149 // erase - Remove the specified element or range from the instruction list.
150 // These functions delete any instructions removed.
152 iterator erase(iterator I) { return Insts.erase(I); }
153 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
154 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
155 void clear() { Insts.clear(); }
157 // Debugging methods.
159 void print(std::ostream &OS) const;
161 private: // Methods used to maintain doubly linked list of blocks...
162 friend class ilist_traits<MachineBasicBlock>;
164 MachineBasicBlock *getPrev() const { return Prev; }
165 MachineBasicBlock *getNext() const { return Next; }
166 void setPrev(MachineBasicBlock *P) { Prev = P; }
167 void setNext(MachineBasicBlock *N) { Next = N; }
169 // Machine-CFG mutators
171 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
172 /// Don't do this unless you know what you're doing, because it doesn't
173 /// update pred's successors list. Use pred->addSuccessor instead.
175 void addPredecessor (MachineBasicBlock *pred) {
176 Predecessors.push_back (pred);
179 /// removePredecessor - Remove pred as a predecessor of this
180 /// MachineBasicBlock. Don't do this unless you know what you're
181 /// doing, because it doesn't update pred's successors list. Use
182 /// pred->removeSuccessor instead.
184 void removePredecessor (MachineBasicBlock *pred) {
185 std::vector<MachineBasicBlock *>::iterator goner =
186 std::find (Predecessors.begin(), Predecessors.end (), pred);
187 Predecessors.erase (goner);
192 //===--------------------------------------------------------------------===//
193 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
194 //===--------------------------------------------------------------------===//
196 // Provide specializations of GraphTraits to be able to treat a
197 // MachineFunction as a graph of MachineBasicBlocks...
200 template <> struct GraphTraits<MachineBasicBlock *> {
201 typedef MachineBasicBlock NodeType;
202 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
204 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
205 static inline ChildIteratorType child_begin(NodeType *N) {
206 return N->succ_begin();
208 static inline ChildIteratorType child_end(NodeType *N) {
209 return N->succ_end();
213 template <> struct GraphTraits<const MachineBasicBlock *> {
214 typedef const MachineBasicBlock NodeType;
215 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
217 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
218 static inline ChildIteratorType child_begin(NodeType *N) {
219 return N->succ_begin();
221 static inline ChildIteratorType child_end(NodeType *N) {
222 return N->succ_end();
226 // Provide specializations of GraphTraits to be able to treat a
227 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
228 // in inverse order. Inverse order for a function is considered
229 // to be when traversing the predecessor edges of a MBB
230 // instead of the successor edges.
232 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
233 typedef MachineBasicBlock NodeType;
234 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
235 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
238 static inline ChildIteratorType child_begin(NodeType *N) {
239 return N->pred_begin();
241 static inline ChildIteratorType child_end(NodeType *N) {
242 return N->pred_end();
246 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
247 typedef const MachineBasicBlock NodeType;
248 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
249 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
252 static inline ChildIteratorType child_begin(NodeType *N) {
253 return N->pred_begin();
255 static inline ChildIteratorType child_end(NodeType *N) {
256 return N->pred_end();
260 } // End llvm namespace