Skip to content

[ValueTracking] Implement Known{Bits,NonZero,FPClass} for llvm.vector.reverse #99013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,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:
Expand Down Expand Up @@ -3026,6 +3028,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:
Expand Down Expand Up @@ -5163,6 +5167,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:
Expand Down
25 changes: 25 additions & 0 deletions llvm/test/Analysis/ValueTracking/known-bits.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=instsimplify < %s -S | FileCheck %s

define <4 x i1> @vec_reverse_known_bits(<4 x i8> %xx) {
; CHECK-LABEL: @vec_reverse_known_bits(
; 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)
%r = icmp slt <4 x i8> %rev, zeroinitializer
ret <4 x i1> %r
}

define <4 x i1> @vec_reverse_known_bits_fail(<4 x i8> %xx) {
; CHECK-LABEL: @vec_reverse_known_bits_fail(
; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], <i8 -128, i8 -128, i8 -128, i8 127>
; 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]]
;
%x = or <4 x i8> %xx, <i8 128, i8 128, i8 128, i8 127>
%rev = call <4 x i8> @llvm.vector.reverse(<4 x i8> %x)
%r = icmp slt <4 x i8> %rev, zeroinitializer
ret <4 x i1> %r
}
26 changes: 26 additions & 0 deletions llvm/test/Analysis/ValueTracking/known-fpclass.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=instsimplify < %s -S | FileCheck %s

define <4 x i1> @vector_reverse_fpclass(<4 x double> nofpclass(nzero nan) %x) {
; CHECK-LABEL: @vector_reverse_fpclass(
; 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)
%cmp = fcmp oge <4 x double> %op, <double 0.0, double 0.0, double 0.0, double 0.0>
ret <4 x i1> %cmp
}

define <4 x i1> @vector_reverse_fpclass2(<4 x double> nofpclass(nzero) %x) {
; CHECK-LABEL: @vector_reverse_fpclass2(
; 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]]
;
%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)
%cmp = fcmp oge <4 x double> %op, <double 0.0, double 0.0, double 0.0, double 0.0>
ret <4 x i1> %cmp
}

23 changes: 23 additions & 0 deletions llvm/test/Analysis/ValueTracking/known-non-zero.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1497,4 +1497,27 @@ define i1 @trunc_nsw_nuw_non_zero_fail(i8 %xx) {
ret i1 %r
}

define <4 x i1> @vec_reverse_non_zero(<4 x i8> %xx) {
; CHECK-LABEL: @vec_reverse_non_zero(
; 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)
%r = icmp eq <4 x i8> %rev, zeroinitializer
ret <4 x i1> %r
}

define <4 x i1> @vec_reverse_non_zero_fail(<4 x i8> %xx) {
; CHECK-LABEL: @vec_reverse_non_zero_fail(
; CHECK-NEXT: [[X:%.*]] = add nuw <4 x i8> [[XX:%.*]], <i8 1, i8 0, 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]]
;
%x = add nuw <4 x i8> %xx, <i8 1, i8 0, i8 1, i8 1>
%rev = call <4 x i8> @llvm.vector.reverse(<4 x i8> %x)
%r = icmp eq <4 x i8> %rev, zeroinitializer
ret <4 x i1> %r
}

declare i32 @llvm.experimental.get.vector.length.i32(i32, i32, i1)
Loading