Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -2459,6 +2459,48 @@ return new ICmpInst(I.getPredicate(), A, B); } + // PR19753: + // (icmp eq/ne (ashr/lshr exact const2, A), const1) -> (icmp eq/ne A, + // Log2(const2/const1)) -> (icmp eq/ne A, Log2(const2) - Log2(const1)). + // TODO : Handle this for other icmp instructions. + if (I.isEquality()) { + ConstantInt *CI2; + if (match(Op0, m_AShr(m_ConstantInt(CI2), m_Value(A))) || + match(Op0, m_LShr(m_ConstantInt(CI2), m_Value(A)))) { + APInt AP1 = CI->getValue(); + APInt AP2 = CI2->getValue(); + int Shift; + // (icmp eq/ne (ashr/lshr exact const2, A), 0) -> + // (icmp ugt A, Log2(const2)). + // const2 = 0/ both const = 0 is already handled previously. + if (!AP1) { + Shift = AP2.logBase2(); + return new ICmpInst(I.ICMP_UGT, A, + ConstantInt::get(A->getType(), Shift)); + } + // if const1 = const2 -> icmp eq/ne A, 0 + if (CI == CI2) + return new ICmpInst(I.getPredicate(), A, + ConstantInt::getNullValue(A->getType())); + // If both the constants are negative, take their positive to calculate + // log. + if (AP1.isNegative() || AP2.isNegative()) { + AP1 = -AP1; + AP2 = -AP2; + } + Shift = AP2.logBase2() - AP1.logBase2(); + if ((cast(Op0)->isExact()) && (AP1.shl(Shift) == AP2)) + return new ICmpInst(I.getPredicate(), A, + ConstantInt::get(A->getType(), Shift)); + // Use lshr here, since we've canonicalized to +ve numbers. + else if (AP1 == AP2.lshr(Shift)) + return new ICmpInst(I.getPredicate(), A, + ConstantInt::get(A->getType(), Shift)); + else + return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType())); + } + } + // 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 @@ -1424,3 +1424,149 @@ %2 = icmp slt i32 %1, -10 ret i1 %2 } + +; CHECK-LABEL: @exact_ashr_eq +; CHECK-NEXT: icmp eq i32 %a, 31 +define i1 @exact_ashr_eq(i32 %a) { + %shr = ashr exact i32 -2147483648, %a + %cmp = icmp eq i32 %shr, -1 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_ashr_ne +; CHECK-NEXT: icmp ne i32 %a, 31 +define i1 @exact_ashr_ne(i32 %a) { + %shr = ashr exact i32 -2147483648, %a + %cmp = icmp ne i32 %shr, -1 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_eq +; CHECK-NEXT: icmp eq i32 %a, 30 +define i1 @exact_lshr_eq(i32 %a) { + %shr = lshr exact i32 1073741824, %a + %cmp = icmp eq i32 %shr, 1 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_ne +; CHECK-NEXT: icmp ne i32 %a, 30 +define i1 @exact_lshr_ne(i32 %a) { + %shr = lshr exact i32 1073741824, %a + %cmp = icmp ne i32 %shr, 1 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_ne_noexactdiv +; CHECK-NEXT: ret i1 false +define i1 @exact_lshr_ne_noexactdiv(i32 %a) { + %shr = lshr exact i32 80, %a + %cmp = icmp ne i32 %shr, 31 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_ashr_ne_noexactdiv +; CHECK-NEXT: ret i1 false +define i1 @exact_ashr_ne_noexactdiv(i32 %a) { + %shr = ashr exact i32 -80, %a + %cmp = icmp ne i32 %shr, -41 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_ashr_both_equal +; CHECK-NEXT: icmp ne i32 %a, 0 +define i1 @exact_ashr_both_equal(i32 %a) { + %shr = ashr exact i32 -2147483648, %a + %cmp = icmp ne i32 %shr, -2147483648 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_both_equal +; CHECK-NEXT: icmp eq i32 %a, 0 +define i1 @exact_lshr_both_equal(i32 %a) { + %shr = lshr exact i32 1073741824, %a + %cmp = icmp eq i32 %shr, 1073741824 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_ashr_noexact +; CHECK-NEXT: icmp ne i32 %a, 31 +define i1 @exact_ashr_noexact(i32 %a) { + %shr = ashr i32 -2147483648, %a + %cmp = icmp ne i32 %shr, -1 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_noexact +; CHECK-NEXT: icmp ne i32 %a, 30 +define i1 @exact_lshr_noexact(i32 %a) { + %shr = lshr i32 1073741824, %a + %cmp = icmp ne i32 %shr, 1 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_ashr_both_zero +; CHECK-NEXT: ret i1 true +define i1 @exact_ashr_both_zero(i32 %a) { + %shr = ashr exact i32 0, %a + %cmp = icmp eq i32 %shr, 0 + ret i1 %cmp +} + +; CHECK-LABEL: @exact_lshr_both_zero +; CHECK-NEXT: ret i1 true +define i1 @exact_lshr_both_zero(i32 %a) { + %shr = lshr exact i32 0, %a + %cmp = icmp eq i32 %shr, 0 + ret i1 %cmp +} + +; CHECK-LABEL: @ashr_both_zero +; CHECK-NEXT: ret i1 true +define i1 @ashr_both_zero(i32 %a) { + %shr = ashr i32 0, %a + %cmp = icmp eq i32 %shr, 0 + ret i1 %cmp +} + +; CHECK-LABEL: @lshr_both_zero +; CHECK-NEXT: ret i1 true +define i1 @lshr_both_zero(i32 %a) { + %shr = lshr i32 0, %a + %cmp = icmp eq i32 %shr, 0 + ret i1 %cmp +} + +; CHECK-LABEL: @lshr_second_zero +; CHECK-NEXT: icmp ugt i32 %a, 30 +define i1 @lshr_second_zero(i32 %a) { + %shr = lshr i32 1073741824, %a + %cmp = icmp eq i32 %shr, 0 + ret i1 %cmp +} + +; CHECK-LABEL: @ashr_second_zero +; CHECK-NEXT: icmp ugt i32 %a, 30 +define i1 @ashr_second_zero(i32 %a) { + %shr = ashr i32 1073741824, %a + %cmp = icmp eq i32 %shr, 0 + 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 1073741824, %a +; CHECK-NEXT: icmp ugt i32 %shr, 1 +define i1 @exact_lshr_no_neeq(i32 %a) { + %shr = lshr exact i32 1073741824, %a + %cmp = icmp ugt i32 %shr, 1 + ret i1 %cmp +}