From 2bcda6bb2896f0f8daf67343edfc64fb226f3e3f Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Sun, 18 Oct 2020 13:34:41 +0200 Subject: [Sema, CodeGen] Implement [[likely]] and [[unlikely]] in SwitchStmt This implements the likelihood attribute for the switch statement. Based on the discussion in D85091 and D86559 it only handles the attribute when placed on the case labels or the default labels. It also marks the likelihood attribute as feature complete. There are more QoI patches in the pipeline. Differential Revision: https://reviews.llvm.org/D89210 --- clang/lib/CodeGen/CodeGenFunction.cpp | 46 +++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 21 deletions(-) (limited to 'clang/lib/CodeGen/CodeGenFunction.cpp') diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 363b418..2dcb531 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -1478,21 +1478,6 @@ bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond, return true; } -static Optional> -getLikelihoodWeights(Stmt::Likelihood LH) { - switch (LH) { - case Stmt::LH_Unlikely: - return std::pair(llvm::UnlikelyBranchWeight, - llvm::LikelyBranchWeight); - case Stmt::LH_None: - return None; - case Stmt::LH_Likely: - return std::pair(llvm::LikelyBranchWeight, - llvm::UnlikelyBranchWeight); - } - llvm_unreachable("Unknown Likelihood"); -} - /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if /// statement) to the specified blocks. Based on the condition, this might try /// to simplify the codegen of the conditional based on the branch. @@ -1692,12 +1677,7 @@ void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, } } - llvm::MDNode *Weights = nullptr; - Optional> LHW = getLikelihoodWeights(LH); - if (LHW) { - llvm::MDBuilder MDHelper(CGM.getLLVMContext()); - Weights = MDHelper.createBranchWeights(LHW->first, LHW->second); - } + llvm::MDNode *Weights = createBranchWeights(LH); if (!Weights) { uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount); Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount); @@ -2569,3 +2549,27 @@ llvm::DebugLoc CodeGenFunction::SourceLocToDebugLoc(SourceLocation Location) { return llvm::DebugLoc(); } + +static Optional> +getLikelihoodWeights(Stmt::Likelihood LH) { + switch (LH) { + case Stmt::LH_Unlikely: + return std::pair(llvm::UnlikelyBranchWeight, + llvm::LikelyBranchWeight); + case Stmt::LH_None: + return None; + case Stmt::LH_Likely: + return std::pair(llvm::LikelyBranchWeight, + llvm::UnlikelyBranchWeight); + } + llvm_unreachable("Unknown Likelihood"); +} + +llvm::MDNode *CodeGenFunction::createBranchWeights(Stmt::Likelihood LH) const { + Optional> LHW = getLikelihoodWeights(LH); + if (!LHW) + return nullptr; + + llvm::MDBuilder MDHelper(CGM.getLLVMContext()); + return MDHelper.createBranchWeights(LHW->first, LHW->second); +} -- cgit v1.1