[llvm] [CGP] Favour LazyBFI/LazyBPI for legacy PM, drop unused BPI, update style (NFC) (PR #189063)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 2 07:05:39 PDT 2026
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/189063
>From 3f58ce5b65c89a6e02ef87579b61733c3317801c Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Fri, 27 Mar 2026 17:50:50 +0100
Subject: [PATCH] [CGP] Favour LazyBFI/LazyBPI for legacy PM, drop unused BPI,
update style (NFC)
Prefer using `LazyBlockFrequencyInfoPass` in the legacy PM, thus avoid
the construction of PDT. Minor opportunity to modernize code (dropped
unneeded `run` layer) and update style where possible.
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 63 +++++++++------------
llvm/test/CodeGen/AArch64/O3-pipeline.ll | 5 +-
llvm/test/CodeGen/AMDGPU/llc-pipeline.ll | 20 +++----
llvm/test/CodeGen/ARM/O3-pipeline.ll | 5 +-
llvm/test/CodeGen/LoongArch/opt-pipeline.ll | 5 +-
llvm/test/CodeGen/PowerPC/O3-pipeline.ll | 5 +-
llvm/test/CodeGen/RISCV/O3-pipeline.ll | 5 +-
llvm/test/CodeGen/SPIRV/llc-pipeline.ll | 5 +-
llvm/test/CodeGen/X86/opt-pipeline.ll | 5 +-
9 files changed, 49 insertions(+), 69 deletions(-)
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 56ee6e8b43304..6f25e823940b6 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -22,10 +22,9 @@
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
-#include "llvm/Analysis/BlockFrequencyInfo.h"
-#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/FloatingPointPredicateUtils.h"
#include "llvm/Analysis/InstructionSimplify.h"
+#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
@@ -305,6 +304,7 @@ class TypePromotionTransaction;
class CodeGenPrepare {
friend class CodeGenPrepareLegacyPass;
+ friend class llvm::CodeGenPreparePass;
const TargetMachine *TM = nullptr;
const TargetSubtargetInfo *SubtargetInfo = nullptr;
const TargetLowering *TLI = nullptr;
@@ -313,8 +313,7 @@ class CodeGenPrepare {
const BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
const TargetLibraryInfo *TLInfo = nullptr;
LoopInfo *LI = nullptr;
- BlockFrequencyInfo *BFI;
- BranchProbabilityInfo *BPI;
+ BlockFrequencyInfo *BFI = nullptr;
ProfileSummaryInfo *PSI = nullptr;
/// As we scan instructions optimizing them, this is the next instruction
@@ -386,18 +385,16 @@ class CodeGenPrepare {
FreshBBs.clear();
}
- bool run(Function &F, FunctionAnalysisManager &AM);
-
private:
- template <typename F>
- void resetIteratorIfInvalidatedWhileCalling(BasicBlock *BB, F f) {
+ template <typename CallableT>
+ void resetIteratorIfInvalidatedWhileCalling(BasicBlock *BB, CallableT &&Fn) {
// Substituting can cause recursive simplifications, which can invalidate
// our iterator. Use a WeakTrackingVH to hold onto it in case this
// happens.
Value *CurValue = &*CurInstIterator;
WeakTrackingVH IterHandle(CurValue);
- f();
+ Fn();
// If the iterator instruction was recursively deleted, start over at the
// start of the block.
@@ -476,7 +473,7 @@ class CodeGenPrepare {
bool combineToUAddWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
bool unfoldPowerOf2Test(CmpInst *Cmp);
void verifyBFIUpdates(Function &F);
- bool _run(Function &F);
+ bool runImpl(Function &F);
};
class CodeGenPrepareLegacyPass : public FunctionPass {
@@ -496,8 +493,7 @@ class CodeGenPrepareLegacyPass : public FunctionPass {
AU.addRequired<TargetPassConfig>();
AU.addRequired<TargetTransformInfoWrapperPass>();
AU.addRequired<LoopInfoWrapperPass>();
- AU.addRequired<BranchProbabilityInfoWrapperPass>();
- AU.addRequired<BlockFrequencyInfoWrapperPass>();
+ LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU);
AU.addUsedIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
}
};
@@ -518,19 +514,19 @@ bool CodeGenPrepareLegacyPass::runOnFunction(Function &F) {
CGP.TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
CGP.TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
CGP.LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
- CGP.BPI = &getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
- CGP.BFI = &getAnalysis<BlockFrequencyInfoWrapperPass>().getBFI();
CGP.PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
+ CGP.BFI = &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI();
auto BBSPRWP =
getAnalysisIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
CGP.BBSectionsProfileReader = BBSPRWP ? &BBSPRWP->getBBSPR() : nullptr;
- return CGP._run(F);
+ return CGP.runImpl(F);
}
INITIALIZE_PASS_BEGIN(CodeGenPrepareLegacyPass, DEBUG_TYPE,
"Optimize for code generation", false, false)
INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReaderWrapperPass)
+INITIALIZE_PASS_DEPENDENCY(LazyBlockFrequencyInfoPass)
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
@@ -546,8 +542,20 @@ FunctionPass *llvm::createCodeGenPrepareLegacyPass() {
PreservedAnalyses CodeGenPreparePass::run(Function &F,
FunctionAnalysisManager &AM) {
CodeGenPrepare CGP(TM);
+ CGP.DL = &F.getDataLayout();
+ CGP.SubtargetInfo = TM->getSubtargetImpl(F);
+ CGP.TLI = CGP.SubtargetInfo->getTargetLowering();
+ CGP.TRI = CGP.SubtargetInfo->getRegisterInfo();
+ CGP.TLInfo = &AM.getResult<TargetLibraryAnalysis>(F);
+ CGP.TTI = &AM.getResult<TargetIRAnalysis>(F);
+ CGP.LI = &AM.getResult<LoopAnalysis>(F);
+ auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
+ CGP.PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
+ CGP.BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
+ CGP.BBSectionsProfileReader =
+ AM.getCachedResult<BasicBlockSectionsProfileReaderAnalysis>(F);
- bool Changed = CGP.run(F, AM);
+ bool Changed = CGP.runImpl(F);
if (!Changed)
return PreservedAnalyses::all();
@@ -557,24 +565,7 @@ PreservedAnalyses CodeGenPreparePass::run(Function &F,
return PA;
}
-bool CodeGenPrepare::run(Function &F, FunctionAnalysisManager &AM) {
- DL = &F.getDataLayout();
- SubtargetInfo = TM->getSubtargetImpl(F);
- TLI = SubtargetInfo->getTargetLowering();
- TRI = SubtargetInfo->getRegisterInfo();
- TLInfo = &AM.getResult<TargetLibraryAnalysis>(F);
- TTI = &AM.getResult<TargetIRAnalysis>(F);
- LI = &AM.getResult<LoopAnalysis>(F);
- BPI = &AM.getResult<BranchProbabilityAnalysis>(F);
- BFI = &AM.getResult<BlockFrequencyAnalysis>(F);
- auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
- PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
- BBSectionsProfileReader =
- AM.getCachedResult<BasicBlockSectionsProfileReaderAnalysis>(F);
- return _run(F);
-}
-
-bool CodeGenPrepare::_run(Function &F) {
+bool CodeGenPrepare::runImpl(Function &F) {
bool EverMadeChange = false;
OptSize = F.hasOptSize();
@@ -593,8 +584,8 @@ bool CodeGenPrepare::_run(Function &F) {
// If PSI shows this function is not hot, we will placed the function
// into unlikely section if (1) PSI shows this is a cold function, or
// (2) the function has a attribute of cold.
- else if (PSI->isFunctionColdInCallGraph(&F, *BFI) ||
- F.hasFnAttribute(Attribute::Cold))
+ else if (F.hasFnAttribute(Attribute::Cold) ||
+ PSI->isFunctionColdInCallGraph(&F, *BFI))
(void)F.setSectionPrefix("unlikely");
else if (ProfileUnknownInSpecialSection && PSI->hasPartialSampleProfile() &&
PSI->isFunctionHotnessUnknown(F))
diff --git a/llvm/test/CodeGen/AArch64/O3-pipeline.ll b/llvm/test/CodeGen/AArch64/O3-pipeline.ll
index 374b0fe10c005..e4b9de0b409f7 100644
--- a/llvm/test/CodeGen/AArch64/O3-pipeline.ll
+++ b/llvm/test/CodeGen/AArch64/O3-pipeline.ll
@@ -103,9 +103,8 @@
; CHECK-NEXT: Interleaved Access Pass
; CHECK-NEXT: Natural Loop Information
; CHECK-NEXT: Type Promotion
-; CHECK-NEXT: Post-Dominator Tree Construction
-; CHECK-NEXT: Branch Probability Analysis
-; CHECK-NEXT: Block Frequency Analysis
+; CHECK-NEXT: Lazy Branch Probability Analysis
+; CHECK-NEXT: Lazy Block Frequency Analysis
; CHECK-NEXT: CodeGen Prepare
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Exception handling preparation
diff --git a/llvm/test/CodeGen/AMDGPU/llc-pipeline.ll b/llvm/test/CodeGen/AMDGPU/llc-pipeline.ll
index c41a43bf9cf48..341cf79f80471 100644
--- a/llvm/test/CodeGen/AMDGPU/llc-pipeline.ll
+++ b/llvm/test/CodeGen/AMDGPU/llc-pipeline.ll
@@ -251,9 +251,8 @@
; GCN-O1-NEXT: Dominator Tree Construction
; GCN-O1-NEXT: AMDGPU Lower Kernel Arguments
; GCN-O1-NEXT: Natural Loop Information
-; GCN-O1-NEXT: Post-Dominator Tree Construction
-; GCN-O1-NEXT: Branch Probability Analysis
-; GCN-O1-NEXT: Block Frequency Analysis
+; GCN-O1-NEXT: Lazy Branch Probability Analysis
+; GCN-O1-NEXT: Lazy Block Frequency Analysis
; GCN-O1-NEXT: CodeGen Prepare
; GCN-O1-NEXT: Lower buffer fat pointer operations to buffer resources
; GCN-O1-NEXT: AMDGPU lower intrinsics
@@ -562,9 +561,8 @@
; GCN-O1-OPTS-NEXT: Dominator Tree Construction
; GCN-O1-OPTS-NEXT: AMDGPU Lower Kernel Arguments
; GCN-O1-OPTS-NEXT: Natural Loop Information
-; GCN-O1-OPTS-NEXT: Post-Dominator Tree Construction
-; GCN-O1-OPTS-NEXT: Branch Probability Analysis
-; GCN-O1-OPTS-NEXT: Block Frequency Analysis
+; GCN-O1-OPTS-NEXT: Lazy Branch Probability Analysis
+; GCN-O1-OPTS-NEXT: Lazy Block Frequency Analysis
; GCN-O1-OPTS-NEXT: CodeGen Prepare
; GCN-O1-OPTS-NEXT: Dominator Tree Construction
; GCN-O1-OPTS-NEXT: Basic Alias Analysis (stateless AA impl)
@@ -889,9 +887,8 @@
; GCN-O2-NEXT: Dominator Tree Construction
; GCN-O2-NEXT: AMDGPU Lower Kernel Arguments
; GCN-O2-NEXT: Natural Loop Information
-; GCN-O2-NEXT: Post-Dominator Tree Construction
-; GCN-O2-NEXT: Branch Probability Analysis
-; GCN-O2-NEXT: Block Frequency Analysis
+; GCN-O2-NEXT: Lazy Branch Probability Analysis
+; GCN-O2-NEXT: Lazy Block Frequency Analysis
; GCN-O2-NEXT: CodeGen Prepare
; GCN-O2-NEXT: Dominator Tree Construction
; GCN-O2-NEXT: Basic Alias Analysis (stateless AA impl)
@@ -1231,9 +1228,8 @@
; GCN-O3-NEXT: Dominator Tree Construction
; GCN-O3-NEXT: AMDGPU Lower Kernel Arguments
; GCN-O3-NEXT: Natural Loop Information
-; GCN-O3-NEXT: Post-Dominator Tree Construction
-; GCN-O3-NEXT: Branch Probability Analysis
-; GCN-O3-NEXT: Block Frequency Analysis
+; GCN-O3-NEXT: Lazy Branch Probability Analysis
+; GCN-O3-NEXT: Lazy Block Frequency Analysis
; GCN-O3-NEXT: CodeGen Prepare
; GCN-O3-NEXT: Dominator Tree Construction
; GCN-O3-NEXT: Basic Alias Analysis (stateless AA impl)
diff --git a/llvm/test/CodeGen/ARM/O3-pipeline.ll b/llvm/test/CodeGen/ARM/O3-pipeline.ll
index c88e96d81ae65..95b611e91f486 100644
--- a/llvm/test/CodeGen/ARM/O3-pipeline.ll
+++ b/llvm/test/CodeGen/ARM/O3-pipeline.ll
@@ -56,9 +56,8 @@
; CHECK-NEXT: Complex Deinterleaving Pass
; CHECK-NEXT: Interleaved Access Pass
; CHECK-NEXT: Type Promotion
-; CHECK-NEXT: Post-Dominator Tree Construction
-; CHECK-NEXT: Branch Probability Analysis
-; CHECK-NEXT: Block Frequency Analysis
+; CHECK-NEXT: Lazy Branch Probability Analysis
+; CHECK-NEXT: Lazy Block Frequency Analysis
; CHECK-NEXT: CodeGen Prepare
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Exception handling preparation
diff --git a/llvm/test/CodeGen/LoongArch/opt-pipeline.ll b/llvm/test/CodeGen/LoongArch/opt-pipeline.ll
index 29a035f2949de..0a6adb36bca45 100644
--- a/llvm/test/CodeGen/LoongArch/opt-pipeline.ll
+++ b/llvm/test/CodeGen/LoongArch/opt-pipeline.ll
@@ -75,9 +75,8 @@
; LAXX-NEXT: Expand reduction intrinsics
; LAXX-NEXT: Natural Loop Information
; LAXX-NEXT: Type Promotion
-; LAXX-NEXT: Post-Dominator Tree Construction
-; LAXX-NEXT: Branch Probability Analysis
-; LAXX-NEXT: Block Frequency Analysis
+; LAXX-NEXT: Lazy Branch Probability Analysis
+; LAXX-NEXT: Lazy Block Frequency Analysis
; LAXX-NEXT: CodeGen Prepare
; LAXX-NEXT: Dominator Tree Construction
; LAXX-NEXT: Exception handling preparation
diff --git a/llvm/test/CodeGen/PowerPC/O3-pipeline.ll b/llvm/test/CodeGen/PowerPC/O3-pipeline.ll
index 7118ae453245d..a95c357eb9427 100644
--- a/llvm/test/CodeGen/PowerPC/O3-pipeline.ll
+++ b/llvm/test/CodeGen/PowerPC/O3-pipeline.ll
@@ -75,9 +75,8 @@
; CHECK-NEXT: Scalarize Masked Memory Intrinsics
; CHECK-NEXT: Expand reduction intrinsics
; CHECK-NEXT: Natural Loop Information
-; CHECK-NEXT: Post-Dominator Tree Construction
-; CHECK-NEXT: Branch Probability Analysis
-; CHECK-NEXT: Block Frequency Analysis
+; CHECK-NEXT: Lazy Branch Probability Analysis
+; CHECK-NEXT: Lazy Block Frequency Analysis
; CHECK-NEXT: CodeGen Prepare
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Exception handling preparation
diff --git a/llvm/test/CodeGen/RISCV/O3-pipeline.ll b/llvm/test/CodeGen/RISCV/O3-pipeline.ll
index 682437b1e0db7..3aa7aebe131ad 100644
--- a/llvm/test/CodeGen/RISCV/O3-pipeline.ll
+++ b/llvm/test/CodeGen/RISCV/O3-pipeline.ll
@@ -87,9 +87,8 @@
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Natural Loop Information
; CHECK-NEXT: Type Promotion
-; CHECK-NEXT: Post-Dominator Tree Construction
-; CHECK-NEXT: Branch Probability Analysis
-; CHECK-NEXT: Block Frequency Analysis
+; CHECK-NEXT: Lazy Branch Probability Analysis
+; CHECK-NEXT: Lazy Block Frequency Analysis
; CHECK-NEXT: CodeGen Prepare
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Exception handling preparation
diff --git a/llvm/test/CodeGen/SPIRV/llc-pipeline.ll b/llvm/test/CodeGen/SPIRV/llc-pipeline.ll
index a22298c18abe5..d318c9347a250 100644
--- a/llvm/test/CodeGen/SPIRV/llc-pipeline.ll
+++ b/llvm/test/CodeGen/SPIRV/llc-pipeline.ll
@@ -152,9 +152,8 @@
; SPIRV-Opt-NEXT: FunctionPass Manager
; SPIRV-Opt-NEXT: Dominator Tree Construction
; SPIRV-Opt-NEXT: Natural Loop Information
-; SPIRV-Opt-NEXT: Post-Dominator Tree Construction
-; SPIRV-Opt-NEXT: Branch Probability Analysis
-; SPIRV-Opt-NEXT: Block Frequency Analysis
+; SPIRV-Opt-NEXT: Lazy Branch Probability Analysis
+; SPIRV-Opt-NEXT: Lazy Block Frequency Analysis
; SPIRV-Opt-NEXT: CodeGen Prepare
; SPIRV-Opt-NEXT: Lower invoke and unwind, for unwindless code generators
; SPIRV-Opt-NEXT: Remove unreachable blocks from the CFG
diff --git a/llvm/test/CodeGen/X86/opt-pipeline.ll b/llvm/test/CodeGen/X86/opt-pipeline.ll
index 81c2f62b1d3dc..48c1ac4e07c49 100644
--- a/llvm/test/CodeGen/X86/opt-pipeline.ll
+++ b/llvm/test/CodeGen/X86/opt-pipeline.ll
@@ -75,9 +75,8 @@
; CHECK-NEXT: X86 Partial Reduction
; CHECK-NEXT: Expand indirectbr instructions
; CHECK-NEXT: Natural Loop Information
-; CHECK-NEXT: Post-Dominator Tree Construction
-; CHECK-NEXT: Branch Probability Analysis
-; CHECK-NEXT: Block Frequency Analysis
+; CHECK-NEXT: Lazy Branch Probability Analysis
+; CHECK-NEXT: Lazy Block Frequency Analysis
; CHECK-NEXT: CodeGen Prepare
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Exception handling preparation
More information about the llvm-commits
mailing list