Skip to content

Commit d8503a3

Browse files
authored
[InstCombine] Update BranchProbabilityAnalysis cache result (#86470)
InstCombine may invert branch condition and profile metadata. In such case, BranchProbabilityAnalysis should also be updated.
1 parent 60baaf1 commit d8503a3

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
7676
SimplifyQuery SQ;
7777
OptimizationRemarkEmitter &ORE;
7878
BlockFrequencyInfo *BFI;
79+
BranchProbabilityInfo *BPI;
7980
ProfileSummaryInfo *PSI;
8081
DomConditionCache DC;
8182

@@ -96,13 +97,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
9697
bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
9798
TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
9899
DominatorTree &DT, OptimizationRemarkEmitter &ORE,
99-
BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
100-
const DataLayout &DL, LoopInfo *LI)
100+
BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI,
101+
ProfileSummaryInfo *PSI, const DataLayout &DL, LoopInfo *LI)
101102
: TTI(TTI), Builder(Builder), Worklist(Worklist),
102103
MinimizeSize(MinimizeSize), AA(AA), AC(AC), TLI(TLI), DT(DT), DL(DL),
103104
SQ(DL, &TLI, &DT, &AC, nullptr, /*UseInstrInfo*/ true,
104105
/*CanUseUndef*/ true, &DC),
105-
ORE(ORE), BFI(BFI), PSI(PSI), LI(LI) {}
106+
ORE(ORE), BFI(BFI), BPI(BPI), PSI(PSI), LI(LI) {}
106107

107108
virtual ~InstCombiner() = default;
108109

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
6767
bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
6868
TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
6969
DominatorTree &DT, OptimizationRemarkEmitter &ORE,
70-
BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
71-
const DataLayout &DL, LoopInfo *LI)
70+
BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI,
71+
ProfileSummaryInfo *PSI, const DataLayout &DL, LoopInfo *LI)
7272
: InstCombiner(Worklist, Builder, MinimizeSize, AA, AC, TLI, TTI, DT, ORE,
73-
BFI, PSI, DL, LI) {}
73+
BFI, BPI, PSI, DL, LI) {}
7474

7575
virtual ~InstCombinerImpl() = default;
7676

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,9 +1347,13 @@ void InstCombinerImpl::freelyInvertAllUsersOf(Value *I, Value *IgnoredUser) {
13471347
SI->swapProfMetadata();
13481348
break;
13491349
}
1350-
case Instruction::Br:
1351-
cast<BranchInst>(U)->swapSuccessors(); // swaps prof metadata too
1350+
case Instruction::Br: {
1351+
BranchInst *BI = cast<BranchInst>(U);
1352+
BI->swapSuccessors(); // swaps prof metadata too
1353+
if (BPI)
1354+
BPI->swapSuccEdgesProbabilities(BI->getParent());
13521355
break;
1356+
}
13531357
case Instruction::Xor:
13541358
replaceInstUsesWith(cast<Instruction>(*U), I);
13551359
// Add to worklist for DCE.
@@ -3525,6 +3529,8 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
35253529
if (match(Cond, m_Not(m_Value(X))) && !isa<Constant>(X)) {
35263530
// Swap Destinations and condition...
35273531
BI.swapSuccessors();
3532+
if (BPI)
3533+
BPI->swapSuccEdgesProbabilities(BI.getParent());
35283534
return replaceOperand(BI, 0, X);
35293535
}
35303536

@@ -3538,6 +3544,8 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
35383544
Value *NotX = Builder.CreateNot(X, "not." + X->getName());
35393545
Value *Or = Builder.CreateLogicalOr(NotX, Y);
35403546
BI.swapSuccessors();
3547+
if (BPI)
3548+
BPI->swapSuccEdgesProbabilities(BI.getParent());
35413549
return replaceOperand(BI, 0, Or);
35423550
}
35433551

