Skip to content

Commit 0d17e1f

Browse files
authored
[LoongArch] Revert sp adjustment in prologue (#88110)
After commit 18c5f3c ("[RegisterScavenger][RISCV] Don't search for FrameSetup instrs if we were searching from Non-FrameSetup instrs"), we can revert the `sp` adjustment 4e2364a ("[LoongArch] Add emergency spill slot for GPR for large frames") to generate better code, as the issue with `RegScavenger` has been resolved. Fixes #88109
1 parent b7a93bc commit 0d17e1f

File tree

3 files changed

+22
-44
lines changed

3 files changed

+22
-44
lines changed

llvm/lib/Target/LoongArch/LoongArchFrameLowering.cpp

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -206,22 +206,19 @@ void LoongArchFrameLowering::emitPrologue(MachineFunction &MF,
206206
if (StackSize == 0 && !MFI.adjustsStack())
207207
return;
208208

209-
uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF, true);
210-
uint64_t SecondSPAdjustAmount = RealStackSize - FirstSPAdjustAmount;
209+
uint64_t FirstSPAdjustAmount = getFirstSPAdjustAmount(MF);
211210
// Split the SP adjustment to reduce the offsets of callee saved spill.
212211
if (FirstSPAdjustAmount)
213212
StackSize = FirstSPAdjustAmount;
214213

215214
// Adjust stack.
216215
adjustReg(MBB, MBBI, DL, SPReg, SPReg, -StackSize, MachineInstr::FrameSetup);
217-
if (FirstSPAdjustAmount != 2048 || SecondSPAdjustAmount == 0) {
218-
// Emit ".cfi_def_cfa_offset StackSize".
219-
unsigned CFIIndex =
220-
MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, StackSize));
221-
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
222-
.addCFIIndex(CFIIndex)
223-
.setMIFlag(MachineInstr::FrameSetup);
224-
}
216+
// Emit ".cfi_def_cfa_offset StackSize".
217+
unsigned CFIIndex =
218+
MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, StackSize));
219+
BuildMI(MBB, MBBI, DL, TII->get(TargetOpcode::CFI_INSTRUCTION))
220+
.addCFIIndex(CFIIndex)
221+
.setMIFlag(MachineInstr::FrameSetup);
225222

226223
const auto &CSI = MFI.getCalleeSavedInfo();
227224

@@ -258,25 +255,14 @@ void LoongArchFrameLowering::emitPrologue(MachineFunction &MF,
258255
}
259256

