@@ -185,10 +185,10 @@ Cost InstCostVisitor::getCodeSizeSavingsForArg(Argument *A, Constant *C) {
185185// / for all Instructions in KnownConstants at once, it should be called only
186186// / after every instruction has been visited, i.e. after:
187187// /
188- // / * getCodeSizeBonus has been run for every constant argument of a
188+ // / * getCodeSizeSavingsForArg has been run for every constant argument of a
189189// / specialization candidate
190190// /
191- // / * getCodeSizeBonusFromPendingPHIs has been run
191+ // / * getCodeSizeSavingsFromPendingPHIs has been run
192192// /
193193// / to ensure that the latency savings are calculated for all Instructions we
194194// / have visited and found to be constant.
@@ -831,6 +831,9 @@ static Function *cloneCandidateFunction(Function *F, unsigned NSpecs) {
831831 return Clone;
832832}
833833
834+ // / Get the unsigned Value of given Cost object. Assumes the Cost is always
835+ // / non-negative, which is true for both TCK_CodeSize and TCK_Latency, and
836+ // / always Valid.
834837static unsigned getCostValue (const Cost &C) {
835838 int64_t Value = *C.getValue ();
836839
@@ -915,49 +918,58 @@ bool FunctionSpecializer::findSpecializations(Function *F, unsigned FuncSize,
915918 }
916919 CodeSize += Visitor.getCodeSizeSavingsFromPendingPHIs ();
917920
918- LLVM_DEBUG (dbgs () << " FnSpecialization: Specialization bonus {CodeSize = "
919- << CodeSize << " , Inlining = " << Score << " }\n " );
920-
921- unsigned LatencySavings = 0 ;
922- unsigned CodeSizeSavings = getCostValue (CodeSize);
923- FunctionGrowth[F] += FuncSize - CodeSizeSavings;
924-
925- auto IsProfitable = [](unsigned CodeSizeSavings, unsigned &LatencySavings,
926- unsigned Score, unsigned FuncSize,
927- unsigned FuncGrowth, InstCostVisitor &V) -> bool {
921+ auto IsProfitable = [&]() -> bool {
928922 // No check required.
929923 if (ForceSpecialization)
930924 return true ;
925+
926+ unsigned CodeSizeSavings = getCostValue (CodeSize);
927+ // TODO: We should only accumulate codesize increase of specializations
928+ // that are actually created.
929+ FunctionGrowth[F] += FuncSize - CodeSizeSavings;
930+
931+ LLVM_DEBUG (
932+ dbgs () << " FnSpecialization: Specialization bonus {Inlining = "
933+ << Score << " (" << (Score * 100 / FuncSize) << " %)}\n " );
934+
931935 // Minimum inlining bonus.
932936 if (Score > MinInliningBonus * FuncSize / 100 )
933937 return true ;
938+
939+ LLVM_DEBUG (
940+ dbgs () << " FnSpecialization: Specialization bonus {CodeSize = "
941+ << CodeSizeSavings << " ("
942+ << (CodeSizeSavings * 100 / FuncSize) << " %)}\n " );
943+
934944 // Minimum codesize savings.
935945 if (CodeSizeSavings < MinCodeSizeSavings * FuncSize / 100 )
936946 return false ;
937947
938948 // Lazily compute the Latency, to avoid unnecessarily computing BFI.
939- LatencySavings = getCostValue (V.getLatencySavingsForKnownConstants ());
949+ unsigned LatencySavings =
950+ getCostValue (Visitor.getLatencySavingsForKnownConstants ());
940951
941952 LLVM_DEBUG (
942953 dbgs () << " FnSpecialization: Specialization bonus {Latency = "
943- << LatencySavings << " }\n " );
954+ << LatencySavings << " ("
955+ << (LatencySavings * 100 / FuncSize) << " %)}\n " );
944956
945957 // Minimum latency savings.
946958 if (LatencySavings < MinLatencySavings * FuncSize / 100 )
947959 return false ;
948960 // Maximum codesize growth.
949- if (FuncGrowth / FuncSize > MaxCodeSizeGrowth)
961+ if (FunctionGrowth[F] / FuncSize > MaxCodeSizeGrowth)
950962 return false ;
963+
964+ Score += std::max (CodeSizeSavings, LatencySavings);
951965 return true ;
952966 };
953967
954968 // Discard unprofitable specialisations.
955- if (!IsProfitable (CodeSizeSavings, LatencySavings, Score, FuncSize,
956- FunctionGrowth[F], Visitor))
969+ if (!IsProfitable ())
957970 continue ;
958971
959972 // Create a new specialisation entry.
960- Score += std::max (CodeSizeSavings, LatencySavings);
961973 auto &Spec = AllSpecs.emplace_back (F, S, Score);
962974 if (CS.getFunction () != F)
963975 Spec.CallSites .push_back (&CS);
0 commit comments