Index: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp =================================================================== --- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -334,6 +334,19 @@ LLVM_DEBUG(dbgs() << "FnSpecialization: Try function: " << F->getName() << "\n"); + + // Determine if it would be profitable to create a specialization of the + // function where the argument takes on the given constant value. If so, + // add the constant to Constants. + auto FnSpecCost = getSpecializationCost(F); + if (!FnSpecCost.isValid()) { + LLVM_DEBUG(dbgs() << "FnSpecialization: Invalid specialisation cost.\n"); + return false; + } + + LLVM_DEBUG(dbgs() << "FnSpecialization: func specialisation cost: "; + FnSpecCost.print(dbgs()); dbgs() << "\n"); + // Determine if we should specialize the function based on the values the // argument can take on. If specialization is not profitable, we continue // on to the next argument. @@ -351,7 +364,7 @@ // be set to false by isArgumentInteresting (that function only adds // values to the Constants list that are deemed profitable). SmallVector Constants; - if (!isArgumentInteresting(&A, Constants, IsPartial)) { + if (!isArgumentInteresting(&A, Constants, FnSpecCost, IsPartial)) { LLVM_DEBUG(dbgs() << "FnSpecialization: Argument is not interesting\n"); continue; } @@ -535,6 +548,7 @@ /// argument. bool isArgumentInteresting(Argument *A, SmallVectorImpl &Constants, + const InstructionCost &FnSpecCost, bool &IsPartial) { Function *F = A->getParent(); @@ -576,18 +590,6 @@ return false; } - // Determine if it would be profitable to create a specialization of the - // function where the argument takes on the given constant value. If so, - // add the constant to Constants. - auto FnSpecCost = getSpecializationCost(F); - if (!FnSpecCost.isValid()) { - LLVM_DEBUG(dbgs() << "FnSpecialization: Invalid specialisation cost.\n"); - return false; - } - - LLVM_DEBUG(dbgs() << "FnSpecialization: func specialisation cost: "; - FnSpecCost.print(dbgs()); dbgs() << "\n"); - for (auto *C : PossibleConstants) { LLVM_DEBUG(dbgs() << "FnSpecialization: Constant: " << *C << "\n"); if (ForceFunctionSpecialization) {