@@ -3554,6 +3562,8 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
35543562
auto *Cmp = cast<CmpInst>(Cond);
35553563
Cmp->setPredicate(CmpInst::getInversePredicate(Pred));
35563564
BI.swapSuccessors();
3565+
if (BPI)
3566+
BPI->swapSuccEdgesProbabilities(BI.getParent());
35573567
Worklist.push(Cmp);
35583568
return &BI;
35593569
}
@@ -5248,7 +5258,8 @@ static bool combineInstructionsOverFunction(
52485258
Function &F, InstructionWorklist &Worklist, AliasAnalysis *AA,
52495259
AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
52505260
DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
5251-
ProfileSummaryInfo *PSI, LoopInfo *LI, const InstCombineOptions &Opts) {
5261+
BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI, LoopInfo *LI,
5262+
const InstCombineOptions &Opts) {
52525263
auto &DL = F.getParent()->getDataLayout();
52535264

52545265
/// Builder - This is an IRBuilder that automatically inserts new
@@ -5286,7 +5297,7 @@ static bool combineInstructionsOverFunction(
52865297
<< F.getName() << "\n");
52875298

52885299
InstCombinerImpl IC(Worklist, Builder, F.hasMinSize(), AA, AC, TLI, TTI, DT,
5289-
ORE, BFI, PSI, DL, LI);
5300+
ORE, BFI, BPI, PSI, DL, LI);
52905301
IC.MaxArraySizeForCombine = MaxArraySize;
52915302
bool MadeChangeInThisIteration = IC.prepareWorklist(F, RPOT);
52925303
MadeChangeInThisIteration |= IC.run();
@@ -5347,9 +5358,10 @@ PreservedAnalyses InstCombinePass::run(Function &F,
53475358
MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
53485359
auto *BFI = (PSI && PSI->hasProfileSummary()) ?
53495360
&AM.getResult<BlockFrequencyAnalysis>(F) : nullptr;
5361+
auto *BPI = AM.getCachedResult<BranchProbabilityAnalysis>(F);
53505362

53515363
if (!combineInstructionsOverFunction(F, Worklist, AA, AC, TLI, TTI, DT, ORE,
5352-
BFI, PSI, LI, Options))
5364+
BFI, BPI, PSI, LI, Options))
53535365
// No changes, all analyses are preserved.
53545366
return PreservedAnalyses::all();
53555367

@@ -5396,9 +5408,14 @@ bool InstructionCombiningPass::runOnFunction(Function &F) {
53965408
(PSI && PSI->hasProfileSummary()) ?
53975409
&getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() :
53985410
nullptr;
5411+
BranchProbabilityInfo *BPI = nullptr;
5412+
if (auto *WrapperPass =
5413+
getAnalysisIfAvailable<BranchProbabilityInfoWrapperPass>())
5414+
BPI = &WrapperPass->getBPI();
53995415

54005416
return combineInstructionsOverFunction(F, Worklist, AA, AC, TLI, TTI, DT, ORE,
5401-
BFI, PSI, LI, InstCombineOptions());
5417+
BFI, BPI, PSI, LI,
5418+
InstCombineOptions());
54025419
}
54035420

54045421
char InstructionCombiningPass::ID = 0;

llvm/test/Transforms/InstCombine/update-bpi.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
; CHECK-NEXT: edge %entry -> %bb2 probability is 0x79e79e7a / 0x80000000 = 95.24% [HOT edge]
77
; CHECK-NEXT: Printing analysis 'Branch Probability Analysis' for function 'invert_cond':
88
; CHECK-NEXT: ---- Branch Probabilities ----
9-
; CHECK-NEXT: edge %entry -> %bb2 probability is 0x06186186 / 0x80000000 = 4.76%
10-
; CHECK-NEXT: edge %entry -> %bb1 probability is 0x79e79e7a / 0x80000000 = 95.24% [HOT edge]
9+
; CHECK-NEXT: edge %entry -> %bb2 probability is 0x79e79e7a / 0x80000000 = 95.24% [HOT edge]
10+
; CHECK-NEXT: edge %entry -> %bb1 probability is 0x06186186 / 0x80000000 = 4.76%
1111

1212
define i32 @invert_cond(ptr %p) {
1313
; CHECK-LABEL: define i32 @invert_cond(

0 commit comments

Comments
 (0)