Skip to content

[ValueTracking] Add support for usub.sat in `isKnownNonZero #87700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

goldsteinn
Copy link
Contributor

A missed case (that we infact had tests for already).

Proof: https://alive2.llvm.org/ce/z/54zsbS

A missed case (that we infact had tests for already).

Proof: https://alive2.llvm.org/ce/z/54zsbS
@goldsteinn goldsteinn requested a review from nikic as a code owner April 4, 2024 20:35
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Apr 4, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2024

@llvm/pr-subscribers-llvm-analysis

Author: None (goldsteinn)

Changes

A missed case (that we infact had tests for already).

Proof: https://alive2.llvm.org/ce/z/54zsbS


Full diff: https://github.com/llvm/llvm-project/pull/87700.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+14)
  • (modified) llvm/test/Analysis/ValueTracking/known-non-zero.ll (+1-5)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 5ad4da43bca7db..57115a78360c1b 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2816,6 +2816,20 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
       case Intrinsic::bswap:
       case Intrinsic::ctpop:
         return isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q);
+      case Intrinsic::usub_sat: {
+        KnownBits XKnown =
+            computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
+        if (XKnown.isUnknown())
+          break;
+        KnownBits YKnown =
+            computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
+        if (YKnown.isUnknown())
+          break;
+        std::optional<bool> NonZero = KnownBits::ugt(XKnown, YKnown);
+        if (NonZero.has_value())
+          return *NonZero;
+        break;
+      }
       case Intrinsic::ssub_sat:
         return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
                             II->getArgOperand(0), II->getArgOperand(1));
diff --git a/llvm/test/Analysis/ValueTracking/known-non-zero.ll b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
index 0159050d925c3e..00a0c07ede9763 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -882,11 +882,7 @@ define i1 @usub_sat_nonzero(i8 %xx, i8 %yy, i8 %ind) {
 ; CHECK-LABEL: @usub_sat_nonzero(
 ; CHECK-NEXT:    [[Y_ULT_31:%.*]] = icmp ult i8 [[YY:%.*]], 31
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[Y_ULT_31]])
-; CHECK-NEXT:    [[XO:%.*]] = or i8 [[XX:%.*]], 34
-; CHECK-NEXT:    [[X:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[XO]], i8 [[YY]])
-; CHECK-NEXT:    [[Z:%.*]] = or i8 [[X]], [[IND:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[Z]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %y_ult_31 = icmp ult i8 %yy, 31
   call void @llvm.assume(i1 %y_ult_31)

@goldsteinn goldsteinn requested a review from dtcxzyw April 4, 2024 20:36
dtcxzyw added a commit to dtcxzyw/llvm-opt-benchmark that referenced this pull request Apr 5, 2024
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not useful, but LGTM.

@@ -882,11 +882,7 @@ define i1 @usub_sat_nonzero(i8 %xx, i8 %yy, i8 %ind) {
; CHECK-LABEL: @usub_sat_nonzero(
; CHECK-NEXT: [[Y_ULT_31:%.*]] = icmp ult i8 [[YY:%.*]], 31
; CHECK-NEXT: call void @llvm.assume(i1 [[Y_ULT_31]])
; CHECK-NEXT: [[XO:%.*]] = or i8 [[XX:%.*]], 34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been handled in InstCombine by converting the usub.sat into sub nuw: https://godbolt.org/z/he76W3K3v

I think this patch just duplicates the logic in InstCombine:

// Make use of known overflow information.
OverflowResult OR = computeOverflow(SI->getBinaryOp(), SI->isSigned(),
Arg0, Arg1, SI);
switch (OR) {
case OverflowResult::MayOverflow:
break;
case OverflowResult::NeverOverflows:
if (SI->isSigned())
return BinaryOperator::CreateNSW(SI->getBinaryOp(), Arg0, Arg1);
else
return BinaryOperator::CreateNUW(SI->getBinaryOp(), Arg0, Arg1);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, good point. Actually, now that you mention it, I'm pretty sure that @goldsteinn already submitted a patch along these lines on phabricator and we reached the same conclusion there... Maybe we should leave a comment so we don't get a third implementation :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I forgot :)

I'll close this and push a comment

@goldsteinn goldsteinn closed this in b65ab0b Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants