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 /// AddressTaken - Indicate that this basic block is potentially the
80 /// target of an indirect branch.
83 // Intrusive list support
84 MachineBasicBlock() {}
86 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
90 // MachineBasicBlocks are allocated and owned by MachineFunction.
91 friend class MachineFunction;
94 /// getBasicBlock - Return the LLVM basic block that this instance
95 /// corresponded to originally. Note that this may be NULL if this instance
96 /// does not correspond directly to an LLVM basic block.
98 const BasicBlock *getBasicBlock() const { return BB; }
100 /// getName - Return the name of the corresponding LLVM basic block, or
102 StringRef getName() const;
104 /// hasAddressTaken - Test whether this block is potentially the target
105 /// of an indirect branch.
106 bool hasAddressTaken() const { return AddressTaken; }
108 /// setHasAddressTaken - Set this block to reflect that it potentially
109 /// is the target of an indirect branch.
110 void setHasAddressTaken() { AddressTaken = true; }
112 /// getParent - Return the MachineFunction containing this basic block.
114 const MachineFunction *getParent() const { return xParent; }
115 MachineFunction *getParent() { return xParent; }
117 typedef Instructions::iterator iterator;
118 typedef Instructions::const_iterator const_iterator;
119 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
120 typedef std::reverse_iterator<iterator> reverse_iterator;
122 unsigned size() const { return (unsigned)Insts.size(); }
123 bool empty() const { return Insts.empty(); }
125 MachineInstr& front() { return Insts.front(); }
126 MachineInstr& back() { return Insts.back(); }
127 const MachineInstr& front() const { return Insts.front(); }
128 const MachineInstr& back() const { return Insts.back(); }
130 iterator begin() { return Insts.begin(); }
131 const_iterator begin() const { return Insts.begin(); }
132 iterator end() { return Insts.end(); }
133 const_iterator end() const { return Insts.end(); }
134 reverse_iterator rbegin() { return Insts.rbegin(); }
135 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
136 reverse_iterator rend () { return Insts.rend(); }
137 const_reverse_iterator rend () const { return Insts.rend(); }
139 // Machine-CFG iterators
140 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
141 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
142 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
143 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
144 typedef std::vector<MachineBasicBlock *>::reverse_iterator
145 pred_reverse_iterator;
146 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
147 const_pred_reverse_iterator;
148 typedef std::vector<MachineBasicBlock *>::reverse_iterator
149 succ_reverse_iterator;
150 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
151 const_succ_reverse_iterator;
153 pred_iterator pred_begin() { return Predecessors.begin(); }
154 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
155 pred_iterator pred_end() { return Predecessors.end(); }
156 const_pred_iterator pred_end() const { return Predecessors.end(); }
157 pred_reverse_iterator pred_rbegin()
158 { return Predecessors.rbegin();}
159 const_pred_reverse_iterator pred_rbegin() const
160 { return Predecessors.rbegin();}
161 pred_reverse_iterator pred_rend()
162 { return Predecessors.rend(); }
163 const_pred_reverse_iterator pred_rend() const
164 { return Predecessors.rend(); }
165 unsigned pred_size() const {
166 return (unsigned)Predecessors.size();
168 bool pred_empty() const { return Predecessors.empty(); }
169 succ_iterator succ_begin() { return Successors.begin(); }
170 const_succ_iterator succ_begin() const { return Successors.begin(); }
171 succ_iterator succ_end() { return Successors.end(); }
172 const_succ_iterator succ_end() const { return Successors.end(); }
173 succ_reverse_iterator succ_rbegin()
174 { return Successors.rbegin(); }
175 const_succ_reverse_iterator succ_rbegin() const
176 { return Successors.rbegin(); }
177 succ_reverse_iterator succ_rend()
178 { return Successors.rend(); }
179 const_succ_reverse_iterator succ_rend() const
180 { return Successors.rend(); }
181 unsigned succ_size() const {
182 return (unsigned)Successors.size();
184 bool succ_empty() const { return Successors.empty(); }
186 // LiveIn management methods.
188 /// addLiveIn - Add the specified register as a live in. Note that it
189 /// is an error to add the same register to the same set more than once.
190 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
192 /// removeLiveIn - Remove the specified register from the live in set.
194 void removeLiveIn(unsigned Reg);
196 /// isLiveIn - Return true if the specified register is in the live in set.
198 bool isLiveIn(unsigned Reg) const;
200 // Iteration support for live in sets. These sets are kept in sorted
201 // order by their register number.
202 typedef std::vector<unsigned>::iterator livein_iterator;
203 typedef std::vector<unsigned>::const_iterator const_livein_iterator;
204 livein_iterator livein_begin() { return LiveIns.begin(); }
205 const_livein_iterator livein_begin() const { return LiveIns.begin(); }
206 livein_iterator livein_end() { return LiveIns.end(); }
207 const_livein_iterator livein_end() const { return LiveIns.end(); }
208 bool livein_empty() const { return LiveIns.empty(); }
210 /// getAlignment - Return alignment of the basic block.
212 unsigned getAlignment() const { return Alignment; }
214 /// setAlignment - Set alignment of the basic block.
216 void setAlignment(unsigned Align) { Alignment = Align; }
218 /// isLandingPad - Returns true if the block is a landing pad. That is
219 /// this basic block is entered via an exception handler.
220 bool isLandingPad() const { return IsLandingPad; }
222 /// setIsLandingPad - Indicates the block is a landing pad. That is
223 /// this basic block is entered via an exception handler.
224 void setIsLandingPad() { IsLandingPad = true; }
226 // Code Layout methods.
228 /// moveBefore/moveAfter - move 'this' block before or after the specified
229 /// block. This only moves the block, it does not modify the CFG or adjust
230 /// potential fall-throughs at the end of the block.
231 void moveBefore(MachineBasicBlock *NewAfter);
232 void moveAfter(MachineBasicBlock *NewBefore);
234 /// updateTerminator - Update the terminator instructions in block to account
235 /// for changes to the layout. If the block previously used a fallthrough,
236 /// it may now need a branch, and if it previously used branching it may now
237 /// be able to use a fallthrough.
238 void updateTerminator();
240 // Machine-CFG mutators
242 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
243 /// The Predecessors list of succ is automatically updated.
245 void addSuccessor(MachineBasicBlock *succ);
247 /// removeSuccessor - Remove successor from the successors list of this
248 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
250 void removeSuccessor(MachineBasicBlock *succ);
252 /// removeSuccessor - Remove specified successor from the successors list of
253 /// this MachineBasicBlock. The Predecessors list of succ is automatically
254 /// updated. Return the iterator to the element after the one removed.
256 succ_iterator removeSuccessor(succ_iterator I);
258 /// transferSuccessors - Transfers all the successors from MBB to this
259 /// machine basic block (i.e., copies all the successors fromMBB and
260 /// remove all the successors from fromMBB).
261 void transferSuccessors(MachineBasicBlock *fromMBB);
263 /// isSuccessor - Return true if the specified MBB is a successor of this
265 bool isSuccessor(const MachineBasicBlock *MBB) const;
267 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
268 /// immediately after this block, such that if this block exits by
269 /// falling through, control will transfer to the specified MBB. Note
270 /// that MBB need not be a successor at all, for example if this block
271 /// ends with an unconditional branch to some other block.
272 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
274 /// getFirstTerminator - returns an iterator to the first terminator
275 /// instruction of this basic block. If a terminator does not exist,
277 iterator getFirstTerminator();
279 /// isOnlyReachableViaFallthough - Return true if this basic block has
280 /// exactly one predecessor and the control transfer mechanism between
281 /// the predecessor and this block is a fall-through.
282 bool isOnlyReachableByFallthrough() const;
284 void pop_front() { Insts.pop_front(); }
285 void pop_back() { Insts.pop_back(); }
286 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
287 template<typename IT>
288 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
289 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
291 // erase - Remove the specified element or range from the instruction list.
292 // These functions delete any instructions removed.
294 iterator erase(iterator I) { return Insts.erase(I); }
295 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
296 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
297 void clear() { Insts.clear(); }
299 /// splice - Take an instruction from MBB 'Other' at the position From,
300 /// and insert it into this MBB right before 'where'.
301 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
302 Insts.splice(where, Other->Insts, From);
305 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
306 /// To), and insert them into this MBB right before 'where'.
307 void splice(iterator where, MachineBasicBlock *Other, iterator From,
309 Insts.splice(where, Other->Insts, From, To);
312 /// removeFromParent - This method unlinks 'this' from the containing
313 /// function, and returns it, but does not delete it.
314 MachineBasicBlock *removeFromParent();
316 /// eraseFromParent - This method unlinks 'this' from the containing
317 /// function and deletes it.
318 void eraseFromParent();
320 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
321 /// 'Old', change the code and CFG so that it branches to 'New' instead.
322 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
324 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
325 /// the CFG to be inserted. If we have proven that MBB can only branch to
326 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
327 /// DestB can be null. Besides DestA and DestB, retain other edges leading
328 /// to LandingPads (currently there can be only one; we don't check or require
329 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
330 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
331 MachineBasicBlock *DestB,
334 // Debugging methods.
336 void print(raw_ostream &OS) const;
338 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
339 /// level, unless they're not in a MachineFunction yet, in which case this
342 int getNumber() const { return Number; }
343 void setNumber(int N) { Number = N; }
345 private: // Methods used to maintain doubly linked list of blocks...
346 friend struct ilist_traits<MachineBasicBlock>;
348 // Machine-CFG mutators
350 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
351 /// Don't do this unless you know what you're doing, because it doesn't
352 /// update pred's successors list. Use pred->addSuccessor instead.
354 void addPredecessor(MachineBasicBlock *pred);
356 /// removePredecessor - Remove pred as a predecessor of this
357 /// MachineBasicBlock. Don't do this unless you know what you're
358 /// doing, because it doesn't update pred's successors list. Use
359 /// pred->removeSuccessor instead.
361 void removePredecessor(MachineBasicBlock *pred);
364 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
366 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
368 //===--------------------------------------------------------------------===//
369 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
370 //===--------------------------------------------------------------------===//
372 // Provide specializations of GraphTraits to be able to treat a
373 // MachineFunction as a graph of MachineBasicBlocks...
376 template <> struct GraphTraits<MachineBasicBlock *> {
377 typedef MachineBasicBlock NodeType;
378 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
380 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
381 static inline ChildIteratorType child_begin(NodeType *N) {
382 return N->succ_begin();
384 static inline ChildIteratorType child_end(NodeType *N) {
385 return N->succ_end();
389 template <> struct GraphTraits<const MachineBasicBlock *> {
390 typedef const MachineBasicBlock NodeType;
391 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
393 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
394 static inline ChildIteratorType child_begin(NodeType *N) {
395 return N->succ_begin();
397 static inline ChildIteratorType child_end(NodeType *N) {
398 return N->succ_end();
402 // Provide specializations of GraphTraits to be able to treat a
403 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
404 // in inverse order. Inverse order for a function is considered
405 // to be when traversing the predecessor edges of a MBB
406 // instead of the successor edges.
408 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
409 typedef MachineBasicBlock NodeType;
410 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
411 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
414 static inline ChildIteratorType child_begin(NodeType *N) {
415 return N->pred_begin();
417 static inline ChildIteratorType child_end(NodeType *N) {
418 return N->pred_end();
422 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
423 typedef const MachineBasicBlock NodeType;
424 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
425 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
428 static inline ChildIteratorType child_begin(NodeType *N) {
429 return N->pred_begin();
431 static inline ChildIteratorType child_end(NodeType *N) {
432 return N->pred_end();
436 } // End llvm namespace