Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SimplifyCFG] Enable LastRunTrackingAnalysis for SimplifyCFGPass #116740

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/Transforms/Scalar/SimplifyCFG.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace llvm {
/// it may further optimize control-flow to create non-canonical forms.
class SimplifyCFGPass : public PassInfoMixin<SimplifyCFGPass> {
SimplifyCFGOptions Options;
static char ID;

public:
/// The default constructor sets the pass options to create canonical IR,
Expand Down
16 changes: 16 additions & 0 deletions llvm/include/llvm/Transforms/Utils/SimplifyCFGOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion llvm/lib/Transforms/Scalar/SimplifyCFGPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -373,19 +374,31 @@ void SimplifyCFGPass::printPipeline(

PreservedAnalyses SimplifyCFGPass::run(Function &F,
FunctionAnalysisManager &AM) {
auto &LRT = AM.getResult<LastRunTrackingAnalysis>(F);
// No changes since last SimplifyCFGPass pass, exit early.
if (LRT.shouldSkip(&ID, Options))
return PreservedAnalyses::all();

auto &TTI = AM.getResult<TargetIRAnalysis>(F);
Options.AC = &AM.getResult<AssumptionAnalysis>(F);
DominatorTree *DT = nullptr;
if (RequireAndPreserveDomTree)
DT = &AM.getResult<DominatorTreeAnalysis>(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<DominatorTreeAnalysis>();
LRT.update(&ID, /*Changed=*/true, Options);
PA.preserve<LastRunTrackingAnalysis>();
return PA;
}

char SimplifyCFGPass::ID = 0;

namespace {
struct CFGSimplifyPass : public FunctionPass {
static char ID;
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Other/new-pm-defaults.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Other/new-pm-thinlto-prelink-defaults.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Other/new-pm-thinlto-prelink-pgo-defaults.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions llvm/unittests/IR/PassManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down