@@ -507,14 +507,28 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
507
507
// MaybeLive. Initialized to a list of RetCount empty lists.
508
508
RetUses MaybeLiveRetUses (RetCount);
509
509
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 ())) {
512
514
if (RI->getNumOperands () != 0 && RI->getOperand (0 )->getType ()
513
515
!= F.getFunctionType ()->getReturnType ()) {
514
516
// We don't support old style multiple return values.
515
517
MarkLive (F);
516
518
return ;
517
519
}
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
+ }
518
532
519
533
if (!F.hasLocalLinkage () && (!ShouldHackArguments || F.isIntrinsic ())) {
520
534
MarkLive (F);
@@ -526,6 +540,9 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
526
540
// Keep track of the number of live retvals, so we can skip checks once all
527
541
// of them turn out to be live.
528
542
unsigned NumLiveRetVals = 0 ;
543
+
544
+ bool HasMustTailCallers = false ;
545
+
529
546
// Loop all uses of the function.
530
547
for (const Use &U : F.uses ()) {
531
548
// If the function is PASSED IN as an argument, its address has been
@@ -536,6 +553,11 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
536
553
return ;
537
554
}
538
555
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
+
539
561
// If this use is anything other than a call site, the function is alive.
540
562
const Instruction *TheCall = CS.getInstruction ();
541
563
if (!TheCall) { // Not a direct call site?
@@ -580,6 +602,11 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
580
602
}
581
603
}
582
604
605
+ if (HasMustTailCallers) {
606
+ DEBUG (dbgs () << " DeadArgumentEliminationPass - " << F.getName ()
607
+ << " has musttail callers\n " );
608
+ }
609
+
583
610
// Now we've inspected all callers, record the liveness of our return values.
584
611
for (unsigned i = 0 ; i != RetCount; ++i)
585
612
MarkValue (CreateRet (&F, i), RetValLiveness[i], MaybeLiveRetUses[i]);
@@ -593,12 +620,19 @@ void DeadArgumentEliminationPass::SurveyFunction(const Function &F) {
593
620
for (Function::const_arg_iterator AI = F.arg_begin (),
594
621
E = F.arg_end (); AI != E; ++AI, ++i) {
595
622
Liveness Result;
596
- if (F.getFunctionType ()->isVarArg ()) {
623
+ if (F.getFunctionType ()->isVarArg () || HasMustTailCallers ||
624
+ HasMustTailCalls) {
597
625
// Variadic functions will already have a va_arg function expanded inside
598
626
// them, making them potentially very sensitive to ABI changes resulting
599
627
// from removing arguments entirely, so don't. For example AArch64 handles
600
628
// register and stack HFAs very differently, and this is reflected in the
601
629
// 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
602
636
Result = Live;
603
637
} else {
604
638
// See what the effect of this use is (recording any uses that cause
0 commit comments