Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- llvm/CodeGen/MachineRegisterInfo.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 // This file defines the MachineRegisterInfo class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H
0014 #define LLVM_CODEGEN_MACHINEREGISTERINFO_H
0015 
0016 #include "llvm/ADT/ArrayRef.h"
0017 #include "llvm/ADT/BitVector.h"
0018 #include "llvm/ADT/IndexedMap.h"
0019 #include "llvm/ADT/PointerUnion.h"
0020 #include "llvm/ADT/SmallPtrSet.h"
0021 #include "llvm/ADT/SmallVector.h"
0022 #include "llvm/ADT/StringSet.h"
0023 #include "llvm/ADT/iterator_range.h"
0024 #include "llvm/CodeGen/MachineBasicBlock.h"
0025 #include "llvm/CodeGen/MachineFunction.h"
0026 #include "llvm/CodeGen/MachineInstrBundle.h"
0027 #include "llvm/CodeGen/MachineOperand.h"
0028 #include "llvm/CodeGen/RegisterBank.h"
0029 #include "llvm/CodeGen/TargetRegisterInfo.h"
0030 #include "llvm/CodeGen/TargetSubtargetInfo.h"
0031 #include "llvm/MC/LaneBitmask.h"
0032 #include <cassert>
0033 #include <cstddef>
0034 #include <cstdint>
0035 #include <iterator>
0036 #include <memory>
0037 #include <utility>
0038 #include <vector>
0039 
0040 namespace llvm {
0041 
0042 class PSetIterator;
0043 
0044 /// Convenient type to represent either a register class or a register bank.
0045 using RegClassOrRegBank =
0046     PointerUnion<const TargetRegisterClass *, const RegisterBank *>;
0047 
0048 /// MachineRegisterInfo - Keep track of information for virtual and physical
0049 /// registers, including vreg register classes, use/def chains for registers,
0050 /// etc.
0051 class MachineRegisterInfo {
0052 public:
0053   class Delegate {
0054     virtual void anchor();
0055 
0056   public:
0057     virtual ~Delegate() = default;
0058 
0059     virtual void MRI_NoteNewVirtualRegister(Register Reg) = 0;
0060     virtual void MRI_NoteCloneVirtualRegister(Register NewReg,
0061                                               Register SrcReg) {
0062       MRI_NoteNewVirtualRegister(NewReg);
0063     }
0064   };
0065 
0066 private:
0067   MachineFunction *MF;
0068   SmallPtrSet<Delegate *, 1> TheDelegates;
0069 
0070   /// True if subregister liveness is tracked.
0071   const bool TracksSubRegLiveness;
0072 
0073   /// VRegInfo - Information we keep for each virtual register.
0074   ///
0075   /// Each element in this list contains the register class of the vreg and the
0076   /// start of the use/def list for the register.
0077   IndexedMap<std::pair<RegClassOrRegBank, MachineOperand *>,
0078              VirtReg2IndexFunctor>
0079       VRegInfo;
0080 
0081   /// Map for recovering vreg name from vreg number.
0082   /// This map is used by the MIR Printer.
0083   IndexedMap<std::string, VirtReg2IndexFunctor> VReg2Name;
0084 
0085   /// StringSet that is used to unique vreg names.
0086   StringSet<> VRegNames;
0087 
0088   /// The flag is true upon \p UpdatedCSRs initialization
0089   /// and false otherwise.
0090   bool IsUpdatedCSRsInitialized = false;
0091 
0092   /// Contains the updated callee saved register list.
0093   /// As opposed to the static list defined in register info,
0094   /// all registers that were disabled are removed from the list.
0095   SmallVector<MCPhysReg, 16> UpdatedCSRs;
0096 
0097   /// RegAllocHints - This vector records register allocation hints for
0098   /// virtual registers. For each virtual register, it keeps a pair of hint
0099   /// type and hints vector making up the allocation hints. Only the first
0100   /// hint may be target specific, and in that case this is reflected by the
0101   /// first member of the pair being non-zero. If the hinted register is
0102   /// virtual, it means the allocator should prefer the physical register
0103   /// allocated to it if any.
0104   IndexedMap<std::pair<unsigned, SmallVector<Register, 4>>,
0105              VirtReg2IndexFunctor>
0106       RegAllocHints;
0107 
0108   /// PhysRegUseDefLists - This is an array of the head of the use/def list for
0109   /// physical registers.
0110   std::unique_ptr<MachineOperand *[]> PhysRegUseDefLists;
0111 
0112   /// getRegUseDefListHead - Return the head pointer for the register use/def
0113   /// list for the specified virtual or physical register.
0114   MachineOperand *&getRegUseDefListHead(Register RegNo) {
0115     if (RegNo.isVirtual())
0116       return VRegInfo[RegNo.id()].second;
0117     return PhysRegUseDefLists[RegNo.id()];
0118   }
0119 
0120   MachineOperand *getRegUseDefListHead(Register RegNo) const {
0121     if (RegNo.isVirtual())
0122       return VRegInfo[RegNo.id()].second;
0123     return PhysRegUseDefLists[RegNo.id()];
0124   }
0125 
0126   /// Get the next element in the use-def chain.
0127   static MachineOperand *getNextOperandForReg(const MachineOperand *MO) {
0128     assert(MO && MO->isReg() && "This is not a register operand!");
0129     return MO->Contents.Reg.Next;
0130   }
0131 
0132   /// UsedPhysRegMask - Additional used physregs including aliases.
0133   /// This bit vector represents all the registers clobbered by function calls.
0134   BitVector UsedPhysRegMask;
0135 
0136   /// ReservedRegs - This is a bit vector of reserved registers.  The target
0137   /// may change its mind about which registers should be reserved.  This
0138   /// vector is the frozen set of reserved registers when register allocation
0139   /// started.
0140   BitVector ReservedRegs;
0141 
0142   using VRegToTypeMap = IndexedMap<LLT, VirtReg2IndexFunctor>;
0143   /// Map generic virtual registers to their low-level type.
0144   VRegToTypeMap VRegToType;
0145 
0146   /// Keep track of the physical registers that are live in to the function.
0147   /// Live in values are typically arguments in registers.  LiveIn values are
0148   /// allowed to have virtual registers associated with them, stored in the
0149   /// second element.
0150   std::vector<std::pair<MCRegister, Register>> LiveIns;
0151 
0152 public:
0153   explicit MachineRegisterInfo(MachineFunction *MF);
0154   MachineRegisterInfo(const MachineRegisterInfo &) = delete;
0155   MachineRegisterInfo &operator=(const MachineRegisterInfo &) = delete;
0156 
0157   const TargetRegisterInfo *getTargetRegisterInfo() const {
0158     return MF->getSubtarget().getRegisterInfo();
0159   }
0160 
0161   void resetDelegate(Delegate *delegate) {
0162     // Ensure another delegate does not take over unless the current
0163     // delegate first unattaches itself.
0164     assert(TheDelegates.count(delegate) &&
0165            "Only an existing delegate can perform reset!");
0166     TheDelegates.erase(delegate);
0167   }
0168 
0169   void addDelegate(Delegate *delegate) {
0170     assert(delegate && !TheDelegates.count(delegate) &&
0171            "Attempted to add null delegate, or to change it without "
0172            "first resetting it!");
0173 
0174     TheDelegates.insert(delegate);
0175   }
0176 
0177   void noteNewVirtualRegister(Register Reg) {
0178     for (auto *TheDelegate : TheDelegates)
0179       TheDelegate->MRI_NoteNewVirtualRegister(Reg);
0180   }
0181 
0182   void noteCloneVirtualRegister(Register NewReg, Register SrcReg) {
0183     for (auto *TheDelegate : TheDelegates)
0184       TheDelegate->MRI_NoteCloneVirtualRegister(NewReg, SrcReg);
0185   }
0186 
0187   const MachineFunction &getMF() const { return *MF; }
0188 
0189   //===--------------------------------------------------------------------===//
0190   // Function State
0191   //===--------------------------------------------------------------------===//
0192 
0193   // isSSA - Returns true when the machine function is in SSA form. Early
0194   // passes require the machine function to be in SSA form where every virtual
0195   // register has a single defining instruction.
0196   //
0197   // The TwoAddressInstructionPass and PHIElimination passes take the machine
0198   // function out of SSA form when they introduce multiple defs per virtual
0199   // register.
0200   bool isSSA() const {
0201     return MF->getProperties().hasProperty(
0202         MachineFunctionProperties::Property::IsSSA);
0203   }
0204 
0205   // leaveSSA - Indicates that the machine function is no longer in SSA form.
0206   void leaveSSA() {
0207     MF->getProperties().reset(MachineFunctionProperties::Property::IsSSA);
0208   }
0209 
0210   /// tracksLiveness - Returns true when tracking register liveness accurately.
0211   /// (see MachineFUnctionProperties::Property description for details)
0212   bool tracksLiveness() const {
0213     return MF->getProperties().hasProperty(
0214         MachineFunctionProperties::Property::TracksLiveness);
0215   }
0216 
0217   /// invalidateLiveness - Indicates that register liveness is no longer being
0218   /// tracked accurately.
0219   ///
0220   /// This should be called by late passes that invalidate the liveness
0221   /// information.
0222   void invalidateLiveness() {
0223     MF->getProperties().reset(
0224         MachineFunctionProperties::Property::TracksLiveness);
0225   }
0226 
0227   /// Returns true if liveness for register class @p RC should be tracked at
0228   /// the subregister level.
0229   bool shouldTrackSubRegLiveness(const TargetRegisterClass &RC) const {
0230     return subRegLivenessEnabled() && RC.HasDisjunctSubRegs;
0231   }
0232   bool shouldTrackSubRegLiveness(Register VReg) const {
0233     assert(VReg.isVirtual() && "Must pass a VReg");
0234     const TargetRegisterClass *RC = getRegClassOrNull(VReg);
0235     return LLVM_LIKELY(RC) ? shouldTrackSubRegLiveness(*RC) : false;
0236   }
0237   bool subRegLivenessEnabled() const {
0238     return TracksSubRegLiveness;
0239   }
0240 
0241   //===--------------------------------------------------------------------===//
0242   // Register Info
0243   //===--------------------------------------------------------------------===//
0244 
0245   /// Returns true if the updated CSR list was initialized and false otherwise.
0246   bool isUpdatedCSRsInitialized() const { return IsUpdatedCSRsInitialized; }
0247 
0248   /// Disables the register from the list of CSRs.
0249   /// I.e. the register will not appear as part of the CSR mask.
0250   /// \see UpdatedCalleeSavedRegs.
0251   void disableCalleeSavedRegister(MCRegister Reg);
0252 
0253   /// Returns list of callee saved registers.
0254   /// The function returns the updated CSR list (after taking into account
0255   /// registers that are disabled from the CSR list).
0256   const MCPhysReg *getCalleeSavedRegs() const;
0257 
0258   /// Sets the updated Callee Saved Registers list.
0259   /// Notice that it will override ant previously disabled/saved CSRs.
0260   void setCalleeSavedRegs(ArrayRef<MCPhysReg> CSRs);
0261 
0262   // Strictly for use by MachineInstr.cpp.
0263   void addRegOperandToUseList(MachineOperand *MO);
0264 
0265   // Strictly for use by MachineInstr.cpp.
0266   void removeRegOperandFromUseList(MachineOperand *MO);
0267 
0268   // Strictly for use by MachineInstr.cpp.
0269   void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps);
0270 
0271   /// Verify the sanity of the use list for Reg.
0272   void verifyUseList(Register Reg) const;
0273 
0274   /// Verify the use list of all registers.
0275   void verifyUseLists() const;
0276 
0277   /// reg_begin/reg_end - Provide iteration support to walk over all definitions
0278   /// and uses of a register within the MachineFunction that corresponds to this
0279   /// MachineRegisterInfo object.
0280   template<bool Uses, bool Defs, bool SkipDebug,
0281            bool ByOperand, bool ByInstr, bool ByBundle>
0282   class defusechain_iterator;
0283   template<bool Uses, bool Defs, bool SkipDebug,
0284            bool ByOperand, bool ByInstr, bool ByBundle>
0285   class defusechain_instr_iterator;
0286 
0287   // Make it a friend so it can access getNextOperandForReg().
0288   template<bool, bool, bool, bool, bool, bool>
0289     friend class defusechain_iterator;
0290   template<bool, bool, bool, bool, bool, bool>
0291     friend class defusechain_instr_iterator;
0292 
0293   /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
0294   /// register.
0295   using reg_iterator =
0296       defusechain_iterator<true, true, false, true, false, false>;
0297   reg_iterator reg_begin(Register RegNo) const {
0298     return reg_iterator(getRegUseDefListHead(RegNo));
0299   }
0300   static reg_iterator reg_end() { return reg_iterator(nullptr); }
0301 
0302   inline iterator_range<reg_iterator> reg_operands(Register Reg) const {
0303     return make_range(reg_begin(Reg), reg_end());
0304   }
0305 
0306   /// reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses
0307   /// of the specified register, stepping by MachineInstr.
0308   using reg_instr_iterator =
0309       defusechain_instr_iterator<true, true, false, false, true, false>;
0310   reg_instr_iterator reg_instr_begin(Register RegNo) const {
0311     return reg_instr_iterator(getRegUseDefListHead(RegNo));
0312   }
0313   static reg_instr_iterator reg_instr_end() {
0314     return reg_instr_iterator(nullptr);
0315   }
0316 
0317   inline iterator_range<reg_instr_iterator>
0318   reg_instructions(Register Reg) const {
0319     return make_range(reg_instr_begin(Reg), reg_instr_end());
0320   }
0321 
0322   /// reg_bundle_iterator/reg_bundle_begin/reg_bundle_end - Walk all defs and uses
0323   /// of the specified register, stepping by bundle.
0324   using reg_bundle_iterator =
0325       defusechain_instr_iterator<true, true, false, false, false, true>;
0326   reg_bundle_iterator reg_bundle_begin(Register RegNo) const {
0327     return reg_bundle_iterator(getRegUseDefListHead(RegNo));
0328   }
0329   static reg_bundle_iterator reg_bundle_end() {
0330     return reg_bundle_iterator(nullptr);
0331   }
0332 
0333   inline iterator_range<reg_bundle_iterator> reg_bundles(Register Reg) const {
0334     return make_range(reg_bundle_begin(Reg), reg_bundle_end());
0335   }
0336 
0337   /// reg_empty - Return true if there are no instructions using or defining the
0338   /// specified register (it may be live-in).
0339   bool reg_empty(Register RegNo) const { return reg_begin(RegNo) == reg_end(); }
0340 
0341   /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
0342   /// of the specified register, skipping those marked as Debug.
0343   using reg_nodbg_iterator =
0344       defusechain_iterator<true, true, true, true, false, false>;
0345   reg_nodbg_iterator reg_nodbg_begin(Register RegNo) const {
0346     return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
0347   }
0348   static reg_nodbg_iterator reg_nodbg_end() {
0349     return reg_nodbg_iterator(nullptr);
0350   }
0351 
0352   inline iterator_range<reg_nodbg_iterator>
0353   reg_nodbg_operands(Register Reg) const {
0354     return make_range(reg_nodbg_begin(Reg), reg_nodbg_end());
0355   }
0356 
0357   /// reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk
0358   /// all defs and uses of the specified register, stepping by MachineInstr,
0359   /// skipping those marked as Debug.
0360   using reg_instr_nodbg_iterator =
0361       defusechain_instr_iterator<true, true, true, false, true, false>;
0362   reg_instr_nodbg_iterator reg_instr_nodbg_begin(Register RegNo) const {
0363     return reg_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
0364   }
0365   static reg_instr_nodbg_iterator reg_instr_nodbg_end() {
0366     return reg_instr_nodbg_iterator(nullptr);
0367   }
0368 
0369   inline iterator_range<reg_instr_nodbg_iterator>
0370   reg_nodbg_instructions(Register Reg) const {
0371     return make_range(reg_instr_nodbg_begin(Reg), reg_instr_nodbg_end());
0372   }
0373 
0374   /// reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk
0375   /// all defs and uses of the specified register, stepping by bundle,
0376   /// skipping those marked as Debug.
0377   using reg_bundle_nodbg_iterator =
0378       defusechain_instr_iterator<true, true, true, false, false, true>;
0379   reg_bundle_nodbg_iterator reg_bundle_nodbg_begin(Register RegNo) const {
0380     return reg_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
0381   }
0382   static reg_bundle_nodbg_iterator reg_bundle_nodbg_end() {
0383     return reg_bundle_nodbg_iterator(nullptr);
0384   }
0385 
0386   inline iterator_range<reg_bundle_nodbg_iterator>
0387   reg_nodbg_bundles(Register Reg) const {
0388     return make_range(reg_bundle_nodbg_begin(Reg), reg_bundle_nodbg_end());
0389   }
0390 
0391   /// reg_nodbg_empty - Return true if the only instructions using or defining
0392   /// Reg are Debug instructions.
0393   bool reg_nodbg_empty(Register RegNo) const {
0394     return reg_nodbg_begin(RegNo) == reg_nodbg_end();
0395   }
0396 
0397   /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
0398   using def_iterator =
0399       defusechain_iterator<false, true, false, true, false, false>;
0400   def_iterator def_begin(Register RegNo) const {
0401     return def_iterator(getRegUseDefListHead(RegNo));
0402   }
0403   static def_iterator def_end() { return def_iterator(nullptr); }
0404 
0405   inline iterator_range<def_iterator> def_operands(Register Reg) const {
0406     return make_range(def_begin(Reg), def_end());
0407   }
0408 
0409   /// def_instr_iterator/def_instr_begin/def_instr_end - Walk all defs of the
0410   /// specified register, stepping by MachineInst.
0411   using def_instr_iterator =
0412       defusechain_instr_iterator<false, true, false, false, true, false>;
0413   def_instr_iterator def_instr_begin(Register RegNo) const {
0414     return def_instr_iterator(getRegUseDefListHead(RegNo));
0415   }
0416   static def_instr_iterator def_instr_end() {
0417     return def_instr_iterator(nullptr);
0418   }
0419 
0420   inline iterator_range<def_instr_iterator>
0421   def_instructions(Register Reg) const {
0422     return make_range(def_instr_begin(Reg), def_instr_end());
0423   }
0424 
0425   /// def_bundle_iterator/def_bundle_begin/def_bundle_end - Walk all defs of the
0426   /// specified register, stepping by bundle.
0427   using def_bundle_iterator =
0428       defusechain_instr_iterator<false, true, false, false, false, true>;
0429   def_bundle_iterator def_bundle_begin(Register RegNo) const {
0430     return def_bundle_iterator(getRegUseDefListHead(RegNo));
0431   }
0432   static def_bundle_iterator def_bundle_end() {
0433     return def_bundle_iterator(nullptr);
0434   }
0435 
0436   inline iterator_range<def_bundle_iterator> def_bundles(Register Reg) const {
0437     return make_range(def_bundle_begin(Reg), def_bundle_end());
0438   }
0439 
0440   /// def_empty - Return true if there are no instructions defining the
0441   /// specified register (it may be live-in).
0442   bool def_empty(Register RegNo) const { return def_begin(RegNo) == def_end(); }
0443 
0444   StringRef getVRegName(Register Reg) const {
0445     return VReg2Name.inBounds(Reg) ? StringRef(VReg2Name[Reg]) : "";
0446   }
0447 
0448   void insertVRegByName(StringRef Name, Register Reg) {
0449     assert((Name.empty() || !VRegNames.contains(Name)) &&
0450            "Named VRegs Must be Unique.");
0451     if (!Name.empty()) {
0452       VRegNames.insert(Name);
0453       VReg2Name.grow(Reg);
0454       VReg2Name[Reg] = Name.str();
0455     }
0456   }
0457 
0458   /// Return true if there is exactly one operand defining the specified
0459   /// register.
0460   bool hasOneDef(Register RegNo) const {
0461     return hasSingleElement(def_operands(RegNo));
0462   }
0463 
0464   /// Returns the defining operand if there is exactly one operand defining the
0465   /// specified register, otherwise nullptr.
0466   MachineOperand *getOneDef(Register Reg) const {
0467     def_iterator DI = def_begin(Reg);
0468     if (DI == def_end()) // No defs.
0469       return nullptr;
0470 
0471     def_iterator OneDef = DI;
0472     if (++DI == def_end())
0473       return &*OneDef;
0474     return nullptr; // Multiple defs.
0475   }
0476 
0477   /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
0478   using use_iterator =
0479       defusechain_iterator<true, false, false, true, false, false>;
0480   use_iterator use_begin(Register RegNo) const {
0481     return use_iterator(getRegUseDefListHead(RegNo));
0482   }
0483   static use_iterator use_end() { return use_iterator(nullptr); }
0484 
0485   inline iterator_range<use_iterator> use_operands(Register Reg) const {
0486     return make_range(use_begin(Reg), use_end());
0487   }
0488 
0489   /// use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the
0490   /// specified register, stepping by MachineInstr.
0491   using use_instr_iterator =
0492       defusechain_instr_iterator<true, false, false, false, true, false>;
0493   use_instr_iterator use_instr_begin(Register RegNo) const {
0494     return use_instr_iterator(getRegUseDefListHead(RegNo));
0495   }
0496   static use_instr_iterator use_instr_end() {
0497     return use_instr_iterator(nullptr);
0498   }
0499 
0500   inline iterator_range<use_instr_iterator>
0501   use_instructions(Register Reg) const {
0502     return make_range(use_instr_begin(Reg), use_instr_end());
0503   }
0504 
0505   /// use_bundle_iterator/use_bundle_begin/use_bundle_end - Walk all uses of the
0506   /// specified register, stepping by bundle.
0507   using use_bundle_iterator =
0508       defusechain_instr_iterator<true, false, false, false, false, true>;
0509   use_bundle_iterator use_bundle_begin(Register RegNo) const {
0510     return use_bundle_iterator(getRegUseDefListHead(RegNo));
0511   }
0512   static use_bundle_iterator use_bundle_end() {
0513     return use_bundle_iterator(nullptr);
0514   }
0515 
0516   inline iterator_range<use_bundle_iterator> use_bundles(Register Reg) const {
0517     return make_range(use_bundle_begin(Reg), use_bundle_end());
0518   }
0519 
0520   /// use_empty - Return true if there are no instructions using the specified
0521   /// register.
0522   bool use_empty(Register RegNo) const { return use_begin(RegNo) == use_end(); }
0523 
0524   /// hasOneUse - Return true if there is exactly one instruction using the
0525   /// specified register.
0526   bool hasOneUse(Register RegNo) const {
0527     return hasSingleElement(use_operands(RegNo));
0528   }
0529 
0530   /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
0531   /// specified register, skipping those marked as Debug.
0532   using use_nodbg_iterator =
0533       defusechain_iterator<true, false, true, true, false, false>;
0534   use_nodbg_iterator use_nodbg_begin(Register RegNo) const {
0535     return use_nodbg_iterator(getRegUseDefListHead(RegNo));
0536   }
0537   static use_nodbg_iterator use_nodbg_end() {
0538     return use_nodbg_iterator(nullptr);
0539   }
0540 
0541   inline iterator_range<use_nodbg_iterator>
0542   use_nodbg_operands(Register Reg) const {
0543     return make_range(use_nodbg_begin(Reg), use_nodbg_end());
0544   }
0545 
0546   /// use_instr_nodbg_iterator/use_instr_nodbg_begin/use_instr_nodbg_end - Walk
0547   /// all uses of the specified register, stepping by MachineInstr, skipping
0548   /// those marked as Debug.
0549   using use_instr_nodbg_iterator =
0550       defusechain_instr_iterator<true, false, true, false, true, false>;
0551   use_instr_nodbg_iterator use_instr_nodbg_begin(Register RegNo) const {
0552     return use_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
0553   }
0554   static use_instr_nodbg_iterator use_instr_nodbg_end() {
0555     return use_instr_nodbg_iterator(nullptr);
0556   }
0557 
0558   inline iterator_range<use_instr_nodbg_iterator>
0559   use_nodbg_instructions(Register Reg) const {
0560     return make_range(use_instr_nodbg_begin(Reg), use_instr_nodbg_end());
0561   }
0562 
0563   /// use_bundle_nodbg_iterator/use_bundle_nodbg_begin/use_bundle_nodbg_end - Walk
0564   /// all uses of the specified register, stepping by bundle, skipping
0565   /// those marked as Debug.
0566   using use_bundle_nodbg_iterator =
0567       defusechain_instr_iterator<true, false, true, false, false, true>;
0568   use_bundle_nodbg_iterator use_bundle_nodbg_begin(Register RegNo) const {
0569     return use_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
0570   }
0571   static use_bundle_nodbg_iterator use_bundle_nodbg_end() {
0572     return use_bundle_nodbg_iterator(nullptr);
0573   }
0574 
0575   inline iterator_range<use_bundle_nodbg_iterator>
0576   use_nodbg_bundles(Register Reg) const {
0577     return make_range(use_bundle_nodbg_begin(Reg), use_bundle_nodbg_end());
0578   }
0579 
0580   /// use_nodbg_empty - Return true if there are no non-Debug instructions
0581   /// using the specified register.
0582   bool use_nodbg_empty(Register RegNo) const {
0583     return use_nodbg_begin(RegNo) == use_nodbg_end();
0584   }
0585 
0586   /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
0587   /// use of the specified register.
0588   bool hasOneNonDBGUse(Register RegNo) const;
0589 
0590   /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
0591   /// instruction using the specified register. Said instruction may have
0592   /// multiple uses.
0593   bool hasOneNonDBGUser(Register RegNo) const;
0594 
0595 
0596   /// hasAtMostUses - Return true if the given register has at most \p MaxUsers
0597   /// non-debug user instructions.
0598   bool hasAtMostUserInstrs(Register Reg, unsigned MaxUsers) const;
0599 
0600   /// replaceRegWith - Replace all instances of FromReg with ToReg in the
0601   /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
0602   /// except that it also changes any definitions of the register as well.
0603   ///
0604   /// Note that it is usually necessary to first constrain ToReg's register
0605   /// class and register bank to match the FromReg constraints using one of the
0606   /// methods:
0607   ///
0608   ///   constrainRegClass(ToReg, getRegClass(FromReg))
0609   ///   constrainRegAttrs(ToReg, FromReg)
0610   ///   RegisterBankInfo::constrainGenericRegister(ToReg,
0611   ///       *MRI.getRegClass(FromReg), MRI)
0612   ///
0613   /// These functions will return a falsy result if the virtual registers have
0614   /// incompatible constraints.
0615   ///
0616   /// Note that if ToReg is a physical register the function will replace and
0617   /// apply sub registers to ToReg in order to obtain a final/proper physical
0618   /// register.
0619   void replaceRegWith(Register FromReg, Register ToReg);
0620 
0621   /// getVRegDef - Return the machine instr that defines the specified virtual
0622   /// register or null if none is found.  This assumes that the code is in SSA
0623   /// form, so there should only be one definition.
0624   MachineInstr *getVRegDef(Register Reg) const;
0625 
0626   /// getUniqueVRegDef - Return the unique machine instr that defines the
0627   /// specified virtual register or null if none is found.  If there are
0628   /// multiple definitions or no definition, return null.
0629   MachineInstr *getUniqueVRegDef(Register Reg) const;
0630 
0631   /// clearKillFlags - Iterate over all the uses of the given register and
0632   /// clear the kill flag from the MachineOperand. This function is used by
0633   /// optimization passes which extend register lifetimes and need only
0634   /// preserve conservative kill flag information.
0635   void clearKillFlags(Register Reg) const;
0636 
0637   void dumpUses(Register RegNo) const;
0638 
0639   /// Returns true if PhysReg is unallocatable and constant throughout the
0640   /// function. Writing to a constant register has no effect.
0641   bool isConstantPhysReg(MCRegister PhysReg) const;
0642 
0643   /// Get an iterator over the pressure sets affected by the given physical or
0644   /// virtual register. If RegUnit is physical, it must be a register unit (from
0645   /// MCRegUnitIterator).
0646   PSetIterator getPressureSets(Register RegUnit) const;
0647 
0648   //===--------------------------------------------------------------------===//
0649   // Virtual Register Info
0650   //===--------------------------------------------------------------------===//
0651 
0652   /// Return the register class of the specified virtual register.
0653   /// This shouldn't be used directly unless \p Reg has a register class.
0654   /// \see getRegClassOrNull when this might happen.
0655   const TargetRegisterClass *getRegClass(Register Reg) const {
0656     assert(isa<const TargetRegisterClass *>(VRegInfo[Reg.id()].first) &&
0657            "Register class not set, wrong accessor");
0658     return cast<const TargetRegisterClass *>(VRegInfo[Reg.id()].first);
0659   }
0660 
0661   /// Return the register class of \p Reg, or null if Reg has not been assigned
0662   /// a register class yet.
0663   ///
0664   /// \note A null register class can only happen when these two
0665   /// conditions are met:
0666   /// 1. Generic virtual registers are created.
0667   /// 2. The machine function has not completely been through the
0668   ///    instruction selection process.
0669   /// None of this condition is possible without GlobalISel for now.
0670   /// In other words, if GlobalISel is not used or if the query happens after
0671   /// the select pass, using getRegClass is safe.
0672   const TargetRegisterClass *getRegClassOrNull(Register Reg) const {
0673     const RegClassOrRegBank &Val = VRegInfo[Reg].first;
0674     return dyn_cast_if_present<const TargetRegisterClass *>(Val);
0675   }
0676 
0677   /// Return the register bank of \p Reg.
0678   /// This shouldn't be used directly unless \p Reg has a register bank.
0679   const RegisterBank *getRegBank(Register Reg) const {
0680     return cast<const RegisterBank *>(VRegInfo[Reg.id()].first);
0681   }
0682 
0683   /// Return the register bank of \p Reg, or null if Reg has not been assigned
0684   /// a register bank or has been assigned a register class.
0685   /// \note It is possible to get the register bank from the register class via
0686   /// RegisterBankInfo::getRegBankFromRegClass.
0687   const RegisterBank *getRegBankOrNull(Register Reg) const {
0688     const RegClassOrRegBank &Val = VRegInfo[Reg].first;
0689     return dyn_cast_if_present<const RegisterBank *>(Val);
0690   }
0691 
0692   /// Return the register bank or register class of \p Reg.
0693   /// \note Before the register bank gets assigned (i.e., before the
0694   /// RegBankSelect pass) \p Reg may not have either.
0695   const RegClassOrRegBank &getRegClassOrRegBank(Register Reg) const {
0696     return VRegInfo[Reg].first;
0697   }
0698 
0699   /// setRegClass - Set the register class of the specified virtual register.
0700   void setRegClass(Register Reg, const TargetRegisterClass *RC);
0701 
0702   /// Set the register bank to \p RegBank for \p Reg.
0703   void setRegBank(Register Reg, const RegisterBank &RegBank);
0704 
0705   void setRegClassOrRegBank(Register Reg,
0706                             const RegClassOrRegBank &RCOrRB){
0707     VRegInfo[Reg].first = RCOrRB;
0708   }
0709 
0710   /// constrainRegClass - Constrain the register class of the specified virtual
0711   /// register to be a common subclass of RC and the current register class,
0712   /// but only if the new class has at least MinNumRegs registers.  Return the
0713   /// new register class, or NULL if no such class exists.
0714   /// This should only be used when the constraint is known to be trivial, like
0715   /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
0716   ///
0717   /// \note Assumes that the register has a register class assigned.
0718   /// Use RegisterBankInfo::constrainGenericRegister in GlobalISel's
0719   /// InstructionSelect pass and constrainRegAttrs in every other pass,
0720   /// including non-select passes of GlobalISel, instead.
0721   const TargetRegisterClass *constrainRegClass(Register Reg,
0722                                                const TargetRegisterClass *RC,
0723                                                unsigned MinNumRegs = 0);
0724 
0725   /// Constrain the register class or the register bank of the virtual register
0726   /// \p Reg (and low-level type) to be a common subclass or a common bank of
0727   /// both registers provided respectively (and a common low-level type). Do
0728   /// nothing if any of the attributes (classes, banks, or low-level types) of
0729   /// the registers are deemed incompatible, or if the resulting register will
0730   /// have a class smaller than before and of size less than \p MinNumRegs.
0731   /// Return true if such register attributes exist, false otherwise.
0732   ///
0733   /// \note Use this method instead of constrainRegClass and
0734   /// RegisterBankInfo::constrainGenericRegister everywhere but SelectionDAG
0735   /// ISel / FastISel and GlobalISel's InstructionSelect pass respectively.
0736   bool constrainRegAttrs(Register Reg, Register ConstrainingReg,
0737                          unsigned MinNumRegs = 0);
0738 
0739   /// recomputeRegClass - Try to find a legal super-class of Reg's register
0740   /// class that still satisfies the constraints from the instructions using
0741   /// Reg.  Returns true if Reg was upgraded.
0742   ///
0743   /// This method can be used after constraints have been removed from a
0744   /// virtual register, for example after removing instructions or splitting
0745   /// the live range.
0746   bool recomputeRegClass(Register Reg);
0747 
0748   /// createVirtualRegister - Create and return a new virtual register in the
0749   /// function with the specified register class.
0750   Register createVirtualRegister(const TargetRegisterClass *RegClass,
0751                                  StringRef Name = "");
0752 
0753   /// All attributes(register class or bank and low-level type) a virtual
0754   /// register can have.
0755   struct VRegAttrs {
0756     RegClassOrRegBank RCOrRB;
0757     LLT Ty;
0758   };
0759 
0760   /// Returns register class or bank and low level type of \p Reg. Always safe
0761   /// to use. Special values are returned when \p Reg does not have some of the
0762   /// attributes.
0763   VRegAttrs getVRegAttrs(Register Reg) const {
0764     return {getRegClassOrRegBank(Reg), getType(Reg)};
0765   }
0766 
0767   /// Create and return a new virtual register in the function with the
0768   /// specified register attributes(register class or bank and low level type).
0769   Register createVirtualRegister(VRegAttrs RegAttr, StringRef Name = "");
0770 
0771   /// Create and return a new virtual register in the function with the same
0772   /// attributes as the given register.
0773   Register cloneVirtualRegister(Register VReg, StringRef Name = "");
0774 
0775   /// Get the low-level type of \p Reg or LLT{} if Reg is not a generic
0776   /// (target independent) virtual register.
0777   LLT getType(Register Reg) const {
0778     if (Reg.isVirtual() && VRegToType.inBounds(Reg))
0779       return VRegToType[Reg];
0780     return LLT{};
0781   }
0782 
0783   /// Set the low-level type of \p VReg to \p Ty.
0784   void setType(Register VReg, LLT Ty);
0785 
0786   /// Create and return a new generic virtual register with low-level
0787   /// type \p Ty.
0788   Register createGenericVirtualRegister(LLT Ty, StringRef Name = "");
0789 
0790   /// Remove all types associated to virtual registers (after instruction
0791   /// selection and constraining of all generic virtual registers).
0792   void clearVirtRegTypes();
0793 
0794   /// Creates a new virtual register that has no register class, register bank
0795   /// or size assigned yet. This is only allowed to be used
0796   /// temporarily while constructing machine instructions. Most operations are
0797   /// undefined on an incomplete register until one of setRegClass(),
0798   /// setRegBank() or setSize() has been called on it.
0799   Register createIncompleteVirtualRegister(StringRef Name = "");
0800 
0801   /// getNumVirtRegs - Return the number of virtual registers created.
0802   unsigned getNumVirtRegs() const { return VRegInfo.size(); }
0803 
0804   /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
0805   void clearVirtRegs();
0806 
0807   /// setRegAllocationHint - Specify a register allocation hint for the
0808   /// specified virtual register. This is typically used by target, and in case
0809   /// of an earlier hint it will be overwritten.
0810   void setRegAllocationHint(Register VReg, unsigned Type, Register PrefReg) {
0811     assert(VReg.isVirtual());
0812     RegAllocHints.grow(Register::index2VirtReg(getNumVirtRegs()));
0813     RegAllocHints[VReg].first  = Type;
0814     RegAllocHints[VReg].second.clear();
0815     RegAllocHints[VReg].second.push_back(PrefReg);
0816   }
0817 
0818   /// addRegAllocationHint - Add a register allocation hint to the hints
0819   /// vector for VReg.
0820   void addRegAllocationHint(Register VReg, Register PrefReg) {
0821     assert(VReg.isVirtual());
0822     RegAllocHints.grow(Register::index2VirtReg(getNumVirtRegs()));
0823     RegAllocHints[VReg].second.push_back(PrefReg);
0824   }
0825 
0826   /// Specify the preferred (target independent) register allocation hint for
0827   /// the specified virtual register.
0828   void setSimpleHint(Register VReg, Register PrefReg) {
0829     setRegAllocationHint(VReg, /*Type=*/0, PrefReg);
0830   }
0831 
0832   void clearSimpleHint(Register VReg) {
0833     assert (!RegAllocHints[VReg].first &&
0834             "Expected to clear a non-target hint!");
0835     if (RegAllocHints.inBounds(VReg))
0836       RegAllocHints[VReg].second.clear();
0837   }
0838 
0839   /// getRegAllocationHint - Return the register allocation hint for the
0840   /// specified virtual register. If there are many hints, this returns the
0841   /// one with the greatest weight.
0842   std::pair<unsigned, Register> getRegAllocationHint(Register VReg) const {
0843     assert(VReg.isVirtual());
0844     if (!RegAllocHints.inBounds(VReg))
0845       return {0, Register()};
0846     Register BestHint = (RegAllocHints[VReg.id()].second.size() ?
0847                          RegAllocHints[VReg.id()].second[0] : Register());
0848     return {RegAllocHints[VReg.id()].first, BestHint};
0849   }
0850 
0851   /// getSimpleHint - same as getRegAllocationHint except it will only return
0852   /// a target independent hint.
0853   Register getSimpleHint(Register VReg) const {
0854     assert(VReg.isVirtual());
0855     std::pair<unsigned, Register> Hint = getRegAllocationHint(VReg);
0856     return Hint.first ? Register() : Hint.second;
0857   }
0858 
0859   /// getRegAllocationHints - Return a reference to the vector of all
0860   /// register allocation hints for VReg.
0861   const std::pair<unsigned, SmallVector<Register, 4>> *
0862   getRegAllocationHints(Register VReg) const {
0863     assert(VReg.isVirtual());
0864     return RegAllocHints.inBounds(VReg) ? &RegAllocHints[VReg] : nullptr;
0865   }
0866 
0867   /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
0868   /// specified register as undefined which causes the DBG_VALUE to be
0869   /// deleted during LiveDebugVariables analysis.
0870   void markUsesInDebugValueAsUndef(Register Reg) const;
0871 
0872   /// updateDbgUsersToReg - Update a collection of debug instructions
0873   /// to refer to the designated register.
0874   void updateDbgUsersToReg(MCRegister OldReg, MCRegister NewReg,
0875                            ArrayRef<MachineInstr *> Users) const {
0876     // If this operand is a register, check whether it overlaps with OldReg.
0877     // If it does, replace with NewReg.
0878     auto UpdateOp = [this, &NewReg, &OldReg](MachineOperand &Op) {
0879       if (Op.isReg() &&
0880           getTargetRegisterInfo()->regsOverlap(Op.getReg(), OldReg))
0881         Op.setReg(NewReg);
0882     };
0883 
0884     // Iterate through (possibly several) operands to DBG_VALUEs and update
0885     // each. For DBG_PHIs, only one operand will be present.
0886     for (MachineInstr *MI : Users) {
0887       if (MI->isDebugValue()) {
0888         for (auto &Op : MI->debug_operands())
0889           UpdateOp(Op);
0890         assert(MI->hasDebugOperandForReg(NewReg) &&
0891                "Expected debug value to have some overlap with OldReg");
0892       } else if (MI->isDebugPHI()) {
0893         UpdateOp(MI->getOperand(0));
0894       } else {
0895         llvm_unreachable("Non-DBG_VALUE, Non-DBG_PHI debug instr updated");
0896       }
0897     }
0898   }
0899 
0900   /// Return true if the specified register is modified in this function.
0901   /// This checks that no defining machine operands exist for the register or
0902   /// any of its aliases. Definitions found on functions marked noreturn are
0903   /// ignored, to consider them pass 'true' for optional parameter
0904   /// SkipNoReturnDef. The register is also considered modified when it is set
0905   /// in the UsedPhysRegMask.
0906   bool isPhysRegModified(MCRegister PhysReg, bool SkipNoReturnDef = false) const;
0907 
0908   /// Return true if the specified register is modified or read in this
0909   /// function. This checks that no machine operands exist for the register or
0910   /// any of its aliases. If SkipRegMaskTest is false, the register is
0911   /// considered used when it is set in the UsedPhysRegMask.
0912   bool isPhysRegUsed(MCRegister PhysReg, bool SkipRegMaskTest = false) const;
0913 
0914   /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
0915   /// This corresponds to the bit mask attached to register mask operands.
0916   void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
0917     UsedPhysRegMask.setBitsNotInMask(RegMask);
0918   }
0919 
0920   const BitVector &getUsedPhysRegsMask() const { return UsedPhysRegMask; }
0921 
0922   //===--------------------------------------------------------------------===//
0923   // Reserved Register Info
0924   //===--------------------------------------------------------------------===//
0925   //
0926   // The set of reserved registers must be invariant during register
0927   // allocation.  For example, the target cannot suddenly decide it needs a
0928   // frame pointer when the register allocator has already used the frame
0929   // pointer register for something else.
0930   //
0931   // These methods can be used by target hooks like hasFP() to avoid changing
0932   // the reserved register set during register allocation.
0933 
0934   /// freezeReservedRegs - Called by the register allocator to freeze the set
0935   /// of reserved registers before allocation begins.
0936   void freezeReservedRegs();
0937 
0938   /// reserveReg -- Mark a register as reserved so checks like isAllocatable 
0939   /// will not suggest using it. This should not be used during the middle
0940   /// of a function walk, or when liveness info is available.
0941   void reserveReg(MCRegister PhysReg, const TargetRegisterInfo *TRI) {
0942     assert(reservedRegsFrozen() &&
0943            "Reserved registers haven't been frozen yet. ");
0944     MCRegAliasIterator R(PhysReg, TRI, true);
0945 
0946     for (; R.isValid(); ++R)
0947       ReservedRegs.set((*R).id());
0948   }
0949 
0950   /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
0951   /// to ensure the set of reserved registers stays constant.
0952   bool reservedRegsFrozen() const {
0953     return !ReservedRegs.empty();
0954   }
0955 
0956   /// canReserveReg - Returns true if PhysReg can be used as a reserved
0957   /// register.  Any register can be reserved before freezeReservedRegs() is
0958   /// called.
0959   bool canReserveReg(MCRegister PhysReg) const {
0960     return !reservedRegsFrozen() || ReservedRegs.test(PhysReg.id());
0961   }
0962 
0963   /// getReservedRegs - Returns a reference to the frozen set of reserved
0964   /// registers. This method should always be preferred to calling
0965   /// TRI::getReservedRegs() when possible.
0966   const BitVector &getReservedRegs() const {
0967     assert(reservedRegsFrozen() &&
0968            "Reserved registers haven't been frozen yet. "
0969            "Use TRI::getReservedRegs().");
0970     return ReservedRegs;
0971   }
0972 
0973   /// isReserved - Returns true when PhysReg is a reserved register.
0974   ///
0975   /// Reserved registers may belong to an allocatable register class, but the
0976   /// target has explicitly requested that they are not used.
0977   bool isReserved(MCRegister PhysReg) const {
0978     return getReservedRegs().test(PhysReg.id());
0979   }
0980 
0981   /// Returns true when the given register unit is considered reserved.
0982   ///
0983   /// Register units are considered reserved when for at least one of their
0984   /// root registers, the root register and all super registers are reserved.
0985   /// This currently iterates the register hierarchy and may be slower than
0986   /// expected.
0987   bool isReservedRegUnit(unsigned Unit) const;
0988 
0989   /// isAllocatable - Returns true when PhysReg belongs to an allocatable
0990   /// register class and it hasn't been reserved.
0991   ///
0992   /// Allocatable registers may show up in the allocation order of some virtual
0993   /// register, so a register allocator needs to track its liveness and
0994   /// availability.
0995   bool isAllocatable(MCRegister PhysReg) const {
0996     return getTargetRegisterInfo()->isInAllocatableClass(PhysReg) &&
0997       !isReserved(PhysReg);
0998   }
0999 
1000   //===--------------------------------------------------------------------===//
1001   // LiveIn Management
1002   //===--------------------------------------------------------------------===//
1003 
1004   /// addLiveIn - Add the specified register as a live-in.  Note that it
1005   /// is an error to add the same register to the same set more than once.
1006   void addLiveIn(MCRegister Reg, Register vreg = Register()) {
1007     LiveIns.push_back(std::make_pair(Reg, vreg));
1008   }
1009 
1010   // Iteration support for the live-ins set.  It's kept in sorted order
1011   // by register number.
1012   using livein_iterator =
1013       std::vector<std::pair<MCRegister,Register>>::const_iterator;
1014   livein_iterator livein_begin() const { return LiveIns.begin(); }
1015   livein_iterator livein_end()   const { return LiveIns.end(); }
1016   bool            livein_empty() const { return LiveIns.empty(); }
1017 
1018   ArrayRef<std::pair<MCRegister, Register>> liveins() const {
1019     return LiveIns;
1020   }
1021 
1022   bool isLiveIn(Register Reg) const;
1023 
1024   /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
1025   /// corresponding live-in physical register.
1026   MCRegister getLiveInPhysReg(Register VReg) const;
1027 
1028   /// getLiveInVirtReg - If PReg is a live-in physical register, return the
1029   /// corresponding live-in virtual register.
1030   Register getLiveInVirtReg(MCRegister PReg) const;
1031 
1032   /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
1033   /// into the given entry block.
1034   void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
1035                         const TargetRegisterInfo &TRI,
1036                         const TargetInstrInfo &TII);
1037 
1038   /// Returns a mask covering all bits that can appear in lane masks of
1039   /// subregisters of the virtual register @p Reg.
1040   LaneBitmask getMaxLaneMaskForVReg(Register Reg) const;
1041 
1042   /// defusechain_iterator - This class provides iterator support for machine
1043   /// operands in the function that use or define a specific register.  If
1044   /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
1045   /// returns defs.  If neither are true then you are silly and it always
1046   /// returns end().  If SkipDebug is true it skips uses marked Debug
1047   /// when incrementing.
1048   template <bool ReturnUses, bool ReturnDefs, bool SkipDebug, bool ByOperand,
1049             bool ByInstr, bool ByBundle>
1050   class defusechain_iterator {
1051     friend class MachineRegisterInfo;
1052 
1053   public:
1054     using iterator_category = std::forward_iterator_tag;
1055     using value_type = MachineOperand;
1056     using difference_type = std::ptrdiff_t;
1057     using pointer = value_type *;
1058     using reference = value_type &;
1059 
1060   private:
1061     MachineOperand *Op = nullptr;
1062 
1063     explicit defusechain_iterator(MachineOperand *op) : Op(op) {
1064       // If the first node isn't one we're interested in, advance to one that
1065       // we are interested in.
1066       if (op) {
1067         if ((!ReturnUses && op->isUse()) ||
1068             (!ReturnDefs && op->isDef()) ||
1069             (SkipDebug && op->isDebug()))
1070           advance();
1071       }
1072     }
1073 
1074     void advance() {
1075       assert(Op && "Cannot increment end iterator!");
1076       Op = getNextOperandForReg(Op);
1077 
1078       // All defs come before the uses, so stop def_iterator early.
1079       if (!ReturnUses) {
1080         if (Op) {
1081           if (Op->isUse())
1082             Op = nullptr;
1083           else
1084             assert(!Op->isDebug() && "Can't have debug defs");
1085         }
1086       } else {
1087         // If this is an operand we don't care about, skip it.
1088         while (Op && ((!ReturnDefs && Op->isDef()) ||
1089                       (SkipDebug && Op->isDebug())))
1090           Op = getNextOperandForReg(Op);
1091       }
1092     }
1093 
1094   public:
1095     defusechain_iterator() = default;
1096 
1097     bool operator==(const defusechain_iterator &x) const {
1098       return Op == x.Op;
1099     }
1100     bool operator!=(const defusechain_iterator &x) const {
1101       return !operator==(x);
1102     }
1103 
1104     // Iterator traversal: forward iteration only
1105     defusechain_iterator &operator++() {          // Preincrement
1106       assert(Op && "Cannot increment end iterator!");
1107       if (ByOperand)
1108         advance();
1109       else if (ByInstr) {
1110         MachineInstr *P = Op->getParent();
1111         do {
1112           advance();
1113         } while (Op && Op->getParent() == P);
1114       } else if (ByBundle) {
1115         MachineBasicBlock::instr_iterator P =
1116             getBundleStart(Op->getParent()->getIterator());
1117         do {
1118           advance();
1119         } while (Op && getBundleStart(Op->getParent()->getIterator()) == P);
1120       }
1121 
1122       return *this;
1123     }
1124     defusechain_iterator operator++(int) {        // Postincrement
1125       defusechain_iterator tmp = *this; ++*this; return tmp;
1126     }
1127 
1128     /// getOperandNo - Return the operand # of this MachineOperand in its
1129     /// MachineInstr.
1130     unsigned getOperandNo() const {
1131       assert(Op && "Cannot dereference end iterator!");
1132       return Op - &Op->getParent()->getOperand(0);
1133     }
1134 
1135     // Retrieve a reference to the current operand.
1136     MachineOperand &operator*() const {
1137       assert(Op && "Cannot dereference end iterator!");
1138       return *Op;
1139     }
1140 
1141     MachineOperand *operator->() const {
1142       assert(Op && "Cannot dereference end iterator!");
1143       return Op;
1144     }
1145   };
1146 
1147   /// defusechain_iterator - This class provides iterator support for machine
1148   /// operands in the function that use or define a specific register.  If
1149   /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
1150   /// returns defs.  If neither are true then you are silly and it always
1151   /// returns end().  If SkipDebug is true it skips uses marked Debug
1152   /// when incrementing.
1153   template <bool ReturnUses, bool ReturnDefs, bool SkipDebug, bool ByOperand,
1154             bool ByInstr, bool ByBundle>
1155   class defusechain_instr_iterator {
1156     friend class MachineRegisterInfo;
1157 
1158   public:
1159     using iterator_category = std::forward_iterator_tag;
1160     using value_type = MachineInstr;
1161     using difference_type = std::ptrdiff_t;
1162     using pointer = value_type *;
1163     using reference = value_type &;
1164 
1165   private:
1166     MachineOperand *Op = nullptr;
1167 
1168     explicit defusechain_instr_iterator(MachineOperand *op) : Op(op) {
1169       // If the first node isn't one we're interested in, advance to one that
1170       // we are interested in.
1171       if (op) {
1172         if ((!ReturnUses && op->isUse()) ||
1173             (!ReturnDefs && op->isDef()) ||
1174             (SkipDebug && op->isDebug()))
1175           advance();
1176       }
1177     }
1178 
1179     void advance() {
1180       assert(Op && "Cannot increment end iterator!");
1181       Op = getNextOperandForReg(Op);
1182 
1183       // All defs come before the uses, so stop def_iterator early.
1184       if (!ReturnUses) {
1185         if (Op) {
1186           if (Op->isUse())
1187             Op = nullptr;
1188           else
1189             assert(!Op->isDebug() && "Can't have debug defs");
1190         }
1191       } else {
1192         // If this is an operand we don't care about, skip it.
1193         while (Op && ((!ReturnDefs && Op->isDef()) ||
1194                       (SkipDebug && Op->isDebug())))
1195           Op = getNextOperandForReg(Op);
1196       }
1197     }
1198 
1199   public:
1200     defusechain_instr_iterator() = default;
1201 
1202     bool operator==(const defusechain_instr_iterator &x) const {
1203       return Op == x.Op;
1204     }
1205     bool operator!=(const defusechain_instr_iterator &x) const {
1206       return !operator==(x);
1207     }
1208 
1209     // Iterator traversal: forward iteration only
1210     defusechain_instr_iterator &operator++() {          // Preincrement
1211       assert(Op && "Cannot increment end iterator!");
1212       if (ByOperand)
1213         advance();
1214       else if (ByInstr) {
1215         MachineInstr *P = Op->getParent();
1216         do {
1217           advance();
1218         } while (Op && Op->getParent() == P);
1219       } else if (ByBundle) {
1220         MachineBasicBlock::instr_iterator P =
1221             getBundleStart(Op->getParent()->getIterator());
1222         do {
1223           advance();
1224         } while (Op && getBundleStart(Op->getParent()->getIterator()) == P);
1225       }
1226 
1227       return *this;
1228     }
1229     defusechain_instr_iterator operator++(int) {        // Postincrement
1230       defusechain_instr_iterator tmp = *this; ++*this; return tmp;
1231     }
1232 
1233     // Retrieve a reference to the current operand.
1234     MachineInstr &operator*() const {
1235       assert(Op && "Cannot dereference end iterator!");
1236       if (ByBundle)
1237         return *getBundleStart(Op->getParent()->getIterator());
1238       return *Op->getParent();
1239     }
1240 
1241     MachineInstr *operator->() const { return &operator*(); }
1242   };
1243 };
1244 
1245 /// Iterate over the pressure sets affected by the given physical or virtual
1246 /// register. If Reg is physical, it must be a register unit (from
1247 /// MCRegUnitIterator).
1248 class PSetIterator {
1249   const int *PSet = nullptr;
1250   unsigned Weight = 0;
1251 
1252 public:
1253   PSetIterator() = default;
1254 
1255   PSetIterator(Register RegUnit, const MachineRegisterInfo *MRI) {
1256     const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
1257     if (RegUnit.isVirtual()) {
1258       const TargetRegisterClass *RC = MRI->getRegClass(RegUnit);
1259       PSet = TRI->getRegClassPressureSets(RC);
1260       Weight = TRI->getRegClassWeight(RC).RegWeight;
1261     } else {
1262       PSet = TRI->getRegUnitPressureSets(RegUnit);
1263       Weight = TRI->getRegUnitWeight(RegUnit);
1264     }
1265     if (*PSet == -1)
1266       PSet = nullptr;
1267   }
1268 
1269   bool isValid() const { return PSet; }
1270 
1271   unsigned getWeight() const { return Weight; }
1272 
1273   unsigned operator*() const { return *PSet; }
1274 
1275   void operator++() {
1276     assert(isValid() && "Invalid PSetIterator.");
1277     ++PSet;
1278     if (*PSet == -1)
1279       PSet = nullptr;
1280   }
1281 };
1282 
1283 inline PSetIterator
1284 MachineRegisterInfo::getPressureSets(Register RegUnit) const {
1285   return PSetIterator(RegUnit, this);
1286 }
1287 
1288 } // end namespace llvm
1289 
1290 #endif // LLVM_CODEGEN_MACHINEREGISTERINFO_H