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;
27 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
29 mutable ilist_half_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 MachineBasicBlock() {}
82 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
86 // MachineBasicBlocks are allocated and owned by MachineFunction.
87 friend class MachineFunction;
90 /// getBasicBlock - Return the LLVM basic block that this instance
91 /// corresponded to originally.
93 const BasicBlock *getBasicBlock() const { return BB; }
95 /// getParent - Return the MachineFunction containing this basic block.
97 const MachineFunction *getParent() const { return xParent; }
98 MachineFunction *getParent() { return xParent; }
100 typedef Instructions::iterator iterator;
101 typedef Instructions::const_iterator const_iterator;
102 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
103 typedef std::reverse_iterator<iterator> reverse_iterator;
105 unsigned size() const { return (unsigned)Insts.size(); }
106 bool empty() const { return Insts.empty(); }
108 MachineInstr& front() { return Insts.front(); }
109 MachineInstr& back() { return Insts.back(); }
110 const MachineInstr& front() const { return Insts.front(); }
111 const MachineInstr& back() const { return Insts.back(); }
113 iterator begin() { return Insts.begin(); }
114 const_iterator begin() const { return Insts.begin(); }
115 iterator end() { return Insts.end(); }
116 const_iterator end() const { return Insts.end(); }
117 reverse_iterator rbegin() { return Insts.rbegin(); }
118 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
119 reverse_iterator rend () { return Insts.rend(); }
120 const_reverse_iterator rend () const { return Insts.rend(); }
122 // Machine-CFG iterators
123 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
124 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
125 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
126 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
127 typedef std::vector<MachineBasicBlock *>::reverse_iterator
128 pred_reverse_iterator;
129 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
130 const_pred_reverse_iterator;
131 typedef std::vector<MachineBasicBlock *>::reverse_iterator
132 succ_reverse_iterator;
133 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
134 const_succ_reverse_iterator;
136 pred_iterator pred_begin() { return Predecessors.begin(); }
137 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
138 pred_iterator pred_end() { return Predecessors.end(); }
139 const_pred_iterator pred_end() const { return Predecessors.end(); }
140 pred_reverse_iterator pred_rbegin()
141 { return Predecessors.rbegin();}
142 const_pred_reverse_iterator pred_rbegin() const
143 { return Predecessors.rbegin();}
144 pred_reverse_iterator pred_rend()
145 { return Predecessors.rend(); }
146 const_pred_reverse_iterator pred_rend() const
147 { return Predecessors.rend(); }
148 unsigned pred_size() const {
149 return (unsigned)Predecessors.size();
151 bool pred_empty() const { return Predecessors.empty(); }
152 bool isPred(MachineBasicBlock *MBB) const {
153 return std::find(pred_begin(), pred_end(), MBB) != pred_end();
156 succ_iterator succ_begin() { return Successors.begin(); }
157 const_succ_iterator succ_begin() const { return Successors.begin(); }
158 succ_iterator succ_end() { return Successors.end(); }
159 const_succ_iterator succ_end() const { return Successors.end(); }
160 succ_reverse_iterator succ_rbegin()
161 { return Successors.rbegin(); }
162 const_succ_reverse_iterator succ_rbegin() const
163 { return Successors.rbegin(); }
164 succ_reverse_iterator succ_rend()
165 { return Successors.rend(); }
166 const_succ_reverse_iterator succ_rend() const
167 { return Successors.rend(); }
168 unsigned succ_size() const {
169 return (unsigned)Successors.size();
171 bool succ_empty() const { return Successors.empty(); }
172 bool isSucc(MachineBasicBlock *MBB) const {
173 return std::find(succ_begin(), succ_end(), MBB) != succ_end();
176 // LiveIn management methods.
178 /// addLiveIn - Add the specified register as a live in. Note that it
179 /// is an error to add the same register to the same set more than once.
180 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
182 /// removeLiveIn - Remove the specified register from the live in set.
184 void removeLiveIn(unsigned Reg);
186 /// isLiveIn - Return true if the specified register is in the live in set.
188 bool isLiveIn(unsigned Reg) const;
190 // Iteration support for live in sets. These sets are kept in sorted
191 // order by their register number.
192 typedef std::vector<unsigned>::iterator livein_iterator;
193 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
194 livein_iterator livein_begin() { return LiveIns.begin(); }
195 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
196 livein_iterator livein_end() { return LiveIns.end(); }
197 const_livein_iterator livein_end() const { return LiveIns.end(); }
198 bool livein_empty() const { return LiveIns.empty(); }
200 /// getAlignment - Return alignment of the basic block.
202 unsigned getAlignment() const { return Alignment; }
204 /// setAlignment - Set alignment of the basic block.
206 void setAlignment(unsigned Align) { Alignment = Align; }
208 /// isLandingPad - Returns true if the block is a landing pad. That is
209 /// this basic block is entered via an exception handler.
210 bool isLandingPad() const { return IsLandingPad; }
212 /// setIsLandingPad - Indicates the block is a landing pad. That is
213 /// this basic block is entered via an exception handler.
214 void setIsLandingPad() { IsLandingPad = true; }
216 // Code Layout methods.
218 /// moveBefore/moveAfter - move 'this' block before or after the specified
219 /// block. This only moves the block, it does not modify the CFG or adjust
220 /// potential fall-throughs at the end of the block.
221 void moveBefore(MachineBasicBlock *NewAfter);
222 void moveAfter(MachineBasicBlock *NewBefore);
224 // Machine-CFG mutators
226 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
227 /// The Predecessors list of succ is automatically updated.
229 void addSuccessor(MachineBasicBlock *succ);
231 /// removeSuccessor - Remove successor from the successors list of this
232 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
234 void removeSuccessor(MachineBasicBlock *succ);
236 /// removeSuccessor - Remove specified successor from the successors list of
237 /// this MachineBasicBlock. The Predecessors list of succ is automatically
238 /// updated. Return the iterator to the element after the one removed.
240 succ_iterator removeSuccessor(succ_iterator I);
242 /// transferSuccessors - Transfers all the successors from MBB to this
243 /// machine basic block (i.e., copies all the successors fromMBB and
244 /// remove all the successors fromBB).
245 void transferSuccessors(MachineBasicBlock *fromMBB);
247 /// isSuccessor - Return true if the specified MBB is a successor of this
249 bool isSuccessor(const MachineBasicBlock *MBB) const;
251 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
252 /// immediately after this block, such that if this block exits by
253 /// falling through, control will transfer to the specified MBB. Note
254 /// that MBB need not be a successor at all, for example if this block
255 /// ends with an unconditional branch to some other block.
256 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
258 /// getFirstTerminator - returns an iterator to the first terminator
259 /// instruction of this basic block. If a terminator does not exist,
261 iterator getFirstTerminator();
263 /// isOnlyReachableViaFallthough - Return true if this basic block has
264 /// exactly one predecessor and the control transfer mechanism between
265 /// the predecessor and this block is a fall-through.
266 bool isOnlyReachableByFallthrough() const;
268 void pop_front() { Insts.pop_front(); }
269 void pop_back() { Insts.pop_back(); }
270 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
271 template<typename IT>
272 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
273 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
275 // erase - Remove the specified element or range from the instruction list.
276 // These functions delete any instructions removed.
278 iterator erase(iterator I) { return Insts.erase(I); }
279 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
280 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
281 void clear() { Insts.clear(); }
283 /// splice - Take an instruction from MBB 'Other' at the position From,
284 /// and insert it into this MBB right before 'where'.
285 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
286 Insts.splice(where, Other->Insts, From);
289 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
290 /// To), and insert them into this MBB right before 'where'.
291 void splice(iterator where, MachineBasicBlock *Other, iterator From,
293 Insts.splice(where, Other->Insts, From, To);
296 /// removeFromParent - This method unlinks 'this' from the containing
297 /// function, and returns it, but does not delete it.
298 MachineBasicBlock *removeFromParent();
300 /// eraseFromParent - This method unlinks 'this' from the containing
301 /// function and deletes it.
302 void eraseFromParent();
304 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
305 /// 'Old', change the code and CFG so that it branches to 'New' instead.
306 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
308 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
309 /// the CFG to be inserted. If we have proven that MBB can only branch to
310 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
311 /// DestB can be null. Besides DestA and DestB, retain other edges leading
312 /// to LandingPads (currently there can be only one; we don't check or require
313 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
314 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
315 MachineBasicBlock *DestB,
318 // Debugging methods.
320 void print(raw_ostream &OS) const;
322 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
323 /// level, unless they're not in a MachineFunction yet, in which case this
326 int getNumber() const { return Number; }
327 void setNumber(int N) { Number = N; }
329 private: // Methods used to maintain doubly linked list of blocks...
330 friend struct ilist_traits<MachineBasicBlock>;
332 // Machine-CFG mutators
334 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
335 /// Don't do this unless you know what you're doing, because it doesn't
336 /// update pred's successors list. Use pred->addSuccessor instead.
338 void addPredecessor(MachineBasicBlock *pred);
340 /// removePredecessor - Remove pred as a predecessor of this
341 /// MachineBasicBlock. Don't do this unless you know what you're
342 /// doing, because it doesn't update pred's successors list. Use
343 /// pred->removeSuccessor instead.
345 void removePredecessor(MachineBasicBlock *pred);
348 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
350 //===--------------------------------------------------------------------===//
351 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
352 //===--------------------------------------------------------------------===//
354 // Provide specializations of GraphTraits to be able to treat a
355 // MachineFunction as a graph of MachineBasicBlocks...
358 template <> struct GraphTraits<MachineBasicBlock *> {
359 typedef MachineBasicBlock NodeType;
360 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
362 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
363 static inline ChildIteratorType child_begin(NodeType *N) {
364 return N->succ_begin();
366 static inline ChildIteratorType child_end(NodeType *N) {
367 return N->succ_end();
371 template <> struct GraphTraits<const MachineBasicBlock *> {
372 typedef const MachineBasicBlock NodeType;
373 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
375 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
376 static inline ChildIteratorType child_begin(NodeType *N) {
377 return N->succ_begin();
379 static inline ChildIteratorType child_end(NodeType *N) {
380 return N->succ_end();
384 // Provide specializations of GraphTraits to be able to treat a
385 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
386 // in inverse order. Inverse order for a function is considered
387 // to be when traversing the predecessor edges of a MBB
388 // instead of the successor edges.
390 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
391 typedef MachineBasicBlock NodeType;
392 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
393 static NodeType *getEntryNode(Inverse<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 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
405 typedef const MachineBasicBlock NodeType;
406 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
407 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
410 static inline ChildIteratorType child_begin(NodeType *N) {
411 return N->pred_begin();
413 static inline ChildIteratorType child_end(NodeType *N) {
414 return N->pred_end();
418 } // End llvm namespace