[llvm] 5eedfff - [ValueTracking] Add additional cases for `isKnownNonZero(mul X, Y)`

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 30 08:07:24 PDT 2023


Author: Noah Goldstein
Date: 2023-04-30T10:06:46-05:00
New Revision: 5eedfff695868087452b3272a42fa1a22b5d610e

URL: https://github.com/llvm/llvm-project/commit/5eedfff695868087452b3272a42fa1a22b5d610e
DIFF: https://github.com/llvm/llvm-project/commit/5eedfff695868087452b3272a42fa1a22b5d610e.diff

LOG: [ValueTracking] Add additional cases for `isKnownNonZero(mul X, Y)`

If either `X` or `Y` is odd and the other is non-zero, the result is
non-zero.

Alive2 Link:
    https://alive2.llvm.org/ce/z/9V7-es

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149418

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 fa267cef3d1f..41fabbd48d8b 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2872,11 +2872,24 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
     // If X and Y are non-zero then so is X * Y as long as the multiplication
     // does not overflow.
     const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
-    if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) &&
-        isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) &&
-        isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q))
-      return true;
-    break;
+    if (Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO))
+      return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) &&
+             isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q);
+
+    // If either X or Y is odd, then if the other is non-zero the result can't
+    // be zero.
+    KnownBits XKnown =
+        computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
+    if (XKnown.One[0])
+      return isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q);
+
+    KnownBits YKnown =
+        computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
+    if (YKnown.One[0])
+      return XKnown.isNonZero() ||
+             isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q);
+
+    return KnownBits::mul(XKnown, YKnown).isNonZero();
   }
   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 6e688b13a089..6b7ecdcc6f68 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -767,13 +767,9 @@ define i1 @cttz_nonzero_fail_maybe_odd(i8 %xx, i8 %cnt, i8 %ind) {
 
 define i1 @mul_nonzero_odd(i8 %xx, i8 %y, i8 %ind) {
 ; CHECK-LABEL: @mul_nonzero_odd(
-; CHECK-NEXT:    [[XO:%.*]] = or i8 [[XX:%.*]], 1
 ; CHECK-NEXT:    [[Y_NZ:%.*]] = icmp ne i8 [[Y:%.*]], 0
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[Y_NZ]])
-; CHECK-NEXT:    [[X:%.*]] = mul i8 [[XO]], [[Y]]
-; CHECK-NEXT:    [[Z:%.*]] = or i8 [[X]], [[IND:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[Z]], 0
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %xo = or i8 %xx, 1
   %y_nz = icmp ne i8 %y, 0


        


More information about the llvm-commits mailing list