[llvm] 7073ec5 - [InstCombine] canonicalize more zext-and-of-bool compare to narrow and

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 30 08:44:05 PDT 2022


Author: Sanjay Patel
Date: 2022-07-30T11:22:05-04:00
New Revision: 7073ec530e556afd2074018cca9e9c9ee484dbee

URL: https://github.com/llvm/llvm-project/commit/7073ec530e556afd2074018cca9e9c9ee484dbee
DIFF: https://github.com/llvm/llvm-project/commit/7073ec530e556afd2074018cca9e9c9ee484dbee.diff

LOG: [InstCombine] canonicalize more zext-and-of-bool compare to narrow and

https://alive2.llvm.org/ce/z/vBNiiM

This matches variants of patterns that were folded with:
b5a9361c90ca

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/test/Transforms/InstCombine/icmp.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 4ac04fb826fc..1712f391c0c8 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1940,14 +1940,15 @@ Instruction *InstCombinerImpl::foldICmpAndConstant(ICmpInst &Cmp,
 
   // ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
   // ((zext i1 X) & Y) != 0 -->  ((trunc Y) & X)
+  // ((zext i1 X) & Y) == 1 -->  ((trunc Y) & X)
+  // ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
   if (match(And, m_OneUse(m_c_And(m_OneUse(m_ZExt(m_Value(X))), m_Value(Y)))) &&
-      C.isZero() && X->getType()->isIntOrIntVectorTy(1)) {
+      X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {
     Value *TruncY = Builder.CreateTrunc(Y, X->getType());
-    if (Pred == CmpInst::ICMP_EQ) {
+    if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
       Value *And = Builder.CreateAnd(TruncY, X);
       return BinaryOperator::CreateNot(And);
     }
-    assert(Pred == CmpInst::ICMP_NE && "Unexpected predicate");
     return BinaryOperator::CreateAnd(TruncY, X);
   }
 

diff  --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll
index 1cfbeb6ed1d3..2e7687737820 100644
--- a/llvm/test/Transforms/InstCombine/icmp.ll
+++ b/llvm/test/Transforms/InstCombine/icmp.ll
@@ -4346,13 +4346,12 @@ define i1 @zext_bool_and_ne0(i1 %x, i8 %y) {
   ret i1 %r
 }
 
-; TODO: This should transform similarly to eq/ne 0.
-
 define i1 @zext_bool_and_ne1(i1 %x, i8 %y) {
 ; CHECK-LABEL: @zext_bool_and_ne1(
-; CHECK-NEXT:    [[ZX:%.*]] = zext i1 [[X:%.*]] to i8
-; CHECK-NEXT:    [[A:%.*]] = and i8 [[ZX]], [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp ne i8 [[A]], 1
+; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[Y:%.*]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i8 [[TMP1]], 0
+; CHECK-NEXT:    [[TMP3:%.*]] = and i1 [[TMP2]], [[X:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = xor i1 [[TMP3]], true
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %zx = zext i1 %x to i8
@@ -4363,9 +4362,8 @@ define i1 @zext_bool_and_ne1(i1 %x, i8 %y) {
 
 define <2 x i1> @zext_bool_and_eq1(<2 x i1> %x, <2 x i8> %y) {
 ; CHECK-LABEL: @zext_bool_and_eq1(
-; CHECK-NEXT:    [[ZX:%.*]] = zext <2 x i1> [[X:%.*]] to <2 x i8>
-; CHECK-NEXT:    [[A:%.*]] = and <2 x i8> [[ZX]], [[Y:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq <2 x i8> [[A]], <i8 1, i8 1>
+; CHECK-NEXT:    [[TMP1:%.*]] = trunc <2 x i8> [[Y:%.*]] to <2 x i1>
+; CHECK-NEXT:    [[R:%.*]] = and <2 x i1> [[TMP1]], [[X:%.*]]
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %zx = zext <2 x i1> %x to <2 x i8>


        


More information about the llvm-commits mailing list