@@ -1941,7 +1941,8 @@ OptimizeFunctions(Module &M,
1941
1941
function_ref<TargetTransformInfo &(Function &)> GetTTI,
1942
1942
function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
1943
1943
function_ref<DominatorTree &(Function &)> LookupDomTree,
1944
- SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
1944
+ SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats,
1945
+ function_ref<void(Function &F)> ChangedCFGCallback) {
1945
1946
1946
1947
bool Changed = false ;
1947
1948
@@ -1974,13 +1975,11 @@ OptimizeFunctions(Module &M,
1974
1975
// So, remove unreachable blocks from the function, because a) there's
1975
1976
// no point in analyzing them and b) GlobalOpt should otherwise grow
1976
1977
// some more complicated logic to break these cycles.
1977
- // Removing unreachable blocks might invalidate the dominator so we
1978
- // recalculate it.
1978
+ // Notify the analysis manager that we've modified the function's CFG.
1979
1979
if (!F.isDeclaration ()) {
1980
1980
if (removeUnreachableBlocks (F)) {
1981
- auto &DT = LookupDomTree (F);
1982
- DT.recalculate (F);
1983
1981
Changed = true ;
1982
+ ChangedCFGCallback (F);
1984
1983
}
1985
1984
}
1986
1985
@@ -2443,12 +2442,13 @@ static bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
2443
2442
return Changed;
2444
2443
}
2445
2444
2446
- static bool optimizeGlobalsInModule (
2447
- Module &M, const DataLayout &DL,
2448
- function_ref<TargetLibraryInfo &(Function &)> GetTLI,
2449
- function_ref<TargetTransformInfo &(Function &)> GetTTI,
2450
- function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
2451
- function_ref<DominatorTree &(Function &)> LookupDomTree) {
2445
+ static bool
2446
+ optimizeGlobalsInModule (Module &M, const DataLayout &DL,
2447
+ function_ref<TargetLibraryInfo &(Function &)> GetTLI,
2448
+ function_ref<TargetTransformInfo &(Function &)> GetTTI,
2449
+ function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
2450
+ function_ref<DominatorTree &(Function &)> LookupDomTree,
2451
+ function_ref<void(Function &F)> ChangedCFGCallback) {
2452
2452
SmallPtrSet<const Comdat *, 8 > NotDiscardableComdats;
2453
2453
bool Changed = false ;
2454
2454
bool LocalChange = true ;
@@ -2473,7 +2473,7 @@ static bool optimizeGlobalsInModule(
2473
2473
2474
2474
// Delete functions that are trivially dead, ccc -> fastcc
2475
2475
LocalChange |= OptimizeFunctions (M, GetTLI, GetTTI, GetBFI, LookupDomTree,
2476
- NotDiscardableComdats);
2476
+ NotDiscardableComdats, ChangedCFGCallback );
2477
2477
2478
2478
// Optimize global_ctors list.
2479
2479
LocalChange |=
@@ -2526,10 +2526,22 @@ PreservedAnalyses GlobalOptPass::run(Module &M, ModuleAnalysisManager &AM) {
2526
2526
auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
2527
2527
return FAM.getResult <BlockFrequencyAnalysis>(F);
2528
2528
};
2529
+ auto ChangedCFGCallback = [&FAM](Function &F) {
2530
+ FAM.invalidate (F, PreservedAnalyses::none ());
2531
+ };
2529
2532
2530
- if (!optimizeGlobalsInModule (M, DL, GetTLI, GetTTI, GetBFI, LookupDomTree))
2533
+ if (!optimizeGlobalsInModule (M, DL, GetTLI, GetTTI, GetBFI, LookupDomTree,
2534
+ ChangedCFGCallback))
2531
2535
return PreservedAnalyses::all ();
2532
- return PreservedAnalyses::none ();
2536
+
2537
+ PreservedAnalyses PA = PreservedAnalyses::none ();
2538
+ // We have not removed or replaced any functions.
2539
+ PA.preserve <FunctionAnalysisManagerModuleProxy>();
2540
+ // The only place we modify the CFG is when calling
2541
+ // removeUnreachableBlocks(), but there we make sure to invalidate analyses
2542
+ // for modified functions.
2543
+ PA.preserveSet <CFGAnalyses>();
2544
+ return PA;
2533
2545
}
2534
2546
2535
2547
namespace {
@@ -2560,8 +2572,13 @@ struct GlobalOptLegacyPass : public ModulePass {
2560
2572
return this ->getAnalysis <BlockFrequencyInfoWrapperPass>(F).getBFI ();
2561
2573
};
2562
2574
2563
- return optimizeGlobalsInModule (M, DL, GetTLI, GetTTI, GetBFI,
2564
- LookupDomTree);
2575
+ auto ChangedCFGCallback = [&LookupDomTree](Function &F) {
2576
+ auto &DT = LookupDomTree (F);
2577
+ DT.recalculate (F);
2578
+ };
2579
+
2580
+ return optimizeGlobalsInModule (M, DL, GetTLI, GetTTI, GetBFI, LookupDomTree,
2581
+ ChangedCFGCallback);
2565
2582
}
2566
2583
2567
2584
void getAnalysisUsage (AnalysisUsage &AU) const override {
0 commit comments