1010#define LLVM_CODEGEN_REGISTER_H
1111
1212#include " llvm/MC/MCRegister.h"
13+ #include " llvm/Support/MathExtras.h"
1314#include < cassert>
1415
1516namespace llvm {
@@ -35,7 +36,10 @@ class Register {
3536 // DenseMapInfo<unsigned> uses -1u and -2u.
3637 static_assert (std::numeric_limits<decltype (Reg)>::max() >= 0xFFFFFFFF ,
3738 " Reg isn't large enough to hold full range." );
38- static constexpr unsigned FirstStackSlot = 1u << 30 ;
39+ static constexpr unsigned MaxFrameIndexBitwidth = 30 ;
40+ static constexpr unsigned FirstStackSlot = 1u << MaxFrameIndexBitwidth;
41+ static const unsigned StackSlotMask =
42+ (unsigned )(-1 ) >> (CHAR_BIT * sizeof (unsigned ) - MaxFrameIndexBitwidth);
3943 static_assert (FirstStackSlot >= MCRegister::LastPhysicalReg);
4044 static constexpr unsigned VirtualRegFlag = 1u << 31 ;
4145
@@ -46,8 +50,8 @@ class Register {
4650
4751 // / Convert a non-negative frame index to a stack slot register value.
4852 static Register index2StackSlot (int FI) {
49- assert (FI >= 0 && " Cannot hold a negative frame index. " );
50- return Register (FI + Register::FirstStackSlot);
53+ assert (isInt< 30 >(FI) && " Frame index must be at most 30 bit integer " );
54+ return Register (( FI & Register::StackSlotMask) | Register::FirstStackSlot);
5155 }
5256
5357 // / Return true if the specified register number is in
@@ -87,7 +91,7 @@ class Register {
8791 // / Compute the frame index from a register value representing a stack slot.
8892 int stackSlotIndex () const {
8993 assert (isStack () && " Not a stack slot" );
90- return static_cast <int >(Reg - Register::FirstStackSlot );
94+ return static_cast <int >(SignExtend64 ( Reg & Register::StackSlotMask, 30 ) );
9195 }
9296
9397 constexpr operator unsigned () const { return Reg; }
0 commit comments