Skip to content

Commit c9b4dc3

Browse files
[FuncSpec][NFC] Avoid redundant computations of DominatorTree/LoopInfo
The `FunctionSpecialization` pass needs loop analysis results for its cost function. For this purpose, it computes the `DominatorTree` and `LoopInfo` for a function in `getSpecializationBonus`. This function, however, is called O(number of call sites x number of arguments), but the DominatorTree/LoopInfo can be computed just once. This patch plugs into the PassManager infrastructure to obtain LoopInfo for a function and removes ad-hoc computation from `getSpecializatioBonus`. Reviewed By: ChuanqiXu, labrinea Differential Revision: https://reviews.llvm.org/D136332
1 parent 524c640 commit c9b4dc3

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

Diff for: llvm/include/llvm/Transforms/Utils/SCCPSolver.h

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Function;
3030
class GlobalVariable;
3131
class Instruction;
3232
class LLVMContext;
33+
class LoopInfo;
3334
class PostDominatorTree;
3435
class StructType;
3536
class TargetLibraryInfo;
@@ -41,6 +42,7 @@ struct AnalysisResultsForFn {
4142
std::unique_ptr<PredicateInfo> PredInfo;
4243
DominatorTree *DT;
4344
PostDominatorTree *PDT;
45+
LoopInfo *LI;
4446
};
4547

4648
/// Helper struct shared between Function Specialization and SCCP Solver.
@@ -77,6 +79,8 @@ class SCCPSolver {
7779

7880
const PredicateBase *getPredicateInfoFor(Instruction *I);
7981

82+
const LoopInfo &getLoopInfo(Function &F);
83+
8084
DomTreeUpdater getDTU(Function &F);
8185

8286
/// trackValueOfGlobalVariable - Clients can use this method to

Diff for: llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ class FunctionSpecializer {
463463
}
464464

465465
SpecializationInfo &S = Specializations.back().second;
466-
S.Gain += getSpecializationBonus(A, C);
466+
S.Gain += getSpecializationBonus(A, C, Solver.getLoopInfo(*F));
467467
S.Args.push_back({A, C});
468468
}
469469
Added = false;
@@ -580,7 +580,7 @@ class FunctionSpecializer {
580580
}
581581

582582
InstructionCost getUserBonus(User *U, llvm::TargetTransformInfo &TTI,
583-
LoopInfo &LI) {
583+
const LoopInfo &LI) {
584584
auto *I = dyn_cast_or_null<Instruction>(U);
585585
// If not an instruction we do not know how to evaluate.
586586
// Keep minimum possible cost for now so that it doesnt affect
@@ -605,10 +605,9 @@ class FunctionSpecializer {
605605
}
606606

607607
/// Compute a bonus for replacing argument \p A with constant \p C.
608-
InstructionCost getSpecializationBonus(Argument *A, Constant *C) {
608+
InstructionCost getSpecializationBonus(Argument *A, Constant *C,
609+
const LoopInfo &LI) {
609610
Function *F = A->getParent();
610-
DominatorTree DT(*F);
611-
LoopInfo LI(DT);
612611
auto &TTI = (GetTTI)(*F);
613612
LLVM_DEBUG(dbgs() << "FnSpecialization: Analysing bonus for constant: "
614613
<< C->getNameOrAsOperand() << "\n");

Diff for: llvm/lib/Transforms/IPO/SCCP.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "llvm/Transforms/IPO/SCCP.h"
1414
#include "llvm/Analysis/AssumptionCache.h"
15+
#include "llvm/Analysis/LoopInfo.h"
1516
#include "llvm/Analysis/PostDominators.h"
1617
#include "llvm/Analysis/TargetLibraryInfo.h"
1718
#include "llvm/Analysis/TargetTransformInfo.h"
@@ -32,7 +33,8 @@ PreservedAnalyses IPSCCPPass::run(Module &M, ModuleAnalysisManager &AM) {
3233
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
3334
return {
3435
std::make_unique<PredicateInfo>(F, DT, FAM.getResult<AssumptionAnalysis>(F)),
35-
&DT, FAM.getCachedResult<PostDominatorTreeAnalysis>(F)};
36+
&DT, FAM.getCachedResult<PostDominatorTreeAnalysis>(F),
37+
nullptr};
3638
};
3739

3840
if (!runIPSCCP(M, DL, GetTLI, getAnalysis))
@@ -75,8 +77,9 @@ class IPSCCPLegacyPass : public ModulePass {
7577
F, DT,
7678
this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
7779
F)),
78-
nullptr, // We cannot preserve the DT or PDT with the legacy pass
79-
nullptr}; // manager, so set them to nullptr.
80+
nullptr, // We cannot preserve the LI, DT or PDT with the legacy pass
81+
nullptr, // manager, so set them to nullptr.
82+
nullptr};
8083
};
8184

8285
return runIPSCCP(M, DL, GetTLI, getAnalysis);
@@ -123,7 +126,8 @@ PreservedAnalyses FunctionSpecializationPass::run(Module &M,
123126
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
124127
return {std::make_unique<PredicateInfo>(
125128
F, DT, FAM.getResult<AssumptionAnalysis>(F)),
126-
&DT, FAM.getCachedResult<PostDominatorTreeAnalysis>(F)};
129+
&DT, FAM.getCachedResult<PostDominatorTreeAnalysis>(F),
130+
&FAM.getResult<LoopAnalysis>(F)};
127131
};
128132

129133
if (!runFunctionSpecialization(M, DL, GetTLI, GetTTI, GetAC, GetAnalysis))
@@ -171,8 +175,9 @@ struct FunctionSpecializationLegacyPass : public ModulePass {
171175
F, DT,
172176
this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
173177
F)),
174-
nullptr, // We cannot preserve the DT or PDT with the legacy pass
175-
nullptr}; // manager, so set them to nullptr.
178+
nullptr, // We cannot preserve the LI, DT, or PDT with the legacy pass
179+
nullptr, // manager, so set them to nullptr.
180+
nullptr};
176181
};
177182
return runFunctionSpecialization(M, DL, GetTLI, GetTTI, GetAC, GetAnalysis);
178183
}

Diff for: llvm/lib/Transforms/Utils/SCCPSolver.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,13 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
339339
return A->second.PredInfo->getPredicateInfoFor(I);
340340
}
341341

342+
const LoopInfo &getLoopInfo(Function &F) {
343+
auto A = AnalysisResults.find(&F);
344+
assert(A != AnalysisResults.end() && A->second.LI &&
345+
"Need LoopInfo analysis results for function.");
346+
return *A->second.LI;
347+
}
348+
342349
DomTreeUpdater getDTU(Function &F) {
343350
auto A = AnalysisResults.find(&F);
344351
assert(A != AnalysisResults.end() && "Need analysis results for function.");
@@ -1527,6 +1534,10 @@ const PredicateBase *SCCPSolver::getPredicateInfoFor(Instruction *I) {
15271534
return Visitor->getPredicateInfoFor(I);
15281535
}
15291536

1537+
const LoopInfo &SCCPSolver::getLoopInfo(Function &F) {
1538+
return Visitor->getLoopInfo(F);
1539+
}
1540+
15301541
DomTreeUpdater SCCPSolver::getDTU(Function &F) { return Visitor->getDTU(F); }
15311542

15321543
void SCCPSolver::trackValueOfGlobalVariable(GlobalVariable *GV) {

0 commit comments

Comments
 (0)