Skip to content

Commit 329e706

Browse files
authored
[NFC][WPD] Pass the module analysis manager instead of lambdas (llvm#155338)
Easier to evolve - if we need more analyses, it becomes clumsy to keep passing around lambdas.
1 parent 36ca674 commit 329e706

File tree

1 file changed

+19
-35
lines changed

1 file changed

+19
-35
lines changed

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,8 @@ void VTableSlotInfo::addCallSite(Value *VTable, CallBase &CB,
572572

573573
struct DevirtModule {
574574
Module &M;
575-
function_ref<AAResults &(Function &)> AARGetter;
576-
function_ref<DominatorTree &(Function &)> LookupDomTree;
575+
ModuleAnalysisManager &MAM;
576+
FunctionAnalysisManager &FAM;
577577

578578
ModuleSummaryIndex *const ExportSummary;
579579
const ModuleSummaryIndex *const ImportSummary;
@@ -589,7 +589,7 @@ struct DevirtModule {
589589
ArrayType *const Int8Arr0Ty;
590590

591591
const bool RemarksEnabled;
592-
function_ref<OptimizationRemarkEmitter &(Function &)> OREGetter;
592+
std::function<OptimizationRemarkEmitter &(Function &)> OREGetter;
593593
MapVector<VTableSlot, VTableSlotInfo> CallSlots;
594594

595595
// Calls that have already been optimized. We may add a call to multiple
@@ -612,20 +612,22 @@ struct DevirtModule {
612612
std::map<CallInst *, unsigned> NumUnsafeUsesForTypeTest;
613613
PatternList FunctionsToSkip;
614614

615-
DevirtModule(Module &M, function_ref<AAResults &(Function &)> AARGetter,
616-
function_ref<OptimizationRemarkEmitter &(Function &)> OREGetter,
617-
function_ref<DominatorTree &(Function &)> LookupDomTree,
615+
DevirtModule(Module &M, ModuleAnalysisManager &MAM,
618616
ModuleSummaryIndex *ExportSummary,
619617
const ModuleSummaryIndex *ImportSummary)
620-
: M(M), AARGetter(AARGetter), LookupDomTree(LookupDomTree),
618+
: M(M), MAM(MAM),
619+
FAM(MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager()),
621620
ExportSummary(ExportSummary), ImportSummary(ImportSummary),
622621
Int8Ty(Type::getInt8Ty(M.getContext())),
623622
Int8PtrTy(PointerType::getUnqual(M.getContext())),
624623
Int32Ty(Type::getInt32Ty(M.getContext())),
625624
Int64Ty(Type::getInt64Ty(M.getContext())),
626625
IntPtrTy(M.getDataLayout().getIntPtrType(M.getContext(), 0)),
627626
Int8Arr0Ty(ArrayType::get(Type::getInt8Ty(M.getContext()), 0)),
628-
RemarksEnabled(areRemarksEnabled()), OREGetter(OREGetter) {
627+
RemarksEnabled(areRemarksEnabled()),
628+
OREGetter([&](Function &F) -> OptimizationRemarkEmitter & {
629+
return FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
630+
}) {
629631
assert(!(ExportSummary && ImportSummary));
630632
FunctionsToSkip.init(SkipFunctionNames);
631633
}
@@ -739,10 +741,7 @@ struct DevirtModule {
739741

740742
// Lower the module using the action and summary passed as command line
741743
// arguments. For testing purposes only.
742-
static bool
743-
runForTesting(Module &M, function_ref<AAResults &(Function &)> AARGetter,
744-
function_ref<OptimizationRemarkEmitter &(Function &)> OREGetter,
745-
function_ref<DominatorTree &(Function &)> LookupDomTree);
744+
static bool runForTesting(Module &M, ModuleAnalysisManager &MAM);
746745
};
747746

748747
struct DevirtIndex {
@@ -783,25 +782,13 @@ struct DevirtIndex {
783782
} // end anonymous namespace
784783

785784
PreservedAnalyses WholeProgramDevirtPass::run(Module &M,
786-
ModuleAnalysisManager &AM) {
787-
auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
788-
auto AARGetter = [&](Function &F) -> AAResults & {
789-
return FAM.getResult<AAManager>(F);
790-
};
791-
auto OREGetter = [&](Function &F) -> OptimizationRemarkEmitter & {
792-
return FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
793-
};
794-
auto LookupDomTree = [&FAM](Function &F) -> DominatorTree & {
795-
return FAM.getResult<DominatorTreeAnalysis>(F);
796-
};
785+
ModuleAnalysisManager &MAM) {
797786
if (UseCommandLine) {
798-
if (!DevirtModule::runForTesting(M, AARGetter, OREGetter, LookupDomTree))
787+
if (!DevirtModule::runForTesting(M, MAM))
799788
return PreservedAnalyses::all();
800789
return PreservedAnalyses::none();
801790
}
802-
if (!DevirtModule(M, AARGetter, OREGetter, LookupDomTree, ExportSummary,
803-
ImportSummary)
804-
.run())
791+
if (!DevirtModule(M, MAM, ExportSummary, ImportSummary).run())
805792
return PreservedAnalyses::all();
806793
return PreservedAnalyses::none();
807794
}
@@ -996,10 +983,7 @@ static Error checkCombinedSummaryForTesting(ModuleSummaryIndex *Summary) {
996983
return ErrorSuccess();
997984
}
998985

999-
bool DevirtModule::runForTesting(
1000-
Module &M, function_ref<AAResults &(Function &)> AARGetter,
1001-
function_ref<OptimizationRemarkEmitter &(Function &)> OREGetter,
1002-
function_ref<DominatorTree &(Function &)> LookupDomTree) {
986+
bool DevirtModule::runForTesting(Module &M, ModuleAnalysisManager &MAM) {
1003987
std::unique_ptr<ModuleSummaryIndex> Summary =
1004988
std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
1005989

@@ -1024,7 +1008,7 @@ bool DevirtModule::runForTesting(
10241008
}
10251009

10261010
bool Changed =
1027-
DevirtModule(M, AARGetter, OREGetter, LookupDomTree,
1011+
DevirtModule(M, MAM,
10281012
ClSummaryAction == PassSummaryAction::Export ? Summary.get()
10291013
: nullptr,
10301014
ClSummaryAction == PassSummaryAction::Import ? Summary.get()
@@ -1877,7 +1861,7 @@ bool DevirtModule::tryVirtualConstProp(
18771861
return false;
18781862

18791863
if (Fn->isDeclaration() ||
1880-
!computeFunctionBodyMemoryAccess(*Fn, AARGetter(*Fn))
1864+
!computeFunctionBodyMemoryAccess(*Fn, FAM.getResult<AAManager>(*Fn))
18811865
.doesNotAccessMemory() ||
18821866
Fn->arg_empty() || !Fn->arg_begin()->use_empty() ||
18831867
Fn->getReturnType() != RetType)
@@ -2051,7 +2035,7 @@ void DevirtModule::scanTypeTestUsers(
20512035
// Search for virtual calls based on %p and add them to DevirtCalls.
20522036
SmallVector<DevirtCallSite, 1> DevirtCalls;
20532037
SmallVector<CallInst *, 1> Assumes;
2054-
auto &DT = LookupDomTree(*CI->getFunction());
2038+
auto &DT = FAM.getResult<DominatorTreeAnalysis>(*CI->getFunction());
20552039
findDevirtualizableCallsForTypeTest(DevirtCalls, Assumes, CI, DT);
20562040

20572041
Metadata *TypeId =
@@ -2128,7 +2112,7 @@ void DevirtModule::scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc) {
21282112
SmallVector<Instruction *, 1> LoadedPtrs;
21292113
SmallVector<Instruction *, 1> Preds;
21302114
bool HasNonCallUses = false;
2131-
auto &DT = LookupDomTree(*CI->getFunction());
2115+
auto &DT = FAM.getResult<DominatorTreeAnalysis>(*CI->getFunction());
21322116
findDevirtualizableCallsForTypeCheckedLoad(DevirtCalls, LoadedPtrs, Preds,
21332117
HasNonCallUses, CI, DT);
21342118

0 commit comments

Comments
 (0)