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"
24 class MachineFunction;
27 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
29 mutable ilist_node<MachineInstr> Sentinel;
31 // this is only set by the MachineBasicBlock owning the LiveList
32 friend class MachineBasicBlock;
33 MachineBasicBlock* Parent;
36 MachineInstr *createSentinel() const {
37 return static_cast<MachineInstr*>(&Sentinel);
39 void destroySentinel(MachineInstr *) const {}
41 MachineInstr *provideInitialHead() const { return createSentinel(); }
42 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
43 static void noteHead(MachineInstr*, MachineInstr*) {}
45 void addNodeToList(MachineInstr* N);
46 void removeNodeFromList(MachineInstr* N);
47 void transferNodesFromList(ilist_traits &SrcTraits,
48 ilist_iterator<MachineInstr> first,
49 ilist_iterator<MachineInstr> last);
50 void deleteNode(MachineInstr *N);
52 void createNode(const MachineInstr &);
55 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
56 typedef ilist<MachineInstr> Instructions;
60 MachineFunction *xParent;
62 /// Predecessors/Successors - Keep track of the predecessor / successor
64 std::vector<MachineBasicBlock *> Predecessors;
65 std::vector<MachineBasicBlock *> Successors;
67 /// LiveIns - Keep track of the physical registers that are livein of
69 std::vector<unsigned> LiveIns;
71 /// Alignment - Alignment of the basic block. Zero if the basic block does
72 /// not need to be aligned.
75 /// IsLandingPad - Indicate that this basic block is entered via an
76 /// exception handler.
79 // Intrusive list support
80 friend struct ilist_sentinel_traits<MachineBasicBlock>;
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(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(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 void pop_front() { Insts.pop_front(); }
258 void pop_back() { Insts.pop_back(); }
259 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
260 template<typename IT>
261 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
262 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
264 // erase - Remove the specified element or range from the instruction list.
265 // These functions delete any instructions removed.
267 iterator erase(iterator I) { return Insts.erase(I); }
268 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
269 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
270 void clear() { Insts.clear(); }
272 /// splice - Take an instruction from MBB 'Other' at the position From,
273 /// and insert it into this MBB right before 'where'.
274 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
275 Insts.splice(where, Other->Insts, From);
278 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
279 /// To), and insert them into this MBB right before 'where'.
280 void splice(iterator where, MachineBasicBlock *Other, iterator From,
282 Insts.splice(where, Other->Insts, From, To);
285 /// removeFromParent - This method unlinks 'this' from the containing
286 /// function, and returns it, but does not delete it.
287 MachineBasicBlock *removeFromParent();
289 /// eraseFromParent - This method unlinks 'this' from the containing
290 /// function and deletes it.
291 void eraseFromParent();
293 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
294 /// 'Old', change the code and CFG so that it branches to 'New' instead.
295 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
297 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
298 /// the CFG to be inserted. If we have proven that MBB can only branch to
299 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
300 /// DestB can be null. Besides DestA and DestB, retain other edges leading
301 /// to LandingPads (currently there can be only one; we don't check or require
302 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
303 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
304 MachineBasicBlock *DestB,
307 // Debugging methods.
309 void print(std::ostream &OS) const;
310 void print(std::ostream *OS) const { if (OS) print(*OS); }
312 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
313 /// level, unless they're not in a MachineFunction yet, in which case this
316 int getNumber() const { return Number; }
317 void setNumber(int N) { Number = N; }
319 private: // Methods used to maintain doubly linked list of blocks...
320 friend struct ilist_traits<MachineBasicBlock>;
322 // Machine-CFG mutators
324 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
325 /// Don't do this unless you know what you're doing, because it doesn't
326 /// update pred's successors list. Use pred->addSuccessor instead.
328 void addPredecessor(MachineBasicBlock *pred);
330 /// removePredecessor - Remove pred as a predecessor of this
331 /// MachineBasicBlock. Don't do this unless you know what you're
332 /// doing, because it doesn't update pred's successors list. Use
333 /// pred->removeSuccessor instead.
335 void removePredecessor(MachineBasicBlock *pred);
338 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
340 //===--------------------------------------------------------------------===//
341 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
342 //===--------------------------------------------------------------------===//
344 // Provide specializations of GraphTraits to be able to treat a
345 // MachineFunction as a graph of MachineBasicBlocks...
348 template <> struct GraphTraits<MachineBasicBlock *> {
349 typedef MachineBasicBlock NodeType;
350 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
352 static NodeType *getEntryNode(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 template <> struct GraphTraits<const MachineBasicBlock *> {
362 typedef const MachineBasicBlock NodeType;
363 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
365 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
366 static inline ChildIteratorType child_begin(NodeType *N) {
367 return N->succ_begin();
369 static inline ChildIteratorType child_end(NodeType *N) {
370 return N->succ_end();
374 // Provide specializations of GraphTraits to be able to treat a
375 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
376 // in inverse order. Inverse order for a function is considered
377 // to be when traversing the predecessor edges of a MBB
378 // instead of the successor edges.
380 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
381 typedef MachineBasicBlock NodeType;
382 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
383 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
386 static inline ChildIteratorType child_begin(NodeType *N) {
387 return N->pred_begin();
389 static inline ChildIteratorType child_end(NodeType *N) {
390 return N->pred_end();
394 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
395 typedef const MachineBasicBlock NodeType;
396 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
397 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
400 static inline ChildIteratorType child_begin(NodeType *N) {
401 return N->pred_begin();
403 static inline ChildIteratorType child_end(NodeType *N) {
404 return N->pred_end();
408 } // End llvm namespace