@@ -507,14 +507,28 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
507507 // MaybeLive. Initialized to a list of RetCount empty lists.
508508 RetUses MaybeLiveRetUses (RetCount);
509509
510- for (Function::const_iterator BB = F.begin (), E = F.end (); BB != E; ++BB)
511- if (const ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator ()))
510+ bool HasMustTailCalls = false ;
511+
512+ for (Function::const_iterator BB = F.begin (), E = F.end (); BB != E; ++BB) {
513+ if (const ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator ())) {
512514 if (RI->getNumOperands () != 0 && RI->getOperand (0 )->getType ()
513515 != F.getFunctionType ()->getReturnType ()) {
514516 // We don't support old style multiple return values.
515517 MarkLive (F);
516518 return ;
517519 }
520+ }
521+
522+ // If we have any returns of `musttail` results - the signature can't
523+ // change
524+ if (BB->getTerminatingMustTailCall () != nullptr )
525+ HasMustTailCalls = true ;
526+ }
527+
528+ if (HasMustTailCalls) {
529+ DEBUG (dbgs () << " DeadArgumentEliminationPass - " << F.getName ()
530+ << " has musttail calls\n " );
531+ }
518532
519533 if (!F.hasLocalLinkage () && (!ShouldHackArguments || F.isIntrinsic ())) {
520534 MarkLive (F);
@@ -526,6 +540,9 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
526540 // Keep track of the number of live retvals, so we can skip checks once all
527541 // of them turn out to be live.
528542 unsigned NumLiveRetVals = 0 ;
543+
544+ bool HasMustTailCallers = false ;
545+
529546 // Loop all uses of the function.
530547 for (const Use &U : F.uses ()) {
531548 // If the function is PASSED IN as an argument, its address has been
@@ -536,6 +553,11 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
536553 return ;
537554 }
538555
556+ // The number of arguments for `musttail` call must match the number of
557+ // arguments of the caller
558+ if (CS.isMustTailCall ())
559+ HasMustTailCallers = true ;
560+
539561 // If this use is anything other than a call site, the function is alive.
540562 const Instruction *TheCall = CS.getInstruction ();
541563 if (!TheCall) { // Not a direct call site?
@@ -580,6 +602,11 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
580602 }
581603 }
582604
605+ if (HasMustTailCallers) {
606+ DEBUG (dbgs () << " DeadArgumentEliminationPass - " << F.getName ()
607+ << " has musttail callers\n " );
608+ }
609+
583610 // Now we've inspected all callers, record the liveness of our return values.
584611 for (unsigned i = 0 ; i != RetCount; ++i)
585612 MarkValue (CreateRet (&F, i), RetValLiveness[i], MaybeLiveRetUses[i]);
@@ -593,12 +620,19 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
593620 for (Function::const_arg_iterator AI = F.arg_begin (),
594621 E = F.arg_end (); AI != E; ++AI, ++i) {
595622 Liveness Result;
596- if (F.getFunctionType ()->isVarArg ()) {
623+ if (F.getFunctionType ()->isVarArg () || HasMustTailCallers ||
624+ HasMustTailCalls) {
597625 // Variadic functions will already have a va_arg function expanded inside
598626 // them, making them potentially very sensitive to ABI changes resulting
599627 // from removing arguments entirely, so don't. For example AArch64 handles
600628 // register and stack HFAs very differently, and this is reflected in the
601629 // IR which has already been generated.
630+ //
631+ // `musttail` calls to this function restrict argument removal attempts.
632+ // The signature of the caller must match the signature of the function.
633+ //
634+ // `musttail` calls in this function prevents us from changing its
635+ // signature
602636 Result = Live;
603637 } else {
604638 // See what the effect of this use is (recording any uses that cause
0 commit comments