Skip to content

Commit 8800b73

Browse files
authored
[RISCV] Refactor FP, SP and RA in RISCVFrameLowering.cpp. NFC (#113818)
Those registers are too fragmented in terms of usage, some are hard coded and some are retrieved by calling function. Also some have comments for alias name, some don't.
1 parent d926828 commit 8800b73

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@ RISCVFrameLowering::RISCVFrameLowering(const RISCVSubtarget &STI)
4242
/*TransientStackAlignment=*/getABIStackAlignment(STI.getTargetABI())),
4343
STI(STI) {}
4444

45+
// The register used to hold the frame pointer.
46+
static constexpr Register FPReg = RISCV::X8;
47+
48+
// The register used to hold the stack pointer.
49+
static constexpr Register SPReg = RISCV::X2;
50+
51+
// The register used to hold the return address.
52+
static constexpr Register RAReg = RISCV::X1;
53+
4554
// Offsets which need to be scale by XLen representing locations of CSRs which
4655
// are given a fixed location by save/restore libcalls or Zcmp Push/Pop.
4756
static const std::pair<MCPhysReg, int8_t> FixedCSRFIMap[] = {
48-
{/*ra*/ RISCV::X1, -1}, {/*s0*/ RISCV::X8, -2},
57+
{/*ra*/ RAReg, -1}, {/*s0*/ FPReg, -2},
4958
{/*s1*/ RISCV::X9, -3}, {/*s2*/ RISCV::X18, -4},
5059
{/*s3*/ RISCV::X19, -5}, {/*s4*/ RISCV::X20, -6},
5160
{/*s5*/ RISCV::X21, -7}, {/*s6*/ RISCV::X22, -8},
@@ -187,6 +196,7 @@ static int getLibCallID(const MachineFunction &MF,
187196
switch (MaxReg) {
188197
default:
189198
llvm_unreachable("Something has gone wrong!");
199+
// clang-format off
190200
case /*s11*/ RISCV::X27: return 12;
191201
case /*s10*/ RISCV::X26: return 11;
192202
case /*s9*/ RISCV::X25: return 10;
@@ -198,8 +208,9 @@ static int getLibCallID(const MachineFunction &MF,
198208
case /*s3*/ RISCV::X19: return 4;
199209
case /*s2*/ RISCV::X18: return 3;
200210
case /*s1*/ RISCV::X9: return 2;
201-
case /*s0*/ RISCV::X8: return 1;
202-
case /*ra*/ RISCV::X1: return 0;
211+
case /*s0*/ FPReg: return 1;
212+
case /*ra*/ RAReg: return 0;
213+
// clang-format on
203214
}
204215
}
205216

@@ -284,9 +295,9 @@ getPushPopEncodingAndNum(const Register MaxReg) {
284295
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S2, 4);
285296
case RISCV::X9: /*s1*/
286297
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S1, 3);
287-
case RISCV::X8: /*s0*/
298+
case FPReg: /*s0*/
288299
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0, 2);
289-
case RISCV::X1: /*ra*/
300+
case RAReg: /*ra*/
290301
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA, 1);
291302
}
292303
}
@@ -372,12 +383,6 @@ uint64_t RISCVFrameLowering::getStackSizeWithRVVPadding(
372383
return alignTo(MFI.getStackSize() + RVFI->getRVVPadding(), getStackAlign());
373384
}
374385

