diff --git a/llvm/include/llvm/CodeGen/Register.h b/llvm/include/llvm/CodeGen/Register.h --- a/llvm/include/llvm/CodeGen/Register.h +++ b/llvm/include/llvm/CodeGen/Register.h @@ -33,6 +33,8 @@ // // Further sentinels can be allocated from the small negative integers. // DenseMapInfo uses -1u and -2u. + static_assert(std::numeric_limits::max() >= 0xFFFFFFFF, + "Reg isn't large enough to hold full range."); /// isStackSlot - Sometimes it is useful the be able to store a non-negative /// frame index in a variable that normally holds a register. isStackSlot() @@ -49,13 +51,13 @@ /// Compute the frame index from a register value representing a stack slot. static int stackSlot2Index(unsigned Reg) { assert(isStackSlot(Reg) && "Not a stack slot"); - return int(Reg - (1u << 30)); + return int(Reg - MCRegister::FirstStackSlot); } /// Convert a non-negative frame index to a stack slot register value. static unsigned index2StackSlot(int FI) { assert(FI >= 0 && "Cannot hold a negative frame index."); - return FI + (1u << 30); + return FI + MCRegister::FirstStackSlot; } /// Return true if the specified register number is in @@ -68,20 +70,21 @@ /// the virtual register namespace. static bool isVirtualRegister(unsigned Reg) { assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first."); - return int(Reg) < 0; + return Reg & MCRegister::VirtualRegFlag; } /// Convert a virtual register number to a 0-based index. /// The first virtual register in a function will get the index 0. static unsigned virtReg2Index(unsigned Reg) { assert(isVirtualRegister(Reg) && "Not a virtual register"); - return Reg & ~(1u << 31); + return Reg & ~MCRegister::VirtualRegFlag; } /// Convert a 0-based index to a virtual register number. /// This is the inverse operation of VirtReg2IndexFunctor below. static unsigned index2VirtReg(unsigned Index) { - return Index | (1u << 31); + assert(Index < (1u << 31) && "Index too large for virtual register range."); + return Index | MCRegister::VirtualRegFlag; } /// Return true if the specified register number is in the virtual register @@ -112,9 +115,7 @@ return MCRegister(Reg); } - bool isValid() const { - return Reg != 0; - } + bool isValid() const { return Reg != MCRegister::NoRegister; } /// Comparisons between register objects bool operator==(const Register &Other) const { return Reg == Other.Reg; } diff --git a/llvm/include/llvm/MC/MCRegister.h b/llvm/include/llvm/MC/MCRegister.h --- a/llvm/include/llvm/MC/MCRegister.h +++ b/llvm/include/llvm/MC/MCRegister.h @@ -35,6 +35,12 @@ // // Further sentinels can be allocated from the small negative integers. // DenseMapInfo uses -1u and -2u. + static_assert(std::numeric_limits::max() >= 0xFFFFFFFF, + "Reg isn't large enough to hold full range."); + static constexpr unsigned NoRegister = 0u; + static constexpr unsigned FirstPhysicalReg = 1u; + static constexpr unsigned FirstStackSlot = 1u << 30; + static constexpr unsigned VirtualRegFlag = 1u << 31; /// This is the portion of the positive number space that is not a physical /// register. StackSlot values do not exist in the MC layer, see @@ -44,14 +50,15 @@ /// slots, so if a variable may contains a stack slot, always check /// isStackSlot() first. static bool isStackSlot(unsigned Reg) { - return int(Reg) >= (1 << 30); + return !(Reg & VirtualRegFlag) && + uint32_t(Reg & ~VirtualRegFlag) >= FirstStackSlot; } /// Return true if the specified register number is in /// the physical register namespace. static bool isPhysicalRegister(unsigned Reg) { assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first."); - return int(Reg) > 0; + return Reg >= FirstPhysicalReg && !(Reg & VirtualRegFlag); } /// Return true if the specified register number is in the physical register @@ -68,9 +75,7 @@ return Reg; } - bool isValid() const { - return Reg != 0; - } + bool isValid() const { return Reg != NoRegister; } /// Comparisons between register objects bool operator==(const MCRegister &Other) const { return Reg == Other.Reg; }