[llvm] 30e49a3 - [InstCombine] Optimise shift+and+boolean conversion pattern to simple comparison
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 23 06:53:27 PDT 2022
Author: chenglin.bi
Date: 2022-06-23T21:53:07+08:00
New Revision: 30e49a3794726fd9d0ae60d34880f15e64a9fb3f
URL: https://github.com/llvm/llvm-project/commit/30e49a3794726fd9d0ae60d34880f15e64a9fb3f
DIFF: https://github.com/llvm/llvm-project/commit/30e49a3794726fd9d0ae60d34880f15e64a9fb3f.diff
LOG: [InstCombine] Optimise shift+and+boolean conversion pattern to simple comparison
if (`C1` is pow2) & (`(C2 & ~(C1-1)) + C1)` is pow2):
((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1));
https://alive2.llvm.org/ce/z/EJAl1R
((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1));
https://alive2.llvm.org/ce/z/3bVRVz
And remove dead code.
Fix: https://github.com/llvm/llvm-project/issues/56124
Reviewed By: spatel
Differential Revision: https://reviews.llvm.org/D126591
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-and-shift.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index afcd0bed257c1..2800d8692fc71 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5559,35 +5559,23 @@ Instruction *InstCombinerImpl::foldICmpUsingKnownBits(ICmpInst &I) {
LHS = Op0;
Value *X;
- if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
- APInt ValToCheck = 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());
+ unsigned Log2C1 = C1->countTrailingZeros();
+ APInt C2 = Op0KnownZeroInverted;
+ APInt C2Pow2 = (C2 & ~(*C1 - 1)) + *C1;
+ if (C2Pow2.isPowerOf2()) {
+ // iff (C1 is pow2) & ((C2 & ~(C1-1)) + C1) is pow2):
+ // ((C1 << X) & C2) == 0 -> X >= (Log2(C2+C1) - Log2(C1))
+ // ((C1 << X) & C2) != 0 -> X < (Log2(C2+C1) - Log2(C1))
+ unsigned Log2C2 = C2Pow2.countTrailingZeros();
+ 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);
}
}
-
- // Check if the LHS is 8 >>u x and the result is a power of 2 like 1.
- const APInt *CI;
- if (Op0KnownZeroInverted.isOne() &&
- match(LHS, m_LShr(m_Power2(CI), m_Value(X)))) {
- // ((8 >>u X) & 1) == 0 -> X != 3
- // ((8 >>u X) & 1) != 0 -> X == 3
- unsigned CmpVal = CI->countTrailingZeros();
- auto NewPred = ICmpInst::getInversePredicate(Pred);
- return new ICmpInst(NewPred, X, ConstantInt::get(X->getType(), CmpVal));
- }
}
break;
}
diff --git a/llvm/test/Transforms/InstCombine/icmp-and-shift.ll b/llvm/test/Transforms/InstCombine/icmp-and-shift.ll
index ae77e74bb220f..f73915adefd14 100644
--- a/llvm/test/Transforms/InstCombine/icmp-and-shift.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-and-shift.ll
@@ -124,9 +124,7 @@ define i32 @icmp_eq_and_pow2_shl_pow2_negative1(i32 %0) {
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: [[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]]
;
@@ -203,9 +201,7 @@ define <2 x i32> @icmp_ne_and_pow2_minus1_shl1_vec(<2 x i32> %0) {
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]]
;
@@ -218,9 +214,7 @@ define i32 @icmp_eq_and_pow2_minus1_shl_pow2(i32 %0) {
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> <i32 4, i32 4>, [[TMP0:%.*]]
-; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[SHL]], <i32 12, i32 12>
-; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i32> [[AND]], zeroinitializer
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i32> [[TMP0:%.*]], <i32 1, i32 1>
; CHECK-NEXT: [[CONV:%.*]] = zext <2 x i1> [[CMP]] to <2 x i32>
; CHECK-NEXT: ret <2 x i32> [[CONV]]
;
@@ -233,9 +227,7 @@ define <2 x i32> @icmp_eq_and_pow2_minus1_shl_pow2_vec(<2 x i32> %0) {
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]]
;
@@ -248,9 +240,7 @@ define i32 @icmp_ne_and_pow2_minus1_shl_pow2(i32 %0) {
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> <i32 4, i32 4>, [[TMP0:%.*]]
-; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[SHL]], <i32 12, i32 12>
-; CHECK-NEXT: [[CMP:%.*]] = icmp ne <2 x i32> [[AND]], zeroinitializer
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i32> [[TMP0:%.*]], <i32 2, i32 2>
; CHECK-NEXT: [[CONV:%.*]] = zext <2 x i1> [[CMP]] to <2 x i32>
; CHECK-NEXT: ret <2 x i32> [[CONV]]
;
More information about the llvm-commits
mailing list