[llvm] [SPARC] Prevent RESTORE from consuming an indirect call's target (PR #228198)
Imre Kaloz via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 4 16:55:52 PDT 2026
https://github.com/kaloz updated https://github.com/llvm/llvm-project/pull/228198
>From bc9d5128e6b3e6bd46b7d2ca78a237c20dc8ebe0 Mon Sep 17 00:00:00 2001
From: Imre Kaloz <kaloz at kernel.org>
Date: Tue, 1 Sep 2026 23:34:24 +0200
Subject: [PATCH 1/2] [SPARC] Prevent RESTORE from consuming an indirect call's
target
Folding an ADD into a RESTORE in a call delay slot moves the addition
into the register window switch, so its destination is no longer
written before the call. An indirect tail call jumps through that
register, so the jump reads only the ADD's first operand and calls
base instead of base + offset; the correct sum lands in an out
register nothing reads.
Keep the ADD when the terminator's target register is the one it
writes. A direct call names its callee instead, so the folded sum, an
outgoing argument, still combines.
Signed-off-by: Imre Kaloz <kaloz at kernel.org>
Assisted-by: Claude
---
llvm/lib/Target/Sparc/DelaySlotFiller.cpp | 9 ++++++++
.../CodeGen/SPARC/tailcall-restore-fold.ll | 21 +++++++++++++++++++
2 files changed, 30 insertions(+)
create mode 100644 llvm/test/CodeGen/SPARC/tailcall-restore-fold.ll
diff --git a/llvm/lib/Target/Sparc/DelaySlotFiller.cpp b/llvm/lib/Target/Sparc/DelaySlotFiller.cpp
index c60b78fcecaf5..d5d6d57c700c9 100644
--- a/llvm/lib/Target/Sparc/DelaySlotFiller.cpp
+++ b/llvm/lib/Target/Sparc/DelaySlotFiller.cpp
@@ -393,6 +393,15 @@ static bool combineRestoreADD(MachineBasicBlock &MBB,
AddMI->readsRegister(SP::O7, TRI))
return false;
+ // An indirect tail call jumps through the register in its first operand. If
+ // this ADD computes that address, folding it into the RESTORE leaves the jump
+ // reading only the ADD's first operand, silently calling base instead of
+ // base + offset. A direct call names its callee here instead, and the folded
+ // sum is an outgoing argument.
+ if (IsCall && LastInst->getOperand(0).isReg() &&
+ LastInst->getOperand(0).getReg() == reg)
+ return false;
+
// Erase RESTORE.
RestoreMI->eraseFromParent();
diff --git a/llvm/test/CodeGen/SPARC/tailcall-restore-fold.ll b/llvm/test/CodeGen/SPARC/tailcall-restore-fold.ll
new file mode 100644
index 0000000000000..d4e24858f039d
--- /dev/null
+++ b/llvm/test/CodeGen/SPARC/tailcall-restore-fold.ll
@@ -0,0 +1,21 @@
+; RUN: llc -mtriple=sparc64 < %s | FileCheck %s
+
+;; DelaySlotFiller folds `add a, b, %iN; restore %g0, %g0, %g0` into
+;; `restore a, b, %oN`. When %iN is the target of an indirect tail call the
+;; fold must not happen: the jump would read only the ADD's first operand and
+;; call base instead of base + offset, with the sum computed into an out
+;; register nothing reads.
+
+declare void @clobber()
+
+define void @tailcall_computed_target(ptr %base, i64 %off) nounwind {
+; CHECK-LABEL: tailcall_computed_target:
+; CHECK: add %i0, %i1, %[[T:i[0-7]]]
+; CHECK-NEXT: jmp %[[T]]
+; CHECK-NEXT: restore{{$}}
+entry:
+ call void @clobber()
+ %tp = getelementptr i8, ptr %base, i64 %off
+ tail call void %tp()
+ ret void
+}
>From aed5bd3c97ce8e6af6ec116e5e8b7b005798409d Mon Sep 17 00:00:00 2001
From: Imre Kaloz <kaloz at kernel.org>
Date: Mon, 5 Oct 2026 01:53:38 +0200
Subject: [PATCH 2/2] [SPARC] Shorten comment, move test into tailcall.ll
Signed-off-by: Imre Kaloz <kaloz at kernel.org>
Assisted-by: Claude
---
llvm/lib/Target/Sparc/DelaySlotFiller.cpp | 7 ++----
.../CodeGen/SPARC/tailcall-restore-fold.ll | 21 -----------------
llvm/test/CodeGen/SPARC/tailcall.ll | 23 +++++++++++++++++++
3 files changed, 25 insertions(+), 26 deletions(-)
delete mode 100644 llvm/test/CodeGen/SPARC/tailcall-restore-fold.ll
diff --git a/llvm/lib/Target/Sparc/DelaySlotFiller.cpp b/llvm/lib/Target/Sparc/DelaySlotFiller.cpp
index d5d6d57c700c9..2ab9e427dac4a 100644
--- a/llvm/lib/Target/Sparc/DelaySlotFiller.cpp
+++ b/llvm/lib/Target/Sparc/DelaySlotFiller.cpp
@@ -393,11 +393,8 @@ static bool combineRestoreADD(MachineBasicBlock &MBB,
AddMI->readsRegister(SP::O7, TRI))
return false;
- // An indirect tail call jumps through the register in its first operand. If
- // this ADD computes that address, folding it into the RESTORE leaves the jump
- // reading only the ADD's first operand, silently calling base instead of
- // base + offset. A direct call names its callee here instead, and the folded
- // sum is an outgoing argument.
+ // An indirect tail call jumps through this register, so keep the ADD
+ // that writes it ahead of the jump.
if (IsCall && LastInst->getOperand(0).isReg() &&
LastInst->getOperand(0).getReg() == reg)
return false;
diff --git a/llvm/test/CodeGen/SPARC/tailcall-restore-fold.ll b/llvm/test/CodeGen/SPARC/tailcall-restore-fold.ll
deleted file mode 100644
index d4e24858f039d..0000000000000
--- a/llvm/test/CodeGen/SPARC/tailcall-restore-fold.ll
+++ /dev/null
@@ -1,21 +0,0 @@
-; RUN: llc -mtriple=sparc64 < %s | FileCheck %s
-
-;; DelaySlotFiller folds `add a, b, %iN; restore %g0, %g0, %g0` into
-;; `restore a, b, %oN`. When %iN is the target of an indirect tail call the
-;; fold must not happen: the jump would read only the ADD's first operand and
-;; call base instead of base + offset, with the sum computed into an out
-;; register nothing reads.
-
-declare void @clobber()
-
-define void @tailcall_computed_target(ptr %base, i64 %off) nounwind {
-; CHECK-LABEL: tailcall_computed_target:
-; CHECK: add %i0, %i1, %[[T:i[0-7]]]
-; CHECK-NEXT: jmp %[[T]]
-; CHECK-NEXT: restore{{$}}
-entry:
- call void @clobber()
- %tp = getelementptr i8, ptr %base, i64 %off
- tail call void %tp()
- ret void
-}
diff --git a/llvm/test/CodeGen/SPARC/tailcall.ll b/llvm/test/CodeGen/SPARC/tailcall.ll
index e9955aea19908..1370ae186628e 100644
--- a/llvm/test/CodeGen/SPARC/tailcall.ll
+++ b/llvm/test/CodeGen/SPARC/tailcall.ll
@@ -289,6 +289,29 @@ entry:
ret void
}
+; Test register + register pattern.
+
+define void @addrr_test(ptr %base, i32 %off) #1 {
+; V8-LABEL: addrr_test:
+; V8: ! %bb.0: ! %entry
+; V8-NEXT: save %sp, -96, %sp
+; V8-NEXT: add %i0, %i1, %i0
+; V8-NEXT: jmp %i0
+; V8-NEXT: restore
+;
+; V9-LABEL: addrr_test:
+; V9: ! %bb.0: ! %entry
+; V9-NEXT: save %sp, -128, %sp
+; V9-NEXT: sra %i1, 0, %i1
+; V9-NEXT: add %i0, %i1, %i0
+; V9-NEXT: jmp %i0
+; V9-NEXT: restore
+entry:
+ %tp = getelementptr i8, ptr %base, i32 %off
+ tail call void %tp() #1
+ ret void
+}
+
%struct.a = type { i32, i32 }
@dest = global [2 x i8] zeroinitializer
More information about the llvm-commits
mailing list