Skip to content

Commit 8c32647

Browse files
[X86] Elect to tail call when sret ptr is passed to the callee
We may be able to allow the callee to be tail-called when the caller expects a `sret` pointer argument, as long as this pointer is forwarded to the callee. Fixes: #146303.
1 parent a79404e commit 8c32647

File tree

2 files changed

+40
-94
lines changed

2 files changed

+40
-94
lines changed

llvm/lib/Target/X86/X86ISelLoweringCall.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,6 +2788,7 @@ bool X86TargetLowering::IsEligibleForTailCallOptimization(
27882788

27892789
// If -tailcallopt is specified, make fastcc functions tail-callable.
27902790
MachineFunction &MF = DAG.getMachineFunction();
2791+
X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
27912792
const Function &CallerF = MF.getFunction();
27922793

27932794
// If the function return type is x86_fp80 and the callee return type is not,
@@ -2824,14 +2825,33 @@ bool X86TargetLowering::IsEligibleForTailCallOptimization(
28242825
if (RegInfo->hasStackRealignment(MF))
28252826
return false;
28262827

2827-
// Also avoid sibcall optimization if we're an sret return fn and the callee
2828-
// is incompatible. See comment in LowerReturn about why hasStructRetAttr is
2829-
// insufficient.
2830-
if (MF.getInfo<X86MachineFunctionInfo>()->getSRetReturnReg()) {
2828+
// Avoid sibcall optimization if we are an sret return function and the callee
2829+
// is incompatible, unless these premises are proven wrong.
2830+
if (Register SRetReg = FuncInfo->getSRetReturnReg()) {
28312831
// For a compatible tail call the callee must return our sret pointer. So it
28322832
// needs to be (a) an sret function itself and (b) we pass our sret as its
2833-
// sret. Condition #b is harder to determine.
2834-
return false;
2833+
// sret. We know the caller has a sret ptr argument (SRetReg). Now locate
2834+
// the operand index within the callee which has a sret ptr as well.
2835+
auto It = find_if(Outs, [](const auto &OA) { return OA.Flags.isSRet(); });
2836+
// Bail out if the callee has not any sret argument.
2837+
if (It == Outs.end())
2838+
return false;
2839+
2840+
bool Found = false;
2841+
unsigned Pos = std::distance(Outs.begin(), It);
2842+
SDValue SRetArgVal = OutVals[Pos];
2843+
// We are looking for a CopyToReg, where the callee sret argument is written
2844+
// into a new vreg (which should later be %rax/%eax, if this is returned).
2845+
for (SDNode *User : SRetArgVal->users()) {
2846+
if (User->getOpcode() != ISD::CopyToReg)
2847+
continue;
2848+
Register Reg = cast<RegisterSDNode>(User->getOperand(1))->getReg();
2849+
if (Reg == SRetReg && User->getOperand(2) == SRetArgVal)
2850+
Found = true;
2851+
}
2852+
2853+
if (!Found)
2854+
return false;
28352855
} else if (IsCalleePopSRet)
28362856
// The callee pops an sret, so we cannot tail-call, as our caller doesn't
28372857
// expect that.
@@ -2953,8 +2973,7 @@ bool X86TargetLowering::IsEligibleForTailCallOptimization(
29532973
X86::isCalleePop(CalleeCC, Subtarget.is64Bit(), isVarArg,
29542974
MF.getTarget().Options.GuaranteedTailCallOpt);
29552975

2956-
if (unsigned BytesToPop =
2957-
MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn()) {
2976+
if (unsigned BytesToPop = FuncInfo->getBytesToPopOnReturn()) {
29582977
// If we have bytes to pop, the callee must pop them.
29592978
bool CalleePopMatches = CalleeWillPop && BytesToPop == StackArgsSize;
29602979
if (!CalleePopMatches)

llvm/test/CodeGen/X86/sibcall.ll

Lines changed: 13 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -444,21 +444,11 @@ define dso_local void @t15(ptr noalias sret(%struct.foo) %agg.result) nounwind
444444
;
445445
; X64-LABEL: t15:
446446
; X64: # %bb.0:
447-
; X64-NEXT: pushq %rbx
448-
; X64-NEXT: movq %rdi, %rbx
449-
; X64-NEXT: callq f
450-
; X64-NEXT: movq %rbx, %rax
451-
; X64-NEXT: popq %rbx
452-
; X64-NEXT: retq
447+
; X64-NEXT: jmp f # TAILCALL
453448
;
454449
; X32-LABEL: t15:
455450
; X32: # %bb.0:
456-
; X32-NEXT: pushq %rbx
457-
; X32-NEXT: movq %rdi, %rbx
458-
; X32-NEXT: callq f
459-
; X32-NEXT: movl %ebx, %eax
460-
; X32-NEXT: popq %rbx
461-
; X32-NEXT: retq
451+
; X32-NEXT: jmp f # TAILCALL
462452
tail call fastcc void @f(ptr noalias sret(%struct.foo) %agg.result) nounwind
463453
ret void
464454
}
@@ -607,103 +597,50 @@ declare dso_local fastcc double @foo20(double) nounwind
607597
define fastcc void @t21_sret_to_sret(ptr noalias sret(%struct.foo) %agg.result) nounwind {
608598
; X86-LABEL: t21_sret_to_sret:
609599
; X86: # %bb.0:
610-
; X86-NEXT: pushl %esi
611-
; X86-NEXT: subl $8, %esp
612-
; X86-NEXT: movl %ecx, %esi
613-
; X86-NEXT: calll t21_f_sret
614-
; X86-NEXT: movl %esi, %eax
615-
; X86-NEXT: addl $8, %esp
616-
; X86-NEXT: popl %esi
617-
; X86-NEXT: retl
600+
; X86-NEXT: jmp t21_f_sret # TAILCALL
618601
;
619602
; X64-LABEL: t21_sret_to_sret:
620603
; X64: # %bb.0:
621-
; X64-NEXT: pushq %rbx
622-
; X64-NEXT: movq %rdi, %rbx
623-
; X64-NEXT: callq t21_f_sret
624-
; X64-NEXT: movq %rbx, %rax
625-
; X64-NEXT: popq %rbx
626-
; X64-NEXT: retq
604+
; X64-NEXT: jmp t21_f_sret # TAILCALL
627605
;
628606
; X32-LABEL: t21_sret_to_sret:
629607
; X32: # %bb.0:
630-
; X32-NEXT: pushq %rbx
631-
; X32-NEXT: movq %rdi, %rbx
632-
; X32-NEXT: callq t21_f_sret
633-
; X32-NEXT: movl %ebx, %eax
634-
; X32-NEXT: popq %rbx
635-
; X32-NEXT: retq
608+
; X32-NEXT: jmp t21_f_sret # TAILCALL
636609
tail call fastcc void @t21_f_sret(ptr noalias sret(%struct.foo) %agg.result) nounwind
637610
ret void
638611
}
639612

640613
define fastcc void @t21_sret_to_sret_more_args(ptr noalias sret(%struct.foo) %agg.result, i32 %a, i32 %b) nounwind {
641614
; X86-LABEL: t21_sret_to_sret_more_args:
642615
; X86: # %bb.0:
643-
; X86-NEXT: pushl %esi
644-
; X86-NEXT: subl $8, %esp
645-
; X86-NEXT: movl %ecx, %esi
646-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
647-
; X86-NEXT: movl %eax, (%esp)
648-
; X86-NEXT: calll f_sret@PLT
649-
; X86-NEXT: movl %esi, %eax
650-
; X86-NEXT: addl $8, %esp
651-
; X86-NEXT: popl %esi
652-
; X86-NEXT: retl
616+
; X86-NEXT: jmp f_sret@PLT # TAILCALL
653617
;
654618
; X64-LABEL: t21_sret_to_sret_more_args:
655619
; X64: # %bb.0:
656-
; X64-NEXT: pushq %rbx
657-
; X64-NEXT: movq %rdi, %rbx
658-
; X64-NEXT: callq f_sret@PLT
659-
; X64-NEXT: movq %rbx, %rax
660-
; X64-NEXT: popq %rbx
661-
; X64-NEXT: retq
620+
; X64-NEXT: jmp f_sret@PLT # TAILCALL
662621
;
663622
; X32-LABEL: t21_sret_to_sret_more_args:
664623
; X32: # %bb.0:
665-
; X32-NEXT: pushq %rbx
666-
; X32-NEXT: movq %rdi, %rbx
667-
; X32-NEXT: callq f_sret@PLT
668-
; X32-NEXT: movl %ebx, %eax
669-
; X32-NEXT: popq %rbx
670-
; X32-NEXT: retq
624+
; X32-NEXT: jmp f_sret@PLT # TAILCALL
671625
tail call fastcc void @f_sret(ptr noalias sret(%struct.foo) %agg.result, i32 %a, i32 %b) nounwind
672626
ret void
673627
}
674628

675629
define fastcc void @t21_sret_to_sret_second_arg_sret(ptr noalias %agg.result, ptr noalias sret(%struct.foo) %ret) nounwind {
676630
; X86-LABEL: t21_sret_to_sret_second_arg_sret:
677631
; X86: # %bb.0:
678-
; X86-NEXT: pushl %esi
679-
; X86-NEXT: subl $8, %esp
680-
; X86-NEXT: movl %edx, %esi
681632
; X86-NEXT: movl %edx, %ecx
682-
; X86-NEXT: calll t21_f_sret
683-
; X86-NEXT: movl %esi, %eax
684-
; X86-NEXT: addl $8, %esp
685-
; X86-NEXT: popl %esi
686-
; X86-NEXT: retl
633+
; X86-NEXT: jmp t21_f_sret # TAILCALL
687634
;
688635
; X64-LABEL: t21_sret_to_sret_second_arg_sret:
689636
; X64: # %bb.0:
690-
; X64-NEXT: pushq %rbx
691-
; X64-NEXT: movq %rsi, %rbx
692637
; X64-NEXT: movq %rsi, %rdi
693-
; X64-NEXT: callq t21_f_sret
694-
; X64-NEXT: movq %rbx, %rax
695-
; X64-NEXT: popq %rbx
696-
; X64-NEXT: retq
638+
; X64-NEXT: jmp t21_f_sret # TAILCALL
697639
;
698640
; X32-LABEL: t21_sret_to_sret_second_arg_sret:
699641
; X32: # %bb.0:
700-
; X32-NEXT: pushq %rbx
701-
; X32-NEXT: movq %rsi, %rbx
702642
; X32-NEXT: movq %rsi, %rdi
703-
; X32-NEXT: callq t21_f_sret
704-
; X32-NEXT: movl %ebx, %eax
705-
; X32-NEXT: popq %rbx
706-
; X32-NEXT: retq
643+
; X32-NEXT: jmp t21_f_sret # TAILCALL
707644
tail call fastcc void @t21_f_sret(ptr noalias sret(%struct.foo) %ret) nounwind
708645
ret void
709646
}
@@ -725,27 +662,17 @@ define fastcc void @t21_sret_to_sret_more_args2(ptr noalias sret(%struct.foo) %a
725662
;
726663
; X64-LABEL: t21_sret_to_sret_more_args2:
727664
; X64: # %bb.0:
728-
; X64-NEXT: pushq %rbx
729665
; X64-NEXT: movl %esi, %eax
730-
; X64-NEXT: movq %rdi, %rbx
731666
; X64-NEXT: movl %edx, %esi
732667
; X64-NEXT: movl %eax, %edx
733-
; X64-NEXT: callq f_sret@PLT
734-
; X64-NEXT: movq %rbx, %rax
735-
; X64-NEXT: popq %rbx
736-
; X64-NEXT: retq
668+
; X64-NEXT: jmp f_sret@PLT # TAILCALL
737669
;
738670
; X32-LABEL: t21_sret_to_sret_more_args2:
739671
; X32: # %bb.0:
740-
; X32-NEXT: pushq %rbx
741672
; X32-NEXT: movl %esi, %eax
742-
; X32-NEXT: movq %rdi, %rbx
743673
; X32-NEXT: movl %edx, %esi
744674
; X32-NEXT: movl %eax, %edx
745-
; X32-NEXT: callq f_sret@PLT
746-
; X32-NEXT: movl %ebx, %eax
747-
; X32-NEXT: popq %rbx
748-
; X32-NEXT: retq
675+
; X32-NEXT: jmp f_sret@PLT # TAILCALL
749676
tail call fastcc void @f_sret(ptr noalias sret(%struct.foo) %agg.result, i32 %b, i32 %a) nounwind
750677
ret void
751678
}

0 commit comments

Comments
 (0)