diff options
author | Shengchen Kan <shengchen.kan@intel.com> | 2024-08-03 00:12:04 +0800 |
---|---|---|
committer | Shengchen Kan <shengchen.kan@intel.com> | 2024-08-03 00:17:28 +0800 |
commit | bb790b8bf2e9bb57db3313edb4868830213d1079 (patch) | |
tree | d6e9bf9b45658bcddb5f34918331589faf6d6cdb /llvm/lib/Transforms/Utils/SimplifyCFG.cpp | |
parent | 6c367168d6787941df2945528d7414f94a697e60 (diff) | |
download | llvm-bb790b8bf2e9bb57db3313edb4868830213d1079.zip llvm-bb790b8bf2e9bb57db3313edb4868830213d1079.tar.gz llvm-bb790b8bf2e9bb57db3313edb4868830213d1079.tar.bz2 |
[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.
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index a89cc9c..ccdfe47 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: |