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 void addNodeToList(MachineInstr* N);
42 void removeNodeFromList(MachineInstr* N);
43 void transferNodesFromList(ilist_traits &SrcTraits,
44 ilist_iterator<MachineInstr> first,
45 ilist_iterator<MachineInstr> last);
46 void deleteNode(MachineInstr *N);
48 void createNode(const MachineInstr &);
51 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
52 typedef ilist<MachineInstr> Instructions;
56 MachineFunction *xParent;
58 /// Predecessors/Successors - Keep track of the predecessor / successor
60 std::vector<MachineBasicBlock *> Predecessors;
61 std::vector<MachineBasicBlock *> Successors;
63 /// LiveIns - Keep track of the physical registers that are livein of
65 std::vector<unsigned> LiveIns;
67 /// Alignment - Alignment of the basic block. Zero if the basic block does
68 /// not need to be aligned.
71 /// IsLandingPad - Indicate that this basic block is entered via an
72 /// exception handler.
75 // Intrusive list support
76 friend struct ilist_sentinel_traits<MachineBasicBlock>;
77 MachineBasicBlock() {}
79 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
83 // MachineBasicBlocks are allocated and owned by MachineFunction.
84 friend class MachineFunction;
87 /// getBasicBlock - Return the LLVM basic block that this instance
88 /// corresponded to originally.
90 const BasicBlock *getBasicBlock() const { return BB; }
92 /// getParent - Return the MachineFunction containing this basic block.
94 const MachineFunction *getParent() const { return xParent; }
95 MachineFunction *getParent() { return xParent; }
97 typedef Instructions::iterator iterator;
98 typedef Instructions::const_iterator const_iterator;
99 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
100 typedef std::reverse_iterator<iterator> reverse_iterator;
102 unsigned size() const { return (unsigned)Insts.size(); }
103 bool empty() const { return Insts.empty(); }
105 MachineInstr& front() { return Insts.front(); }
106 MachineInstr& back() { return Insts.back(); }
107 const MachineInstr& front() const { return Insts.front(); }
108 const MachineInstr& back() const { return Insts.back(); }
110 iterator begin() { return Insts.begin(); }
111 const_iterator begin() const { return Insts.begin(); }
112 iterator end() { return Insts.end(); }
113 const_iterator end() const { return Insts.end(); }
114 reverse_iterator rbegin() { return Insts.rbegin(); }
115 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
116 reverse_iterator rend () { return Insts.rend(); }
117 const_reverse_iterator rend () const { return Insts.rend(); }
119 // Machine-CFG iterators
120 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
121 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
122 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
123 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
124 typedef std::vector<MachineBasicBlock *>::reverse_iterator
125 pred_reverse_iterator;
126 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
127 const_pred_reverse_iterator;
128 typedef std::vector<MachineBasicBlock *>::reverse_iterator
129 succ_reverse_iterator;
130 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
131 const_succ_reverse_iterator;
133 pred_iterator pred_begin() { return Predecessors.begin(); }
134 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
135 pred_iterator pred_end() { return Predecessors.end(); }
136 const_pred_iterator pred_end() const { return Predecessors.end(); }
137 pred_reverse_iterator pred_rbegin()
138 { return Predecessors.rbegin();}
139 const_pred_reverse_iterator pred_rbegin() const
140 { return Predecessors.rbegin();}
141 pred_reverse_iterator pred_rend()
142 { return Predecessors.rend(); }
143 const_pred_reverse_iterator pred_rend() const
144 { return Predecessors.rend(); }
145 unsigned pred_size() const {
146 return (unsigned)Predecessors.size();
148 bool pred_empty() const { return Predecessors.empty(); }
149 succ_iterator succ_begin() { return Successors.begin(); }
150 const_succ_iterator succ_begin() const { return Successors.begin(); }
151 succ_iterator succ_end() { return Successors.end(); }
152 const_succ_iterator succ_end() const { return Successors.end(); }
153 succ_reverse_iterator succ_rbegin()
154 { return Successors.rbegin(); }
155 const_succ_reverse_iterator succ_rbegin() const
156 { return Successors.rbegin(); }
157 succ_reverse_iterator succ_rend()
158 { return Successors.rend(); }
159 const_succ_reverse_iterator succ_rend() const
160 { return Successors.rend(); }
161 unsigned succ_size() const {
162 return (unsigned)Successors.size();
164 bool succ_empty() const { return Successors.empty(); }
166 // LiveIn management methods.
168 /// addLiveIn - Add the specified register as a live in. Note that it
169 /// is an error to add the same register to the same set more than once.
170 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
172 /// removeLiveIn - Remove the specified register from the live in set.
174 void removeLiveIn(unsigned Reg);
176 /// isLiveIn - Return true if the specified register is in the live in set.
178 bool isLiveIn(unsigned Reg) const;
180 // Iteration support for live in sets. These sets are kept in sorted
181 // order by their register number.
182 typedef std::vector<unsigned>::iterator livein_iterator;
183 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
184 livein_iterator livein_begin() { return LiveIns.begin(); }
185 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
186 livein_iterator livein_end() { return LiveIns.end(); }
187 const_livein_iterator livein_end() const { return LiveIns.end(); }
188 bool livein_empty() const { return LiveIns.empty(); }
190 /// getAlignment - Return alignment of the basic block.
192 unsigned getAlignment() const { return Alignment; }
194 /// setAlignment - Set alignment of the basic block.
196 void setAlignment(unsigned Align) { Alignment = Align; }
198 /// isLandingPad - Returns true if the block is a landing pad. That is
199 /// this basic block is entered via an exception handler.
200 bool isLandingPad() const { return IsLandingPad; }
202 /// setIsLandingPad - Indicates the block is a landing pad. That is
203 /// this basic block is entered via an exception handler.
204 void setIsLandingPad() { IsLandingPad = true; }
206 // Code Layout methods.
208 /// moveBefore/moveAfter - move 'this' block before or after the specified
209 /// block. This only moves the block, it does not modify the CFG or adjust
210 /// potential fall-throughs at the end of the block.
211 void moveBefore(MachineBasicBlock *NewAfter);
212 void moveAfter(MachineBasicBlock *NewBefore);
214 // Machine-CFG mutators
216 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
217 /// The Predecessors list of succ is automatically updated.
219 void addSuccessor(MachineBasicBlock *succ);
221 /// removeSuccessor - Remove successor from the successors list of this
222 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
224 void removeSuccessor(MachineBasicBlock *succ);
226 /// removeSuccessor - Remove specified successor from the successors list of
227 /// this MachineBasicBlock. The Predecessors list of succ is automatically
228 /// updated. Return the iterator to the element after the one removed.
230 succ_iterator removeSuccessor(succ_iterator I);
232 /// transferSuccessors - Transfers all the successors from MBB to this
233 /// machine basic block (i.e., copies all the successors fromMBB and
234 /// remove all the successors fromBB).
235 void transferSuccessors(MachineBasicBlock *fromMBB);
237 /// isSuccessor - Return true if the specified MBB is a successor of this
239 bool isSuccessor(MachineBasicBlock *MBB) const;
241 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
242 /// immediately after this block, such that if this block exits by
243 /// falling through, control will transfer to the specified MBB. Note
244 /// that MBB need not be a successor at all, for example if this block
245 /// ends with an unconditional branch to some other block.
246 bool isLayoutSuccessor(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 an instruction from MBB 'Other' at the position From,
269 /// and insert it into this MBB right before 'where'.
270 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
271 Insts.splice(where, Other->Insts, From);
274 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
275 /// To), and insert them into this MBB right before 'where'.
276 void splice(iterator where, MachineBasicBlock *Other, iterator From,
278 Insts.splice(where, Other->Insts, From, To);
281 /// removeFromParent - This method unlinks 'this' from the containing
282 /// function, and returns it, but does not delete it.
283 MachineBasicBlock *removeFromParent();
285 /// eraseFromParent - This method unlinks 'this' from the containing
286 /// function and deletes it.
287 void eraseFromParent();
289 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
290 /// 'Old', change the code and CFG so that it branches to 'New' instead.
291 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
293 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
294 /// the CFG to be inserted. If we have proven that MBB can only branch to
295 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
296 /// DestB can be null. Besides DestA and DestB, retain other edges leading
297 /// to LandingPads (currently there can be only one; we don't check or require
298 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
299 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
300 MachineBasicBlock *DestB,
303 // Debugging methods.
305 void print(std::ostream &OS) const;
306 void print(std::ostream *OS) const { if (OS) print(*OS); }
308 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
309 /// level, unless they're not in a MachineFunction yet, in which case this
312 int getNumber() const { return Number; }
313 void setNumber(int N) { Number = N; }
315 private: // Methods used to maintain doubly linked list of blocks...
316 friend struct ilist_traits<MachineBasicBlock>;
318 // Machine-CFG mutators
320 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
321 /// Don't do this unless you know what you're doing, because it doesn't
322 /// update pred's successors list. Use pred->addSuccessor instead.
324 void addPredecessor(MachineBasicBlock *pred);
326 /// removePredecessor - Remove pred as a predecessor of this
327 /// MachineBasicBlock. Don't do this unless you know what you're
328 /// doing, because it doesn't update pred's successors list. Use
329 /// pred->removeSuccessor instead.
331 void removePredecessor(MachineBasicBlock *pred);
334 std::ostream& operator<<(std::ostream &OS, const MachineBasicBlock &MBB);
336 //===--------------------------------------------------------------------===//
337 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
338 //===--------------------------------------------------------------------===//
340 // Provide specializations of GraphTraits to be able to treat a
341 // MachineFunction as a graph of MachineBasicBlocks...
344 template <> struct GraphTraits<MachineBasicBlock *> {
345 typedef MachineBasicBlock NodeType;
346 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
348 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
349 static inline ChildIteratorType child_begin(NodeType *N) {
350 return N->succ_begin();
352 static inline ChildIteratorType child_end(NodeType *N) {
353 return N->succ_end();
357 template <> struct GraphTraits<const MachineBasicBlock *> {
358 typedef const MachineBasicBlock NodeType;
359 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
361 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
362 static inline ChildIteratorType child_begin(NodeType *N) {
363 return N->succ_begin();
365 static inline ChildIteratorType child_end(NodeType *N) {
366 return N->succ_end();
370 // Provide specializations of GraphTraits to be able to treat a
371 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
372 // in inverse order. Inverse order for a function is considered
373 // to be when traversing the predecessor edges of a MBB
374 // instead of the successor edges.
376 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
377 typedef MachineBasicBlock NodeType;
378 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
379 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
382 static inline ChildIteratorType child_begin(NodeType *N) {
383 return N->pred_begin();
385 static inline ChildIteratorType child_end(NodeType *N) {
386 return N->pred_end();
390 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
391 typedef const MachineBasicBlock NodeType;
392 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
393 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
396 static inline ChildIteratorType child_begin(NodeType *N) {
397 return N->pred_begin();
399 static inline ChildIteratorType child_end(NodeType *N) {
400 return N->pred_end();
404 } // End llvm namespace