[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
Fri Apr 7 19:49:14 PDT 2023


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

Add some comments


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,8 +4631,7 @@
 define i1 @mul_or_constant_sgt(i8 %a, i8 %b) {
 ; CHECK-LABEL: @mul_or_constant_sgt(
 ; CHECK-NEXT:    [[MUL1:%.*]] = mul 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 i8 %a, %b
@@ -4644,8 +4643,7 @@
 define <2 x i1> @mul_or_constant_vec_sgt(<2 x i8> %a, <2 x i8> %b) {
 ; CHECK-LABEL: @mul_or_constant_vec_sgt(
 ; CHECK-NEXT:    [[MUL1:%.*]] = mul <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 +4667,7 @@
 define i1 @mul_or_constant_sle(i8 %a, i8 %b) {
 ; CHECK-LABEL: @mul_or_constant_sle(
 ; CHECK-NEXT:    [[MUL1:%.*]] = mul 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 i8 %a, %b
@@ -4682,8 +4679,7 @@
 define <2 x i1> @mul_or_constant_vec_sle(<2 x i8> %a, <2 x i8> %b) {
 ; CHECK-LABEL: @mul_or_constant_vec_sle(
 ; CHECK-NEXT:    [[MUL1:%.*]] = mul <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,27 @@
     return new ICmpInst(NewPred, X, NewC);
   }
 
+  const APInt* LHS;
+  // icmp(X | LHS, C) --> icmp(X) iff LHS > C s>= 0
+  if (match(Or, m_c_Or(m_Value(X), m_APInt(LHS)))) {
+    if (LHS->isStrictlyPositive() && C.isNonNegative() && LHS->sgt(C)) {
+      switch (Pred) {
+      // X | RHS s<= C --> X s< C
+      case ICmpInst::ICMP_SLE:
+      // X | RHS s< C --> X s< C
+      case ICmpInst::ICMP_SLT:
+        return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantInt::getNullValue(X->getType()));
+      // X | RHS s>= C --> X s>= C
+      case ICmpInst::ICMP_SGE:
+      // X | RHS s> C --> X s>= C
+      case ICmpInst::ICMP_SGT:
+        return new ICmpInst(ICmpInst::ICMP_SGE, X, ConstantInt::getNullValue(X->getType()));
+      default:
+        break;
+      }
+    }
+  }
+
   if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
     return nullptr;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147597.511838.patch
Type: text/x-patch
Size: 3062 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230408/dc70931a/attachment.bin>


More information about the llvm-commits mailing list