-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[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
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-llvm-analysis @llvm/pr-subscribers-llvm-selectiondag Author: AZero13 (AZero13) ChangesIn NUW, X + Y is not 0 if X and Y are nonequal. Full diff: https://github.com/llvm/llvm-project/pull/148101.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 21f844c4d2f45..154dce6f1dfd5 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -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.
+ 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.
@@ -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.
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index c1356239ad206..bd17b1d570858 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -6015,10 +6015,15 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
break;
case ISD::ADD:
- if (Op->getFlags().hasNoUnsignedWrap())
+ if (Op->getFlags().hasNoUnsignedWrap()) {
if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
isKnownNeverZero(Op.getOperand(0), Depth + 1))
return true;
+ std::optional<bool> ne =
+ KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
+ computeKnownBits(Op.getOperand(1), Depth + 1));
+ return ne && *ne;
+ }
// TODO: There are a lot more cases we can prove for add.
break;
|
In NUW, X + Y is not 0 if X and Y are nonequal.
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.
Missing tests
@@ -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. |
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.
A simpler way to state this:
// X + Y != 0 if X != Y, and X + Y is NUW. | |
// If X != Y then X +nuw Y != 0. |
In NUW, X + Y is not 0 if X and Y are nonequal.