diff --git a/llvm/include/llvm/Transforms/Utils/SCCPSolver.h b/llvm/include/llvm/Transforms/Utils/SCCPSolver.h --- a/llvm/include/llvm/Transforms/Utils/SCCPSolver.h +++ b/llvm/include/llvm/Transforms/Utils/SCCPSolver.h @@ -30,6 +30,7 @@ class GlobalVariable; class Instruction; class LLVMContext; +class LoopInfo; class PostDominatorTree; class StructType; class TargetLibraryInfo; @@ -41,6 +42,7 @@ std::unique_ptr PredInfo; DominatorTree *DT; PostDominatorTree *PDT; + LoopInfo *LI; }; /// Helper struct shared between Function Specialization and SCCP Solver. @@ -77,6 +79,8 @@ const PredicateBase *getPredicateInfoFor(Instruction *I); + const LoopInfo &getLoopInfo(Function &F); + DomTreeUpdater getDTU(Function &F); /// trackValueOfGlobalVariable - Clients can use this method to diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp --- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -463,7 +463,7 @@ } SpecializationInfo &S = Specializations.back().second; - S.Gain += getSpecializationBonus(A, C); + S.Gain += getSpecializationBonus(A, C, Solver.getLoopInfo(*F)); S.Args.push_back({A, C}); } Added = false; @@ -580,7 +580,7 @@ } InstructionCost getUserBonus(User *U, llvm::TargetTransformInfo &TTI, - LoopInfo &LI) { + const LoopInfo &LI) { auto *I = dyn_cast_or_null(U); // If not an instruction we do not know how to evaluate. // Keep minimum possible cost for now so that it doesnt affect @@ -605,10 +605,9 @@ } /// Compute a bonus for replacing argument \p A with constant \p C. - InstructionCost getSpecializationBonus(Argument *A, Constant *C) { + InstructionCost getSpecializationBonus(Argument *A, Constant *C, + const LoopInfo &LI) { Function *F = A->getParent(); - DominatorTree DT(*F); - LoopInfo LI(DT); auto &TTI = (GetTTI)(*F); LLVM_DEBUG(dbgs() << "FnSpecialization: Analysing bonus for constant: " << C->getNameOrAsOperand() << "\n"); diff --git a/llvm/lib/Transforms/IPO/SCCP.cpp b/llvm/lib/Transforms/IPO/SCCP.cpp --- a/llvm/lib/Transforms/IPO/SCCP.cpp +++ b/llvm/lib/Transforms/IPO/SCCP.cpp @@ -12,6 +12,7 @@ #include "llvm/Transforms/IPO/SCCP.h" #include "llvm/Analysis/AssumptionCache.h" +#include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/PostDominators.h" #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" @@ -32,7 +33,8 @@ DominatorTree &DT = FAM.getResult(F); return { std::make_unique(F, DT, FAM.getResult(F)), - &DT, FAM.getCachedResult(F)}; + &DT, FAM.getCachedResult(F), + nullptr}; }; if (!runIPSCCP(M, DL, GetTLI, getAnalysis)) @@ -75,8 +77,9 @@ F, DT, this->getAnalysis().getAssumptionCache( F)), - nullptr, // We cannot preserve the DT or PDT with the legacy pass - nullptr}; // manager, so set them to nullptr. + nullptr, // We cannot preserve the LI, DT or PDT with the legacy pass + nullptr, // manager, so set them to nullptr. + nullptr}; }; return runIPSCCP(M, DL, GetTLI, getAnalysis); @@ -123,7 +126,8 @@ DominatorTree &DT = FAM.getResult(F); return {std::make_unique( F, DT, FAM.getResult(F)), - &DT, FAM.getCachedResult(F)}; + &DT, FAM.getCachedResult(F), + &FAM.getResult(F)}; }; if (!runFunctionSpecialization(M, DL, GetTLI, GetTTI, GetAC, GetAnalysis)) @@ -171,8 +175,9 @@ F, DT, this->getAnalysis().getAssumptionCache( F)), - nullptr, // We cannot preserve the DT or PDT with the legacy pass - nullptr}; // manager, so set them to nullptr. + nullptr, // We cannot preserve the LI, DT, or PDT with the legacy pass + nullptr, // manager, so set them to nullptr. + nullptr}; }; return runFunctionSpecialization(M, DL, GetTLI, GetTTI, GetAC, GetAnalysis); } diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp --- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp +++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp @@ -339,6 +339,13 @@ return A->second.PredInfo->getPredicateInfoFor(I); } + const LoopInfo &getLoopInfo(Function &F) { + auto A = AnalysisResults.find(&F); + assert(A != AnalysisResults.end() && A->second.LI && + "Need LoopInfo analysis results for function."); + return *A->second.LI; + } + DomTreeUpdater getDTU(Function &F) { auto A = AnalysisResults.find(&F); assert(A != AnalysisResults.end() && "Need analysis results for function."); @@ -1527,6 +1534,10 @@ return Visitor->getPredicateInfoFor(I); } +const LoopInfo &SCCPSolver::getLoopInfo(Function &F) { + return Visitor->getLoopInfo(F); +} + DomTreeUpdater SCCPSolver::getDTU(Function &F) { return Visitor->getDTU(F); } void SCCPSolver::trackValueOfGlobalVariable(GlobalVariable *GV) {