Index: llvm/lib/Analysis/BranchProbabilityInfo.cpp =================================================================== --- llvm/lib/Analysis/BranchProbabilityInfo.cpp +++ llvm/lib/Analysis/BranchProbabilityInfo.cpp @@ -877,67 +877,31 @@ INTH_TAKEN_WEIGHT + INTH_NONTAKEN_WEIGHT); BranchProbability UntakenProb(INTH_NONTAKEN_WEIGHT, INTH_TAKEN_WEIGHT + INTH_NONTAKEN_WEIGHT); + // If the LHS is the result of AND'ing a value with a single bit bitmask, + // we don't have information about probabilities. + if (Instruction *LHS = dyn_cast(CI->getOperand(0))) + if (LHS->getOpcode() == Instruction::And) + if (ConstantInt *AndRHS = dyn_cast(LHS->getOperand(1))) + if (AndRHS->getValue().isPowerOf2()) + return false; + Value *RHS = CI->getOperand(1); ConstantInt *CV = GetConstantInt(RHS); - if (!CV) { + if (CI->isEquality()) { // X == Y -> Unlikely - // Otherwise -> Likely - if (CI->isTrueWhenEqual()) + // X != Y -> Likely + if (CI->getPredicate() == CmpInst::ICMP_EQ) std::swap(TakenProb, UntakenProb); setEdgeProbability( BB, SmallVector({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. - if (Instruction *LHS = dyn_cast(CI->getOperand(0))) - if (LHS->getOpcode() == Instruction::And) - if (ConstantInt *AndRHS = dyn_cast(LHS->getOperand(1))) - if (AndRHS->getValue().isPowerOf2()) - return false; - - // Check if the LHS is the return value of a library function - LibFunc Func = NumLibFuncs; - if (TLI) - if (CallInst *Call = dyn_cast(CI->getOperand(0))) - if (Function *CalledFn = Call->getCalledFunction()) - TLI->getLibFunc(*CalledFn, Func); + if (!CV) + return false; bool isProb; - if (Func == LibFunc_strcasecmp || - Func == LibFunc_strcmp || - Func == LibFunc_strncasecmp || - Func == LibFunc_strncmp || - Func == LibFunc_memcmp || - Func == LibFunc_bcmp) { - // strcmp and similar functions return zero, negative, or positive, if the - // first string is equal, less, or greater than the second. We consider it - // likely that the strings are not equal, so a comparison with zero is - // probably false, but also a comparison with any other number is also - // probably false given that what exactly is returned for nonzero values is - // not specified. Any kind of comparison other than equality we know - // nothing about. - switch (CI->getPredicate()) { - case CmpInst::ICMP_EQ: - isProb = false; - break; - case CmpInst::ICMP_NE: - isProb = true; - break; - default: - return false; - } - } else if (CV->isZero()) { + 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; @@ -958,14 +922,6 @@ return false; } else if (CV->isMinusOne()) { switch (CI->getPredicate()) { - case CmpInst::ICMP_EQ: - // X == -1 -> Unlikely - isProb = false; - break; - case CmpInst::ICMP_NE: - // X != -1 -> Likely - isProb = true; - break; case CmpInst::ICMP_SGT: // InstCombine canonicalizes X >= 0 into X > -1. // X >= 0 -> Likely @@ -975,18 +931,7 @@ return false; } } else { - // X == C -> Unlikely - // X != C -> Likely - switch (CI->getPredicate()) { - case CmpInst::ICMP_EQ: - isProb = false; - break; - case CmpInst::ICMP_NE: - isProb = true; - break; - default: - return false; - } + return false; } if (!isProb) Index: llvm/test/Analysis/BranchProbabilityInfo/integer_heuristics.ll =================================================================== --- llvm/test/Analysis/BranchProbabilityInfo/integer_heuristics.ll +++ llvm/test/Analysis/BranchProbabilityInfo/integer_heuristics.ll @@ -142,8 +142,8 @@ entry: %c = icmp ult i32 %x, %y br i1 %c, label %then, label %else -; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50% -; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50% +; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00% +; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00% then: br label %else ; CHECK: edge then -> else probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge] Index: llvm/test/Analysis/BranchProbabilityInfo/libfunc_call.ll =================================================================== --- llvm/test/Analysis/BranchProbabilityInfo/libfunc_call.ll +++ llvm/test/Analysis/BranchProbabilityInfo/libfunc_call.ll @@ -86,8 +86,8 @@ %val = call i32 @strcmp(i8* %p, i8* %q) %cond = icmp sgt i32 %val, 0 br i1 %cond, label %then, label %else -; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00% -; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00% +; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50% +; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50% then: br label %exit @@ -108,8 +108,8 @@ %val = call i32 @strcmp(i8* %p, i8* %q) %cond = icmp slt i32 %val, 0 br i1 %cond, label %then, label %else -; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00% -; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00% +; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50% +; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50% then: br label %exit @@ -133,8 +133,8 @@ %val = call i32 @strncmp(i8* %p, i8* %q, i32 4) %cond = icmp sgt i32 %val, 0 br i1 %cond, label %then, label %else -; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00% -; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00% +; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50% +; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50% then: br label %exit @@ -155,8 +155,8 @@ %val = call i32 @strcasecmp(i8* %p, i8* %q) %cond = icmp sgt i32 %val, 0 br i1 %cond, label %then, label %else -; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00% -; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00% +; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50% +; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50% then: br label %exit @@ -177,8 +177,8 @@ %val = call i32 @strncasecmp(i8* %p, i8* %q, i32 4) %cond = icmp sgt i32 %val, 0 br i1 %cond, label %then, label %else -; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00% -; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00% +; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50% +; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50% then: br label %exit @@ -199,8 +199,8 @@ %val = call i32 @memcmp(i8* %p, i8* %q) %cond = icmp sgt i32 %val, 0 br i1 %cond, label %then, label %else -; CHECK: edge entry -> then probability is 0x40000000 / 0x80000000 = 50.00% -; CHECK: edge entry -> else probability is 0x40000000 / 0x80000000 = 50.00% +; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50% +; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50% then: br label %exit @@ -353,4 +353,4 @@ exit: %result = phi i32 [ 0, %then ], [ 1, %else ] ret i32 %result -} \ No newline at end of file +}