-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
A missed case (that we infact had tests for already). Proof: https://alive2.llvm.org/ce/z/54zsbS
@llvm/pr-subscribers-llvm-analysis Author: None (goldsteinn) ChangesA 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:
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)
|
There was a problem hiding this 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 |
There was a problem hiding this comment.
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:
llvm-project/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Lines 2150 to 2160 in 8461d90
// 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); |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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
A missed case (that we infact had tests for already).
Proof: https://alive2.llvm.org/ce/z/54zsbS