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/ADT/GraphTraits.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/Support/DataTypes.h"
27 class MachineFunction;
33 class MachineBranchProbabilityInfo;
35 // Forward declaration to avoid circular include problem with TargetRegisterInfo
36 typedef unsigned LaneBitmask;
39 struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
41 mutable ilist_half_node<MachineInstr> Sentinel;
43 // this is only set by the MachineBasicBlock owning the LiveList
44 friend class MachineBasicBlock;
45 MachineBasicBlock* Parent;
48 MachineInstr *createSentinel() const {
49 return static_cast<MachineInstr*>(&Sentinel);
51 void destroySentinel(MachineInstr *) const {}
53 MachineInstr *provideInitialHead() const { return createSentinel(); }
54 MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
55 static void noteHead(MachineInstr*, MachineInstr*) {}
57 void addNodeToList(MachineInstr* N);
58 void removeNodeFromList(MachineInstr* N);
59 void transferNodesFromList(ilist_traits &SrcTraits,
60 ilist_iterator<MachineInstr> First,
61 ilist_iterator<MachineInstr> Last);
62 void deleteNode(MachineInstr *N);
64 void createNode(const MachineInstr &);
67 class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
69 /// Pair of physical register and lane mask.
70 /// This is not simply a std::pair typedef because the members should be named
71 /// clearly as they both have an integer type.
72 struct RegisterMaskPair {
77 RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
78 : PhysReg(PhysReg), LaneMask(LaneMask) {}
82 typedef ilist<MachineInstr> Instructions;
86 MachineFunction *xParent;
88 /// Keep track of the predecessor / successor basic blocks.
89 std::vector<MachineBasicBlock *> Predecessors;
90 std::vector<MachineBasicBlock *> Successors;
92 /// Keep track of the weights to the successors. This vector has the same
93 /// order as Successors, or it is empty if we don't use it (disable
95 std::vector<uint32_t> Weights;
96 typedef std::vector<uint32_t>::iterator weight_iterator;
97 typedef std::vector<uint32_t>::const_iterator const_weight_iterator;
99 /// Keep track of the physical registers that are livein of the basicblock.
100 typedef std::vector<RegisterMaskPair> LiveInVector;
101 LiveInVector LiveIns;
103 /// Alignment of the basic block. Zero if the basic block does not need to be
104 /// aligned. The alignment is specified as log2(bytes).
105 unsigned Alignment = 0;
107 /// Indicate that this basic block is entered via an exception handler.
108 bool IsEHPad = false;
110 /// Indicate that this basic block is potentially the target of an indirect
112 bool AddressTaken = false;
114 /// Indicate that this basic block is the entry block of an EH funclet.
115 bool IsEHFuncletEntry = false;
117 /// Indicate that this basic block is the entry block of a cleanup funclet.
118 bool IsCleanupFuncletEntry = false;
120 /// \brief since getSymbol is a relatively heavy-weight operation, the symbol
121 /// is only computed once and is cached.
122 mutable MCSymbol *CachedMCSymbol = nullptr;
124 // Intrusive list support
125 MachineBasicBlock() {}
127 explicit MachineBasicBlock(MachineFunction &MF, const BasicBlock *BB);
129 ~MachineBasicBlock();
131 // MachineBasicBlocks are allocated and owned by MachineFunction.
132 friend class MachineFunction;
135 /// Return the LLVM basic block that this instance corresponded to originally.
136 /// Note that this may be NULL if this instance does not correspond directly
137 /// to an LLVM basic block.
138 const BasicBlock *getBasicBlock() const { return BB; }
140 /// Return the name of the corresponding LLVM basic block, or "(null)".
141 StringRef getName() const;
143 /// Return a formatted string to identify this block and its parent function.
144 std::string getFullName() const;
146 /// Test whether this block is potentially the target of an indirect branch.
147 bool hasAddressTaken() const { return AddressTaken; }
149 /// Set this block to reflect that it potentially is the target of an indirect
151 void setHasAddressTaken() { AddressTaken = true; }
153 /// Return the MachineFunction containing this basic block.
154 const MachineFunction *getParent() const { return xParent; }
155 MachineFunction *getParent() { return xParent; }
157 /// MachineBasicBlock iterator that automatically skips over MIs that are
158 /// inside bundles (i.e. walk top level MIs only).
159 template<typename Ty, typename IterTy>
160 class bundle_iterator
161 : public std::iterator<std::bidirectional_iterator_tag, Ty, ptrdiff_t> {
165 bundle_iterator(IterTy MI) : MII(MI) {}
167 bundle_iterator(Ty &MI) : MII(MI) {
168 assert(!MI.isBundledWithPred() &&
169 "It's not legal to initialize bundle_iterator with a bundled MI");
171 bundle_iterator(Ty *MI) : MII(MI) {
172 assert((!MI || !MI->isBundledWithPred()) &&
173 "It's not legal to initialize bundle_iterator with a bundled MI");
175 // Template allows conversion from const to nonconst.
176 template<class OtherTy, class OtherIterTy>
177 bundle_iterator(const bundle_iterator<OtherTy, OtherIterTy> &I)
178 : MII(I.getInstrIterator()) {}
179 bundle_iterator() : MII(nullptr) {}
181 Ty &operator*() const { return *MII; }
182 Ty *operator->() const { return &operator*(); }
184 operator Ty *() const { return MII.getNodePtrUnchecked(); }
186 bool operator==(const bundle_iterator &X) const {
189 bool operator!=(const bundle_iterator &X) const {
190 return !operator==(X);
193 // Increment and decrement operators...
194 bundle_iterator &operator--() { // predecrement - Back up
196 while (MII->isBundledWithPred());
199 bundle_iterator &operator++() { // preincrement - Advance
200 while (MII->isBundledWithSucc())
205 bundle_iterator operator--(int) { // postdecrement operators...
206 bundle_iterator tmp = *this;
210 bundle_iterator operator++(int) { // postincrement operators...
211 bundle_iterator tmp = *this;
216 IterTy getInstrIterator() const {
221 typedef Instructions::iterator instr_iterator;
222 typedef Instructions::const_iterator const_instr_iterator;
223 typedef std::reverse_iterator<instr_iterator> reverse_instr_iterator;
225 std::reverse_iterator<const_instr_iterator> const_reverse_instr_iterator;
228 bundle_iterator<MachineInstr,instr_iterator> iterator;
230 bundle_iterator<const MachineInstr,const_instr_iterator> const_iterator;
231 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
232 typedef std::reverse_iterator<iterator> reverse_iterator;
235 unsigned size() const { return (unsigned)Insts.size(); }
236 bool empty() const { return Insts.empty(); }
238 MachineInstr &instr_front() { return Insts.front(); }
239 MachineInstr &instr_back() { return Insts.back(); }
240 const MachineInstr &instr_front() const { return Insts.front(); }
241 const MachineInstr &instr_back() const { return Insts.back(); }
243 MachineInstr &front() { return Insts.front(); }
244 MachineInstr &back() { return *--end(); }
245 const MachineInstr &front() const { return Insts.front(); }
246 const MachineInstr &back() const { return *--end(); }
248 instr_iterator instr_begin() { return Insts.begin(); }
249 const_instr_iterator instr_begin() const { return Insts.begin(); }
250 instr_iterator instr_end() { return Insts.end(); }
251 const_instr_iterator instr_end() const { return Insts.end(); }
252 reverse_instr_iterator instr_rbegin() { return Insts.rbegin(); }
253 const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
254 reverse_instr_iterator instr_rend () { return Insts.rend(); }
255 const_reverse_instr_iterator instr_rend () const { return Insts.rend(); }
257 iterator begin() { return instr_begin(); }
258 const_iterator begin() const { return instr_begin(); }
259 iterator end () { return instr_end(); }
260 const_iterator end () const { return instr_end(); }
261 reverse_iterator rbegin() { return instr_rbegin(); }
262 const_reverse_iterator rbegin() const { return instr_rbegin(); }
263 reverse_iterator rend () { return instr_rend(); }
264 const_reverse_iterator rend () const { return instr_rend(); }
266 inline iterator_range<iterator> terminators() {
267 return iterator_range<iterator>(getFirstTerminator(), end());
269 inline iterator_range<const_iterator> terminators() const {
270 return iterator_range<const_iterator>(getFirstTerminator(), end());
273 // Machine-CFG iterators
274 typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
275 typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
276 typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
277 typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
278 typedef std::vector<MachineBasicBlock *>::reverse_iterator
279 pred_reverse_iterator;
280 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
281 const_pred_reverse_iterator;
282 typedef std::vector<MachineBasicBlock *>::reverse_iterator
283 succ_reverse_iterator;
284 typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
285 const_succ_reverse_iterator;
286 pred_iterator pred_begin() { return Predecessors.begin(); }
287 const_pred_iterator pred_begin() const { return Predecessors.begin(); }
288 pred_iterator pred_end() { return Predecessors.end(); }
289 const_pred_iterator pred_end() const { return Predecessors.end(); }
290 pred_reverse_iterator pred_rbegin()
291 { return Predecessors.rbegin();}
292 const_pred_reverse_iterator pred_rbegin() const
293 { return Predecessors.rbegin();}
294 pred_reverse_iterator pred_rend()
295 { return Predecessors.rend(); }
296 const_pred_reverse_iterator pred_rend() const
297 { return Predecessors.rend(); }
298 unsigned pred_size() const {
299 return (unsigned)Predecessors.size();
301 bool pred_empty() const { return Predecessors.empty(); }
302 succ_iterator succ_begin() { return Successors.begin(); }
303 const_succ_iterator succ_begin() const { return Successors.begin(); }
304 succ_iterator succ_end() { return Successors.end(); }
305 const_succ_iterator succ_end() const { return Successors.end(); }
306 succ_reverse_iterator succ_rbegin()
307 { return Successors.rbegin(); }
308 const_succ_reverse_iterator succ_rbegin() const
309 { return Successors.rbegin(); }
310 succ_reverse_iterator succ_rend()
311 { return Successors.rend(); }
312 const_succ_reverse_iterator succ_rend() const
313 { return Successors.rend(); }
314 unsigned succ_size() const {
315 return (unsigned)Successors.size();
317 bool succ_empty() const { return Successors.empty(); }
319 inline iterator_range<pred_iterator> predecessors() {
320 return iterator_range<pred_iterator>(pred_begin(), pred_end());
322 inline iterator_range<const_pred_iterator> predecessors() const {
323 return iterator_range<const_pred_iterator>(pred_begin(), pred_end());
325 inline iterator_range<succ_iterator> successors() {
326 return iterator_range<succ_iterator>(succ_begin(), succ_end());
328 inline iterator_range<const_succ_iterator> successors() const {
329 return iterator_range<const_succ_iterator>(succ_begin(), succ_end());
332 // LiveIn management methods.
334 /// Adds the specified register as a live in. Note that it is an error to add
335 /// the same register to the same set more than once unless the intention is
336 /// to call sortUniqueLiveIns after all registers are added.
337 void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask = ~0u) {
338 LiveIns.push_back(RegisterMaskPair(PhysReg, LaneMask));
340 void addLiveIn(const RegisterMaskPair &RegMaskPair) {
341 LiveIns.push_back(RegMaskPair);
344 /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
345 /// this than repeatedly calling isLiveIn before calling addLiveIn for every
346 /// LiveIn insertion.
347 void sortUniqueLiveIns();
349 /// Add PhysReg as live in to this block, and ensure that there is a copy of
350 /// PhysReg to a virtual register of class RC. Return the virtual register
351 /// that is a copy of the live in PhysReg.
352 unsigned addLiveIn(MCPhysReg PhysReg, const TargetRegisterClass *RC);
354 /// Remove the specified register from the live in set.
355 void removeLiveIn(MCPhysReg Reg, LaneBitmask LaneMask = ~0u);
357 /// Return true if the specified register is in the live in set.
358 bool isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask = ~0u) const;
360 // Iteration support for live in sets. These sets are kept in sorted
361 // order by their register number.
362 typedef LiveInVector::const_iterator livein_iterator;
363 livein_iterator livein_begin() const { return LiveIns.begin(); }
364 livein_iterator livein_end() const { return LiveIns.end(); }
365 bool livein_empty() const { return LiveIns.empty(); }
366 iterator_range<livein_iterator> liveins() const {
367 return make_range(livein_begin(), livein_end());
370 /// Return alignment of the basic block. The alignment is specified as
372 unsigned getAlignment() const { return Alignment; }
374 /// Set alignment of the basic block. The alignment is specified as
376 void setAlignment(unsigned Align) { Alignment = Align; }
378 /// Returns true if the block is a landing pad. That is this basic block is
379 /// entered via an exception handler.
380 bool isEHPad() const { return IsEHPad; }
382 /// Indicates the block is a landing pad. That is this basic block is entered
383 /// via an exception handler.
384 void setIsEHPad(bool V = true) { IsEHPad = V; }
386 /// If this block has a successor that is a landing pad, return it. Otherwise
388 const MachineBasicBlock *getLandingPadSuccessor() const;
390 bool hasEHPadSuccessor() const;
392 /// Returns true if this is the entry block of an EH funclet.
393 bool isEHFuncletEntry() const { return IsEHFuncletEntry; }
395 /// Indicates if this is the entry block of an EH funclet.
396 void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; }
398 /// Returns true if this is the entry block of a cleanup funclet.
399 bool isCleanupFuncletEntry() const { return IsCleanupFuncletEntry; }
401 /// Indicates if this is the entry block of a cleanup funclet.
402 void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; }
404 // Code Layout methods.
406 /// Move 'this' block before or after the specified block. This only moves
407 /// the block, it does not modify the CFG or adjust potential fall-throughs at
408 /// the end of the block.
409 void moveBefore(MachineBasicBlock *NewAfter);
410 void moveAfter(MachineBasicBlock *NewBefore);
412 /// Update the terminator instructions in block to account for changes to the
413 /// layout. If the block previously used a fallthrough, it may now need a
414 /// branch, and if it previously used branching it may now be able to use a
416 void updateTerminator();
418 // Machine-CFG mutators
420 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
421 /// of Succ is automatically updated. WEIGHT parameter is stored in Weights
422 /// list and it may be used by MachineBranchProbabilityInfo analysis to
423 /// calculate branch probability.
425 /// Note that duplicate Machine CFG edges are not allowed.
426 void addSuccessor(MachineBasicBlock *Succ, uint32_t Weight = 0);
428 /// Add Succ as a successor of this MachineBasicBlock. The Predecessors list
429 /// of Succ is automatically updated. The weight is not provided because BPI
430 /// is not available (e.g. -O0 is used), in which case edge weights won't be
431 /// used. Using this interface can save some space.
432 void addSuccessorWithoutWeight(MachineBasicBlock *Succ);
434 /// Set successor weight of a given iterator.
435 void setSuccWeight(succ_iterator I, uint32_t Weight);
437 /// Remove successor from the successors list of this MachineBasicBlock. The
438 /// Predecessors list of Succ is automatically updated.
439 void removeSuccessor(MachineBasicBlock *Succ);
441 /// Remove specified successor from the successors list of this
442 /// MachineBasicBlock. The Predecessors list of Succ is automatically updated.
443 /// Return the iterator to the element after the one removed.
444 succ_iterator removeSuccessor(succ_iterator I);
446 /// Replace successor OLD with NEW and update weight info.
447 void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
449 /// Transfers all the successors from MBB to this machine basic block (i.e.,
450 /// copies all the successors FromMBB and remove all the successors from
452 void transferSuccessors(MachineBasicBlock *FromMBB);
454 /// Transfers all the successors, as in transferSuccessors, and update PHI
455 /// operands in the successor blocks which refer to FromMBB to refer to this.
456 void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB);
458 /// Return true if any of the successors have weights attached to them.
459 bool hasSuccessorWeights() const { return !Weights.empty(); }
461 /// Return true if the specified MBB is a predecessor of this block.
462 bool isPredecessor(const MachineBasicBlock *MBB) const;
464 /// Return true if the specified MBB is a successor of this block.
465 bool isSuccessor(const MachineBasicBlock *MBB) const;
467 /// Return true if the specified MBB will be emitted immediately after this
468 /// block, such that if this block exits by falling through, control will
469 /// transfer to the specified MBB. Note that MBB need not be a successor at
470 /// all, for example if this block ends with an unconditional branch to some
472 bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
474 /// Return true if the block can implicitly transfer control to the block
475 /// after it by falling off the end of it. This should return false if it can
476 /// reach the block after it, but it uses an explicit branch to do so (e.g., a
477 /// table jump). True is a conservative answer.
478 bool canFallThrough();
480 /// Returns a pointer to the first instruction in this block that is not a
481 /// PHINode instruction. When adding instructions to the beginning of the
482 /// basic block, they should be added before the returned value, not before
483 /// the first instruction, which might be PHI.
484 /// Returns end() is there's no non-PHI instruction.
485 iterator getFirstNonPHI();
487 /// Return the first instruction in MBB after I that is not a PHI or a label.
488 /// This is the correct point to insert copies at the beginning of a basic
490 iterator SkipPHIsAndLabels(iterator I);
492 /// Returns an iterator to the first terminator instruction of this basic
493 /// block. If a terminator does not exist, it returns end().
494 iterator getFirstTerminator();
495 const_iterator getFirstTerminator() const {
496 return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
499 /// Same getFirstTerminator but it ignores bundles and return an
500 /// instr_iterator instead.
501 instr_iterator getFirstInstrTerminator();
503 /// Returns an iterator to the first non-debug instruction in the basic block,
505 iterator getFirstNonDebugInstr();
506 const_iterator getFirstNonDebugInstr() const {
507 return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr();
510 /// Returns an iterator to the last non-debug instruction in the basic block,
512 iterator getLastNonDebugInstr();
513 const_iterator getLastNonDebugInstr() const {
514 return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr();
517 /// Convenience function that returns true if the block has no successors and
518 /// contains a return instruction.
519 bool isReturnBlock() const {
520 return !empty() && back().isReturn();
523 /// Split the critical edge from this block to the given successor block, and
524 /// return the newly created block, or null if splitting is not possible.
526 /// This function updates LiveVariables, MachineDominatorTree, and
527 /// MachineLoopInfo, as applicable.
528 MachineBasicBlock *SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P);
530 void pop_front() { Insts.pop_front(); }
531 void pop_back() { Insts.pop_back(); }
532 void push_back(MachineInstr *MI) { Insts.push_back(MI); }
534 /// Insert MI into the instruction list before I, possibly inside a bundle.
536 /// If the insertion point is inside a bundle, MI will be added to the bundle,
537 /// otherwise MI will not be added to any bundle. That means this function
538 /// alone can't be used to prepend or append instructions to bundles. See
539 /// MIBundleBuilder::insert() for a more reliable way of doing that.
540 instr_iterator insert(instr_iterator I, MachineInstr *M);
542 /// Insert a range of instructions into the instruction list before I.
543 template<typename IT>
544 void insert(iterator I, IT S, IT E) {
545 assert((I == end() || I->getParent() == this) &&
546 "iterator points outside of basic block");
547 Insts.insert(I.getInstrIterator(), S, E);
550 /// Insert MI into the instruction list before I.
551 iterator insert(iterator I, MachineInstr *MI) {
552 assert((I == end() || I->getParent() == this) &&
553 "iterator points outside of basic block");
554 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
555 "Cannot insert instruction with bundle flags");
556 return Insts.insert(I.getInstrIterator(), MI);
559 /// Insert MI into the instruction list after I.
560 iterator insertAfter(iterator I, MachineInstr *MI) {
561 assert((I == end() || I->getParent() == this) &&
562 "iterator points outside of basic block");
563 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
564 "Cannot insert instruction with bundle flags");
565 return Insts.insertAfter(I.getInstrIterator(), MI);
568 /// Remove an instruction from the instruction list and delete it.
570 /// If the instruction is part of a bundle, the other instructions in the
571 /// bundle will still be bundled after removing the single instruction.
572 instr_iterator erase(instr_iterator I);
574 /// Remove an instruction from the instruction list and delete it.
576 /// If the instruction is part of a bundle, the other instructions in the
577 /// bundle will still be bundled after removing the single instruction.
578 instr_iterator erase_instr(MachineInstr *I) {
579 return erase(instr_iterator(I));
582 /// Remove a range of instructions from the instruction list and delete them.
583 iterator erase(iterator I, iterator E) {
584 return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
587 /// Remove an instruction or bundle from the instruction list and delete it.
589 /// If I points to a bundle of instructions, they are all erased.
590 iterator erase(iterator I) {
591 return erase(I, std::next(I));
594 /// Remove an instruction from the instruction list and delete it.
596 /// If I is the head of a bundle of instructions, the whole bundle will be
598 iterator erase(MachineInstr *I) {
599 return erase(iterator(I));
602 /// Remove the unbundled instruction from the instruction list without
605 /// This function can not be used to remove bundled instructions, use
606 /// remove_instr to remove individual instructions from a bundle.
607 MachineInstr *remove(MachineInstr *I) {
608 assert(!I->isBundled() && "Cannot remove bundled instructions");
609 return Insts.remove(instr_iterator(I));
612 /// Remove the possibly bundled instruction from the instruction list
613 /// without deleting it.
615 /// If the instruction is part of a bundle, the other instructions in the
616 /// bundle will still be bundled after removing the single instruction.
617 MachineInstr *remove_instr(MachineInstr *I);
623 /// Take an instruction from MBB 'Other' at the position From, and insert it
624 /// into this MBB right before 'Where'.
626 /// If From points to a bundle of instructions, the whole bundle is moved.
627 void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
628 // The range splice() doesn't allow noop moves, but this one does.
630 splice(Where, Other, From, std::next(From));
633 /// Take a block of instructions from MBB 'Other' in the range [From, To),
634 /// and insert them into this MBB right before 'Where'.
636 /// The instruction at 'Where' must not be included in the range of
637 /// instructions to move.
638 void splice(iterator Where, MachineBasicBlock *Other,
639 iterator From, iterator To) {
640 Insts.splice(Where.getInstrIterator(), Other->Insts,
641 From.getInstrIterator(), To.getInstrIterator());
644 /// This method unlinks 'this' from the containing function, and returns it,
645 /// but does not delete it.
646 MachineBasicBlock *removeFromParent();
648 /// This method unlinks 'this' from the containing function and deletes it.
649 void eraseFromParent();
651 /// Given a machine basic block that branched to 'Old', change the code and
652 /// CFG so that it branches to 'New' instead.
653 void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
655 /// Various pieces of code can cause excess edges in the CFG to be inserted.
656 /// If we have proven that MBB can only branch to DestA and DestB, remove any
657 /// other MBB successors from the CFG. DestA and DestB can be null. Besides
658 /// DestA and DestB, retain other edges leading to LandingPads (currently
659 /// there can be only one; we don't check or require that here). Note it is
660 /// possible that DestA and/or DestB are LandingPads.
661 bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
662 MachineBasicBlock *DestB,
665 /// Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE
666 /// instructions. Return UnknownLoc if there is none.
667 DebugLoc findDebugLoc(instr_iterator MBBI);
668 DebugLoc findDebugLoc(iterator MBBI) {
669 return findDebugLoc(MBBI.getInstrIterator());
672 /// Possible outcome of a register liveness query to computeRegisterLiveness()
673 enum LivenessQueryResult {
674 LQR_Live, ///< Register is known to be live.
675 LQR_OverlappingLive, ///< Register itself is not live, but some overlapping
677 LQR_Dead, ///< Register is known to be dead.
678 LQR_Unknown ///< Register liveness not decidable from local
682 /// Return whether (physical) register \p Reg has been <def>ined and not
683 /// <kill>ed as of just before \p Before.
685 /// Search is localised to a neighborhood of \p Neighborhood instructions
686 /// before (searching for defs or kills) and \p Neighborhood instructions
687 /// after (searching just for defs) \p Before.
689 /// \p Reg must be a physical register.
690 LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
692 const_iterator Before,
693 unsigned Neighborhood=10) const;
695 // Debugging methods.
697 void print(raw_ostream &OS, SlotIndexes* = nullptr) const;
698 void print(raw_ostream &OS, ModuleSlotTracker &MST,
699 SlotIndexes * = nullptr) const;
701 // Printing method used by LoopInfo.
702 void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
704 /// MachineBasicBlocks are uniquely numbered at the function level, unless
705 /// they're not in a MachineFunction yet, in which case this will return -1.
706 int getNumber() const { return Number; }
707 void setNumber(int N) { Number = N; }
709 /// Return the MCSymbol for this basic block.
710 MCSymbol *getSymbol() const;
714 /// Return weight iterator corresponding to the I successor iterator.
715 weight_iterator getWeightIterator(succ_iterator I);
716 const_weight_iterator getWeightIterator(const_succ_iterator I) const;
718 friend class MachineBranchProbabilityInfo;
719 friend class MIPrinter;
721 /// Return weight of the edge from this block to MBB. This method should NOT
722 /// be called directly, but by using getEdgeWeight method from
723 /// MachineBranchProbabilityInfo class.
724 uint32_t getSuccWeight(const_succ_iterator Succ) const;
727 // Methods used to maintain doubly linked list of blocks...
728 friend struct ilist_traits<MachineBasicBlock>;
730 // Machine-CFG mutators
732 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
733 /// unless you know what you're doing, because it doesn't update Pred's
734 /// successors list. Use Pred->addSuccessor instead.
735 void addPredecessor(MachineBasicBlock *Pred);
737 /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
738 /// unless you know what you're doing, because it doesn't update Pred's
739 /// successors list. Use Pred->removeSuccessor instead.
740 void removePredecessor(MachineBasicBlock *Pred);
743 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
745 // This is useful when building IndexedMaps keyed on basic block pointers.
746 struct MBB2NumberFunctor :
747 public std::unary_function<const MachineBasicBlock*, unsigned> {
748 unsigned operator()(const MachineBasicBlock *MBB) const {
749 return MBB->getNumber();
753 //===--------------------------------------------------------------------===//
754 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
755 //===--------------------------------------------------------------------===//
757 // Provide specializations of GraphTraits to be able to treat a
758 // MachineFunction as a graph of MachineBasicBlocks.
761 template <> struct GraphTraits<MachineBasicBlock *> {
762 typedef MachineBasicBlock NodeType;
763 typedef MachineBasicBlock::succ_iterator ChildIteratorType;
765 static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
766 static inline ChildIteratorType child_begin(NodeType *N) {
767 return N->succ_begin();
769 static inline ChildIteratorType child_end(NodeType *N) {
770 return N->succ_end();
774 template <> struct GraphTraits<const MachineBasicBlock *> {
775 typedef const MachineBasicBlock NodeType;
776 typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
778 static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
779 static inline ChildIteratorType child_begin(NodeType *N) {
780 return N->succ_begin();
782 static inline ChildIteratorType child_end(NodeType *N) {
783 return N->succ_end();
787 // Provide specializations of GraphTraits to be able to treat a
788 // MachineFunction as a graph of MachineBasicBlocks and to walk it
789 // in inverse order. Inverse order for a function is considered
790 // to be when traversing the predecessor edges of a MBB
791 // instead of the successor edges.
793 template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
794 typedef MachineBasicBlock NodeType;
795 typedef MachineBasicBlock::pred_iterator ChildIteratorType;
796 static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
799 static inline ChildIteratorType child_begin(NodeType *N) {
800 return N->pred_begin();
802 static inline ChildIteratorType child_end(NodeType *N) {
803 return N->pred_end();
807 template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
808 typedef const MachineBasicBlock NodeType;
809 typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
810 static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
813 static inline ChildIteratorType child_begin(NodeType *N) {
814 return N->pred_begin();
816 static inline ChildIteratorType child_end(NodeType *N) {
817 return N->pred_end();
823 /// MachineInstrSpan provides an interface to get an iteration range
824 /// containing the instruction it was initialized with, along with all
825 /// those instructions inserted prior to or following that instruction
826 /// at some point after the MachineInstrSpan is constructed.
827 class MachineInstrSpan {
828 MachineBasicBlock &MBB;
829 MachineBasicBlock::iterator I, B, E;
831 MachineInstrSpan(MachineBasicBlock::iterator I)
832 : MBB(*I->getParent()),
834 B(I == MBB.begin() ? MBB.end() : std::prev(I)),
837 MachineBasicBlock::iterator begin() {
838 return B == MBB.end() ? MBB.begin() : std::next(B);
840 MachineBasicBlock::iterator end() { return E; }
841 bool empty() { return begin() == end(); }
843 MachineBasicBlock::iterator getInitial() { return I; }
846 } // End llvm namespace