[llvm] bb790b8 - [NFC] Extract the probability check for the hoisted BB into a local function
Shengchen Kan via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 2 09:18:12 PDT 2024
Author: Shengchen Kan
Date: 2024-08-03T00:17:28+08:00
New Revision: bb790b8bf2e9bb57db3313edb4868830213d1079
URL: https://github.com/llvm/llvm-project/commit/bb790b8bf2e9bb57db3313edb4868830213d1079
DIFF: https://github.com/llvm/llvm-project/commit/bb790b8bf2e9bb57db3313edb4868830213d1079.diff
LOG: [NFC] Extract the probability check for the hoisted BB into a local function
So that we can early bail out to avoid nested if clauses.
This is to extract the NFC change in #96878 into a separate PR.
Added:
Modified:
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index a89cc9cd11d4d..ccdfe47ef81e7 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2960,6 +2960,24 @@ static bool validateAndCostRequiredSelects(BasicBlock *BB, BasicBlock *ThenBB,
return HaveRewritablePHIs;
}
+static bool isProfitableToSpeculate(const BranchInst *BI, bool Invert,
+ const TargetTransformInfo &TTI) {
+ // If the branch is non-unpredictable, and is predicted to *not* branch to
+ // the `then` block, then avoid speculating it.
+ if (BI->getMetadata(LLVMContext::MD_unpredictable))
+ return true;
+
+ uint64_t TWeight, FWeight;
+ if (!extractBranchWeights(*BI, TWeight, FWeight) || (TWeight + FWeight) == 0)
+ return true;
+
+ uint64_t EndWeight = Invert ? TWeight : FWeight;
+ BranchProbability BIEndProb =
+ BranchProbability::getBranchProbability(EndWeight, TWeight + FWeight);
+ BranchProbability Likely = TTI.getPredictableBranchThreshold();
+ return BIEndProb < Likely;
+}
+
/// Speculate a conditional basic block flattening the CFG.
///
/// Note that this is a very risky transform currently. Speculating
@@ -3021,20 +3039,8 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
}
assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block");
- // If the branch is non-unpredictable, and is predicted to *not* branch to
- // the `then` block, then avoid speculating it.
- if (!BI->getMetadata(LLVMContext::MD_unpredictable)) {
- uint64_t TWeight, FWeight;
- if (extractBranchWeights(*BI, TWeight, FWeight) &&
- (TWeight + FWeight) != 0) {
- uint64_t EndWeight = Invert ? TWeight : FWeight;
- BranchProbability BIEndProb =
- BranchProbability::getBranchProbability(EndWeight, TWeight + FWeight);
- BranchProbability Likely = TTI.getPredictableBranchThreshold();
- if (BIEndProb >= Likely)
- return false;
- }
- }
+ if (!isProfitableToSpeculate(BI, Invert, TTI))
+ return false;
// Keep a count of how many times instructions are used within ThenBB when
// they are candidates for sinking into ThenBB. Specifically:
More information about the llvm-commits
mailing list