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"
23 class MachineFunction;
26 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
28 mutable ilist_node<MachineInstr> Sentinel;
30 // this is only set by the MachineBasicBlock owning the LiveList
31 friend class MachineBasicBlock;
32 MachineBasicBlock* Parent;
35 MachineInstr *createSentinel() const {
36 return static_cast<MachineInstr*>(&Sentinel);
38 void destroySentinel(MachineInstr *) const {}
40 MachineInstr *provideInitialHead() const { return createSentinel(); }
41 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
42 static void noteHead(MachineInstr*, MachineInstr*) {}
44 void addNodeToList(MachineInstr* N);
45 void removeNodeFromList(MachineInstr* N);
46 void transferNodesFromList(ilist_traits &SrcTraits,
47 ilist_iterator<MachineInstr> first,
48 ilist_iterator<MachineInstr> last);
49 void deleteNode(MachineInstr *N);
51 void createNode(const MachineInstr &);
54 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
55 typedef ilist<MachineInstr> Instructions;
59 MachineFunction *xParent;
61 /// Predecessors/Successors - Keep track of the predecessor / successor
63 std::vector<MachineBasicBlock *> Predecessors;
64 std::vector<MachineBasicBlock *> Successors;
66 /// LiveIns - Keep track of the physical registers that are livein of
68 std::vector<unsigned> LiveIns;
70 /// Alignment - Alignment of the basic block. Zero if the basic block does
71 /// not need to be aligned.
74 /// IsLandingPad - Indicate that this basic block is entered via an
75 /// exception handler.
78 // Intrusive list support
79 MachineBasicBlock() {}
81 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
85 // MachineBasicBlocks are allocated and owned by MachineFunction.
86 friend class MachineFunction;
89 /// getBasicBlock - Return the LLVM basic block that this instance
90 /// corresponded to originally.
92 const BasicBlock *getBasicBlock() const { return BB; }
94 /// getParent - Return the MachineFunction containing this basic block.
96 const MachineFunction *getParent() const { return xParent; }
97 MachineFunction *getParent() { return xParent; }
99 typedef Instructions::iterator iterator;
100 typedef Instructions::const_iterator const_iterator;
101 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
102 typedef std::reverse_iterator<iterator> reverse_iterator;
104 unsigned size() const { return (unsigned)Insts.size(); }
105 bool empty() const { return Insts.empty(); }
107 MachineInstr& front() { return Insts.front(); }
108 MachineInstr& back() { return Insts.back(); }
109 const MachineInstr& front() const { return Insts.front(); }
110 const MachineInstr& back() const { return Insts.back(); }
112 iterator begin() { return Insts.begin(); }
113 const_iterator begin() const { return Insts.begin(); }
114 iterator end() { return Insts.end(); }
115 const_iterator end() const { return Insts.end(); }
116 reverse_iterator rbegin() { return Insts.rbegin(); }
117 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
118 reverse_iterator rend () { return Insts.rend(); }
119 const_reverse_iterator rend () const { return Insts.rend(); }
121 // Machine-CFG iterators
122 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
123 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
124 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
125 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
126 typedef std::vector<MachineBasicBlock *>::reverse_iterator
127 pred_reverse_iterator;
128 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
129 const_pred_reverse_iterator;
130 typedef std::vector<MachineBasicBlock *>::reverse_iterator
131 succ_reverse_iterator;
132 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
133 const_succ_reverse_iterator;
135 pred_iterator pred_begin() { return Predecessors.begin(); }
136 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
137 pred_iterator pred_end() { return Predecessors.end(); }
138 const_pred_iterator pred_end() const { return Predecessors.end(); }
139 pred_reverse_iterator pred_rbegin()
140 { return Predecessors.rbegin();}
141 const_pred_reverse_iterator pred_rbegin() const
142 { return Predecessors.rbegin();}
143 pred_reverse_iterator pred_rend()
144 { return Predecessors.rend(); }
145 const_pred_reverse_iterator pred_rend() const
146 { return Predecessors.rend(); }
147 unsigned pred_size() const {
148 return (unsigned)Predecessors.size();
150 bool pred_empty() const { return Predecessors.empty(); }
151 succ_iterator succ_begin() { return Successors.begin(); }
152 const_succ_iterator succ_begin() const { return Successors.begin(); }
153 succ_iterator succ_end() { return Successors.end(); }
154 const_succ_iterator succ_end() const { return Successors.end(); }
155 succ_reverse_iterator succ_rbegin()
156 { return Successors.rbegin(); }
157 const_succ_reverse_iterator succ_rbegin() const
158 { return Successors.rbegin(); }
159 succ_reverse_iterator succ_rend()
160 { return Successors.rend(); }
161 const_succ_reverse_iterator succ_rend() const
162 { return Successors.rend(); }
163 unsigned succ_size() const {
164 return (unsigned)Successors.size();
166 bool succ_empty() const { return Successors.empty(); }
168 // LiveIn management methods.
170 /// addLiveIn - Add the specified register as a live in. Note that it
171 /// is an error to add the same register to the same set more than once.
172 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
174 /// removeLiveIn - Remove the specified register from the live in set.
176 void removeLiveIn(unsigned Reg);
178 /// isLiveIn - Return true if the specified register is in the live in set.
180 bool isLiveIn(unsigned Reg) const;
182 // Iteration support for live in sets. These sets are kept in sorted
183 // order by their register number.
184 typedef std::vector<unsigned>::iterator livein_iterator;
185 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
186 livein_iterator livein_begin() { return LiveIns.begin(); }
187 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
188 livein_iterator livein_end() { return LiveIns.end(); }
189 const_livein_iterator livein_end() const { return LiveIns.end(); }
190 bool livein_empty() const { return LiveIns.empty(); }
192 /// getAlignment - Return alignment of the basic block.
194 unsigned getAlignment() const { return Alignment; }
196 /// setAlignment - Set alignment of the basic block.
198 void setAlignment(unsigned Align) { Alignment = Align; }
200 /// isLandingPad - Returns true if the block is a landing pad. That is
201 /// this basic block is entered via an exception handler.
202 bool isLandingPad() const { return IsLandingPad; }
204 /// setIsLandingPad - Indicates the block is a landing pad. That is
205 /// this basic block is entered via an exception handler.
206 void setIsLandingPad() { IsLandingPad = true; }
208 // Code Layout methods.
210 /// moveBefore/moveAfter - move 'this' block before or after the specified
211 /// block. This only moves the block, it does not modify the CFG or adjust
212 /// potential fall-throughs at the end of the block.
213 void moveBefore(MachineBasicBlock *NewAfter);
214 void moveAfter(MachineBasicBlock *NewBefore);
216 // Machine-CFG mutators
218 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
219 /// The Predecessors list of succ is automatically updated.
221 void addSuccessor(MachineBasicBlock *succ);
223 /// removeSuccessor - Remove successor from the successors list of this
224 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
226 void removeSuccessor(MachineBasicBlock *succ);
228 /// removeSuccessor - Remove specified successor from the successors list of
229 /// this MachineBasicBlock. The Predecessors list of succ is automatically
230 /// updated. Return the iterator to the element after the one removed.
232 succ_iterator removeSuccessor(succ_iterator I);
234 /// transferSuccessors - Transfers all the successors from MBB to this
235 /// machine basic block (i.e., copies all the successors fromMBB and
236 /// remove all the successors fromBB).
237 void transferSuccessors(MachineBasicBlock *fromMBB);
239 /// isSuccessor - Return true if the specified MBB is a successor of this
241 bool isSuccessor(const MachineBasicBlock *MBB) const;
243 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
244 /// immediately after this block, such that if this block exits by
245 /// falling through, control will transfer to the specified MBB. Note
246 /// that MBB need not be a successor at all, for example if this block
247 /// ends with an unconditional branch to some other block.
248 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
250 /// getFirstTerminator - returns an iterator to the first terminator
251 /// instruction of this basic block. If a terminator does not exist,
253 iterator getFirstTerminator();
255 /// isOnlyReachableViaFallthough - Return true if this basic block has
256 /// exactly one predecessor and the control transfer mechanism between
257 /// the predecessor and this block is a fall-through.
258 bool isOnlyReachableByFallthrough() const;
260 void pop_front() { Insts.pop_front(); }
261 void pop_back() { Insts.pop_back(); }
262 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
263 template<typename IT>
264 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
265 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
267 // erase - Remove the specified element or range from the instruction list.
268 // These functions delete any instructions removed.
270 iterator erase(iterator I) { return Insts.erase(I); }
271 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
272 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
273 void clear() { Insts.clear(); }
275 /// splice - Take an instruction from MBB 'Other' at the position From,
276 /// and insert it into this MBB right before 'where'.
277 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
278 Insts.splice(where, Other->Insts, From);
281 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
282 /// To), and insert them into this MBB right before 'where'.
283 void splice(iterator where, MachineBasicBlock *Other, iterator From,
285 Insts.splice(where, Other->Insts, From, To);
288 /// removeFromParent - This method unlinks 'this' from the containing
289 /// function, and returns it, but does not delete it.
290 MachineBasicBlock *removeFromParent();
292 /// eraseFromParent - This method unlinks 'this' from the containing
293 /// function and deletes it.
294 void eraseFromParent();
296 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
297 /// 'Old', change the code and CFG so that it branches to 'New' instead.
298 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
300 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
301 /// the CFG to be inserted. If we have proven that MBB can only branch to
302 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
303 /// DestB can be null. Besides DestA and DestB, retain other edges leading
304 /// to LandingPads (currently there can be only one; we don't check or require
305 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
306 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
307 MachineBasicBlock *DestB,
310 // Debugging methods.
312 void print(std::ostream &OS) const;
313 void print(std::ostream *OS) const { if (OS) print(*OS); }
315 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
316 /// level, unless they're not in a MachineFunction yet, in which case this
319 int getNumber() const { return Number; }
320 void setNumber(int N) { Number = N; }
322 private: // Methods used to maintain doubly linked list of blocks...
323 friend struct ilist_traits<MachineBasicBlock>;
325 // Machine-CFG mutators
327 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
328 /// Don't do this unless you know what you're doing, because it doesn't
329 /// update pred's successors list. Use pred->addSuccessor instead.
331 void addPredecessor(MachineBasicBlock *pred);
333 /// removePredecessor - Remove pred as a predecessor of this
334 /// MachineBasicBlock. Don't do this unless you know what you're
335 /// doing, because it doesn't update pred's successors list. Use
336 /// pred->removeSuccessor instead.
338 void removePredecessor(MachineBasicBlock *pred);
341 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
343 //===--------------------------------------------------------------------===//
344 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
345 //===--------------------------------------------------------------------===//
347 // Provide specializations of GraphTraits to be able to treat a
348 // MachineFunction as a graph of MachineBasicBlocks...
351 template <> struct GraphTraits<MachineBasicBlock *> {
352 typedef MachineBasicBlock NodeType;
353 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
355 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
356 static inline ChildIteratorType child_begin(NodeType *N) {
357 return N->succ_begin();
359 static inline ChildIteratorType child_end(NodeType *N) {
360 return N->succ_end();
364 template <> struct GraphTraits<const MachineBasicBlock *> {
365 typedef const MachineBasicBlock NodeType;
366 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
368 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
369 static inline ChildIteratorType child_begin(NodeType *N) {
370 return N->succ_begin();
372 static inline ChildIteratorType child_end(NodeType *N) {
373 return N->succ_end();
377 // Provide specializations of GraphTraits to be able to treat a
378 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
379 // in inverse order. Inverse order for a function is considered
380 // to be when traversing the predecessor edges of a MBB
381 // instead of the successor edges.
383 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
384 typedef MachineBasicBlock NodeType;
385 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
386 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
389 static inline ChildIteratorType child_begin(NodeType *N) {
390 return N->pred_begin();
392 static inline ChildIteratorType child_end(NodeType *N) {
393 return N->pred_end();
397 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
398 typedef const MachineBasicBlock NodeType;
399 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
400 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
403 static inline ChildIteratorType child_begin(NodeType *N) {
404 return N->pred_begin();
406 static inline ChildIteratorType child_end(NodeType *N) {
407 return N->pred_end();
411 } // End llvm namespace