260257
// Emit the second SP adjustment after saving callee saved registers.
261-
if (FirstSPAdjustAmount && SecondSPAdjustAmount) {
262-
if (hasFP(MF)) {
263-
assert(SecondSPAdjustAmount > 0 &&
264-
"SecondSPAdjustAmount should be greater than zero");
265-
adjustReg(MBB, MBBI, DL, SPReg, SPReg, -SecondSPAdjustAmount,
266-
MachineInstr::FrameSetup);
267-
} else {
268-
// FIXME: RegScavenger will place the spill instruction before the
269-
// prologue if a VReg is created in the prologue. This will pollute the
270-
// caller's stack data. Therefore, until there is better way, we just use
271-
// the `addi.w/d` instruction for stack adjustment to ensure that VReg
272-
// will not be created.
273-
for (int Val = SecondSPAdjustAmount; Val > 0; Val -= 2048)
274-
BuildMI(MBB, MBBI, DL,
275-
TII->get(IsLA64 ? LoongArch::ADDI_D : LoongArch::ADDI_W), SPReg)
276-
.addReg(SPReg)
277-
.addImm(Val < 2048 ? -Val : -2048)
278-
.setMIFlag(MachineInstr::FrameSetup);
258+
if (FirstSPAdjustAmount) {
259+
uint64_t SecondSPAdjustAmount = RealStackSize - FirstSPAdjustAmount;
260+
assert(SecondSPAdjustAmount > 0 &&
261+
"SecondSPAdjustAmount should be greater than zero");
262+
adjustReg(MBB, MBBI, DL, SPReg, SPReg, -SecondSPAdjustAmount,
263+
MachineInstr::FrameSetup);
279264

265+
if (!hasFP(MF)) {
280266
// If we are using a frame-pointer, and thus emitted ".cfi_def_cfa fp, 0",
281267
// don't emit an sp-based .cfi_def_cfa_offset
282268
// Emit ".cfi_def_cfa_offset RealStackSize"
@@ -369,27 +355,20 @@ void LoongArchFrameLowering::emitEpilogue(MachineFunction &MF,
369355
// st.d $ra, $sp, 2024
370356
// st.d $fp, $sp, 2016
371357
// addi.d $sp, $sp, -16
372-
uint64_t
373-
LoongArchFrameLowering::getFirstSPAdjustAmount(const MachineFunction &MF,
374-
bool IsPrologue) const {
358+
uint64_t LoongArchFrameLowering::getFirstSPAdjustAmount(
359+
const MachineFunction &MF) const {
375360
const MachineFrameInfo &MFI = MF.getFrameInfo();
376361
const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
377362

378363
// Return the FirstSPAdjustAmount if the StackSize can not fit in a signed
379364
// 12-bit and there exists a callee-saved register needing to be pushed.
380-
if (!isInt<12>(MFI.getStackSize())) {
365+
if (!isInt<12>(MFI.getStackSize()) && (CSI.size() > 0)) {
381366
// FirstSPAdjustAmount is chosen as (2048 - StackAlign) because 2048 will
382367
// cause sp = sp + 2048 in the epilogue to be split into multiple
383368
// instructions. Offsets smaller than 2048 can fit in a single load/store
384369
// instruction, and we have to stick with the stack alignment.
385370
// So (2048 - StackAlign) will satisfy the stack alignment.
386-
//
387-
// FIXME: This place may seem odd. When using multiple ADDI instructions to
388-
// adjust the stack in Prologue, and there are no callee-saved registers, we
389-
// can take advantage of the logic of split sp ajustment to reduce code
390-
// changes.
391-
return CSI.size() > 0 ? 2048 - getStackAlign().value()
392-
: (IsPrologue ? 2048 : 0);
371+
return 2048 - getStackAlign().value();
393372
}
394373
return 0;
395374
}

llvm/lib/Target/LoongArch/LoongArchFrameLowering.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class LoongArchFrameLowering : public TargetFrameLowering {
5252
bool hasFP(const MachineFunction &MF) const override;
5353
bool hasBP(const MachineFunction &MF) const;
5454

55-
uint64_t getFirstSPAdjustAmount(const MachineFunction &MF,
56-
bool IsPrologue = false) const;
55+
uint64_t getFirstSPAdjustAmount(const MachineFunction &MF) const;
5756

5857
bool enableShrinkWrapping(const MachineFunction &MF) const override;
5958

llvm/test/CodeGen/LoongArch/emergency-spill-slot.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
define void @func() {
77
; CHECK-LABEL: func:
88
; CHECK: # %bb.0:
9-
; CHECK-NEXT: addi.d $sp, $sp, -2048
10-
; CHECK-NEXT: addi.d $sp, $sp, -2048
11-
; CHECK-NEXT: addi.d $sp, $sp, -16
9+
; CHECK-NEXT: lu12i.w $a0, 1
10+
; CHECK-NEXT: ori $a0, $a0, 16
11+
; CHECK-NEXT: sub.d $sp, $sp, $a0
1212
; CHECK-NEXT: .cfi_def_cfa_offset 4112
1313
; CHECK-NEXT: pcalau12i $a0, %got_pc_hi20(var)
1414
; CHECK-NEXT: ld.d $a1, $a0, %got_pc_lo12(var)

0 commit comments

Comments
 (0)