From 79a2f63b9a3218386a214d426996b97cef51ffe7 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Tue, 19 Nov 2024 12:23:37 +0800 Subject: [PATCH] [SimplifyCFG] Enable LastRunTrackingAnalysis for SimplifyCFGPass --- .../include/llvm/Transforms/Scalar/SimplifyCFG.h | 1 + .../llvm/Transforms/Utils/SimplifyCFGOptions.h | 16 ++++++++++++++++ llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp | 15 ++++++++++++++- llvm/test/Other/new-pm-defaults.ll | 2 +- .../Other/new-pm-thinlto-prelink-defaults.ll | 2 +- .../Other/new-pm-thinlto-prelink-pgo-defaults.ll | 4 ++-- .../new-pm-thinlto-prelink-samplepgo-defaults.ll | 2 +- llvm/unittests/IR/PassManagerTest.cpp | 4 ++++ 8 files changed, 40 insertions(+), 6 deletions(-) diff --git a/llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h b/llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h index 67db5031a44383..a36d11b1657dcd 100644 --- a/llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h +++ b/llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h @@ -28,6 +28,7 @@ namespace llvm { /// it may further optimize control-flow to create non-canonical forms. class SimplifyCFGPass : public PassInfoMixin { SimplifyCFGOptions Options; + static char ID; public: /// The default constructor sets the pass options to create canonical IR, diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h b/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h index ee3cc950cdb503..6c8f57f1054933 100644 --- a/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h +++ b/llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h @@ -85,6 +85,22 @@ struct SimplifyCFGOptions { SpeculateUnpredictables = B; return *this; } + + bool isCompatibleWith(const SimplifyCFGOptions &LastOptions) const { + return BonusInstThreshold == LastOptions.BonusInstThreshold && + ForwardSwitchCondToPhi == LastOptions.ForwardSwitchCondToPhi && + ConvertSwitchRangeToICmp == LastOptions.ConvertSwitchRangeToICmp && + ConvertSwitchToLookupTable == + LastOptions.ConvertSwitchToLookupTable && + NeedCanonicalLoop == LastOptions.NeedCanonicalLoop && + HoistCommonInsts == LastOptions.HoistCommonInsts && + HoistLoadsStoresWithCondFaulting == + LastOptions.HoistLoadsStoresWithCondFaulting && + SinkCommonInsts == LastOptions.SinkCommonInsts && + SimplifyCondBranch == LastOptions.SimplifyCondBranch && + SpeculateBlocks == LastOptions.SpeculateBlocks && + SpeculateUnpredictables == LastOptions.SpeculateUnpredictables; + } }; } // namespace llvm diff --git a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp index 4e437e9abeb43e..1000d6f3db6010 100644 --- a/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp +++ b/llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp @@ -28,6 +28,7 @@ #include "llvm/Analysis/CFG.h" #include "llvm/Analysis/DomTreeUpdater.h" #include "llvm/Analysis/GlobalsModRef.h" +#include "llvm/Analysis/LastRunTrackingAnalysis.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/IR/Attributes.h" #include "llvm/IR/CFG.h" @@ -373,19 +374,31 @@ void SimplifyCFGPass::printPipeline( PreservedAnalyses SimplifyCFGPass::run(Function &F, FunctionAnalysisManager &AM) { + auto &LRT = AM.getResult(F); + // No changes since last SimplifyCFGPass pass, exit early. + if (LRT.shouldSkip(&ID, Options)) + return PreservedAnalyses::all(); + auto &TTI = AM.getResult(F); Options.AC = &AM.getResult(F); DominatorTree *DT = nullptr; if (RequireAndPreserveDomTree) DT = &AM.getResult(F); - if (!simplifyFunctionCFG(F, TTI, DT, Options)) + if (!simplifyFunctionCFG(F, TTI, DT, Options)) { + LRT.update(&ID, /*Changed=*/false, Options); return PreservedAnalyses::all(); + } + PreservedAnalyses PA; if (RequireAndPreserveDomTree) PA.preserve(); + LRT.update(&ID, /*Changed=*/true, Options); + PA.preserve(); return PA; } +char SimplifyCFGPass::ID = 0; + namespace { struct CFGSimplifyPass : public FunctionPass { static char ID; diff --git a/llvm/test/Other/new-pm-defaults.ll b/llvm/test/Other/new-pm-defaults.ll index 7cf035b0c6f376..4abf3a809c7c72 100644 --- a/llvm/test/Other/new-pm-defaults.ll +++ b/llvm/test/Other/new-pm-defaults.ll @@ -104,6 +104,7 @@ ; CHECK-O-NEXT: Running pass: EntryExitInstrumenterPass ; CHECK-O-NEXT: Running pass: LowerExpectIntrinsicPass ; CHECK-O-NEXT: Running pass: SimplifyCFGPass +; CHECK-O-NEXT: Running analysis: LastRunTrackingAnalysis ; CHECK-O-NEXT: Running analysis: TargetIRAnalysis ; CHECK-O-NEXT: Running analysis: AssumptionAnalysis ; CHECK-O-NEXT: Running pass: SROAPass @@ -118,7 +119,6 @@ ; CHECK-O-NEXT: Running pass: GlobalOptPass ; CHECK-O-NEXT: Running pass: PromotePass ; CHECK-O-NEXT: Running pass: InstCombinePass -; CHECK-O-NEXT: Running analysis: LastRunTrackingAnalysis ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis ; CHECK-O-NEXT: Running analysis: AAManager ; CHECK-O-NEXT: Running analysis: BasicAA diff --git a/llvm/test/Other/new-pm-thinlto-prelink-defaults.ll b/llvm/test/Other/new-pm-thinlto-prelink-defaults.ll index 5aacd26def2be5..fef1bfc32afa45 100644 --- a/llvm/test/Other/new-pm-thinlto-prelink-defaults.ll +++ b/llvm/test/Other/new-pm-thinlto-prelink-defaults.ll @@ -73,6 +73,7 @@ ; CHECK-O-NEXT: Running pass: EntryExitInstrumenterPass ; CHECK-O-NEXT: Running pass: LowerExpectIntrinsicPass ; CHECK-O-NEXT: Running pass: SimplifyCFGPass +; CHECK-O-NEXT: Running analysis: LastRunTrackingAnalysis ; CHECK-O-NEXT: Running analysis: TargetIRAnalysis ; CHECK-O-NEXT: Running analysis: AssumptionAnalysis ; CHECK-O-NEXT: Running pass: SROAPass @@ -86,7 +87,6 @@ ; CHECK-O-NEXT: Running pass: GlobalOptPass ; CHECK-O-NEXT: Running pass: PromotePass ; CHECK-O-NEXT: Running pass: InstCombinePass -; CHECK-O-NEXT: Running analysis: LastRunTrackingAnalysis ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis ; CHECK-O-NEXT: Running analysis: AAManager ; CHECK-O-NEXT: Running analysis: BasicAA diff --git a/llvm/test/Other/new-pm-thinlto-prelink-pgo-defaults.ll b/llvm/test/Other/new-pm-thinlto-prelink-pgo-defaults.ll index f6a94065968038..b201f714c0938d 100644 --- a/llvm/test/Other/new-pm-thinlto-prelink-pgo-defaults.ll +++ b/llvm/test/Other/new-pm-thinlto-prelink-pgo-defaults.ll @@ -37,6 +37,7 @@ ; CHECK-O-NEXT: Running pass: EntryExitInstrumenterPass ; CHECK-O-NEXT: Running pass: LowerExpectIntrinsicPass ; CHECK-O-NEXT: Running pass: SimplifyCFGPass +; CHECK-O-NEXT: Running analysis: LastRunTrackingAnalysis ; CHECK-O-NEXT: Running analysis: TargetIRAnalysis ; CHECK-O-NEXT: Running analysis: AssumptionAnalysis ; CHECK-O-NEXT: Running pass: SROAPass @@ -50,7 +51,6 @@ ; CHECK-O-NEXT: Running pass: GlobalOptPass ; CHECK-O-NEXT: Running pass: PromotePass ; CHECK-O-NEXT: Running pass: InstCombinePass -; CHECK-O-NEXT: Running analysis: LastRunTrackingAnalysis ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis ; CHECK-O-NEXT: Running analysis: AAManager ; CHECK-O-NEXT: Running analysis: BasicAA @@ -122,8 +122,8 @@ ; CHECK-O23SZ-NEXT: Running pass: CorrelatedValuePropagationPass ; CHECK-O23SZ-NEXT: Invalidating analysis: LazyValueAnalysis ; CHECK-O-NEXT: Running pass: SimplifyCFGPass -; CHECK-O-NEXT: Running pass: InstCombinePass ; CHECK-O-NEXT: Running analysis: LastRunTrackingAnalysis +; CHECK-O-NEXT: Running pass: InstCombinePass ; CHECK-O-NEXT: Running analysis: BlockFrequencyAnalysis on foo ; CHECK-O-NEXT: Running analysis: BranchProbabilityAnalysis on foo ; CHECK-O-NEXT: Running analysis: LoopAnalysis on foo diff --git a/llvm/test/Other/new-pm-thinlto-prelink-samplepgo-defaults.ll b/llvm/test/Other/new-pm-thinlto-prelink-samplepgo-defaults.ll index 48a9433d249996..eccbd7026eab20 100644 --- a/llvm/test/Other/new-pm-thinlto-prelink-samplepgo-defaults.ll +++ b/llvm/test/Other/new-pm-thinlto-prelink-samplepgo-defaults.ll @@ -36,6 +36,7 @@ ; CHECK-O-NEXT: Running pass: EntryExitInstrumenterPass ; CHECK-O-NEXT: Running pass: LowerExpectIntrinsicPass ; CHECK-O-NEXT: Running pass: SimplifyCFGPass +; CHECK-O-NEXT: Running analysis: LastRunTrackingAnalysis ; CHECK-O-NEXT: Running analysis: TargetIRAnalysis ; CHECK-O-NEXT: Running analysis: AssumptionAnalysis ; CHECK-O-NEXT: Running pass: SROAPass @@ -53,7 +54,6 @@ ; CHECK-O-NEXT: Running pass: GlobalOptPass ; CHECK-O-NEXT: Running pass: PromotePass ; CHECK-O-NEXT: Running pass: InstCombinePass -; CHECK-O-NEXT: Running analysis: LastRunTrackingAnalysis ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis on foo ; CHECK-O-NEXT: Running analysis: AAManager on foo ; CHECK-O-NEXT: Running analysis: BasicAA diff --git a/llvm/unittests/IR/PassManagerTest.cpp b/llvm/unittests/IR/PassManagerTest.cpp index a6487169224c2b..d4d12d73ef2fef 100644 --- a/llvm/unittests/IR/PassManagerTest.cpp +++ b/llvm/unittests/IR/PassManagerTest.cpp @@ -8,6 +8,7 @@ #include "llvm/IR/PassManager.h" #include "llvm/Analysis/AssumptionCache.h" +#include "llvm/Analysis/LastRunTrackingAnalysis.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/AsmParser/Parser.h" #include "llvm/IR/Dominators.h" @@ -835,6 +836,7 @@ TEST_F(PassManagerTest, FunctionPassCFGChecker) { FAM.registerPass([&] { return DominatorTreeAnalysis(); }); FAM.registerPass([&] { return AssumptionAnalysis(); }); FAM.registerPass([&] { return TargetIRAnalysis(); }); + FAM.registerPass([&] { return LastRunTrackingAnalysis(); }); FPM.addPass(SimplifyCFGPass()); FPM.run(*F, FAM); @@ -884,6 +886,7 @@ TEST_F(PassManagerTest, FunctionPassCFGCheckerInvalidateAnalysis) { FAM.registerPass([&] { return DominatorTreeAnalysis(); }); FAM.registerPass([&] { return AssumptionAnalysis(); }); FAM.registerPass([&] { return TargetIRAnalysis(); }); + FAM.registerPass([&] { return LastRunTrackingAnalysis(); }); FPM.addPass(TestSimplifyCFGInvalidatingAnalysisPass()); FPM.run(*F, FAM); @@ -952,6 +955,7 @@ TEST_F(PassManagerTest, FunctionPassCFGCheckerWrapped) { FAM.registerPass([&] { return DominatorTreeAnalysis(); }); FAM.registerPass([&] { return AssumptionAnalysis(); }); FAM.registerPass([&] { return TargetIRAnalysis(); }); + FAM.registerPass([&] { return LastRunTrackingAnalysis(); }); FunctionPassManager InnerFPM; InnerFPM.addPass(SimplifyCFGPass());