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,9 @@ 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 = (unsigned )(-1 ) >> (CHAR_BIT * sizeof (unsigned ) - MaxFrameIndexBitwidth);
3942 static_assert (FirstStackSlot >= MCRegister::LastPhysicalReg);
4043 static constexpr unsigned VirtualRegFlag = 1u << 31 ;
4144
@@ -46,8 +49,8 @@ class Register {
4649
4750 // / Convert a non-negative frame index to a stack slot register value.
4851 static Register index2StackSlot (int FI) {
49- assert (FI >= 0 && " Cannot hold a negative frame index. " );
50- return Register (FI + Register::FirstStackSlot);
52+ assert (isInt< 30 >(FI) && " Frame index must be at most 30 bit integer " );
53+ return Register (( FI & Register::StackSlotMask) | Register::FirstStackSlot);
5154 }
5255
5356 // / Return true if the specified register number is in
@@ -87,7 +90,7 @@ class Register {
8790 // / Compute the frame index from a register value representing a stack slot.
8891 int stackSlotIndex () const {
8992 assert (isStack () && " Not a stack slot" );
90- return static_cast <int >(Reg - Register::FirstStackSlot );
93+ return static_cast <int >(SignExtend64 ( Reg & Register::StackSlotMask, 30 ) );
9194 }
9295
9396 constexpr operator unsigned () const { return Reg; }
0 commit comments