[PATCH] D142300: [InstSimplify] Simplify `icmp eq/ne/ugt/ult X, 0` -> true/false where `X` is known to be zero.

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 21 21:56:45 PST 2023


goldstein.w.n created this revision.
Herald added a subscriber: 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 case was previously missing as the above icmp conditions only
checked if `X` was known non-zero.

Later on `isKnownNonEqual` also doesn't run (to save compile time) as
its assumed that `simplifyICmpWithZero` handles the `icmp pred X, 0`
cases sufficiently.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142300

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/icmp-constant.ll


Index: llvm/test/Transforms/InstSimplify/icmp-constant.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/icmp-constant.ll
+++ llvm/test/Transforms/InstSimplify/icmp-constant.ll
@@ -1144,11 +1144,7 @@
 
 define i1 @cmp_ne_0_is_false(i32 %x) {
 ; CHECK-LABEL: @cmp_ne_0_is_false(
-; CHECK-NEXT:    [[Y:%.*]] = add i32 [[X:%.*]], 1
-; CHECK-NEXT:    [[Z:%.*]] = and i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[B:%.*]] = and i32 [[Z]], 1
-; CHECK-NEXT:    [[E:%.*]] = icmp ne i32 [[B]], 0
-; CHECK-NEXT:    ret i1 [[E]]
+; CHECK-NEXT:    ret i1 false
 ;
   %y = add i32 %x, 1
   %z = and i32 %x, %y
@@ -1159,11 +1155,7 @@
 
 define i1 @cmp_eq_0_is_true(i32 %x) {
 ; CHECK-LABEL: @cmp_eq_0_is_true(
-; CHECK-NEXT:    [[Y:%.*]] = add i32 [[X:%.*]], 1
-; CHECK-NEXT:    [[Z:%.*]] = and i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[B:%.*]] = and i32 [[Z]], 1
-; CHECK-NEXT:    [[E:%.*]] = icmp eq i32 [[B]], 0
-; CHECK-NEXT:    ret i1 [[E]]
+; CHECK-NEXT:    ret i1 true
 ;
   %y = add i32 %x, 1
   %z = and i32 %x, %y
@@ -1174,11 +1166,7 @@
 
 define <2 x i1> @cmp_ule_0_is_true(<2 x i32> %x) {
 ; CHECK-LABEL: @cmp_ule_0_is_true(
-; CHECK-NEXT:    [[Y:%.*]] = add <2 x i32> [[X:%.*]], <i32 1, i32 1>
-; CHECK-NEXT:    [[Z:%.*]] = and <2 x i32> [[X]], [[Y]]
-; CHECK-NEXT:    [[B:%.*]] = and <2 x i32> [[Z]], <i32 1, i32 1>
-; CHECK-NEXT:    [[E:%.*]] = icmp ule <2 x i32> [[B]], zeroinitializer
-; CHECK-NEXT:    ret <2 x i1> [[E]]
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
 ;
   %y = add <2 x i32> %x, <i32 1, i32 1>
   %z = and <2 x i32> %x, %y
@@ -1189,11 +1177,7 @@
 
 define <2 x i1> @cmp_ugt_0_is_false(<2 x i32> %x) {
 ; CHECK-LABEL: @cmp_ugt_0_is_false(
-; CHECK-NEXT:    [[Y:%.*]] = add <2 x i32> [[X:%.*]], <i32 1, i32 1>
-; CHECK-NEXT:    [[Z:%.*]] = and <2 x i32> [[X]], [[Y]]
-; CHECK-NEXT:    [[B:%.*]] = and <2 x i32> [[Z]], <i32 1, i32 1>
-; CHECK-NEXT:    [[E:%.*]] = icmp ugt <2 x i32> [[B]], zeroinitializer
-; CHECK-NEXT:    ret <2 x i1> [[E]]
+; CHECK-NEXT:    ret <2 x i1> zeroinitializer
 ;
   %y = add <2 x i32> %x, <i32 1, i32 1>
   %z = and <2 x i32> %x, %y
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -2979,15 +2979,21 @@
   case ICmpInst::ICMP_UGE:
     return getTrue(ITy);
   case ICmpInst::ICMP_EQ:
-  case ICmpInst::ICMP_ULE:
+  case ICmpInst::ICMP_ULE: {
     if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo))
       return getFalse(ITy);
-    break;
+    KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
+    if (LHSKnown.Zero.isAllOnes())
+      return getTrue(ITy);
+  } break;
   case ICmpInst::ICMP_NE:
-  case ICmpInst::ICMP_UGT:
+  case ICmpInst::ICMP_UGT: {
     if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo))
       return getTrue(ITy);
-    break;
+    KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
+    if (LHSKnown.Zero.isAllOnes())
+      return getFalse(ITy);
+  } break;
   case ICmpInst::ICMP_SLT: {
     KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
     if (LHSKnown.isNegative())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142300.491129.patch
Type: text/x-patch
Size: 3275 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230122/dbdb8212/attachment.bin>


More information about the llvm-commits mailing list