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/Support/Streams.h"
22 class MachineFunction;
25 struct alist_traits<MachineInstr, MachineInstr> {
27 // this is only set by the MachineBasicBlock owning the LiveList
28 friend class MachineBasicBlock;
29 MachineBasicBlock* Parent;
31 typedef alist_iterator<MachineInstr> iterator;
34 alist_traits<MachineInstr, MachineInstr>() : Parent(0) { }
36 void addNodeToList(MachineInstr* N);
37 void removeNodeFromList(MachineInstr* N);
38 void transferNodesFromList(alist_traits &, iterator, iterator);
39 void deleteNode(MachineInstr *N);
44 class MachineBasicBlock {
45 typedef alist<MachineInstr> Instructions;
49 MachineFunction *xParent;
51 /// Predecessors/Successors - Keep track of the predecessor / successor
53 std::vector<MachineBasicBlock *> Predecessors;
54 std::vector<MachineBasicBlock *> Successors;
56 /// LiveIns - Keep track of the physical registers that are livein of
58 std::vector<unsigned> LiveIns;
60 /// Alignment - Alignment of the basic block. Zero if the basic block does
61 /// not need to be aligned.
64 /// IsLandingPad - Indicate that this basic block is entered via an
65 /// exception handler.
68 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
72 // MachineBasicBlocks are allocated and owned by MachineFunction.
73 friend class MachineFunction;
76 /// getBasicBlock - Return the LLVM basic block that this instance
77 /// corresponded to originally.
79 const BasicBlock *getBasicBlock() const { return BB; }
81 /// getParent - Return the MachineFunction containing this basic block.
83 const MachineFunction *getParent() const { return xParent; }
84 MachineFunction *getParent() { return xParent; }
86 typedef Instructions::iterator iterator;
87 typedef Instructions::const_iterator const_iterator;
88 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
89 typedef std::reverse_iterator<iterator> reverse_iterator;
91 unsigned size() const { return (unsigned)Insts.size(); }
92 bool empty() const { return Insts.empty(); }
94 MachineInstr& front() { return Insts.front(); }
95 MachineInstr& back() { return Insts.back(); }
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(); }
106 // 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 typedef std::vector<MachineBasicBlock *>::reverse_iterator
112 pred_reverse_iterator;
113 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
114 const_pred_reverse_iterator;
115 typedef std::vector<MachineBasicBlock *>::reverse_iterator
116 succ_reverse_iterator;
117 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
118 const_succ_reverse_iterator;
120 pred_iterator pred_begin() { return Predecessors.begin(); }
121 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
122 pred_iterator pred_end() { return Predecessors.end(); }
123 const_pred_iterator pred_end() const { return Predecessors.end(); }
124 pred_reverse_iterator pred_rbegin()
125 { return Predecessors.rbegin();}
126 const_pred_reverse_iterator pred_rbegin() const
127 { return Predecessors.rbegin();}
128 pred_reverse_iterator pred_rend()
129 { return Predecessors.rend(); }
130 const_pred_reverse_iterator pred_rend() const
131 { return Predecessors.rend(); }
132 unsigned pred_size() const {
133 return (unsigned)Predecessors.size();
135 bool pred_empty() const { return Predecessors.empty(); }
136 succ_iterator succ_begin() { return Successors.begin(); }
137 const_succ_iterator succ_begin() const { return Successors.begin(); }
138 succ_iterator succ_end() { return Successors.end(); }
139 const_succ_iterator succ_end() const { return Successors.end(); }
140 succ_reverse_iterator succ_rbegin()
141 { return Successors.rbegin(); }
142 const_succ_reverse_iterator succ_rbegin() const
143 { return Successors.rbegin(); }
144 succ_reverse_iterator succ_rend()
145 { return Successors.rend(); }
146 const_succ_reverse_iterator succ_rend() const
147 { return Successors.rend(); }
148 unsigned succ_size() const {
149 return (unsigned)Successors.size();
151 bool succ_empty() const { return Successors.empty(); }
153 // LiveIn management methods.
155 /// addLiveIn - Add the specified register as a live in. Note that it
156 /// is an error to add the same register to the same set more than once.
157 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
159 /// removeLiveIn - Remove the specified register from the live in set.
161 void removeLiveIn(unsigned Reg);
163 /// isLiveIn - Return true if the specified register is in the live in set.
165 bool isLiveIn(unsigned Reg) const;
167 // Iteration support for live in sets. These sets are kept in sorted
168 // order by their register number.
169 typedef std::vector<unsigned>::iterator livein_iterator;
170 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
171 livein_iterator livein_begin() { return LiveIns.begin(); }
172 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
173 livein_iterator livein_end() { return LiveIns.end(); }
174 const_livein_iterator livein_end() const { return LiveIns.end(); }
175 bool livein_empty() const { return LiveIns.empty(); }
177 /// getAlignment - Return alignment of the basic block.
179 unsigned getAlignment() const { return Alignment; }
181 /// setAlignment - Set alignment of the basic block.
183 void setAlignment(unsigned Align) { Alignment = Align; }
185 /// isLandingPad - Returns true if the block is a landing pad. That is
186 /// this basic block is entered via an exception handler.
187 bool isLandingPad() const { return IsLandingPad; }
189 /// setIsLandingPad - Indicates the block is a landing pad. That is
190 /// this basic block is entered via an exception handler.
191 void setIsLandingPad() { IsLandingPad = true; }
193 // Code Layout methods.
195 /// moveBefore/moveAfter - move 'this' block before or after the specified
196 /// block. This only moves the block, it does not modify the CFG or adjust
197 /// potential fall-throughs at the end of the block.
198 void moveBefore(MachineBasicBlock *NewAfter);
199 void moveAfter(MachineBasicBlock *NewBefore);
201 // Machine-CFG mutators
203 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
204 /// The Predecessors list of succ is automatically updated.
206 void addSuccessor(MachineBasicBlock *succ);
208 /// removeSuccessor - Remove successor from the successors list of this
209 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
211 void removeSuccessor(MachineBasicBlock *succ);
213 /// removeSuccessor - Remove specified successor from the successors list of
214 /// this MachineBasicBlock. The Predecessors list of succ is automatically
215 /// updated. Return the iterator to the element after the one removed.
217 succ_iterator removeSuccessor(succ_iterator I);
219 /// transferSuccessors - Transfers all the successors from MBB to this
220 /// machine basic block (i.e., copies all the successors fromMBB and
221 /// remove all the successors fromBB).
222 void transferSuccessors(MachineBasicBlock *fromMBB);
224 /// isSuccessor - Return true if the specified MBB is a successor of this
226 bool isSuccessor(MachineBasicBlock *MBB) const;
228 /// getFirstTerminator - returns an iterator to the first terminator
229 /// instruction of this basic block. If a terminator does not exist,
231 iterator getFirstTerminator();
233 void pop_front() { Insts.pop_front(); }
234 void pop_back() { Insts.pop_back(); }
235 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
236 template<typename IT>
237 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
238 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
240 // erase - Remove the specified element or range from the instruction list.
241 // These functions delete any instructions removed.
243 iterator erase(iterator I) { return Insts.erase(I); }
244 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
245 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
246 void clear() { Insts.clear(); }
248 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
249 /// To), and insert them into this MBB right before 'where'.
250 void splice(iterator where, MachineBasicBlock *Other, iterator From,
252 Insts.splice(where, Other->Insts, From, To);
255 /// removeFromParent - This method unlinks 'this' from the containing
256 /// function, and returns it, but does not delete it.
257 MachineBasicBlock *removeFromParent();
259 /// eraseFromParent - This method unlinks 'this' from the containing
260 /// function and deletes it.
261 void eraseFromParent();
263 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
264 /// 'Old', change the code and CFG so that it branches to 'New' instead.
265 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
267 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
268 /// the CFG to be inserted. If we have proven that MBB can only branch to
269 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
270 /// DestB can be null. Besides DestA and DestB, retain other edges leading
271 /// to LandingPads (currently there can be only one; we don't check or require
272 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
273 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
274 MachineBasicBlock *DestB,
277 // Debugging methods.
279 void print(std::ostream &OS) const;
280 void print(std::ostream *OS) const { if (OS) print(*OS); }
282 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
283 /// level, unless they're not in a MachineFunction yet, in which case this
286 int getNumber() const { return Number; }
287 void setNumber(int N) { Number = N; }
289 private: // Methods used to maintain doubly linked list of blocks...
290 friend struct alist_traits<MachineBasicBlock>;
292 // Machine-CFG mutators
294 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
295 /// Don't do this unless you know what you're doing, because it doesn't
296 /// update pred's successors list. Use pred->addSuccessor instead.
298 void addPredecessor(MachineBasicBlock *pred);
300 /// removePredecessor - Remove pred as a predecessor of this
301 /// MachineBasicBlock. Don't do this unless you know what you're
302 /// doing, because it doesn't update pred's successors list. Use
303 /// pred->removeSuccessor instead.
305 void removePredecessor(MachineBasicBlock *pred);
308 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
310 //===--------------------------------------------------------------------===//
311 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
312 //===--------------------------------------------------------------------===//
314 // Provide specializations of GraphTraits to be able to treat a
315 // MachineFunction as a graph of MachineBasicBlocks...
318 template <> struct GraphTraits<MachineBasicBlock *> {
319 typedef MachineBasicBlock NodeType;
320 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
322 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
323 static inline ChildIteratorType child_begin(NodeType *N) {
324 return N->succ_begin();
326 static inline ChildIteratorType child_end(NodeType *N) {
327 return N->succ_end();
331 template <> struct GraphTraits<const MachineBasicBlock *> {
332 typedef const MachineBasicBlock NodeType;
333 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
335 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
336 static inline ChildIteratorType child_begin(NodeType *N) {
337 return N->succ_begin();
339 static inline ChildIteratorType child_end(NodeType *N) {
340 return N->succ_end();
344 // Provide specializations of GraphTraits to be able to treat a
345 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
346 // in inverse order. Inverse order for a function is considered
347 // to be when traversing the predecessor edges of a MBB
348 // instead of the successor edges.
350 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
351 typedef MachineBasicBlock NodeType;
352 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
353 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
356 static inline ChildIteratorType child_begin(NodeType *N) {
357 return N->pred_begin();
359 static inline ChildIteratorType child_end(NodeType *N) {
360 return N->pred_end();
364 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
365 typedef const MachineBasicBlock NodeType;
366 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
367 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
370 static inline ChildIteratorType child_begin(NodeType *N) {
371 return N->pred_begin();
373 static inline ChildIteratorType child_end(NodeType *N) {
374 return N->pred_end();
378 } // End llvm namespace