[llvm] 294726d - Reapply "[InstCombine] Folding `(icmp eq/ne (and X, -P2), INT_MIN)`" (#111236)

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 23 07:12:22 PDT 2024


Author: Noah Goldstein
Date: 2024-10-23T09:12:08-05:00
New Revision: 294726d738c47cc9df41618862f235fd985642d7

URL: https://github.com/llvm/llvm-project/commit/294726d738c47cc9df41618862f235fd985642d7
DIFF: https://github.com/llvm/llvm-project/commit/294726d738c47cc9df41618862f235fd985642d7.diff

LOG: Reapply "[InstCombine] Folding `(icmp eq/ne (and X, -P2), INT_MIN)`" (#111236)

The underlying issue with msan was fixed by #113200

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/test/Transforms/InstCombine/and-or-icmps.ll
    llvm/test/Transforms/InstCombine/icmp-signmask.ll
    llvm/test/Transforms/InstCombine/icmp.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index d9d41e052a32a6..338e9772c7cc08 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5013,6 +5013,18 @@ Instruction *InstCombinerImpl::foldICmpBinOp(ICmpInst &I,
     }
   }
 
+  // (icmp eq/ne (X, -P2), INT_MIN)
+  //	-> (icmp slt/sge X, INT_MIN + P2)
+  if (ICmpInst::isEquality(Pred) && BO0 &&
+      match(I.getOperand(1), m_SignMask()) &&
+      match(BO0, m_And(m_Value(), m_NegatedPower2OrZero()))) {
+    // Will Constant fold.
+    Value *NewC = Builder.CreateSub(I.getOperand(1), BO0->getOperand(1));
+    return new ICmpInst(Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_SLT
+                                                  : ICmpInst::ICMP_SGE,
+                        BO0->getOperand(0), NewC);
+  }
+
   {
     // Similar to above: an unsigned overflow comparison may use offset + mask:
     // ((Op1 + C) & C) u<  Op1 --> Op1 != 0

diff  --git a/llvm/test/Transforms/InstCombine/and-or-icmps.ll b/llvm/test/Transforms/InstCombine/and-or-icmps.ll
index eb4723c86542de..2b5d430e295757 100644
--- a/llvm/test/Transforms/InstCombine/and-or-icmps.ll
+++ b/llvm/test/Transforms/InstCombine/and-or-icmps.ll
@@ -3335,8 +3335,7 @@ define i1 @icmp_eq_or_z_or_pow2orz_fail_bad_pred2(i8 %x, i8 %y) {
 
 define i1 @and_slt_to_mask(i8 %x) {
 ; CHECK-LABEL: @and_slt_to_mask(
-; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X:%.*]], -2
-; CHECK-NEXT:    [[AND2:%.*]] = icmp eq i8 [[TMP1]], -128
+; CHECK-NEXT:    [[AND2:%.*]] = icmp slt i8 [[X:%.*]], -126
 ; CHECK-NEXT:    ret i1 [[AND2]]
 ;
   %cmp = icmp slt i8 %x, -124

diff  --git a/llvm/test/Transforms/InstCombine/icmp-signmask.ll b/llvm/test/Transforms/InstCombine/icmp-signmask.ll
index bea8da2074ab0b..5424f7d7e8021f 100644
--- a/llvm/test/Transforms/InstCombine/icmp-signmask.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-signmask.ll
@@ -3,8 +3,7 @@
 
 define i1 @cmp_x_and_negp2_with_eq(i8 %x) {
 ; CHECK-LABEL: @cmp_x_and_negp2_with_eq(
-; CHECK-NEXT:    [[ANDX:%.*]] = and i8 [[X:%.*]], -2
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[ANDX]], -128
+; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[X:%.*]], -126
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %andx = and i8 %x, -2
@@ -25,8 +24,7 @@ define i1 @cmp_x_and_negp2_with_eq_fail_not_signmask(i8 %x) {
 
 define <2 x i1> @cmp_x_and_negp2_with_ne(<2 x i8> %x) {
 ; CHECK-LABEL: @cmp_x_and_negp2_with_ne(
-; CHECK-NEXT:    [[ANDX:%.*]] = and <2 x i8> [[X:%.*]], <i8 -8, i8 -16>
-; CHECK-NEXT:    [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], <i8 -128, i8 -128>
+; CHECK-NEXT:    [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 -121, i8 -113>
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %andx = and <2 x i8> %x, <i8 -8, i8 -16>
@@ -36,8 +34,7 @@ define <2 x i1> @cmp_x_and_negp2_with_ne(<2 x i8> %x) {
 
 define <2 x i1> @cmp_x_and_negp2_with_ne_or_z(<2 x i8> %x) {
 ; CHECK-LABEL: @cmp_x_and_negp2_with_ne_or_z(
-; CHECK-NEXT:    [[ANDX:%.*]] = and <2 x i8> [[X:%.*]], <i8 0, i8 -16>
-; CHECK-NEXT:    [[R:%.*]] = icmp ne <2 x i8> [[ANDX]], <i8 -128, i8 -128>
+; CHECK-NEXT:    [[R:%.*]] = icmp sge <2 x i8> [[X:%.*]], <i8 -128, i8 -112>
 ; CHECK-NEXT:    ret <2 x i1> [[R]]
 ;
   %andx = and <2 x i8> %x, <i8 0, i8 -16>

diff  --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll
index c695dc1cd69c8b..d52a0b76979373 100644
--- a/llvm/test/Transforms/InstCombine/icmp.ll
+++ b/llvm/test/Transforms/InstCombine/icmp.ll
@@ -1116,8 +1116,7 @@ define i1 @test53(i32 %a, i32 %b) {
 
 define i1 @test54(i8 %a) {
 ; CHECK-LABEL: @test54(
-; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[A:%.*]], -64
-; CHECK-NEXT:    [[RET:%.*]] = icmp eq i8 [[TMP1]], -128
+; CHECK-NEXT:    [[RET:%.*]] = icmp slt i8 [[A:%.*]], -64
 ; CHECK-NEXT:    ret i1 [[RET]]
 ;
   %ext = zext i8 %a to i32


        


More information about the llvm-commits mailing list