[PATCH] D147597: [InstCombine] Fold icmp(bin(X, Y) | LHS, RHS) --> icmp(bin(X, Y)) iff LHS > RHS s>= 0

Jun Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 6 00:58:31 PDT 2023


junaire updated this revision to Diff 511304.
junaire added a comment.

Move code to foldICmporConstant


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147597/new/

https://reviews.llvm.org/D147597

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


Index: llvm/test/Transforms/InstCombine/icmp.ll
===================================================================
--- llvm/test/Transforms/InstCombine/icmp.ll
+++ llvm/test/Transforms/InstCombine/icmp.ll
@@ -4631,10 +4631,10 @@
 define i1 @mul_add_constant_sgt(i8 %a, i8 %b) {
 ; CHECK-LABEL: @mul_add_constant_sgt(
 ; CHECK-NEXT:    [[MUL1:%.*]] = mul nsw i8 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    [[ADD:%.*]] = or i8 [[MUL1]], 24
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[ADD]], 0
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt i8 [[MUL1]], -1
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
+
   %mul1 = mul nsw i8 %a, %b
   %add = or i8 %mul1, 24
   %cmp = icmp sgt i8 %add, 0
@@ -4644,8 +4644,7 @@
 define <2 x i1> @mul_add_constant_vec_sgt(<2 x i8> %a, <2 x i8> %b) {
 ; CHECK-LABEL: @mul_add_constant_vec_sgt(
 ; CHECK-NEXT:    [[MUL1:%.*]] = mul nsw <2 x i8> [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    [[ADD:%.*]] = or <2 x i8> [[MUL1]], <i8 24, i8 24>
-; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt <2 x i8> [[ADD]], zeroinitializer
+; CHECK-NEXT:    [[CMP:%.*]] = icmp sgt <2 x i8> [[MUL1]], <i8 -1, i8 -1>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
 
@@ -4669,8 +4668,7 @@
 define i1 @mul_add_constant_sle(i8 %a, i8 %b) {
 ; CHECK-LABEL: @mul_add_constant_sle(
 ; CHECK-NEXT:    [[MUL1:%.*]] = mul nsw i8 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    [[ADD:%.*]] = or i8 [[MUL1]], 24
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[ADD]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[MUL1]], 0
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %mul1 = mul nsw i8 %a, %b
@@ -4682,8 +4680,7 @@
 define <2 x i1> @mul_add_constant_vec_sle(<2 x i8> %a, <2 x i8> %b) {
 ; CHECK-LABEL: @mul_add_constant_vec_sle(
 ; CHECK-NEXT:    [[MUL1:%.*]] = mul nsw <2 x i8> [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT:    [[ADD:%.*]] = or <2 x i8> [[MUL1]], <i8 24, i8 24>
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt <2 x i8> [[ADD]], <i8 1, i8 1>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp slt <2 x i8> [[MUL1]], zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
 
Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1953,6 +1953,24 @@
     return new ICmpInst(NewPred, X, NewC);
   }
 
+  BinaryOperator* BO;
+  const APInt* LHS;
+  // icmp(bin(X, Y) | LHS, C) --> icmp(bin(X, Y)) iff LHS > C s>= 0
+  if (match(Or, m_c_Or(m_BinOp(BO), m_APInt(LHS)))) {
+    if (LHS->isStrictlyPositive() && C.isNonNegative() && LHS->sgt(C)) {
+      switch (Pred) {
+      case ICmpInst::ICMP_SLE:
+      case ICmpInst::ICMP_SLT:
+        return new ICmpInst(ICmpInst::ICMP_SLT, BO, ConstantInt::getNullValue(BO->getType()));
+      case ICmpInst::ICMP_SGE:
+      case ICmpInst::ICMP_SGT:
+        return new ICmpInst(ICmpInst::ICMP_SGE, BO, ConstantInt::getNullValue(BO->getType()));
+      default:
+        break;
+      }
+    }
+  }
+
   if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
     return nullptr;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147597.511304.patch
Type: text/x-patch
Size: 3058 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230406/8873482b/attachment.bin>


More information about the llvm-commits mailing list