Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2444,13 +2444,23 @@ // Cases where const1 doesn't divide const2 exactly or Quotient is not // exact of log2 are handled by SimplifyICmpInst call above where we // return false. - // TODO: Handle this for lshr exact with udiv. { ConstantInt *CI2; + APInt Quotient; + unsigned shift; if (match(Op0, m_AShr(m_ConstantInt(CI2), m_Value(A))) && (cast(Op0)->isExact())) { - APInt Quotient = CI2->getValue().sdiv(CI->getValue()); - unsigned shift = Quotient.logBase2(); + Quotient = CI2->getValue().sdiv(CI->getValue()); + shift = Quotient.logBase2(); + return new ICmpInst(I.getPredicate(), A, + ConstantInt::get(A->getType(), shift)); + } + + // Handle the case for lhsr similar to above transformation. + else if (match(Op0, m_LShr(m_ConstantInt(CI2), m_Value(A))) && + (cast(Op0)->isExact())) { + Quotient = CI2->getValue().udiv(CI->getValue()); + shift = Quotient.logBase2(); return new ICmpInst(I.getPredicate(), A, ConstantInt::get(A->getType(), shift)); } Index: test/Transforms/InstCombine/icmp.ll =================================================================== --- test/Transforms/InstCombine/icmp.ll +++ test/Transforms/InstCombine/icmp.ll @@ -1373,3 +1373,11 @@ %cmp = icmp eq i32 %shr, -15 ret i1 %cmp } + +; CHECK-LABEL: @exact_lhsr +; CHECK-NEXT: icmp eq i32 %a, 3 +define i1 @exact_lhsr(i32 %a) { + %shr = lshr exact i32 80, %a + %cmp = icmp eq i32 %shr, 10 + ret i1 %cmp +}