diff options
author | Mark de Wever <koraq@xs4all.nl> | 2020-10-18 13:34:41 +0200 |
---|---|---|
committer | Mark de Wever <koraq@xs4all.nl> | 2020-10-18 13:48:42 +0200 |
commit | 2bcda6bb2896f0f8daf67343edfc64fb226f3e3f (patch) | |
tree | f606041cb0f082b30bd0cfd2bf4a2c3b6cb04234 /clang/lib/CodeGen/CodeGenFunction.cpp | |
parent | 7081db99eee0123f955ab01e550bdfdfce606dd2 (diff) | |
download | llvm-2bcda6bb2896f0f8daf67343edfc64fb226f3e3f.zip llvm-2bcda6bb2896f0f8daf67343edfc64fb226f3e3f.tar.gz llvm-2bcda6bb2896f0f8daf67343edfc64fb226f3e3f.tar.bz2 |
[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
Diffstat (limited to 'clang/lib/CodeGen/CodeGenFunction.cpp')
-rw-r--r-- | clang/lib/CodeGen/CodeGenFunction.cpp | 46 |
1 files changed, 25 insertions, 21 deletions
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<std::pair<uint32_t, uint32_t>> -getLikelihoodWeights(Stmt::Likelihood LH) { - switch (LH) { - case Stmt::LH_Unlikely: - return std::pair<uint32_t, uint32_t>(llvm::UnlikelyBranchWeight, - llvm::LikelyBranchWeight); - case Stmt::LH_None: - return None; - case Stmt::LH_Likely: - return std::pair<uint32_t, uint32_t>(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<std::pair<uint32_t, uint32_t>> 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<std::pair<uint32_t, uint32_t>> +getLikelihoodWeights(Stmt::Likelihood LH) { + switch (LH) { + case Stmt::LH_Unlikely: + return std::pair<uint32_t, uint32_t>(llvm::UnlikelyBranchWeight, + llvm::LikelyBranchWeight); + case Stmt::LH_None: + return None; + case Stmt::LH_Likely: + return std::pair<uint32_t, uint32_t>(llvm::LikelyBranchWeight, + llvm::UnlikelyBranchWeight); + } + llvm_unreachable("Unknown Likelihood"); +} + +llvm::MDNode *CodeGenFunction::createBranchWeights(Stmt::Likelihood LH) const { + Optional<std::pair<uint32_t, uint32_t>> LHW = getLikelihoodWeights(LH); + if (!LHW) + return nullptr; + + llvm::MDBuilder MDHelper(CGM.getLLVMContext()); + return MDHelper.createBranchWeights(LHW->first, LHW->second); +} |