@@ -1892,10 +1892,19 @@ void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
1892
1892
return ;
1893
1893
}
1894
1894
1895
+ // Emit the code with the fully general case.
1896
+ llvm::Value *CondV;
1897
+ {
1898
+ ApplyDebugLocation DL (*this , Cond);
1899
+ CondV = EvaluateExprAsBool (Cond);
1900
+ }
1901
+
1902
+ llvm::MDNode *Weights = nullptr ;
1903
+ llvm::MDNode *Unpredictable = nullptr ;
1904
+
1895
1905
// If the branch has a condition wrapped by __builtin_unpredictable,
1896
1906
// create metadata that specifies that the branch is unpredictable.
1897
1907
// Don't bother if not optimizing because that metadata would not be used.
1898
- llvm::MDNode *Unpredictable = nullptr ;
1899
1908
auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts ());
1900
1909
if (Call && CGM.getCodeGenOpts ().OptimizationLevel != 0 ) {
1901
1910
auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl ());
@@ -1905,18 +1914,17 @@ void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
1905
1914
}
1906
1915
}
1907
1916
1908
- llvm::MDNode *Weights = createBranchWeights (LH);
1909
- if (!Weights) {
1917
+ // If there is a Likelihood knowledge for the cond, lower it.
1918
+ // Note that if not optimizing this won't emit anything.
1919
+ llvm::Value *NewCondV = emitCondLikelihoodViaExpectIntrinsic (CondV, LH);
1920
+ if (CondV != NewCondV)
1921
+ CondV = NewCondV;
1922
+ else {
1923
+ // Otherwise, lower profile counts. Note that we do this even at -O0.
1910
1924
uint64_t CurrentCount = std::max (getCurrentProfileCount (), TrueCount);
1911
1925
Weights = createProfileWeights (TrueCount, CurrentCount - TrueCount);
1912
1926
}
1913
1927
1914
- // Emit the code with the fully general case.
1915
- llvm::Value *CondV;
1916
- {
1917
- ApplyDebugLocation DL (*this , Cond);
1918
- CondV = EvaluateExprAsBool (Cond);
1919
- }
1920
1928
Builder.CreateCondBr (CondV, TrueBlock, FalseBlock, Weights, Unpredictable);
1921
1929
}
1922
1930
@@ -2808,35 +2816,26 @@ llvm::DebugLoc CodeGenFunction::SourceLocToDebugLoc(SourceLocation Location) {
2808
2816
return llvm::DebugLoc ();
2809
2817
}
2810
2818
2811
- static Optional<std::pair<uint32_t , uint32_t >>
2812
- getLikelihoodWeights (Stmt::Likelihood LH) {
2819
+ llvm::Value *
2820
+ CodeGenFunction::emitCondLikelihoodViaExpectIntrinsic (llvm::Value *Cond,
2821
+ Stmt::Likelihood LH) {
2813
2822
switch (LH) {
2814
- case Stmt::LH_Unlikely:
2815
- return std::pair<uint32_t , uint32_t >(llvm::UnlikelyBranchWeight,
2816
- llvm::LikelyBranchWeight);
2817
2823
case Stmt::LH_None:
2818
- return None ;
2824
+ return Cond ;
2819
2825
case Stmt::LH_Likely:
2820
- return std::pair<uint32_t , uint32_t >(llvm::LikelyBranchWeight,
2821
- llvm::UnlikelyBranchWeight);
2826
+ case Stmt::LH_Unlikely:
2827
+ // Don't generate llvm.expect on -O0 as the backend won't use it for
2828
+ // anything.
2829
+ if (CGM.getCodeGenOpts ().OptimizationLevel == 0 )
2830
+ return Cond;
2831
+ llvm::Type *CondTy = Cond->getType ();
2832
+ assert (CondTy->isIntegerTy (1 ) && " expecting condition to be a boolean" );
2833
+ llvm::Function *FnExpect =
2834
+ CGM.getIntrinsic (llvm::Intrinsic::expect, CondTy);
2835
+ llvm::Value *ExpectedValueOfCond =
2836
+ llvm::ConstantInt::getBool (CondTy, LH == Stmt::LH_Likely);
2837
+ return Builder.CreateCall (FnExpect, {Cond, ExpectedValueOfCond},
2838
+ Cond->getName () + " .expval" );
2822
2839
}
2823
2840
llvm_unreachable (" Unknown Likelihood" );
2824
2841
}
2825
-
2826
- llvm::MDNode *CodeGenFunction::createBranchWeights (Stmt::Likelihood LH) const {
2827
- Optional<std::pair<uint32_t , uint32_t >> LHW = getLikelihoodWeights (LH);
2828
- if (!LHW)
2829
- return nullptr ;
2830
-
2831
- llvm::MDBuilder MDHelper (CGM.getLLVMContext ());
2832
- return MDHelper.createBranchWeights (LHW->first , LHW->second );
2833
- }
2834
-
2835
- llvm::MDNode *CodeGenFunction::createProfileOrBranchWeightsForLoop (
2836
- const Stmt *Cond, uint64_t LoopCount, const Stmt *Body) const {
2837
- llvm::MDNode *Weights = createProfileWeightsForLoop (Cond, LoopCount);
2838
- if (!Weights && CGM.getCodeGenOpts ().OptimizationLevel )
2839
- Weights = createBranchWeights (Stmt::getLikelihood (Body));
2840
-
2841
- return Weights;
2842
- }
0 commit comments