[llvm] 769952d - [ValueTracking] Implement Known{Bits,NonZero,FPClass} for `llvm.vector.reverse`

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 16 11:48:35 PDT 2024


Author: Noah Goldstein
Date: 2024-07-17T02:48:09+08:00
New Revision: 769952d72ff383e0e5fda46802fb8717712784d3

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

LOG: [ValueTracking] Implement Known{Bits,NonZero,FPClass} for `llvm.vector.reverse`

`llvm.vector.reverse` preserves each of the elements and thus elements
common to them.

Alive2 doesn't support the intrin yet, but the logic seems pretty
self-evident.

Closes #99013

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Analysis/ValueTracking/known-bits.ll
    llvm/test/Analysis/ValueTracking/known-fpclass.ll
    llvm/test/Analysis/ValueTracking/known-non-zero.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 68e585981673d..f8ec868398323 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1710,6 +1710,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
         computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
         Known = KnownBits::ssub_sat(Known, Known2);
         break;
+        // Vec reverse preserves bits from input vec.
+      case Intrinsic::vector_reverse:
         // for min/max/and/or reduce, any bit common to each element in the
         // input vec is set in the output.
       case Intrinsic::vector_reduce_and:
@@ -3090,6 +3092,8 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
         return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
                             II->getArgOperand(0), II->getArgOperand(1),
                             /*NSW=*/true, /* NUW=*/false);
+        // Vec reverse preserves zero/non-zero status from input vec.
+      case Intrinsic::vector_reverse:
         // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
       case Intrinsic::vector_reduce_or:
       case Intrinsic::vector_reduce_umax:
@@ -5227,6 +5231,11 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
         Known.SignBit.reset();
       break;
     }
+      // reverse preserves all characteristics of the input vec's element.
+    case Intrinsic::vector_reverse:
+      Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
+                                  InterestedClasses, Depth + 1, Q);
+      break;
     case Intrinsic::trunc:
     case Intrinsic::floor:
     case Intrinsic::ceil:

diff  --git a/llvm/test/Analysis/ValueTracking/known-bits.ll b/llvm/test/Analysis/ValueTracking/known-bits.ll
index 9491f2bcf3adf..035ccf8d42d13 100644
--- a/llvm/test/Analysis/ValueTracking/known-bits.ll
+++ b/llvm/test/Analysis/ValueTracking/known-bits.ll
@@ -3,10 +3,7 @@
 
 define <4 x i1> @vec_reverse_known_bits(<4 x i8> %xx) {
 ; CHECK-LABEL: @vec_reverse_known_bits(
-; CHECK-NEXT:    [[X:%.*]] = or <4 x i8> [[XX:%.*]], <i8 -128, i8 -128, i8 -128, i8 -128>
-; CHECK-NEXT:    [[REV:%.*]] = call <4 x i8> @llvm.vector.reverse.v4i8(<4 x i8> [[X]])
-; CHECK-NEXT:    [[R:%.*]] = icmp slt <4 x i8> [[REV]], zeroinitializer
-; CHECK-NEXT:    ret <4 x i1> [[R]]
+; CHECK-NEXT:    ret <4 x i1> <i1 true, i1 true, i1 true, i1 true>
 ;
   %x = or <4 x i8> %xx, <i8 128, i8 128, i8 128, i8 128>
   %rev = call <4 x i8> @llvm.vector.reverse(<4 x i8> %x)

diff  --git a/llvm/test/Analysis/ValueTracking/known-fpclass.ll b/llvm/test/Analysis/ValueTracking/known-fpclass.ll
index aafdf63ba7770..59f3eed715b52 100644
--- a/llvm/test/Analysis/ValueTracking/known-fpclass.ll
+++ b/llvm/test/Analysis/ValueTracking/known-fpclass.ll
@@ -3,10 +3,7 @@
 
 define <4 x i1> @vector_reverse_fpclass(<4 x double> nofpclass(nzero nan) %x) {
 ; CHECK-LABEL: @vector_reverse_fpclass(
-; CHECK-NEXT:    [[X_ABS:%.*]] = call <4 x double> @llvm.fabs.v4f64(<4 x double> [[X:%.*]])
-; CHECK-NEXT:    [[OP:%.*]] = call <4 x double> @llvm.vector.reverse.v4f64(<4 x double> [[X_ABS]])
-; CHECK-NEXT:    [[CMP:%.*]] = fcmp oge <4 x double> [[OP]], zeroinitializer
-; CHECK-NEXT:    ret <4 x i1> [[CMP]]
+; CHECK-NEXT:    ret <4 x i1> <i1 true, i1 true, i1 true, i1 true>
 ;
   %x.abs = call <4 x double> @llvm.fabs.v4f64(<4 x double> %x)
   %op = call <4 x double> @llvm.vector.reverse(<4 x double> %x.abs)

diff  --git a/llvm/test/Analysis/ValueTracking/known-non-zero.ll b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
index 3e6d1e0860645..5704586d92300 100644
--- a/llvm/test/Analysis/ValueTracking/known-non-zero.ll
+++ b/llvm/test/Analysis/ValueTracking/known-non-zero.ll
@@ -1499,10 +1499,7 @@ define i1 @trunc_nsw_nuw_non_zero_fail(i8 %xx) {
 
 define <4 x i1> @vec_reverse_non_zero(<4 x i8> %xx) {
 ; CHECK-LABEL: @vec_reverse_non_zero(
-; CHECK-NEXT:    [[X:%.*]] = add nuw <4 x i8> [[XX:%.*]], <i8 1, i8 1, i8 1, i8 1>
-; CHECK-NEXT:    [[REV:%.*]] = call <4 x i8> @llvm.vector.reverse.v4i8(<4 x i8> [[X]])
-; CHECK-NEXT:    [[R:%.*]] = icmp eq <4 x i8> [[REV]], zeroinitializer
-; CHECK-NEXT:    ret <4 x i1> [[R]]
+; CHECK-NEXT:    ret <4 x i1> zeroinitializer
 ;
   %x = add nuw <4 x i8> %xx, <i8 1, i8 1, i8 1, i8 1>
   %rev = call <4 x i8> @llvm.vector.reverse(<4 x i8> %x)


        


More information about the llvm-commits mailing list