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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 1 addition & 5 deletions llvm/test/Analysis/ValueTracking/known-non-zero.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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

; 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)
Expand Down