[llvm] [X86] Skip debug instructions when adjusting the stack with pops (PR #229392)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 05:14:20 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-x86
Author: Yongqiang Tian (yqtian-se)
<details>
<summary>Changes</summary>
At `minsize`, X86 can discard one or two stack slots after a call using one-byte `popl` instructions instead of a larger `addl` instruction. Adding debug information could prevent this optimization and change the ordinary generated code.
`eliminateCallFramePseudoInstr()` removes the call-frame adjustment pseudo and advances the cleanup insertion point past debug instructions. `adjustStackWithPops()` then used `std::prev()` to inspect the preceding instruction. If a `DBG_VALUE` appeared between the call and the insertion point, it saw the debug instruction instead of the call and rejected the pop-based cleanup.
Use `prev_nodbg()` to find the preceding call while preserving the existing pseudo-probe behavior. The regression test places a debug value after a call and verifies that the two-slot cleanup still uses two `popl` instructions rather than `addl $8, %esp`.
The regression check fails before this change and passes afterward. The complete X86 CodeGen test suite also passes.
---
Full diff: https://github.com/llvm/llvm-project/pull/229392.diff
2 Files Affected:
- (modified) llvm/lib/Target/X86/X86FrameLowering.cpp (+3-3)
- (modified) llvm/test/CodeGen/X86/pop-stack-cleanup.ll (+26)
``````````diff
diff --git a/llvm/lib/Target/X86/X86FrameLowering.cpp b/llvm/lib/Target/X86/X86FrameLowering.cpp
index 73c4daab75648..8fc51e4058d36 100644
--- a/llvm/lib/Target/X86/X86FrameLowering.cpp
+++ b/llvm/lib/Target/X86/X86FrameLowering.cpp
@@ -4006,11 +4006,11 @@ bool X86FrameLowering::adjustStackWithPops(MachineBasicBlock &MBB,
if (NumPops != 1 && NumPops != 2)
return false;
- // Handle only the trivial case where the adjustment directly follows
- // a call. This is the most common one, anyway.
+ // Handle only the trivial case where the adjustment follows a call, ignoring
+ // debug instructions. This is the most common one, anyway.
if (MBBI == MBB.begin())
return false;
- MachineBasicBlock::iterator Prev = std::prev(MBBI);
+ MachineBasicBlock::iterator Prev = prev_nodbg(MBBI, MBB.begin());
if (!Prev->isCall() || !Prev->getOperand(1).isRegMask())
return false;
diff --git a/llvm/test/CodeGen/X86/pop-stack-cleanup.ll b/llvm/test/CodeGen/X86/pop-stack-cleanup.ll
index 050fb41ed218f..2cd414e2dc6ed 100644
--- a/llvm/test/CodeGen/X86/pop-stack-cleanup.ll
+++ b/llvm/test/CodeGen/X86/pop-stack-cleanup.ll
@@ -34,6 +34,19 @@ define void @test() minsize nounwind {
ret void
}
+define void @debug_value_after_call(i32 %a) minsize nounwind !dbg !5 {
+; CHECK-LABEL: debug_value_after_call:
+; CHECK: calll _param2
+; CHECK-NOT: addl $8, %esp
+; CHECK: popl %eax
+; CHECK-NEXT: popl %ecx
+; CHECK: calll _param2
+ call void @param2(i32 %a, i32 %a), !dbg !9
+ #dbg_value(i32 5, !8, !DIExpression(), !9)
+ call void @param2(i32 5, i32 %a), !dbg !9
+ ret void, !dbg !9
+}
+
define void @negative(i32 %k) {
; CHECK-LABEL: negative:
; CHECK: calll _param1
@@ -85,3 +98,16 @@ define i32 @test_linux64_i32(i32 %size) minsize nounwind {
%r = call i32 @param8_ret(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8)
ret i32 %r
}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, emissionKind: FullDebug)
+!1 = !DIFile(filename: "t.c", directory: "/")
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "debug_value_after_call", scope: !1, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !7)
+!6 = !DISubroutineType(types: !7)
+!7 = !{}
+!8 = !DILocalVariable(name: "i", scope: !5, file: !1, line: 2, type: !10)
+!9 = !DILocation(line: 2, column: 1, scope: !5)
+!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
``````````
</details>
https://github.com/llvm/llvm-project/pull/229392
More information about the llvm-commits
mailing list