blob: 4dfda90ae80e4efdba89ce27f875691b8979d735 [file] [log] [blame]
Chris Lattner51a8d852002-10-28 01:21:55 +00001//===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
Vikram S. Advedeb96542002-07-08 22:40:34 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// 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//
Chris Lattnere8b54132002-10-27 20:49:47 +000010// Collect the sequence of machine instructions for a basic block.
11//
Chris Lattner51a8d852002-10-28 01:21:55 +000012//===----------------------------------------------------------------------===//
Vikram S. Advedeb96542002-07-08 22:40:34 +000013
Misha Brukmanfce11432002-10-28 00:28:31 +000014#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15#define LLVM_CODEGEN_MACHINEBASICBLOCK_H
Vikram S. Advedeb96542002-07-08 22:40:34 +000016
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner0aef12a2004-05-01 21:05:34 +000018#include "Support/GraphTraits.h"
Alkis Evlogimenos94dc0772004-02-12 19:12:03 +000019#include "Support/ilist"
Brian Gaekef13a3f42004-02-13 04:40:15 +000020#include <iosfwd>
Brian Gaeked0fde302003-11-11 22:41:34 +000021
22namespace llvm {
Chris Lattner5e61fa92004-02-19 16:13:54 +000023 class MachineFunction;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Alkis Evlogimenos94dc0772004-02-12 19:12:03 +000025// ilist_traits
26template <>
Chris Lattner5e61fa92004-02-19 16:13:54 +000027class ilist_traits<MachineInstr> {
Alkis Evlogimenos94dc0772004-02-12 19:12:03 +000028 // this is only set by the MachineBasicBlock owning the ilist
29 friend class MachineBasicBlock;
30 MachineBasicBlock* parent;
31
32public:
33 ilist_traits<MachineInstr>() : parent(0) { }
34
35 static MachineInstr* getPrev(MachineInstr* N) { return N->prev; }
36 static MachineInstr* getNext(MachineInstr* N) { return N->next; }
37
38 static const MachineInstr*
39 getPrev(const MachineInstr* N) { return N->prev; }
40
41 static const MachineInstr*
42 getNext(const MachineInstr* N) { return N->next; }
43
44 static void setPrev(MachineInstr* N, MachineInstr* prev) { N->prev = prev; }
45 static void setNext(MachineInstr* N, MachineInstr* next) { N->next = next; }
46
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000047 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);
Alkis Evlogimenos94dc0772004-02-12 19:12:03 +000054};
55
Chris Lattneraec11f12002-10-28 01:53:00 +000056class BasicBlock;
Vikram S. Advedeb96542002-07-08 22:40:34 +000057
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +000058class MachineBasicBlock {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000059public:
60 typedef ilist<MachineInstr> Instructions;
61 Instructions Insts;
Chris Lattner8e7ae982002-10-28 02:08:43 +000062 MachineBasicBlock *Prev, *Next;
Chris Lattner1194e952003-07-26 23:30:37 +000063 const BasicBlock *BB;
Brian Gaeke76456bc2004-04-28 02:16:33 +000064 std::vector<MachineBasicBlock *> Predecessors;
65 std::vector<MachineBasicBlock *> Successors;
Brian Gaekec07d8d82004-05-12 21:35:20 +000066 int Number;
Tanya Lattner792699c2004-05-24 06:11:51 +000067 MachineFunction *Parent;
Brian Gaeke76456bc2004-04-28 02:16:33 +000068
Vikram S. Advedeb96542002-07-08 22:40:34 +000069public:
Brian Gaekec07d8d82004-05-12 21:35:20 +000070 MachineBasicBlock(const BasicBlock *bb = 0) : Prev(0), Next(0), BB(bb),
Tanya Lattner792699c2004-05-24 06:11:51 +000071 Number(-1), Parent(0) {
Alkis Evlogimenosab8672c2004-02-12 18:49:07 +000072 Insts.parent = this;
73 }
Misha Brukmanfce11432002-10-28 00:28:31 +000074 ~MachineBasicBlock() {}
Vikram S. Advedeb96542002-07-08 22:40:34 +000075
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +000076 /// getBasicBlock - Return the LLVM basic block that this instance
77 /// corresponded to originally.
78 ///
Chris Lattner1194e952003-07-26 23:30:37 +000079 const BasicBlock *getBasicBlock() const { return BB; }
Chris Lattner5e61fa92004-02-19 16:13:54 +000080
81 /// getParent - Return the MachineFunction containing this basic block.
82 ///
Tanya Lattner792699c2004-05-24 06:11:51 +000083 const MachineFunction *getParent() const { return Parent; }
84 MachineFunction *getParent() { return Parent; }
Chris Lattner5e61fa92004-02-19 16:13:54 +000085
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000086 typedef ilist<MachineInstr>::iterator iterator;
87 typedef ilist<MachineInstr>::const_iterator const_iterator;
Vikram S. Advedeb96542002-07-08 22:40:34 +000088 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
89 typedef std::reverse_iterator<iterator> reverse_iterator;
90
91 unsigned size() const { return Insts.size(); }
92 bool empty() const { return Insts.empty(); }
93
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000094 MachineInstr& front() { return Insts.front(); }
95 MachineInstr& back() { return Insts.back(); }
Vikram S. Advedeb96542002-07-08 22:40:34 +000096
97 iterator begin() { return Insts.begin(); }
98 const_iterator begin() const { return Insts.begin(); }
99 iterator end() { return Insts.end(); }
100 const_iterator end() const { return Insts.end(); }
101 reverse_iterator rbegin() { return Insts.rbegin(); }
102 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
103 reverse_iterator rend () { return Insts.rend(); }
104 const_reverse_iterator rend () const { return Insts.rend(); }
105
Brian Gaeke76456bc2004-04-28 02:16:33 +0000106 // Machine-CFG iterators
107 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
108 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
109 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
110 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
111
112 pred_iterator pred_begin() { return Predecessors.begin (); }
113 const_pred_iterator pred_begin() const { return Predecessors.begin (); }
114 pred_iterator pred_end() { return Predecessors.end (); }
115 const_pred_iterator pred_end() const { return Predecessors.end (); }
Brian Gaeke37072412004-04-28 04:46:35 +0000116 unsigned pred_size() const { return Predecessors.size (); }
Brian Gaeke76456bc2004-04-28 02:16:33 +0000117 succ_iterator succ_begin() { return Successors.begin (); }
118 const_succ_iterator succ_begin() const { return Successors.begin (); }
119 succ_iterator succ_end() { return Successors.end (); }
120 const_succ_iterator succ_end() const { return Successors.end (); }
Brian Gaeke37072412004-04-28 04:46:35 +0000121 unsigned succ_size() const { return Successors.size (); }
Brian Gaeke76456bc2004-04-28 02:16:33 +0000122
123 // Machine-CFG mutators
124
125 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
126 /// The Predecessors list of succ is automatically updated.
127 ///
128 void addSuccessor (MachineBasicBlock *succ) {
Brian Gaeke61d3d5c2004-04-28 03:59:48 +0000129 Successors.push_back (succ);
Brian Gaeke76456bc2004-04-28 02:16:33 +0000130 succ->addPredecessor (this);
131 }
132
133 /// removeSuccessor - Remove succ from the successors list of this
134 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
135 ///
136 void removeSuccessor (MachineBasicBlock *succ) {
137 succ->removePredecessor (this);
138 std::vector<MachineBasicBlock *>::iterator goner =
139 std::find (Successors.begin(), Successors.end (), succ);
Brian Gaeke76456bc2004-04-28 02:16:33 +0000140 Successors.erase (goner);
141 }
142
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +0000143 /// getFirstTerminator - returns an iterator to the first terminator
144 /// instruction of this basic block. If a terminator does not exist,
145 /// it returns end()
146 iterator getFirstTerminator();
147
Vikram S. Advedeb96542002-07-08 22:40:34 +0000148 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
149 template<typename IT>
150 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
151 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
152
Vikram S. Adve46d6a1a2002-09-20 00:55:57 +0000153 // erase - Remove the specified element or range from the instruction list.
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000154 // These functions delete any instructions removed.
Vikram S. Advedeb96542002-07-08 22:40:34 +0000155 //
Vikram S. Adve46d6a1a2002-09-20 00:55:57 +0000156 iterator erase(iterator I) { return Insts.erase(I); }
Vikram S. Advedeb96542002-07-08 22:40:34 +0000157 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
Chris Lattner83706a52004-03-31 21:58:50 +0000158 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
Brian Gaekeda44b152004-03-31 22:43:12 +0000159 void clear() { Insts.clear(); }
Chris Lattner8e7ae982002-10-28 02:08:43 +0000160
Brian Gaekef13a3f42004-02-13 04:40:15 +0000161 // Debugging methods.
162 void dump() const;
163 void print(std::ostream &OS) const;
164
Brian Gaekeda86bdc2004-05-12 21:57:23 +0000165 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
166 /// level, unless they're not in a MachineFunction yet, in which case this
167 /// will return -1.
168 ///
Brian Gaekec07d8d82004-05-12 21:35:20 +0000169 int getNumber() const { return Number; }
170
Chris Lattner8e7ae982002-10-28 02:08:43 +0000171private: // Methods used to maintain doubly linked list of blocks...
172 friend class ilist_traits<MachineBasicBlock>;
173
174 MachineBasicBlock *getPrev() const { return Prev; }
175 MachineBasicBlock *getNext() const { return Next; }
176 void setPrev(MachineBasicBlock *P) { Prev = P; }
177 void setNext(MachineBasicBlock *N) { Next = N; }
Brian Gaeke8560af42004-04-28 04:15:06 +0000178
179 // Machine-CFG mutators
180
181 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
182 /// Don't do this unless you know what you're doing, because it doesn't
183 /// update pred's successors list. Use pred->addSuccessor instead.
184 ///
185 void addPredecessor (MachineBasicBlock *pred) {
Brian Gaeke8560af42004-04-28 04:15:06 +0000186 Predecessors.push_back (pred);
187 }
188
189 /// removePredecessor - Remove pred as a predecessor of this
190 /// MachineBasicBlock. Don't do this unless you know what you're
191 /// doing, because it doesn't update pred's successors list. Use
192 /// pred->removeSuccessor instead.
193 ///
194 void removePredecessor (MachineBasicBlock *pred) {
195 std::vector<MachineBasicBlock *>::iterator goner =
196 std::find (Predecessors.begin(), Predecessors.end (), pred);
Brian Gaeke8560af42004-04-28 04:15:06 +0000197 Predecessors.erase (goner);
198 }
Vikram S. Advedeb96542002-07-08 22:40:34 +0000199};
200
Chris Lattner0aef12a2004-05-01 21:05:34 +0000201
202//===--------------------------------------------------------------------===//
203// GraphTraits specializations for machine basic block graphs (machine-CFGs)
204//===--------------------------------------------------------------------===//
205
206// Provide specializations of GraphTraits to be able to treat a
207// MachineFunction as a graph of MachineBasicBlocks...
208//
209
210template <> struct GraphTraits<MachineBasicBlock *> {
211 typedef MachineBasicBlock NodeType;
212 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
213
214 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
215 static inline ChildIteratorType child_begin(NodeType *N) {
216 return N->succ_begin();
217 }
218 static inline ChildIteratorType child_end(NodeType *N) {
219 return N->succ_end();
220 }
221};
222
223template <> struct GraphTraits<const MachineBasicBlock *> {
224 typedef const MachineBasicBlock NodeType;
225 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
226
227 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
228 static inline ChildIteratorType child_begin(NodeType *N) {
229 return N->succ_begin();
230 }
231 static inline ChildIteratorType child_end(NodeType *N) {
232 return N->succ_end();
233 }
234};
235
236// Provide specializations of GraphTraits to be able to treat a
237// MachineFunction as a graph of MachineBasicBlocks... and to walk it
238// in inverse order. Inverse order for a function is considered
239// to be when traversing the predecessor edges of a MBB
240// instead of the successor edges.
241//
242template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
243 typedef MachineBasicBlock NodeType;
244 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
245 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
246 return G.Graph;
247 }
248 static inline ChildIteratorType child_begin(NodeType *N) {
249 return N->pred_begin();
250 }
251 static inline ChildIteratorType child_end(NodeType *N) {
252 return N->pred_end();
253 }
254};
255
256template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
257 typedef const MachineBasicBlock NodeType;
258 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
259 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
260 return G.Graph;
261 }
262 static inline ChildIteratorType child_begin(NodeType *N) {
263 return N->pred_begin();
264 }
265 static inline ChildIteratorType child_end(NodeType *N) {
266 return N->pred_end();
267 }
268};
269
Brian Gaeked0fde302003-11-11 22:41:34 +0000270} // End llvm namespace
Vikram S. Advedeb96542002-07-08 22:40:34 +0000271
272#endif