diff options
author | Dávid Bolvanský <david.bolvansky@gmail.com> | 2020-08-13 12:45:14 +0200 |
---|---|---|
committer | Dávid Bolvanský <david.bolvansky@gmail.com> | 2020-08-13 14:23:58 +0200 |
commit | 44587e2f7e732604cd6340061d40ac21e7e188e5 (patch) | |
tree | 44d26c3c867da5e5a145b8ee066db552e34ea91f /llvm/lib | |
parent | 8af160b0b8ca8102b9490a287244af75727872f5 (diff) | |
download | llvm-44587e2f7e732604cd6340061d40ac21e7e188e5.zip llvm-44587e2f7e732604cd6340061d40ac21e7e188e5.tar.gz llvm-44587e2f7e732604cd6340061d40ac21e7e188e5.tar.bz2 |
[BPI] Improve static heuristics for integer comparisons
Similarly as for pointers, even for integers a == b is usually false.
GCC also uses this heuristic.
Reviewed By: ebrevnov
Differential Revision: https://reviews.llvm.org/D85781
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Analysis/BranchProbabilityInfo.cpp | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/llvm/lib/Analysis/BranchProbabilityInfo.cpp b/llvm/lib/Analysis/BranchProbabilityInfo.cpp index eae2c4e..6df90e6 100644 --- a/llvm/lib/Analysis/BranchProbabilityInfo.cpp +++ b/llvm/lib/Analysis/BranchProbabilityInfo.cpp @@ -122,8 +122,8 @@ static const uint32_t CC_NONTAKEN_WEIGHT = 64; static const uint32_t PH_TAKEN_WEIGHT = 20; static const uint32_t PH_NONTAKEN_WEIGHT = 12; -static const uint32_t ZH_TAKEN_WEIGHT = 20; -static const uint32_t ZH_NONTAKEN_WEIGHT = 12; +static const uint32_t INTH_TAKEN_WEIGHT = 20; +static const uint32_t INTH_NONTAKEN_WEIGHT = 12; static const uint32_t FPH_TAKEN_WEIGHT = 20; static const uint32_t FPH_NONTAKEN_WEIGHT = 12; @@ -856,7 +856,7 @@ bool BranchProbabilityInfo::calcLoopBranchHeuristics(const BasicBlock *BB, return true; } -bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB, +bool BranchProbabilityInfo::calcIntegerHeuristics(const BasicBlock *BB, const TargetLibraryInfo *TLI) { const BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); if (!BI || !BI->isConditional()) @@ -873,10 +873,21 @@ bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB, return dyn_cast<ConstantInt>(V); }; + BranchProbability TakenProb(INTH_TAKEN_WEIGHT, + INTH_TAKEN_WEIGHT + INTH_NONTAKEN_WEIGHT); + BranchProbability UntakenProb(INTH_NONTAKEN_WEIGHT, + INTH_TAKEN_WEIGHT + INTH_NONTAKEN_WEIGHT); Value *RHS = CI->getOperand(1); ConstantInt *CV = GetConstantInt(RHS); - if (!CV) - return false; + if (!CV) { + // X == Y -> Unlikely + // Otherwise -> Likely + if (CI->isTrueWhenEqual()) + std::swap(TakenProb, UntakenProb); + setEdgeProbability( + BB, SmallVector<BranchProbability, 2>({TakenProb, UntakenProb})); + return true; + } // If the LHS is the result of AND'ing a value with a single bit bitmask, // we don't have information about probabilities. @@ -964,10 +975,6 @@ bool BranchProbabilityInfo::calcZeroHeuristics(const BasicBlock *BB, return false; } - BranchProbability TakenProb(ZH_TAKEN_WEIGHT, - ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT); - BranchProbability UntakenProb(ZH_NONTAKEN_WEIGHT, - ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT); if (!isProb) std::swap(TakenProb, UntakenProb); @@ -1221,7 +1228,7 @@ void BranchProbabilityInfo::calculate(const Function &F, const LoopInfo &LI, continue; if (calcPointerHeuristics(BB)) continue; - if (calcZeroHeuristics(BB, TLI)) + if (calcIntegerHeuristics(BB, TLI)) continue; if (calcFloatingPointHeuristics(BB)) continue; |