diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index ca48cfe773815..4120876889dec 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -1621,6 +1621,14 @@ static void computeKnownBitsFromOperator(const Operator *I, computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); Known = KnownBits::ssub_sat(Known, Known2); break; + // for min/max reduce, any bit common to each element in the input vec + // is set in the output. + case Intrinsic::vector_reduce_umax: + case Intrinsic::vector_reduce_umin: + case Intrinsic::vector_reduce_smax: + case Intrinsic::vector_reduce_smin: + computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); + break; case Intrinsic::umin: computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); @@ -2824,6 +2832,12 @@ static bool isKnownNonZeroFromOperator(const Operator *I, return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/true, /* NUW=*/false); + // umin/smin/smax/smin of all non-zero elements is always non-zero. + case Intrinsic::vector_reduce_umax: + case Intrinsic::vector_reduce_umin: + case Intrinsic::vector_reduce_smax: + case Intrinsic::vector_reduce_smin: + return isKnownNonZero(II->getArgOperand(0), Depth, Q); case Intrinsic::umax: case Intrinsic::uadd_sat: return isKnownNonZero(II->getArgOperand(1), DemandedElts, Depth, Q) || diff --git a/llvm/test/Transforms/InstCombine/vector-reduce-min-max-known.ll b/llvm/test/Transforms/InstCombine/vector-reduce-min-max-known.ll new file mode 100644 index 0000000000000..65d0008353262 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/vector-reduce-min-max-known.ll @@ -0,0 +1,274 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -passes=instcombine -S | FileCheck %s + +define i1 @vec_reduce_umax_non_zero(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umax_non_zero( +; CHECK-NEXT: [[X:%.*]] = add nuw <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umax.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[V]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %x = add nuw <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umax(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i1 @vec_reduce_umax_non_zero_fail(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umax_non_zero_fail( +; CHECK-NEXT: [[X:%.*]] = add nsw <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umax.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[V]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %x = add nsw <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umax(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i1 @vec_reduce_umin_non_zero(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umin_non_zero( +; CHECK-NEXT: ret i1 false +; + %x = add nuw <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umin(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i1 @vec_reduce_umin_non_zero_fail(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umin_non_zero_fail( +; CHECK-NEXT: [[X:%.*]] = add nuw <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umin.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[V]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %x = add nuw <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umin(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i1 @vec_reduce_smax_non_zero0(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smax_non_zero0( +; CHECK-NEXT: ret i1 false +; + %x = add nuw <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.smax(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i1 @vec_reduce_smax_non_zero1(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smax_non_zero1( +; CHECK-NEXT: [[X0:%.*]] = and <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[X0]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.smax.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[V]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %x0 = and <4 x i8> %xx, + %x = or <4 x i8> %x0, + %v = call i8 @llvm.vector.reduce.smax(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i1 @vec_reduce_smax_non_zero_fail(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smax_non_zero_fail( +; CHECK-NEXT: [[X0:%.*]] = and <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[X:%.*]] = add nuw <4 x i8> [[X0]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.smax.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[V]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %x0 = and <4 x i8> %xx, + %x = add nuw <4 x i8> %x0, + %v = call i8 @llvm.vector.reduce.smax(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i1 @vec_reduce_smin_non_zero0(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smin_non_zero0( +; CHECK-NEXT: ret i1 false +; + %x = add nuw <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.smin(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i1 @vec_reduce_smin_non_zero1(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smin_non_zero1( +; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.smin.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[V]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.smin(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i1 @vec_reduce_smin_non_zero_fail(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smin_non_zero_fail( +; CHECK-NEXT: [[X0:%.*]] = or <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[X:%.*]] = add <4 x i8> [[X0]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.smin.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[V]], 0 +; CHECK-NEXT: ret i1 [[R]] +; + %x0 = or <4 x i8> %xx, + %x = add <4 x i8> %x0, + %v = call i8 @llvm.vector.reduce.smin(<4 x i8> %x) + %r = icmp eq i8 %v, 0 + ret i1 %r +} + +define i8 @vec_reduce_umax_known0(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umax_known0( +; CHECK-NEXT: ret i8 1 +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umax(<4 x i8> %x) + %r = and i8 %v, 1 + ret i8 %r +} + +define i8 @vec_reduce_umax_known1(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umax_known1( +; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umax.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], -128 +; CHECK-NEXT: ret i8 [[R]] +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umax(<4 x i8> %x) + %r = and i8 %v, 128 + ret i8 %r +} + +define i8 @vec_reduce_umax_known_fail0(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umax_known_fail0( +; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umax.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1 +; CHECK-NEXT: ret i8 [[R]] +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umax(<4 x i8> %x) + %r = and i8 %v, 1 + ret i8 %r +} + +define i8 @vec_reduce_umax_known_fail1(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umax_known_fail1( +; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umax.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1 +; CHECK-NEXT: ret i8 [[R]] +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umax(<4 x i8> %x) + %r = and i8 %v, 1 + ret i8 %r +} + +define i8 @vec_reduce_umin_known0(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umin_known0( +; CHECK-NEXT: ret i8 1 +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umin(<4 x i8> %x) + %r = and i8 %v, 1 + ret i8 %r +} + +define i8 @vec_reduce_umin_known1(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umin_known1( +; CHECK-NEXT: [[X:%.*]] = and <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umin.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], -128 +; CHECK-NEXT: ret i8 [[R]] +; + %x = and <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umin(<4 x i8> %x) + %r = and i8 %v, 128 + ret i8 %r +} + +define i8 @vec_reduce_umin_known_fail0(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umin_known_fail0( +; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umin.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1 +; CHECK-NEXT: ret i8 [[R]] +; + %x0 = and <4 x i8> %xx, + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umin(<4 x i8> %x) + %r = and i8 %v, 1 + ret i8 %r +} + +define i8 @vec_reduce_umin_known_fail1(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_umin_known_fail1( +; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umin.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 1 +; CHECK-NEXT: ret i8 [[R]] +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umin(<4 x i8> %x) + %r = and i8 %v, 1 + ret i8 %r +} + +define i8 @vec_reduce_smax_known(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smax_known( +; CHECK-NEXT: ret i8 4 +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.smax(<4 x i8> %x) + %r = and i8 %v, 4 + ret i8 %r +} + +define i8 @vec_reduce_smax_known_fail(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smax_known_fail( +; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.umax.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 4 +; CHECK-NEXT: ret i8 [[R]] +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.umax(<4 x i8> %x) + %r = and i8 %v, 4 + ret i8 %r +} + +define i8 @vec_reduce_smin_known(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smin_known( +; CHECK-NEXT: ret i8 8 +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.smin(<4 x i8> %x) + %r = and i8 %v, 8 + ret i8 %r +} + +define i8 @vec_reduce_smin_known_fail(<4 x i8> %xx) { +; CHECK-LABEL: @vec_reduce_smin_known_fail( +; CHECK-NEXT: [[X:%.*]] = or <4 x i8> [[XX:%.*]], +; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.vector.reduce.smin.v4i8(<4 x i8> [[X]]) +; CHECK-NEXT: [[R:%.*]] = and i8 [[V]], 8 +; CHECK-NEXT: ret i8 [[R]] +; + %x = or <4 x i8> %xx, + %v = call i8 @llvm.vector.reduce.smin(<4 x i8> %x) + %r = and i8 %v, 8 + ret i8 %r +}