[llvm] [X86] Elect to tail call when `sret` ptr is passed to the callee (PR #146575)

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 1 11:01:27 PDT 2025


================
@@ -2824,14 +2825,33 @@ bool X86TargetLowering::IsEligibleForTailCallOptimization(
   if (RegInfo->hasStackRealignment(MF))
     return false;
 
-  // Also avoid sibcall optimization if we're an sret return fn and the callee
-  // is incompatible. See comment in LowerReturn about why hasStructRetAttr is
-  // insufficient.
-  if (MF.getInfo<X86MachineFunctionInfo>()->getSRetReturnReg()) {
+  // Avoid sibcall optimization if we are an sret return function and the callee
+  // is incompatible, unless these premises are proven wrong.
+  if (Register SRetReg = FuncInfo->getSRetReturnReg()) {
     // For a compatible tail call the callee must return our sret pointer. So it
     // needs to be (a) an sret function itself and (b) we pass our sret as its
-    // sret. Condition #b is harder to determine.
-    return false;
+    // sret. We know the caller has a sret ptr argument (SRetReg). Now locate
+    // the operand index within the callee which has a sret ptr as well.
+    auto It = find_if(Outs, [](const auto &OA) { return OA.Flags.isSRet(); });
----------------
rnk wrote:

Given the need for an index from `distance`, I would just use the indexed for loop search to make this more readable.

https://github.com/llvm/llvm-project/pull/146575


More information about the llvm-commits mailing list