diff options
author | paperchalice <liujunchang97@outlook.com> | 2024-06-28 10:59:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-28 10:59:23 +0800 |
commit | d38b518e0444b42123ac05ac5cce267baecec294 (patch) | |
tree | aaf1bb85596b21b40ef8f35894c7ad5a7db5095c | |
parent | e48c4011ca80385573f1b92793c75dc98abb228f (diff) | |
download | llvm-d38b518e0444b42123ac05ac5cce267baecec294.zip llvm-d38b518e0444b42123ac05ac5cce267baecec294.tar.gz llvm-d38b518e0444b42123ac05ac5cce267baecec294.tar.bz2 |
Reapply "[CodeGen][NewPM] Port machine-branch-prob to new pass manager" (#96858) (#96869)
This reverts commit ab58b6d58edf6a7c8881044fc716ca435d7a0156.
In `CodeGen/Generic/MachineBranchProb.ll`, `llc` crashed with dumped MIR
when targeting PowerPC. Move test to `llc/new-pm`, which is X86
specific.
24 files changed, 184 insertions, 73 deletions
diff --git a/llvm/include/llvm/CodeGen/MachineBranchProbabilityInfo.h b/llvm/include/llvm/CodeGen/MachineBranchProbabilityInfo.h index bd54442..12d33f9 100644 --- a/llvm/include/llvm/CodeGen/MachineBranchProbabilityInfo.h +++ b/llvm/include/llvm/CodeGen/MachineBranchProbabilityInfo.h @@ -14,14 +14,13 @@ #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H #include "llvm/CodeGen/MachineBasicBlock.h" +#include "llvm/CodeGen/MachinePassManager.h" #include "llvm/Pass.h" #include "llvm/Support/BranchProbability.h" namespace llvm { -class MachineBranchProbabilityInfo : public ImmutablePass { - virtual void anchor(); - +class MachineBranchProbabilityInfo { // Default weight value. Used when we don't have information about the edge. // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of // the successors have a weight yet. But it doesn't make sense when providing @@ -31,13 +30,8 @@ class MachineBranchProbabilityInfo : public ImmutablePass { static const uint32_t DEFAULT_WEIGHT = 16; public: - static char ID; - - MachineBranchProbabilityInfo(); - - void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.setPreservesAll(); - } + bool invalidate(MachineFunction &, const PreservedAnalyses &PA, + MachineFunctionAnalysisManager::Invalidator &); // Return edge probability. BranchProbability getEdgeProbability(const MachineBasicBlock *Src, @@ -61,6 +55,45 @@ public: const MachineBasicBlock *Dst) const; }; +class MachineBranchProbabilityAnalysis + : public AnalysisInfoMixin<MachineBranchProbabilityAnalysis> { + friend AnalysisInfoMixin<MachineBranchProbabilityAnalysis>; + + static AnalysisKey Key; + +public: + using Result = MachineBranchProbabilityInfo; + + Result run(MachineFunction &, MachineFunctionAnalysisManager &); +}; + +class MachineBranchProbabilityPrinterPass + : public PassInfoMixin<MachineBranchProbabilityPrinterPass> { + raw_ostream &OS; + +public: + MachineBranchProbabilityPrinterPass(raw_ostream &OS) : OS(OS) {} + PreservedAnalyses run(MachineFunction &MF, + MachineFunctionAnalysisManager &MFAM); +}; + +class MachineBranchProbabilityInfoWrapperPass : public ImmutablePass { + virtual void anchor(); + + MachineBranchProbabilityInfo MBPI; + +public: + static char ID; + + MachineBranchProbabilityInfoWrapperPass(); + + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.setPreservesAll(); + } + + MachineBranchProbabilityInfo &getMBPI() { return MBPI; } + const MachineBranchProbabilityInfo &getMBPI() const { return MBPI; } +}; } diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h index 4ddb711..421c09a 100644 --- a/llvm/include/llvm/InitializePasses.h +++ b/llvm/include/llvm/InitializePasses.h @@ -182,7 +182,7 @@ void initializeMIRPrintingPassPass(PassRegistry&); void initializeMachineBlockFrequencyInfoPass(PassRegistry&); void initializeMachineBlockPlacementPass(PassRegistry&); void initializeMachineBlockPlacementStatsPass(PassRegistry&); -void initializeMachineBranchProbabilityInfoPass(PassRegistry&); +void initializeMachineBranchProbabilityInfoWrapperPassPass(PassRegistry &); void initializeMachineCFGPrinterPass(PassRegistry &); void initializeMachineCSEPass(PassRegistry&); void initializeMachineCombinerPass(PassRegistry&); diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def index b43f41b..b1542ab 100644 --- a/llvm/include/llvm/Passes/MachinePassRegistry.def +++ b/llvm/include/llvm/Passes/MachinePassRegistry.def @@ -89,6 +89,8 @@ LOOP_PASS("loop-reduce", LoopStrengthReducePass()) #ifndef MACHINE_FUNCTION_ANALYSIS #define MACHINE_FUNCTION_ANALYSIS(NAME, CREATE_PASS) #endif +MACHINE_FUNCTION_ANALYSIS("machine-branch-prob", + MachineBranchProbabilityAnalysis()) MACHINE_FUNCTION_ANALYSIS("machine-dom-tree", MachineDominatorTreeAnalysis()) MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree", MachinePostDominatorTreeAnalysis()) @@ -130,6 +132,8 @@ MACHINE_FUNCTION_PASS("finalize-isel", FinalizeISelPass()) MACHINE_FUNCTION_PASS("localstackalloc", LocalStackSlotAllocationPass()) MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass()) MACHINE_FUNCTION_PASS("print", PrintMIRPass()) +MACHINE_FUNCTION_PASS("print<machine-branch-prob>", + MachineBranchProbabilityPrinterPass(dbgs())) MACHINE_FUNCTION_PASS("print<machine-dom-tree>", MachineDominatorTreePrinterPass(dbgs())) MACHINE_FUNCTION_PASS("print<machine-post-dom-tree>", diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 40f4dc2..505bd8a 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -444,7 +444,7 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { AU.addRequired<MachineOptimizationRemarkEmitterPass>(); AU.addRequired<GCModuleInfo>(); AU.addRequired<LazyMachineBlockFrequencyInfoPass>(); - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); } bool AsmPrinter::doInitialization(Module &M) { @@ -1478,8 +1478,9 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) { ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() : nullptr; const MachineBranchProbabilityInfo *MBPI = - Features.BrProb ? &getAnalysis<MachineBranchProbabilityInfo>() - : nullptr; + Features.BrProb + ? &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI() + : nullptr; if (Features.BBFreq || Features.BrProb) { for (const MachineBasicBlock &MBB : MF) { diff --git a/llvm/lib/CodeGen/BranchFolding.cpp b/llvm/lib/CodeGen/BranchFolding.cpp index c6c48cf..1b6a6ee 100644 --- a/llvm/lib/CodeGen/BranchFolding.cpp +++ b/llvm/lib/CodeGen/BranchFolding.cpp @@ -99,7 +99,7 @@ namespace { void getAnalysisUsage(AnalysisUsage &AU) const override { AU.addRequired<MachineBlockFrequencyInfo>(); - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<ProfileSummaryInfoWrapperPass>(); AU.addRequired<TargetPassConfig>(); MachineFunctionPass::getAnalysisUsage(AU); @@ -131,9 +131,10 @@ bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) { PassConfig->getEnableTailMerge(); MBFIWrapper MBBFreqInfo( getAnalysis<MachineBlockFrequencyInfo>()); - BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo, - getAnalysis<MachineBranchProbabilityInfo>(), - &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI()); + BranchFolder Folder( + EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo, + getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(), + &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI()); return Folder.OptimizeFunction(MF, MF.getSubtarget().getInstrInfo(), MF.getSubtarget().getRegisterInfo()); } diff --git a/llvm/lib/CodeGen/EarlyIfConversion.cpp b/llvm/lib/CodeGen/EarlyIfConversion.cpp index 0135f33..5f3e850 100644 --- a/llvm/lib/CodeGen/EarlyIfConversion.cpp +++ b/llvm/lib/CodeGen/EarlyIfConversion.cpp @@ -787,14 +787,14 @@ char &llvm::EarlyIfConverterID = EarlyIfConverter::ID; INITIALIZE_PASS_BEGIN(EarlyIfConverter, DEBUG_TYPE, "Early If Converter", false, false) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics) INITIALIZE_PASS_END(EarlyIfConverter, DEBUG_TYPE, "Early If Converter", false, false) void EarlyIfConverter::getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineDominatorTreeWrapperPass>(); AU.addPreserved<MachineDominatorTreeWrapperPass>(); AU.addRequired<MachineLoopInfo>(); @@ -1142,12 +1142,12 @@ char &llvm::EarlyIfPredicatorID = EarlyIfPredicator::ID; INITIALIZE_PASS_BEGIN(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator", false, false) INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_END(EarlyIfPredicator, DEBUG_TYPE, "Early If Predicator", false, false) void EarlyIfPredicator::getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineDominatorTreeWrapperPass>(); AU.addPreserved<MachineDominatorTreeWrapperPass>(); AU.addRequired<MachineLoopInfo>(); @@ -1222,7 +1222,7 @@ bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) { SchedModel.init(&STI); DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); Loops = &getAnalysis<MachineLoopInfo>(); - MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); bool Changed = false; IfConv.runOnMachineFunction(MF); diff --git a/llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp b/llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp index 383cb61..e067bd8 100644 --- a/llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp +++ b/llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp @@ -63,7 +63,7 @@ INITIALIZE_PASS_BEGIN(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false); INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, @@ -86,7 +86,7 @@ void RegBankSelect::init(MachineFunction &MF) { TPC = &getAnalysis<TargetPassConfig>(); if (OptMode != Mode::Fast) { MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); - MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); } else { MBFI = nullptr; MBPI = nullptr; @@ -100,7 +100,7 @@ void RegBankSelect::getAnalysisUsage(AnalysisUsage &AU) const { // We could preserve the information from these two analysis but // the APIs do not allow to do so yet. AU.addRequired<MachineBlockFrequencyInfo>(); - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); } AU.addRequired<TargetPassConfig>(); getSelectionDAGFallbackAnalysisUsage(AU); @@ -955,8 +955,10 @@ uint64_t RegBankSelect::EdgeInsertPoint::frequency(const Pass &P) const { if (WasMaterialized) return MBFI->getBlockFreq(DstOrSplit).getFrequency(); + auto *MBPIWrapper = + P.getAnalysisIfAvailable<MachineBranchProbabilityInfoWrapperPass>(); const MachineBranchProbabilityInfo *MBPI = - P.getAnalysisIfAvailable<MachineBranchProbabilityInfo>(); + MBPIWrapper ? &MBPIWrapper->getMBPI() : nullptr; if (!MBPI) return 1; // The basic block will be on the edge. diff --git a/llvm/lib/CodeGen/IfConversion.cpp b/llvm/lib/CodeGen/IfConversion.cpp index e8e276a..02cb95c 100644 --- a/llvm/lib/CodeGen/IfConversion.cpp +++ b/llvm/lib/CodeGen/IfConversion.cpp @@ -210,7 +210,7 @@ namespace { void getAnalysisUsage(AnalysisUsage &AU) const override { AU.addRequired<MachineBlockFrequencyInfo>(); - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<ProfileSummaryInfoWrapperPass>(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -432,7 +432,7 @@ char IfConverter::ID = 0; char &llvm::IfConverterID = IfConverter::ID; INITIALIZE_PASS_BEGIN(IfConverter, DEBUG_TYPE, "If Converter", false, false) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) INITIALIZE_PASS_END(IfConverter, DEBUG_TYPE, "If Converter", false, false) @@ -445,7 +445,7 @@ bool IfConverter::runOnMachineFunction(MachineFunction &MF) { TII = ST.getInstrInfo(); TRI = ST.getRegisterInfo(); MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>()); - MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); ProfileSummaryInfo *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); MRI = &MF.getRegInfo(); diff --git a/llvm/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp b/llvm/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp index 721b759..83b16fc 100644 --- a/llvm/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp +++ b/llvm/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp @@ -23,7 +23,7 @@ using namespace llvm; INITIALIZE_PASS_BEGIN(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE, "Lazy Machine Block Frequency Analysis", true, true) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) INITIALIZE_PASS_END(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE, "Lazy Machine Block Frequency Analysis", true, true) @@ -43,7 +43,7 @@ void LazyMachineBlockFrequencyInfoPass::print(raw_ostream &OS, void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage( AnalysisUsage &AU) const { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.setPreservesAll(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -62,7 +62,7 @@ LazyMachineBlockFrequencyInfoPass::calculateIfNotAvailable() const { return *MBFI; } - auto &MBPI = getAnalysis<MachineBranchProbabilityInfo>(); + auto &MBPI = getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); auto *MLI = getAnalysisIfAvailable<MachineLoopInfo>(); auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>(); auto *MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr; diff --git a/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp b/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp index 7ebecc6..2a68f18 100644 --- a/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp +++ b/llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp @@ -163,7 +163,7 @@ struct DOTGraphTraits<MachineBlockFrequencyInfo *> INITIALIZE_PASS_BEGIN(MachineBlockFrequencyInfo, DEBUG_TYPE, "Machine Block Frequency Analysis", true, true) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) INITIALIZE_PASS_END(MachineBlockFrequencyInfo, DEBUG_TYPE, "Machine Block Frequency Analysis", true, true) @@ -185,7 +185,7 @@ MachineBlockFrequencyInfo::MachineBlockFrequencyInfo( MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default; void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineLoopInfo>(); AU.setPreservesAll(); MachineFunctionPass::getAnalysisUsage(AU); @@ -209,7 +209,7 @@ void MachineBlockFrequencyInfo::calculate( bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) { MachineBranchProbabilityInfo &MBPI = - getAnalysis<MachineBranchProbabilityInfo>(); + getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); calculate(F, MBPI, MLI); return false; diff --git a/llvm/lib/CodeGen/MachineBlockPlacement.cpp b/llvm/lib/CodeGen/MachineBlockPlacement.cpp index 1cb71f3..a229475 100644 --- a/llvm/lib/CodeGen/MachineBlockPlacement.cpp +++ b/llvm/lib/CodeGen/MachineBlockPlacement.cpp @@ -608,7 +608,7 @@ public: } void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineBlockFrequencyInfo>(); if (TailDupPlacement) AU.addRequired<MachinePostDominatorTreeWrapperPass>(); @@ -627,7 +627,7 @@ char &llvm::MachineBlockPlacementID = MachineBlockPlacement::ID; INITIALIZE_PASS_BEGIN(MachineBlockPlacement, DEBUG_TYPE, "Branch Probability Basic Block Placement", false, false) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo) INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) @@ -3425,7 +3425,7 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) { return false; F = &MF; - MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); MBFI = std::make_unique<MBFIWrapper>( getAnalysis<MachineBlockFrequencyInfo>()); MLI = &getAnalysis<MachineLoopInfo>(); @@ -3726,7 +3726,7 @@ public: bool runOnMachineFunction(MachineFunction &F) override; void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineBlockFrequencyInfo>(); AU.setPreservesAll(); MachineFunctionPass::getAnalysisUsage(AU); @@ -3741,7 +3741,7 @@ char &llvm::MachineBlockPlacementStatsID = MachineBlockPlacementStats::ID; INITIALIZE_PASS_BEGIN(MachineBlockPlacementStats, "block-placement-stats", "Basic Block Placement Stats", false, false) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo) INITIALIZE_PASS_END(MachineBlockPlacementStats, "block-placement-stats", "Basic Block Placement Stats", false, false) @@ -3754,7 +3754,7 @@ bool MachineBlockPlacementStats::runOnMachineFunction(MachineFunction &F) { if (!isFunctionInPrintList(F.getName())) return false; - MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); for (MachineBasicBlock &MBB : F) { diff --git a/llvm/lib/CodeGen/MachineBranchProbabilityInfo.cpp b/llvm/lib/CodeGen/MachineBranchProbabilityInfo.cpp index a84377d..56fffff 100644 --- a/llvm/lib/CodeGen/MachineBranchProbabilityInfo.cpp +++ b/llvm/lib/CodeGen/MachineBranchProbabilityInfo.cpp @@ -18,9 +18,11 @@ using namespace llvm; -INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob", +INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfoWrapperPass, + "machine-branch-prob", "Machine Branch Probability Analysis", false, true) -INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob", +INITIALIZE_PASS_END(MachineBranchProbabilityInfoWrapperPass, + "machine-branch-prob", "Machine Branch Probability Analysis", false, true) namespace llvm { @@ -37,15 +39,45 @@ cl::opt<unsigned> ProfileLikelyProb( cl::init(51), cl::Hidden); } // namespace llvm -char MachineBranchProbabilityInfo::ID = 0; +MachineBranchProbabilityAnalysis::Result +MachineBranchProbabilityAnalysis::run(MachineFunction &, + MachineFunctionAnalysisManager &) { + return MachineBranchProbabilityInfo(); +} + +PreservedAnalyses +MachineBranchProbabilityPrinterPass::run(MachineFunction &MF, + MachineFunctionAnalysisManager &MFAM) { + OS << "Printing analysis 'Machine Branch Probability Analysis' for machine " + "function '" + << MF.getName() << "':\n"; + auto &MBPI = MFAM.getResult<MachineBranchProbabilityAnalysis>(MF); + for (const MachineBasicBlock &MBB : MF) { + for (const MachineBasicBlock *Succ : MBB.successors()) + MBPI.printEdgeProbability(OS << " ", &MBB, Succ); + } + return PreservedAnalyses::all(); +} + +char MachineBranchProbabilityInfoWrapperPass::ID = 0; -MachineBranchProbabilityInfo::MachineBranchProbabilityInfo() +MachineBranchProbabilityInfoWrapperPass:: + MachineBranchProbabilityInfoWrapperPass() : ImmutablePass(ID) { PassRegistry &Registry = *PassRegistry::getPassRegistry(); - initializeMachineBranchProbabilityInfoPass(Registry); + initializeMachineBranchProbabilityInfoWrapperPassPass(Registry); } -void MachineBranchProbabilityInfo::anchor() {} +void MachineBranchProbabilityInfoWrapperPass::anchor() {} + +AnalysisKey MachineBranchProbabilityAnalysis::Key; + +bool MachineBranchProbabilityInfo::invalidate( + MachineFunction &, const PreservedAnalyses &PA, + MachineFunctionAnalysisManager::Invalidator &) { + auto PAC = PA.getChecker<MachineBranchProbabilityAnalysis>(); + return !PAC.preservedWhenStateless(); +} BranchProbability MachineBranchProbabilityInfo::getEdgeProbability( const MachineBasicBlock *Src, diff --git a/llvm/lib/CodeGen/MachineSink.cpp b/llvm/lib/CodeGen/MachineSink.cpp index 76947a4..4dabaab 100644 --- a/llvm/lib/CodeGen/MachineSink.cpp +++ b/llvm/lib/CodeGen/MachineSink.cpp @@ -187,7 +187,7 @@ namespace { AU.addRequired<MachineDominatorTreeWrapperPass>(); AU.addRequired<MachinePostDominatorTreeWrapperPass>(); AU.addRequired<MachineCycleInfoWrapperPass>(); - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addPreserved<MachineCycleInfoWrapperPass>(); AU.addPreserved<MachineLoopInfo>(); if (UseBlockFreqInfo) @@ -273,7 +273,7 @@ char &llvm::MachineSinkingID = MachineSinking::ID; INITIALIZE_PASS_BEGIN(MachineSinking, DEBUG_TYPE, "Machine code sinking", false, false) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineCycleInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) @@ -712,7 +712,7 @@ bool MachineSinking::runOnMachineFunction(MachineFunction &MF) { PDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree(); CI = &getAnalysis<MachineCycleInfoWrapperPass>().getCycleInfo(); MBFI = UseBlockFreqInfo ? &getAnalysis<MachineBlockFrequencyInfo>() : nullptr; - MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); RegClassInfo.runOnMachineFunction(MF); TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>(); diff --git a/llvm/lib/CodeGen/MachineTraceMetrics.cpp b/llvm/lib/CodeGen/MachineTraceMetrics.cpp index 3892f85..0f44777 100644 --- a/llvm/lib/CodeGen/MachineTraceMetrics.cpp +++ b/llvm/lib/CodeGen/MachineTraceMetrics.cpp @@ -46,7 +46,7 @@ char &llvm::MachineTraceMetricsID = MachineTraceMetrics::ID; INITIALIZE_PASS_BEGIN(MachineTraceMetrics, DEBUG_TYPE, "Machine Trace Metrics", false, true) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) INITIALIZE_PASS_END(MachineTraceMetrics, DEBUG_TYPE, "Machine Trace Metrics", false, true) @@ -57,7 +57,7 @@ MachineTraceMetrics::MachineTraceMetrics() : MachineFunctionPass(ID) { void MachineTraceMetrics::getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineLoopInfo>(); MachineFunctionPass::getAnalysisUsage(AU); } diff --git a/llvm/lib/CodeGen/TailDuplication.cpp b/llvm/lib/CodeGen/TailDuplication.cpp index bf3d208..25f20d9 100644 --- a/llvm/lib/CodeGen/TailDuplication.cpp +++ b/llvm/lib/CodeGen/TailDuplication.cpp @@ -40,7 +40,7 @@ public: bool runOnMachineFunction(MachineFunction &MF) override; void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<LazyMachineBlockFrequencyInfoPass>(); AU.addRequired<ProfileSummaryInfoWrapperPass>(); MachineFunctionPass::getAnalysisUsage(AU); @@ -84,7 +84,7 @@ bool TailDuplicateBase::runOnMachineFunction(MachineFunction &MF) { if (skipFunction(MF.getFunction())) return false; - auto MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + auto MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); auto *MBFI = (PSI && PSI->hasProfileSummary()) ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() : diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index 8d2a64d..7d36ead3 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -93,6 +93,7 @@ #include "llvm/CodeGen/LocalStackSlotAllocation.h" #include "llvm/CodeGen/LowerEmuTLS.h" #include "llvm/CodeGen/MIRPrinter.h" +#include "llvm/CodeGen/MachineBranchProbabilityInfo.h" #include "llvm/CodeGen/MachineDominators.h" #include "llvm/CodeGen/MachineFunctionAnalysis.h" #include "llvm/CodeGen/MachinePassManager.h" diff --git a/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp b/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp index 9a78812..3c2201e 100644 --- a/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp +++ b/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp @@ -794,7 +794,7 @@ char AArch64ConditionalCompares::ID = 0; INITIALIZE_PASS_BEGIN(AArch64ConditionalCompares, "aarch64-ccmp", "AArch64 CCMP Pass", false, false) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics) INITIALIZE_PASS_END(AArch64ConditionalCompares, "aarch64-ccmp", @@ -805,7 +805,7 @@ FunctionPass *llvm::createAArch64ConditionalCompares() { } void AArch64ConditionalCompares::getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineDominatorTreeWrapperPass>(); AU.addPreserved<MachineDominatorTreeWrapperPass>(); AU.addRequired<MachineLoopInfo>(); @@ -935,7 +935,7 @@ bool AArch64ConditionalCompares::runOnMachineFunction(MachineFunction &MF) { MRI = &MF.getRegInfo(); DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); Loops = &getAnalysis<MachineLoopInfo>(); - MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); Traces = &getAnalysis<MachineTraceMetrics>(); MinInstr = nullptr; MinSize = MF.getFunction().hasMinSize(); diff --git a/llvm/lib/Target/Hexagon/HexagonEarlyIfConv.cpp b/llvm/lib/Target/Hexagon/HexagonEarlyIfConv.cpp index 03f6882..e99496d 100644 --- a/llvm/lib/Target/Hexagon/HexagonEarlyIfConv.cpp +++ b/llvm/lib/Target/Hexagon/HexagonEarlyIfConv.cpp @@ -161,7 +161,7 @@ namespace { } void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineDominatorTreeWrapperPass>(); AU.addPreserved<MachineDominatorTreeWrapperPass>(); AU.addRequired<MachineLoopInfo>(); @@ -1056,8 +1056,9 @@ bool HexagonEarlyIfConversion::runOnMachineFunction(MachineFunction &MF) { MRI = &MF.getRegInfo(); MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); MLI = &getAnalysis<MachineLoopInfo>(); - MBPI = EnableHexagonBP ? &getAnalysis<MachineBranchProbabilityInfo>() : - nullptr; + MBPI = EnableHexagonBP + ? &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI() + : nullptr; Deleted.clear(); bool Changed = false; diff --git a/llvm/lib/Target/Hexagon/HexagonLoopAlign.cpp b/llvm/lib/Target/Hexagon/HexagonLoopAlign.cpp index c79b528..6aa95ff 100644 --- a/llvm/lib/Target/Hexagon/HexagonLoopAlign.cpp +++ b/llvm/lib/Target/Hexagon/HexagonLoopAlign.cpp @@ -74,7 +74,7 @@ public: bool attemptToBalignSmallLoop(MachineFunction &MF, MachineBasicBlock &MBB); void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineBlockFrequencyInfo>(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -148,7 +148,7 @@ bool HexagonLoopAlign::attemptToBalignSmallLoop(MachineFunction &MF, return false; const MachineBranchProbabilityInfo *MBPI = - &getAnalysis<MachineBranchProbabilityInfo>(); + &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); const MachineBlockFrequencyInfo *MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); diff --git a/llvm/lib/Target/Hexagon/HexagonNewValueJump.cpp b/llvm/lib/Target/Hexagon/HexagonNewValueJump.cpp index f539717..36c52df 100644 --- a/llvm/lib/Target/Hexagon/HexagonNewValueJump.cpp +++ b/llvm/lib/Target/Hexagon/HexagonNewValueJump.cpp @@ -78,7 +78,7 @@ namespace { HexagonNewValueJump() : MachineFunctionPass(ID) {} void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -107,7 +107,7 @@ char HexagonNewValueJump::ID = 0; INITIALIZE_PASS_BEGIN(HexagonNewValueJump, "hexagon-nvj", "Hexagon NewValueJump", false, false) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_END(HexagonNewValueJump, "hexagon-nvj", "Hexagon NewValueJump", false, false) @@ -459,7 +459,7 @@ bool HexagonNewValueJump::runOnMachineFunction(MachineFunction &MF) { QII = static_cast<const HexagonInstrInfo *>(MF.getSubtarget().getInstrInfo()); QRI = static_cast<const HexagonRegisterInfo *>( MF.getSubtarget().getRegisterInfo()); - MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); if (DisableNewValueJumps || !MF.getSubtarget<HexagonSubtarget>().useNewValueJumps()) diff --git a/llvm/lib/Target/Hexagon/HexagonVLIWPacketizer.cpp b/llvm/lib/Target/Hexagon/HexagonVLIWPacketizer.cpp index 2d5352b..ef2677a 100644 --- a/llvm/lib/Target/Hexagon/HexagonVLIWPacketizer.cpp +++ b/llvm/lib/Target/Hexagon/HexagonVLIWPacketizer.cpp @@ -96,7 +96,7 @@ namespace { void getAnalysisUsage(AnalysisUsage &AU) const override { AU.setPreservesCFG(); AU.addRequired<AAResultsWrapperPass>(); - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineDominatorTreeWrapperPass>(); AU.addRequired<MachineLoopInfo>(); AU.addPreserved<MachineDominatorTreeWrapperPass>(); @@ -125,7 +125,7 @@ char HexagonPacketizer::ID = 0; INITIALIZE_PASS_BEGIN(HexagonPacketizer, "hexagon-packetizer", "Hexagon Packetizer", false, false) INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) -INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) INITIALIZE_PASS_END(HexagonPacketizer, "hexagon-packetizer", @@ -213,7 +213,8 @@ bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) { HRI = HST.getRegisterInfo(); auto &MLI = getAnalysis<MachineLoopInfo>(); auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); - auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + auto *MBPI = + &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); if (EnableGenAllInsnClass) HII->genAllInsnTimingClasses(MF); diff --git a/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp b/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp index b525606..4ec01ab 100644 --- a/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp +++ b/llvm/lib/Target/Mips/MipsDelaySlotFiller.cpp @@ -236,7 +236,7 @@ namespace { } void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -872,7 +872,7 @@ MipsDelaySlotFiller::selectSuccBB(MachineBasicBlock &B) const { return nullptr; // Select the successor with the larget edge weight. - auto &Prob = getAnalysis<MachineBranchProbabilityInfo>(); + auto &Prob = getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); MachineBasicBlock *S = *std::max_element( B.succ_begin(), B.succ_end(), [&](const MachineBasicBlock *Dst0, const MachineBasicBlock *Dst1) { diff --git a/llvm/lib/Target/PowerPC/PPCReduceCRLogicals.cpp b/llvm/lib/Target/PowerPC/PPCReduceCRLogicals.cpp index d1cc2ad..0bfcba9 100644 --- a/llvm/lib/Target/PowerPC/PPCReduceCRLogicals.cpp +++ b/llvm/lib/Target/PowerPC/PPCReduceCRLogicals.cpp @@ -426,7 +426,7 @@ public: } CRLogicalOpInfo createCRLogicalOpInfo(MachineInstr &MI); void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.addRequired<MachineBranchProbabilityInfo>(); + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); AU.addRequired<MachineDominatorTreeWrapperPass>(); MachineFunctionPass::getAnalysisUsage(AU); } @@ -570,7 +570,7 @@ void PPCReduceCRLogicals::initialize(MachineFunction &MFParam) { MF = &MFParam; MRI = &MF->getRegInfo(); TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo(); - MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); AllCRLogicalOps.clear(); } diff --git a/llvm/test/tools/llc/new-pm/machine-branch-prob.ll b/llvm/test/tools/llc/new-pm/machine-branch-prob.ll new file mode 100644 index 0000000..8854c85 --- /dev/null +++ b/llvm/test/tools/llc/new-pm/machine-branch-prob.ll @@ -0,0 +1,35 @@ +; RUN: llc -mtriple=x86_64-linux-gnu %s -stop-after=finalize-isel -o - | \ +; RUN: llc -mtriple=x86_64-linux-gnu -passes='print<machine-branch-prob>' -x mir -filetype=null 2>&1 | \ +; RUN: FileCheck %s + +declare void @foo() + +define i32 @test2(i32 %x) nounwind uwtable readnone ssp { +entry: + %conv = sext i32 %x to i64 + switch i64 %conv, label %return [ + i64 0, label %sw.bb + i64 1, label %sw.bb + i64 4, label %sw.bb + i64 5, label %sw.bb1 + i64 15, label %sw.bb + ], !prof !0 + +sw.bb: +; this call will prevent simplifyCFG from optimizing the block away in ARM/AArch64. + tail call void @foo() + br label %return + +sw.bb1: + br label %return + +return: + %retval.0 = phi i32 [ 5, %sw.bb1 ], [ 1, %sw.bb ], [ 0, %entry ] + ret i32 %retval.0 +} + +!0 = !{!"branch_weights", i32 7, i32 6, i32 4, i32 4, i32 64, i21 1000} + +; CHECK: Printing analysis 'Machine Branch Probability Analysis' for machine function 'test2': +; CHECK: edge %bb.4 -> %bb.6 probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] +; CHECK: edge %bb.5 -> %bb.6 probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] |