Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:30

0001 //===- llvm/CodeGen/MachineBasicBlock.h -------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Collect the sequence of machine instructions for a basic block.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
0014 #define LLVM_CODEGEN_MACHINEBASICBLOCK_H
0015 
0016 #include "llvm/ADT/DenseMapInfo.h"
0017 #include "llvm/ADT/GraphTraits.h"
0018 #include "llvm/ADT/SparseBitVector.h"
0019 #include "llvm/ADT/ilist.h"
0020 #include "llvm/ADT/iterator_range.h"
0021 #include "llvm/CodeGen/MachineInstr.h"
0022 #include "llvm/CodeGen/MachineInstrBundleIterator.h"
0023 #include "llvm/IR/DebugLoc.h"
0024 #include "llvm/MC/LaneBitmask.h"
0025 #include "llvm/Support/BranchProbability.h"
0026 #include <cassert>
0027 #include <cstdint>
0028 #include <iterator>
0029 #include <string>
0030 #include <vector>
0031 
0032 namespace llvm {
0033 
0034 class BasicBlock;
0035 class MachineDomTreeUpdater;
0036 class MachineFunction;
0037 class MCSymbol;
0038 class ModuleSlotTracker;
0039 class Pass;
0040 class Printable;
0041 class SlotIndexes;
0042 class StringRef;
0043 class raw_ostream;
0044 class LiveIntervals;
0045 class TargetRegisterClass;
0046 class TargetRegisterInfo;
0047 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
0048 using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
0049 
0050 // This structure uniquely identifies a basic block section.
0051 // Possible values are
0052 //  {Type: Default, Number: (unsigned)} (These are regular section IDs)
0053 //  {Type: Exception, Number: 0}  (ExceptionSectionID)
0054 //  {Type: Cold, Number: 0}  (ColdSectionID)
0055 struct MBBSectionID {
0056   enum SectionType {
0057     Default = 0, // Regular section (these sections are distinguished by the
0058                  // Number field).
0059     Exception,   // Special section type for exception handling blocks
0060     Cold,        // Special section type for cold blocks
0061   } Type;
0062   unsigned Number;
0063 
0064   MBBSectionID(unsigned N) : Type(Default), Number(N) {}
0065 
0066   // Special unique sections for cold and exception blocks.
0067   const static MBBSectionID ColdSectionID;
0068   const static MBBSectionID ExceptionSectionID;
0069 
0070   bool operator==(const MBBSectionID &Other) const {
0071     return Type == Other.Type && Number == Other.Number;
0072   }
0073 
0074   bool operator!=(const MBBSectionID &Other) const { return !(*this == Other); }
0075 
0076 private:
0077   // This is only used to construct the special cold and exception sections.
0078   MBBSectionID(SectionType T) : Type(T), Number(0) {}
0079 };
0080 
0081 template <> struct DenseMapInfo<MBBSectionID> {
0082   using TypeInfo = DenseMapInfo<MBBSectionID::SectionType>;
0083   using NumberInfo = DenseMapInfo<unsigned>;
0084 
0085   static inline MBBSectionID getEmptyKey() {
0086     return MBBSectionID(NumberInfo::getEmptyKey());
0087   }
0088   static inline MBBSectionID getTombstoneKey() {
0089     return MBBSectionID(NumberInfo::getTombstoneKey());
0090   }
0091   static unsigned getHashValue(const MBBSectionID &SecID) {
0092     return detail::combineHashValue(TypeInfo::getHashValue(SecID.Type),
0093                                     NumberInfo::getHashValue(SecID.Number));
0094   }
0095   static bool isEqual(const MBBSectionID &LHS, const MBBSectionID &RHS) {
0096     return LHS == RHS;
0097   }
0098 };
0099 
0100 // This structure represents the information for a basic block pertaining to
0101 // the basic block sections profile.
0102 struct UniqueBBID {
0103   unsigned BaseID;
0104   unsigned CloneID;
0105 };
0106 
0107 template <> struct ilist_traits<MachineInstr> {
0108 private:
0109   friend class MachineBasicBlock; // Set by the owning MachineBasicBlock.
0110 
0111   MachineBasicBlock *Parent;
0112 
0113   using instr_iterator =
0114       simple_ilist<MachineInstr, ilist_sentinel_tracking<true>>::iterator;
0115 
0116 public:
0117   void addNodeToList(MachineInstr *N);
0118   void removeNodeFromList(MachineInstr *N);
0119   void transferNodesFromList(ilist_traits &FromList, instr_iterator First,
0120                              instr_iterator Last);
0121   void deleteNode(MachineInstr *MI);
0122 };
0123 
0124 class MachineBasicBlock
0125     : public ilist_node_with_parent<MachineBasicBlock, MachineFunction> {
0126 public:
0127   /// Pair of physical register and lane mask.
0128   /// This is not simply a std::pair typedef because the members should be named
0129   /// clearly as they both have an integer type.
0130   struct RegisterMaskPair {
0131   public:
0132     MCRegister PhysReg;
0133     LaneBitmask LaneMask;
0134 
0135     RegisterMaskPair(MCPhysReg PhysReg, LaneBitmask LaneMask)
0136         : PhysReg(PhysReg), LaneMask(LaneMask) {}
0137 
0138     bool operator==(const RegisterMaskPair &other) const {
0139       return PhysReg == other.PhysReg && LaneMask == other.LaneMask;
0140     }
0141   };
0142 
0143 private:
0144   using Instructions = ilist<MachineInstr, ilist_sentinel_tracking<true>>;
0145 
0146   const BasicBlock *BB;
0147   int Number;
0148 
0149   /// The call frame size on entry to this basic block due to call frame setup
0150   /// instructions in a predecessor. This is usually zero, unless basic blocks
0151   /// are split in the middle of a call sequence.
0152   ///
0153   /// This information is only maintained until PrologEpilogInserter eliminates
0154   /// call frame pseudos.
0155   unsigned CallFrameSize = 0;
0156 
0157   MachineFunction *xParent;
0158   Instructions Insts;
0159 
0160   /// Keep track of the predecessor / successor basic blocks.
0161   SmallVector<MachineBasicBlock *, 4> Predecessors;
0162   SmallVector<MachineBasicBlock *, 2> Successors;
0163 
0164   /// Keep track of the probabilities to the successors. This vector has the
0165   /// same order as Successors, or it is empty if we don't use it (disable
0166   /// optimization).
0167   std::vector<BranchProbability> Probs;
0168   using probability_iterator = std::vector<BranchProbability>::iterator;
0169   using const_probability_iterator =
0170       std::vector<BranchProbability>::const_iterator;
0171 
0172   std::optional<uint64_t> IrrLoopHeaderWeight;
0173 
0174   /// Keep track of the physical registers that are livein of the basicblock.
0175   using LiveInVector = std::vector<RegisterMaskPair>;
0176   LiveInVector LiveIns;
0177 
0178   /// Alignment of the basic block. One if the basic block does not need to be
0179   /// aligned.
0180   Align Alignment;
0181   /// Maximum amount of bytes that can be added to align the basic block. If the
0182   /// alignment cannot be reached in this many bytes, no bytes are emitted.
0183   /// Zero to represent no maximum.
0184   unsigned MaxBytesForAlignment = 0;
0185 
0186   /// Indicate that this basic block is entered via an exception handler.
0187   bool IsEHPad = false;
0188 
0189   /// Indicate that this MachineBasicBlock is referenced somewhere other than
0190   /// as predecessor/successor, a terminator MachineInstr, or a jump table.
0191   bool MachineBlockAddressTaken = false;
0192 
0193   /// If this MachineBasicBlock corresponds to an IR-level "blockaddress"
0194   /// constant, this contains a pointer to that block.
0195   BasicBlock *AddressTakenIRBlock = nullptr;
0196 
0197   /// Indicate that this basic block needs its symbol be emitted regardless of
0198   /// whether the flow just falls-through to it.
0199   bool LabelMustBeEmitted = false;
0200 
0201   /// Indicate that this basic block is the entry block of an EH scope, i.e.,
0202   /// the block that used to have a catchpad or cleanuppad instruction in the
0203   /// LLVM IR.
0204   bool IsEHScopeEntry = false;
0205 
0206   /// Indicates if this is a target block of a catchret.
0207   bool IsEHCatchretTarget = false;
0208 
0209   /// Indicate that this basic block is the entry block of an EH funclet.
0210   bool IsEHFuncletEntry = false;
0211 
0212   /// Indicate that this basic block is the entry block of a cleanup funclet.
0213   bool IsCleanupFuncletEntry = false;
0214 
0215   /// Fixed unique ID assigned to this basic block upon creation. Used with
0216   /// basic block sections and basic block labels.
0217   std::optional<UniqueBBID> BBID;
0218 
0219   /// With basic block sections, this stores the Section ID of the basic block.
0220   MBBSectionID SectionID{0};
0221 
0222   // Indicate that this basic block begins a section.
0223   bool IsBeginSection = false;
0224 
0225   // Indicate that this basic block ends a section.
0226   bool IsEndSection = false;
0227 
0228   /// Indicate that this basic block is the indirect dest of an INLINEASM_BR.
0229   bool IsInlineAsmBrIndirectTarget = false;
0230 
0231   /// since getSymbol is a relatively heavy-weight operation, the symbol
0232   /// is only computed once and is cached.
0233   mutable MCSymbol *CachedMCSymbol = nullptr;
0234 
0235   /// Cached MCSymbol for this block (used if IsEHCatchRetTarget).
0236   mutable MCSymbol *CachedEHCatchretMCSymbol = nullptr;
0237 
0238   /// Marks the end of the basic block. Used during basic block sections to
0239   /// calculate the size of the basic block, or the BB section ending with it.
0240   mutable MCSymbol *CachedEndMCSymbol = nullptr;
0241 
0242   // Intrusive list support
0243   MachineBasicBlock() = default;
0244 
0245   explicit MachineBasicBlock(MachineFunction &MF, const BasicBlock *BB);
0246 
0247   ~MachineBasicBlock();
0248 
0249   // MachineBasicBlocks are allocated and owned by MachineFunction.
0250   friend class MachineFunction;
0251 
0252 public:
0253   /// Return the LLVM basic block that this instance corresponded to originally.
0254   /// Note that this may be NULL if this instance does not correspond directly
0255   /// to an LLVM basic block.
0256   const BasicBlock *getBasicBlock() const { return BB; }
0257 
0258   /// Remove the reference to the underlying IR BasicBlock. This is for
0259   /// reduction tools and should generally not be used.
0260   void clearBasicBlock() {
0261     BB = nullptr;
0262   }
0263 
0264   /// Check if there is a name of corresponding LLVM basic block.
0265   bool hasName() const;
0266 
0267   /// Return the name of the corresponding LLVM basic block, or an empty string.
0268   StringRef getName() const;
0269 
0270   /// Return a formatted string to identify this block and its parent function.
0271   std::string getFullName() const;
0272 
0273   /// Test whether this block is used as something other than the target
0274   /// of a terminator, exception-handling target, or jump table. This is
0275   /// either the result of an IR-level "blockaddress", or some form
0276   /// of target-specific branch lowering.
0277   bool hasAddressTaken() const {
0278     return MachineBlockAddressTaken || AddressTakenIRBlock;
0279   }
0280 
0281   /// Test whether this block is used as something other than the target of a
0282   /// terminator, exception-handling target, jump table, or IR blockaddress.
0283   /// For example, its address might be loaded into a register, or
0284   /// stored in some branch table that isn't part of MachineJumpTableInfo.
0285   bool isMachineBlockAddressTaken() const { return MachineBlockAddressTaken; }
0286 
0287   /// Test whether this block is the target of an IR BlockAddress.  (There can
0288   /// more than one MBB associated with an IR BB where the address is taken.)
0289   bool isIRBlockAddressTaken() const { return AddressTakenIRBlock; }
0290 
0291   /// Retrieves the BasicBlock which corresponds to this MachineBasicBlock.
0292   BasicBlock *getAddressTakenIRBlock() const { return AddressTakenIRBlock; }
0293 
0294   /// Set this block to indicate that its address is used as something other
0295   /// than the target of a terminator, exception-handling target, jump table,
0296   /// or IR-level "blockaddress".
0297   void setMachineBlockAddressTaken() { MachineBlockAddressTaken = true; }
0298 
0299   /// Set this block to reflect that it corresponds to an IR-level basic block
0300   /// with a BlockAddress.
0301   void setAddressTakenIRBlock(BasicBlock *BB) { AddressTakenIRBlock = BB; }
0302 
0303   /// Test whether this block must have its label emitted.
0304   bool hasLabelMustBeEmitted() const { return LabelMustBeEmitted; }
0305 
0306   /// Set this block to reflect that, regardless how we flow to it, we need
0307   /// its label be emitted.
0308   void setLabelMustBeEmitted() { LabelMustBeEmitted = true; }
0309 
0310   /// Return the MachineFunction containing this basic block.
0311   const MachineFunction *getParent() const { return xParent; }
0312   MachineFunction *getParent() { return xParent; }
0313 
0314   /// Returns true if the original IR terminator is an `indirectbr`. This
0315   /// typically corresponds to a `goto` in C, rather than jump tables.
0316   bool terminatorIsComputedGoto() const {
0317     return back().isIndirectBranch() &&
0318            llvm::all_of(successors(), [](const MachineBasicBlock *Succ) {
0319              return Succ->isIRBlockAddressTaken();
0320            });
0321   }
0322 
0323   using instr_iterator = Instructions::iterator;
0324   using const_instr_iterator = Instructions::const_iterator;
0325   using reverse_instr_iterator = Instructions::reverse_iterator;
0326   using const_reverse_instr_iterator = Instructions::const_reverse_iterator;
0327 
0328   using iterator = MachineInstrBundleIterator<MachineInstr>;
0329   using const_iterator = MachineInstrBundleIterator<const MachineInstr>;
0330   using reverse_iterator = MachineInstrBundleIterator<MachineInstr, true>;
0331   using const_reverse_iterator =
0332       MachineInstrBundleIterator<const MachineInstr, true>;
0333 
0334   unsigned size() const { return (unsigned)Insts.size(); }
0335   bool sizeWithoutDebugLargerThan(unsigned Limit) const;
0336   bool empty() const { return Insts.empty(); }
0337 
0338   MachineInstr       &instr_front()       { return Insts.front(); }
0339   MachineInstr       &instr_back()        { return Insts.back();  }
0340   const MachineInstr &instr_front() const { return Insts.front(); }
0341   const MachineInstr &instr_back()  const { return Insts.back();  }
0342 
0343   MachineInstr       &front()             { return Insts.front(); }
0344   MachineInstr       &back()              { return *--end();      }
0345   const MachineInstr &front()       const { return Insts.front(); }
0346   const MachineInstr &back()        const { return *--end();      }
0347 
0348   instr_iterator                instr_begin()       { return Insts.begin();  }
0349   const_instr_iterator          instr_begin() const { return Insts.begin();  }
0350   instr_iterator                  instr_end()       { return Insts.end();    }
0351   const_instr_iterator            instr_end() const { return Insts.end();    }
0352   reverse_instr_iterator       instr_rbegin()       { return Insts.rbegin(); }
0353   const_reverse_instr_iterator instr_rbegin() const { return Insts.rbegin(); }
0354   reverse_instr_iterator       instr_rend  ()       { return Insts.rend();   }
0355   const_reverse_instr_iterator instr_rend  () const { return Insts.rend();   }
0356 
0357   using instr_range = iterator_range<instr_iterator>;
0358   using const_instr_range = iterator_range<const_instr_iterator>;
0359   instr_range instrs() { return instr_range(instr_begin(), instr_end()); }
0360   const_instr_range instrs() const {
0361     return const_instr_range(instr_begin(), instr_end());
0362   }
0363 
0364   iterator                begin()       { return instr_begin();  }
0365   const_iterator          begin() const { return instr_begin();  }
0366   iterator                end  ()       { return instr_end();    }
0367   const_iterator          end  () const { return instr_end();    }
0368   reverse_iterator rbegin() {
0369     return reverse_iterator::getAtBundleBegin(instr_rbegin());
0370   }
0371   const_reverse_iterator rbegin() const {
0372     return const_reverse_iterator::getAtBundleBegin(instr_rbegin());
0373   }
0374   reverse_iterator rend() { return reverse_iterator(instr_rend()); }
0375   const_reverse_iterator rend() const {
0376     return const_reverse_iterator(instr_rend());
0377   }
0378 
0379   /// Support for MachineInstr::getNextNode().
0380   static Instructions MachineBasicBlock::*getSublistAccess(MachineInstr *) {
0381     return &MachineBasicBlock::Insts;
0382   }
0383 
0384   inline iterator_range<iterator> terminators() {
0385     return make_range(getFirstTerminator(), end());
0386   }
0387   inline iterator_range<const_iterator> terminators() const {
0388     return make_range(getFirstTerminator(), end());
0389   }
0390 
0391   /// Returns a range that iterates over the phis in the basic block.
0392   inline iterator_range<iterator> phis() {
0393     return make_range(begin(), getFirstNonPHI());
0394   }
0395   inline iterator_range<const_iterator> phis() const {
0396     return const_cast<MachineBasicBlock *>(this)->phis();
0397   }
0398 
0399   // Machine-CFG iterators
0400   using pred_iterator = SmallVectorImpl<MachineBasicBlock *>::iterator;
0401   using const_pred_iterator =
0402       SmallVectorImpl<MachineBasicBlock *>::const_iterator;
0403   using succ_iterator = SmallVectorImpl<MachineBasicBlock *>::iterator;
0404   using const_succ_iterator =
0405       SmallVectorImpl<MachineBasicBlock *>::const_iterator;
0406   using pred_reverse_iterator =
0407       SmallVectorImpl<MachineBasicBlock *>::reverse_iterator;
0408   using const_pred_reverse_iterator =
0409       SmallVectorImpl<MachineBasicBlock *>::const_reverse_iterator;
0410   using succ_reverse_iterator =
0411       SmallVectorImpl<MachineBasicBlock *>::reverse_iterator;
0412   using const_succ_reverse_iterator =
0413       SmallVectorImpl<MachineBasicBlock *>::const_reverse_iterator;
0414   pred_iterator        pred_begin()       { return Predecessors.begin(); }
0415   const_pred_iterator  pred_begin() const { return Predecessors.begin(); }
0416   pred_iterator        pred_end()         { return Predecessors.end();   }
0417   const_pred_iterator  pred_end()   const { return Predecessors.end();   }
0418   pred_reverse_iterator        pred_rbegin()
0419                                           { return Predecessors.rbegin();}
0420   const_pred_reverse_iterator  pred_rbegin() const
0421                                           { return Predecessors.rbegin();}
0422   pred_reverse_iterator        pred_rend()
0423                                           { return Predecessors.rend();  }
0424   const_pred_reverse_iterator  pred_rend()   const
0425                                           { return Predecessors.rend();  }
0426   unsigned             pred_size()  const {
0427     return (unsigned)Predecessors.size();
0428   }
0429   bool                 pred_empty() const { return Predecessors.empty(); }
0430   succ_iterator        succ_begin()       { return Successors.begin();   }
0431   const_succ_iterator  succ_begin() const { return Successors.begin();   }
0432   succ_iterator        succ_end()         { return Successors.end();     }
0433   const_succ_iterator  succ_end()   const { return Successors.end();     }
0434   succ_reverse_iterator        succ_rbegin()
0435                                           { return Successors.rbegin();  }
0436   const_succ_reverse_iterator  succ_rbegin() const
0437                                           { return Successors.rbegin();  }
0438   succ_reverse_iterator        succ_rend()
0439                                           { return Successors.rend();    }
0440   const_succ_reverse_iterator  succ_rend()   const
0441                                           { return Successors.rend();    }
0442   unsigned             succ_size()  const {
0443     return (unsigned)Successors.size();
0444   }
0445   bool                 succ_empty() const { return Successors.empty();   }
0446 
0447   inline iterator_range<pred_iterator> predecessors() {
0448     return make_range(pred_begin(), pred_end());
0449   }
0450   inline iterator_range<const_pred_iterator> predecessors() const {
0451     return make_range(pred_begin(), pred_end());
0452   }
0453   inline iterator_range<succ_iterator> successors() {
0454     return make_range(succ_begin(), succ_end());
0455   }
0456   inline iterator_range<const_succ_iterator> successors() const {
0457     return make_range(succ_begin(), succ_end());
0458   }
0459 
0460   // LiveIn management methods.
0461 
0462   /// Adds the specified register as a live in. Note that it is an error to add
0463   /// the same register to the same set more than once unless the intention is
0464   /// to call sortUniqueLiveIns after all registers are added.
0465   void addLiveIn(MCRegister PhysReg,
0466                  LaneBitmask LaneMask = LaneBitmask::getAll()) {
0467     LiveIns.push_back(RegisterMaskPair(PhysReg, LaneMask));
0468   }
0469   void addLiveIn(const RegisterMaskPair &RegMaskPair) {
0470     LiveIns.push_back(RegMaskPair);
0471   }
0472 
0473   /// Sorts and uniques the LiveIns vector. It can be significantly faster to do
0474   /// this than repeatedly calling isLiveIn before calling addLiveIn for every
0475   /// LiveIn insertion.
0476   void sortUniqueLiveIns();
0477 
0478   /// Clear live in list.
0479   void clearLiveIns();
0480 
0481   /// Clear the live in list, and return the removed live in's in \p OldLiveIns.
0482   /// Requires that the vector \p OldLiveIns is empty.
0483   void clearLiveIns(std::vector<RegisterMaskPair> &OldLiveIns);
0484 
0485   /// Add PhysReg as live in to this block, and ensure that there is a copy of
0486   /// PhysReg to a virtual register of class RC. Return the virtual register
0487   /// that is a copy of the live in PhysReg.
0488   Register addLiveIn(MCRegister PhysReg, const TargetRegisterClass *RC);
0489 
0490   /// Remove the specified register from the live in set.
0491   void removeLiveIn(MCRegister Reg,
0492                     LaneBitmask LaneMask = LaneBitmask::getAll());
0493 
0494   /// Return true if the specified register is in the live in set.
0495   bool isLiveIn(MCRegister Reg,
0496                 LaneBitmask LaneMask = LaneBitmask::getAll()) const;
0497 
0498   // Iteration support for live in sets.  These sets are kept in sorted
0499   // order by their register number.
0500   using livein_iterator = LiveInVector::const_iterator;
0501 
0502   /// Unlike livein_begin, this method does not check that the liveness
0503   /// information is accurate. Still for debug purposes it may be useful
0504   /// to have iterators that won't assert if the liveness information
0505   /// is not current.
0506   livein_iterator livein_begin_dbg() const { return LiveIns.begin(); }
0507   iterator_range<livein_iterator> liveins_dbg() const {
0508     return make_range(livein_begin_dbg(), livein_end());
0509   }
0510 
0511   livein_iterator livein_begin() const;
0512   livein_iterator livein_end()   const { return LiveIns.end(); }
0513   bool            livein_empty() const { return LiveIns.empty(); }
0514   iterator_range<livein_iterator> liveins() const {
0515     return make_range(livein_begin(), livein_end());
0516   }
0517 
0518   /// Remove entry from the livein set and return iterator to the next.
0519   livein_iterator removeLiveIn(livein_iterator I);
0520 
0521   const std::vector<RegisterMaskPair> &getLiveIns() const { return LiveIns; }
0522 
0523   class liveout_iterator {
0524   public:
0525     using iterator_category = std::input_iterator_tag;
0526     using difference_type = std::ptrdiff_t;
0527     using value_type = RegisterMaskPair;
0528     using pointer = const RegisterMaskPair *;
0529     using reference = const RegisterMaskPair &;
0530 
0531     liveout_iterator(const MachineBasicBlock &MBB, MCPhysReg ExceptionPointer,
0532                      MCPhysReg ExceptionSelector, bool End)
0533         : ExceptionPointer(ExceptionPointer),
0534           ExceptionSelector(ExceptionSelector), BlockI(MBB.succ_begin()),
0535           BlockEnd(MBB.succ_end()) {
0536       if (End)
0537         BlockI = BlockEnd;
0538       else if (BlockI != BlockEnd) {
0539         LiveRegI = (*BlockI)->livein_begin();
0540         if (!advanceToValidPosition())
0541           return;
0542         if (LiveRegI->PhysReg == ExceptionPointer ||
0543             LiveRegI->PhysReg == ExceptionSelector)
0544           ++(*this);
0545       }
0546     }
0547 
0548     liveout_iterator &operator++() {
0549       do {
0550         ++LiveRegI;
0551         if (!advanceToValidPosition())
0552           return *this;
0553       } while ((*BlockI)->isEHPad() &&
0554                (LiveRegI->PhysReg == ExceptionPointer ||
0555                 LiveRegI->PhysReg == ExceptionSelector));
0556       return *this;
0557     }
0558 
0559     liveout_iterator operator++(int) {
0560       liveout_iterator Tmp = *this;
0561       ++(*this);
0562       return Tmp;
0563     }
0564 
0565     reference operator*() const {
0566       return *LiveRegI;
0567     }
0568 
0569     pointer operator->() const {
0570       return &*LiveRegI;
0571     }
0572 
0573     bool operator==(const liveout_iterator &RHS) const {
0574       if (BlockI != BlockEnd)
0575         return BlockI == RHS.BlockI && LiveRegI == RHS.LiveRegI;
0576       return RHS.BlockI == BlockEnd;
0577     }
0578 
0579     bool operator!=(const liveout_iterator &RHS) const {
0580       return !(*this == RHS);
0581     }
0582   private:
0583     bool advanceToValidPosition() {
0584       if (LiveRegI != (*BlockI)->livein_end())
0585         return true;
0586 
0587       do {
0588         ++BlockI;
0589       } while (BlockI != BlockEnd && (*BlockI)->livein_empty());
0590       if (BlockI == BlockEnd)
0591         return false;
0592 
0593       LiveRegI = (*BlockI)->livein_begin();
0594       return true;
0595     }
0596 
0597     MCPhysReg ExceptionPointer, ExceptionSelector;
0598     const_succ_iterator BlockI;
0599     const_succ_iterator BlockEnd;
0600     livein_iterator LiveRegI;
0601   };
0602 
0603   /// Iterator scanning successor basic blocks' liveins to determine the
0604   /// registers potentially live at the end of this block. There may be
0605   /// duplicates or overlapping registers in the list returned.
0606   liveout_iterator liveout_begin() const;
0607   liveout_iterator liveout_end() const {
0608     return liveout_iterator(*this, 0, 0, true);
0609   }
0610   iterator_range<liveout_iterator> liveouts() const {
0611     return make_range(liveout_begin(), liveout_end());
0612   }
0613 
0614   /// Get the clobber mask for the start of this basic block. Funclets use this
0615   /// to prevent register allocation across funclet transitions.
0616   const uint32_t *getBeginClobberMask(const TargetRegisterInfo *TRI) const;
0617 
0618   /// Get the clobber mask for the end of the basic block.
0619   /// \see getBeginClobberMask()
0620   const uint32_t *getEndClobberMask(const TargetRegisterInfo *TRI) const;
0621 
0622   /// Return alignment of the basic block.
0623   Align getAlignment() const { return Alignment; }
0624 
0625   /// Set alignment of the basic block.
0626   void setAlignment(Align A) { Alignment = A; }
0627 
0628   void setAlignment(Align A, unsigned MaxBytes) {
0629     setAlignment(A);
0630     setMaxBytesForAlignment(MaxBytes);
0631   }
0632 
0633   /// Return the maximum amount of padding allowed for aligning the basic block.
0634   unsigned getMaxBytesForAlignment() const { return MaxBytesForAlignment; }
0635 
0636   /// Set the maximum amount of padding allowed for aligning the basic block
0637   void setMaxBytesForAlignment(unsigned MaxBytes) {
0638     MaxBytesForAlignment = MaxBytes;
0639   }
0640 
0641   /// Returns true if the block is a landing pad. That is this basic block is
0642   /// entered via an exception handler.
0643   bool isEHPad() const { return IsEHPad; }
0644 
0645   /// Indicates the block is a landing pad.  That is this basic block is entered
0646   /// via an exception handler.
0647   void setIsEHPad(bool V = true) { IsEHPad = V; }
0648 
0649   bool hasEHPadSuccessor() const;
0650 
0651   /// Returns true if this is the entry block of the function.
0652   bool isEntryBlock() const;
0653 
0654   /// Returns true if this is the entry block of an EH scope, i.e., the block
0655   /// that used to have a catchpad or cleanuppad instruction in the LLVM IR.
0656   bool isEHScopeEntry() const { return IsEHScopeEntry; }
0657 
0658   /// Indicates if this is the entry block of an EH scope, i.e., the block that
0659   /// that used to have a catchpad or cleanuppad instruction in the LLVM IR.
0660   void setIsEHScopeEntry(bool V = true) { IsEHScopeEntry = V; }
0661 
0662   /// Returns true if this is a target block of a catchret.
0663   bool isEHCatchretTarget() const { return IsEHCatchretTarget; }
0664 
0665   /// Indicates if this is a target block of a catchret.
0666   void setIsEHCatchretTarget(bool V = true) { IsEHCatchretTarget = V; }
0667 
0668   /// Returns true if this is the entry block of an EH funclet.
0669   bool isEHFuncletEntry() const { return IsEHFuncletEntry; }
0670 
0671   /// Indicates if this is the entry block of an EH funclet.
0672   void setIsEHFuncletEntry(bool V = true) { IsEHFuncletEntry = V; }
0673 
0674   /// Returns true if this is the entry block of a cleanup funclet.
0675   bool isCleanupFuncletEntry() const { return IsCleanupFuncletEntry; }
0676 
0677   /// Indicates if this is the entry block of a cleanup funclet.
0678   void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; }
0679 
0680   /// Returns true if this block begins any section.
0681   bool isBeginSection() const { return IsBeginSection; }
0682 
0683   /// Returns true if this block ends any section.
0684   bool isEndSection() const { return IsEndSection; }
0685 
0686   void setIsBeginSection(bool V = true) { IsBeginSection = V; }
0687 
0688   void setIsEndSection(bool V = true) { IsEndSection = V; }
0689 
0690   std::optional<UniqueBBID> getBBID() const { return BBID; }
0691 
0692   /// Returns the section ID of this basic block.
0693   MBBSectionID getSectionID() const { return SectionID; }
0694 
0695   /// Sets the fixed BBID of this basic block.
0696   void setBBID(const UniqueBBID &V) {
0697     assert(!BBID.has_value() && "Cannot change BBID.");
0698     BBID = V;
0699   }
0700 
0701   /// Sets the section ID for this basic block.
0702   void setSectionID(MBBSectionID V) { SectionID = V; }
0703 
0704   /// Returns the MCSymbol marking the end of this basic block.
0705   MCSymbol *getEndSymbol() const;
0706 
0707   /// Returns true if this block may have an INLINEASM_BR (overestimate, by
0708   /// checking if any of the successors are indirect targets of any inlineasm_br
0709   /// in the function).
0710   bool mayHaveInlineAsmBr() const;
0711 
0712   /// Returns true if this is the indirect dest of an INLINEASM_BR.
0713   bool isInlineAsmBrIndirectTarget() const {
0714     return IsInlineAsmBrIndirectTarget;
0715   }
0716 
0717   /// Indicates if this is the indirect dest of an INLINEASM_BR.
0718   void setIsInlineAsmBrIndirectTarget(bool V = true) {
0719     IsInlineAsmBrIndirectTarget = V;
0720   }
0721 
0722   /// Returns true if it is legal to hoist instructions into this block.
0723   bool isLegalToHoistInto() const;
0724 
0725   // Code Layout methods.
0726 
0727   /// Move 'this' block before or after the specified block.  This only moves
0728   /// the block, it does not modify the CFG or adjust potential fall-throughs at
0729   /// the end of the block.
0730   void moveBefore(MachineBasicBlock *NewAfter);
0731   void moveAfter(MachineBasicBlock *NewBefore);
0732 
0733   /// Returns true if this and MBB belong to the same section.
0734   bool sameSection(const MachineBasicBlock *MBB) const {
0735     return getSectionID() == MBB->getSectionID();
0736   }
0737 
0738   /// Update the terminator instructions in block to account for changes to
0739   /// block layout which may have been made. PreviousLayoutSuccessor should be
0740   /// set to the block which may have been used as fallthrough before the block
0741   /// layout was modified.  If the block previously fell through to that block,
0742   /// it may now need a branch. If it previously branched to another block, it
0743   /// may now be able to fallthrough to the current layout successor.
0744   void updateTerminator(MachineBasicBlock *PreviousLayoutSuccessor);
0745 
0746   // Machine-CFG mutators
0747 
0748   /// Add Succ as a successor of this MachineBasicBlock.  The Predecessors list
0749   /// of Succ is automatically updated. PROB parameter is stored in
0750   /// Probabilities list. The default probability is set as unknown. Mixing
0751   /// known and unknown probabilities in successor list is not allowed. When all
0752   /// successors have unknown probabilities, 1 / N is returned as the
0753   /// probability for each successor, where N is the number of successors.
0754   ///
0755   /// Note that duplicate Machine CFG edges are not allowed.
0756   void addSuccessor(MachineBasicBlock *Succ,
0757                     BranchProbability Prob = BranchProbability::getUnknown());
0758 
0759   /// Add Succ as a successor of this MachineBasicBlock.  The Predecessors list
0760   /// of Succ is automatically updated. The probability is not provided because
0761   /// BPI is not available (e.g. -O0 is used), in which case edge probabilities
0762   /// won't be used. Using this interface can save some space.
0763   void addSuccessorWithoutProb(MachineBasicBlock *Succ);
0764 
0765   /// Set successor probability of a given iterator.
0766   void setSuccProbability(succ_iterator I, BranchProbability Prob);
0767 
0768   /// Normalize probabilities of all successors so that the sum of them becomes
0769   /// one. This is usually done when the current update on this MBB is done, and
0770   /// the sum of its successors' probabilities is not guaranteed to be one. The
0771   /// user is responsible for the correct use of this function.
0772   /// MBB::removeSuccessor() has an option to do this automatically.
0773   void normalizeSuccProbs() {
0774     BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
0775   }
0776 
0777   /// Validate successors' probabilities and check if the sum of them is
0778   /// approximate one. This only works in DEBUG mode.
0779   void validateSuccProbs() const;
0780 
0781   /// Remove successor from the successors list of this MachineBasicBlock. The
0782   /// Predecessors list of Succ is automatically updated.
0783   /// If NormalizeSuccProbs is true, then normalize successors' probabilities
0784   /// after the successor is removed.
0785   void removeSuccessor(MachineBasicBlock *Succ,
0786                        bool NormalizeSuccProbs = false);
0787 
0788   /// Remove specified successor from the successors list of this
0789   /// MachineBasicBlock. The Predecessors list of Succ is automatically updated.
0790   /// If NormalizeSuccProbs is true, then normalize successors' probabilities
0791   /// after the successor is removed.
0792   /// Return the iterator to the element after the one removed.
0793   succ_iterator removeSuccessor(succ_iterator I,
0794                                 bool NormalizeSuccProbs = false);
0795 
0796   /// Replace successor OLD with NEW and update probability info.
0797   void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New);
0798 
0799   /// Copy a successor (and any probability info) from original block to this
0800   /// block's. Uses an iterator into the original blocks successors.
0801   ///
0802   /// This is useful when doing a partial clone of successors. Afterward, the
0803   /// probabilities may need to be normalized.
0804   void copySuccessor(const MachineBasicBlock *Orig, succ_iterator I);
0805 
0806   /// Split the old successor into old plus new and updates the probability
0807   /// info.
0808   void splitSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New,
0809                       bool NormalizeSuccProbs = false);
0810 
0811   /// Transfers all the successors from MBB to this machine basic block (i.e.,
0812   /// copies all the successors FromMBB and remove all the successors from
0813   /// FromMBB).
0814   void transferSuccessors(MachineBasicBlock *FromMBB);
0815 
0816   /// Transfers all the successors, as in transferSuccessors, and update PHI
0817   /// operands in the successor blocks which refer to FromMBB to refer to this.
0818   void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB);
0819 
0820   /// Return true if any of the successors have probabilities attached to them.
0821   bool hasSuccessorProbabilities() const { return !Probs.empty(); }
0822 
0823   /// Return true if the specified MBB is a predecessor of this block.
0824   bool isPredecessor(const MachineBasicBlock *MBB) const;
0825 
0826   /// Return true if the specified MBB is a successor of this block.
0827   bool isSuccessor(const MachineBasicBlock *MBB) const;
0828 
0829   /// Return true if the specified MBB will be emitted immediately after this
0830   /// block, such that if this block exits by falling through, control will
0831   /// transfer to the specified MBB. Note that MBB need not be a successor at
0832   /// all, for example if this block ends with an unconditional branch to some
0833   /// other block.
0834   bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
0835 
0836   /// Return the successor of this block if it has a single successor.
0837   /// Otherwise return a null pointer.
0838   ///
0839   const MachineBasicBlock *getSingleSuccessor() const;
0840   MachineBasicBlock *getSingleSuccessor() {
0841     return const_cast<MachineBasicBlock *>(
0842         static_cast<const MachineBasicBlock *>(this)->getSingleSuccessor());
0843   }
0844 
0845   /// Return the predecessor of this block if it has a single predecessor.
0846   /// Otherwise return a null pointer.
0847   ///
0848   const MachineBasicBlock *getSinglePredecessor() const;
0849   MachineBasicBlock *getSinglePredecessor() {
0850     return const_cast<MachineBasicBlock *>(
0851         static_cast<const MachineBasicBlock *>(this)->getSinglePredecessor());
0852   }
0853 
0854   /// Return the fallthrough block if the block can implicitly
0855   /// transfer control to the block after it by falling off the end of
0856   /// it. If an explicit branch to the fallthrough block is not allowed,
0857   /// set JumpToFallThrough to be false. Non-null return is a conservative
0858   /// answer.
0859   MachineBasicBlock *getFallThrough(bool JumpToFallThrough = true);
0860 
0861   /// Return the fallthrough block if the block can implicitly
0862   /// transfer control to it's successor, whether by a branch or
0863   /// a fallthrough. Non-null return is a conservative answer.
0864   MachineBasicBlock *getLogicalFallThrough() { return getFallThrough(false); }
0865 
0866   /// Return true if the block can implicitly transfer control to the
0867   /// block after it by falling off the end of it.  This should return
0868   /// false if it can reach the block after it, but it uses an
0869   /// explicit branch to do so (e.g., a table jump).  True is a
0870   /// conservative answer.
0871   bool canFallThrough();
0872 
0873   /// Returns a pointer to the first instruction in this block that is not a
0874   /// PHINode instruction. When adding instructions to the beginning of the
0875   /// basic block, they should be added before the returned value, not before
0876   /// the first instruction, which might be PHI.
0877   /// Returns end() is there's no non-PHI instruction.
0878   iterator getFirstNonPHI();
0879   const_iterator getFirstNonPHI() const {
0880     return const_cast<MachineBasicBlock *>(this)->getFirstNonPHI();
0881   }
0882 
0883   /// Return the first instruction in MBB after I that is not a PHI or a label.
0884   /// This is the correct point to insert lowered copies at the beginning of a
0885   /// basic block that must be before any debugging information.
0886   iterator SkipPHIsAndLabels(iterator I);
0887 
0888   /// Return the first instruction in MBB after I that is not a PHI, label or
0889   /// debug.  This is the correct point to insert copies at the beginning of a
0890   /// basic block. \p Reg is the register being used by a spill or defined for a
0891   /// restore/split during register allocation.
0892   iterator SkipPHIsLabelsAndDebug(iterator I, Register Reg = Register(),
0893                                   bool SkipPseudoOp = true);
0894 
0895   /// Returns an iterator to the first terminator instruction of this basic
0896   /// block. If a terminator does not exist, it returns end().
0897   iterator getFirstTerminator();
0898   const_iterator getFirstTerminator() const {
0899     return const_cast<MachineBasicBlock *>(this)->getFirstTerminator();
0900   }
0901 
0902   /// Same getFirstTerminator but it ignores bundles and return an
0903   /// instr_iterator instead.
0904   instr_iterator getFirstInstrTerminator();
0905 
0906   /// Finds the first terminator in a block by scanning forward. This can handle
0907   /// cases in GlobalISel where there may be non-terminator instructions between
0908   /// terminators, for which getFirstTerminator() will not work correctly.
0909   iterator getFirstTerminatorForward();
0910 
0911   /// Returns an iterator to the first non-debug instruction in the basic block,
0912   /// or end(). Skip any pseudo probe operation if \c SkipPseudoOp is true.
0913   /// Pseudo probes are like debug instructions which do not turn into real
0914   /// machine code. We try to use the function to skip both debug instructions
0915   /// and pseudo probe operations to avoid API proliferation. This should work
0916   /// most of the time when considering optimizing the rest of code in the
0917   /// block, except for certain cases where pseudo probes are designed to block
0918   /// the optimizations. For example, code merge like optimizations are supposed
0919   /// to be blocked by pseudo probes for better AutoFDO profile quality.
0920   /// Therefore, they should be considered as a valid instruction when this
0921   /// function is called in a context of such optimizations. On the other hand,
0922   /// \c SkipPseudoOp should be true when it's used in optimizations that
0923   /// unlikely hurt profile quality, e.g., without block merging. The default
0924   /// value of \c SkipPseudoOp is set to true to maximize code quality in
0925   /// general, with an explict false value passed in in a few places like branch
0926   /// folding and if-conversion to favor profile quality.
0927   iterator getFirstNonDebugInstr(bool SkipPseudoOp = true);
0928   const_iterator getFirstNonDebugInstr(bool SkipPseudoOp = true) const {
0929     return const_cast<MachineBasicBlock *>(this)->getFirstNonDebugInstr(
0930         SkipPseudoOp);
0931   }
0932 
0933   /// Returns an iterator to the last non-debug instruction in the basic block,
0934   /// or end(). Skip any pseudo operation if \c SkipPseudoOp is true.
0935   /// Pseudo probes are like debug instructions which do not turn into real
0936   /// machine code. We try to use the function to skip both debug instructions
0937   /// and pseudo probe operations to avoid API proliferation. This should work
0938   /// most of the time when considering optimizing the rest of code in the
0939   /// block, except for certain cases where pseudo probes are designed to block
0940   /// the optimizations. For example, code merge like optimizations are supposed
0941   /// to be blocked by pseudo probes for better AutoFDO profile quality.
0942   /// Therefore, they should be considered as a valid instruction when this
0943   /// function is called in a context of such optimizations. On the other hand,
0944   /// \c SkipPseudoOp should be true when it's used in optimizations that
0945   /// unlikely hurt profile quality, e.g., without block merging. The default
0946   /// value of \c SkipPseudoOp is set to true to maximize code quality in
0947   /// general, with an explict false value passed in in a few places like branch
0948   /// folding and if-conversion to favor profile quality.
0949   iterator getLastNonDebugInstr(bool SkipPseudoOp = true);
0950   const_iterator getLastNonDebugInstr(bool SkipPseudoOp = true) const {
0951     return const_cast<MachineBasicBlock *>(this)->getLastNonDebugInstr(
0952         SkipPseudoOp);
0953   }
0954 
0955   /// Convenience function that returns true if the block ends in a return
0956   /// instruction.
0957   bool isReturnBlock() const {
0958     return !empty() && back().isReturn();
0959   }
0960 
0961   /// Convenience function that returns true if the bock ends in a EH scope
0962   /// return instruction.
0963   bool isEHScopeReturnBlock() const {
0964     return !empty() && back().isEHScopeReturn();
0965   }
0966 
0967   /// Split a basic block into 2 pieces at \p SplitPoint. A new block will be
0968   /// inserted after this block, and all instructions after \p SplitInst moved
0969   /// to it (\p SplitInst will be in the original block). If \p LIS is provided,
0970   /// LiveIntervals will be appropriately updated. \return the newly inserted
0971   /// block.
0972   ///
0973   /// If \p UpdateLiveIns is true, this will ensure the live ins list is
0974   /// accurate, including for physreg uses/defs in the original block.
0975   MachineBasicBlock *splitAt(MachineInstr &SplitInst, bool UpdateLiveIns = true,
0976                              LiveIntervals *LIS = nullptr);
0977 
0978   /// Split the critical edge from this block to the given successor block, and
0979   /// return the newly created block, or null if splitting is not possible.
0980   ///
0981   /// This function updates LiveVariables, MachineDominatorTree, and
0982   /// MachineLoopInfo, as applicable.
0983   MachineBasicBlock *
0984   SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P,
0985                     std::vector<SparseBitVector<>> *LiveInSets = nullptr,
0986                     MachineDomTreeUpdater *MDTU = nullptr) {
0987     return SplitCriticalEdge(Succ, &P, nullptr, LiveInSets, MDTU);
0988   }
0989 
0990   MachineBasicBlock *
0991   SplitCriticalEdge(MachineBasicBlock *Succ,
0992                     MachineFunctionAnalysisManager &MFAM,
0993                     std::vector<SparseBitVector<>> *LiveInSets = nullptr,
0994                     MachineDomTreeUpdater *MDTU = nullptr) {
0995     return SplitCriticalEdge(Succ, nullptr, &MFAM, LiveInSets, MDTU);
0996   }
0997 
0998   // Helper method for new pass manager migration.
0999   MachineBasicBlock *SplitCriticalEdge(
1000       MachineBasicBlock *Succ, Pass *P, MachineFunctionAnalysisManager *MFAM,
1001       std::vector<SparseBitVector<>> *LiveInSets, MachineDomTreeUpdater *MDTU);
1002 
1003   /// Check if the edge between this block and the given successor \p
1004   /// Succ, can be split. If this returns true a subsequent call to
1005   /// SplitCriticalEdge is guaranteed to return a valid basic block if
1006   /// no changes occurred in the meantime.
1007   bool canSplitCriticalEdge(const MachineBasicBlock *Succ) const;
1008 
1009   void pop_front() { Insts.pop_front(); }
1010   void pop_back() { Insts.pop_back(); }
1011   void push_back(MachineInstr *MI) { Insts.push_back(MI); }
1012 
1013   /// Insert MI into the instruction list before I, possibly inside a bundle.
1014   ///
1015   /// If the insertion point is inside a bundle, MI will be added to the bundle,
1016   /// otherwise MI will not be added to any bundle. That means this function
1017   /// alone can't be used to prepend or append instructions to bundles. See
1018   /// MIBundleBuilder::insert() for a more reliable way of doing that.
1019   instr_iterator insert(instr_iterator I, MachineInstr *M);
1020 
1021   /// Insert a range of instructions into the instruction list before I.
1022   template<typename IT>
1023   void insert(iterator I, IT S, IT E) {
1024     assert((I == end() || I->getParent() == this) &&
1025            "iterator points outside of basic block");
1026     Insts.insert(I.getInstrIterator(), S, E);
1027   }
1028 
1029   /// Insert MI into the instruction list before I.
1030   iterator insert(iterator I, MachineInstr *MI) {
1031     assert((I == end() || I->getParent() == this) &&
1032            "iterator points outside of basic block");
1033     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
1034            "Cannot insert instruction with bundle flags");
1035     return Insts.insert(I.getInstrIterator(), MI);
1036   }
1037 
1038   /// Insert MI into the instruction list after I.
1039   iterator insertAfter(iterator I, MachineInstr *MI) {
1040     assert((I == end() || I->getParent() == this) &&
1041            "iterator points outside of basic block");
1042     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
1043            "Cannot insert instruction with bundle flags");
1044     return Insts.insertAfter(I.getInstrIterator(), MI);
1045   }
1046 
1047   /// If I is bundled then insert MI into the instruction list after the end of
1048   /// the bundle, otherwise insert MI immediately after I.
1049   instr_iterator insertAfterBundle(instr_iterator I, MachineInstr *MI) {
1050     assert((I == instr_end() || I->getParent() == this) &&
1051            "iterator points outside of basic block");
1052     assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
1053            "Cannot insert instruction with bundle flags");
1054     while (I->isBundledWithSucc())
1055       ++I;
1056     return Insts.insertAfter(I, MI);
1057   }
1058 
1059   /// Remove an instruction from the instruction list and delete it.
1060   ///
1061   /// If the instruction is part of a bundle, the other instructions in the
1062   /// bundle will still be bundled after removing the single instruction.
1063   instr_iterator erase(instr_iterator I);
1064 
1065   /// Remove an instruction from the instruction list and delete it.
1066   ///
1067   /// If the instruction is part of a bundle, the other instructions in the
1068   /// bundle will still be bundled after removing the single instruction.
1069   instr_iterator erase_instr(MachineInstr *I) {
1070     return erase(instr_iterator(I));
1071   }
1072 
1073   /// Remove a range of instructions from the instruction list and delete them.
1074   iterator erase(iterator I, iterator E) {
1075     return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
1076   }
1077 
1078   /// Remove an instruction or bundle from the instruction list and delete it.
1079   ///
1080   /// If I points to a bundle of instructions, they are all erased.
1081   iterator erase(iterator I) {
1082     return erase(I, std::next(I));
1083   }
1084 
1085   /// Remove an instruction from the instruction list and delete it.
1086   ///
1087   /// If I is the head of a bundle of instructions, the whole bundle will be
1088   /// erased.
1089   iterator erase(MachineInstr *I) {
1090     return erase(iterator(I));
1091   }
1092 
1093   /// Remove the unbundled instruction from the instruction list without
1094   /// deleting it.
1095   ///
1096   /// This function can not be used to remove bundled instructions, use
1097   /// remove_instr to remove individual instructions from a bundle.
1098   MachineInstr *remove(MachineInstr *I) {
1099     assert(!I->isBundled() && "Cannot remove bundled instructions");
1100     return Insts.remove(instr_iterator(I));
1101   }
1102 
1103   /// Remove the possibly bundled instruction from the instruction list
1104   /// without deleting it.
1105   ///
1106   /// If the instruction is part of a bundle, the other instructions in the
1107   /// bundle will still be bundled after removing the single instruction.
1108   MachineInstr *remove_instr(MachineInstr *I);
1109 
1110   void clear() {
1111     Insts.clear();
1112   }
1113 
1114   /// Take an instruction from MBB 'Other' at the position From, and insert it
1115   /// into this MBB right before 'Where'.
1116   ///
1117   /// If From points to a bundle of instructions, the whole bundle is moved.
1118   void splice(iterator Where, MachineBasicBlock *Other, iterator From) {
1119     // The range splice() doesn't allow noop moves, but this one does.
1120     if (Where != From)
1121       splice(Where, Other, From, std::next(From));
1122   }
1123 
1124   /// Take a block of instructions from MBB 'Other' in the range [From, To),
1125   /// and insert them into this MBB right before 'Where'.
1126   ///
1127   /// The instruction at 'Where' must not be included in the range of
1128   /// instructions to move.
1129   void splice(iterator Where, MachineBasicBlock *Other,
1130               iterator From, iterator To) {
1131     Insts.splice(Where.getInstrIterator(), Other->Insts,
1132                  From.getInstrIterator(), To.getInstrIterator());
1133   }
1134 
1135   /// This method unlinks 'this' from the containing function, and returns it,
1136   /// but does not delete it.
1137   MachineBasicBlock *removeFromParent();
1138 
1139   /// This method unlinks 'this' from the containing function and deletes it.
1140   void eraseFromParent();
1141 
1142   /// Given a machine basic block that branched to 'Old', change the code and
1143   /// CFG so that it branches to 'New' instead.
1144   void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
1145 
1146   /// Update all phi nodes in this basic block to refer to basic block \p New
1147   /// instead of basic block \p Old.
1148   void replacePhiUsesWith(MachineBasicBlock *Old, MachineBasicBlock *New);
1149 
1150   /// Find the next valid DebugLoc starting at MBBI, skipping any debug
1151   /// instructions.  Return UnknownLoc if there is none.
1152   DebugLoc findDebugLoc(instr_iterator MBBI);
1153   DebugLoc findDebugLoc(iterator MBBI) {
1154     return findDebugLoc(MBBI.getInstrIterator());
1155   }
1156 
1157   /// Has exact same behavior as @ref findDebugLoc (it also searches towards the
1158   /// end of this MBB) except that this function takes a reverse iterator to
1159   /// identify the starting MI.
1160   DebugLoc rfindDebugLoc(reverse_instr_iterator MBBI);
1161   DebugLoc rfindDebugLoc(reverse_iterator MBBI) {
1162     return rfindDebugLoc(MBBI.getInstrIterator());
1163   }
1164 
1165   /// Find the previous valid DebugLoc preceding MBBI, skipping any debug
1166   /// instructions. It is possible to find the last DebugLoc in the MBB using
1167   /// findPrevDebugLoc(instr_end()).  Return UnknownLoc if there is none.
1168   DebugLoc findPrevDebugLoc(instr_iterator MBBI);
1169   DebugLoc findPrevDebugLoc(iterator MBBI) {
1170     return findPrevDebugLoc(MBBI.getInstrIterator());
1171   }
1172 
1173   /// Has exact same behavior as @ref findPrevDebugLoc (it also searches towards
1174   /// the beginning of this MBB) except that this function takes reverse
1175   /// iterator to identify the starting MI. A minor difference compared to
1176   /// findPrevDebugLoc is that we can't start scanning at "instr_end".
1177   DebugLoc rfindPrevDebugLoc(reverse_instr_iterator MBBI);
1178   DebugLoc rfindPrevDebugLoc(reverse_iterator MBBI) {
1179     return rfindPrevDebugLoc(MBBI.getInstrIterator());
1180   }
1181 
1182   /// Find and return the merged DebugLoc of the branch instructions of the
1183   /// block. Return UnknownLoc if there is none.
1184   DebugLoc findBranchDebugLoc();
1185 
1186   /// Possible outcome of a register liveness query to computeRegisterLiveness()
1187   enum LivenessQueryResult {
1188     LQR_Live,   ///< Register is known to be (at least partially) live.
1189     LQR_Dead,   ///< Register is known to be fully dead.
1190     LQR_Unknown ///< Register liveness not decidable from local neighborhood.
1191   };
1192 
1193   /// Return whether (physical) register \p Reg has been defined and not
1194   /// killed as of just before \p Before.
1195   ///
1196   /// Search is localised to a neighborhood of \p Neighborhood instructions
1197   /// before (searching for defs or kills) and \p Neighborhood instructions
1198   /// after (searching just for defs) \p Before.
1199   ///
1200   /// \p Reg must be a physical register.
1201   LivenessQueryResult computeRegisterLiveness(const TargetRegisterInfo *TRI,
1202                                               MCRegister Reg,
1203                                               const_iterator Before,
1204                                               unsigned Neighborhood = 10) const;
1205 
1206   // Debugging methods.
1207   void dump() const;
1208   void print(raw_ostream &OS, const SlotIndexes * = nullptr,
1209              bool IsStandalone = true) const;
1210   void print(raw_ostream &OS, ModuleSlotTracker &MST,
1211              const SlotIndexes * = nullptr, bool IsStandalone = true) const;
1212 
1213   enum PrintNameFlag {
1214     PrintNameIr = (1 << 0), ///< Add IR name where available
1215     PrintNameAttributes = (1 << 1), ///< Print attributes
1216   };
1217 
1218   void printName(raw_ostream &os, unsigned printNameFlags = PrintNameIr,
1219                  ModuleSlotTracker *moduleSlotTracker = nullptr) const;
1220 
1221   // Printing method used by LoopInfo.
1222   void printAsOperand(raw_ostream &OS, bool PrintType = true) const;
1223 
1224   /// MachineBasicBlocks are uniquely numbered at the function level, unless
1225   /// they're not in a MachineFunction yet, in which case this will return -1.
1226   int getNumber() const { return Number; }
1227   void setNumber(int N) { Number = N; }
1228 
1229   /// Return the call frame size on entry to this basic block.
1230   unsigned getCallFrameSize() const { return CallFrameSize; }
1231   /// Set the call frame size on entry to this basic block.
1232   void setCallFrameSize(unsigned N) { CallFrameSize = N; }
1233 
1234   /// Return the MCSymbol for this basic block.
1235   MCSymbol *getSymbol() const;
1236 
1237   /// Return the EHCatchret Symbol for this basic block.
1238   MCSymbol *getEHCatchretSymbol() const;
1239 
1240   std::optional<uint64_t> getIrrLoopHeaderWeight() const {
1241     return IrrLoopHeaderWeight;
1242   }
1243 
1244   void setIrrLoopHeaderWeight(uint64_t Weight) {
1245     IrrLoopHeaderWeight = Weight;
1246   }
1247 
1248   /// Return probability of the edge from this block to MBB. This method should
1249   /// NOT be called directly, but by using getEdgeProbability method from
1250   /// MachineBranchProbabilityInfo class.
1251   BranchProbability getSuccProbability(const_succ_iterator Succ) const;
1252 
1253 private:
1254   /// Return probability iterator corresponding to the I successor iterator.
1255   probability_iterator getProbabilityIterator(succ_iterator I);
1256   const_probability_iterator
1257   getProbabilityIterator(const_succ_iterator I) const;
1258 
1259   friend class MachineBranchProbabilityInfo;
1260   friend class MIPrinter;
1261 
1262   // Methods used to maintain doubly linked list of blocks...
1263   friend struct ilist_callback_traits<MachineBasicBlock>;
1264 
1265   // Machine-CFG mutators
1266 
1267   /// Add Pred as a predecessor of this MachineBasicBlock. Don't do this
1268   /// unless you know what you're doing, because it doesn't update Pred's
1269   /// successors list. Use Pred->addSuccessor instead.
1270   void addPredecessor(MachineBasicBlock *Pred);
1271 
1272   /// Remove Pred as a predecessor of this MachineBasicBlock. Don't do this
1273   /// unless you know what you're doing, because it doesn't update Pred's
1274   /// successors list. Use Pred->removeSuccessor instead.
1275   void removePredecessor(MachineBasicBlock *Pred);
1276 };
1277 
1278 raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
1279 
1280 /// Prints a machine basic block reference.
1281 ///
1282 /// The format is:
1283 ///   %bb.5           - a machine basic block with MBB.getNumber() == 5.
1284 ///
1285 /// Usage: OS << printMBBReference(MBB) << '\n';
1286 Printable printMBBReference(const MachineBasicBlock &MBB);
1287 
1288 // This is useful when building IndexedMaps keyed on basic block pointers.
1289 struct MBB2NumberFunctor {
1290   using argument_type = const MachineBasicBlock *;
1291   unsigned operator()(const MachineBasicBlock *MBB) const {
1292     return MBB->getNumber();
1293   }
1294 };
1295 
1296 //===--------------------------------------------------------------------===//
1297 // GraphTraits specializations for machine basic block graphs (machine-CFGs)
1298 //===--------------------------------------------------------------------===//
1299 
1300 // Provide specializations of GraphTraits to be able to treat a
1301 // MachineFunction as a graph of MachineBasicBlocks.
1302 //
1303 
1304 template <> struct GraphTraits<MachineBasicBlock *> {
1305   using NodeRef = MachineBasicBlock *;
1306   using ChildIteratorType = MachineBasicBlock::succ_iterator;
1307 
1308   static NodeRef getEntryNode(MachineBasicBlock *BB) { return BB; }
1309   static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
1310   static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
1311 
1312   static unsigned getNumber(MachineBasicBlock *BB) {
1313     assert(BB->getNumber() >= 0 && "negative block number");
1314     return BB->getNumber();
1315   }
1316 };
1317 
1318 static_assert(GraphHasNodeNumbers<MachineBasicBlock *>,
1319               "GraphTraits getNumber() not detected");
1320 
1321 template <> struct GraphTraits<const MachineBasicBlock *> {
1322   using NodeRef = const MachineBasicBlock *;
1323   using ChildIteratorType = MachineBasicBlock::const_succ_iterator;
1324 
1325   static NodeRef getEntryNode(const MachineBasicBlock *BB) { return BB; }
1326   static ChildIteratorType child_begin(NodeRef N) { return N->succ_begin(); }
1327   static ChildIteratorType child_end(NodeRef N) { return N->succ_end(); }
1328 
1329   static unsigned getNumber(const MachineBasicBlock *BB) {
1330     assert(BB->getNumber() >= 0 && "negative block number");
1331     return BB->getNumber();
1332   }
1333 };
1334 
1335 static_assert(GraphHasNodeNumbers<const MachineBasicBlock *>,
1336               "GraphTraits getNumber() not detected");
1337 
1338 // Provide specializations of GraphTraits to be able to treat a
1339 // MachineFunction as a graph of MachineBasicBlocks and to walk it
1340 // in inverse order.  Inverse order for a function is considered
1341 // to be when traversing the predecessor edges of a MBB
1342 // instead of the successor edges.
1343 //
1344 template <> struct GraphTraits<Inverse<MachineBasicBlock*>> {
1345   using NodeRef = MachineBasicBlock *;
1346   using ChildIteratorType = MachineBasicBlock::pred_iterator;
1347 
1348   static NodeRef getEntryNode(Inverse<MachineBasicBlock *> G) {
1349     return G.Graph;
1350   }
1351 
1352   static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
1353   static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
1354 
1355   static unsigned getNumber(MachineBasicBlock *BB) {
1356     assert(BB->getNumber() >= 0 && "negative block number");
1357     return BB->getNumber();
1358   }
1359 };
1360 
1361 static_assert(GraphHasNodeNumbers<Inverse<MachineBasicBlock *>>,
1362               "GraphTraits getNumber() not detected");
1363 
1364 template <> struct GraphTraits<Inverse<const MachineBasicBlock*>> {
1365   using NodeRef = const MachineBasicBlock *;
1366   using ChildIteratorType = MachineBasicBlock::const_pred_iterator;
1367 
1368   static NodeRef getEntryNode(Inverse<const MachineBasicBlock *> G) {
1369     return G.Graph;
1370   }
1371 
1372   static ChildIteratorType child_begin(NodeRef N) { return N->pred_begin(); }
1373   static ChildIteratorType child_end(NodeRef N) { return N->pred_end(); }
1374 
1375   static unsigned getNumber(const MachineBasicBlock *BB) {
1376     assert(BB->getNumber() >= 0 && "negative block number");
1377     return BB->getNumber();
1378   }
1379 };
1380 
1381 static_assert(GraphHasNodeNumbers<Inverse<const MachineBasicBlock *>>,
1382               "GraphTraits getNumber() not detected");
1383 
1384 // These accessors are handy for sharing templated code between IR and MIR.
1385 inline auto successors(const MachineBasicBlock *BB) { return BB->successors(); }
1386 inline auto predecessors(const MachineBasicBlock *BB) {
1387   return BB->predecessors();
1388 }
1389 inline auto succ_size(const MachineBasicBlock *BB) { return BB->succ_size(); }
1390 inline auto pred_size(const MachineBasicBlock *BB) { return BB->pred_size(); }
1391 inline auto succ_begin(const MachineBasicBlock *BB) { return BB->succ_begin(); }
1392 inline auto pred_begin(const MachineBasicBlock *BB) { return BB->pred_begin(); }
1393 inline auto succ_end(const MachineBasicBlock *BB) { return BB->succ_end(); }
1394 inline auto pred_end(const MachineBasicBlock *BB) { return BB->pred_end(); }
1395 
1396 /// MachineInstrSpan provides an interface to get an iteration range
1397 /// containing the instruction it was initialized with, along with all
1398 /// those instructions inserted prior to or following that instruction
1399 /// at some point after the MachineInstrSpan is constructed.
1400 class MachineInstrSpan {
1401   MachineBasicBlock &MBB;
1402   MachineBasicBlock::iterator I, B, E;
1403 
1404 public:
1405   MachineInstrSpan(MachineBasicBlock::iterator I, MachineBasicBlock *BB)
1406       : MBB(*BB), I(I), B(I == MBB.begin() ? MBB.end() : std::prev(I)),
1407         E(std::next(I)) {
1408     assert(I == BB->end() || I->getParent() == BB);
1409   }
1410 
1411   MachineBasicBlock::iterator begin() {
1412     return B == MBB.end() ? MBB.begin() : std::next(B);
1413   }
1414   MachineBasicBlock::iterator end() { return E; }
1415   bool empty() { return begin() == end(); }
1416 
1417   MachineBasicBlock::iterator getInitial() { return I; }
1418 };
1419 
1420 /// Increment \p It until it points to a non-debug instruction or to \p End
1421 /// and return the resulting iterator. This function should only be used
1422 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
1423 /// const_instr_iterator} and the respective reverse iterators.
1424 template <typename IterT>
1425 inline IterT skipDebugInstructionsForward(IterT It, IterT End,
1426                                           bool SkipPseudoOp = true) {
1427   while (It != End &&
1428          (It->isDebugInstr() || (SkipPseudoOp && It->isPseudoProbe())))
1429     ++It;
1430   return It;
1431 }
1432 
1433 /// Decrement \p It until it points to a non-debug instruction or to \p Begin
1434 /// and return the resulting iterator. This function should only be used
1435 /// MachineBasicBlock::{iterator, const_iterator, instr_iterator,
1436 /// const_instr_iterator} and the respective reverse iterators.
1437 template <class IterT>
1438 inline IterT skipDebugInstructionsBackward(IterT It, IterT Begin,
1439                                            bool SkipPseudoOp = true) {
1440   while (It != Begin &&
1441          (It->isDebugInstr() || (SkipPseudoOp && It->isPseudoProbe())))
1442     --It;
1443   return It;
1444 }
1445 
1446 /// Increment \p It, then continue incrementing it while it points to a debug
1447 /// instruction. A replacement for std::next.
1448 template <typename IterT>
1449 inline IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp = true) {
1450   return skipDebugInstructionsForward(std::next(It), End, SkipPseudoOp);
1451 }
1452 
1453 /// Decrement \p It, then continue decrementing it while it points to a debug
1454 /// instruction. A replacement for std::prev.
1455 template <typename IterT>
1456 inline IterT prev_nodbg(IterT It, IterT Begin, bool SkipPseudoOp = true) {
1457   return skipDebugInstructionsBackward(std::prev(It), Begin, SkipPseudoOp);
1458 }
1459 
1460 /// Construct a range iterator which begins at \p It and moves forwards until
1461 /// \p End is reached, skipping any debug instructions.
1462 template <typename IterT>
1463 inline auto instructionsWithoutDebug(IterT It, IterT End,
1464                                      bool SkipPseudoOp = true) {
1465   return make_filter_range(make_range(It, End), [=](const MachineInstr &MI) {
1466     return !MI.isDebugInstr() && !(SkipPseudoOp && MI.isPseudoProbe());
1467   });
1468 }
1469 
1470 } // end namespace llvm
1471 
1472 #endif // LLVM_CODEGEN_MACHINEBASICBLOCK_H