[llvm] [X86] Recognize POP/ADD/SUB modifying rsp in getSPAdjust. (PR #114265)
Daniel Zabawa via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 30 09:57:18 PDT 2024
https://github.com/daniel-zabawa created https://github.com/llvm/llvm-project/pull/114265
This code assumed only PUSHes would appear in call sequences. However, if calls require frame-pointer/base-pointer spills, only the PUSH operations inserted by spillFPBP will be recognized, and the adjustments to frame object offsets in prologepilog will be incorrect.
This change correctly reports the SP adjustment for POP and ADD/SUB to rsp, and an assertion for unrecognized instructions that modify rsp.
There is no unit test provided as the issue can only be reliably reproduced in MIR (to force a spill/restore outside and inside a call sequence, respectively), but MIR cannot serialize the FPClobberedByCall field that triggers spillFPBP to run.
>From cb1a106044c5ea81539582d5f53db049715621c0 Mon Sep 17 00:00:00 2001
From: "Zabawa, Daniel" <daniel.zabawa at intel.com>
Date: Wed, 30 Oct 2024 09:54:04 -0700
Subject: [PATCH] [X86] Recognize POP/ADD/SUB modifying rsp in getSPAdjust.
This code assumed only PUSHes would appear in call sequences. However,
if calls require frame-pointer/base-pointer spills, only the PUSH
operations inserted by spillFPBP will be recognized, and the adjustments
to frame object offsets in prologepilog will be incorrect.
This change correctly reports the SP adjustment for POP and ADD/SUB to
rsp, and an assertion for unrecognized instructions that modify rsp.
There is no unit test provided as the issue can only be reliably
reproduced in MIR (to force a spill/restore outside and inside a call
sequence, respectively), but MIR cannot serialize the FPClobberedByCall
field that triggers spillFPBP to run.
---
llvm/lib/Target/X86/X86InstrInfo.cpp | 31 ++++++++++++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/X86/X86InstrInfo.cpp b/llvm/lib/Target/X86/X86InstrInfo.cpp
index 38ea1f35be2b9a..e2285417a155e4 100644
--- a/llvm/lib/Target/X86/X86InstrInfo.cpp
+++ b/llvm/lib/Target/X86/X86InstrInfo.cpp
@@ -452,10 +452,13 @@ int X86InstrInfo::getSPAdjust(const MachineInstr &MI) const {
return -(I->getOperand(1).getImm());
}
- // Currently handle only PUSHes we can reasonably expect to see
- // in call sequences
+ // Handle other opcodes we reasonably expect to see in call
+ // sequences. Note this may include spill/restore of FP/BP.
switch (MI.getOpcode()) {
default:
+ assert(!(MI.modifiesRegister(X86::RSP, &RI) ||
+ MI.getDesc().hasImplicitDefOfPhysReg(X86::RSP)) &&
+ "Unhandled opcode in getSPAdjust");
return 0;
case X86::PUSH32r:
case X86::PUSH32rmm:
@@ -467,6 +470,30 @@ int X86InstrInfo::getSPAdjust(const MachineInstr &MI) const {
case X86::PUSH64rmr:
case X86::PUSH64i32:
return 8;
+ case X86::POP32r:
+ case X86::POP32rmm:
+ case X86::POP32rmr:
+ return -4;
+ case X86::POP64r:
+ case X86::POP64rmm:
+ case X86::POP64rmr:
+ return -8;
+ // FIXME: (implement and) use isAddImmediate in the
+ // default case instead of the following ADD/SUB cases.
+ case X86::ADD32ri:
+ case X86::ADD32ri8:
+ case X86::ADD64ri32:
+ if (MI.getOperand(0).getReg() == X86::RSP &&
+ MI.getOperand(1).getReg() == X86::RSP)
+ return -MI.getOperand(2).getImm();
+ return 0;
+ case X86::SUB32ri:
+ case X86::SUB32ri8:
+ case X86::SUB64ri32:
+ if (MI.getOperand(0).getReg() == X86::RSP &&
+ MI.getOperand(1).getReg() == X86::RSP)
+ return MI.getOperand(2).getImm();
+ return 0;
}
}
More information about the llvm-commits
mailing list