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/CodeGen/Dump.h"
24 class MachineFunction;
28 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
30 mutable ilist_node<MachineInstr> Sentinel;
32 // this is only set by the MachineBasicBlock owning the LiveList
33 friend class MachineBasicBlock;
34 MachineBasicBlock* Parent;
37 MachineInstr *createSentinel() const {
38 return static_cast<MachineInstr*>(&Sentinel);
40 void destroySentinel(MachineInstr *) const {}
42 MachineInstr *provideInitialHead() const { return createSentinel(); }
43 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
44 static void noteHead(MachineInstr*, MachineInstr*) {}
46 void addNodeToList(MachineInstr* N);
47 void removeNodeFromList(MachineInstr* N);
48 void transferNodesFromList(ilist_traits &SrcTraits,
49 ilist_iterator<MachineInstr> first,
50 ilist_iterator<MachineInstr> last);
51 void deleteNode(MachineInstr *N);
53 void createNode(const MachineInstr &);
56 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
57 typedef ilist<MachineInstr> Instructions;
61 MachineFunction *xParent;
63 /// Predecessors/Successors - Keep track of the predecessor / successor
65 std::vector<MachineBasicBlock *> Predecessors;
66 std::vector<MachineBasicBlock *> Successors;
68 /// LiveIns - Keep track of the physical registers that are livein of
70 std::vector<unsigned> LiveIns;
72 /// Alignment - Alignment of the basic block. Zero if the basic block does
73 /// not need to be aligned.
76 /// IsLandingPad - Indicate that this basic block is entered via an
77 /// exception handler.
80 // Intrusive list support
81 MachineBasicBlock() {}
83 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
87 // MachineBasicBlocks are allocated and owned by MachineFunction.
88 friend class MachineFunction;
91 /// getBasicBlock - Return the LLVM basic block that this instance
92 /// corresponded to originally.
94 const BasicBlock *getBasicBlock() const { return BB; }
96 /// getParent - Return the MachineFunction containing this basic block.
98 const MachineFunction *getParent() const { return xParent; }
99 MachineFunction *getParent() { return xParent; }
101 typedef Instructions::iterator iterator;
102 typedef Instructions::const_iterator const_iterator;
103 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
104 typedef std::reverse_iterator<iterator> reverse_iterator;
106 unsigned size() const { return (unsigned)Insts.size(); }
107 bool empty() const { return Insts.empty(); }
109 MachineInstr& front() { return Insts.front(); }
110 MachineInstr& back() { return Insts.back(); }
111 const MachineInstr& front() const { return Insts.front(); }
112 const MachineInstr& back() const { return Insts.back(); }
114 iterator begin() { return Insts.begin(); }
115 const_iterator begin() const { return Insts.begin(); }
116 iterator end() { return Insts.end(); }
117 const_iterator end() const { return Insts.end(); }
118 reverse_iterator rbegin() { return Insts.rbegin(); }
119 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
120 reverse_iterator rend () { return Insts.rend(); }
121 const_reverse_iterator rend () const { return Insts.rend(); }
123 // Machine-CFG iterators
124 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
125 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
126 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
127 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
128 typedef std::vector<MachineBasicBlock *>::reverse_iterator
129 pred_reverse_iterator;
130 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
131 const_pred_reverse_iterator;
132 typedef std::vector<MachineBasicBlock *>::reverse_iterator
133 succ_reverse_iterator;
134 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
135 const_succ_reverse_iterator;
137 pred_iterator pred_begin() { return Predecessors.begin(); }
138 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
139 pred_iterator pred_end() { return Predecessors.end(); }
140 const_pred_iterator pred_end() const { return Predecessors.end(); }
141 pred_reverse_iterator pred_rbegin()
142 { return Predecessors.rbegin();}
143 const_pred_reverse_iterator pred_rbegin() const
144 { return Predecessors.rbegin();}
145 pred_reverse_iterator pred_rend()
146 { return Predecessors.rend(); }
147 const_pred_reverse_iterator pred_rend() const
148 { return Predecessors.rend(); }
149 unsigned pred_size() const {
150 return (unsigned)Predecessors.size();
152 bool pred_empty() const { return Predecessors.empty(); }
153 succ_iterator succ_begin() { return Successors.begin(); }
154 const_succ_iterator succ_begin() const { return Successors.begin(); }
155 succ_iterator succ_end() { return Successors.end(); }
156 const_succ_iterator succ_end() const { return Successors.end(); }
157 succ_reverse_iterator succ_rbegin()
158 { return Successors.rbegin(); }
159 const_succ_reverse_iterator succ_rbegin() const
160 { return Successors.rbegin(); }
161 succ_reverse_iterator succ_rend()
162 { return Successors.rend(); }
163 const_succ_reverse_iterator succ_rend() const
164 { return Successors.rend(); }
165 unsigned succ_size() const {
166 return (unsigned)Successors.size();
168 bool succ_empty() const { return Successors.empty(); }
170 // LiveIn management methods.
172 /// addLiveIn - Add the specified register as a live in. Note that it
173 /// is an error to add the same register to the same set more than once.
174 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
176 /// removeLiveIn - Remove the specified register from the live in set.
178 void removeLiveIn(unsigned Reg);
180 /// isLiveIn - Return true if the specified register is in the live in set.
182 bool isLiveIn(unsigned Reg) const;
184 // Iteration support for live in sets. These sets are kept in sorted
185 // order by their register number.
186 typedef std::vector<unsigned>::iterator livein_iterator;
187 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
188 livein_iterator livein_begin() { return LiveIns.begin(); }
189 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
190 livein_iterator livein_end() { return LiveIns.end(); }
191 const_livein_iterator livein_end() const { return LiveIns.end(); }
192 bool livein_empty() const { return LiveIns.empty(); }
194 /// getAlignment - Return alignment of the basic block.
196 unsigned getAlignment() const { return Alignment; }
198 /// setAlignment - Set alignment of the basic block.
200 void setAlignment(unsigned Align) { Alignment = Align; }
202 /// isLandingPad - Returns true if the block is a landing pad. That is
203 /// this basic block is entered via an exception handler.
204 bool isLandingPad() const { return IsLandingPad; }
206 /// setIsLandingPad - Indicates the block is a landing pad. That is
207 /// this basic block is entered via an exception handler.
208 void setIsLandingPad() { IsLandingPad = true; }
210 // Code Layout methods.
212 /// moveBefore/moveAfter - move 'this' block before or after the specified
213 /// block. This only moves the block, it does not modify the CFG or adjust
214 /// potential fall-throughs at the end of the block.
215 void moveBefore(MachineBasicBlock *NewAfter);
216 void moveAfter(MachineBasicBlock *NewBefore);
218 // Machine-CFG mutators
220 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
221 /// The Predecessors list of succ is automatically updated.
223 void addSuccessor(MachineBasicBlock *succ);
225 /// removeSuccessor - Remove successor from the successors list of this
226 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
228 void removeSuccessor(MachineBasicBlock *succ);
230 /// removeSuccessor - Remove specified successor from the successors list of
231 /// this MachineBasicBlock. The Predecessors list of succ is automatically
232 /// updated. Return the iterator to the element after the one removed.
234 succ_iterator removeSuccessor(succ_iterator I);
236 /// transferSuccessors - Transfers all the successors from MBB to this
237 /// machine basic block (i.e., copies all the successors fromMBB and
238 /// remove all the successors fromBB).
239 void transferSuccessors(MachineBasicBlock *fromMBB);
241 /// isSuccessor - Return true if the specified MBB is a successor of this
243 bool isSuccessor(const MachineBasicBlock *MBB) const;
245 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
246 /// immediately after this block, such that if this block exits by
247 /// falling through, control will transfer to the specified MBB. Note
248 /// that MBB need not be a successor at all, for example if this block
249 /// ends with an unconditional branch to some other block.
250 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
252 /// getFirstTerminator - returns an iterator to the first terminator
253 /// instruction of this basic block. If a terminator does not exist,
255 iterator getFirstTerminator();
257 /// isOnlyReachableViaFallthough - Return true if this basic block has
258 /// exactly one predecessor and the control transfer mechanism between
259 /// the predecessor and this block is a fall-through.
260 bool isOnlyReachableByFallthrough() const;
262 void pop_front() { Insts.pop_front(); }
263 void pop_back() { Insts.pop_back(); }
264 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
265 template<typename IT>
266 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
267 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
269 // erase - Remove the specified element or range from the instruction list.
270 // These functions delete any instructions removed.
272 iterator erase(iterator I) { return Insts.erase(I); }
273 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
274 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
275 void clear() { Insts.clear(); }
277 /// splice - Take an instruction from MBB 'Other' at the position From,
278 /// and insert it into this MBB right before 'where'.
279 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
280 Insts.splice(where, Other->Insts, From);
283 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
284 /// To), and insert them into this MBB right before 'where'.
285 void splice(iterator where, MachineBasicBlock *Other, iterator From,
287 Insts.splice(where, Other->Insts, From, To);
290 /// removeFromParent - This method unlinks 'this' from the containing
291 /// function, and returns it, but does not delete it.
292 MachineBasicBlock *removeFromParent();
294 /// eraseFromParent - This method unlinks 'this' from the containing
295 /// function and deletes it.
296 void eraseFromParent();
298 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
299 /// 'Old', change the code and CFG so that it branches to 'New' instead.
300 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
302 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
303 /// the CFG to be inserted. If we have proven that MBB can only branch to
304 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
305 /// DestB can be null. Besides DestA and DestB, retain other edges leading
306 /// to LandingPads (currently there can be only one; we don't check or require
307 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
308 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
309 MachineBasicBlock *DestB,
312 // Debugging methods.
314 void print(std::ostream &OS,
315 const PrefixPrinter &prefix = PrefixPrinter()) const;
316 void print(std::ostream *OS,
317 const PrefixPrinter &prefix = PrefixPrinter()) const {
318 if (OS) print(*OS, prefix);
320 void print(raw_ostream &OS,
321 const PrefixPrinter &prefix = PrefixPrinter()) const;
322 void print(raw_ostream *OS,
323 const PrefixPrinter &prefix = PrefixPrinter()) const {
324 if (OS) print(*OS, prefix);
327 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
328 /// level, unless they're not in a MachineFunction yet, in which case this
331 int getNumber() const { return Number; }
332 void setNumber(int N) { Number = N; }
334 private: // Methods used to maintain doubly linked list of blocks...
335 friend struct ilist_traits<MachineBasicBlock>;
337 // Machine-CFG mutators
339 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
340 /// Don't do this unless you know what you're doing, because it doesn't
341 /// update pred's successors list. Use pred->addSuccessor instead.
343 void addPredecessor(MachineBasicBlock *pred);
345 /// removePredecessor - Remove pred as a predecessor of this
346 /// MachineBasicBlock. Don't do this unless you know what you're
347 /// doing, because it doesn't update pred's successors list. Use
348 /// pred->removeSuccessor instead.
350 void removePredecessor(MachineBasicBlock *pred);
353 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
354 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
356 //===--------------------------------------------------------------------===//
357 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
358 //===--------------------------------------------------------------------===//
360 // Provide specializations of GraphTraits to be able to treat a
361 // MachineFunction as a graph of MachineBasicBlocks...
364 template <> struct GraphTraits<MachineBasicBlock *> {
365 typedef MachineBasicBlock NodeType;
366 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
368 static NodeType *getEntryNode(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 template <> struct GraphTraits<const MachineBasicBlock *> {
378 typedef const MachineBasicBlock NodeType;
379 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
381 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
382 static inline ChildIteratorType child_begin(NodeType *N) {
383 return N->succ_begin();
385 static inline ChildIteratorType child_end(NodeType *N) {
386 return N->succ_end();
390 // Provide specializations of GraphTraits to be able to treat a
391 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
392 // in inverse order. Inverse order for a function is considered
393 // to be when traversing the predecessor edges of a MBB
394 // instead of the successor edges.
396 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
397 typedef MachineBasicBlock NodeType;
398 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
399 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
402 static inline ChildIteratorType child_begin(NodeType *N) {
403 return N->pred_begin();
405 static inline ChildIteratorType child_end(NodeType *N) {
406 return N->pred_end();
410 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
411 typedef const MachineBasicBlock NodeType;
412 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
413 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
416 static inline ChildIteratorType child_begin(NodeType *N) {
417 return N->pred_begin();
419 static inline ChildIteratorType child_end(NodeType *N) {
420 return N->pred_end();
424 } // End llvm namespace