375-
// Returns the register used to hold the frame pointer.
376-
static Register getFPReg(const RISCVSubtarget &STI) { return RISCV::X8; }
377-
378-
// Returns the register used to hold the stack pointer.
379-
static Register getSPReg(const RISCVSubtarget &STI) { return RISCV::X2; }
380-
381386
static SmallVector<CalleeSavedInfo, 8>
382387
getUnmanagedCSI(const MachineFunction &MF,
383388
const std::vector<CalleeSavedInfo> &CSI) {
@@ -415,8 +420,6 @@ void RISCVFrameLowering::adjustStackForRVV(MachineFunction &MF,
415420
MachineInstr::MIFlag Flag) const {
416421
assert(Amount != 0 && "Did not need to adjust stack pointer for RVV.");
417422

418-
const Register SPReg = getSPReg(STI);
419-
420423
// Optimize compile time offset case
421424
StackOffset Offset = StackOffset::getScalable(Amount);
422425
if (auto VLEN = STI.getRealVLen()) {
@@ -479,7 +482,7 @@ static MCCFIInstruction createDefCFAExpression(const TargetRegisterInfo &TRI,
479482
unsigned DwarfReg = TRI.getDwarfRegNum(Reg, true);
480483
Expr.push_back((uint8_t)(dwarf::DW_OP_breg0 + DwarfReg));
481484
Expr.push_back(0);
482-
if (Reg == RISCV::X2)
485+
if (Reg == SPReg)
483486
Comment << "sp";
484487
else
485488
Comment << printReg(Reg, &TRI);
@@ -530,8 +533,6 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
530533
const RISCVInstrInfo *TII = STI.getInstrInfo();
531534
MachineBasicBlock::iterator MBBI = MBB.begin();
532535

533-
Register FPReg = getFPReg(STI);
534-
Register SPReg = getSPReg(STI);
535536
Register BPReg = RISCVABI::getBPReg();
536537

537538
// Debug location must be unknown since the first debug location is used
@@ -762,8 +763,6 @@ void RISCVFrameLowering::deallocateStack(MachineFunction &MF,
762763
int64_t CFAOffset) const {
763764
const RISCVRegisterInfo *RI = STI.getRegisterInfo();
764765

765-
Register SPReg = getSPReg(STI);
766-
767766
RI->adjustReg(MBB, MBBI, DL, SPReg, SPReg, StackOffset::getFixed(StackSize),
768767
MachineInstr::FrameDestroy, getStackAlign());
769768
}
@@ -773,8 +772,6 @@ void RISCVFrameLowering::emitEpilogue(MachineFunction &MF,
773772
const RISCVRegisterInfo *RI = STI.getRegisterInfo();
774773
MachineFrameInfo &MFI = MF.getFrameInfo();
775774
auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
776-
Register FPReg = getFPReg(STI);
777-
Register SPReg = getSPReg(STI);
778775

779776
// All calls are tail calls in GHC calling conv, and functions have no
780777
// prologue/epilogue.
@@ -922,7 +919,7 @@ RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
922919
}
923920

924921
if (FI >= MinCSFI && FI <= MaxCSFI) {
925-
FrameReg = RISCV::X2;
922+
FrameReg = SPReg;
926923

927924
if (FirstSPAdjustAmount)
928925
Offset += StackOffset::getFixed(FirstSPAdjustAmount);
@@ -969,13 +966,13 @@ RISCVFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
969966
} else {
970967
// VarSize objects must be empty in this case!
971968
assert(!MFI.hasVarSizedObjects());
972-
FrameReg = RISCV::X2;
969+
FrameReg = SPReg;
973970
}
974971
} else {
975972
FrameReg = RI->getFrameRegister(MF);
976973
}
977974

978-
if (FrameReg == getFPReg(STI)) {
975+
if (FrameReg == FPReg) {
979976
Offset += StackOffset::getFixed(RVFI->getVarArgsSaveSize());
980977
// When using FP to access scalable vector objects, we need to minus
981978
// the frame size.
@@ -1067,8 +1064,8 @@ void RISCVFrameLowering::determineCalleeSaves(MachineFunction &MF,
10671064
// Unconditionally spill RA and FP only if the function uses a frame
10681065
// pointer.
10691066
if (hasFP(MF)) {
1070-
SavedRegs.set(RISCV::X1);
1071-
SavedRegs.set(RISCV::X8);
1067+
SavedRegs.set(RAReg);
1068+
SavedRegs.set(FPReg);
10721069
}
10731070
// Mark BP as used if function has dedicated base pointer.
10741071
if (hasBP(MF))
@@ -1328,7 +1325,6 @@ bool RISCVFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
13281325
MachineBasicBlock::iterator RISCVFrameLowering::eliminateCallFramePseudoInstr(
13291326
MachineFunction &MF, MachineBasicBlock &MBB,
13301327
MachineBasicBlock::iterator MI) const {
1331-
Register SPReg = RISCV::X2;
13321328
DebugLoc DL = MI->getDebugLoc();
13331329

13341330
if (!hasReservedCallFrame(MF)) {

0 commit comments

Comments
 (0)