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

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 27 23:19:33 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: StephenFan, nikic, spatel.
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 `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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149410

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
@@ -622,11 +622,7 @@
 
 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)
@@ -637,11 +633,7 @@
 
 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)
@@ -667,11 +659,7 @@
 
 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)
@@ -682,11 +670,7 @@
 
 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)
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2905,6 +2905,12 @@
             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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149410.517794.patch
Type: text/x-patch
Size: 2868 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230428/45ac91e0/attachment.bin>


More information about the llvm-commits mailing list