Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -5540,22 +5540,35 @@ LHS = Op0; Value *X; - if (match(LHS, m_Shl(m_One(), m_Value(X)))) { - APInt ValToCheck = Op0KnownZeroInverted; + APInt C2 = Op0KnownZeroInverted; + const APInt *C1; + if (match(LHS, m_Shl(m_Power2(C1), m_Value(X)))) { Type *XTy = X->getType(); - if (ValToCheck.isPowerOf2()) { - // ((1 << X) & 8) == 0 -> X != 3 - // ((1 << X) & 8) != 0 -> X == 3 - auto *CmpC = ConstantInt::get(XTy, ValToCheck.countTrailingZeros()); - auto NewPred = ICmpInst::getInversePredicate(Pred); - return new ICmpInst(NewPred, X, CmpC); - } else if ((++ValToCheck).isPowerOf2()) { - // ((1 << X) & 7) == 0 -> X >= 3 - // ((1 << X) & 7) != 0 -> X < 3 - auto *CmpC = ConstantInt::get(XTy, ValToCheck.countTrailingZeros()); - auto NewPred = - Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE : CmpInst::ICMP_ULT; - return new ICmpInst(NewPred, X, CmpC); + unsigned Log2C1 = C1->countTrailingZeros(); + if (C2.isPowerOf2()) { + // iff (C1 is pow2) & (C2 is pow2) & (C1 <= C2): + // ((C1 << X) & C2) == 0 -> X != (Log2(C2) - Log2(C1)) + // ((C1 << X) & C2) != 0 -> X == (Log2(C2) - Log2(C1)) + unsigned Log2C2 = C2.countTrailingZeros(); + if (Log2C1 <= Log2C2) { + auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1); + auto NewPred = ICmpInst::getInversePredicate(Pred); + return new ICmpInst(NewPred, X, CmpC); + } + } else { + APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1; + if (C2Pow2.isPowerOf2()) { + // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2) & (C1 < C2): + // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1)) + // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1)) + unsigned Log2C2 = C2Pow2.countTrailingZeros(); + if (Log2C1 <= Log2C2) { + auto *CmpC = ConstantInt::get(XTy, Log2C2 - Log2C1); + auto NewPred = Pred == CmpInst::ICMP_EQ ? CmpInst::ICMP_UGE + : CmpInst::ICMP_ULT; + return new ICmpInst(NewPred, X, CmpC); + } + } } } Index: llvm/test/Transforms/InstCombine/icmp-and-shift.ll =================================================================== --- llvm/test/Transforms/InstCombine/icmp-and-shift.ll +++ llvm/test/Transforms/InstCombine/icmp-and-shift.ll @@ -57,11 +57,9 @@ define i32 @icmp_eq_and_pow2_shl_pow2(i32 %0) { ; CHECK-LABEL: @icmp_eq_and_pow2_shl_pow2( -; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = lshr i32 [[SHL]], 4 -; CHECK-NEXT: [[AND_LOBIT:%.*]] = and i32 [[AND]], 1 -; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[AND_LOBIT]], 1 -; CHECK-NEXT: ret i32 [[TMP2]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[TMP0:%.*]], 3 +; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP]] to i32 +; CHECK-NEXT: ret i32 [[CONV]] ; %shl = shl i32 2, %0 %and = and i32 %shl, 16 @@ -72,11 +70,9 @@ define <2 x i32> @icmp_eq_and_pow2_shl_pow2_vec(<2 x i32> %0) { ; CHECK-LABEL: @icmp_eq_and_pow2_shl_pow2_vec( -; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> , [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = lshr <2 x i32> [[SHL]], -; CHECK-NEXT: [[AND_LOBIT:%.*]] = and <2 x i32> [[AND]], -; CHECK-NEXT: [[TMP2:%.*]] = xor <2 x i32> [[AND_LOBIT]], -; CHECK-NEXT: ret <2 x i32> [[TMP2]] +; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[CONV:%.*]] = zext <2 x i1> [[CMP]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[CONV]] ; %shl = shl <2 x i32> , %0 %and = and <2 x i32> %shl, @@ -87,10 +83,9 @@ define i32 @icmp_ne_and_pow2_shl_pow2(i32 %0) { ; CHECK-LABEL: @icmp_ne_and_pow2_shl_pow2( -; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = lshr i32 [[SHL]], 4 -; CHECK-NEXT: [[AND_LOBIT:%.*]] = and i32 [[AND]], 1 -; CHECK-NEXT: ret i32 [[AND_LOBIT]] +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP0:%.*]], 3 +; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP]] to i32 +; CHECK-NEXT: ret i32 [[CONV]] ; %shl = shl i32 2, %0 %and = and i32 %shl, 16 @@ -101,10 +96,9 @@ define <2 x i32> @icmp_ne_and_pow2_shl_pow2_vec(<2 x i32> %0) { ; CHECK-LABEL: @icmp_ne_and_pow2_shl_pow2_vec( -; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> , [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = lshr <2 x i32> [[SHL]], -; CHECK-NEXT: [[AND_LOBIT:%.*]] = and <2 x i32> [[AND]], -; CHECK-NEXT: ret <2 x i32> [[AND_LOBIT]] +; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[CONV:%.*]] = zext <2 x i1> [[CMP]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[CONV]] ; %shl = shl <2 x i32> , %0 %and = and <2 x i32> %shl, @@ -131,13 +125,13 @@ define i32 @icmp_eq_and_pow2_shl_pow2_negative2(i32 %0) { ; CHECK-LABEL: @icmp_eq_and_pow2_shl_pow2_negative2( ; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], 14 +; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], 10 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0 ; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP]] to i32 ; CHECK-NEXT: ret i32 [[CONV]] ; %shl = shl i32 2, %0 - %and = and i32 %shl, 14 + %and = and i32 %shl, 11 %cmp = icmp eq i32 %and, 0 %conv = zext i1 %cmp to i32 ret i32 %conv @@ -209,9 +203,7 @@ define i32 @icmp_eq_and_pow2_minus1_shl_pow2(i32 %0) { ; CHECK-LABEL: @icmp_eq_and_pow2_minus1_shl_pow2( -; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], 14 -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0 +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[TMP0:%.*]], 2 ; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP]] to i32 ; CHECK-NEXT: ret i32 [[CONV]] ; @@ -224,9 +216,7 @@ define <2 x i32> @icmp_eq_and_pow2_minus1_shl_pow2_vec(<2 x i32> %0) { ; CHECK-LABEL: @icmp_eq_and_pow2_minus1_shl_pow2_vec( -; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> , [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[SHL]], -; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[AND]], zeroinitializer +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i32> [[TMP0:%.*]], ; CHECK-NEXT: [[CONV:%.*]] = zext <2 x i1> [[CMP]] to <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[CONV]] ; @@ -239,9 +229,7 @@ define i32 @icmp_ne_and_pow2_minus1_shl_pow2(i32 %0) { ; CHECK-LABEL: @icmp_ne_and_pow2_minus1_shl_pow2( -; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], 14 -; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[AND]], 0 +; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP0:%.*]], 3 ; CHECK-NEXT: [[CONV:%.*]] = zext i1 [[CMP]] to i32 ; CHECK-NEXT: ret i32 [[CONV]] ; @@ -254,9 +242,7 @@ define <2 x i32> @icmp_ne_and_pow2_minus1_shl_pow2_vec(<2 x i32> %0) { ; CHECK-LABEL: @icmp_ne_and_pow2_minus1_shl_pow2_vec( -; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> , [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[SHL]], -; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[AND]], zeroinitializer +; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i32> [[TMP0:%.*]], ; CHECK-NEXT: [[CONV:%.*]] = zext <2 x i1> [[CMP]] to <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[CONV]] ;