Skip to content

Commit 0d8cc4d

Browse files
committed
[RISCV] Remove the offset numbers from the FixedCSRFIMap. NFC
Use the position within the table instead with a little bit of arithmetic.
1 parent bdace10 commit 0d8cc4d

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ static constexpr MCPhysReg SPReg = RISCV::X2;
110110
// The register used to hold the return address.
111111
static 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.
@@ -371,8 +371,8 @@ static Register getMaxPushPopReg(const MachineFunction &MF,
371371
const std::vector<CalleeSavedInfo> &CSI) {
372372
MCRegister MaxPushPopReg;
373373
for (auto &CS : CSI) {
374-
if (llvm::find_if(FixedCSRFIMap, [&](auto P) {
375-
return P.first == CS.getReg();
374+
if (llvm::find_if(FixedCSRFIMap, [&](MCPhysReg P) {
375+
return P == CS.getReg();
376376
}) != std::end(FixedCSRFIMap))
377377
MaxPushPopReg = std::max(MaxPushPopReg.id(), CS.getReg().id());
378378
}
@@ -488,7 +488,7 @@ getPushOrLibCallsSavedInfo(const MachineFunction &MF,
488488

489489
for (const auto &CS : CSI) {
490490
const auto *FII = llvm::find_if(
491-
FixedCSRFIMap, [&](auto P) { return P.first == CS.getReg(); });
491+
FixedCSRFIMap, [&](MCPhysReg P) { return P == CS.getReg(); });
492492
if (FII != std::end(FixedCSRFIMap))
493493
PushOrLibCallsCSI.push_back(CS);
494494
}
@@ -1813,13 +1813,15 @@ bool RISCVFrameLowering::assignCalleeSavedSpillSlots(
18131813
// This might need a fixed stack slot.
18141814
if (RVFI->useSaveRestoreLibCalls(MF) || RVFI->isPushable(MF)) {
18151815
const auto *FII = llvm::find_if(
1816-
FixedCSRFIMap, [&](auto P) { return P.first == CS.getReg(); });
1816+
FixedCSRFIMap, [&](MCPhysReg P) { return P == CS.getReg(); });
1817+
unsigned RegNum = std::distance(std::begin(FixedCSRFIMap), FII);
1818+
18171819
if (FII != std::end(FixedCSRFIMap)) {
18181820
int64_t Offset;
18191821
if (RVFI->isPushable(MF))
1820-
Offset = -((FII->second + RVFI->getRVPushRegs() + 1) * (int64_t)Size);
1822+
Offset = -int64_t(RVFI->getRVPushRegs() - RegNum) * Size;
18211823
else
1822-
Offset = FII->second * (int64_t)Size;
1824+
Offset = -int64_t(RegNum + 1) * Size;
18231825

18241826
int FrameIdx = MFI.CreateFixedSpillStackObject(Size, Offset);
18251827
assert(FrameIdx < 0);
@@ -1888,7 +1890,7 @@ bool RISCVFrameLowering::spillCalleeSavedRegisters(
18881890
PushBuilder.addImm(0);
18891891

18901892
for (unsigned i = 0; i < PushedRegNum; i++)
1891-
PushBuilder.addUse(FixedCSRFIMap[i].first, RegState::Implicit);
1893+
PushBuilder.addUse(FixedCSRFIMap[i], RegState::Implicit);
18921894
}
18931895
} else if (const char *SpillLibCall = getSpillLibCallName(*MF, CSI)) {
18941896
// Add spill libcall via non-callee-saved register t0.
@@ -2043,7 +2045,7 @@ bool RISCVFrameLowering::restoreCalleeSavedRegisters(
20432045
PopBuilder.addImm(0);
20442046

20452047
for (unsigned i = 0; i < RVFI->getRVPushRegs(); i++)
2046-
PopBuilder.addDef(FixedCSRFIMap[i].first, RegState::ImplicitDefine);
2048+
PopBuilder.addDef(FixedCSRFIMap[i], RegState::ImplicitDefine);
20472049
}
20482050
} else {
20492051
const char *RestoreLibCall = getRestoreLibCallName(*MF, CSI);

0 commit comments

Comments
 (0)