This looks like a big change/rewrite, but it is not as bad as it looks as it mostly is the same code that is refactored to decouple the cost and benefit analysis. The biggest change is top-level function specializeFunctions that now drives the transformation likes this:
specializeFunctions() { Cost = getSpecializationCost(F); calculateGains(F, Cost); specializeFunction(F); }
while this is just a restructuring and decoupling of the cost and benefit analysis, this separation helps the actual functional change in calculateGains. We now sort the candidates based on the expected specialisation gain, which we didn't do before. For this, a book keeping struct ArgInfo was introduced. If we have a list of N candidates, but we only want specialise less than N as set by option -func-specialization-max-constants, we sort the list and discard the candidates that give the least benefit.
Given a formal argument, this change results in selecting the best actual argument(s). In a follow up, I want to go one step further and compare all functions and all arguments. But that will mostly build on top of this refactoring and change, and will be less change; this is enough change for now.
For a class used only in a cpp file, it may be better to wrap it into anonymous namespace to make sure the linkage is internal.