Skip to content

[ValueTracking] improve isKnownNonZero precision for smax #88170

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 2 commits 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
41 changes: 31 additions & 10 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2828,23 +2828,44 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
case Intrinsic::uadd_sat:
return isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q) ||
isKnownNonZero(II->getArgOperand(0), DemandedElts, Depth, Q);
case Intrinsic::smin:
case Intrinsic::smax: {
auto KnownOpImpliesNonZero = [&](const KnownBits &K) {
return II->getIntrinsicID() == Intrinsic::smin
? K.isNegative()
: K.isStrictlyPositive();
// If either arg is strictly positive the result is non-zero. Otherwise
// the result is non-zero if both ops are non-zero.
auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
KnownBits OpKnown) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
KnownBits OpKnown) {
const KnownBits &OpKnown) {

if (!OpNonZero.has_value())
OpNonZero = OpKnown.isNonZero() ||
isKnownNonZero(Op, DemandedElts, Depth, Q);
return *OpNonZero;
};
KnownBits XKnown =
// Avoid re-computing isKnownNonZero.
std::optional<bool> Op0NonZero, Op1NonZero;
KnownBits Op1Known =
computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
if (Op1Known.isNonNegative() &&
IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
return true;
KnownBits Op0Known =
computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
if (KnownOpImpliesNonZero(XKnown))
if (Op0Known.isNonNegative() &&
IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
return true;
KnownBits YKnown =
return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
}
case Intrinsic::smin: {
// If either arg is negative the result is non-zero. Otherwise
// the result is non-zero if both ops are non-zero.
KnownBits Op1Known =
computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
if (KnownOpImpliesNonZero(YKnown))
if (Op1Known.isNegative())
return true;
KnownBits Op0Known =
computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
if (Op0Known.isNegative())
return true;

if (XKnown.isNonZero() && YKnown.isNonZero())
if (Op1Known.isNonZero() && Op0Known.isNonZero())
return true;
}
[[fallthrough]];
Expand Down
11 changes: 11 additions & 0 deletions llvm/test/Transforms/InstSimplify/known-non-zero.ll
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,14 @@ A:
B:
ret i1 0
}

define i1 @smax_non_zero(i8 %xx, i8 %y) {
; CHECK-LABEL: @smax_non_zero(
; CHECK-NEXT: ret i1 false
;
%x0 = and i8 %xx, 63
%x = add i8 %x0, 1
%v = call i8 @llvm.smax.i8(i8 %x, i8 %y)
%r = icmp eq i8 %v, 0
ret i1 %r
}