[PATCH] D154574: [InstCombine] Fold `(icmp eq/ne (zext i1 X) (sext i1 Y))`-> `(icmp eq/ne (or X, Y), 0)`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 6 00:30:56 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: nikic, RKSimon.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This comes up when adding two `bool` types in C/C++

      bool foo(bool a, bool b) {
          return a + b;
      }
      ...
      ->
      define i1 @foo(i1 %a, i1 %b) {
          %conv = zext i1 %a to i32
          %conv3.neg = sext i1 %b to i32
          %tobool4 = icmp ne i32 %conv, %conv3.neg
          ret i1 %tobool4
  }

Proof: https://alive2.llvm.org/ce/z/HffWAN


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154574

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
  llvm/test/Transforms/InstCombine/icmp-ext-ext.ll


Index: llvm/test/Transforms/InstCombine/icmp-ext-ext.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp-ext-ext.ll
+++ llvm/test/Transforms/InstCombine/icmp-ext-ext.ll
@@ -384,9 +384,8 @@
 
 define i1 @zext_eq_sext(i1 %a, i1 %b) {
 ; CHECK-LABEL: @zext_eq_sext(
-; CHECK-NEXT:    [[CONV:%.*]] = zext i1 [[A:%.*]] to i32
-; CHECK-NEXT:    [[CONV3_NEG:%.*]] = sext i1 [[B:%.*]] to i32
-; CHECK-NEXT:    [[TOBOOL4:%.*]] = icmp eq i32 [[CONV]], [[CONV3_NEG]]
+; CHECK-NEXT:    [[TMP1:%.*]] = or i1 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    [[TOBOOL4:%.*]] = xor i1 [[TMP1]], true
 ; CHECK-NEXT:    ret i1 [[TOBOOL4]]
 ;
   %conv = zext i1 %a to i32
@@ -410,10 +409,8 @@
 
 define <2 x i1> @zext_ne_sext(<2 x i1> %a, <2 x i1> %b) {
 ; CHECK-LABEL: @zext_ne_sext(
-; CHECK-NEXT:    [[CONV:%.*]] = zext <2 x i1> [[A:%.*]] to <2 x i8>
-; CHECK-NEXT:    [[CONV3_NEG:%.*]] = sext <2 x i1> [[B:%.*]] to <2 x i8>
-; CHECK-NEXT:    [[TOBOOL4:%.*]] = icmp ne <2 x i8> [[CONV3_NEG]], [[CONV]]
-; CHECK-NEXT:    ret <2 x i1> [[TOBOOL4]]
+; CHECK-NEXT:    [[TMP1:%.*]] = or <2 x i1> [[B:%.*]], [[A:%.*]]
+; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
 ;
   %conv = zext <2 x i1> %a to <2 x i8>
   %conv3.neg = sext <2 x i1> %b to <2 x i8>
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5233,10 +5233,19 @@
     bool IsZext0 = isa<ZExtOperator>(ICmp.getOperand(0));
     bool IsZext1 = isa<ZExtOperator>(ICmp.getOperand(1));
 
-    // If we have mismatched casts, treat the zext of a non-negative source as
-    // a sext to simulate matching casts. Otherwise, we are done.
-    // TODO: Can we handle some predicates (equality) without non-negative?
     if (IsZext0 != IsZext1) {
+        // If X and Y and both i1
+        // (icmp eq/ne (zext X) (sext Y))
+        //      eq -> (icmp eq (or X, Y), 0)
+        //      ne -> (icmp ne (or X, Y), 0)
+      if (ICmp.isEquality() && X->getType()->isIntOrIntVectorTy(1) &&
+          Y->getType()->isIntOrIntVectorTy(1))
+        return new ICmpInst(ICmp.getPredicate(), Builder.CreateOr(X, Y),
+                            Constant::getNullValue(X->getType()));
+
+      // If we have mismatched casts, treat the zext of a non-negative source as
+      // a sext to simulate matching casts. Otherwise, we are done.
+      // TODO: Can we handle some predicates (equality) without non-negative?
       if ((IsZext0 && isKnownNonNegative(X, DL, 0, &AC, &ICmp, &DT)) ||
           (IsZext1 && isKnownNonNegative(Y, DL, 0, &AC, &ICmp, &DT)))
         IsSignedExt = true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154574.537613.patch
Type: text/x-patch
Size: 2760 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230706/e406bdf9/attachment.bin>


More information about the llvm-commits mailing list