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 "llvm/ADT/GraphTraits.h"
19 #include "llvm/ADT/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;
67 MachineFunction *Parent;
70 MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb),
71 Number(-1), Parent(0) {
77 /// getBasicBlock - Return the LLVM basic block that this instance
78 /// corresponded to originally.
80 const BasicBlock *getBasicBlock() const { return BB; }
82 /// getParent - Return the MachineFunction containing this basic block.
84 const MachineFunction *getParent() const { return Parent; }
85 MachineFunction *getParent() { return Parent; }
87 typedef ilist<MachineInstr>::iterator iterator;
88 typedef ilist<MachineInstr>::const_iterator const_iterator;
89 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
90 typedef std::reverse_iterator<iterator> reverse_iterator;
92 unsigned size() const { return Insts.size(); }
93 bool empty() const { return Insts.empty(); }
95 MachineInstr& front() { return Insts.front(); }
96 MachineInstr& back() { return Insts.back(); }
98 iterator begin() { return Insts.begin(); }
99 const_iterator begin() const { return Insts.begin(); }
100 iterator end() { return Insts.end(); }
101 const_iterator end() const { return Insts.end(); }
102 reverse_iterator rbegin() { return Insts.rbegin(); }
103 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
104 reverse_iterator rend () { return Insts.rend(); }
105 const_reverse_iterator rend () const { return Insts.rend(); }
107 // Machine-CFG iterators
108 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
109 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
110 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
111 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
113 pred_iterator pred_begin() { return Predecessors.begin (); }
114 const_pred_iterator pred_begin() const { return Predecessors.begin (); }
115 pred_iterator pred_end() { return Predecessors.end (); }
116 const_pred_iterator pred_end() const { return Predecessors.end (); }
117 unsigned pred_size() const { return Predecessors.size (); }
118 bool pred_empty() const { return Predecessors.empty(); }
119 succ_iterator succ_begin() { return Successors.begin (); }
120 const_succ_iterator succ_begin() const { return Successors.begin (); }
121 succ_iterator succ_end() { return Successors.end (); }
122 const_succ_iterator succ_end() const { return Successors.end (); }
123 unsigned succ_size() const { return Successors.size (); }
124 bool succ_empty() const { return Successors.empty(); }
126 // Machine-CFG mutators
128 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
129 /// The Predecessors list of succ is automatically updated.
131 void addSuccessor(MachineBasicBlock *succ);
133 /// removeSuccessor - Remove successor from the successors list of this
134 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
136 void removeSuccessor(MachineBasicBlock *succ);
138 /// removeSuccessor - Remove specified successor from the successors list of
139 /// this MachineBasicBlock. The Predecessors list of succ is automatically
142 void removeSuccessor(succ_iterator I);
144 /// getFirstTerminator - returns an iterator to the first terminator
145 /// instruction of this basic block. If a terminator does not exist,
147 iterator getFirstTerminator();
149 void pop_front() { Insts.pop_front(); }
150 void pop_back() { Insts.pop_back(); }
151 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
152 template<typename IT>
153 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
154 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
156 // erase - Remove the specified element or range from the instruction list.
157 // These functions delete any instructions removed.
159 iterator erase(iterator I) { return Insts.erase(I); }
160 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
161 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
162 void clear() { Insts.clear(); }
164 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
165 /// To), and insert them into this MBB right before 'where'.
166 void splice(iterator where, MachineBasicBlock *Other, iterator From,
168 Insts.splice(where, Other->Insts, From, To);
171 // Debugging methods.
173 void print(std::ostream &OS) const;
175 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
176 /// level, unless they're not in a MachineFunction yet, in which case this
179 int getNumber() const { return Number; }
181 private: // Methods used to maintain doubly linked list of blocks...
182 friend class ilist_traits<MachineBasicBlock>;
184 MachineBasicBlock *getPrev() const { return Prev; }
185 MachineBasicBlock *getNext() const { return Next; }
186 void setPrev(MachineBasicBlock *P) { Prev = P; }
187 void setNext(MachineBasicBlock *N) { Next = N; }
189 // Machine-CFG mutators
191 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
192 /// Don't do this unless you know what you're doing, because it doesn't
193 /// update pred's successors list. Use pred->addSuccessor instead.
195 void addPredecessor(MachineBasicBlock *pred);
197 /// removePredecessor - Remove pred as a predecessor of this
198 /// MachineBasicBlock. Don't do this unless you know what you're
199 /// doing, because it doesn't update pred's successors list. Use
200 /// pred->removeSuccessor instead.
202 void removePredecessor(MachineBasicBlock *pred);
207 //===--------------------------------------------------------------------===//
208 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
209 //===--------------------------------------------------------------------===//
211 // Provide specializations of GraphTraits to be able to treat a
212 // MachineFunction as a graph of MachineBasicBlocks...
215 template <> struct GraphTraits<MachineBasicBlock *> {
216 typedef MachineBasicBlock NodeType;
217 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
219 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
220 static inline ChildIteratorType child_begin(NodeType *N) {
221 return N->succ_begin();
223 static inline ChildIteratorType child_end(NodeType *N) {
224 return N->succ_end();
228 template <> struct GraphTraits<const MachineBasicBlock *> {
229 typedef const MachineBasicBlock NodeType;
230 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
232 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
233 static inline ChildIteratorType child_begin(NodeType *N) {
234 return N->succ_begin();
236 static inline ChildIteratorType child_end(NodeType *N) {
237 return N->succ_end();
241 // Provide specializations of GraphTraits to be able to treat a
242 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
243 // in inverse order. Inverse order for a function is considered
244 // to be when traversing the predecessor edges of a MBB
245 // instead of the successor edges.
247 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
248 typedef MachineBasicBlock NodeType;
249 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
250 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
253 static inline ChildIteratorType child_begin(NodeType *N) {
254 return N->pred_begin();
256 static inline ChildIteratorType child_end(NodeType *N) {
257 return N->pred_end();
261 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
262 typedef const MachineBasicBlock NodeType;
263 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
264 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
267 static inline ChildIteratorType child_begin(NodeType *N) {
268 return N->pred_begin();
270 static inline ChildIteratorType child_end(NodeType *N) {
271 return N->pred_end();
275 } // End llvm namespace