Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- llvm/CodeGen/Register.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 #ifndef LLVM_CODEGEN_REGISTER_H
0010 #define LLVM_CODEGEN_REGISTER_H
0011 
0012 #include "llvm/MC/MCRegister.h"
0013 #include <cassert>
0014 
0015 namespace llvm {
0016 
0017 /// Wrapper class representing virtual and physical registers. Should be passed
0018 /// by value.
0019 class Register {
0020   unsigned Reg;
0021 
0022 public:
0023   constexpr Register(unsigned Val = 0) : Reg(Val) {}
0024   constexpr Register(MCRegister Val) : Reg(Val.id()) {}
0025 
0026   // Register numbers can represent physical registers, virtual registers, and
0027   // sometimes stack slots. The unsigned values are divided into these ranges:
0028   //
0029   //   0           Not a register, can be used as a sentinel.
0030   //   [1;2^30)    Physical registers assigned by TableGen.
0031   //   [2^30;2^31) Stack slots. (Rarely used.)
0032   //   [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
0033   //
0034   // Further sentinels can be allocated from the small negative integers.
0035   // DenseMapInfo<unsigned> uses -1u and -2u.
0036   static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
0037                 "Reg isn't large enough to hold full range.");
0038 
0039   /// isStackSlot - Sometimes it is useful to be able to store a non-negative
0040   /// frame index in a variable that normally holds a register. isStackSlot()
0041   /// returns true if Reg is in the range used for stack slots.
0042   ///
0043   /// FIXME: remove in favor of member.
0044   static constexpr bool isStackSlot(unsigned Reg) {
0045     return MCRegister::isStackSlot(Reg);
0046   }
0047 
0048   /// Return true if this is a stack slot.
0049   constexpr bool isStack() const { return MCRegister::isStackSlot(Reg); }
0050 
0051   /// Compute the frame index from a register value representing a stack slot.
0052   static int stackSlot2Index(Register Reg) {
0053     assert(Reg.isStack() && "Not a stack slot");
0054     return int(Reg.id() - MCRegister::FirstStackSlot);
0055   }
0056 
0057   /// Convert a non-negative frame index to a stack slot register value.
0058   static Register index2StackSlot(int FI) {
0059     assert(FI >= 0 && "Cannot hold a negative frame index.");
0060     return Register(FI + MCRegister::FirstStackSlot);
0061   }
0062 
0063   /// Return true if the specified register number is in
0064   /// the physical register namespace.
0065   static constexpr bool isPhysicalRegister(unsigned Reg) {
0066     return MCRegister::isPhysicalRegister(Reg);
0067   }
0068 
0069   /// Return true if the specified register number is in
0070   /// the virtual register namespace.
0071   static constexpr bool isVirtualRegister(unsigned Reg) {
0072     return Reg & MCRegister::VirtualRegFlag;
0073   }
0074 
0075   /// Convert a virtual register number to a 0-based index.
0076   /// The first virtual register in a function will get the index 0.
0077   static unsigned virtReg2Index(Register Reg) {
0078     assert(Reg.isVirtual() && "Not a virtual register");
0079     return Reg.id() & ~MCRegister::VirtualRegFlag;
0080   }
0081 
0082   /// Convert a 0-based index to a virtual register number.
0083   /// This is the inverse operation of VirtReg2IndexFunctor below.
0084   static Register index2VirtReg(unsigned Index) {
0085     assert(Index < (1u << 31) && "Index too large for virtual register range.");
0086     return Index | MCRegister::VirtualRegFlag;
0087   }
0088 
0089   /// Return true if the specified register number is in the virtual register
0090   /// namespace.
0091   constexpr bool isVirtual() const { return isVirtualRegister(Reg); }
0092 
0093   /// Return true if the specified register number is in the physical register
0094   /// namespace.
0095   constexpr bool isPhysical() const { return isPhysicalRegister(Reg); }
0096 
0097   /// Convert a virtual register number to a 0-based index. The first virtual
0098   /// register in a function will get the index 0.
0099   unsigned virtRegIndex() const { return virtReg2Index(Reg); }
0100 
0101   constexpr operator unsigned() const { return Reg; }
0102 
0103   constexpr unsigned id() const { return Reg; }
0104 
0105   constexpr operator MCRegister() const { return MCRegister(Reg); }
0106 
0107   /// Utility to check-convert this value to a MCRegister. The caller is
0108   /// expected to have already validated that this Register is, indeed,
0109   /// physical.
0110   MCRegister asMCReg() const {
0111     assert(!isValid() || isPhysical());
0112     return MCRegister(Reg);
0113   }
0114 
0115   constexpr bool isValid() const { return Reg != MCRegister::NoRegister; }
0116 
0117   /// Comparisons between register objects
0118   constexpr bool operator==(const Register &Other) const {
0119     return Reg == Other.Reg;
0120   }
0121   constexpr bool operator!=(const Register &Other) const {
0122     return Reg != Other.Reg;
0123   }
0124   constexpr bool operator==(const MCRegister &Other) const {
0125     return Reg == Other.id();
0126   }
0127   constexpr bool operator!=(const MCRegister &Other) const {
0128     return Reg != Other.id();
0129   }
0130 
0131   /// Comparisons against register constants. E.g.
0132   /// * R == AArch64::WZR
0133   /// * R == 0
0134   constexpr bool operator==(unsigned Other) const { return Reg == Other; }
0135   constexpr bool operator!=(unsigned Other) const { return Reg != Other; }
0136   constexpr bool operator==(int Other) const { return Reg == unsigned(Other); }
0137   constexpr bool operator!=(int Other) const { return Reg != unsigned(Other); }
0138   // MSVC requires that we explicitly declare these two as well.
0139   constexpr bool operator==(MCPhysReg Other) const {
0140     return Reg == unsigned(Other);
0141   }
0142   constexpr bool operator!=(MCPhysReg Other) const {
0143     return Reg != unsigned(Other);
0144   }
0145 };
0146 
0147 // Provide DenseMapInfo for Register
0148 template <> struct DenseMapInfo<Register> {
0149   static inline Register getEmptyKey() {
0150     return DenseMapInfo<unsigned>::getEmptyKey();
0151   }
0152   static inline Register getTombstoneKey() {
0153     return DenseMapInfo<unsigned>::getTombstoneKey();
0154   }
0155   static unsigned getHashValue(const Register &Val) {
0156     return DenseMapInfo<unsigned>::getHashValue(Val.id());
0157   }
0158   static bool isEqual(const Register &LHS, const Register &RHS) {
0159     return LHS == RHS;
0160   }
0161 };
0162 
0163 /// Wrapper class representing a virtual register or register unit.
0164 class VirtRegOrUnit {
0165   unsigned VRegOrUnit;
0166 
0167 public:
0168   constexpr explicit VirtRegOrUnit(MCRegUnit Unit) : VRegOrUnit(Unit) {
0169     assert(!Register::isVirtualRegister(VRegOrUnit));
0170   }
0171   constexpr explicit VirtRegOrUnit(Register Reg) : VRegOrUnit(Reg.id()) {
0172     assert(Reg.isVirtual());
0173   }
0174 
0175   constexpr bool isVirtualReg() const {
0176     return Register::isVirtualRegister(VRegOrUnit);
0177   }
0178 
0179   constexpr MCRegUnit asMCRegUnit() const {
0180     assert(!isVirtualReg() && "Not a register unit");
0181     return VRegOrUnit;
0182   }
0183 
0184   constexpr Register asVirtualReg() const {
0185     assert(isVirtualReg() && "Not a virtual register");
0186     return Register(VRegOrUnit);
0187   }
0188 
0189   constexpr bool operator==(const VirtRegOrUnit &Other) const {
0190     return VRegOrUnit == Other.VRegOrUnit;
0191   }
0192 };
0193 
0194 } // namespace llvm
0195 
0196 #endif // LLVM_CODEGEN_REGISTER_H