[llvm] [X86] Simplify hasCalleePopSRet, NFCI (PR #176519)

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 16 16:58:16 PST 2026


https://github.com/rnk created https://github.com/llvm/llvm-project/pull/176519

The implementation was rewritten for clarity, and the extra boolean parameter to the sibcall eligibility check was removed in favor of recalculating this property. The compile time impact should be negigible, the vast majority of callers will return early on the TT.isX86_32() check.

The comments now try to clarify which platforms have this callee-pop-sret behavior, which was always hard for me to figure out from the previous code.

I was able to remove two ambiguous checks for `canGuaranteeTCO`, and what those checks were really doing was checking for `fastcc` and other calling conventions that pass arguments in registers. Instead of looking for the `inreg` IR attribute, now the code looks at the CCValAssign to check if it the pointer is passed in memory or registers, so it works smoothly with conventions like `fastcc` that don't require explicit `inreg` annotations.

>From 6868858285f32e09de064a93a3e6480f6cbab9a2 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rkleckner at nvidia.com>
Date: Sat, 17 Jan 2026 00:03:36 +0000
Subject: [PATCH] [X86] Simplify hasCalleePopSRet, NFC

The implementation was rewritten for clarity, and the extra boolean
parameter to the sibcall eligibility check was removed in favor of
recalculating this property. The compile time impact should be
negigible, the vast majority of callers will return early on the
TT.isX86_32() check.

The comments now try to clarify which platforms have this
callee-pop-sret behavior, which was always hard for me to figure out
from the previous code.

I was able to remove two ambiguous checks for `canGuaranteeTCO`, and
what those checks were really doing was checking for `fastcc` and other
calling conventions that pass arguments in registers. Instead of looking
for the `inreg` IR attribute, now the code looks at the CCValAssign to
check if it the pointer is passed in memory or registers, so it works
smoothly with conventions like `fastcc` that don't require explicit
`inreg` annotations.
---
 llvm/lib/Target/X86/X86ISelLowering.h       |  3 +-
 llvm/lib/Target/X86/X86ISelLoweringCall.cpp | 60 +++++++--------------
 2 files changed, 21 insertions(+), 42 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.h b/llvm/lib/Target/X86/X86ISelLowering.h
index 1d164253ccc80..31427689358e1 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.h
+++ b/llvm/lib/Target/X86/X86ISelLowering.h
@@ -1749,8 +1749,7 @@ namespace llvm {
     /// Check whether the call is eligible for sibling call optimization.
     bool isEligibleForSiblingCallOpt(TargetLowering::CallLoweringInfo &CLI,
                                      CCState &CCInfo,
-                                     SmallVectorImpl<CCValAssign> &ArgLocs,
-                                     bool IsCalleePopSRet) const;
+                                     SmallVectorImpl<CCValAssign> &ArgLocs) const;
     SDValue EmitTailCallLoadRetAddr(SelectionDAG &DAG, SDValue &OutRetAddr,
                                     SDValue Chain, bool IsTailCall,
                                     bool Is64Bit, int FPDiff,
diff --git a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
index accf27ed50f84..746c6737fa1b9 100644
--- a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
+++ b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
@@ -1205,50 +1205,31 @@ SDValue X86TargetLowering::LowerCallResult(
   return Chain;
 }
 
-//===----------------------------------------------------------------------===//
-//                C & StdCall & Fast Calling Convention implementation
-//===----------------------------------------------------------------------===//
-//  StdCall calling convention seems to be standard for many Windows' API
-//  routines and around. It differs from C calling convention just a little:
-//  callee should clean up the stack, not caller. Symbols should be also
-//  decorated in some fancy way :) It doesn't support any vector arguments.
-//  For info on fast calling convention see Fast Calling Convention (tail call)
-//  implementation LowerX86_32FastCCCallTo.
-
 /// Determines whether Args, either a set of outgoing arguments to a call, or a
 /// set of incoming args of a call, contains an sret pointer that the callee
-/// pops
+/// pops. This happens on most x86-32, System V platforms, unless register
+/// parameters are in use (-mregparm=1+, regcallcc, etc).
 template <typename T>
 static bool hasCalleePopSRet(const SmallVectorImpl<T> &Args,
+                             const SmallVectorImpl<CCValAssign> &ArgLocs,
                              const X86Subtarget &Subtarget) {
   // Not C++20 (yet), so no concepts available.
   static_assert(std::is_same_v<T, ISD::OutputArg> ||
                     std::is_same_v<T, ISD::InputArg>,
                 "requires ISD::OutputArg or ISD::InputArg");
 
-  // Only 32-bit pops the sret.  It's a 64-bit world these days, so early-out
-  // for most compilations.
-  if (!Subtarget.is32Bit())
-    return false;
-
-  if (Args.empty())
-    return false;
-
-  // Most calls do not have an sret argument, check the arg next.
-  const ISD::ArgFlagsTy &Flags = Args[0].Flags;
-  if (!Flags.isSRet() || Flags.isInReg())
+  // Popping the sret pointer only happens on x86-32 System V ABI platforms
+  // (Linux, Cygwin, BSDs, Mac, etc). That excludes Windows-minus-Cygwin and
+  // MCU.
+  const Triple &TT = Subtarget.getTargetTriple();
+  if (!TT.isX86_32() || TT.isOSMSVCRT() || TT.isOSIAMCU())
     return false;
 
-  // The MSVCabi does not pop the sret.
-  if (Subtarget.getTargetTriple().isOSMSVCRT())
-    return false;
-
-  // MCUs don't pop the sret
-  if (Subtarget.isTargetMCU())
-    return false;
-
-  // Callee pops argument
-  return true;
+  // Check if the first argument is marked sret and if it is passed in memory.
+  bool IsSRetInMem = false;
+  if (!Args.empty())
+    IsSRetInMem = Args.front().Flags.isSRet() && ArgLocs.front().isMemLoc();
+  return IsSRetInMem;
 }
 
 /// Make a copy of an aggregate at address specified by "Src" to address
@@ -1894,7 +1875,7 @@ SDValue X86TargetLowering::LowerFormalArguments(
   } else {
     FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
     // If this is an sret function, the return should pop the hidden pointer.
-    if (!canGuaranteeTCO(CallConv) && hasCalleePopSRet(Ins, Subtarget))
+    if (hasCalleePopSRet(Ins, ArgLocs, Subtarget))
       FuncInfo->setBytesToPopOnReturn(4);
   }
 
@@ -2073,8 +2054,6 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
   bool IsWin64 = Subtarget.isCallingConvWin64(CallConv);
   bool ShouldGuaranteeTCO = shouldGuaranteeTCO(
       CallConv, MF.getTarget().Options.GuaranteedTailCallOpt);
-  bool IsCalleePopSRet =
-      !ShouldGuaranteeTCO && hasCalleePopSRet(Outs, Subtarget);
   X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
   bool HasNCSR = (CB && isa<CallInst>(CB) &&
                   CB->hasFnAttr("no_caller_saved_registers"));
@@ -2137,7 +2116,7 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
     // address or swapping byval arguments. We treat some musttail calls as
     // sibling calls to avoid unnecessary argument copies.
     IsSibcall =
-        isEligibleForSiblingCallOpt(CLI, CCInfo, ArgLocs, IsCalleePopSRet);
+        isEligibleForSiblingCallOpt(CLI, CCInfo, ArgLocs);
     isTailCall = IsSibcall || IsMustTail;
   }
 
@@ -2735,12 +2714,13 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
   // Create the CALLSEQ_END node.
   unsigned NumBytesForCalleeToPop = 0; // Callee pops nothing.
   if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
-                       DAG.getTarget().Options.GuaranteedTailCallOpt))
+                       DAG.getTarget().Options.GuaranteedTailCallOpt)) {
     NumBytesForCalleeToPop = NumBytes;    // Callee pops everything
-  else if (!canGuaranteeTCO(CallConv) && IsCalleePopSRet)
+  } else if (hasCalleePopSRet(Outs, ArgLocs, Subtarget)) {
     // If this call passes a struct-return pointer, the callee
     // pops that struct pointer.
     NumBytesForCalleeToPop = 4;
+  }
 
   // Returns a glue for retval copy to use.
   if (!IsSibcall) {
@@ -2947,7 +2927,7 @@ mayBeSRetTailCallCompatible(const TargetLowering::CallLoweringInfo &CLI,
 /// emission in all cases.
 bool X86TargetLowering::isEligibleForSiblingCallOpt(
     TargetLowering::CallLoweringInfo &CLI, CCState &CCInfo,
-    SmallVectorImpl<CCValAssign> &ArgLocs, bool IsCalleePopSRet) const {
+    SmallVectorImpl<CCValAssign> &ArgLocs) const {
   SelectionDAG &DAG = CLI.DAG;
   const SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
   const SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
@@ -3009,7 +2989,7 @@ bool X86TargetLowering::isEligibleForSiblingCallOpt(
     // sret. Condition #b is harder to determine.
     if (!mayBeSRetTailCallCompatible(CLI, SRetReg))
       return false;
-  } else if (IsCalleePopSRet)
+  } else if (hasCalleePopSRet(Outs, ArgLocs, Subtarget))
     // The callee pops an sret, so we cannot tail-call, as our caller doesn't
     // expect that.
     return false;



More information about the llvm-commits mailing list