@@ -643,6 +643,18 @@ FunctionSpecializer::~FunctionSpecializer() {
643
643
cleanUpSSA ();
644
644
}
645
645
646
+ // / Get the unsigned Value of given Cost object. Assumes the Cost is always
647
+ // / non-negative, which is true for both TCK_CodeSize and TCK_Latency, and
648
+ // / always Valid.
649
+ static unsigned getCostValue (const Cost &C) {
650
+ int64_t Value = *C.getValue ();
651
+
652
+ assert (Value >= 0 && " CodeSize and Latency cannot be negative" );
653
+ // It is safe to down cast since we know the arguments cannot be negative and
654
+ // Cost is of type int64_t.
655
+ return static_cast <unsigned >(Value);
656
+ }
657
+
646
658
// / Attempt to specialize functions in the module to enable constant
647
659
// / propagation across function boundaries.
648
660
// /
@@ -757,6 +769,11 @@ bool FunctionSpecializer::run() {
757
769
SmallVector<Function *> Clones;
758
770
for (unsigned I = 0 ; I < NSpecs; ++I) {
759
771
Spec &S = AllSpecs[BestSpecs[I]];
772
+
773
+ // Accumulate the codesize growth for the function, now we are creating the
774
+ // specialization.
775
+ FunctionGrowth[S.F ] += S.CodeSize ;
776
+
760
777
S.Clone = createSpecialization (S.F , S.Sig );
761
778
762
779
// Update the known call sites to call the clone.
@@ -835,18 +852,6 @@ static Function *cloneCandidateFunction(Function *F, unsigned NSpecs) {
835
852
return Clone;
836
853
}
837
854
838
- // / Get the unsigned Value of given Cost object. Assumes the Cost is always
839
- // / non-negative, which is true for both TCK_CodeSize and TCK_Latency, and
840
- // / always Valid.
841
- static unsigned getCostValue (const Cost &C) {
842
- int64_t Value = *C.getValue ();
843
-
844
- assert (Value >= 0 && " CodeSize and Latency cannot be negative" );
845
- // It is safe to down cast since we know the arguments cannot be negative and
846
- // Cost is of type int64_t.
847
- return static_cast <unsigned >(Value);
848
- }
849
-
850
855
bool FunctionSpecializer::findSpecializations (Function *F, unsigned FuncSize,
851
856
SmallVectorImpl<Spec> &AllSpecs,
852
857
SpecMap &SM) {
@@ -922,16 +927,14 @@ bool FunctionSpecializer::findSpecializations(Function *F, unsigned FuncSize,
922
927
}
923
928
CodeSize += Visitor.getCodeSizeSavingsFromPendingPHIs ();
924
929
930
+ unsigned CodeSizeSavings = getCostValue (CodeSize);
931
+ unsigned SpecSize = FuncSize - CodeSizeSavings;
932
+
925
933
auto IsProfitable = [&]() -> bool {
926
934
// No check required.
927
935
if (ForceSpecialization)
928
936
return true ;
929
937
930
- unsigned CodeSizeSavings = getCostValue (CodeSize);
931
- // TODO: We should only accumulate codesize increase of specializations
932
- // that are actually created.
933
- FunctionGrowth[F] += FuncSize - CodeSizeSavings;
934
-
935
938
LLVM_DEBUG (
936
939
dbgs () << " FnSpecialization: Specialization bonus {Inlining = "
937
940
<< Score << " (" << (Score * 100 / FuncSize) << " %)}\n " );
@@ -962,7 +965,7 @@ bool FunctionSpecializer::findSpecializations(Function *F, unsigned FuncSize,
962
965
if (LatencySavings < MinLatencySavings * FuncSize / 100 )
963
966
return false ;
964
967
// Maximum codesize growth.
965
- if (FunctionGrowth[F] / FuncSize > MaxCodeSizeGrowth)
968
+ if (( FunctionGrowth[F] + SpecSize) / FuncSize > MaxCodeSizeGrowth)
966
969
return false ;
967
970
968
971
Score += std::max (CodeSizeSavings, LatencySavings);
@@ -974,7 +977,7 @@ bool FunctionSpecializer::findSpecializations(Function *F, unsigned FuncSize,
974
977
continue ;
975
978
976
979
// Create a new specialisation entry.
977
- auto &Spec = AllSpecs.emplace_back (F, S, Score);
980
+ auto &Spec = AllSpecs.emplace_back (F, S, Score, SpecSize );
978
981
if (CS.getFunction () != F)
979
982
Spec.CallSites .push_back (&CS);
980
983
const unsigned Index = AllSpecs.size () - 1 ;
0 commit comments