@@ -2053,14 +2053,6 @@ X86TargetLowering::ByValCopyKind X86TargetLowering::ByValNeedsCopyForTailCall(
20532053 if (!FixedSrc || (FixedSrc && SrcOffset < 0 ))
20542054 return CopyOnce;
20552055
2056- // In the case of byval arguments split between registers and the stack,
2057- // computeAddrForCallArg returns a FrameIndex which corresponds only to the
2058- // stack portion, but the Src SDValue will refer to the full value, including
2059- // the local stack memory that the register portion gets stored into. We only
2060- // need to compare them for equality, so normalise on the full value version.
2061- uint64_t RegSize = Flags.getByValSize () - MFI.getObjectSize (DstFI);
2062- DstOffset -= RegSize;
2063-
20642056 // If the value is already in the correct location, then no copying is
20652057 // needed. If not, then we need to copy via a temporary.
20662058 if (SrcOffset == DstOffset)
@@ -2154,19 +2146,15 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
21542146 IsSibcall = IsEligibleForTailCallOptimization (CLI, CCInfo, ArgLocs,
21552147 IsCalleePopSRet);
21562148
2157- if (!IsMustTail) {
2158- isTailCall = IsSibcall;
2159-
2160- // Sibcalls are automatically detected tailcalls which do not require
2161- // ABI changes.
2162- IsSibcall = IsSibcall && !IsGuaranteeTCO;
2163- }
2149+ if (isTailCall && !getTargetMachine ().Options .GuaranteedTailCallOpt &&
2150+ CallConv != CallingConv::Tail && CallConv != CallingConv::SwiftTail)
2151+ IsSibcall = true ;
21642152
21652153 if (isTailCall)
21662154 ++NumTailCalls;
21672155 }
21682156
2169- if (IsMustTail && !isTailCall )
2157+ if (!isTailCall && CLI. CB && CLI. CB -> isMustTailCall () )
21702158 report_fatal_error (" failed to perform tail call elimination on a call "
21712159 " site marked musttail" );
21722160
@@ -2231,7 +2219,7 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
22312219
22322220 // Destination: where this byval should live in the callee’s frame
22332221 // after the tail call.
2234- int32_t Offset = VA.getLocMemOffset () + FPDiff + RetAddrSize ;
2222+ int32_t Offset = VA.getLocMemOffset () + FPDiff;
22352223 int Size = VA.getLocVT ().getFixedSizeInBits () / 8 ;
22362224 int FI = MF.getFrameInfo ().CreateFixedObject (Size, Offset, true );
22372225 SDValue Dst = DAG.getFrameIndex (FI, PtrVT);
@@ -2265,6 +2253,7 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
22652253 ByValCopyChains.push_back (CopyChain);
22662254 }
22672255 }
2256+
22682257 if (!ByValCopyChains.empty ())
22692258 ByValTempChain =
22702259 DAG.getNode (ISD::TokenFactor, dl, MVT::Other, ByValCopyChains);
@@ -2484,7 +2473,7 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
24842473 // For tail calls lower the arguments to the 'real' stack slots. Sibcalls
24852474 // don't need this because the eligibility check rejects calls that require
24862475 // shuffling arguments passed in memory.
2487- if (!IsSibcall && isTailCall) {
2476+ if (isTailCall) {
24882477 // Force all the incoming stack arguments to be loaded from the stack
24892478 // before any new outgoing arguments or the return address are stored to the
24902479 // stack, because the outgoing stack slots may alias the incoming argument
@@ -2543,14 +2532,15 @@ X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
25432532
25442533 // FIXME: contrary to the arm backend, with the current logic we always
25452534 // seem to need a stack copy.
2546- ( void ) NeedsStackCopy;
2535+ if ( NeedsStackCopy) {
25472536
2548- auto PtrVT = getPointerTy (DAG.getDataLayout ());
2549- SDValue DstAddr = DAG.getFrameIndex (FI, PtrVT);
2537+ auto PtrVT = getPointerTy (DAG.getDataLayout ());
2538+ SDValue DstAddr = DAG.getFrameIndex (FI, PtrVT);
25502539
2551- // Copy the struct contents from ByValSrc to DstAddr.
2552- MemOpChains2.push_back (CreateCopyOfByValArgument (
2553- ByValSrc, DstAddr, Chain, Flags, DAG, dl));
2540+ // Copy the struct contents from ByValSrc to DstAddr.
2541+ MemOpChains2.push_back (CreateCopyOfByValArgument (
2542+ ByValSrc, DstAddr, Chain, Flags, DAG, dl));
2543+ }
25542544 } else {
25552545 // Store relative to framepointer.
25562546 MemOpChains2.push_back (DAG.getStore (
@@ -2951,9 +2941,10 @@ mayBeSRetTailCallCompatible(const TargetLowering::CallLoweringInfo &CLI,
29512941
29522942// / Check whether the call is eligible for tail call optimization. Targets
29532943// / that want to do tail call optimization should implement this function.
2954- // / Note that the x86 backend does not check musttail calls for eligibility! The
2955- // / rest of x86 tail call lowering must be prepared to forward arguments of any
2956- // / type.
2944+ // /
2945+ // / Note that this function also processes musttail calls, so when this
2946+ // / function returns false on a valid musttail call, a fatal backend error
2947+ // / occurs.
29572948bool X86TargetLowering::IsEligibleForTailCallOptimization (
29582949 TargetLowering::CallLoweringInfo &CLI, CCState &CCInfo,
29592950 SmallVectorImpl<CCValAssign> &ArgLocs, bool IsCalleePopSRet) const {
@@ -3080,26 +3071,6 @@ bool X86TargetLowering::IsEligibleForTailCallOptimization(
30803071 // If the callee takes no arguments then go on to check the results of the
30813072 // call.
30823073 if (!Outs.empty ()) {
3083- if (StackArgsSize > 0 ) {
3084- // Check if the arguments are already laid out in the right way as
3085- // the caller's fixed stack objects.
3086- MachineFrameInfo &MFI = MF.getFrameInfo ();
3087- const MachineRegisterInfo *MRI = &MF.getRegInfo ();
3088- const X86InstrInfo *TII = Subtarget.getInstrInfo ();
3089- for (unsigned I = 0 , E = ArgLocs.size (); I != E; ++I) {
3090- const CCValAssign &VA = ArgLocs[I];
3091- SDValue Arg = OutVals[I];
3092- ISD::ArgFlagsTy Flags = Outs[I].Flags ;
3093- if (VA.getLocInfo () == CCValAssign::Indirect)
3094- return false ;
3095- if (!VA.isRegLoc ()) {
3096- if (!MatchingStackOffset (Arg, VA.getLocMemOffset (), Flags, MFI, MRI,
3097- TII, VA))
3098- return false ;
3099- }
3100- }
3101- }
3102-
31033074 bool PositionIndependent = isPositionIndependent ();
31043075 // If the tailcall address may be in a register, then make sure it's
31053076 // possible to register allocate for it. In 32-bit, the call address can
@@ -3137,6 +3108,11 @@ bool X86TargetLowering::IsEligibleForTailCallOptimization(
31373108 X86::isCalleePop (CalleeCC, Subtarget.is64Bit (), isVarArg,
31383109 MF.getTarget ().Options .GuaranteedTailCallOpt );
31393110
3111+ // If the stack arguments for this call do not fit into our own save area then
3112+ // the call cannot be made tail.
3113+ if (CCInfo.getStackSize () > FuncInfo->getArgumentStackSize ())
3114+ return false ;
3115+
31403116 if (unsigned BytesToPop = FuncInfo->getBytesToPopOnReturn ()) {
31413117 // If we have bytes to pop, the callee must pop them.
31423118 bool CalleePopMatches = CalleeWillPop && BytesToPop == StackArgsSize;
0 commit comments