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"
20 #include "llvm/Support/Streams.h"
23 class MachineFunction;
27 struct ilist_traits<MachineInstr> {
29 // this is only set by the MachineBasicBlock owning the ilist
30 friend class MachineBasicBlock;
31 MachineBasicBlock* parent;
34 ilist_traits<MachineInstr>() : parent(0) { }
36 static MachineInstr* getPrev(MachineInstr* N) { return N->prev; }
37 static MachineInstr* getNext(MachineInstr* N) { return N->next; }
39 static const MachineInstr*
40 getPrev(const MachineInstr* N) { return N->prev; }
42 static const MachineInstr*
43 getNext(const MachineInstr* N) { return N->next; }
45 static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; }
46 static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; }
48 static MachineInstr* createSentinel();
49 static void destroySentinel(MachineInstr *MI) { delete MI; }
50 void addNodeToList(MachineInstr* N);
51 void removeNodeFromList(MachineInstr* N);
52 void transferNodesFromList(
53 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
54 ilist_iterator<MachineInstr> first,
55 ilist_iterator<MachineInstr> last);
60 class MachineBasicBlock {
62 typedef ilist<MachineInstr> Instructions;
64 MachineBasicBlock *Prev, *Next;
67 MachineFunction *Parent;
69 /// Predecessors/Successors - Keep track of the predecessor / successor
71 std::vector<MachineBasicBlock *> Predecessors;
72 std::vector<MachineBasicBlock *> Successors;
74 /// LiveIns - Keep track of the physical registers that are livein of
76 std::vector<unsigned> LiveIns;
79 MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb),
80 Number(-1), Parent(0) {
86 /// getBasicBlock - Return the LLVM basic block that this instance
87 /// corresponded to originally.
89 const BasicBlock *getBasicBlock() const { return BB; }
91 /// getParent - Return the MachineFunction containing this basic block.
93 const MachineFunction *getParent() const { return Parent; }
94 MachineFunction *getParent() { return Parent; }
96 typedef ilist<MachineInstr>::iterator iterator;
97 typedef ilist<MachineInstr>::const_iterator const_iterator;
98 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
99 typedef std::reverse_iterator<iterator> reverse_iterator;
101 unsigned size() const { return Insts.size(); }
102 bool empty() const { return Insts.empty(); }
104 MachineInstr& front() { return Insts.front(); }
105 MachineInstr& back() { return Insts.back(); }
107 iterator begin() { return Insts.begin(); }
108 const_iterator begin() const { return Insts.begin(); }
109 iterator end() { return Insts.end(); }
110 const_iterator end() const { return Insts.end(); }
111 reverse_iterator rbegin() { return Insts.rbegin(); }
112 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
113 reverse_iterator rend () { return Insts.rend(); }
114 const_reverse_iterator rend () const { return Insts.rend(); }
116 // Machine-CFG iterators
117 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
118 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
119 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
120 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
122 pred_iterator pred_begin() { return Predecessors.begin(); }
123 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
124 pred_iterator pred_end() { return Predecessors.end(); }
125 const_pred_iterator pred_end() const { return Predecessors.end(); }
126 unsigned pred_size() const { return Predecessors.size(); }
127 bool pred_empty() const { return Predecessors.empty(); }
128 succ_iterator succ_begin() { return Successors.begin(); }
129 const_succ_iterator succ_begin() const { return Successors.begin(); }
130 succ_iterator succ_end() { return Successors.end(); }
131 const_succ_iterator succ_end() const { return Successors.end(); }
132 unsigned succ_size() const { return Successors.size(); }
133 bool succ_empty() const { return Successors.empty(); }
135 // LiveIn management methods.
137 /// addLiveIn - Add the specified register as a live in. Note that it
138 /// is an error to add the same register to the same set more than once.
139 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
141 // Iteration support for live in sets. These sets are kept in sorted
142 // order by their register number.
143 typedef std::vector<unsigned>::const_iterator livein_iterator;
144 livein_iterator livein_begin() const { return LiveIns.begin(); }
145 livein_iterator livein_end() const { return LiveIns.end(); }
146 bool livein_empty() const { return LiveIns.empty(); }
148 // Code Layout methods.
150 /// moveBefore/moveAfter - move 'this' block before or after the specified
151 /// block. This only moves the block, it does not modify the CFG or adjust
152 /// potential fall-throughs at the end of the block.
153 void moveBefore(MachineBasicBlock *NewAfter);
154 void moveAfter(MachineBasicBlock *NewBefore);
156 // Machine-CFG mutators
158 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
159 /// The Predecessors list of succ is automatically updated.
161 void addSuccessor(MachineBasicBlock *succ);
163 /// removeSuccessor - Remove successor from the successors list of this
164 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
166 void removeSuccessor(MachineBasicBlock *succ);
168 /// removeSuccessor - Remove specified successor from the successors list of
169 /// this MachineBasicBlock. The Predecessors list of succ is automatically
172 void removeSuccessor(succ_iterator I);
174 /// isSuccessor - Return true if the specified MBB is a successor of this
176 bool isSuccessor(MachineBasicBlock *MBB) const {
177 for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
183 /// getFirstTerminator - returns an iterator to the first terminator
184 /// instruction of this basic block. If a terminator does not exist,
186 iterator getFirstTerminator();
188 void pop_front() { Insts.pop_front(); }
189 void pop_back() { Insts.pop_back(); }
190 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
191 template<typename IT>
192 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
193 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
195 // erase - Remove the specified element or range from the instruction list.
196 // These functions delete any instructions removed.
198 iterator erase(iterator I) { return Insts.erase(I); }
199 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
200 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
201 void clear() { Insts.clear(); }
203 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
204 /// To), and insert them into this MBB right before 'where'.
205 void splice(iterator where, MachineBasicBlock *Other, iterator From,
207 Insts.splice(where, Other->Insts, From, To);
210 // Debugging methods.
212 void print(std::ostream &OS) const;
213 void print(std::ostream *OS) const { if (OS) print(*OS); }
215 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
216 /// level, unless they're not in a MachineFunction yet, in which case this
219 int getNumber() const { return Number; }
220 void setNumber(int N) { Number = N; }
222 private: // Methods used to maintain doubly linked list of blocks...
223 friend struct ilist_traits<MachineBasicBlock>;
225 MachineBasicBlock *getPrev() const { return Prev; }
226 MachineBasicBlock *getNext() const { return Next; }
227 void setPrev(MachineBasicBlock *P) { Prev = P; }
228 void setNext(MachineBasicBlock *N) { Next = N; }
230 // Machine-CFG mutators
232 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
233 /// Don't do this unless you know what you're doing, because it doesn't
234 /// update pred's successors list. Use pred->addSuccessor instead.
236 void addPredecessor(MachineBasicBlock *pred);
238 /// removePredecessor - Remove pred as a predecessor of this
239 /// MachineBasicBlock. Don't do this unless you know what you're
240 /// doing, because it doesn't update pred's successors list. Use
241 /// pred->removeSuccessor instead.
243 void removePredecessor(MachineBasicBlock *pred);
246 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
248 //===--------------------------------------------------------------------===//
249 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
250 //===--------------------------------------------------------------------===//
252 // Provide specializations of GraphTraits to be able to treat a
253 // MachineFunction as a graph of MachineBasicBlocks...
256 template <> struct GraphTraits<MachineBasicBlock *> {
257 typedef MachineBasicBlock NodeType;
258 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
260 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
261 static inline ChildIteratorType child_begin(NodeType *N) {
262 return N->succ_begin();
264 static inline ChildIteratorType child_end(NodeType *N) {
265 return N->succ_end();
269 template <> struct GraphTraits<const MachineBasicBlock *> {
270 typedef const MachineBasicBlock NodeType;
271 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
273 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
274 static inline ChildIteratorType child_begin(NodeType *N) {
275 return N->succ_begin();
277 static inline ChildIteratorType child_end(NodeType *N) {
278 return N->succ_end();
282 // Provide specializations of GraphTraits to be able to treat a
283 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
284 // in inverse order. Inverse order for a function is considered
285 // to be when traversing the predecessor edges of a MBB
286 // instead of the successor edges.
288 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
289 typedef MachineBasicBlock NodeType;
290 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
291 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
294 static inline ChildIteratorType child_begin(NodeType *N) {
295 return N->pred_begin();
297 static inline ChildIteratorType child_end(NodeType *N) {
298 return N->pred_end();
302 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
303 typedef const MachineBasicBlock NodeType;
304 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
305 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
308 static inline ChildIteratorType child_begin(NodeType *N) {
309 return N->pred_begin();
311 static inline ChildIteratorType child_end(NodeType *N) {
312 return N->pred_end();
316 } // End llvm namespace