Index: lib/CodeGen/SelectionDAG/TargetLowering.cpp =================================================================== --- lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -751,6 +751,22 @@ KnownOne &= KnownOne2; KnownZero &= KnownZero2; break; + case ISD::SETCC: + // If we only need the sign-bit and the setcc operands are the same width as + // the setcc result, we may be able to bypass the setcc. + if (NewMask.isSignBit() && + Op.getOperand(0).getScalarValueSizeInBits() == BitWidth) { + ISD::CondCode CC = cast(Op.getOperand(2))->get(); + // If we're testing if X < 0, then this compare isn't needed - just use X! + if (CC == ISD::SETLT && + (isNullConstant(Op.getOperand(1)) || + ISD::isBuildVectorAllZeros(Op.getOperand(1).getNode()))) + return TLO.CombineTo(Op, Op.getOperand(0)); + + // TODO: Should we check for other forms of sign-bit comparisons? + // Examples: X <= -1, X >= 0 + } + break; case ISD::SHL: if (ConstantSDNode *SA = dyn_cast(Op.getOperand(1))) { unsigned ShAmt = SA->getZExtValue(); Index: test/CodeGen/X86/vselect-pcmp.ll =================================================================== --- test/CodeGen/X86/vselect-pcmp.ll +++ test/CodeGen/X86/vselect-pcmp.ll @@ -9,12 +9,22 @@ ; Test 128-bit vectors for all legal element types. define <16 x i8> @signbit_sel_v16i8(<16 x i8> %x, <16 x i8> %y, <16 x i8> %mask) { -; AVX-LABEL: signbit_sel_v16i8: -; AVX: # BB#0: -; AVX-NEXT: vpxor %xmm3, %xmm3, %xmm3 -; AVX-NEXT: vpcmpgtb %xmm2, %xmm3, %xmm2 -; AVX-NEXT: vpblendvb %xmm2, %xmm0, %xmm1, %xmm0 -; AVX-NEXT: retq +; AVX1-LABEL: signbit_sel_v16i8: +; AVX1: # BB#0: +; AVX1-NEXT: vpblendvb %xmm2, %xmm0, %xmm1, %xmm0 +; AVX1-NEXT: retq +; +; AVX2-LABEL: signbit_sel_v16i8: +; AVX2: # BB#0: +; AVX2-NEXT: vpblendvb %xmm2, %xmm0, %xmm1, %xmm0 +; AVX2-NEXT: retq +; +; AVX512F-LABEL: signbit_sel_v16i8: +; AVX512F: # BB#0: +; AVX512F-NEXT: vpxor %xmm3, %xmm3, %xmm3 +; AVX512F-NEXT: vpcmpgtb %xmm2, %xmm3, %xmm2 +; AVX512F-NEXT: vpblendvb %xmm2, %xmm0, %xmm1, %xmm0 +; AVX512F-NEXT: retq %tr = icmp slt <16 x i8> %mask, zeroinitializer %z = select <16 x i1> %tr, <16 x i8> %x, <16 x i8> %y ret <16 x i8> %z @@ -39,8 +49,6 @@ define <4 x i32> @signbit_sel_v4i32(<4 x i32> %x, <4 x i32> %y, <4 x i32> %mask) { ; AVX-LABEL: signbit_sel_v4i32: ; AVX: # BB#0: -; AVX-NEXT: vpxor %xmm3, %xmm3, %xmm3 -; AVX-NEXT: vpcmpgtd %xmm2, %xmm3, %xmm2 ; AVX-NEXT: vblendvps %xmm2, %xmm0, %xmm1, %xmm0 ; AVX-NEXT: retq %tr = icmp slt <4 x i32> %mask, zeroinitializer @@ -51,8 +59,6 @@ define <2 x i64> @signbit_sel_v2i64(<2 x i64> %x, <2 x i64> %y, <2 x i64> %mask) { ; AVX-LABEL: signbit_sel_v2i64: ; AVX: # BB#0: -; AVX-NEXT: vpxor %xmm3, %xmm3, %xmm3 -; AVX-NEXT: vpcmpgtq %xmm2, %xmm3, %xmm2 ; AVX-NEXT: vblendvpd %xmm2, %xmm0, %xmm1, %xmm0 ; AVX-NEXT: retq %tr = icmp slt <2 x i64> %mask, zeroinitializer @@ -63,8 +69,6 @@ define <4 x float> @signbit_sel_v4f32(<4 x float> %x, <4 x float> %y, <4 x i32> %mask) { ; AVX-LABEL: signbit_sel_v4f32: ; AVX: # BB#0: -; AVX-NEXT: vpxor %xmm3, %xmm3, %xmm3 -; AVX-NEXT: vpcmpgtd %xmm2, %xmm3, %xmm2 ; AVX-NEXT: vblendvps %xmm2, %xmm0, %xmm1, %xmm0 ; AVX-NEXT: retq %tr = icmp slt <4 x i32> %mask, zeroinitializer @@ -75,8 +79,6 @@ define <2 x double> @signbit_sel_v2f64(<2 x double> %x, <2 x double> %y, <2 x i64> %mask) { ; AVX-LABEL: signbit_sel_v2f64: ; AVX: # BB#0: -; AVX-NEXT: vpxor %xmm3, %xmm3, %xmm3 -; AVX-NEXT: vpcmpgtq %xmm2, %xmm3, %xmm2 ; AVX-NEXT: vblendvpd %xmm2, %xmm0, %xmm1, %xmm0 ; AVX-NEXT: retq %tr = icmp slt <2 x i64> %mask, zeroinitializer @@ -101,8 +103,6 @@ ; ; AVX2-LABEL: signbit_sel_v32i8: ; AVX2: # BB#0: -; AVX2-NEXT: vpxor %ymm3, %ymm3, %ymm3 -; AVX2-NEXT: vpcmpgtb %ymm2, %ymm3, %ymm2 ; AVX2-NEXT: vpblendvb %ymm2, %ymm0, %ymm1, %ymm0 ; AVX2-NEXT: retq ; @@ -157,18 +157,11 @@ define <8 x i32> @signbit_sel_v8i32(<8 x i32> %x, <8 x i32> %y, <8 x i32> %mask) { ; AVX1-LABEL: signbit_sel_v8i32: ; AVX1: # BB#0: -; AVX1-NEXT: vextractf128 $1, %ymm2, %xmm3 -; AVX1-NEXT: vpxor %xmm4, %xmm4, %xmm4 -; AVX1-NEXT: vpcmpgtd %xmm3, %xmm4, %xmm3 -; AVX1-NEXT: vpcmpgtd %xmm2, %xmm4, %xmm2 -; AVX1-NEXT: vinsertf128 $1, %xmm3, %ymm2, %ymm2 ; AVX1-NEXT: vblendvps %ymm2, %ymm0, %ymm1, %ymm0 ; AVX1-NEXT: retq ; ; AVX2-LABEL: signbit_sel_v8i32: ; AVX2: # BB#0: -; AVX2-NEXT: vpxor %ymm3, %ymm3, %ymm3 -; AVX2-NEXT: vpcmpgtd %ymm2, %ymm3, %ymm2 ; AVX2-NEXT: vblendvps %ymm2, %ymm0, %ymm1, %ymm0 ; AVX2-NEXT: retq ; @@ -188,58 +181,20 @@ } define <4 x i64> @signbit_sel_v4i64(<4 x i64> %x, <4 x i64> %y, <4 x i64> %mask) { -; AVX1-LABEL: signbit_sel_v4i64: -; AVX1: # BB#0: -; AVX1-NEXT: vextractf128 $1, %ymm2, %xmm3 -; AVX1-NEXT: vpxor %xmm4, %xmm4, %xmm4 -; AVX1-NEXT: vpcmpgtq %xmm3, %xmm4, %xmm3 -; AVX1-NEXT: vpcmpgtq %xmm2, %xmm4, %xmm2 -; AVX1-NEXT: vinsertf128 $1, %xmm3, %ymm2, %ymm2 -; AVX1-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 -; AVX1-NEXT: retq -; -; AVX2-LABEL: signbit_sel_v4i64: -; AVX2: # BB#0: -; AVX2-NEXT: vpxor %ymm3, %ymm3, %ymm3 -; AVX2-NEXT: vpcmpgtq %ymm2, %ymm3, %ymm2 -; AVX2-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 -; AVX2-NEXT: retq -; -; AVX512F-LABEL: signbit_sel_v4i64: -; AVX512F: # BB#0: -; AVX512F-NEXT: vpxor %ymm3, %ymm3, %ymm3 -; AVX512F-NEXT: vpcmpgtq %ymm2, %ymm3, %ymm2 -; AVX512F-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 -; AVX512F-NEXT: retq +; AVX-LABEL: signbit_sel_v4i64: +; AVX: # BB#0: +; AVX-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 +; AVX-NEXT: retq %tr = icmp slt <4 x i64> %mask, zeroinitializer %z = select <4 x i1> %tr, <4 x i64> %x, <4 x i64> %y ret <4 x i64> %z } define <4 x double> @signbit_sel_v4f64(<4 x double> %x, <4 x double> %y, <4 x i64> %mask) { -; AVX1-LABEL: signbit_sel_v4f64: -; AVX1: # BB#0: -; AVX1-NEXT: vextractf128 $1, %ymm2, %xmm3 -; AVX1-NEXT: vpxor %xmm4, %xmm4, %xmm4 -; AVX1-NEXT: vpcmpgtq %xmm3, %xmm4, %xmm3 -; AVX1-NEXT: vpcmpgtq %xmm2, %xmm4, %xmm2 -; AVX1-NEXT: vinsertf128 $1, %xmm3, %ymm2, %ymm2 -; AVX1-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 -; AVX1-NEXT: retq -; -; AVX2-LABEL: signbit_sel_v4f64: -; AVX2: # BB#0: -; AVX2-NEXT: vpxor %ymm3, %ymm3, %ymm3 -; AVX2-NEXT: vpcmpgtq %ymm2, %ymm3, %ymm2 -; AVX2-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 -; AVX2-NEXT: retq -; -; AVX512F-LABEL: signbit_sel_v4f64: -; AVX512F: # BB#0: -; AVX512F-NEXT: vpxor %ymm3, %ymm3, %ymm3 -; AVX512F-NEXT: vpcmpgtq %ymm2, %ymm3, %ymm2 -; AVX512F-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 -; AVX512F-NEXT: retq +; AVX-LABEL: signbit_sel_v4f64: +; AVX: # BB#0: +; AVX-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 +; AVX-NEXT: retq %tr = icmp slt <4 x i64> %mask, zeroinitializer %z = select <4 x i1> %tr, <4 x double> %x, <4 x double> %y ret <4 x double> %z @@ -261,16 +216,12 @@ ; ; AVX2-LABEL: signbit_sel_v4f64_small_mask: ; AVX2: # BB#0: -; AVX2-NEXT: vpxor %xmm3, %xmm3, %xmm3 -; AVX2-NEXT: vpcmpgtd %xmm2, %xmm3, %xmm2 ; AVX2-NEXT: vpmovsxdq %xmm2, %ymm2 ; AVX2-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 ; AVX2-NEXT: retq ; ; AVX512F-LABEL: signbit_sel_v4f64_small_mask: ; AVX512F: # BB#0: -; AVX512F-NEXT: vpxor %xmm3, %xmm3, %xmm3 -; AVX512F-NEXT: vpcmpgtd %xmm2, %xmm3, %xmm2 ; AVX512F-NEXT: vpmovsxdq %xmm2, %ymm2 ; AVX512F-NEXT: vblendvpd %ymm2, %ymm0, %ymm1, %ymm0 ; AVX512F-NEXT: retq @@ -284,24 +235,12 @@ define <8 x double> @signbit_sel_v8f64(<8 x double> %x, <8 x double> %y, <8 x i64> %mask) { ; AVX1-LABEL: signbit_sel_v8f64: ; AVX1: # BB#0: -; AVX1-NEXT: vextractf128 $1, %ymm5, %xmm6 -; AVX1-NEXT: vpxor %xmm7, %xmm7, %xmm7 -; AVX1-NEXT: vpcmpgtq %xmm6, %xmm7, %xmm6 -; AVX1-NEXT: vpcmpgtq %xmm5, %xmm7, %xmm5 -; AVX1-NEXT: vinsertf128 $1, %xmm6, %ymm5, %ymm5 -; AVX1-NEXT: vextractf128 $1, %ymm4, %xmm6 -; AVX1-NEXT: vpcmpgtq %xmm6, %xmm7, %xmm6 -; AVX1-NEXT: vpcmpgtq %xmm4, %xmm7, %xmm4 -; AVX1-NEXT: vinsertf128 $1, %xmm6, %ymm4, %ymm4 ; AVX1-NEXT: vblendvpd %ymm4, %ymm0, %ymm2, %ymm0 ; AVX1-NEXT: vblendvpd %ymm5, %ymm1, %ymm3, %ymm1 ; AVX1-NEXT: retq ; ; AVX2-LABEL: signbit_sel_v8f64: ; AVX2: # BB#0: -; AVX2-NEXT: vpxor %ymm6, %ymm6, %ymm6 -; AVX2-NEXT: vpcmpgtq %ymm5, %ymm6, %ymm5 -; AVX2-NEXT: vpcmpgtq %ymm4, %ymm6, %ymm4 ; AVX2-NEXT: vblendvpd %ymm4, %ymm0, %ymm2, %ymm0 ; AVX2-NEXT: vblendvpd %ymm5, %ymm1, %ymm3, %ymm1 ; AVX2-NEXT: retq