[llvm] 774ecc2 - [ValueTracking] deduce `X * Y != 0` if `LowestKnownBit(X) * LowestKnownBit(Y) != 0`
Noah Goldstein via llvm-commits
llvm-commits at lists.llvm.org
Tue May 16 16:58:40 PDT 2023
Author: Noah Goldstein
Date: 2023-05-16T18:58:12-05:00
New Revision: 774ecc20e121e238099ef5ef8efad3ed062a4ae5
URL: https://github.com/llvm/llvm-project/commit/774ecc20e121e238099ef5ef8efad3ed062a4ae5
DIFF: https://github.com/llvm/llvm-project/commit/774ecc20e121e238099ef5ef8efad3ed062a4ae5.diff
LOG: [ValueTracking] deduce `X * Y != 0` if `LowestKnownBit(X) * LowestKnownBit(Y) != 0`
For `X * Y`, if there exists a subset of `X` and subset of `Y` s.t `sX * sY != 0`,
then `X * Y != 0`.
- See first proof: https://alive2.llvm.org/ce/z/28C9CG
- NB: This is why the previous Odd case works.
In knownbits we could exhaustively hunt for such a subset, but
`LSB(X)` and `LSB(Y)` actually works. If `LSB(X) * LSB(Y) != 0`, then
`X * Y != 0`
- See proof: https://alive2.llvm.org/ce/z/p5wWid
In `isKnownNonZero` we can use this as if the `LowestKnownOne(X) *
LowestKnownOne(Y) != 0`, then `X * Y != 0`, and we don't need to try
and other subsets.
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D150425
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Analysis/ValueTracking/known-non-zero.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 1085be6986db0..2862e429dc140 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2884,7 +2884,13 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
return XKnown.isNonZero() ||
isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q);
- return KnownBits::mul(XKnown, YKnown).isNonZero();
+ // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
+ // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
+ // the the lowest known One of X and Y. If they are non-zero, the result
+ // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
+ // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
+ return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
+ BitWidth;
}
case Instruction::Select:
// (C ? X : Y) != 0 if X != 0 and Y != 0.
diff --git a/llvm/test/Analysis/ValueTracking/known-non-zero.ll b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
index fca016bfbdee6..cc77caa29f699 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -1099,11 +1099,7 @@ define i1 @smax_nonzero_pos_arg_fail_nonstrict_pos(i8 %xx, i8 %yy, i8 %ind) {
define i1 @mul_nonzero_contains_nonzero_mul(i8 %x, i8 %y) {
; CHECK-LABEL: @mul_nonzero_contains_nonzero_mul(
-; CHECK-NEXT: [[XX:%.*]] = or i8 [[X:%.*]], 16
-; CHECK-NEXT: [[YY:%.*]] = or i8 [[Y:%.*]], 8
-; CHECK-NEXT: [[XY:%.*]] = mul i8 [[XX]], [[YY]]
-; CHECK-NEXT: [[NZ:%.*]] = icmp ne i8 [[XY]], 0
-; CHECK-NEXT: ret i1 [[NZ]]
+; CHECK-NEXT: ret i1 true
;
%xx = or i8 %x, 16
%yy = or i8 %y, 8
More information about the llvm-commits
mailing list