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"
24 class MachineFunction;
31 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
33 mutable ilist_half_node<MachineInstr> Sentinel;
35 // this is only set by the MachineBasicBlock owning the LiveList
36 friend class MachineBasicBlock;
37 MachineBasicBlock* Parent;
40 MachineInstr *createSentinel() const {
41 return static_cast<MachineInstr*>(&Sentinel);
43 void destroySentinel(MachineInstr *) const {}
45 MachineInstr *provideInitialHead() const { return createSentinel(); }
46 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
47 static void noteHead(MachineInstr*, MachineInstr*) {}
49 void addNodeToList(MachineInstr* N);
50 void removeNodeFromList(MachineInstr* N);
51 void transferNodesFromList(ilist_traits &SrcTraits,
52 ilist_iterator<MachineInstr> first,
53 ilist_iterator<MachineInstr> last);
54 void deleteNode(MachineInstr *N);
56 void createNode(const MachineInstr &);
59 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
60 typedef ilist<MachineInstr> Instructions;
64 MachineFunction *xParent;
66 /// Predecessors/Successors - Keep track of the predecessor / successor
68 std::vector<MachineBasicBlock *> Predecessors;
69 std::vector<MachineBasicBlock *> Successors;
71 /// LiveIns - Keep track of the physical registers that are livein of
73 std::vector<unsigned> LiveIns;
75 /// Alignment - Alignment of the basic block. Zero if the basic block does
76 /// not need to be aligned.
79 /// IsLandingPad - Indicate that this basic block is entered via an
80 /// exception handler.
83 /// AddressTaken - Indicate that this basic block is potentially the
84 /// target of an indirect branch.
87 // Intrusive list support
88 MachineBasicBlock() {}
90 explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
94 // MachineBasicBlocks are allocated and owned by MachineFunction.
95 friend class MachineFunction;
98 /// getBasicBlock - Return the LLVM basic block that this instance
99 /// corresponded to originally. Note that this may be NULL if this instance
100 /// does not correspond directly to an LLVM basic block.
102 const BasicBlock *getBasicBlock() const { return BB; }
104 /// getName - Return the name of the corresponding LLVM basic block, or
106 StringRef getName() const;
108 /// hasAddressTaken - Test whether this block is potentially the target
109 /// of an indirect branch.
110 bool hasAddressTaken() const { return AddressTaken; }
112 /// setHasAddressTaken - Set this block to reflect that it potentially
113 /// is the target of an indirect branch.
114 void setHasAddressTaken() { AddressTaken = true; }
116 /// getParent - Return the MachineFunction containing this basic block.
118 const MachineFunction *getParent() const { return xParent; }
119 MachineFunction *getParent() { return xParent; }
121 typedef Instructions::iterator iterator;
122 typedef Instructions::const_iterator const_iterator;
123 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
124 typedef std::reverse_iterator<iterator> reverse_iterator;
126 unsigned size() const { return (unsigned)Insts.size(); }
127 bool empty() const { return Insts.empty(); }
129 MachineInstr& front() { return Insts.front(); }
130 MachineInstr& back() { return Insts.back(); }
131 const MachineInstr& front() const { return Insts.front(); }
132 const MachineInstr& back() const { return Insts.back(); }
134 iterator begin() { return Insts.begin(); }
135 const_iterator begin() const { return Insts.begin(); }
136 iterator end() { return Insts.end(); }
137 const_iterator end() const { return Insts.end(); }
138 reverse_iterator rbegin() { return Insts.rbegin(); }
139 const_reverse_iterator rbegin() const { return Insts.rbegin(); }
140 reverse_iterator rend () { return Insts.rend(); }
141 const_reverse_iterator rend () const { return Insts.rend(); }
143 // Machine-CFG iterators
144 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
145 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
146 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
147 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
148 typedef std::vector<MachineBasicBlock *>::reverse_iterator
149 pred_reverse_iterator;
150 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
151 const_pred_reverse_iterator;
152 typedef std::vector<MachineBasicBlock *>::reverse_iterator
153 succ_reverse_iterator;
154 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
155 const_succ_reverse_iterator;
157 pred_iterator pred_begin() { return Predecessors.begin(); }
158 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
159 pred_iterator pred_end() { return Predecessors.end(); }
160 const_pred_iterator pred_end() const { return Predecessors.end(); }
161 pred_reverse_iterator pred_rbegin()
162 { return Predecessors.rbegin();}
163 const_pred_reverse_iterator pred_rbegin() const
164 { return Predecessors.rbegin();}
165 pred_reverse_iterator pred_rend()
166 { return Predecessors.rend(); }
167 const_pred_reverse_iterator pred_rend() const
168 { return Predecessors.rend(); }
169 unsigned pred_size() const {
170 return (unsigned)Predecessors.size();
172 bool pred_empty() const { return Predecessors.empty(); }
173 succ_iterator succ_begin() { return Successors.begin(); }
174 const_succ_iterator succ_begin() const { return Successors.begin(); }
175 succ_iterator succ_end() { return Successors.end(); }
176 const_succ_iterator succ_end() const { return Successors.end(); }
177 succ_reverse_iterator succ_rbegin()
178 { return Successors.rbegin(); }
179 const_succ_reverse_iterator succ_rbegin() const
180 { return Successors.rbegin(); }
181 succ_reverse_iterator succ_rend()
182 { return Successors.rend(); }
183 const_succ_reverse_iterator succ_rend() const
184 { return Successors.rend(); }
185 unsigned succ_size() const {
186 return (unsigned)Successors.size();
188 bool succ_empty() const { return Successors.empty(); }
190 // LiveIn management methods.
192 /// addLiveIn - Add the specified register as a live in. Note that it
193 /// is an error to add the same register to the same set more than once.
194 void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
196 /// removeLiveIn - Remove the specified register from the live in set.
198 void removeLiveIn(unsigned Reg);
200 /// isLiveIn - Return true if the specified register is in the live in set.
202 bool isLiveIn(unsigned Reg) const;
204 // Iteration support for live in sets. These sets are kept in sorted
205 // order by their register number.
206 typedef std::vector<unsigned>::const_iterator livein_iterator;
207 livein_iterator livein_begin() const { return LiveIns.begin(); }
208 livein_iterator livein_end() const { return LiveIns.end(); }
209 bool livein_empty() const { return LiveIns.empty(); }
211 /// getAlignment - Return alignment of the basic block.
213 unsigned getAlignment() const { return Alignment; }
215 /// setAlignment - Set alignment of the basic block.
217 void setAlignment(unsigned Align) { Alignment = Align; }
219 /// isLandingPad - Returns true if the block is a landing pad. That is
220 /// this basic block is entered via an exception handler.
221 bool isLandingPad() const { return IsLandingPad; }
223 /// setIsLandingPad - Indicates the block is a landing pad. That is
224 /// this basic block is entered via an exception handler.
225 void setIsLandingPad() { IsLandingPad = true; }
227 /// getLandingPadSuccessor - If this block has a successor that is a landing
228 /// pad, return it. Otherwise return NULL.
229 const MachineBasicBlock *getLandingPadSuccessor() const;
231 // Code Layout methods.
233 /// moveBefore/moveAfter - move 'this' block before or after the specified
234 /// block. This only moves the block, it does not modify the CFG or adjust
235 /// potential fall-throughs at the end of the block.
236 void moveBefore(MachineBasicBlock *NewAfter);
237 void moveAfter(MachineBasicBlock *NewBefore);
239 /// updateTerminator - Update the terminator instructions in block to account
240 /// for changes to the layout. If the block previously used a fallthrough,
241 /// it may now need a branch, and if it previously used branching it may now
242 /// be able to use a fallthrough.
243 void updateTerminator();
245 // Machine-CFG mutators
247 /// addSuccessor - Add succ as a successor of this MachineBasicBlock.
248 /// The Predecessors list of succ is automatically updated.
250 void addSuccessor(MachineBasicBlock *succ);
252 /// removeSuccessor - Remove successor from the successors list of this
253 /// MachineBasicBlock. The Predecessors list of succ is automatically updated.
255 void removeSuccessor(MachineBasicBlock *succ);
257 /// removeSuccessor - Remove specified successor from the successors list of
258 /// this MachineBasicBlock. The Predecessors list of succ is automatically
259 /// updated. Return the iterator to the element after the one removed.
261 succ_iterator removeSuccessor(succ_iterator I);
263 /// transferSuccessors - Transfers all the successors from MBB to this
264 /// machine basic block (i.e., copies all the successors fromMBB and
265 /// remove all the successors from fromMBB).
266 void transferSuccessors(MachineBasicBlock *fromMBB);
268 /// transferSuccessorsAndUpdatePHIs - Transfers all the successors, as
269 /// in transferSuccessors, and update PHI operands in the successor blocks
270 /// which refer to fromMBB to refer to this.
271 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB);
273 /// isSuccessor - Return true if the specified MBB is a successor of this
275 bool isSuccessor(const MachineBasicBlock *MBB) const;
277 /// isLayoutSuccessor - Return true if the specified MBB will be emitted
278 /// immediately after this block, such that if this block exits by
279 /// falling through, control will transfer to the specified MBB. Note
280 /// that MBB need not be a successor at all, for example if this block
281 /// ends with an unconditional branch to some other block.
282 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
284 /// canFallThrough - Return true if the block can implicitly transfer
285 /// control to the block after it by falling off the end of it. This should
286 /// return false if it can reach the block after it, but it uses an explicit
287 /// branch to do so (e.g., a table jump). True is a conservative answer.
288 bool canFallThrough();
290 /// Returns a pointer to the first instructon in this block that is not a
291 /// PHINode instruction. When adding instruction to the beginning of the
292 /// basic block, they should be added before the returned value, not before
293 /// the first instruction, which might be PHI.
294 /// Returns end() is there's no non-PHI instruction.
295 iterator getFirstNonPHI();
297 /// SkipPHIsAndLabels - Return the first instruction in MBB after I that is
298 /// not a PHI or a label. This is the correct point to insert copies at the
299 /// beginning of a basic block.
300 iterator SkipPHIsAndLabels(iterator I);
302 /// getFirstTerminator - returns an iterator to the first terminator
303 /// instruction of this basic block. If a terminator does not exist,
305 iterator getFirstTerminator();
307 /// getLastNonDebugInstr - returns an iterator to the last non-debug
308 /// instruction in the basic block, or end()
309 iterator getLastNonDebugInstr();
311 /// SplitCriticalEdge - Split the critical edge from this block to the
312 /// given successor block, and return the newly created block, or null
313 /// if splitting is not possible.
315 /// This function updates LiveVariables, MachineDominatorTree, and
316 /// MachineLoopInfo, as applicable.
317 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
319 void pop_front() { Insts.pop_front(); }
320 void pop_back() { Insts.pop_back(); }
321 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
322 template<typename IT>
323 void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
324 iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
325 iterator insertAfter(iterator I, MachineInstr *M) {
326 return Insts.insertAfter(I, M);
329 // erase - Remove the specified element or range from the instruction list.
330 // These functions delete any instructions removed.
332 iterator erase(iterator I) { return Insts.erase(I); }
333 iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
334 MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
335 void clear() { Insts.clear(); }
337 /// splice - Take an instruction from MBB 'Other' at the position From,
338 /// and insert it into this MBB right before 'where'.
339 void splice(iterator where, MachineBasicBlock *Other, iterator From) {
340 Insts.splice(where, Other->Insts, From);
343 /// splice - Take a block of instructions from MBB 'Other' in the range [From,
344 /// To), and insert them into this MBB right before 'where'.
345 void splice(iterator where, MachineBasicBlock *Other, iterator From,
347 Insts.splice(where, Other->Insts, From, To);
350 /// removeFromParent - This method unlinks 'this' from the containing
351 /// function, and returns it, but does not delete it.
352 MachineBasicBlock *removeFromParent();
354 /// eraseFromParent - This method unlinks 'this' from the containing
355 /// function and deletes it.
356 void eraseFromParent();
358 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
359 /// 'Old', change the code and CFG so that it branches to 'New' instead.
360 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
362 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
363 /// the CFG to be inserted. If we have proven that MBB can only branch to
364 /// DestA and DestB, remove any other MBB successors from the CFG. DestA and
365 /// DestB can be null. Besides DestA and DestB, retain other edges leading
366 /// to LandingPads (currently there can be only one; we don't check or require
367 /// that here). Note it is possible that DestA and/or DestB are LandingPads.
368 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
369 MachineBasicBlock *DestB,
372 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
373 /// any DBG_VALUE instructions. Return UnknownLoc if there is none.
374 DebugLoc findDebugLoc(MachineBasicBlock::iterator &MBBI);
376 // Debugging methods.
378 void print(raw_ostream &OS, SlotIndexes* = 0) const;
380 /// getNumber - MachineBasicBlocks are uniquely numbered at the function
381 /// level, unless they're not in a MachineFunction yet, in which case this
384 int getNumber() const { return Number; }
385 void setNumber(int N) { Number = N; }
387 /// getSymbol - Return the MCSymbol for this basic block.
389 MCSymbol *getSymbol() const;
391 private: // Methods used to maintain doubly linked list of blocks...
392 friend struct ilist_traits<MachineBasicBlock>;
394 // Machine-CFG mutators
396 /// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
397 /// Don't do this unless you know what you're doing, because it doesn't
398 /// update pred's successors list. Use pred->addSuccessor instead.
400 void addPredecessor(MachineBasicBlock *pred);
402 /// removePredecessor - Remove pred as a predecessor of this
403 /// MachineBasicBlock. Don't do this unless you know what you're
404 /// doing, because it doesn't update pred's successors list. Use
405 /// pred->removeSuccessor instead.
407 void removePredecessor(MachineBasicBlock *pred);
410 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
412 void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
414 //===--------------------------------------------------------------------===//
415 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
416 //===--------------------------------------------------------------------===//
418 // Provide specializations of GraphTraits to be able to treat a
419 // MachineFunction as a graph of MachineBasicBlocks...
422 template <> struct GraphTraits<MachineBasicBlock *> {
423 typedef MachineBasicBlock NodeType;
424 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
426 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
427 static inline ChildIteratorType child_begin(NodeType *N) {
428 return N->succ_begin();
430 static inline ChildIteratorType child_end(NodeType *N) {
431 return N->succ_end();
435 template <> struct GraphTraits<const MachineBasicBlock *> {
436 typedef const MachineBasicBlock NodeType;
437 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
439 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
440 static inline ChildIteratorType child_begin(NodeType *N) {
441 return N->succ_begin();
443 static inline ChildIteratorType child_end(NodeType *N) {
444 return N->succ_end();
448 // Provide specializations of GraphTraits to be able to treat a
449 // MachineFunction as a graph of MachineBasicBlocks... and to walk it
450 // in inverse order. Inverse order for a function is considered
451 // to be when traversing the predecessor edges of a MBB
452 // instead of the successor edges.
454 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
455 typedef MachineBasicBlock NodeType;
456 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
457 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
460 static inline ChildIteratorType child_begin(NodeType *N) {
461 return N->pred_begin();
463 static inline ChildIteratorType child_end(NodeType *N) {
464 return N->pred_end();
468 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
469 typedef const MachineBasicBlock NodeType;
470 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
471 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
474 static inline ChildIteratorType child_begin(NodeType *N) {
475 return N->pred_begin();
477 static inline ChildIteratorType child_end(NodeType *N) {
478 return N->pred_end();
482 } // End llvm namespace