From be8732128029530a0b8671af3a47ce6085039fa2 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Sun, 21 Mar 2021 23:22:41 +0300 Subject: [PATCH] [clang][Codegen] EmitBranchOnBoolExpr(): emit prof branch counts even at -O0 This restores the original behaviour before i unadvertedly broke it in e3a470162738871bba982416748ae5f5e3572947 and clang/test/Profile/ caught it. --- clang/lib/CodeGen/CodeGenFunction.cpp | 38 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 1c53826..fd70884 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -1774,27 +1774,27 @@ void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, llvm::MDNode *Weights = nullptr; llvm::MDNode *Unpredictable = nullptr; - // If optimizing, lower unpredictability/probability knowledge about cond. - if (CGM.getCodeGenOpts().OptimizationLevel != 0) { - // If the branch has a condition wrapped by __builtin_unpredictable, - // create metadata that specifies that the branch is unpredictable. - if (auto *Call = dyn_cast(Cond->IgnoreImpCasts())) { - auto *FD = dyn_cast_or_null(Call->getCalleeDecl()); - if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) { - llvm::MDBuilder MDHelper(getLLVMContext()); - Unpredictable = MDHelper.createUnpredictable(); - } + // If the branch has a condition wrapped by __builtin_unpredictable, + // create metadata that specifies that the branch is unpredictable. + // Don't bother if not optimizing because that metadata would not be used. + auto *Call = dyn_cast(Cond->IgnoreImpCasts()); + if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) { + auto *FD = dyn_cast_or_null(Call->getCalleeDecl()); + if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) { + llvm::MDBuilder MDHelper(getLLVMContext()); + Unpredictable = MDHelper.createUnpredictable(); } + } - // If there is a Likelihood knowledge for the cond, lower it. - llvm::Value *NewCondV = emitCondLikelihoodViaExpectIntrinsic(CondV, LH); - if (CondV != NewCondV) - CondV = NewCondV; - else { - // Otherwise, lower profile counts. - uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount); - Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount); - } + // If there is a Likelihood knowledge for the cond, lower it. + // Note that if not optimizing this won't emit anything. + llvm::Value *NewCondV = emitCondLikelihoodViaExpectIntrinsic(CondV, LH); + if (CondV != NewCondV) + CondV = NewCondV; + else { + // Otherwise, lower profile counts. Note that we do this even at -O0. + uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount); + Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount); } Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights, Unpredictable); -- 2.7.4