@@ -87,11 +87,6 @@ class DAE : public ModulePass {
87
87
virtual bool shouldHackArguments () const { return false ; }
88
88
};
89
89
90
- bool isMustTailCalleeAnalyzable (const CallBase &CB) {
91
- assert (CB.isMustTailCall ());
92
- return CB.getCalledFunction () && !CB.getCalledFunction ()->isDeclaration ();
93
- }
94
-
95
90
} // end anonymous namespace
96
91
97
92
char DAE::ID = 0 ;
@@ -280,7 +275,7 @@ bool DeadArgumentEliminationPass::removeDeadArgumentsFromCallers(Function &F) {
280
275
// they are fully alive (e.g., called indirectly) and except for the fragile
281
276
// (variadic) ones. In these cases, we may still be able to improve their
282
277
// statically known call sites.
283
- if ((F.hasLocalLinkage () && !LiveFunctions .count (&F)) &&
278
+ if ((F.hasLocalLinkage () && !FrozenFunctions .count (&F)) &&
284
279
!F.getFunctionType ()->isVarArg ())
285
280
return false ;
286
281
@@ -496,15 +491,15 @@ void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
496
491
// particular register and memory layout.
497
492
if (F.getAttributes ().hasAttrSomewhere (Attribute::InAlloca) ||
498
493
F.getAttributes ().hasAttrSomewhere (Attribute::Preallocated)) {
499
- markLive (F);
494
+ markFrozen (F);
500
495
return ;
501
496
}
502
497
503
498
// Don't touch naked functions. The assembly might be using an argument, or
504
499
// otherwise rely on the frame layout in a way that this analysis will not
505
500
// see.
506
501
if (F.hasFnAttribute (Attribute::Naked)) {
507
- markLive (F);
502
+ markFrozen (F);
508
503
return ;
509
504
}
510
505
@@ -522,29 +517,17 @@ void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
522
517
// MaybeLive. Initialized to a list of RetCount empty lists.
523
518
RetUses MaybeLiveRetUses (RetCount);
524
519
525
- bool HasMustTailCalls = false ;
526
520
for (const BasicBlock &BB : F) {
527
- // If we have any returns of `musttail` results - the signature can't
528
- // change
529
- if (const auto *TC = BB.getTerminatingMustTailCall ()) {
530
- HasMustTailCalls = true ;
531
- // In addition, if the called function is not locally defined (or unknown,
532
- // if this is an indirect call), we can't change the callsite and thus
533
- // can't change this function's signature either.
534
- if (!isMustTailCalleeAnalyzable (*TC)) {
535
- markLive (F);
521
+ if (BB.getTerminatingMustTailCall ()) {
522
+ LLVM_DEBUG (dbgs () << " DeadArgumentEliminationPass - " << F.getName ()
523
+ << " has musttail calls\n " );
524
+ if (markFnOrRetTyFrozenOnMusttail (F))
536
525
return ;
537
- }
538
526
}
539
527
}
540
528
541
- if (HasMustTailCalls) {
542
- LLVM_DEBUG (dbgs () << " DeadArgumentEliminationPass - " << F.getName ()
543
- << " has musttail calls\n " );
544
- }
545
-
546
529
if (!F.hasLocalLinkage () && (!ShouldHackArguments || F.isIntrinsic ())) {
547
- markLive (F);
530
+ markFrozen (F);
548
531
return ;
549
532
}
550
533
@@ -555,23 +538,23 @@ void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
555
538
// of them turn out to be live.
556
539
unsigned NumLiveRetVals = 0 ;
557
540
558
- bool HasMustTailCallers = false ;
559
-
560
541
// Loop all uses of the function.
561
542
for (const Use &U : F.uses ()) {
562
543
// If the function is PASSED IN as an argument, its address has been
563
544
// taken.
564
545
const auto *CB = dyn_cast<CallBase>(U.getUser ());
565
546
if (!CB || !CB->isCallee (&U) ||
566
547
CB->getFunctionType () != F.getFunctionType ()) {
567
- markLive (F);
548
+ markFrozen (F);
568
549
return ;
569
550
}
570
551
571
- // The number of arguments for `musttail` call must match the number of
572
- // arguments of the caller
573
- if (CB->isMustTailCall ())
574
- HasMustTailCallers = true ;
552
+ if (CB->isMustTailCall ()) {
553
+ LLVM_DEBUG (dbgs () << " DeadArgumentEliminationPass - " << F.getName ()
554
+ << " has musttail callers\n " );
555
+ if (markFnOrRetTyFrozenOnMusttail (F))
556
+ return ;
557
+ }
575
558
576
559
// If we end up here, we are looking at a direct call to our function.
577
560
@@ -610,11 +593,6 @@ void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
610
593
}
611
594
}
612
595
613
- if (HasMustTailCallers) {
614
- LLVM_DEBUG (dbgs () << " DeadArgumentEliminationPass - " << F.getName ()
615
- << " has musttail callers\n " );
616
- }
617
-
618
596
// Now we've inspected all callers, record the liveness of our return values.
619
597
for (unsigned Ri = 0 ; Ri != RetCount; ++Ri)
620
598
markValue (createRet (&F, Ri), RetValLiveness[Ri], MaybeLiveRetUses[Ri]);
@@ -628,19 +606,12 @@ void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
628
606
for (Function::const_arg_iterator AI = F.arg_begin (), E = F.arg_end ();
629
607
AI != E; ++AI, ++ArgI) {
630
608
Liveness Result;
631
- if (F.getFunctionType ()->isVarArg () || HasMustTailCallers ||
632
- HasMustTailCalls) {
609
+ if (F.getFunctionType ()->isVarArg ()) {
633
610
// Variadic functions will already have a va_arg function expanded inside
634
611
// them, making them potentially very sensitive to ABI changes resulting
635
612
// from removing arguments entirely, so don't. For example AArch64 handles
636
613
// register and stack HFAs very differently, and this is reflected in the
637
614
// IR which has already been generated.
638
- //
639
- // `musttail` calls to this function restrict argument removal attempts.
640
- // The signature of the caller must match the signature of the function.
641
- //
642
- // `musttail` calls in this function prevents us from changing its
643
- // signature
644
615
Result = Live;
645
616
} else {
646
617
// See what the effect of this use is (recording any uses that cause
@@ -680,14 +651,30 @@ void DeadArgumentEliminationPass::markValue(const RetOrArg &RA, Liveness L,
680
651
}
681
652
}
682
653
654
+ // / Return true if we freeze the whole function.
655
+ // / If the calling convention is not swifttailcc or tailcc, the caller and
656
+ // / callee of musttail must have exactly the same signature. Otherwise we
657
+ // / only needs to guarantee they have the same return type.
658
+ bool DeadArgumentEliminationPass::markFnOrRetTyFrozenOnMusttail (
659
+ const Function &F) {
660
+ if (F.getCallingConv () != CallingConv::SwiftTail ||
661
+ F.getCallingConv () != CallingConv::Tail) {
662
+ markFrozen (F);
663
+ return true ;
664
+ } else {
665
+ markRetTyFrozen (F);
666
+ return false ;
667
+ }
668
+ }
669
+
683
670
// / Mark the given Function as alive, meaning that it cannot be changed in any
684
671
// / way. Additionally, mark any values that are used as this function's
685
672
// / parameters or by its return values (according to Uses) live as well.
686
- void DeadArgumentEliminationPass::markLive (const Function &F) {
687
- LLVM_DEBUG (dbgs () << " DeadArgumentEliminationPass - Intrinsically live fn: "
673
+ void DeadArgumentEliminationPass::markFrozen (const Function &F) {
674
+ LLVM_DEBUG (dbgs () << " DeadArgumentEliminationPass - frozen fn: "
688
675
<< F.getName () << " \n " );
689
- // Mark the function as live .
690
- LiveFunctions .insert (&F);
676
+ // Mark the function as frozen .
677
+ FrozenFunctions .insert (&F);
691
678
// Mark all arguments as live.
692
679
for (unsigned ArgI = 0 , E = F.arg_size (); ArgI != E; ++ArgI)
693
680
propagateLiveness (createArg (&F, ArgI));
@@ -696,6 +683,12 @@ void DeadArgumentEliminationPass::markLive(const Function &F) {
696
683
propagateLiveness (createRet (&F, Ri));
697
684
}
698
685
686
+ void DeadArgumentEliminationPass::markRetTyFrozen (const Function &F) {
687
+ LLVM_DEBUG (dbgs () << " DeadArgumentEliminationPass - frozen return type fn: "
688
+ << F.getName () << " \n " );
689
+ FrozenRetTyFunctions.insert (&F);
690
+ }
691
+
699
692
// / Mark the given return value or argument as live. Additionally, mark any
700
693
// / values that are used by this value (according to Uses) live as well.
701
694
void DeadArgumentEliminationPass::markLive (const RetOrArg &RA) {
@@ -710,7 +703,7 @@ void DeadArgumentEliminationPass::markLive(const RetOrArg &RA) {
710
703
}
711
704
712
705
bool DeadArgumentEliminationPass::isLive (const RetOrArg &RA) {
713
- return LiveFunctions .count (RA.F ) || LiveValues.count (RA);
706
+ return FrozenFunctions .count (RA.F ) || LiveValues.count (RA);
714
707
}
715
708
716
709
// / Given that RA is a live value, propagate it's liveness to any other values
@@ -734,8 +727,8 @@ void DeadArgumentEliminationPass::propagateLiveness(const RetOrArg &RA) {
734
727
// / Transform the function and all the callees of the function to not have these
735
728
// / arguments and return values.
736
729
bool DeadArgumentEliminationPass::removeDeadStuffFromFunction (Function *F) {
737
- // Don't modify fully live functions
738
- if (LiveFunctions .count (F))
730
+ // Don't modify frozen functions
731
+ if (FrozenFunctions .count (F))
739
732
return false ;
740
733
741
734
// Start by computing a new prototype for the function, which is the same as
@@ -807,7 +800,8 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
807
800
// performance win, so the second option can just be used always for now.
808
801
//
809
802
// This should be revisited if 'returned' is ever applied more liberally.
810
- if (RetTy->isVoidTy () || HasLiveReturnedArg) {
803
+ if (RetTy->isVoidTy () || HasLiveReturnedArg ||
804
+ FrozenRetTyFunctions.count (F)) {
811
805
NRetTy = RetTy;
812
806
} else {
813
807
// Look at each of the original return values individually.
@@ -1109,26 +1103,6 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
1109
1103
return true ;
1110
1104
}
1111
1105
1112
- void DeadArgumentEliminationPass::propagateVirtMustcallLiveness (
1113
- const Module &M) {
1114
- // If a function was marked "live", and it has musttail callers, they in turn
1115
- // can't change either.
1116
- LiveFuncSet NewLiveFuncs (LiveFunctions);
1117
- while (!NewLiveFuncs.empty ()) {
1118
- LiveFuncSet Temp;
1119
- for (const auto *F : NewLiveFuncs)
1120
- for (const auto *U : F->users ())
1121
- if (const auto *CB = dyn_cast<CallBase>(U))
1122
- if (CB->isMustTailCall ())
1123
- if (!LiveFunctions.count (CB->getParent ()->getParent ()))
1124
- Temp.insert (CB->getParent ()->getParent ());
1125
- NewLiveFuncs.clear ();
1126
- NewLiveFuncs.insert (Temp.begin (), Temp.end ());
1127
- for (const auto *F : Temp)
1128
- markLive (*F);
1129
- }
1130
- }
1131
-
1132
1106
PreservedAnalyses DeadArgumentEliminationPass::run (Module &M,
1133
1107
ModuleAnalysisManager &) {
1134
1108
bool Changed = false ;
@@ -1149,8 +1123,6 @@ PreservedAnalyses DeadArgumentEliminationPass::run(Module &M,
1149
1123
for (auto &F : M)
1150
1124
surveyFunction (F);
1151
1125
1152
- propagateVirtMustcallLiveness (M);
1153
-
1154
1126
// Now, remove all dead arguments and return values from each function in
1155
1127
// turn. We use make_early_inc_range here because functions will probably get
1156
1128
// removed (i.e. replaced by new ones).
0 commit comments