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;
29 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
31 mutable ilist_half_node<MachineInstr> Sentinel;
33 // this is only set by the MachineBasicBlock owning the LiveList
34 friend class MachineBasicBlock;
35 MachineBasicBlock* Parent;
38 MachineInstr *createSentinel() const {
39 return static_cast<MachineInstr*>(&Sentinel);
41 void destroySentinel(MachineInstr *) const {}
43 MachineInstr *provideInitialHead() const { return createSentinel(); }
44 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
45 static void noteHead(MachineInstr*, MachineInstr*) {}
47 void addNodeToList(MachineInstr* N);
48 void removeNodeFromList(MachineInstr* N);
49 void transferNodesFromList(ilist_traits &SrcTraits,
50 ilist_iterator<MachineInstr> first,
51 ilist_iterator<MachineInstr> last);
52 void deleteNode(MachineInstr *N);
54 void createNode(const MachineInstr &);
57 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
58 typedef ilist<MachineInstr> Instructions;
62 MachineFunction *xParent;
64 /// Predecessors/Successors - Keep track of the predecessor / successor
66 std::vector<MachineBasicBlock *> Predecessors;
67 std::vector<MachineBasicBlock *> Successors;
69 /// LiveIns - Keep track of the physical registers that are livein of
71 std::vector<unsigned> LiveIns;
73 /// Alignment - Alignment of the basic block. Zero if the basic block does
74 /// not need to be aligned.
77 /// IsLandingPad - Indicate that this basic block is entered via an
78 /// exception handler.
81 /// AddressTaken - Indicate that this basic block is potentially the
82 /// target of an indirect branch.
85 // Intrusive list support
86 MachineBasicBlock() {}
88 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
92 // MachineBasicBlocks are allocated and owned by MachineFunction.
93 friend class MachineFunction;
96 /// getBasicBlock - Return the LLVM basic block that this instance
97 /// corresponded to originally. Note that this may be NULL if this instance
98 /// does not correspond directly to an LLVM basic block.
100 const BasicBlock *getBasicBlock() const { return BB; }
102 /// getName - Return the name of the corresponding LLVM basic block, or
104 StringRef getName() const;
106 /// hasAddressTaken - Test whether this block is potentially the target
107 /// of an indirect branch.
108 bool hasAddressTaken() const { return AddressTaken; }
110 /// setHasAddressTaken - Set this block to reflect that it potentially
111 /// is the target of an indirect branch.
112 void setHasAddressTaken() { AddressTaken = true; }
114 /// getParent - Return the MachineFunction containing this basic block.
116 const MachineFunction *getParent() const { return xParent; }
117 MachineFunction *getParent() { return xParent; }
119 typedef Instructions::iterator iterator;
120 typedef Instructions::const_iterator const_iterator;
121 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
122 typedef std::reverse_iterator<iterator> reverse_iterator;
124 unsigned size() const { return (unsigned)Insts.size(); }
125 bool empty() const { return Insts.empty(); }
127 MachineInstr& front() { return Insts.front(); }
128 MachineInstr& back() { return Insts.back(); }
129 const MachineInstr& front() const { return Insts.front(); }
130 const MachineInstr& back() const { return Insts.back(); }
132 iterator begin() { return Insts.begin(); }
133 const_iterator begin() const { return Insts.begin(); }
134 iterator end() { return Insts.end(); }
135 const_iterator end() const { return Insts.end(); }
136 reverse_iterator rbegin() { return Insts.rbegin(); }
137 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
138 reverse_iterator rend () { return Insts.rend(); }
139 const_reverse_iterator rend () const { return Insts.rend(); }
141 // Machine-CFG iterators
142 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
143 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
144 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
145 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
146 typedef std::vector<MachineBasicBlock *>::reverse_iterator
147 pred_reverse_iterator;
148 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
149 const_pred_reverse_iterator;
150 typedef std::vector<MachineBasicBlock *>::reverse_iterator
151 succ_reverse_iterator;
152 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
153 const_succ_reverse_iterator;
155 pred_iterator pred_begin() { return Predecessors.begin(); }
156 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
157 pred_iterator pred_end() { return Predecessors.end(); }
158 const_pred_iterator pred_end() const { return Predecessors.end(); }
159 pred_reverse_iterator pred_rbegin()
160 { return Predecessors.rbegin();}
161 const_pred_reverse_iterator pred_rbegin() const
162 { return Predecessors.rbegin();}
163 pred_reverse_iterator pred_rend()
164 { return Predecessors.rend(); }
165 const_pred_reverse_iterator pred_rend() const
166 { return Predecessors.rend(); }
167 unsigned pred_size() const {
168 return (unsigned)Predecessors.size();
170 bool pred_empty() const { return Predecessors.empty(); }
171 succ_iterator succ_begin() { return Successors.begin(); }
172 const_succ_iterator succ_begin() const { return Successors.begin(); }
173 succ_iterator succ_end() { return Successors.end(); }
174 const_succ_iterator succ_end() const { return Successors.end(); }
175 succ_reverse_iterator succ_rbegin()
176 { return Successors.rbegin(); }
177 const_succ_reverse_iterator succ_rbegin() const
178 { return Successors.rbegin(); }
179 succ_reverse_iterator succ_rend()
180 { return Successors.rend(); }
181 const_succ_reverse_iterator succ_rend() const
182 { return Successors.rend(); }
183 unsigned succ_size() const {
184 return (unsigned)Successors.size();
186 bool succ_empty() const { return Successors.empty(); }
188 // LiveIn management methods.
190 /// addLiveIn - Add the specified register as a live in. Note that it
191 /// is an error to add the same register to the same set more than once.
192 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
194 /// removeLiveIn - Remove the specified register from the live in set.
196 void removeLiveIn(unsigned Reg);
198 /// isLiveIn - Return true if the specified register is in the live in set.
200 bool isLiveIn(unsigned Reg) const;
202 // Iteration support for live in sets. These sets are kept in sorted
203 // order by their register number.
204 typedef std::vector<unsigned>::const_iterator livein_iterator;
205 livein_iterator livein_begin() const { return LiveIns.begin(); }
206 livein_iterator livein_end() const { return LiveIns.end(); }
207 bool livein_empty() const { return LiveIns.empty(); }
209 /// getAlignment - Return alignment of the basic block.
211 unsigned getAlignment() const { return Alignment; }
213 /// setAlignment - Set alignment of the basic block.
215 void setAlignment(unsigned Align) { Alignment = Align; }
217 /// isLandingPad - Returns true if the block is a landing pad. That is
218 /// this basic block is entered via an exception handler.
219 bool isLandingPad() const { return IsLandingPad; }
221 /// setIsLandingPad - Indicates the block is a landing pad. That is
222 /// this basic block is entered via an exception handler.
223 void setIsLandingPad() { IsLandingPad = true; }
225 // Code Layout methods.
227 /// moveBefore/moveAfter - move 'this' block before or after the specified
228 /// block. This only moves the block, it does not modify the CFG or adjust
229 /// potential fall-throughs at the end of the block.
230 void moveBefore(MachineBasicBlock *NewAfter);
231 void moveAfter(MachineBasicBlock *NewBefore);
233 /// updateTerminator - Update the terminator instructions in block to account
234 /// for changes to the layout. If the block previously used a fallthrough,
235 /// it may now need a branch, and if it previously used branching it may now
236 /// be able to use a fallthrough.
237 void updateTerminator();
239 // Machine-CFG mutators
241 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
242 /// The Predecessors list of succ is automatically updated.
244 void addSuccessor(MachineBasicBlock *succ);
246 /// removeSuccessor - Remove successor from the successors list of this
247 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
249 void removeSuccessor(MachineBasicBlock *succ);
251 /// removeSuccessor - Remove specified successor from the successors list of
252 /// this MachineBasicBlock. The Predecessors list of succ is automatically
253 /// updated. Return the iterator to the element after the one removed.
255 succ_iterator removeSuccessor(succ_iterator I);
257 /// transferSuccessors - Transfers all the successors from MBB to this
258 /// machine basic block (i.e., copies all the successors fromMBB and
259 /// remove all the successors from fromMBB).
260 void transferSuccessors(MachineBasicBlock *fromMBB);
262 /// isSuccessor - Return true if the specified MBB is a successor of this
264 bool isSuccessor(const MachineBasicBlock *MBB) const;
266 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
267 /// immediately after this block, such that if this block exits by
268 /// falling through, control will transfer to the specified MBB. Note
269 /// that MBB need not be a successor at all, for example if this block
270 /// ends with an unconditional branch to some other block.
271 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
273 /// canFallThrough - Return true if the block can implicitly transfer
274 /// control to the block after it by falling off the end of it. This should
275 /// return false if it can reach the block after it, but it uses an explicit
276 /// branch to do so (e.g., a table jump). True is a conservative answer.
277 bool canFallThrough();
279 /// getFirstTerminator - returns an iterator to the first terminator
280 /// instruction of this basic block. If a terminator does not exist,
282 iterator getFirstTerminator();
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 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
335 /// any DBG_VALUE instructions. Return UnknownLoc if there is none.
336 DebugLoc findDebugLoc(MachineBasicBlock::iterator &MBBI);
338 // Debugging methods.
340 void print(raw_ostream &OS) const;
342 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
343 /// level, unless they're not in a MachineFunction yet, in which case this
346 int getNumber() const { return Number; }
347 void setNumber(int N) { Number = N; }
349 /// getSymbol - Return the MCSymbol for this basic block.
351 MCSymbol *getSymbol() const;
353 private: // Methods used to maintain doubly linked list of blocks...
354 friend struct ilist_traits<MachineBasicBlock>;
356 // Machine-CFG mutators
358 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
359 /// Don't do this unless you know what you're doing, because it doesn't
360 /// update pred's successors list. Use pred->addSuccessor instead.
362 void addPredecessor(MachineBasicBlock *pred);
364 /// removePredecessor - Remove pred as a predecessor of this
365 /// MachineBasicBlock. Don't do this unless you know what you're
366 /// doing, because it doesn't update pred's successors list. Use
367 /// pred->removeSuccessor instead.
369 void removePredecessor(MachineBasicBlock *pred);
372 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
374 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
376 //===--------------------------------------------------------------------===//
377 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
378 //===--------------------------------------------------------------------===//
380 // Provide specializations of GraphTraits to be able to treat a
381 // MachineFunction as a graph of MachineBasicBlocks...
384 template <> struct GraphTraits<MachineBasicBlock *> {
385 typedef MachineBasicBlock NodeType;
386 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
388 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
389 static inline ChildIteratorType child_begin(NodeType *N) {
390 return N->succ_begin();
392 static inline ChildIteratorType child_end(NodeType *N) {
393 return N->succ_end();
397 template <> struct GraphTraits<const MachineBasicBlock *> {
398 typedef const MachineBasicBlock NodeType;
399 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
401 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
402 static inline ChildIteratorType child_begin(NodeType *N) {
403 return N->succ_begin();
405 static inline ChildIteratorType child_end(NodeType *N) {
406 return N->succ_end();
410 // Provide specializations of GraphTraits to be able to treat a
411 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
412 // in inverse order. Inverse order for a function is considered
413 // to be when traversing the predecessor edges of a MBB
414 // instead of the successor edges.
416 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
417 typedef MachineBasicBlock NodeType;
418 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
419 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
422 static inline ChildIteratorType child_begin(NodeType *N) {
423 return N->pred_begin();
425 static inline ChildIteratorType child_end(NodeType *N) {
426 return N->pred_end();
430 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
431 typedef const MachineBasicBlock NodeType;
432 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
433 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
436 static inline ChildIteratorType child_begin(NodeType *N) {
437 return N->pred_begin();
439 static inline ChildIteratorType child_end(NodeType *N) {
440 return N->pred_end();
444 } // End llvm namespace