[llvm] [RISCV] Ignore debug instructions in epilogue block checks (PR #219401)
Yongqiang Tian via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 10 17:11:59 PDT 2026
https://github.com/yqtian-se updated https://github.com/llvm/llvm-project/pull/219401
>From 7808aeffc7ef3f678c2f213ea1a87c82b2469f0a Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Fri, 11 Sep 2026 00:11:13 +0000
Subject: [PATCH 1/2] [RISCV] Add a test for debug-sensitive shrink wrapping
Add IR coverage documenting how a debug instruction in the return block prevents save/restore shrink wrapping. Generate the assembly checks with update_llc_test_checks.py.
Assisted-by: OpenAI Codex 5.6 Sol, co-reviewed by Claude and Yongqiang Tian.
---
.../RISCV/shrinkwrap-save-restore-debug.ll | 92 +++++++++++++++++++
1 file changed, 92 insertions(+)
create mode 100644 llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-debug.ll
diff --git a/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-debug.ll b/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-debug.ll
new file mode 100644
index 0000000000000..9d2232e6ff8c8
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-debug.ll
@@ -0,0 +1,92 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv32 -mattr=+save-restore -verify-machineinstrs < %s | FileCheck %s
+;
+; Compare save/restore shrink wrapping with and without a debug instruction in
+; the common return block.
+
+declare void @notdead(ptr)
+
+define void @without_debug(i32 %n) nounwind {
+; CHECK-LABEL: without_debug:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: li a1, 32
+; CHECK-NEXT: bltu a1, a0, .LBB0_2
+; CHECK-NEXT: # %bb.1: # %if.then
+; CHECK-NEXT: call t0, __riscv_save_2
+; CHECK-NEXT: addi s0, sp, 16
+; CHECK-NEXT: mv s1, sp
+; CHECK-NEXT: addi a0, a0, 15
+; CHECK-NEXT: andi a0, a0, -16
+; CHECK-NEXT: sub a0, sp, a0
+; CHECK-NEXT: mv sp, a0
+; CHECK-NEXT: call notdead
+; CHECK-NEXT: mv sp, s1
+; CHECK-NEXT: addi sp, s0, -16
+; CHECK-NEXT: tail __riscv_restore_2
+; CHECK-NEXT: .LBB0_2: # %if.end
+; CHECK-NEXT: ret
+entry:
+ %cmp = icmp ult i32 %n, 33
+ br i1 %cmp, label %if.then, label %if.end
+
+if.then:
+ %size = zext i32 %n to i64
+ %stack = call ptr @llvm.stacksave.p0()
+ %buffer = alloca i8, i64 %size, align 16
+ call void @notdead(ptr %buffer)
+ call void @llvm.stackrestore.p0(ptr %stack)
+ br label %if.end
+
+if.end:
+ ret void
+}
+
+define void @with_debug(i32 %n) nounwind !dbg !4 {
+; CHECK-LABEL: with_debug:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: call t0, __riscv_save_2
+; CHECK-NEXT: addi s0, sp, 16
+; CHECK-NEXT: li a1, 32
+; CHECK-NEXT: bltu a1, a0, .LBB1_2
+; CHECK-NEXT: # %bb.1: # %if.then
+; CHECK-NEXT: mv s1, sp
+; CHECK-NEXT: addi a0, a0, 15
+; CHECK-NEXT: andi a0, a0, -16
+; CHECK-NEXT: sub a0, sp, a0
+; CHECK-NEXT: mv sp, a0
+; CHECK-NEXT: call notdead
+; CHECK-NEXT: mv sp, s1
+; CHECK-NEXT: .LBB1_2: # %if.end
+; CHECK-NEXT: #DEBUG_VALUE: with_debug:ghost <- 0
+; CHECK-NEXT: addi sp, s0, -16
+; CHECK-NEXT: tail __riscv_restore_2
+entry:
+ %cmp = icmp ult i32 %n, 33
+ br i1 %cmp, label %if.then, label %if.end
+
+if.then:
+ %size = zext i32 %n to i64
+ %stack = call ptr @llvm.stacksave.p0()
+ %buffer = alloca i8, i64 %size, align 16
+ call void @notdead(ptr %buffer)
+ call void @llvm.stackrestore.p0(ptr %stack)
+ br label %if.end
+
+if.end:
+ #dbg_value(i32 0, !8, !DIExpression(), !9)
+ ret void, !dbg !10
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3}
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: true, emissionKind: NoDebug)
+!1 = !DIFile(filename: "x.c", directory: "")
+!2 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = distinct !DISubprogram(name: "with_debug", scope: !1, file: !1, line: 1, type: !5, unit: !0, retainedNodes: !6)
+!5 = !DISubroutineType(types: !11)
+!6 = !{!8}
+!8 = !DILocalVariable(name: "ghost", scope: !4, file: !1, line: 2, type: !2)
+!9 = !DILocation(line: 0, scope: !4)
+!10 = !DILocation(line: 3, scope: !4)
+!11 = !{null, !2}
>From 56dbe7126b37b2e4bd2b7761d2a3d0ab4ffcd1a9 Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Fri, 11 Sep 2026 00:11:25 +0000
Subject: [PATCH 2/2] [RISCV] Ignore debug instructions in epilogue block
checks
Ignore debug instructions when checking whether a return successor contains only the return. Keep iterating over the raw instruction range so bundles, pseudo probes, and other non-debug instructions retain their existing behavior.
Assisted-by: OpenAI Codex 5.6 Sol, co-reviewed by Claude and Yongqiang Tian.
---
llvm/lib/Target/RISCV/RISCVFrameLowering.cpp | 10 +++++++---
.../CodeGen/RISCV/shrinkwrap-save-restore-debug.ll | 9 +++++----
2 files changed, 12 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
index 7f2f70a1cf185..d2815db0b8b34 100644
--- a/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVFrameLowering.cpp
@@ -2713,9 +2713,13 @@ bool RISCVFrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const {
if (!SuccMBB)
return true;
- // The successor can only contain a return, since we would effectively be
- // replacing the successor with our own tail return at the end of our block.
- return SuccMBB->isReturnBlock() && SuccMBB->size() == 1;
+ // The successor can only contain a return and debug instructions, since we
+ // would effectively replace it with our own tail return at the end of this
+ // block. The debug instructions would not execute on the tail-return path.
+ return SuccMBB->isReturnBlock() &&
+ llvm::count_if(SuccMBB->instrs(), [](const MachineInstr &MI) {
+ return !MI.isDebugInstr();
+ }) == 1;
}
bool RISCVFrameLowering::isSupportedStackID(TargetStackID::Value ID) const {
diff --git a/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-debug.ll b/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-debug.ll
index 9d2232e6ff8c8..77ab591e7e4bb 100644
--- a/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-debug.ll
+++ b/llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-debug.ll
@@ -44,11 +44,11 @@ if.end:
define void @with_debug(i32 %n) nounwind !dbg !4 {
; CHECK-LABEL: with_debug:
; CHECK: # %bb.0: # %entry
-; CHECK-NEXT: call t0, __riscv_save_2
-; CHECK-NEXT: addi s0, sp, 16
; CHECK-NEXT: li a1, 32
; CHECK-NEXT: bltu a1, a0, .LBB1_2
; CHECK-NEXT: # %bb.1: # %if.then
+; CHECK-NEXT: call t0, __riscv_save_2
+; CHECK-NEXT: addi s0, sp, 16
; CHECK-NEXT: mv s1, sp
; CHECK-NEXT: addi a0, a0, 15
; CHECK-NEXT: andi a0, a0, -16
@@ -56,10 +56,11 @@ define void @with_debug(i32 %n) nounwind !dbg !4 {
; CHECK-NEXT: mv sp, a0
; CHECK-NEXT: call notdead
; CHECK-NEXT: mv sp, s1
-; CHECK-NEXT: .LBB1_2: # %if.end
-; CHECK-NEXT: #DEBUG_VALUE: with_debug:ghost <- 0
; CHECK-NEXT: addi sp, s0, -16
; CHECK-NEXT: tail __riscv_restore_2
+; CHECK-NEXT: .LBB1_2: # %if.end
+; CHECK-NEXT: #DEBUG_VALUE: with_debug:ghost <- 0
+; CHECK-NEXT: ret
entry:
%cmp = icmp ult i32 %n, 33
br i1 %cmp, label %if.then, label %if.end
More information about the llvm-commits
mailing list