Skip to content

[ValueTracking] For NUW, X + Y are not 0 if they are nonequal #148101

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 13 additions & 2 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2833,19 +2833,27 @@ static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
if (matchOpWithOpEqZero(X, Y))
return true;

if (NUW)
if (NUW) {
// X + Y != 0 if X != Y, and X + Y is NUW.
Copy link
Contributor

Choose a reason for hiding this comment

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

A simpler way to state this:

Suggested change
// X + Y != 0 if X != Y, and X + Y is NUW.
// If X != Y then X +nuw Y != 0.

if (isKnownNonEqual(X, Y, DemandedElts, Q, Depth))
return true;
return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
isKnownNonZero(X, DemandedElts, Q, Depth);
}

KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);

// If X and Y are both non-negative (as signed values) then their sum is not
// zero unless both X and Y are zero.
if (XKnown.isNonNegative() && YKnown.isNonNegative())
if (XKnown.isNonNegative() && YKnown.isNonNegative()) {
if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
isKnownNonZero(X, DemandedElts, Q, Depth))
return true;
// X + Y != 0 if X != Y, and X + Y are both nonnegative.
if (isKnownNonEqual(X, Y, DemandedElts, Q, Depth))
return true;
}

// If X and Y are both negative (as signed values) then their sum is not
// zero unless both X and Y equal INT_MIN.
Expand All @@ -2859,6 +2867,9 @@ static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
// to INT_MIN.
if (YKnown.One.intersects(Mask))
return true;
// X + Y != 0 because if they are not equal, they cannot both be INT_MIN.
if (isKnownNonEqual(X, Y, DemandedElts, Q, Depth))
return true;
}

// The sum of a non-negative number and a power of two is not zero.
Expand Down
Loading