[PATCH] D150425: [ValueTracking] deduce `X * Y != 0` if `LowestKnownBit(X) * LowestKnownBit(Y) != 0`

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 11 22:05:16 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: nikic, spatel, StephenFan, holland11.
Herald added a subscriber: hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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 second proof: https://alive2.llvm.org/ce/z/28C9CG

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D150425

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Analysis/ValueTracking/known-non-zero.ll


Index: llvm/test/Analysis/ValueTracking/known-non-zero.ll
===================================================================
--- llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -1099,11 +1099,7 @@
 
 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 [[X]], 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 %x, 8
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2887,6 +2887,17 @@
       return XKnown.isNonZero() ||
              isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q);
 
+    // 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.
+    if (XKnown.isNonZero() && YKnown.isNonZero()) {
+      APInt XLowBit = XKnown.One & (-XKnown.One);
+      APInt YLowBit = YKnown.One & (-YKnown.One);
+      if (!(XLowBit * YLowBit).isZero())
+        return true;
+    }
+
     return KnownBits::mul(XKnown, YKnown).isNonZero();
   }
   case Instruction::Select:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150425.521554.patch
Type: text/x-patch
Size: 1589 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230512/576805a1/attachment.bin>


More information about the llvm-commits mailing list