diff --git a/llvm/lib/Target/X86/X86CmovConversion.cpp b/llvm/lib/Target/X86/X86CmovConversion.cpp --- a/llvm/lib/Target/X86/X86CmovConversion.cpp +++ b/llvm/lib/Target/X86/X86CmovConversion.cpp @@ -364,12 +364,13 @@ /// \param TrueOpDepth depth cost of CMOV true value operand. /// \param FalseOpDepth depth cost of CMOV false value operand. static unsigned getDepthOfOptCmov(unsigned TrueOpDepth, unsigned FalseOpDepth) { - //===--------------------------------------------------------------------===// - // With no info about branch weight, we assume 50% for each value operand. - // Thus, depth of optimized CMOV instruction is the rounded up average of - // its True-Operand-Value-Depth and False-Operand-Value-Depth. - //===--------------------------------------------------------------------===// - return (TrueOpDepth + FalseOpDepth + 1) / 2; + // The depth of the result after branch conversion is + // TrueOpDepth * TrueOpProbability + FalseOpDepth * FalseOpProbability. + // As we have no info about branch weight, we assume 75% for one and 25% for + // the other, and pick the result with the largest resulting depth. + return std::max( + divideCeil(TrueOpDepth * 3 + FalseOpDepth, 4), + divideCeil(FalseOpDepth * 3 + TrueOpDepth, 4)); } bool X86CmovConverterPass::checkForProfitableCmovCandidates(