Skip to content

Commit cbfb3d9

Browse files
ShivaChentclin914
authored andcommitted
[RISCV] Fix incorrect calculation of stack size used by the libcall with the ABI ilp32e and lp64e.
The purpose of this patch is to fix incorrect calculation of stack size used by the libcall with the ABI ilp32e and lp64e. It's always 12 bytes for ilp32e and 24 byte for lp64e.
1 parent af47038 commit cbfb3d9

File tree

2 files changed

+240
-227
lines changed

2 files changed

+240
-227
lines changed

llvm/lib/Target/RISCV/RISCVFrameLowering.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ static void emitSCSEpilogue(MachineFunction &MF, MachineBasicBlock &MBB,
168168
// restored by the libcall, except it is zero-indexed - ID 0 corresponds to a
169169
// single register.
170170
static int getLibCallID(const MachineFunction &MF,
171-
const std::vector<CalleeSavedInfo> &CSI) {
171+
const std::vector<CalleeSavedInfo> &CSI,
172+
RISCVABI::ABI ABI) {
172173
const auto *RVFI = MF.getInfo<RISCVMachineFunctionInfo>();
173174

174175
if (CSI.empty() || !RVFI->useSaveRestoreLibCalls(MF))
@@ -184,6 +185,14 @@ static int getLibCallID(const MachineFunction &MF,
184185
if (MaxReg == RISCV::NoRegister)
185186
return -1;
186187

188+
// The libcall always save/restore ra/s0/s1 with the ABI ilp32e and lp64e.
189+
if (ABI == RISCVABI::ABI_ILP32E || ABI == RISCVABI::ABI_LP64E) {
190+
assert(
191+
(MaxReg == RISCV::X1 || MaxReg == RISCV::X8 || MaxReg == RISCV::X9) &&
192+
"Invalid MaxReg for ABI ilp32e and lp64e");
193+
MaxReg = RISCV::X9;
194+
}
195+
187196
switch (MaxReg) {
188197
default:
189198
llvm_unreachable("Something has gone wrong!");
@@ -205,9 +214,9 @@ static int getLibCallID(const MachineFunction &MF,
205214

206215
// Get the name of the libcall used for spilling callee saved registers.
207216
// If this function will not use save/restore libcalls, then return a nullptr.
208-
static const char *
209-
getSpillLibCallName(const MachineFunction &MF,
210-
const std::vector<CalleeSavedInfo> &CSI) {
217+
static const char *getSpillLibCallName(const MachineFunction &MF,
218+
const std::vector<CalleeSavedInfo> &CSI,
219+
RISCVABI::ABI ABI) {
211220
static const char *const SpillLibCalls[] = {
212221
"__riscv_save_0",
213222
"__riscv_save_1",
@@ -224,7 +233,7 @@ getSpillLibCallName(const MachineFunction &MF,
224233
"__riscv_save_12"
225234
};
226235

227-
int LibCallID = getLibCallID(MF, CSI);
236+
int LibCallID = getLibCallID(MF, CSI, ABI);
228237
if (LibCallID == -1)
229238
return nullptr;
230239
return SpillLibCalls[LibCallID];
@@ -234,7 +243,8 @@ getSpillLibCallName(const MachineFunction &MF,
234243
// If this function will not use save/restore libcalls, then return a nullptr.
235244
static const char *
236245
getRestoreLibCallName(const MachineFunction &MF,
237-
const std::vector<CalleeSavedInfo> &CSI) {
246+
const std::vector<CalleeSavedInfo> &CSI,
247+
RISCVABI::ABI ABI) {
238248
static const char *const RestoreLibCalls[] = {
239249
"__riscv_restore_0",
240250
"__riscv_restore_1",
@@ -251,7 +261,7 @@ getRestoreLibCallName(const MachineFunction &MF,
251261
"__riscv_restore_12"
252262
};
253263

254-
int LibCallID = getLibCallID(MF, CSI);
264+
int LibCallID = getLibCallID(MF, CSI, ABI);
255265
if (LibCallID == -1)
256266
return nullptr;
257267
return RestoreLibCalls[LibCallID];
@@ -573,8 +583,9 @@ void RISCVFrameLowering::emitPrologue(MachineFunction &MF,
573583
// For negative frame indices, the offset from the frame pointer will differ
574584
// depending on which of these groups the frame index applies to.
575585
// The following calculates the correct offset knowing the number of callee
576-
// saved registers spilt by the two methods.
577-
if (int LibCallRegs = getLibCallID(MF, MFI.getCalleeSavedInfo()) + 1) {
586+
// saved registers spilt by the two methods.STI.getTargetABI()
587+
if (int LibCallRegs =
588+
getLibCallID(MF, MFI.getCalleeSavedInfo(), STI.getTargetABI()) + 1) {
578589
// Calculate the size of the frame managed by the libcall. The stack
579590
// alignment of these libcalls should be the same as how we set it in
580591
// getABIStackAlignment.
@@ -1463,7 +1474,7 @@ bool RISCVFrameLowering::assignCalleeSavedSpillSlots(
14631474
if (RVFI->isPushable(MF)) {
14641475
if (int64_t PushSize = RVFI->getRVPushStackSize())
14651476
MFI.CreateFixedSpillStackObject(PushSize, -PushSize);
1466-
} else if (int LibCallRegs = getLibCallID(MF, CSI) + 1) {
1477+
} else if (int LibCallRegs = getLibCallID(MF, CSI, STI.getTargetABI()) + 1) {
14671478
int64_t LibCallFrameSize =
14681479
alignTo((STI.getXLen() / 8) * LibCallRegs, getStackAlign());
14691480
MFI.CreateFixedSpillStackObject(LibCallFrameSize, -LibCallFrameSize);
@@ -1500,7 +1511,8 @@ bool RISCVFrameLowering::spillCalleeSavedRegisters(
15001511
for (unsigned i = 0; i < PushedRegNum; i++)
15011512
PushBuilder.addUse(FixedCSRFIMap[i].first, RegState::Implicit);
15021513
}
1503-
} else if (const char *SpillLibCall = getSpillLibCallName(*MF, CSI)) {
1514+
} else if (const char *SpillLibCall =
1515+
getSpillLibCallName(*MF, CSI, STI.getTargetABI())) {
15041516
// Add spill libcall via non-callee-saved register t0.
15051517
BuildMI(MBB, MI, DL, TII.get(RISCV::PseudoCALLReg), RISCV::X5)
15061518
.addExternalSymbol(SpillLibCall, RISCVII::MO_CALL)
@@ -1625,7 +1637,8 @@ bool RISCVFrameLowering::restoreCalleeSavedRegisters(
16251637
PopBuilder.addDef(FixedCSRFIMap[i].first, RegState::ImplicitDefine);
16261638
}
16271639
} else {
1628-
const char *RestoreLibCall = getRestoreLibCallName(*MF, CSI);
1640+
const char *RestoreLibCall =
1641+
getRestoreLibCallName(*MF, CSI, STI.getTargetABI());
16291642
if (RestoreLibCall) {
16301643
// Add restore libcall via tail call.
16311644
MachineBasicBlock::iterator NewMI =

0 commit comments

Comments
 (0)