[llvm] [CGP] Sink and duplicate call for tail position when duplicating return (PR #172882)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 23 03:13:27 PST 2026
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/172882
>From 5badd46b2e37565bf5372256691762cd79650381 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Tue, 20 Jan 2026 10:35:12 +0100
Subject: [PATCH 1/3] [CGP] Introduce test for PR172882 (NFC)
---
.../CodeGen/X86/tailcall-cgp-sink-and-dup.ll | 236 ++++++++++++++++++
1 file changed, 236 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/tailcall-cgp-sink-and-dup.ll
diff --git a/llvm/test/CodeGen/X86/tailcall-cgp-sink-and-dup.ll b/llvm/test/CodeGen/X86/tailcall-cgp-sink-and-dup.ll
new file mode 100644
index 00000000000000..d9d52e90aa9c45
--- /dev/null
+++ b/llvm/test/CodeGen/X86/tailcall-cgp-sink-and-dup.ll
@@ -0,0 +1,236 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-apple-darwin | FileCheck %s
+; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' < %s -mtriple=x86_64-apple-darwin | FileCheck %s --check-prefix=OPT
+
+define i32 @sink_and_duplicate_for_tailc(i1 %c) nounwind {
+; OPT-LABEL: define i32 @sink_and_duplicate_for_tailc(
+; OPT-SAME: i1 [[C:%.*]]) #[[ATTR0:[0-9]+]] {
+; OPT-NEXT: [[ENTRY:.*:]]
+; OPT-NEXT: [[RV:%.*]] = tail call i32 @qux()
+; OPT-NEXT: br i1 [[C]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
+; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[TMP0:%.*]] = tail call i32 @quux()
+; OPT-NEXT: br label %[[RETURN]]
+; OPT: [[RETURN]]:
+; OPT-NEXT: ret i32 [[RV]]
+;
+; CHECK-LABEL: sink_and_duplicate_for_tailc:
+; CHECK: ## %bb.0: ## %entry
+; CHECK-NEXT: pushq %rbx
+; CHECK-NEXT: movl %edi, %ebx
+; CHECK-NEXT: callq _qux
+; CHECK-NEXT: testb $1, %bl
+; CHECK-NEXT: je LBB0_2
+; CHECK-NEXT: ## %bb.1: ## %if.then
+; CHECK-NEXT: movl %eax, %ebx
+; CHECK-NEXT: callq _quux
+; CHECK-NEXT: movl %ebx, %eax
+; CHECK-NEXT: LBB0_2: ## %return
+; CHECK-NEXT: popq %rbx
+; CHECK-NEXT: retq
+entry:
+ %rv = tail call i32 @qux()
+ br i1 %c, label %if.then, label %return
+
+if.then:
+ tail call i32 @quux()
+ br label %return
+
+return:
+ ret i32 %rv
+}
+
+define ptr @sink_and_duplicate_for_tailc_extractvalue(i1 noundef %c) nounwind {
+; OPT-LABEL: define ptr @sink_and_duplicate_for_tailc_extractvalue(
+; OPT-SAME: i1 noundef [[C:%.*]]) #[[ATTR0]] {
+; OPT-NEXT: [[ENTRY:.*:]]
+; OPT-NEXT: [[CALL:%.*]] = tail call { ptr, i32 } @qux_twice()
+; OPT-NEXT: br i1 [[C]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
+; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[TMP0:%.*]] = tail call { ptr, i32 } @quux_twice()
+; OPT-NEXT: br label %[[RETURN]]
+; OPT: [[RETURN]]:
+; OPT-NEXT: [[RV:%.*]] = extractvalue { ptr, i32 } [[CALL]], 0
+; OPT-NEXT: ret ptr [[RV]]
+;
+; CHECK-LABEL: sink_and_duplicate_for_tailc_extractvalue:
+; CHECK: ## %bb.0: ## %entry
+; CHECK-NEXT: pushq %rbx
+; CHECK-NEXT: movl %edi, %ebx
+; CHECK-NEXT: callq _qux_twice
+; CHECK-NEXT: testb $1, %bl
+; CHECK-NEXT: je LBB2_2
+; CHECK-NEXT: ## %bb.1: ## %if.then
+; CHECK-NEXT: movq %rax, %rbx
+; CHECK-NEXT: callq _quux_twice
+; CHECK-NEXT: movq %rbx, %rax
+; CHECK-NEXT: LBB2_2: ## %return
+; CHECK-NEXT: popq %rbx
+; CHECK-NEXT: retq
+entry:
+ %call = tail call { ptr, i32 } @qux_twice()
+ br i1 %c, label %if.then, label %return
+
+if.then:
+ tail call { ptr, i32 } @quux_twice()
+ br label %return
+
+return:
+ %rv = extractvalue { ptr, i32 } %call, 0
+ ret ptr %rv
+}
+
+define i32 @sink_and_duplicate_for_tailc_two_args(i1 noundef %c, i32 %a, i32 %b) nounwind {
+; OPT-LABEL: define i32 @sink_and_duplicate_for_tailc_two_args(
+; OPT-SAME: i1 [[C:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR0]] {
+; OPT-NEXT: [[ENTRY:.*:]]
+; OPT-NEXT: [[RV:%.*]] = tail call i32 @two_args(i32 [[A]], i32 [[B]])
+; OPT-NEXT: br i1 [[C]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
+; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[TMP0:%.*]] = tail call i32 @quux()
+; OPT-NEXT: br label %[[RETURN]]
+; OPT: [[RETURN]]:
+; OPT-NEXT: ret i32 [[RV]]
+;
+; CHECK-LABEL: sink_and_duplicate_for_tailc_two_args:
+; CHECK: ## %bb.0: ## %entry
+; CHECK-NEXT: pushq %rbx
+; CHECK-NEXT: movl %edi, %ebx
+; CHECK-NEXT: movl %esi, %edi
+; CHECK-NEXT: movl %edx, %esi
+; CHECK-NEXT: callq _two_args
+; CHECK-NEXT: testb $1, %bl
+; CHECK-NEXT: je LBB3_2
+; CHECK-NEXT: ## %bb.1: ## %if.then
+; CHECK-NEXT: movl %eax, %ebx
+; CHECK-NEXT: callq _quux
+; CHECK-NEXT: movl %ebx, %eax
+; CHECK-NEXT: LBB3_2: ## %return
+; CHECK-NEXT: popq %rbx
+; CHECK-NEXT: retq
+entry:
+ %rv = tail call i32 @two_args(i32 %a, i32 %b)
+ br i1 %c, label %if.then, label %return
+
+if.then:
+ tail call i32 @quux()
+ br label %return
+
+return:
+ ret i32 %rv
+}
+
+; Negative tests for duplicate for tail call.
+
+define i32 @sink_and_duplicate_for_tailc_illegal(i1 %c) {
+; OPT-LABEL: define i32 @sink_and_duplicate_for_tailc_illegal(
+; OPT-SAME: i1 [[C:%.*]]) {
+; OPT-NEXT: [[ENTRY:.*:]]
+; OPT-NEXT: [[RV:%.*]] = tail call i32 @qux()
+; OPT-NEXT: br i1 [[C]], label %[[NEXT:.*]], label %[[IF_THEN:.*]]
+; OPT: [[NEXT]]:
+; OPT-NEXT: br i1 [[C]], label %[[IF_THEN]], label %[[RETURN:.*]]
+; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[TMP0:%.*]] = tail call i32 @quux()
+; OPT-NEXT: br label %[[RETURN]]
+; OPT: [[RETURN]]:
+; OPT-NEXT: ret i32 [[RV]]
+;
+entry:
+ %rv = tail call i32 @qux()
+ br i1 %c, label %next, label %if.then
+
+next:
+ br i1 %c, label %if.then, label %return
+
+if.then:
+ tail call i32 @quux()
+ br label %return
+
+return:
+ ret i32 %rv
+}
+
+define i32 @sink_and_duplicate_for_tailc_illegal_store_between(i1 %c, ptr %p) {
+; OPT-LABEL: define i32 @sink_and_duplicate_for_tailc_illegal_store_between(
+; OPT-SAME: i1 [[C:%.*]], ptr [[P:%.*]]) {
+; OPT-NEXT: [[ENTRY:.*:]]
+; OPT-NEXT: [[RV:%.*]] = tail call i32 @baz(ptr [[P]], ptr null)
+; OPT-NEXT: store i32 0, ptr [[P]], align 4
+; OPT-NEXT: br i1 [[C]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
+; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[TMP0:%.*]] = tail call i32 @quux()
+; OPT-NEXT: br label %[[RETURN]]
+; OPT: [[RETURN]]:
+; OPT-NEXT: ret i32 [[RV]]
+;
+entry:
+ %rv = tail call i32 @baz(ptr %p, ptr null)
+ store i32 0, ptr %p
+ br i1 %c, label %if.then, label %return
+
+if.then:
+ tail call i32 @quux()
+ br label %return
+
+return:
+ ret i32 %rv
+}
+
+define i32 @sink_and_duplicate_for_tailc_illegal_excessive_args(i1 %c, i32 %a, i32 %b, i32 %d) {
+; OPT-LABEL: define i32 @sink_and_duplicate_for_tailc_illegal_excessive_args(
+; OPT-SAME: i1 [[C:%.*]], i32 [[A:%.*]], i32 [[B:%.*]], i32 [[D:%.*]]) {
+; OPT-NEXT: [[ENTRY:.*:]]
+; OPT-NEXT: [[RV:%.*]] = tail call i32 @three_args(i32 [[A]], i32 [[B]], i32 [[D]])
+; OPT-NEXT: br i1 [[C]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
+; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[TMP0:%.*]] = tail call i32 @quux()
+; OPT-NEXT: br label %[[RETURN]]
+; OPT: [[RETURN]]:
+; OPT-NEXT: ret i32 [[RV]]
+;
+entry:
+ %rv = tail call i32 @three_args(i32 %a, i32 %b, i32 %d)
+ br i1 %c, label %if.then, label %return
+
+if.then:
+ tail call i32 @quux()
+ br label %return
+
+return:
+ ret i32 %rv
+}
+
+%struct.ByVal = type { [2 x i64] }
+define i32 @sink_and_duplicate_for_tailc_illegal_byval(i1 %c, ptr %p) {
+; OPT-LABEL: define i32 @sink_and_duplicate_for_tailc_illegal_byval(
+; OPT-SAME: i1 [[C:%.*]], ptr [[P:%.*]]) {
+; OPT-NEXT: [[ENTRY:.*:]]
+; OPT-NEXT: [[RV:%.*]] = tail call i32 @func_byval(ptr byval([[STRUCT_BYVAL:%.*]]) [[P]])
+; OPT-NEXT: br i1 [[C]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
+; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[TMP0:%.*]] = tail call i32 @quux()
+; OPT-NEXT: br label %[[RETURN]]
+; OPT: [[RETURN]]:
+; OPT-NEXT: ret i32 [[RV]]
+;
+entry:
+ %rv = tail call i32 @func_byval(ptr byval(%struct.ByVal) %p)
+ br i1 %c, label %if.then, label %return
+
+if.then:
+ tail call i32 @quux()
+ br label %return
+
+return:
+ ret i32 %rv
+}
+
+declare i32 @two_args(i32, i32)
+declare i32 @three_args(i32, i32, i32)
+declare i32 @func_byval(ptr byval(%struct.ByVal))
+declare ptr @baz(ptr, ptr)
+declare i32 @qux()
+declare i32 @quux()
+declare { ptr, i32 } @qux_twice()
+declare { ptr, i32 } @quux_twice()
>From 7717b848a2b60cdbe6da1879f840f225de409f26 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Tue, 20 Jan 2026 11:06:14 +0100
Subject: [PATCH 2/3] [CGP] Sink and duplicate call for tail position when
duplicating return
Prior to duplicating returns in `dupRetToEnableTailCallOpts`, on the
chance no tail call opportunities have been found yet, for triangle-
like if/then it may be profitable to sink the call in one successor,
and duplicate the call in the return block, so that this latter call
may be eligible for tail call.
Fixes: https://github.com/llvm/llvm-project/issues/163811.
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 124 +++++++++++++++++-
.../CodeGen/X86/tailcall-cgp-sink-and-dup.ll | 49 +++----
2 files changed, 144 insertions(+), 29 deletions(-)
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index a3347df45e4e0c..41aea8a50757fb 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -275,6 +275,10 @@ static cl::opt<bool>
DisableDeletePHIs("disable-cgp-delete-phis", cl::Hidden, cl::init(false),
cl::desc("Disable elimination of dead PHI nodes."));
+static cl::opt<bool> EnableSinkCallForTailCall(
+ "cgp-sink-call-for-tailcall", cl::Hidden, cl::init(true),
+ cl::desc("Enable sinking and duplicate call for tail call optimization."));
+
namespace {
enum ExtType {
@@ -2912,10 +2916,104 @@ static bool isIntrinsicOrLFToBeTailCalled(const TargetLibraryInfo *TLInfo,
return false;
}
+/// Attempt to find a triangle-like pattern where sinking and duplicating a call
+/// may be profitable in order to enable further tail call optimization.
+/// We look for the following pattern:
+/// @code
+/// entry:
+/// %retval = call @func()
+/// br i1 %c, label %if.then, label %return
+/// if.then:
+/// ...
+/// br label %return
+/// return:
+/// ret %retval
+/// @endcode
+static bool
+sinkAndDuplicateCallForTailCall(CallInst *CI, BasicBlock *RetBB,
+ function_ref<bool(BasicBlock *)> TryFoldReturn,
+ SmallPtrSetImpl<Instruction *> &InsertedInsts) {
+ if (!CI->hasOneUse() || CI->cannotDuplicate() || CI->isConvergent() ||
+ pred_size(RetBB) != 2)
+ return false;
+
+ constexpr static auto MaxArgsForDuplication = 2;
+ if (CI->arg_size() > MaxArgsForDuplication ||
+ CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
+ return false;
+
+ auto It = std::next(CI->getIterator());
+ while (isa<PseudoProbeInst>(It) || isa<LifetimeIntrinsic>(It))
+ It = std::next(It);
+
+ // No instructions between the call and its terminator.
+ auto *CIBBTerm = CI->getParent()->getTerminator();
+ if (&*It != CIBBTerm)
+ return false;
+
+ auto *BI = dyn_cast<BranchInst>(CIBBTerm);
+ if (!BI || !BI->isConditional())
+ return false;
+
+ BasicBlock *BBToSinkCall;
+ if (BI->getSuccessor(0) == RetBB)
+ BBToSinkCall = BI->getSuccessor(1);
+ else if (BI->getSuccessor(1) == RetBB)
+ BBToSinkCall = BI->getSuccessor(0);
+ else
+ return false;
+
+ // Won't be profitable to duplicate the call if the basic block where we sink
+ // the call is more than just a jump (and at most another call).
+ if (BBToSinkCall->sizeWithoutDebug() > 2)
+ return false;
+
+ // Maybe found a candidate block to sink the call into.
+ if (!TryFoldReturn(BBToSinkCall))
+ return false;
+
+ // The return has been duplicated in one of its predecessor. Make sure the
+ // original call is sunk to such a predecessor, and duplicated in the
+ // original block that returns.
+ auto *NewRI =
+ cast<ReturnInst>(BBToSinkCall->getTerminator())->getReturnValue();
+ auto *OrigCI = dyn_cast<CallInst>(NewRI);
+ if (!OrigCI) {
+ OrigCI = dyn_cast<CallInst>(cast<ExtractValueInst>(NewRI)->getOperand(0));
+ assert(OrigCI && "Expected call-site.");
+ }
+
+ auto *OrigCITerm = cast<BranchInst>(OrigCI->getParent()->getTerminator());
+ auto *Cond = OrigCITerm->getCondition();
+ if (!isGuaranteedNotToBeUndefOrPoison(Cond)) {
+ auto *FI = new FreezeInst(Cond, Cond->getName() + ".fr",
+ OrigCITerm->getIterator());
+ OrigCITerm->setCondition(FI);
+ InsertedInsts.insert(FI);
+ }
+
+ // Sink original call to if/then basic block.
+ OrigCI->moveBefore(BBToSinkCall->getFirstInsertionPt());
+
+ // Duplicate the original call in the return basic block.
+ auto *ClonedCI = OrigCI->clone();
+ auto *OrigRI = cast<ReturnInst>(RetBB->getTerminator());
+ ClonedCI->setName(OrigCI->getName() + ".cloned");
+ ClonedCI->insertBefore(RetBB->getFirstInsertionPt());
+ if (auto *EVI = dyn_cast<ExtractValueInst>(OrigRI->getReturnValue()))
+ EVI->setOperand(0, ClonedCI);
+ else
+ OrigRI->setOperand(0, ClonedCI);
+
+ InsertedInsts.insert(ClonedCI);
+ return true;
+}
+
/// Look for opportunities to duplicate return instructions to the predecessor
/// to enable tail call optimizations. The case it is currently looking for is
-/// the following one. Known intrinsics or library function that may be tail
-/// called are taken into account as well.
+/// primarily the following one. In some cases, duplicating a function call to
+/// enable TCO in one block is allowed. Known intrinsics or library function
+/// that may be tail called are taken into account as well.
/// @code
/// bb0:
/// %tmp0 = tail call i32 @f0()
@@ -3085,13 +3183,12 @@ bool CodeGenPrepare::dupRetToEnableTailCallOpts(BasicBlock *BB,
}
}
- bool Changed = false;
- for (auto const &TailCallBB : TailCallBBs) {
+ auto TryFoldReturn = [&](BasicBlock *TailCallBB) -> bool {
// Make sure the call instruction is followed by an unconditional branch to
// the return block.
BranchInst *BI = dyn_cast<BranchInst>(TailCallBB->getTerminator());
if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
- continue;
+ return false;
// Duplicate the return into TailCallBB.
(void)FoldReturnIntoUncondBranch(RetI, BB, TailCallBB);
@@ -3100,10 +3197,25 @@ bool CodeGenPrepare::dupRetToEnableTailCallOpts(BasicBlock *BB,
BFI->setBlockFreq(BB,
(BFI->getBlockFreq(BB) - BFI->getBlockFreq(TailCallBB)));
ModifiedDT = ModifyDT::ModifyBBDT;
- Changed = true;
++NumRetsDup;
+ return true;
+ };
+
+ // If we have not found any tail call opportunity yet, but we know the return
+ // value of function call is being returned, then in triangle-like if/then it
+ // may be profitable to sink the call in one branch and duplicate it in the
+ // current return block, so that the duplicated call is in tail position.
+ if (EnableSinkCallForTailCall && TailCallBBs.empty()) {
+ auto *CI = dyn_cast_or_null<CallInst>(V);
+ if (CI && MayBePermittedAsTailCall(CI))
+ if (sinkAndDuplicateCallForTailCall(CI, BB, TryFoldReturn, InsertedInsts))
+ return true;
}
+ bool Changed = false;
+ for (const auto &TailCallBB : TailCallBBs)
+ Changed |= TryFoldReturn(TailCallBB);
+
// If we eliminated all predecessors of the block, delete the block now.
if (Changed && !BB->hasAddressTaken() && pred_empty(BB)) {
// Copy the fake uses found in the original return block to all blocks
diff --git a/llvm/test/CodeGen/X86/tailcall-cgp-sink-and-dup.ll b/llvm/test/CodeGen/X86/tailcall-cgp-sink-and-dup.ll
index d9d52e90aa9c45..c112eb7e66c0d9 100644
--- a/llvm/test/CodeGen/X86/tailcall-cgp-sink-and-dup.ll
+++ b/llvm/test/CodeGen/X86/tailcall-cgp-sink-and-dup.ll
@@ -6,26 +6,26 @@ define i32 @sink_and_duplicate_for_tailc(i1 %c) nounwind {
; OPT-LABEL: define i32 @sink_and_duplicate_for_tailc(
; OPT-SAME: i1 [[C:%.*]]) #[[ATTR0:[0-9]+]] {
; OPT-NEXT: [[ENTRY:.*:]]
-; OPT-NEXT: [[RV:%.*]] = tail call i32 @qux()
-; OPT-NEXT: br i1 [[C]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
+; OPT-NEXT: [[C_FR:%.*]] = freeze i1 [[C]]
+; OPT-NEXT: br i1 [[C_FR]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[RV1:%.*]] = tail call i32 @qux()
; OPT-NEXT: [[TMP0:%.*]] = tail call i32 @quux()
-; OPT-NEXT: br label %[[RETURN]]
+; OPT-NEXT: ret i32 [[RV1]]
; OPT: [[RETURN]]:
+; OPT-NEXT: [[RV:%.*]] = tail call i32 @qux()
; OPT-NEXT: ret i32 [[RV]]
;
; CHECK-LABEL: sink_and_duplicate_for_tailc:
; CHECK: ## %bb.0: ## %entry
+; CHECK-NEXT: testb $1, %dil
+; CHECK-NEXT: je _qux ## TAILCALL
+; CHECK-NEXT: ## %bb.1: ## %if.then
; CHECK-NEXT: pushq %rbx
-; CHECK-NEXT: movl %edi, %ebx
; CHECK-NEXT: callq _qux
-; CHECK-NEXT: testb $1, %bl
-; CHECK-NEXT: je LBB0_2
-; CHECK-NEXT: ## %bb.1: ## %if.then
; CHECK-NEXT: movl %eax, %ebx
; CHECK-NEXT: callq _quux
; CHECK-NEXT: movl %ebx, %eax
-; CHECK-NEXT: LBB0_2: ## %return
; CHECK-NEXT: popq %rbx
; CHECK-NEXT: retq
entry:
@@ -44,27 +44,27 @@ define ptr @sink_and_duplicate_for_tailc_extractvalue(i1 noundef %c) nounwind {
; OPT-LABEL: define ptr @sink_and_duplicate_for_tailc_extractvalue(
; OPT-SAME: i1 noundef [[C:%.*]]) #[[ATTR0]] {
; OPT-NEXT: [[ENTRY:.*:]]
-; OPT-NEXT: [[CALL:%.*]] = tail call { ptr, i32 } @qux_twice()
; OPT-NEXT: br i1 [[C]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[CALL1:%.*]] = tail call { ptr, i32 } @qux_twice()
; OPT-NEXT: [[TMP0:%.*]] = tail call { ptr, i32 } @quux_twice()
-; OPT-NEXT: br label %[[RETURN]]
+; OPT-NEXT: [[TMP1:%.*]] = extractvalue { ptr, i32 } [[CALL1]], 0
+; OPT-NEXT: ret ptr [[TMP1]]
; OPT: [[RETURN]]:
+; OPT-NEXT: [[CALL:%.*]] = tail call { ptr, i32 } @qux_twice()
; OPT-NEXT: [[RV:%.*]] = extractvalue { ptr, i32 } [[CALL]], 0
; OPT-NEXT: ret ptr [[RV]]
;
; CHECK-LABEL: sink_and_duplicate_for_tailc_extractvalue:
; CHECK: ## %bb.0: ## %entry
+; CHECK-NEXT: testb $1, %dil
+; CHECK-NEXT: je _qux_twice ## TAILCALL
+; CHECK-NEXT: ## %bb.1: ## %if.then
; CHECK-NEXT: pushq %rbx
-; CHECK-NEXT: movl %edi, %ebx
; CHECK-NEXT: callq _qux_twice
-; CHECK-NEXT: testb $1, %bl
-; CHECK-NEXT: je LBB2_2
-; CHECK-NEXT: ## %bb.1: ## %if.then
; CHECK-NEXT: movq %rax, %rbx
; CHECK-NEXT: callq _quux_twice
; CHECK-NEXT: movq %rbx, %rax
-; CHECK-NEXT: LBB2_2: ## %return
; CHECK-NEXT: popq %rbx
; CHECK-NEXT: retq
entry:
@@ -82,32 +82,35 @@ return:
define i32 @sink_and_duplicate_for_tailc_two_args(i1 noundef %c, i32 %a, i32 %b) nounwind {
; OPT-LABEL: define i32 @sink_and_duplicate_for_tailc_two_args(
-; OPT-SAME: i1 [[C:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR0]] {
+; OPT-SAME: i1 noundef [[C:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR0]] {
; OPT-NEXT: [[ENTRY:.*:]]
-; OPT-NEXT: [[RV:%.*]] = tail call i32 @two_args(i32 [[A]], i32 [[B]])
; OPT-NEXT: br i1 [[C]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
; OPT: [[IF_THEN]]:
+; OPT-NEXT: [[RV1:%.*]] = tail call i32 @two_args(i32 [[A]], i32 [[B]])
; OPT-NEXT: [[TMP0:%.*]] = tail call i32 @quux()
-; OPT-NEXT: br label %[[RETURN]]
+; OPT-NEXT: ret i32 [[RV1]]
; OPT: [[RETURN]]:
+; OPT-NEXT: [[RV:%.*]] = tail call i32 @two_args(i32 [[A]], i32 [[B]])
; OPT-NEXT: ret i32 [[RV]]
;
; CHECK-LABEL: sink_and_duplicate_for_tailc_two_args:
; CHECK: ## %bb.0: ## %entry
+; CHECK-NEXT: testb $1, %dil
+; CHECK-NEXT: je LBB2_2
+; CHECK-NEXT: ## %bb.1: ## %if.then
; CHECK-NEXT: pushq %rbx
-; CHECK-NEXT: movl %edi, %ebx
; CHECK-NEXT: movl %esi, %edi
; CHECK-NEXT: movl %edx, %esi
; CHECK-NEXT: callq _two_args
-; CHECK-NEXT: testb $1, %bl
-; CHECK-NEXT: je LBB3_2
-; CHECK-NEXT: ## %bb.1: ## %if.then
; CHECK-NEXT: movl %eax, %ebx
; CHECK-NEXT: callq _quux
; CHECK-NEXT: movl %ebx, %eax
-; CHECK-NEXT: LBB3_2: ## %return
; CHECK-NEXT: popq %rbx
; CHECK-NEXT: retq
+; CHECK-NEXT: LBB2_2: ## %return
+; CHECK-NEXT: movl %esi, %edi
+; CHECK-NEXT: movl %edx, %esi
+; CHECK-NEXT: jmp _two_args ## TAILCALL
entry:
%rv = tail call i32 @two_args(i32 %a, i32 %b)
br i1 %c, label %if.then, label %return
>From 1d2e333c237044d61de3058778677877e90e432f Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Fri, 23 Jan 2026 12:12:07 +0100
Subject: [PATCH 3/3] !fixup list transformed code
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 41aea8a50757fb..7985c74b2e4faf 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -2918,10 +2918,10 @@ static bool isIntrinsicOrLFToBeTailCalled(const TargetLibraryInfo *TLInfo,
/// Attempt to find a triangle-like pattern where sinking and duplicating a call
/// may be profitable in order to enable further tail call optimization.
-/// We look for the following pattern:
+/// The following pattern:
/// @code
/// entry:
-/// %retval = call @func()
+/// %retval = tail call @func()
/// br i1 %c, label %if.then, label %return
/// if.then:
/// ...
@@ -2929,6 +2929,21 @@ static bool isIntrinsicOrLFToBeTailCalled(const TargetLibraryInfo *TLInfo,
/// return:
/// ret %retval
/// @endcode
+/// May be transformed into the following:
+/// @code
+/// entry:
+/// %c.fr = freeze i1 %c
+/// br i1 %c.fr, label %if.then, label %return
+/// if.then:
+/// %retval = tail call @func()
+/// ...
+/// ret %retval
+/// return:
+/// %retval.cloned = tail call @func()
+/// ret %retval.cloned
+/// @endcode
+/// The freeze is needed in order to avoid introducing undefined behaviour. The
+/// duplicated call in %return is now in tail position.
static bool
sinkAndDuplicateCallForTailCall(CallInst *CI, BasicBlock *RetBB,
function_ref<bool(BasicBlock *)> TryFoldReturn,
More information about the llvm-commits
mailing list