@@ -110,16 +110,16 @@ static constexpr MCPhysReg SPReg = RISCV::X2;
110110// The register used to hold the return address.
111111static constexpr MCPhysReg RAReg = RISCV::X1;
112112
113- // Offsets which need to be scale by XLen representing locations of CSRs which
114- // are given a fixed location by save/restore libcalls or Zcmp Push/Pop.
115- static const std::pair<MCPhysReg, int8_t > FixedCSRFIMap[] = {
116- { /* ra */ RAReg, - 1 }, { /* s0 */ FPReg, - 2 },
117- { /* s1 */ RISCV::X9, - 3 }, { /* s2 */ RISCV::X18, - 4 },
118- { /* s3 */ RISCV::X19, - 5 } , { /* s4 */ RISCV::X20, - 6 } ,
119- { /* s5 */ RISCV::X21, - 7 }, { /* s6 */ RISCV::X22, - 8 } ,
120- { /* s7 */ RISCV::X23, - 9 }, { /* s8 */ RISCV::X24, - 10 } ,
121- { /* s9*/ RISCV::X25, - 11 }, { /* s10*/ RISCV::X26, - 12 } ,
122- { /* s11*/ RISCV::X27, - 13 } };
113+ // LIst of CSRs that are given a fixed location by save/restore libcalls or
114+ // Zcmp/Xqccmp Push/Pop. The order in this table indicates the order the
115+ // registers are saved on the stack. Zcmp uses the reverse order of save/restore
116+ // and Xqccmp on the stack, but this is handled when offsets are calculated.
117+ static const MCPhysReg FixedCSRFIMap[] = {
118+ /* ra */ RAReg, /* s0 */ FPReg , /* s1 */ RISCV::X9 ,
119+ /* s2 */ RISCV::X18, /* s3 */ RISCV::X19, /* s4 */ RISCV::X20 ,
120+ /* s5 */ RISCV::X21, /* s6 */ RISCV::X22, /* s7 */ RISCV::X23 ,
121+ /* s8 */ RISCV::X24, /* s9*/ RISCV::X25, /* s10*/ RISCV::X26,
122+ /* s11*/ RISCV::X27};
123123
124124// For now we use x3, a.k.a gp, as pointer to shadow call stack.
125125// User should not use x3 in their asm.
@@ -370,8 +370,8 @@ getPushPopEncodingAndNum(const Register MaxReg) {
370370static Register getMaxPushPopReg (const std::vector<CalleeSavedInfo> &CSI) {
371371 MCRegister MaxPushPopReg;
372372 for (auto &CS : CSI) {
373- if (llvm::find_if (FixedCSRFIMap, [&](auto P) {
374- return P. first == CS.getReg ();
373+ if (llvm::find_if (FixedCSRFIMap, [&](MCPhysReg P) {
374+ return P == CS.getReg ();
375375 }) != std::end (FixedCSRFIMap))
376376 MaxPushPopReg = std::max (MaxPushPopReg.id (), CS.getReg ().id ());
377377 }
@@ -487,7 +487,7 @@ getPushOrLibCallsSavedInfo(const MachineFunction &MF,
487487
488488 for (const auto &CS : CSI) {
489489 const auto *FII = llvm::find_if (
490- FixedCSRFIMap, [&](auto P) { return P. first == CS.getReg (); });
490+ FixedCSRFIMap, [&](MCPhysReg P) { return P == CS.getReg (); });
491491 if (FII != std::end (FixedCSRFIMap))
492492 PushOrLibCallsCSI.push_back (CS);
493493 }
@@ -1812,13 +1812,15 @@ bool RISCVFrameLowering::assignCalleeSavedSpillSlots(
18121812 // This might need a fixed stack slot.
18131813 if (RVFI->useSaveRestoreLibCalls (MF) || RVFI->isPushable (MF)) {
18141814 const auto *FII = llvm::find_if (
1815- FixedCSRFIMap, [&](auto P) { return P.first == CS.getReg (); });
1815+ FixedCSRFIMap, [&](MCPhysReg P) { return P == CS.getReg (); });
1816+ unsigned RegNum = std::distance (std::begin (FixedCSRFIMap), FII);
1817+
18161818 if (FII != std::end (FixedCSRFIMap)) {
18171819 int64_t Offset;
18181820 if (RVFI->isPushable (MF))
1819- Offset = -((FII-> second + RVFI->getRVPushRegs () + 1 ) * ( int64_t ) Size) ;
1821+ Offset = -int64_t ( RVFI->getRVPushRegs () - RegNum ) * Size;
18201822 else
1821- Offset = FII-> second * ( int64_t ) Size;
1823+ Offset = - int64_t (RegNum + 1 ) * Size;
18221824
18231825 int FrameIdx = MFI.CreateFixedSpillStackObject (Size, Offset);
18241826 assert (FrameIdx < 0 );
@@ -1887,7 +1889,7 @@ bool RISCVFrameLowering::spillCalleeSavedRegisters(
18871889 PushBuilder.addImm (0 );
18881890
18891891 for (unsigned i = 0 ; i < PushedRegNum; i++)
1890- PushBuilder.addUse (FixedCSRFIMap[i]. first , RegState::Implicit);
1892+ PushBuilder.addUse (FixedCSRFIMap[i], RegState::Implicit);
18911893 }
18921894 } else if (const char *SpillLibCall = getSpillLibCallName (*MF, CSI)) {
18931895 // Add spill libcall via non-callee-saved register t0.
@@ -2042,7 +2044,7 @@ bool RISCVFrameLowering::restoreCalleeSavedRegisters(
20422044 PopBuilder.addImm (0 );
20432045
20442046 for (unsigned i = 0 ; i < RVFI->getRVPushRegs (); i++)
2045- PopBuilder.addDef (FixedCSRFIMap[i]. first , RegState::ImplicitDefine);
2047+ PopBuilder.addDef (FixedCSRFIMap[i], RegState::ImplicitDefine);
20462048 }
20472049 } else {
20482050 const char *RestoreLibCall = getRestoreLibCallName (*MF, CSI);
0 commit comments