Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:14

0001 //===- MC/MCRegisterInfo.h - Target Register Description --------*- 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 describes an abstract interface used to get information about a
0010 // target machines register file.  This information is used for a variety of
0011 // purposed, especially register allocation.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_MC_MCREGISTERINFO_H
0016 #define LLVM_MC_MCREGISTERINFO_H
0017 
0018 #include "llvm/ADT/DenseMap.h"
0019 #include "llvm/ADT/iterator.h"
0020 #include "llvm/ADT/iterator_range.h"
0021 #include "llvm/MC/LaneBitmask.h"
0022 #include "llvm/MC/MCRegister.h"
0023 #include <cassert>
0024 #include <cstdint>
0025 #include <iterator>
0026 #include <utility>
0027 
0028 namespace llvm {
0029 
0030 class MCRegUnitIterator;
0031 class MCSubRegIterator;
0032 class MCSuperRegIterator;
0033 
0034 /// MCRegisterClass - Base class of TargetRegisterClass.
0035 class MCRegisterClass {
0036 public:
0037   using iterator = const MCPhysReg*;
0038   using const_iterator = const MCPhysReg*;
0039 
0040   const iterator RegsBegin;
0041   const uint8_t *const RegSet;
0042   const uint32_t NameIdx;
0043   const uint16_t RegsSize;
0044   const uint16_t RegSetSize;
0045   const uint16_t ID;
0046   const uint16_t RegSizeInBits;
0047   const int8_t CopyCost;
0048   const bool Allocatable;
0049   const bool BaseClass;
0050 
0051   /// getID() - Return the register class ID number.
0052   ///
0053   unsigned getID() const { return ID; }
0054 
0055   /// begin/end - Return all of the registers in this class.
0056   ///
0057   iterator       begin() const { return RegsBegin; }
0058   iterator         end() const { return RegsBegin + RegsSize; }
0059 
0060   /// getNumRegs - Return the number of registers in this class.
0061   ///
0062   unsigned getNumRegs() const { return RegsSize; }
0063 
0064   /// getRegister - Return the specified register in the class.
0065   ///
0066   unsigned getRegister(unsigned i) const {
0067     assert(i < getNumRegs() && "Register number out of range!");
0068     return RegsBegin[i];
0069   }
0070 
0071   /// contains - Return true if the specified register is included in this
0072   /// register class.  This does not include virtual registers.
0073   bool contains(MCRegister Reg) const {
0074     unsigned RegNo = Reg.id();
0075     unsigned InByte = RegNo % 8;
0076     unsigned Byte = RegNo / 8;
0077     if (Byte >= RegSetSize)
0078       return false;
0079     return (RegSet[Byte] & (1 << InByte)) != 0;
0080   }
0081 
0082   /// contains - Return true if both registers are in this class.
0083   bool contains(MCRegister Reg1, MCRegister Reg2) const {
0084     return contains(Reg1) && contains(Reg2);
0085   }
0086 
0087   /// Return the size of the physical register in bits if we are able to
0088   /// determine it. This always returns zero for registers of targets that use
0089   /// HW modes, as we need more information to determine the size of registers
0090   /// in such cases. Use TargetRegisterInfo to cover them.
0091   unsigned getSizeInBits() const { return RegSizeInBits; }
0092 
0093   /// getCopyCost - Return the cost of copying a value between two registers in
0094   /// this class. A negative number means the register class is very expensive
0095   /// to copy e.g. status flag register classes.
0096   int getCopyCost() const { return CopyCost; }
0097 
0098   /// isAllocatable - Return true if this register class may be used to create
0099   /// virtual registers.
0100   bool isAllocatable() const { return Allocatable; }
0101 
0102   /// Return true if this register class has a defined BaseClassOrder.
0103   bool isBaseClass() const { return BaseClass; }
0104 };
0105 
0106 /// MCRegisterDesc - This record contains information about a particular
0107 /// register.  The SubRegs field is a zero terminated array of registers that
0108 /// are sub-registers of the specific register, e.g. AL, AH are sub-registers
0109 /// of AX. The SuperRegs field is a zero terminated array of registers that are
0110 /// super-registers of the specific register, e.g. RAX, EAX, are
0111 /// super-registers of AX.
0112 ///
0113 struct MCRegisterDesc {
0114   uint32_t Name;      // Printable name for the reg (for debugging)
0115   uint32_t SubRegs;   // Sub-register set, described above
0116   uint32_t SuperRegs; // Super-register set, described above
0117 
0118   // Offset into MCRI::SubRegIndices of a list of sub-register indices for each
0119   // sub-register in SubRegs.
0120   uint32_t SubRegIndices;
0121 
0122   // Points to the list of register units. The low bits hold the first regunit
0123   // number, the high bits hold an offset into DiffLists. See MCRegUnitIterator.
0124   uint32_t RegUnits;
0125 
0126   /// Index into list with lane mask sequences. The sequence contains a lanemask
0127   /// for every register unit.
0128   uint16_t RegUnitLaneMasks;
0129 
0130   // Is true for constant registers.
0131   bool IsConstant;
0132 
0133   // Is true for artificial registers.
0134   bool IsArtificial;
0135 };
0136 
0137 /// MCRegisterInfo base class - We assume that the target defines a static
0138 /// array of MCRegisterDesc objects that represent all of the machine
0139 /// registers that the target has.  As such, we simply have to track a pointer
0140 /// to this array so that we can turn register number into a register
0141 /// descriptor.
0142 ///
0143 /// Note this class is designed to be a base class of TargetRegisterInfo, which
0144 /// is the interface used by codegen. However, specific targets *should never*
0145 /// specialize this class. MCRegisterInfo should only contain getters to access
0146 /// TableGen generated physical register data. It must not be extended with
0147 /// virtual methods.
0148 ///
0149 class MCRegisterInfo {
0150 public:
0151   using regclass_iterator = const MCRegisterClass *;
0152 
0153   /// DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be
0154   /// performed with a binary search.
0155   struct DwarfLLVMRegPair {
0156     unsigned FromReg;
0157     unsigned ToReg;
0158 
0159     bool operator<(DwarfLLVMRegPair RHS) const { return FromReg < RHS.FromReg; }
0160   };
0161 
0162 private:
0163   const MCRegisterDesc *Desc;                 // Pointer to the descriptor array
0164   unsigned NumRegs;                           // Number of entries in the array
0165   MCRegister RAReg;                           // Return address register
0166   MCRegister PCReg;                           // Program counter register
0167   const MCRegisterClass *Classes;             // Pointer to the regclass array
0168   unsigned NumClasses;                        // Number of entries in the array
0169   unsigned NumRegUnits;                       // Number of regunits.
0170   const MCPhysReg (*RegUnitRoots)[2];         // Pointer to regunit root table.
0171   const int16_t *DiffLists;                   // Pointer to the difflists array
0172   const LaneBitmask *RegUnitMaskSequences;    // Pointer to lane mask sequences
0173                                               // for register units.
0174   const char *RegStrings;                     // Pointer to the string table.
0175   const char *RegClassStrings;                // Pointer to the class strings.
0176   const uint16_t *SubRegIndices;              // Pointer to the subreg lookup
0177                                               // array.
0178   unsigned NumSubRegIndices;                  // Number of subreg indices.
0179   const uint16_t *RegEncodingTable;           // Pointer to array of register
0180                                               // encodings.
0181 
0182   unsigned L2DwarfRegsSize;
0183   unsigned EHL2DwarfRegsSize;
0184   unsigned Dwarf2LRegsSize;
0185   unsigned EHDwarf2LRegsSize;
0186   const DwarfLLVMRegPair *L2DwarfRegs;        // LLVM to Dwarf regs mapping
0187   const DwarfLLVMRegPair *EHL2DwarfRegs;      // LLVM to Dwarf regs mapping EH
0188   const DwarfLLVMRegPair *Dwarf2LRegs;        // Dwarf to LLVM regs mapping
0189   const DwarfLLVMRegPair *EHDwarf2LRegs;      // Dwarf to LLVM regs mapping EH
0190   DenseMap<MCRegister, int> L2SEHRegs;        // LLVM to SEH regs mapping
0191   DenseMap<MCRegister, int> L2CVRegs;         // LLVM to CV regs mapping
0192 
0193   mutable std::vector<std::vector<MCPhysReg>> RegAliasesCache;
0194   ArrayRef<MCPhysReg> getCachedAliasesOf(MCRegister R) const;
0195 
0196   /// Iterator class that can traverse the differentially encoded values in
0197   /// DiffLists. Don't use this class directly, use one of the adaptors below.
0198   class DiffListIterator
0199       : public iterator_facade_base<DiffListIterator, std::forward_iterator_tag,
0200                                     unsigned> {
0201     unsigned Val = 0;
0202     const int16_t *List = nullptr;
0203 
0204   public:
0205     /// Constructs an invalid iterator, which is also the end iterator.
0206     /// Call init() to point to something useful.
0207     DiffListIterator() = default;
0208 
0209     /// Point the iterator to InitVal, decoding subsequent values from DiffList.
0210     void init(unsigned InitVal, const int16_t *DiffList) {
0211       Val = InitVal;
0212       List = DiffList;
0213     }
0214 
0215     /// Returns true if this iterator is not yet at the end.
0216     bool isValid() const { return List; }
0217 
0218     /// Dereference the iterator to get the value at the current position.
0219     const unsigned &operator*() const { return Val; }
0220 
0221     using DiffListIterator::iterator_facade_base::operator++;
0222     /// Pre-increment to move to the next position.
0223     DiffListIterator &operator++() {
0224       assert(isValid() && "Cannot move off the end of the list.");
0225       int16_t D = *List++;
0226       Val += D;
0227       // The end of the list is encoded as a 0 differential.
0228       if (!D)
0229         List = nullptr;
0230       return *this;
0231     }
0232 
0233     bool operator==(const DiffListIterator &Other) const {
0234       return List == Other.List;
0235     }
0236   };
0237 
0238 public:
0239   /// Return an iterator range over all sub-registers of \p Reg, excluding \p
0240   /// Reg.
0241   iterator_range<MCSubRegIterator> subregs(MCRegister Reg) const;
0242 
0243   /// Return an iterator range over all sub-registers of \p Reg, including \p
0244   /// Reg.
0245   iterator_range<MCSubRegIterator> subregs_inclusive(MCRegister Reg) const;
0246 
0247   /// Return an iterator range over all super-registers of \p Reg, excluding \p
0248   /// Reg.
0249   iterator_range<MCSuperRegIterator> superregs(MCRegister Reg) const;
0250 
0251   /// Return an iterator range over all super-registers of \p Reg, including \p
0252   /// Reg.
0253   iterator_range<MCSuperRegIterator> superregs_inclusive(MCRegister Reg) const;
0254 
0255   /// Return an iterator range over all sub- and super-registers of \p Reg,
0256   /// including \p Reg.
0257   detail::concat_range<const MCPhysReg, iterator_range<MCSubRegIterator>,
0258                        iterator_range<MCSuperRegIterator>>
0259   sub_and_superregs_inclusive(MCRegister Reg) const;
0260 
0261   /// Returns an iterator range over all regunits for \p Reg.
0262   iterator_range<MCRegUnitIterator> regunits(MCRegister Reg) const;
0263 
0264   // These iterators are allowed to sub-class DiffListIterator and access
0265   // internal list pointers.
0266   friend class MCSubRegIterator;
0267   friend class MCSubRegIndexIterator;
0268   friend class MCSuperRegIterator;
0269   friend class MCRegUnitIterator;
0270   friend class MCRegUnitMaskIterator;
0271   friend class MCRegUnitRootIterator;
0272   friend class MCRegAliasIterator;
0273 
0274   virtual ~MCRegisterInfo() {}
0275 
0276   /// Initialize MCRegisterInfo, called by TableGen
0277   /// auto-generated routines. *DO NOT USE*.
0278   void InitMCRegisterInfo(const MCRegisterDesc *D, unsigned NR, unsigned RA,
0279                           unsigned PC, const MCRegisterClass *C, unsigned NC,
0280                           const MCPhysReg (*RURoots)[2], unsigned NRU,
0281                           const int16_t *DL, const LaneBitmask *RUMS,
0282                           const char *Strings, const char *ClassStrings,
0283                           const uint16_t *SubIndices, unsigned NumIndices,
0284                           const uint16_t *RET) {
0285     Desc = D;
0286     NumRegs = NR;
0287     RAReg = RA;
0288     PCReg = PC;
0289     Classes = C;
0290     DiffLists = DL;
0291     RegUnitMaskSequences = RUMS;
0292     RegStrings = Strings;
0293     RegClassStrings = ClassStrings;
0294     NumClasses = NC;
0295     RegUnitRoots = RURoots;
0296     NumRegUnits = NRU;
0297     SubRegIndices = SubIndices;
0298     NumSubRegIndices = NumIndices;
0299     RegEncodingTable = RET;
0300 
0301     // Initialize DWARF register mapping variables
0302     EHL2DwarfRegs = nullptr;
0303     EHL2DwarfRegsSize = 0;
0304     L2DwarfRegs = nullptr;
0305     L2DwarfRegsSize = 0;
0306     EHDwarf2LRegs = nullptr;
0307     EHDwarf2LRegsSize = 0;
0308     Dwarf2LRegs = nullptr;
0309     Dwarf2LRegsSize = 0;
0310 
0311     RegAliasesCache.resize(NumRegs);
0312   }
0313 
0314   /// Used to initialize LLVM register to Dwarf
0315   /// register number mapping. Called by TableGen auto-generated routines.
0316   /// *DO NOT USE*.
0317   void mapLLVMRegsToDwarfRegs(const DwarfLLVMRegPair *Map, unsigned Size,
0318                               bool isEH) {
0319     if (isEH) {
0320       EHL2DwarfRegs = Map;
0321       EHL2DwarfRegsSize = Size;
0322     } else {
0323       L2DwarfRegs = Map;
0324       L2DwarfRegsSize = Size;
0325     }
0326   }
0327 
0328   /// Used to initialize Dwarf register to LLVM
0329   /// register number mapping. Called by TableGen auto-generated routines.
0330   /// *DO NOT USE*.
0331   void mapDwarfRegsToLLVMRegs(const DwarfLLVMRegPair *Map, unsigned Size,
0332                               bool isEH) {
0333     if (isEH) {
0334       EHDwarf2LRegs = Map;
0335       EHDwarf2LRegsSize = Size;
0336     } else {
0337       Dwarf2LRegs = Map;
0338       Dwarf2LRegsSize = Size;
0339     }
0340   }
0341 
0342   /// mapLLVMRegToSEHReg - Used to initialize LLVM register to SEH register
0343   /// number mapping. By default the SEH register number is just the same
0344   /// as the LLVM register number.
0345   /// FIXME: TableGen these numbers. Currently this requires target specific
0346   /// initialization code.
0347   void mapLLVMRegToSEHReg(MCRegister LLVMReg, int SEHReg) {
0348     L2SEHRegs[LLVMReg] = SEHReg;
0349   }
0350 
0351   void mapLLVMRegToCVReg(MCRegister LLVMReg, int CVReg) {
0352     L2CVRegs[LLVMReg] = CVReg;
0353   }
0354 
0355   /// This method should return the register where the return
0356   /// address can be found.
0357   MCRegister getRARegister() const {
0358     return RAReg;
0359   }
0360 
0361   /// Return the register which is the program counter.
0362   MCRegister getProgramCounter() const {
0363     return PCReg;
0364   }
0365 
0366   const MCRegisterDesc &operator[](MCRegister Reg) const {
0367     assert(Reg.id() < NumRegs &&
0368            "Attempting to access record for invalid register number!");
0369     return Desc[Reg.id()];
0370   }
0371 
0372   /// Provide a get method, equivalent to [], but more useful with a
0373   /// pointer to this object.
0374   const MCRegisterDesc &get(MCRegister Reg) const {
0375     return operator[](Reg);
0376   }
0377 
0378   /// Returns the physical register number of sub-register "Index"
0379   /// for physical register RegNo. Return zero if the sub-register does not
0380   /// exist.
0381   MCRegister getSubReg(MCRegister Reg, unsigned Idx) const;
0382 
0383   /// Return a super-register of the specified register
0384   /// Reg so its sub-register of index SubIdx is Reg.
0385   MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx,
0386                                  const MCRegisterClass *RC) const;
0387 
0388   /// For a given register pair, return the sub-register index
0389   /// if the second register is a sub-register of the first. Return zero
0390   /// otherwise.
0391   unsigned getSubRegIndex(MCRegister RegNo, MCRegister SubRegNo) const;
0392 
0393   /// Return the human-readable symbolic target-specific name for the
0394   /// specified physical register.
0395   const char *getName(MCRegister RegNo) const {
0396     return RegStrings + get(RegNo).Name;
0397   }
0398 
0399   /// Returns true if the given register is constant.
0400   bool isConstant(MCRegister RegNo) const { return get(RegNo).IsConstant; }
0401 
0402   /// Returns true if the given register is artificial, which means it
0403   /// represents a regunit that is not separately addressable but still needs to
0404   /// be modelled, such as the top 16-bits of a 32-bit GPR.
0405   bool isArtificial(MCRegister RegNo) const { return get(RegNo).IsArtificial; }
0406 
0407   /// Returns true when the given register unit is considered artificial.
0408   /// Register units are considered artificial when at least one of the
0409   /// root registers is artificial.
0410   bool isArtificialRegUnit(MCRegUnit Unit) const;
0411 
0412   /// Return the number of registers this target has (useful for
0413   /// sizing arrays holding per register information)
0414   unsigned getNumRegs() const {
0415     return NumRegs;
0416   }
0417 
0418   /// Return the number of sub-register indices
0419   /// understood by the target. Index 0 is reserved for the no-op sub-register,
0420   /// while 1 to getNumSubRegIndices() - 1 represent real sub-registers.
0421   unsigned getNumSubRegIndices() const {
0422     return NumSubRegIndices;
0423   }
0424 
0425   /// Return the number of (native) register units in the
0426   /// target. Register units are numbered from 0 to getNumRegUnits() - 1. They
0427   /// can be accessed through MCRegUnitIterator defined below.
0428   unsigned getNumRegUnits() const {
0429     return NumRegUnits;
0430   }
0431 
0432   /// Map a target register to an equivalent dwarf register
0433   /// number.  Returns -1 if there is no equivalent value.  The second
0434   /// parameter allows targets to use different numberings for EH info and
0435   /// debugging info.
0436   virtual int64_t getDwarfRegNum(MCRegister RegNum, bool isEH) const;
0437 
0438   /// Map a dwarf register back to a target register. Returns std::nullopt if
0439   /// there is no mapping.
0440   std::optional<MCRegister> getLLVMRegNum(uint64_t RegNum, bool isEH) const;
0441 
0442   /// Map a target EH register number to an equivalent DWARF register
0443   /// number.
0444   int64_t getDwarfRegNumFromDwarfEHRegNum(uint64_t RegNum) const;
0445 
0446   /// Map a target register to an equivalent SEH register
0447   /// number.  Returns LLVM register number if there is no equivalent value.
0448   int getSEHRegNum(MCRegister RegNum) const;
0449 
0450   /// Map a target register to an equivalent CodeView register
0451   /// number.
0452   int getCodeViewRegNum(MCRegister RegNum) const;
0453 
0454   regclass_iterator regclass_begin() const { return Classes; }
0455   regclass_iterator regclass_end() const { return Classes+NumClasses; }
0456   iterator_range<regclass_iterator> regclasses() const {
0457     return make_range(regclass_begin(), regclass_end());
0458   }
0459 
0460   unsigned getNumRegClasses() const {
0461     return (unsigned)(regclass_end()-regclass_begin());
0462   }
0463 
0464   /// Returns the register class associated with the enumeration
0465   /// value.  See class MCOperandInfo.
0466   const MCRegisterClass& getRegClass(unsigned i) const {
0467     assert(i < getNumRegClasses() && "Register Class ID out of range");
0468     return Classes[i];
0469   }
0470 
0471   const char *getRegClassName(const MCRegisterClass *Class) const {
0472     return RegClassStrings + Class->NameIdx;
0473   }
0474 
0475    /// Returns the encoding for Reg
0476   uint16_t getEncodingValue(MCRegister Reg) const {
0477     assert(Reg.id() < NumRegs &&
0478            "Attempting to get encoding for invalid register number!");
0479     return RegEncodingTable[Reg.id()];
0480   }
0481 
0482   /// Returns true if RegB is a sub-register of RegA.
0483   bool isSubRegister(MCRegister RegA, MCRegister RegB) const {
0484     return isSuperRegister(RegB, RegA);
0485   }
0486 
0487   /// Returns true if RegB is a super-register of RegA.
0488   bool isSuperRegister(MCRegister RegA, MCRegister RegB) const;
0489 
0490   /// Returns true if RegB is a sub-register of RegA or if RegB == RegA.
0491   bool isSubRegisterEq(MCRegister RegA, MCRegister RegB) const {
0492     return isSuperRegisterEq(RegB, RegA);
0493   }
0494 
0495   /// Returns true if RegB is a super-register of RegA or if
0496   /// RegB == RegA.
0497   bool isSuperRegisterEq(MCRegister RegA, MCRegister RegB) const {
0498     return RegA == RegB || isSuperRegister(RegA, RegB);
0499   }
0500 
0501   /// Returns true if RegB is a super-register or sub-register of RegA
0502   /// or if RegB == RegA.
0503   bool isSuperOrSubRegisterEq(MCRegister RegA, MCRegister RegB) const {
0504     return isSubRegisterEq(RegA, RegB) || isSuperRegister(RegA, RegB);
0505   }
0506 
0507   /// Returns true if the two registers are equal or alias each other.
0508   bool regsOverlap(MCRegister RegA, MCRegister RegB) const;
0509 };
0510 
0511 //===----------------------------------------------------------------------===//
0512 //                          Register List Iterators
0513 //===----------------------------------------------------------------------===//
0514 
0515 // MCRegisterInfo provides lists of super-registers, sub-registers, and
0516 // aliasing registers. Use these iterator classes to traverse the lists.
0517 
0518 /// MCSubRegIterator enumerates all sub-registers of Reg.
0519 /// If IncludeSelf is set, Reg itself is included in the list.
0520 class MCSubRegIterator
0521     : public iterator_adaptor_base<MCSubRegIterator,
0522                                    MCRegisterInfo::DiffListIterator,
0523                                    std::forward_iterator_tag, const MCPhysReg> {
0524   // Cache the current value, so that we can return a reference to it.
0525   MCPhysReg Val;
0526 
0527 public:
0528   /// Constructs an end iterator.
0529   MCSubRegIterator() = default;
0530 
0531   MCSubRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI,
0532                    bool IncludeSelf = false) {
0533     assert(Reg.isPhysical());
0534     I.init(Reg.id(), MCRI->DiffLists + MCRI->get(Reg).SubRegs);
0535     // Initially, the iterator points to Reg itself.
0536     Val = MCPhysReg(*I);
0537     if (!IncludeSelf)
0538       ++*this;
0539   }
0540 
0541   const MCPhysReg &operator*() const { return Val; }
0542 
0543   using iterator_adaptor_base::operator++;
0544   MCSubRegIterator &operator++() {
0545     Val = MCPhysReg(*++I);
0546     return *this;
0547   }
0548 
0549   /// Returns true if this iterator is not yet at the end.
0550   bool isValid() const { return I.isValid(); }
0551 };
0552 
0553 /// Iterator that enumerates the sub-registers of a Reg and the associated
0554 /// sub-register indices.
0555 class MCSubRegIndexIterator {
0556   MCSubRegIterator SRIter;
0557   const uint16_t *SRIndex;
0558 
0559 public:
0560   /// Constructs an iterator that traverses subregisters and their
0561   /// associated subregister indices.
0562   MCSubRegIndexIterator(MCRegister Reg, const MCRegisterInfo *MCRI)
0563     : SRIter(Reg, MCRI) {
0564     SRIndex = MCRI->SubRegIndices + MCRI->get(Reg).SubRegIndices;
0565   }
0566 
0567   /// Returns current sub-register.
0568   MCRegister getSubReg() const {
0569     return *SRIter;
0570   }
0571 
0572   /// Returns sub-register index of the current sub-register.
0573   unsigned getSubRegIndex() const {
0574     return *SRIndex;
0575   }
0576 
0577   /// Returns true if this iterator is not yet at the end.
0578   bool isValid() const { return SRIter.isValid(); }
0579 
0580   /// Moves to the next position.
0581   MCSubRegIndexIterator &operator++() {
0582     ++SRIter;
0583     ++SRIndex;
0584     return *this;
0585   }
0586 };
0587 
0588 /// MCSuperRegIterator enumerates all super-registers of Reg.
0589 /// If IncludeSelf is set, Reg itself is included in the list.
0590 class MCSuperRegIterator
0591     : public iterator_adaptor_base<MCSuperRegIterator,
0592                                    MCRegisterInfo::DiffListIterator,
0593                                    std::forward_iterator_tag, const MCPhysReg> {
0594   // Cache the current value, so that we can return a reference to it.
0595   MCPhysReg Val;
0596 
0597 public:
0598   /// Constructs an end iterator.
0599   MCSuperRegIterator() = default;
0600 
0601   MCSuperRegIterator(MCRegister Reg, const MCRegisterInfo *MCRI,
0602                      bool IncludeSelf = false) {
0603     assert(Reg.isPhysical());
0604     I.init(Reg.id(), MCRI->DiffLists + MCRI->get(Reg).SuperRegs);
0605     // Initially, the iterator points to Reg itself.
0606     Val = MCPhysReg(*I);
0607     if (!IncludeSelf)
0608       ++*this;
0609   }
0610 
0611   const MCPhysReg &operator*() const { return Val; }
0612 
0613   using iterator_adaptor_base::operator++;
0614   MCSuperRegIterator &operator++() {
0615     Val = MCPhysReg(*++I);
0616     return *this;
0617   }
0618 
0619   /// Returns true if this iterator is not yet at the end.
0620   bool isValid() const { return I.isValid(); }
0621 };
0622 
0623 // Definition for isSuperRegister. Put it down here since it needs the
0624 // iterator defined above in addition to the MCRegisterInfo class itself.
0625 inline bool MCRegisterInfo::isSuperRegister(MCRegister RegA, MCRegister RegB) const{
0626   return is_contained(superregs(RegA), RegB);
0627 }
0628 
0629 //===----------------------------------------------------------------------===//
0630 //                               Register Units
0631 //===----------------------------------------------------------------------===//
0632 
0633 // MCRegUnitIterator enumerates a list of register units for Reg. The list is
0634 // in ascending numerical order.
0635 class MCRegUnitIterator
0636     : public iterator_adaptor_base<MCRegUnitIterator,
0637                                    MCRegisterInfo::DiffListIterator,
0638                                    std::forward_iterator_tag, const MCRegUnit> {
0639   // The value must be kept in sync with RegisterInfoEmitter.cpp.
0640   static constexpr unsigned RegUnitBits = 12;
0641   // Cache the current value, so that we can return a reference to it.
0642   MCRegUnit Val;
0643 
0644 public:
0645   /// Constructs an end iterator.
0646   MCRegUnitIterator() = default;
0647 
0648   MCRegUnitIterator(MCRegister Reg, const MCRegisterInfo *MCRI) {
0649     assert(Reg.isPhysical());
0650     // Decode the RegUnits MCRegisterDesc field.
0651     unsigned RU = MCRI->get(Reg).RegUnits;
0652     unsigned FirstRU = RU & ((1u << RegUnitBits) - 1);
0653     unsigned Offset = RU >> RegUnitBits;
0654     I.init(FirstRU, MCRI->DiffLists + Offset);
0655     Val = MCRegUnit(*I);
0656   }
0657 
0658   const MCRegUnit &operator*() const { return Val; }
0659 
0660   using iterator_adaptor_base::operator++;
0661   MCRegUnitIterator &operator++() {
0662     Val = MCRegUnit(*++I);
0663     return *this;
0664   }
0665 
0666   /// Returns true if this iterator is not yet at the end.
0667   bool isValid() const { return I.isValid(); }
0668 };
0669 
0670 /// MCRegUnitMaskIterator enumerates a list of register units and their
0671 /// associated lane masks for Reg. The register units are in ascending
0672 /// numerical order.
0673 class MCRegUnitMaskIterator {
0674   MCRegUnitIterator RUIter;
0675   const LaneBitmask *MaskListIter;
0676 
0677 public:
0678   MCRegUnitMaskIterator() = default;
0679 
0680   /// Constructs an iterator that traverses the register units and their
0681   /// associated LaneMasks in Reg.
0682   MCRegUnitMaskIterator(MCRegister Reg, const MCRegisterInfo *MCRI)
0683     : RUIter(Reg, MCRI) {
0684       uint16_t Idx = MCRI->get(Reg).RegUnitLaneMasks;
0685       MaskListIter = &MCRI->RegUnitMaskSequences[Idx];
0686   }
0687 
0688   /// Returns a (RegUnit, LaneMask) pair.
0689   std::pair<unsigned,LaneBitmask> operator*() const {
0690     return std::make_pair(*RUIter, *MaskListIter);
0691   }
0692 
0693   /// Returns true if this iterator is not yet at the end.
0694   bool isValid() const { return RUIter.isValid(); }
0695 
0696   /// Moves to the next position.
0697   MCRegUnitMaskIterator &operator++() {
0698     ++MaskListIter;
0699     ++RUIter;
0700     return *this;
0701   }
0702 };
0703 
0704 // Each register unit has one or two root registers. The complete set of
0705 // registers containing a register unit is the union of the roots and their
0706 // super-registers. All registers aliasing Unit can be visited like this:
0707 //
0708 //   for (MCRegUnitRootIterator RI(Unit, MCRI); RI.isValid(); ++RI) {
0709 //     for (MCSuperRegIterator SI(*RI, MCRI, true); SI.isValid(); ++SI)
0710 //       visit(*SI);
0711 //    }
0712 
0713 /// MCRegUnitRootIterator enumerates the root registers of a register unit.
0714 class MCRegUnitRootIterator {
0715   uint16_t Reg0 = 0;
0716   uint16_t Reg1 = 0;
0717 
0718 public:
0719   MCRegUnitRootIterator() = default;
0720 
0721   MCRegUnitRootIterator(unsigned RegUnit, const MCRegisterInfo *MCRI) {
0722     assert(RegUnit < MCRI->getNumRegUnits() && "Invalid register unit");
0723     Reg0 = MCRI->RegUnitRoots[RegUnit][0];
0724     Reg1 = MCRI->RegUnitRoots[RegUnit][1];
0725   }
0726 
0727   /// Dereference to get the current root register.
0728   unsigned operator*() const {
0729     return Reg0;
0730   }
0731 
0732   /// Check if the iterator is at the end of the list.
0733   bool isValid() const {
0734     return Reg0;
0735   }
0736 
0737   /// Preincrement to move to the next root register.
0738   MCRegUnitRootIterator &operator++() {
0739     assert(isValid() && "Cannot move off the end of the list.");
0740     Reg0 = Reg1;
0741     Reg1 = 0;
0742     return *this;
0743   }
0744 };
0745 
0746 /// MCRegAliasIterator enumerates all registers aliasing Reg.
0747 class MCRegAliasIterator {
0748 private:
0749   const MCPhysReg *It = nullptr;
0750   const MCPhysReg *End = nullptr;
0751 
0752 public:
0753   MCRegAliasIterator(MCRegister Reg, const MCRegisterInfo *MCRI,
0754                      bool IncludeSelf) {
0755     ArrayRef<MCPhysReg> Cache = MCRI->getCachedAliasesOf(Reg);
0756     assert(Cache.back() == Reg);
0757     It = Cache.begin();
0758     End = Cache.end();
0759     if (!IncludeSelf)
0760       --End;
0761   }
0762 
0763   bool isValid() const { return It != End; }
0764 
0765   MCRegister operator*() const { return *It; }
0766 
0767   MCRegAliasIterator &operator++() {
0768     assert(isValid() && "Cannot move off the end of the list.");
0769     ++It;
0770     return *this;
0771   }
0772 };
0773 
0774 inline iterator_range<MCSubRegIterator>
0775 MCRegisterInfo::subregs(MCRegister Reg) const {
0776   return make_range({Reg, this, /*IncludeSelf=*/false}, MCSubRegIterator());
0777 }
0778 
0779 inline iterator_range<MCSubRegIterator>
0780 MCRegisterInfo::subregs_inclusive(MCRegister Reg) const {
0781   return make_range({Reg, this, /*IncludeSelf=*/true}, MCSubRegIterator());
0782 }
0783 
0784 inline iterator_range<MCSuperRegIterator>
0785 MCRegisterInfo::superregs(MCRegister Reg) const {
0786   return make_range({Reg, this, /*IncludeSelf=*/false}, MCSuperRegIterator());
0787 }
0788 
0789 inline iterator_range<MCSuperRegIterator>
0790 MCRegisterInfo::superregs_inclusive(MCRegister Reg) const {
0791   return make_range({Reg, this, /*IncludeSelf=*/true}, MCSuperRegIterator());
0792 }
0793 
0794 inline detail::concat_range<const MCPhysReg, iterator_range<MCSubRegIterator>,
0795                             iterator_range<MCSuperRegIterator>>
0796 MCRegisterInfo::sub_and_superregs_inclusive(MCRegister Reg) const {
0797   return concat<const MCPhysReg>(subregs_inclusive(Reg), superregs(Reg));
0798 }
0799 
0800 inline iterator_range<MCRegUnitIterator>
0801 MCRegisterInfo::regunits(MCRegister Reg) const {
0802   return make_range({Reg, this}, MCRegUnitIterator());
0803 }
0804 
0805 } // end namespace llvm
0806 
0807 #endif // LLVM_MC_MCREGISTERINFO_H