@@ -1764,31 +1764,39 @@ void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
1764
1764
return ;
1765
1765
}
1766
1766
1767
- // If the branch has a condition wrapped by __builtin_unpredictable,
1768
- // create metadata that specifies that the branch is unpredictable.
1769
- // Don't bother if not optimizing because that metadata would not be used.
1770
- llvm::MDNode *Unpredictable = nullptr ;
1771
- auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts ());
1772
- if (Call && CGM.getCodeGenOpts ().OptimizationLevel != 0 ) {
1773
- auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl ());
1774
- if (FD && FD->getBuiltinID () == Builtin::BI__builtin_unpredictable) {
1775
- llvm::MDBuilder MDHelper (getLLVMContext ());
1776
- Unpredictable = MDHelper.createUnpredictable ();
1777
- }
1778
- }
1779
-
1780
- llvm::MDNode *Weights = createBranchWeights (LH);
1781
- if (!Weights) {
1782
- uint64_t CurrentCount = std::max (getCurrentProfileCount (), TrueCount);
1783
- Weights = createProfileWeights (TrueCount, CurrentCount - TrueCount);
1784
- }
1785
-
1786
1767
// Emit the code with the fully general case.
1787
1768
llvm::Value *CondV;
1788
1769
{
1789
1770
ApplyDebugLocation DL (*this , Cond);
1790
1771
CondV = EvaluateExprAsBool (Cond);
1791
1772
}
1773
+
1774
+ llvm::MDNode *Weights = nullptr ;
1775
+ llvm::MDNode *Unpredictable = nullptr ;
1776
+
1777
+ // If optimizing, lower unpredictability/probability knowledge about cond.
1778
+ if (CGM.getCodeGenOpts ().OptimizationLevel != 0 ) {
1779
+ // If the branch has a condition wrapped by __builtin_unpredictable,
1780
+ // create metadata that specifies that the branch is unpredictable.
1781
+ if (auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts ())) {
1782
+ auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl ());
1783
+ if (FD && FD->getBuiltinID () == Builtin::BI__builtin_unpredictable) {
1784
+ llvm::MDBuilder MDHelper (getLLVMContext ());
1785
+ Unpredictable = MDHelper.createUnpredictable ();
1786
+ }
1787
+ }
1788
+
1789
+ // If there is a Likelihood knowledge for the cond, lower it.
1790
+ llvm::Value *NewCondV = emitCondLikelihoodViaExpectIntrinsic (CondV, LH);
1791
+ if (CondV != NewCondV)
1792
+ CondV = NewCondV;
1793
+ else {
1794
+ // Otherwise, lower profile counts.
1795
+ uint64_t CurrentCount = std::max (getCurrentProfileCount (), TrueCount);
1796
+ Weights = createProfileWeights (TrueCount, CurrentCount - TrueCount);
1797
+ }
1798
+ }
1799
+
1792
1800
Builder.CreateCondBr (CondV, TrueBlock, FalseBlock, Weights, Unpredictable);
1793
1801
}
1794
1802
@@ -2632,35 +2640,26 @@ llvm::DebugLoc CodeGenFunction::SourceLocToDebugLoc(SourceLocation Location) {
2632
2640
return llvm::DebugLoc ();
2633
2641
}
2634
2642
2635
- static Optional<std::pair<uint32_t , uint32_t >>
2636
- getLikelihoodWeights (Stmt::Likelihood LH) {
2643
+ llvm::Value *
2644
+ CodeGenFunction::emitCondLikelihoodViaExpectIntrinsic (llvm::Value *Cond,
2645
+ Stmt::Likelihood LH) {
2637
2646
switch (LH) {
2638
- case Stmt::LH_Unlikely:
2639
- return std::pair<uint32_t , uint32_t >(llvm::UnlikelyBranchWeight,
2640
- llvm::LikelyBranchWeight);
2641
2647
case Stmt::LH_None:
2642
- return None ;
2648
+ return Cond ;
2643
2649
case Stmt::LH_Likely:
2644
- return std::pair<uint32_t , uint32_t >(llvm::LikelyBranchWeight,
2645
- llvm::UnlikelyBranchWeight);
2650
+ case Stmt::LH_Unlikely:
2651
+ // Don't generate llvm.expect on -O0 as the backend won't use it for
2652
+ // anything.
2653
+ if (CGM.getCodeGenOpts ().OptimizationLevel == 0 )
2654
+ return Cond;
2655
+ llvm::Type *CondTy = Cond->getType ();
2656
+ assert (CondTy->isIntegerTy (1 ) && " expecting condition to be a boolean" );
2657
+ llvm::Function *FnExpect =
2658
+ CGM.getIntrinsic (llvm::Intrinsic::expect, CondTy);
2659
+ llvm::Value *ExpectedValueOfCond =
2660
+ llvm::ConstantInt::getBool (CondTy, LH == Stmt::LH_Likely);
2661
+ return Builder.CreateCall (FnExpect, {Cond, ExpectedValueOfCond},
2662
+ Cond->getName () + " .expval" );
2646
2663
}
2647
2664
llvm_unreachable (" Unknown Likelihood" );
2648
2665
}
2649
-
2650
- llvm::MDNode *CodeGenFunction::createBranchWeights (Stmt::Likelihood LH) const {
2651
- Optional<std::pair<uint32_t , uint32_t >> LHW = getLikelihoodWeights (LH);
2652
- if (!LHW)
2653
- return nullptr ;
2654
-
2655
- llvm::MDBuilder MDHelper (CGM.getLLVMContext ());
2656
- return MDHelper.createBranchWeights (LHW->first , LHW->second );
2657
- }
2658
-
2659
- llvm::MDNode *CodeGenFunction::createProfileOrBranchWeightsForLoop (
2660
- const Stmt *Cond, uint64_t LoopCount, const Stmt *Body) const {
2661
- llvm::MDNode *Weights = createProfileWeightsForLoop (Cond, LoopCount);
2662
- if (!Weights && CGM.getCodeGenOpts ().OptimizationLevel )
2663
- Weights = createBranchWeights (Stmt::getLikelihood (Body));
2664
-
2665
- return Weights;
2666
- }
0 commit comments