diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -2878,6 +2878,50 @@ return convertFromScalableVector(VT, Res, DAG, Subtarget); } +// Lower the following shuffle to vslidedown. +// t49: v8i8 = extract_subvector t13, Constant:i64<0> +// t109: v8i8 = extract_subvector t12, Constant:i64<8> +// t108: v8i8 = vector_shuffle<1,2,3,4,5,6,7,8> t49, t106 +static SDValue lowerVECTOR_SHUFFLEAsVSlidedown(const SDLoc &DL, MVT VT, + SDValue V1, SDValue V2, + ArrayRef Mask, + const RISCVSubtarget &Subtarget, + SelectionDAG &DAG) { + // Both input must be extracts. + if (V1.getOpcode() != ISD::EXTRACT_SUBVECTOR || + V2.getOpcode() != ISD::EXTRACT_SUBVECTOR) + return SDValue(); + + // Extracting from the same source. + SDValue Src = V1.getOperand(0); + if (Src != V2.getOperand(0)) + return SDValue(); + + // V1 and V2 are continuous. + if (V1.getConstantOperandVal(1) + + V1.getSimpleValueType().getVectorNumElements() != + V2.getConstantOperandVal(1)) + return SDValue(); + + // Mask is also continuous. + for (unsigned i = 1; i != Mask.size(); ++i) + if (Mask[i - 1] + 1 != Mask[i]) + return SDValue(); + + MVT XLenVT = Subtarget.getXLenVT(); + MVT SrcVT = Src.getSimpleValueType(); + MVT ContainerVT = getContainerForFixedLengthVector(DAG, SrcVT, Subtarget); + auto [TrueMask, VL] = getDefaultVLOps(SrcVT, ContainerVT, DL, DAG, Subtarget); + SDValue Slidedown = DAG.getNode( + RISCVISD::VSLIDEDOWN_VL, DL, ContainerVT, DAG.getUNDEF(ContainerVT), + convertToScalableVector(ContainerVT, Src, DAG, Subtarget), + DAG.getConstant(Mask[0], DL, XLenVT), TrueMask, VL); + return DAG.getNode( + ISD::EXTRACT_SUBVECTOR, DL, VT, + convertFromScalableVector(SrcVT, Slidedown, DAG, Subtarget), + DAG.getConstant(0, DL, XLenVT)); +} + static SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG, const RISCVSubtarget &Subtarget) { SDValue V1 = Op.getOperand(0); @@ -2969,6 +3013,10 @@ ArrayRef Mask = SVN->getMask(); + if (SDValue V = + lowerVECTOR_SHUFFLEAsVSlidedown(DL, VT, V1, V2, Mask, Subtarget, DAG)) + return V; + // Lower rotations to a SLIDEDOWN and a SLIDEUP. One of the source vectors may // be undef which can be handled with a single SLIDEDOWN/UP. int LoSrc, HiSrc; diff --git a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-fp-shuffles.ll b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-fp-shuffles.ll --- a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-fp-shuffles.ll +++ b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-fp-shuffles.ll @@ -226,6 +226,39 @@ ret <4 x double> %s } +define <4 x half> @shuffle_v8f16_to_vslidedown_1(<8 x half> %x) { +; CHECK-LABEL: shuffle_v8f16_to_vslidedown_1: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma +; CHECK-NEXT: vslidedown.vi v8, v8, 1 +; CHECK-NEXT: ret +entry: + %s = shufflevector <8 x half> %x, <8 x half> poison, <4 x i32> + ret <4 x half> %s +} + +define <4 x half> @shuffle_v8f16_to_vslidedown_3(<8 x half> %x) { +; CHECK-LABEL: shuffle_v8f16_to_vslidedown_3: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma +; CHECK-NEXT: vslidedown.vi v8, v8, 3 +; CHECK-NEXT: ret +entry: + %s = shufflevector <8 x half> %x, <8 x half> poison, <4 x i32> + ret <4 x half> %s +} + +define <2 x float> @shuffle_v4f32_to_vslidedown(<4 x float> %x) { +; CHECK-LABEL: shuffle_v4f32_to_vslidedown: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetivli zero, 4, e32, m1, ta, ma +; CHECK-NEXT: vslidedown.vi v8, v8, 1 +; CHECK-NEXT: ret +entry: + %s = shufflevector <4 x float> %x, <4 x float> poison, <2 x i32> + ret <2 x float> %s +} + define <4 x half> @slidedown_v4f16(<4 x half> %x) { ; CHECK-LABEL: slidedown_v4f16: ; CHECK: # %bb.0: diff --git a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-int-shuffles.ll b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-int-shuffles.ll --- a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-int-shuffles.ll +++ b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-int-shuffles.ll @@ -292,6 +292,39 @@ ret <8 x i64> %s } +define <4 x i16> @shuffle_v8i16_to_vslidedown_1(<8 x i16> %x) { +; CHECK-LABEL: shuffle_v8i16_to_vslidedown_1: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma +; CHECK-NEXT: vslidedown.vi v8, v8, 1 +; CHECK-NEXT: ret +entry: + %s = shufflevector <8 x i16> %x, <8 x i16> poison, <4 x i32> + ret <4 x i16> %s +} + +define <4 x i16> @shuffle_v8i16_to_vslidedown_3(<8 x i16> %x) { +; CHECK-LABEL: shuffle_v8i16_to_vslidedown_3: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma +; CHECK-NEXT: vslidedown.vi v8, v8, 3 +; CHECK-NEXT: ret +entry: + %s = shufflevector <8 x i16> %x, <8 x i16> poison, <4 x i32> + ret <4 x i16> %s +} + +define <2 x i32> @shuffle_v4i32_to_vslidedown(<4 x i32> %x) { +; CHECK-LABEL: shuffle_v4i32_to_vslidedown: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetivli zero, 4, e32, m1, ta, ma +; CHECK-NEXT: vslidedown.vi v8, v8, 1 +; CHECK-NEXT: ret +entry: + %s = shufflevector <4 x i32> %x, <4 x i32> poison, <2 x i32> + ret <2 x i32> %s +} + define <4 x i8> @interleave_shuffles(<4 x i8> %x) { ; CHECK-LABEL: interleave_shuffles: ; CHECK: # %bb.0: