Skip to content

Commit 313b71f

Browse files
authored
[RISCV] Simplify tracking of tracking and encoding of push/pop in RISCVFrameLowering. NFC (#129343)
Previously we calculated the max register id. Then converted it to the number of registers and encoding. Then converted number of registers to stack size. Then saved number of registers, encoding, and stack size to MachineFunctionInfo. This patch removes the calculation of max register id, and instead calculates the number of registers. The encoding is removed from MachineFunctionInfo in favor of converting the number of registers to encoding at the time of use.
1 parent efb880d commit 313b71f

File tree

3 files changed

+26
-56
lines changed

3 files changed

+26
-56
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,15 @@ inline unsigned encodeRlist(MCRegister EndReg, bool IsRV32E = false) {
624624
}
625625
}
626626

627+
inline static unsigned encodeRlistNumRegs(unsigned NumRegs) {
628+
assert(NumRegs > 0 && NumRegs < 14 && NumRegs != 12 &&
629+
"Unexpected number of registers");
630+
if (NumRegs == 13)
631+
return RLISTENCODE::RA_S0_S11;
632+
633+
return RLISTENCODE::RA + (NumRegs - 1);
634+
}
635+
627636
inline static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64) {
628637
assert(RlistVal != RLISTENCODE::INVALID_RLIST &&
629638
"{ra, s0-s10} is not supported, s11 must be included.");

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -332,51 +332,19 @@ getRestoreLibCallName(const MachineFunction &MF,
332332
return RestoreLibCalls[LibCallID];
333333
}
334334

335-
// Return encoded value and register count for PUSH/POP instruction,
336-
// representing registers to store/load.
337-
static std::pair<unsigned, unsigned>
338-
getPushPopEncodingAndNum(const Register MaxReg) {
339-
switch (MaxReg.id()) {
340-
default:
341-
llvm_unreachable("Unexpected Reg for Push/Pop Inst");
342-
case RISCV::X27: /*s11*/
343-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S11, 13);
344-
case RISCV::X25: /*s9*/
345-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S9, 11);
346-
case RISCV::X24: /*s8*/
347-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S8, 10);
348-
case RISCV::X23: /*s7*/
349-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S7, 9);
350-
case RISCV::X22: /*s6*/
351-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S6, 8);
352-
case RISCV::X21: /*s5*/
353-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S5, 7);
354-
case RISCV::X20: /*s4*/
355-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S4, 6);
356-
case RISCV::X19: /*s3*/
357-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S3, 5);
358-
case RISCV::X18: /*s2*/
359-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S2, 4);
360-
case RISCV::X9: /*s1*/
361-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0_S1, 3);
362-
case FPReg: /*s0*/
363-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA_S0, 2);
364-
case RAReg: /*ra*/
365-
return std::make_pair(llvm::RISCVZC::RLISTENCODE::RA, 1);
366-
}
367-
}
368-
369335
// Get the max reg of Push/Pop for restoring callee saved registers.
370-
static Register getMaxPushPopReg(const std::vector<CalleeSavedInfo> &CSI) {
371-
MCRegister MaxPushPopReg;
336+
static unsigned getNumPushPopRegs(const std::vector<CalleeSavedInfo> &CSI) {
337+
unsigned NumPushPopRegs = 0;
372338
for (auto &CS : CSI) {
373-
if (llvm::find_if(FixedCSRFIMap, [&](MCPhysReg P) {
374-
return P == CS.getReg();
375-
}) != std::end(FixedCSRFIMap))
376-
MaxPushPopReg = std::max(MaxPushPopReg.id(), CS.getReg().id());
339+
auto *FII = llvm::find_if(FixedCSRFIMap,
340+
[&](MCPhysReg P) { return P == CS.getReg(); });
341+
if (FII != std::end(FixedCSRFIMap)) {
342+
unsigned RegNum = std::distance(std::begin(FixedCSRFIMap), FII);
343+
NumPushPopRegs = std::max(NumPushPopRegs, RegNum + 1);
344+
}
377345
}
378-
assert(MaxPushPopReg != RISCV::X26 && "x26 requires x27 to also be pushed");
379-
return MaxPushPopReg;
346+
assert(NumPushPopRegs != 12 && "x26 requires x27 to also be pushed");
347+
return NumPushPopRegs;
380348
}
381349

382350
// Return true if the specified function should have a dedicated frame
@@ -1790,14 +1758,10 @@ bool RISCVFrameLowering::assignCalleeSavedSpillSlots(
17901758

17911759
if (RVFI->isPushable(MF)) {
17921760
// Determine how many GPRs we need to push and save it to RVFI.
1793-
Register MaxReg = getMaxPushPopReg(CSI);
1794-
if (MaxReg != RISCV::NoRegister) {
1795-
auto [RegEnc, PushedRegNum] = getPushPopEncodingAndNum(MaxReg);
1761+
unsigned PushedRegNum = getNumPushPopRegs(CSI);
1762+
if (PushedRegNum) {
17961763
RVFI->setRVPushRegs(PushedRegNum);
17971764
RVFI->setRVPushStackSize(alignTo((STI.getXLen() / 8) * PushedRegNum, 16));
1798-
1799-
// Use encoded number to represent registers to spill.
1800-
RVFI->setRVPushRlist(RegEnc);
18011765
}
18021766
}
18031767

@@ -1881,11 +1845,11 @@ bool RISCVFrameLowering::spillCalleeSavedRegisters(
18811845
unsigned PushedRegNum = RVFI->getRVPushRegs();
18821846
if (PushedRegNum > 0) {
18831847
// Use encoded number to represent registers to spill.
1884-
int RegEnc = RVFI->getRVPushRlist();
1848+
unsigned RegEnc = RISCVZC::encodeRlistNumRegs(PushedRegNum);
18851849
MachineInstrBuilder PushBuilder =
18861850
BuildMI(MBB, MI, DL, TII.get(RISCV::CM_PUSH))
18871851
.setMIFlag(MachineInstr::FrameSetup);
1888-
PushBuilder.addImm((int64_t)RegEnc);
1852+
PushBuilder.addImm(RegEnc);
18891853
PushBuilder.addImm(0);
18901854

18911855
for (unsigned i = 0; i < PushedRegNum; i++)
@@ -2034,8 +1998,9 @@ bool RISCVFrameLowering::restoreCalleeSavedRegisters(
20341998

20351999
RISCVMachineFunctionInfo *RVFI = MF->getInfo<RISCVMachineFunctionInfo>();
20362000
if (RVFI->isPushable(*MF)) {
2037-
int RegEnc = RVFI->getRVPushRlist();
2038-
if (RegEnc != llvm::RISCVZC::RLISTENCODE::INVALID_RLIST) {
2001+
unsigned PushedRegNum = RVFI->getRVPushRegs();
2002+
if (PushedRegNum > 0) {
2003+
unsigned RegEnc = RISCVZC::encodeRlistNumRegs(PushedRegNum);
20392004
MachineInstrBuilder PopBuilder =
20402005
BuildMI(MBB, MI, DL, TII.get(RISCV::CM_POP))
20412006
.setMIFlag(MachineInstr::FrameDestroy);

llvm/lib/Target/RISCV/RISCVMachineFunctionInfo.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class RISCVMachineFunctionInfo : public MachineFunctionInfo {
7474
/// Size of stack frame for Zcmp PUSH/POP
7575
unsigned RVPushStackSize = 0;
7676
unsigned RVPushRegs = 0;
77-
int RVPushRlist = llvm::RISCVZC::RLISTENCODE::INVALID_RLIST;
7877

7978
int64_t StackProbeSize = 0;
8079

@@ -146,9 +145,6 @@ class RISCVMachineFunctionInfo : public MachineFunctionInfo {
146145
VarArgsSaveSize == 0;
147146
}
148147

149-
int getRVPushRlist() const { return RVPushRlist; }
150-
void setRVPushRlist(int Rlist) { RVPushRlist = Rlist; }
151-
152148
unsigned getRVPushRegs() const { return RVPushRegs; }
153149
void setRVPushRegs(unsigned Regs) { RVPushRegs = Regs; }
154150

0 commit comments

Comments
 (0)