File indexing completed on 2026-05-10 08:43:33
0001
0002
0003
0004
0005
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
0018
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
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
0037 "Reg isn't large enough to hold full range.");
0038
0039
0040
0041
0042
0043
0044 static constexpr bool isStackSlot(unsigned Reg) {
0045 return MCRegister::isStackSlot(Reg);
0046 }
0047
0048
0049 constexpr bool isStack() const { return MCRegister::isStackSlot(Reg); }
0050
0051
0052 static int stackSlot2Index(Register Reg) {
0053 assert(Reg.isStack() && "Not a stack slot");
0054 return int(Reg.id() - MCRegister::FirstStackSlot);
0055 }
0056
0057
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
0064
0065 static constexpr bool isPhysicalRegister(unsigned Reg) {
0066 return MCRegister::isPhysicalRegister(Reg);
0067 }
0068
0069
0070
0071 static constexpr bool isVirtualRegister(unsigned Reg) {
0072 return Reg & MCRegister::VirtualRegFlag;
0073 }
0074
0075
0076
0077 static unsigned virtReg2Index(Register Reg) {
0078 assert(Reg.isVirtual() && "Not a virtual register");
0079 return Reg.id() & ~MCRegister::VirtualRegFlag;
0080 }
0081
0082
0083
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
0090
0091 constexpr bool isVirtual() const { return isVirtualRegister(Reg); }
0092
0093
0094
0095 constexpr bool isPhysical() const { return isPhysicalRegister(Reg); }
0096
0097
0098
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
0108
0109
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
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
0132
0133
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
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
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
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 }
0195
0196 #endif