@@ -1347,9 +1347,13 @@ void InstCombinerImpl::freelyInvertAllUsersOf(Value *I, Value *IgnoredUser) {
1347
1347
SI->swapProfMetadata ();
1348
1348
break ;
1349
1349
}
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 ());
1352
1355
break ;
1356
+ }
1353
1357
case Instruction::Xor:
1354
1358
replaceInstUsesWith (cast<Instruction>(*U), I);
1355
1359
// Add to worklist for DCE.
@@ -3525,6 +3529,8 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
3525
3529
if (match (Cond, m_Not (m_Value (X))) && !isa<Constant>(X)) {
3526
3530
// Swap Destinations and condition...
3527
3531
BI.swapSuccessors ();
3532
+ if (BPI)
3533
+ BPI->swapSuccEdgesProbabilities (BI.getParent ());
3528
3534
return replaceOperand (BI, 0 , X);
3529
3535
}
3530
3536
@@ -3538,6 +3544,8 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
3538
3544
Value *NotX = Builder.CreateNot (X, " not." + X->getName ());
3539
3545
Value *Or = Builder.CreateLogicalOr (NotX, Y);
3540
3546
BI.swapSuccessors ();
3547
+ if (BPI)
3548
+ BPI->swapSuccEdgesProbabilities (BI.getParent ());
3541
3549
return replaceOperand (BI, 0 , Or);
3542
3550
}
3543
3551
@@ -3554,6 +3562,8 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
3554
3562
auto *Cmp = cast<CmpInst>(Cond);
3555
3563
Cmp->setPredicate (CmpInst::getInversePredicate (Pred));
3556
3564
BI.swapSuccessors ();
3565
+ if (BPI)
3566
+ BPI->swapSuccEdgesProbabilities (BI.getParent ());
3557
3567
Worklist.push (Cmp);
3558
3568
return &BI;
3559
3569
}
@@ -5248,7 +5258,8 @@ static bool combineInstructionsOverFunction(
5248
5258
Function &F, InstructionWorklist &Worklist, AliasAnalysis *AA,
5249
5259
AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
5250
5260
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) {
5252
5263
auto &DL = F.getParent ()->getDataLayout ();
5253
5264
5254
5265
// / Builder - This is an IRBuilder that automatically inserts new
@@ -5286,7 +5297,7 @@ static bool combineInstructionsOverFunction(
5286
5297
<< F.getName () << " \n " );
5287
5298
5288
5299
InstCombinerImpl IC (Worklist, Builder, F.hasMinSize (), AA, AC, TLI, TTI, DT,
5289
- ORE, BFI, PSI, DL, LI);
5300
+ ORE, BFI, BPI, PSI, DL, LI);
5290
5301
IC.MaxArraySizeForCombine = MaxArraySize;
5291
5302
bool MadeChangeInThisIteration = IC.prepareWorklist (F, RPOT);
5292
5303
MadeChangeInThisIteration |= IC.run ();
@@ -5347,9 +5358,10 @@ PreservedAnalyses InstCombinePass::run(Function &F,
5347
5358
MAMProxy.getCachedResult <ProfileSummaryAnalysis>(*F.getParent ());
5348
5359
auto *BFI = (PSI && PSI->hasProfileSummary ()) ?
5349
5360
&AM.getResult <BlockFrequencyAnalysis>(F) : nullptr ;
5361
+ auto *BPI = AM.getCachedResult <BranchProbabilityAnalysis>(F);
5350
5362
5351
5363
if (!combineInstructionsOverFunction (F, Worklist, AA, AC, TLI, TTI, DT, ORE,
5352
- BFI, PSI, LI, Options))
5364
+ BFI, BPI, PSI, LI, Options))
5353
5365
// No changes, all analyses are preserved.
5354
5366
return PreservedAnalyses::all ();
5355
5367
@@ -5396,9 +5408,14 @@ bool InstructionCombiningPass::runOnFunction(Function &F) {
5396
5408
(PSI && PSI->hasProfileSummary ()) ?
5397
5409
&getAnalysis<LazyBlockFrequencyInfoPass>().getBFI () :
5398
5410
nullptr ;
5411
+ BranchProbabilityInfo *BPI = nullptr ;
5412
+ if (auto *WrapperPass =
5413
+ getAnalysisIfAvailable<BranchProbabilityInfoWrapperPass>())
5414
+ BPI = &WrapperPass->getBPI ();
5399
5415
5400
5416
return combineInstructionsOverFunction (F, Worklist, AA, AC, TLI, TTI, DT, ORE,
5401
- BFI, PSI, LI, InstCombineOptions ());
5417
+ BFI, BPI, PSI, LI,
5418
+ InstCombineOptions ());
5402
5419
}
5403
5420
5404
5421
char InstructionCombiningPass::ID = 0 ;
0 commit comments