[llvm] 191b4f5 - X86: Handle undef push operands in call frame optimization (#220368)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 3 06:00:59 PDT 2026


Author: Matt Arsenault
Date: 2026-09-03T13:00:53Z
New Revision: 191b4f522082ca64c63591b61ec2d40091596a5e

URL: https://github.com/llvm/llvm-project/commit/191b4f522082ca64c63591b61ec2d40091596a5e
DIFF: https://github.com/llvm/llvm-project/commit/191b4f522082ca64c63591b61ec2d40091596a5e.diff

LOG: X86: Handle undef push operands in call frame optimization (#220368)

Avoid unchecked getVRegDef call, which will fail for undef
operands. The register-push fallback also dropped the undef flag when
rebuilding the store as a PUSH.

Co-Authored-By: Claude <noreply at anthropic.com> (Claude Opus 4.8)

Added: 
    llvm/test/CodeGen/X86/callframe-opt-undef-push.mir

Modified: 
    llvm/lib/Target/X86/X86CallFrameOptimization.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/X86CallFrameOptimization.cpp b/llvm/lib/Target/X86/X86CallFrameOptimization.cpp
index b3dedbc1e436a..4966ec2fe8dc9 100644
--- a/llvm/lib/Target/X86/X86CallFrameOptimization.cpp
+++ b/llvm/lib/Target/X86/X86CallFrameOptimization.cpp
@@ -539,6 +539,7 @@ void X86CallFrameOptimizationImpl::adjustCallSequence(
 
       // If storing a 32-bit vreg on 64-bit targets, extend to a 64-bit vreg
       // in preparation for the PUSH64. The upper 32 bits can be undef.
+      bool RegIsUndef = PushOp.isUndef();
       if (Is64Bit && Store->getOpcode() == X86::MOV32mr) {
         Register UndefReg = MRI->createVirtualRegister(&X86::GR64RegClass);
         Reg = MRI->createVirtualRegister(&X86::GR64RegClass);
@@ -547,6 +548,7 @@ void X86CallFrameOptimizationImpl::adjustCallSequence(
             .addReg(UndefReg)
             .add(PushOp)
             .addImm(X86::sub_32bit);
+        RegIsUndef = false;
       }
 
       // If PUSHrmm is not slow on this target, try to fold the source of the
@@ -568,7 +570,7 @@ void X86CallFrameOptimizationImpl::adjustCallSequence(
       } else {
         PushOpcode = Is64Bit ? X86::PUSH64r : X86::PUSH32r;
         Push = BuildMI(MBB, Context.Call, DL, TII->get(PushOpcode))
-                   .addReg(Reg)
+                   .addReg(Reg, getUndefRegState(RegIsUndef))
                    .getInstr();
         Push->cloneMemRefs(MF, *Store);
       }
@@ -617,22 +619,24 @@ MachineInstr *X86CallFrameOptimizationImpl::canFoldIntoRegPush(
   if (!MRI->hasOneNonDBGUse(Reg))
     return nullptr;
 
-  MachineInstr &DefMI = *MRI->getVRegDef(Reg);
+  MachineInstr *DefMI = MRI->getVRegDef(Reg);
+  if (!DefMI)
+    return nullptr;
 
   // Make sure the def is a MOV from memory.
   // If the def is in another block, give up.
-  if ((DefMI.getOpcode() != X86::MOV32rm &&
-       DefMI.getOpcode() != X86::MOV64rm) ||
-      DefMI.getParent() != FrameSetup->getParent())
+  if ((DefMI->getOpcode() != X86::MOV32rm &&
+       DefMI->getOpcode() != X86::MOV64rm) ||
+      DefMI->getParent() != FrameSetup->getParent())
     return nullptr;
 
   // Make sure we don't have any instructions between DefMI and the
   // push that make folding the load illegal.
-  for (MachineBasicBlock::iterator I = DefMI; I != FrameSetup; ++I)
+  for (MachineBasicBlock::iterator I = *DefMI; I != FrameSetup; ++I)
     if (I->isLoadFoldBarrier())
       return nullptr;
 
-  return &DefMI;
+  return DefMI;
 }
 
 FunctionPass *llvm::createX86CallFrameOptimizationLegacyPass() {

diff  --git a/llvm/test/CodeGen/X86/callframe-opt-undef-push.mir b/llvm/test/CodeGen/X86/callframe-opt-undef-push.mir
new file mode 100644
index 0000000000000..47b22ee3cafb6
--- /dev/null
+++ b/llvm/test/CodeGen/X86/callframe-opt-undef-push.mir
@@ -0,0 +1,39 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=i686-- --run-pass=x86-cf-opt %s -o - | FileCheck %s
+# RUN: llc -mtriple=i686-- -passes=x86-cf-opt %s -o - | FileCheck %s
+
+# An undef push operand has no defining instruction and must not crash
+
+--- |
+  target triple = "i686-unknown-linux-gnu"
+  declare void @g(i32, i32)
+  define void @undef_push(i32 %x) { ret void }
+...
+---
+name:            undef_push
+tracksRegLiveness: true
+body:             |
+  bb.0:
+    liveins: $edi
+
+    ; CHECK-LABEL: name: undef_push
+    ; CHECK: liveins: $edi
+    ; CHECK-NEXT: {{  $}}
+    ; CHECK-NEXT: [[COPY:%[0-9]+]]:gr32 = COPY $edi
+    ; CHECK-NEXT: ADJCALLSTACKDOWN32 8, 0, 8, implicit-def dead $esp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $esp, implicit $ssp
+    ; CHECK-NEXT: PUSH32r [[COPY]], implicit-def $esp, implicit $esp :: (store (s32) into stack + 4)
+    ; CHECK-NEXT: CFI_INSTRUCTION adjust_cfa_offset 4
+    ; CHECK-NEXT: PUSH32r undef %2:gr32, implicit-def $esp, implicit $esp :: (store (s32) into stack)
+    ; CHECK-NEXT: CFI_INSTRUCTION adjust_cfa_offset 4
+    ; CHECK-NEXT: CALLpcrel32 target-flags(x86-plt) @g, csr_32, implicit $esp, implicit $ssp, implicit-def $esp, implicit-def $ssp
+    ; CHECK-NEXT: ADJCALLSTACKUP32 8, 0, implicit-def dead $esp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $esp, implicit $ssp
+    ; CHECK-NEXT: RET32
+    %0:gr32 = COPY $edi
+    ADJCALLSTACKDOWN32 8, 0, 0, implicit-def dead $esp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $esp, implicit $ssp
+    %1:gr32 = COPY $esp
+    MOV32mr %1, 1, $noreg, 4, $noreg, %0 :: (store (s32) into stack + 4)
+    MOV32mr %1, 1, $noreg, 0, $noreg, undef %2:gr32 :: (store (s32) into stack)
+    CALLpcrel32 target-flags(x86-plt) @g, csr_32, implicit $esp, implicit $ssp, implicit-def $esp, implicit-def $ssp
+    ADJCALLSTACKUP32 8, 0, implicit-def dead $esp, implicit-def dead $eflags, implicit-def dead $ssp, implicit $esp, implicit $ssp
+    RET32
+...


        


More information about the llvm-commits mailing list