Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2318,6 +2318,30 @@ return GlobalSwapBenefits > 0; } +// Helper function to check whether Op represents a lshr/ashr exact +// instruction. For example: +// (icmp eq/ne (ashr exact const2, A), const1) -> icmp eq/ne A, Log2(const2/const1) +// Here if Op represents -> (ashr exact const2, A), and CI represents +// const1, we compute Quotient as const2/const1. +static bool checkShrExact(Value *Op, APInt &Quotient, const ConstantInt *CI, + Value *&A) { + ConstantInt *CI2; + if (match(Op, m_AShr(m_ConstantInt(CI2), m_Value(A))) && + (cast(Op)->isExact())) { + Quotient = CI2->getValue().sdiv(CI->getValue()); + return true; + } + + // Handle the case for lshr. + if (match(Op, m_LShr(m_ConstantInt(CI2), m_Value(A))) && + (cast(Op)->isExact())) { + Quotient = CI2->getValue().udiv(CI->getValue()); + return true; + } + + return false; +} + Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { bool Changed = false; Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); @@ -2439,6 +2463,21 @@ return new ICmpInst(I.getPredicate(), A, B); } + // PR19753: + // (icmp eq/ne (ashr exact const2, A), const1) -> icmp eq/ne A, Log2(const2/const1) + // 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. Similar for lshr. + // TODO : Handle this for other icmp instructions. + { + APInt Quotient; + if (I.isEquality() && checkShrExact(Op0, Quotient, CI, A)) { + unsigned shift = Quotient.logBase2(); + return new ICmpInst(I.getPredicate(), A, + ConstantInt::get(A->getType(), shift)); + } + } + // If we have an icmp le or icmp ge instruction, turn it into the // appropriate icmp lt or icmp gt instruction. This allows us to rely on // them being folded in the code below. The SimplifyICmpInst code has Index: test/Transforms/InstCombine/icmp.ll =================================================================== --- test/Transforms/InstCombine/icmp.ll +++ test/Transforms/InstCombine/icmp.ll @@ -1382,3 +1382,71 @@ %2 = icmp slt i32 %1, -10 ret i1 %2 } + +; CHECK-LABEL: @exact_ashr_eq +; CHECK-NEXT: icmp eq i32 %a, 1 +define i1 @exact_ashr_eq(i32 %a) { + %shr = ashr exact i32 -30, %a + %cmp = icmp eq i32 %shr, -15 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_ashr_ne +; CHECK-NEXT: icmp ne i32 %a, 1 +define i1 @exact_ashr_ne(i32 %a) { + %shr = ashr exact i32 -30, %a + %cmp = icmp ne i32 %shr, -15 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_eq +; CHECK-NEXT: icmp eq i32 %a, 3 +define i1 @exact_lshr_eq(i32 %a) { + %shr = lshr exact i32 80, %a + %cmp = icmp eq i32 %shr, 10 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_ne +; CHECK-NEXT: icmp ne i32 %a, 3 +define i1 @exact_lshr_ne(i32 %a) { + %shr = lshr exact i32 80, %a + %cmp = icmp ne i32 %shr, 10 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_ashr_noexact +; CHECK-NEXT: ashr i32 -30, %a +; CHECK-NEXT: icmp ne i32 %shr, -15 +define i1 @exact_ashr_noexact(i32 %a) { + %shr = ashr i32 -30, %a + %cmp = icmp ne i32 %shr, -15 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_noexact +; CHECK-NEXT: lshr i32 80, %a +; CHECK-NEXT: icmp ne i32 %shr, 10 +define i1 @exact_lshr_noexact(i32 %a) { + %shr = lshr i32 80, %a + %cmp = icmp ne i32 %shr, 10 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_ashr_no_neeq +; CHECK-NEXT: ashr exact i32 -30, %a +; CHECK-NEXT: icmp ult i32 %shr, -15 +define i1 @exact_ashr_no_neeq(i32 %a) { + %shr = ashr exact i32 -30, %a + %cmp = icmp ult i32 %shr, -15 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_no_neeq +; CHECK-NEXT: lshr exact i32 80, %a +; CHECK-NEXT: icmp ugt i32 %shr, 10 +define i1 @exact_lshr_no_neeq(i32 %a) { + %shr = lshr exact i32 80, %a + %cmp = icmp ugt i32 %shr, 10 + ret i1 %cmp +}