1 //===-- llvm/CodeGen/MachineBasicBlock.h ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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.h"
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 {
61 typedef ilist<MachineInstr> Instructions;
63 MachineBasicBlock *Prev, *Next;
66 MachineFunction *xParent;
68 void setParent(MachineFunction *P) { xParent = P; }
70 /// Predecessors/Successors - Keep track of the predecessor / successor
72 std::vector<MachineBasicBlock *> Predecessors;
73 std::vector<MachineBasicBlock *> Successors;
75 /// LiveIns - Keep track of the physical registers that are livein of
77 std::vector<unsigned> LiveIns;
79 /// Alignment - Alignment of the basic block. Zero if the basic block does
80 /// not need to be aligned.
83 /// IsLandingPad - Indicate that this basic block is entered via an
84 /// exception handler.
88 explicit MachineBasicBlock(const BasicBlock *bb = 0)
89 : Prev(0), Next(0), BB(bb), Number(-1), xParent(0),
90 Alignment(0), IsLandingPad(false) {
96 /// getBasicBlock - Return the LLVM basic block that this instance
97 /// corresponded to originally.
99 const BasicBlock *getBasicBlock() const { return BB; }
101 /// getParent - Return the MachineFunction containing this basic block.
103 const MachineFunction *getParent() const { return xParent; }
104 MachineFunction *getParent() { return xParent; }
106 typedef ilist<MachineInstr>::iterator iterator;
107 typedef ilist<MachineInstr>::const_iterator const_iterator;
108 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
109 typedef std::reverse_iterator<iterator> reverse_iterator;
111 unsigned size() const { return (unsigned)Insts.size(); }
112 bool empty() const { return Insts.empty(); }
114 MachineInstr& front() { return Insts.front(); }
115 MachineInstr& back() { return Insts.back(); }
117 iterator begin() { return Insts.begin(); }
118 const_iterator begin() const { return Insts.begin(); }
119 iterator end() { return Insts.end(); }
120 const_iterator end() const { return Insts.end(); }
121 reverse_iterator rbegin() { return Insts.rbegin(); }
122 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
123 reverse_iterator rend () { return Insts.rend(); }
124 const_reverse_iterator rend () const { return Insts.rend(); }
126 // Machine-CFG iterators
127 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
128 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
129 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
130 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
131 typedef std::vector<MachineBasicBlock *>::reverse_iterator
132 pred_reverse_iterator;
133 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
134 const_pred_reverse_iterator;
135 typedef std::vector<MachineBasicBlock *>::reverse_iterator
136 succ_reverse_iterator;
137 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
138 const_succ_reverse_iterator;
140 pred_iterator pred_begin() { return Predecessors.begin(); }
141 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
142 pred_iterator pred_end() { return Predecessors.end(); }
143 const_pred_iterator pred_end() const { return Predecessors.end(); }
144 pred_reverse_iterator pred_rbegin()
145 { return Predecessors.rbegin();}
146 const_pred_reverse_iterator pred_rbegin() const
147 { return Predecessors.rbegin();}
148 pred_reverse_iterator pred_rend()
149 { return Predecessors.rend(); }
150 const_pred_reverse_iterator pred_rend() const
151 { return Predecessors.rend(); }
152 unsigned pred_size() const {
153 return (unsigned)Predecessors.size();
155 bool pred_empty() const { return Predecessors.empty(); }
156 succ_iterator succ_begin() { return Successors.begin(); }
157 const_succ_iterator succ_begin() const { return Successors.begin(); }
158 succ_iterator succ_end() { return Successors.end(); }
159 const_succ_iterator succ_end() const { return Successors.end(); }
160 succ_reverse_iterator succ_rbegin()
161 { return Successors.rbegin(); }
162 const_succ_reverse_iterator succ_rbegin() const
163 { return Successors.rbegin(); }
164 succ_reverse_iterator succ_rend()
165 { return Successors.rend(); }
166 const_succ_reverse_iterator succ_rend() const
167 { return Successors.rend(); }
168 unsigned succ_size() const {
169 return (unsigned)Successors.size();
171 bool succ_empty() const { return Successors.empty(); }
173 // LiveIn management methods.
175 /// addLiveIn - Add the specified register as a live in. Note that it
176 /// is an error to add the same register to the same set more than once.
177 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
179 /// removeLiveIn - Remove the specified register from the live in set.
181 void removeLiveIn(unsigned Reg);
183 /// isLiveIn - Return true if the specified register is in the live in set.
185 bool isLiveIn(unsigned Reg) const;
187 // Iteration support for live in sets. These sets are kept in sorted
188 // order by their register number.
189 typedef std::vector<unsigned>::iterator livein_iterator;
190 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
191 livein_iterator livein_begin() { return LiveIns.begin(); }
192 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
193 livein_iterator livein_end() { return LiveIns.end(); }
194 const_livein_iterator livein_end() const { return LiveIns.end(); }
195 bool livein_empty() const { return LiveIns.empty(); }
197 /// getAlignment - Return alignment of the basic block.
199 unsigned getAlignment() const { return Alignment; }
201 /// setAlignment - Set alignment of the basic block.
203 void setAlignment(unsigned Align) { Alignment = Align; }
205 /// isLandingPad - Returns true if the block is a landing pad. That is
206 /// this basic block is entered via an exception handler.
207 bool isLandingPad() const { return IsLandingPad; }
209 /// setIsLandingPad - Indicates the block is a landing pad. That is
210 /// this basic block is entered via an exception handler.
211 void setIsLandingPad() { IsLandingPad = true; }
213 // Code Layout methods.
215 /// moveBefore/moveAfter - move 'this' block before or after the specified
216 /// block. This only moves the block, it does not modify the CFG or adjust
217 /// potential fall-throughs at the end of the block.
218 void moveBefore(MachineBasicBlock *NewAfter);
219 void moveAfter(MachineBasicBlock *NewBefore);
221 // Machine-CFG mutators
223 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
224 /// The Predecessors list of succ is automatically updated.
226 void addSuccessor(MachineBasicBlock *succ);
228 /// removeSuccessor - Remove successor from the successors list of this
229 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
231 void removeSuccessor(MachineBasicBlock *succ);
233 /// removeSuccessor - Remove specified successor from the successors list of
234 /// this MachineBasicBlock. The Predecessors list of succ is automatically
235 /// updated. Return the iterator to the element after the one removed.
237 succ_iterator removeSuccessor(succ_iterator I);
239 /// transferSuccessors - Transfers all the successors from MBB to this
240 /// machine basic block (i.e., copies all the successors fromMBB and
241 /// remove all the successors fromBB).
242 void transferSuccessors(MachineBasicBlock *fromMBB);
244 /// isSuccessor - Return true if the specified MBB is a successor of this
246 bool isSuccessor(MachineBasicBlock *MBB) const;
248 /// getFirstTerminator - returns an iterator to the first terminator
249 /// instruction of this basic block. If a terminator does not exist,
251 iterator getFirstTerminator();
253 void pop_front() { Insts.pop_front(); }
254 void pop_back() { Insts.pop_back(); }
255 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
256 template<typename IT>
257 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
258 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
260 // erase - Remove the specified element or range from the instruction list.
261 // These functions delete any instructions removed.
263 iterator erase(iterator I) { return Insts.erase(I); }
264 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
265 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
266 void clear() { Insts.clear(); }
268 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
269 /// To), and insert them into this MBB right before 'where'.
270 void splice(iterator where, MachineBasicBlock *Other, iterator From,
272 Insts.splice(where, Other->Insts, From, To);
275 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
276 /// 'Old', change the code and CFG so that it branches to 'New' instead.
277 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
279 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
280 /// the CFG to be inserted. If we have proven that MBB can only branch to
281 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
282 /// DestB can be null. Besides DestA and DestB, retain other edges leading
283 /// to LandingPads (currently there can be only one; we don't check or require
284 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
285 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
286 MachineBasicBlock *DestB,
289 // Debugging methods.
291 void print(std::ostream &OS) const;
292 void print(std::ostream *OS) const { if (OS) print(*OS); }
294 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
295 /// level, unless they're not in a MachineFunction yet, in which case this
298 int getNumber() const { return Number; }
299 void setNumber(int N) { Number = N; }
301 private: // Methods used to maintain doubly linked list of blocks...
302 friend struct ilist_traits<MachineBasicBlock>;
304 MachineBasicBlock *getPrev() const { return Prev; }
305 MachineBasicBlock *getNext() const { return Next; }
306 void setPrev(MachineBasicBlock *P) { Prev = P; }
307 void setNext(MachineBasicBlock *N) { Next = N; }
309 // Machine-CFG mutators
311 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
312 /// Don't do this unless you know what you're doing, because it doesn't
313 /// update pred's successors list. Use pred->addSuccessor instead.
315 void addPredecessor(MachineBasicBlock *pred);
317 /// removePredecessor - Remove pred as a predecessor of this
318 /// MachineBasicBlock. Don't do this unless you know what you're
319 /// doing, because it doesn't update pred's successors list. Use
320 /// pred->removeSuccessor instead.
322 void removePredecessor(MachineBasicBlock *pred);
325 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
327 //===--------------------------------------------------------------------===//
328 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
329 //===--------------------------------------------------------------------===//
331 // Provide specializations of GraphTraits to be able to treat a
332 // MachineFunction as a graph of MachineBasicBlocks...
335 template <> struct GraphTraits<MachineBasicBlock *> {
336 typedef MachineBasicBlock NodeType;
337 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
339 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
340 static inline ChildIteratorType child_begin(NodeType *N) {
341 return N->succ_begin();
343 static inline ChildIteratorType child_end(NodeType *N) {
344 return N->succ_end();
348 template <> struct GraphTraits<const MachineBasicBlock *> {
349 typedef const MachineBasicBlock NodeType;
350 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
352 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
353 static inline ChildIteratorType child_begin(NodeType *N) {
354 return N->succ_begin();
356 static inline ChildIteratorType child_end(NodeType *N) {
357 return N->succ_end();
361 // Provide specializations of GraphTraits to be able to treat a
362 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
363 // in inverse order. Inverse order for a function is considered
364 // to be when traversing the predecessor edges of a MBB
365 // instead of the successor edges.
367 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
368 typedef MachineBasicBlock NodeType;
369 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
370 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
373 static inline ChildIteratorType child_begin(NodeType *N) {
374 return N->pred_begin();
376 static inline ChildIteratorType child_end(NodeType *N) {
377 return N->pred_end();
381 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
382 typedef const MachineBasicBlock NodeType;
383 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
384 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
387 static inline ChildIteratorType child_begin(NodeType *N) {
388 return N->pred_begin();
390 static inline ChildIteratorType child_end(NodeType *N) {
391 return N->pred_end();
395 } // End llvm namespace