[llvm] ea5a0d4 - [ValueTracking] Add logic for `isKnownNonZero(ctlz/cttz X)`

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


Author: Noah Goldstein
Date: 2023-04-30T10:06:45-05:00
New Revision: ea5a0d4b909fb71376686a3c34bbaf05ed47fb3f

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

LOG: [ValueTracking] Add logic for `isKnownNonZero(ctlz/cttz X)`

for `cttz` if `X[0]` is non-zero, then the expression is non-zero.
for `ctlz` if `X[SignBit]` is non-zero, then the expression in
non-zero.

Alive2 Links:
    cttz (false): https://alive2.llvm.org/ce/z/ySQzbg
    cttz (true): https://alive2.llvm.org/ce/z/auiTCJ
    ctlz (false): https://alive2.llvm.org/ce/z/yk3sTJ
    ctlz (true): https://alive2.llvm.org/ce/z/-JuDty

Reviewed By: nikic

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

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 9fb044b74580..da369c91c0c0 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2932,6 +2932,12 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
             isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q))
           return true;
         break;
+      case Intrinsic::cttz:
+        return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
+            .Zero[0];
+      case Intrinsic::ctlz:
+        return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
+            .isNonNegative();
       case Intrinsic::fshr:
       case Intrinsic::fshl:
         // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.

diff  --git a/llvm/test/Analysis/ValueTracking/known-non-zero.ll b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
index 2ae4ba08d947..52f809cd13cf 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -693,11 +693,7 @@ define i1 @bitcast_nonzero_fail_dont_check_float(float %xx, i32 %ind) {
 
 define i1 @ctlz_true_nonzero(i8 %xx, i8 %ind) {
 ; CHECK-LABEL: @ctlz_true_nonzero(
-; CHECK-NEXT:    [[XS:%.*]] = lshr i8 [[XX:%.*]], 1
-; CHECK-NEXT:    [[X:%.*]] = call i8 @llvm.ctlz.i8(i8 [[XS]], i1 true)
-; 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
 ;
   %xs = lshr i8 %xx, 1
   %x = call i8 @llvm.ctlz.i8(i8 %xs, i1 true)
@@ -708,11 +704,7 @@ define i1 @ctlz_true_nonzero(i8 %xx, i8 %ind) {
 
 define i1 @ctlz_false_nonzero(i8 %xx, i8 %ind) {
 ; CHECK-LABEL: @ctlz_false_nonzero(
-; CHECK-NEXT:    [[XA:%.*]] = and i8 [[XX:%.*]], 127
-; CHECK-NEXT:    [[X:%.*]] = call i8 @llvm.ctlz.i8(i8 [[XA]], i1 true)
-; 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
 ;
   %xa = and i8 %xx, 127
   %x = call i8 @llvm.ctlz.i8(i8 %xa, i1 true)
@@ -738,11 +730,7 @@ define i1 @ctlz_nonzero_fail_maybe_neg(i8 %xx, i8 %ind) {
 
 define i1 @cttz_true_nonzero(i8 %xx, i8 %ind) {
 ; CHECK-LABEL: @cttz_true_nonzero(
-; CHECK-NEXT:    [[XS:%.*]] = shl i8 [[XX:%.*]], 1
-; CHECK-NEXT:    [[X:%.*]] = call i8 @llvm.cttz.i8(i8 [[XS]], i1 true)
-; 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
 ;
   %xs = shl i8 %xx, 1
   %x = call i8 @llvm.cttz.i8(i8 %xs, i1 true)
@@ -753,11 +741,7 @@ define i1 @cttz_true_nonzero(i8 %xx, i8 %ind) {
 
 define i1 @cttz_false_nonzero(i8 %xx, i8 %ind) {
 ; CHECK-LABEL: @cttz_false_nonzero(
-; CHECK-NEXT:    [[XA:%.*]] = and i8 [[XX:%.*]], -2
-; CHECK-NEXT:    [[X:%.*]] = call i8 @llvm.cttz.i8(i8 [[XA]], i1 true)
-; 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
 ;
   %xa = and i8 %xx, -2
   %x = call i8 @llvm.cttz.i8(i8 %xa, i1 true)


        


More information about the llvm-commits mailing list