From 26eb870d7e0e28ecb25a0cd77e6cdc0490707eff Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 4 Sep 2011 23:53:04 +0000 Subject: [PATCH] Use canonical forms for the branch probability zero heutistic. - Drop support for X >u 0, it's equivalent to X != 0 and should be canonicalized into the latter. - Add X < 1 -> unlikely, which is what instcombine canonicalizes X <= 0 into. - Add X > -1 -> likely, which is what instcombine canonicalizes X >= 0 into. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139110 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/BranchProbabilityInfo.cpp | 55 ++++++++++++++------------ 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/lib/Analysis/BranchProbabilityInfo.cpp b/lib/Analysis/BranchProbabilityInfo.cpp index c6f6fa77bc2..9c54a7eadd4 100644 --- a/lib/Analysis/BranchProbabilityInfo.cpp +++ b/lib/Analysis/BranchProbabilityInfo.cpp @@ -289,37 +289,42 @@ bool BranchProbabilityAnalysis::calcZeroHeuristics(BasicBlock *BB) { Value *RHS = CI->getOperand(1); ConstantInt *CV = dyn_cast(RHS); - if (!CV || !CV->isZero()) + if (!CV) return false; bool isProb; - switch (CI->getPredicate()) { - case CmpInst::ICMP_EQ: - // Equal to zero is not expected to be taken. + if (CV->isZero()) { + switch (CI->getPredicate()) { + case CmpInst::ICMP_EQ: + // X == 0 -> Unlikely + isProb = false; + break; + case CmpInst::ICMP_NE: + // X != 0 -> Likely + isProb = true; + break; + case CmpInst::ICMP_SLT: + // X < 0 -> Unlikely + isProb = false; + break; + case CmpInst::ICMP_SGT: + // X > 0 -> Likely + isProb = true; + break; + default: + return false; + } + } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) { + // InstCombine canonicalizes X <= 0 into X < 1. + // X <= 0 -> Unlikely isProb = false; - break; - - case CmpInst::ICMP_NE: - // Not equal to zero is expected. + } else if (CV->isAllOnesValue() && CI->getPredicate() == CmpInst::ICMP_SGT) { + // InstCombine canonicalizes X >= 0 into X > -1. + // X >= 0 -> Likely isProb = true; - break; - - case CmpInst::ICMP_SLT: - // Less or equal to zero is not expected. - // X < 0 -> Unlikely - isProb = false; - break; - - case CmpInst::ICMP_UGT: - case CmpInst::ICMP_SGT: - // Greater or equal to zero is expected. - // X > 0 -> Likely - isProb = true; - break; - - default: + } else { return false; - }; + } BasicBlock *Taken = BI->getSuccessor(0); BasicBlock *NonTaken = BI->getSuccessor(1);