https://github.com/sihuan updated https://github.com/llvm/llvm-project/pull/241730
>From d201d5e47bd526f75d972af0fd53ab9c046ca719 Mon Sep 17 00:00:00 2001 From: SiHuaN <[email protected]> Date: Thu, 22 Oct 2026 10:21:14 +0000 Subject: [PATCH] [RISCV][P-ext] Add scalar saturating and rounding shift intrinsics Add __riscv_ssha_i32, __riscv_sshar_i32, __riscv_sshl_u32, __riscv_sshlr_u32 and the RV64-only __riscv_sha_i64, __riscv_shar_i64, __riscv_shl_u64 and __riscv_shlr_u64 from the "Saturating and Rounding Shift" section of the P intrinsic spec: https://github.com/riscv/riscv-p-spec/blob/master/P-ext-intrinsics.adoc#saturating-and-rounding-shift The 32-bit forms reuse the PSSHA/PSSHAR/PSSHL/PSSHLR nodes, which now also accept a scalar. RV32 selects ssha/sshar/sshl/sshlr and RV64 uses the .ws forms on the low word. The 64-bit forms do not saturate and get new SHA/SHAR/SHL/SHLR nodes. Constant shift amounts select the immediate forms where possible. The packed 32-bit .ws shift patterns now use PatGprShiftMask so that masking a shift amount to the 8 bits the instructions read is folded on RV64 as it already is on RV32. --- clang/include/clang/Basic/BuiltinsRISCV.td | 12 ++ clang/lib/CodeGen/TargetBuiltins/RISCV.cpp | 26 +++ clang/lib/Headers/riscv_packed_simd.h | 12 ++ clang/test/CodeGen/RISCV/rvp-intrinsics.c | 108 ++++++++++++ .../riscv_packed_simd.c | 56 ++++++ llvm/include/llvm/IR/IntrinsicsRISCV.td | 21 ++- llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 47 +++++- llvm/lib/Target/RISCV/RISCVInstrInfoP.td | 40 ++++- llvm/test/CodeGen/RISCV/rvp-scalar-rv64.ll | 112 ++++++++++++ llvm/test/CodeGen/RISCV/rvp-scalar.ll | 159 ++++++++++++++++++ 10 files changed, 584 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/Basic/BuiltinsRISCV.td b/clang/include/clang/Basic/BuiltinsRISCV.td index fa4cb4808f7cfc..d3899df3bb3cc1 100644 --- a/clang/include/clang/Basic/BuiltinsRISCV.td +++ b/clang/include/clang/Basic/BuiltinsRISCV.td @@ -282,6 +282,18 @@ def sati_i64 : RISCVBuiltin<"int64_t(int64_t, _Constant unsigned int)">; def usati_u64 : RISCVBuiltin<"uint64_t(int64_t, _Constant unsigned int)">; } // Features = "experimental-p,64bit" +// Scalar Saturating and Rounding Shift +def ssha_i32 : RISCVBuiltin<"int32_t(int32_t, int)">; +def sshar_i32 : RISCVBuiltin<"int32_t(int32_t, int)">; +def sshl_u32 : RISCVBuiltin<"uint32_t(uint32_t, int)">; +def sshlr_u32 : RISCVBuiltin<"uint32_t(uint32_t, int)">; +let Features = "experimental-p,64bit" in { +def sha_i64 : RISCVBuiltin<"int64_t(int64_t, int)">; +def shar_i64 : RISCVBuiltin<"int64_t(int64_t, int)">; +def shl_u64 : RISCVBuiltin<"uint64_t(uint64_t, int)">; +def shlr_u64 : RISCVBuiltin<"uint64_t(uint64_t, int)">; +} // Features = "experimental-p,64bit" + // Scalar Multiply High def mulh_i32 : RISCVBuiltin<"int32_t(int32_t, int32_t)">; def mulhr_i32 : RISCVBuiltin<"int32_t(int32_t, int32_t)">; diff --git a/clang/lib/CodeGen/TargetBuiltins/RISCV.cpp b/clang/lib/CodeGen/TargetBuiltins/RISCV.cpp index 423a15b5167864..2029f30a42aeca 100644 --- a/clang/lib/CodeGen/TargetBuiltins/RISCV.cpp +++ b/clang/lib/CodeGen/TargetBuiltins/RISCV.cpp @@ -1705,6 +1705,32 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID, ID = Intrinsic::riscv_ssh1sadd_i32; break; + // Scalar Saturating and Rounding Shift + case RISCV::BI__builtin_riscv_ssha_i32: + ID = Intrinsic::riscv_ssha_i32; + break; + case RISCV::BI__builtin_riscv_sshar_i32: + ID = Intrinsic::riscv_sshar_i32; + break; + case RISCV::BI__builtin_riscv_sshl_u32: + ID = Intrinsic::riscv_sshl_u32; + break; + case RISCV::BI__builtin_riscv_sshlr_u32: + ID = Intrinsic::riscv_sshlr_u32; + break; + case RISCV::BI__builtin_riscv_sha_i64: + ID = Intrinsic::riscv_sha_i64; + break; + case RISCV::BI__builtin_riscv_shar_i64: + ID = Intrinsic::riscv_shar_i64; + break; + case RISCV::BI__builtin_riscv_shl_u64: + ID = Intrinsic::riscv_shl_u64; + break; + case RISCV::BI__builtin_riscv_shlr_u64: + ID = Intrinsic::riscv_shlr_u64; + break; + // Scalar Multiply High case RISCV::BI__builtin_riscv_mulhr_i32: case RISCV::BI__builtin_riscv_mulhru_u32: diff --git a/clang/lib/Headers/riscv_packed_simd.h b/clang/lib/Headers/riscv_packed_simd.h index 116da9e42512aa..2ba48d6092c7f3 100644 --- a/clang/lib/Headers/riscv_packed_simd.h +++ b/clang/lib/Headers/riscv_packed_simd.h @@ -493,6 +493,18 @@ __packed_pabs(abs_u64, int64_t, uint64_t) #define __riscv_usati_u64(rs1, width) __builtin_riscv_usati_u64(rs1, width) #endif +/* Scalar Saturating and Rounding Shift */ +__packed_binary_builtin_mixed(ssha_i32, int32_t, int32_t, int, __builtin_riscv_ssha_i32) +__packed_binary_builtin_mixed(sshar_i32, int32_t, int32_t, int, __builtin_riscv_sshar_i32) +__packed_binary_builtin_mixed(sshl_u32, uint32_t, uint32_t, int, __builtin_riscv_sshl_u32) +__packed_binary_builtin_mixed(sshlr_u32, uint32_t, uint32_t, int, __builtin_riscv_sshlr_u32) +#if __riscv_xlen == 64 +__packed_binary_builtin_mixed(sha_i64, int64_t, int64_t, int, __builtin_riscv_sha_i64) +__packed_binary_builtin_mixed(shar_i64, int64_t, int64_t, int, __builtin_riscv_shar_i64) +__packed_binary_builtin_mixed(shl_u64, uint64_t, uint64_t, int, __builtin_riscv_shl_u64) +__packed_binary_builtin_mixed(shlr_u64, uint64_t, uint64_t, int, __builtin_riscv_shlr_u64) +#endif + /* Scalar Multiply High */ #define __packed_mulh_builtin(name, res_ty, ty1, ty2) \ static __inline__ res_ty __DEFAULT_FN_ATTRS __riscv_##name(ty1 __rs1, \ diff --git a/clang/test/CodeGen/RISCV/rvp-intrinsics.c b/clang/test/CodeGen/RISCV/rvp-intrinsics.c index 2357234a280170..3cc7f12c6b2411 100644 --- a/clang/test/CodeGen/RISCV/rvp-intrinsics.c +++ b/clang/test/CodeGen/RISCV/rvp-intrinsics.c @@ -438,6 +438,114 @@ uint64_t test_usati_u64(int64_t a) { } #endif +/* Scalar Saturating and Rounding Shift */ + +// RV32-LABEL: define dso_local i32 @test_ssha_i32( +// RV32-SAME: i32 noundef [[A:%.*]], i32 noundef [[S:%.*]]) #[[ATTR0]] { +// RV32-NEXT: [[ENTRY:.*:]] +// RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.ssha.i32(i32 [[A]], i32 [[S]]) +// RV32-NEXT: ret i32 [[TMP0]] +// +// RV64-LABEL: define dso_local signext i32 @test_ssha_i32( +// RV64-SAME: i32 noundef signext [[A:%.*]], i32 noundef signext [[S:%.*]]) #[[ATTR0]] { +// RV64-NEXT: [[ENTRY:.*:]] +// RV64-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.ssha.i32(i32 [[A]], i32 [[S]]) +// RV64-NEXT: ret i32 [[TMP0]] +// +int32_t test_ssha_i32(int32_t a, int s) { + return __riscv_ssha_i32(a, s); +} + +// RV32-LABEL: define dso_local i32 @test_sshar_i32( +// RV32-SAME: i32 noundef [[A:%.*]], i32 noundef [[S:%.*]]) #[[ATTR0]] { +// RV32-NEXT: [[ENTRY:.*:]] +// RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.sshar.i32(i32 [[A]], i32 [[S]]) +// RV32-NEXT: ret i32 [[TMP0]] +// +// RV64-LABEL: define dso_local signext i32 @test_sshar_i32( +// RV64-SAME: i32 noundef signext [[A:%.*]], i32 noundef signext [[S:%.*]]) #[[ATTR0]] { +// RV64-NEXT: [[ENTRY:.*:]] +// RV64-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.sshar.i32(i32 [[A]], i32 [[S]]) +// RV64-NEXT: ret i32 [[TMP0]] +// +int32_t test_sshar_i32(int32_t a, int s) { + return __riscv_sshar_i32(a, s); +} + +// RV32-LABEL: define dso_local i32 @test_sshl_u32( +// RV32-SAME: i32 noundef [[A:%.*]], i32 noundef [[S:%.*]]) #[[ATTR0]] { +// RV32-NEXT: [[ENTRY:.*:]] +// RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.sshl.u32(i32 [[A]], i32 [[S]]) +// RV32-NEXT: ret i32 [[TMP0]] +// +// RV64-LABEL: define dso_local signext i32 @test_sshl_u32( +// RV64-SAME: i32 noundef signext [[A:%.*]], i32 noundef signext [[S:%.*]]) #[[ATTR0]] { +// RV64-NEXT: [[ENTRY:.*:]] +// RV64-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.sshl.u32(i32 [[A]], i32 [[S]]) +// RV64-NEXT: ret i32 [[TMP0]] +// +uint32_t test_sshl_u32(uint32_t a, int s) { + return __riscv_sshl_u32(a, s); +} + +// RV32-LABEL: define dso_local i32 @test_sshlr_u32( +// RV32-SAME: i32 noundef [[A:%.*]], i32 noundef [[S:%.*]]) #[[ATTR0]] { +// RV32-NEXT: [[ENTRY:.*:]] +// RV32-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.sshlr.u32(i32 [[A]], i32 [[S]]) +// RV32-NEXT: ret i32 [[TMP0]] +// +// RV64-LABEL: define dso_local signext i32 @test_sshlr_u32( +// RV64-SAME: i32 noundef signext [[A:%.*]], i32 noundef signext [[S:%.*]]) #[[ATTR0]] { +// RV64-NEXT: [[ENTRY:.*:]] +// RV64-NEXT: [[TMP0:%.*]] = call i32 @llvm.riscv.sshlr.u32(i32 [[A]], i32 [[S]]) +// RV64-NEXT: ret i32 [[TMP0]] +// +uint32_t test_sshlr_u32(uint32_t a, int s) { + return __riscv_sshlr_u32(a, s); +} + +#if __riscv_xlen == 64 +// RV64-LABEL: define dso_local i64 @test_sha_i64( +// RV64-SAME: i64 noundef [[A:%.*]], i32 noundef signext [[S:%.*]]) #[[ATTR0]] { +// RV64-NEXT: [[ENTRY:.*:]] +// RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.sha.i64(i64 [[A]], i32 [[S]]) +// RV64-NEXT: ret i64 [[TMP0]] +// +int64_t test_sha_i64(int64_t a, int s) { + return __riscv_sha_i64(a, s); +} + +// RV64-LABEL: define dso_local i64 @test_shar_i64( +// RV64-SAME: i64 noundef [[A:%.*]], i32 noundef signext [[S:%.*]]) #[[ATTR0]] { +// RV64-NEXT: [[ENTRY:.*:]] +// RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.shar.i64(i64 [[A]], i32 [[S]]) +// RV64-NEXT: ret i64 [[TMP0]] +// +int64_t test_shar_i64(int64_t a, int s) { + return __riscv_shar_i64(a, s); +} + +// RV64-LABEL: define dso_local i64 @test_shl_u64( +// RV64-SAME: i64 noundef [[A:%.*]], i32 noundef signext [[S:%.*]]) #[[ATTR0]] { +// RV64-NEXT: [[ENTRY:.*:]] +// RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.shl.u64(i64 [[A]], i32 [[S]]) +// RV64-NEXT: ret i64 [[TMP0]] +// +uint64_t test_shl_u64(uint64_t a, int s) { + return __riscv_shl_u64(a, s); +} + +// RV64-LABEL: define dso_local i64 @test_shlr_u64( +// RV64-SAME: i64 noundef [[A:%.*]], i32 noundef signext [[S:%.*]]) #[[ATTR0]] { +// RV64-NEXT: [[ENTRY:.*:]] +// RV64-NEXT: [[TMP0:%.*]] = call i64 @llvm.riscv.shlr.u64(i64 [[A]], i32 [[S]]) +// RV64-NEXT: ret i64 [[TMP0]] +// +uint64_t test_shlr_u64(uint64_t a, int s) { + return __riscv_shlr_u64(a, s); +} +#endif + // RV32-LABEL: define dso_local i32 @test_mulh_i32( // RV32-SAME: i32 noundef [[A:%.*]], i32 noundef [[B:%.*]]) #[[ATTR0]] { // RV32-NEXT: [[ENTRY:.*:]] diff --git a/cross-project-tests/intrinsic-header-tests/riscv_packed_simd.c b/cross-project-tests/intrinsic-header-tests/riscv_packed_simd.c index dd749535f2113f..765d14d511b951 100644 --- a/cross-project-tests/intrinsic-header-tests/riscv_packed_simd.c +++ b/cross-project-tests/intrinsic-header-tests/riscv_packed_simd.c @@ -164,6 +164,62 @@ int64_t test_sati_i64(int64_t a) { return __riscv_sati_i64(a, 16); } uint64_t test_usati_u64(int64_t a) { return __riscv_usati_u64(a, 16); } #endif +// CHECK-LABEL: test_ssha_i32: +// RV32: ssha{{[[:space:]]}} +// RV64: pssha.ws +int32_t test_ssha_i32(int32_t a, int s) { return __riscv_ssha_i32(a, s); } + +// CHECK-LABEL: test_ssha_i32_imm: +// RV32: sslai{{[[:space:]]}} +// RV64: psslai.w +int32_t test_ssha_i32_imm(int32_t a) { return __riscv_ssha_i32(a, 3); } + +// CHECK-LABEL: test_sshar_i32: +// RV32: sshar{{[[:space:]]}} +// RV64: psshar.ws +int32_t test_sshar_i32(int32_t a, int s) { return __riscv_sshar_i32(a, s); } + +// CHECK-LABEL: test_sshar_i32_negimm: +// RV32: srari{{[[:space:]]}} +// RV64: psrari.w +int32_t test_sshar_i32_negimm(int32_t a) { return __riscv_sshar_i32(a, -3); } + +// CHECK-LABEL: test_sshl_u32: +// RV32: sshl{{[[:space:]]}} +// RV64: psshl.ws +uint32_t test_sshl_u32(uint32_t a, int s) { return __riscv_sshl_u32(a, s); } + +// CHECK-LABEL: test_sshlr_u32: +// RV32: sshlr{{[[:space:]]}} +// RV64: psshlr.ws +uint32_t test_sshlr_u32(uint32_t a, int s) { return __riscv_sshlr_u32(a, s); } + +#if __riscv_xlen == 64 +// RV64-LABEL: test_sha_i64: +// RV64: sha{{[[:space:]]}} +int64_t test_sha_i64(int64_t a, int s) { return __riscv_sha_i64(a, s); } + +// RV64-LABEL: test_sha_i64_imm: +// RV64: slli{{[[:space:]]}} +int64_t test_sha_i64_imm(int64_t a) { return __riscv_sha_i64(a, 3); } + +// RV64-LABEL: test_shar_i64: +// RV64: shar{{[[:space:]]}} +int64_t test_shar_i64(int64_t a, int s) { return __riscv_shar_i64(a, s); } + +// RV64-LABEL: test_shar_i64_negimm: +// RV64: srari{{[[:space:]]}} +int64_t test_shar_i64_negimm(int64_t a) { return __riscv_shar_i64(a, -3); } + +// RV64-LABEL: test_shl_u64: +// RV64: shl{{[[:space:]]}} +uint64_t test_shl_u64(uint64_t a, int s) { return __riscv_shl_u64(a, s); } + +// RV64-LABEL: test_shlr_u64: +// RV64: shlr{{[[:space:]]}} +uint64_t test_shlr_u64(uint64_t a, int s) { return __riscv_shlr_u64(a, s); } +#endif + // CHECK-LABEL: test_pmv_s_u8x4: // CHECK: pmv.bs uint8x4_t test_pmv_s_u8x4(uint8_t x) { return __riscv_pmv_s_u8x4(x); } diff --git a/llvm/include/llvm/IR/IntrinsicsRISCV.td b/llvm/include/llvm/IR/IntrinsicsRISCV.td index d077c8e124498f..0c9eb1c52be27d 100644 --- a/llvm/include/llvm/IR/IntrinsicsRISCV.td +++ b/llvm/include/llvm/IR/IntrinsicsRISCV.td @@ -2174,11 +2174,13 @@ class RVPBinaryIntrinsic def int_riscv_pmulhsu : RVPBinaryIntrinsic; def int_riscv_pmulhrsu : RVPBinaryIntrinsic; - // Scalar Averaging Addition and Subtraction. On RV64 these map to the - // packed paadd.w family on the low words. + // Scalar binary intrinsic on i32 operands. On RV64 these map to a packed .w + // instruction on the low words. class RVPScalarBinaryIntrinsic : DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i32_ty], [IntrNoMem, IntrSpeculatable]>; + + // Scalar Averaging Addition and Subtraction. def int_riscv_aadd_i32 : RVPScalarBinaryIntrinsic; def int_riscv_aaddu_u32 : RVPScalarBinaryIntrinsic; def int_riscv_asub_i32 : RVPScalarBinaryIntrinsic; @@ -2187,6 +2189,21 @@ class RVPBinaryIntrinsic // Scalar Shift-Add. On RV64 this maps to pssh1sadd.w on the low words. def int_riscv_ssh1sadd_i32 : RVPScalarBinaryIntrinsic; + // Scalar Saturating and Rounding Shifts. The 32-bit forms saturate, the + // 64-bit forms do not. The second operand is a signed shift amount; negative + // amounts shift right. + class RVPScalarShift64Intrinsic + : DefaultAttrsIntrinsic<[llvm_i64_ty], [llvm_i64_ty, llvm_i32_ty], + [IntrNoMem, IntrSpeculatable]>; + def int_riscv_ssha_i32 : RVPScalarBinaryIntrinsic; + def int_riscv_sshar_i32 : RVPScalarBinaryIntrinsic; + def int_riscv_sshl_u32 : RVPScalarBinaryIntrinsic; + def int_riscv_sshlr_u32 : RVPScalarBinaryIntrinsic; + def int_riscv_sha_i64 : RVPScalarShift64Intrinsic; + def int_riscv_shar_i64 : RVPScalarShift64Intrinsic; + def int_riscv_shl_u64 : RVPScalarShift64Intrinsic; + def int_riscv_shlr_u64 : RVPScalarShift64Intrinsic; + // Scalar Multiply High. On RV32 these map to the M-extension mulh // instructions and the P-extension mulhr instructions. class RVPScalarMulHighIntrinsic diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp index 0cc9e5f646f876..e32de57fb74d65 100644 --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -12459,6 +12459,14 @@ static unsigned getRVPShiftOpcode(Intrinsic::ID IntNo) { return RISCVISD::PSSHL; case Intrinsic::riscv_psshlr: return RISCVISD::PSSHLR; + case Intrinsic::riscv_sha_i64: + return RISCVISD::SHA; + case Intrinsic::riscv_shar_i64: + return RISCVISD::SHAR; + case Intrinsic::riscv_shl_u64: + return RISCVISD::SHL; + case Intrinsic::riscv_shlr_u64: + return RISCVISD::SHLR; } } @@ -12516,7 +12524,7 @@ static unsigned getRVPScalarOpcode(unsigned IntNo) { default: llvm_unreachable( "Unexpected RISC-V scalar averaging, multiply high, Q-format " - "multiply or multiply high accumulate intrinsic"); + "multiply, multiply high accumulate or saturating shift intrinsic"); case Intrinsic::riscv_aadd_i32: return ISD::AVGFLOORS; case Intrinsic::riscv_aaddu_u32: @@ -12527,6 +12535,14 @@ static unsigned getRVPScalarOpcode(unsigned IntNo) { return ISD::AVGSUBFLOORU; case Intrinsic::riscv_ssh1sadd_i32: return RISCVISD::SSH1SADD; + case Intrinsic::riscv_ssha_i32: + return RISCVISD::PSSHA; + case Intrinsic::riscv_sshar_i32: + return RISCVISD::PSSHAR; + case Intrinsic::riscv_sshl_u32: + return RISCVISD::PSSHL; + case Intrinsic::riscv_sshlr_u32: + return RISCVISD::PSSHLR; case Intrinsic::riscv_mulhr_i32: return RISCVISD::MULHR; case Intrinsic::riscv_mulhru_u32: @@ -13370,7 +13386,13 @@ SDValue RISCVTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, case Intrinsic::riscv_pssha: case Intrinsic::riscv_psshar: case Intrinsic::riscv_psshl: - case Intrinsic::riscv_psshlr: { + case Intrinsic::riscv_psshlr: + case Intrinsic::riscv_sha_i64: + case Intrinsic::riscv_shar_i64: + case Intrinsic::riscv_shl_u64: + case Intrinsic::riscv_shlr_u64: { + // The 64-bit scalar shifts share the shift amount handling here; the 32-bit + // scalar shifts are lowered with the scalar i32 intrinsics below. SDValue ShAmt = Op.getOperand(2); ShAmt = DAG.getAnyExtOrTrunc(ShAmt, DL, XLenVT); return DAG.getNode(getRVPShiftOpcode(IntNo), DL, Op.getValueType(), @@ -13515,6 +13537,10 @@ SDValue RISCVTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, case Intrinsic::riscv_asub_i32: case Intrinsic::riscv_asubu_u32: case Intrinsic::riscv_ssh1sadd_i32: + case Intrinsic::riscv_ssha_i32: + case Intrinsic::riscv_sshar_i32: + case Intrinsic::riscv_sshl_u32: + case Intrinsic::riscv_sshlr_u32: case Intrinsic::riscv_mulhr_i32: case Intrinsic::riscv_mulhru_u32: case Intrinsic::riscv_mulhsu_i32: @@ -18629,6 +18655,23 @@ void RISCVTargetLowering::ReplaceNodeResults(SDNode *N, Results.push_back(DAG.getExtractVectorElt(DL, MVT::i32, Res, 0)); return; } + case Intrinsic::riscv_ssha_i32: + case Intrinsic::riscv_sshar_i32: + case Intrinsic::riscv_sshl_u32: + case Intrinsic::riscv_sshlr_u32: { + // RV64 has no scalar saturating shift instructions; reuse the packed .ws + // family on the low word. The shift amount stays a scalar. Unlike sati + // this cannot be promoted to i64: SHA/SHL do not saturate. + assert(Subtarget.is64Bit() && N->getValueType(0) == MVT::i32 && + "Unexpected subtarget or result type"); + unsigned Opc = getRVPScalarOpcode(IntNo); + SDValue Rs1 = + DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v2i32, N->getOperand(1)); + SDValue ShAmt = DAG.getAnyExtOrTrunc(N->getOperand(2), DL, MVT::i64); + SDValue Res = DAG.getNode(Opc, DL, MVT::v2i32, Rs1, ShAmt); + Results.push_back(DAG.getExtractVectorElt(DL, MVT::i32, Res, 0)); + return; + } case Intrinsic::riscv_sati: case Intrinsic::riscv_usati: { // RV64 has no 32-bit sati; promote to i64. diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoP.td b/llvm/lib/Target/RISCV/RISCVInstrInfoP.td index e561b4f40017a4..c72ff518e709fa 100644 --- a/llvm/lib/Target/RISCV/RISCVInstrInfoP.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoP.td @@ -125,6 +125,7 @@ class RISCVUImmNegLeaf<int bitsNum> def uimm4_neg : RISCVUImmNegLeaf<4>; def uimm5_neg : RISCVUImmNegLeaf<5>; +def uimm6_neg : RISCVUImmNegLeaf<6>; //===----------------------------------------------------------------------===// // Instruction class templates @@ -2106,7 +2107,9 @@ def riscv_pm2addasu : RVSDNode<"PM2ADDASU", SDT_RISCVHorizontalMulAcc>; def riscv_mulq : RVSDNode<"MULQ", SDTIntBinOp, [SDNPCommutative]>; def riscv_mulqr : RVSDNode<"MULQR", SDTIntBinOp, [SDNPCommutative]>; -def SDT_RISCVPackedShift : SDTypeProfile<1, 2, [SDTCisVec<0>, +// Shift amount is a scalar. The shifted value is a packed vector, or a scalar +// for the 32-bit scalar saturating shifts. +def SDT_RISCVPackedShift : SDTypeProfile<1, 2, [SDTCisInt<0>, SDTCisSameAs<0, 1>, SDTCisVT<2, XLenVT>]>; def riscv_psll : RVSDNode<"PSLL", SDT_RISCVPackedShift>; @@ -2117,6 +2120,13 @@ def riscv_psshar : RVSDNode<"PSSHAR", SDT_RISCVPackedShift>; def riscv_psshl : RVSDNode<"PSSHL", SDT_RISCVPackedShift>; def riscv_psshlr : RVSDNode<"PSSHLR", SDT_RISCVPackedShift>; +// Non-saturating 64-bit shifts. The shift amount is a signed value taken from +// the low 8 bits of rs2. +def riscv_sha : RVSDNode<"SHA", SDTIntShiftOp>; +def riscv_shar : RVSDNode<"SHAR", SDTIntShiftOp>; +def riscv_shl : RVSDNode<"SHL", SDTIntShiftOp>; +def riscv_shlr : RVSDNode<"SHLR", SDTIntShiftOp>; + // RV32 packed narrowing shift. def SDT_RISCVPackedNarrowingShift : SDTypeProfile<1, 2, [SDTCisVec<0>, SDTCisVec<1>, @@ -2611,6 +2621,16 @@ let append Predicates = [IsRV32] in { def : PatGprImm<sshlsat, SSLAI, uimm5>; // No SSLLI + // 32-bit saturating and rounding shift patterns + def : PatGprImm<riscv_pssha, SSLAI, uimm5>; + def : PatGprImm<riscv_psshar, SSLAI, uimm5>; + def : PatGprImm<riscv_pssha, SRAI, uimm5_neg>; + def : PatGprImm<riscv_psshar, SRARI_RV32, uimm5_neg>; + def : PatGprShiftMask<riscv_pssha, SSHA, shiftMask256>; + def : PatGprShiftMask<riscv_psshar, SSHAR, shiftMask256>; + def : PatGprShiftMask<riscv_psshl, SSHL, shiftMask256>; + def : PatGprShiftMask<riscv_psshlr, SSHLR, shiftMask256>; + def : Pat<(XLenVT (saddsat (sshlsat GPR:$rs1, (XLenVT 1)), GPR:$rs2)), (SSH1SADD GPR:$rs1, GPR:$rs2)>; def : Pat<(XLenVT (saddsat (saddsat GPR:$rs1, GPR:$rs1), GPR:$rs2)), @@ -3707,10 +3727,20 @@ let append Predicates = [IsRV64] in { def : PatGprImm<riscv_psshar, PSSLAI_W, uimm5, v2i32>; def : PatGprImm<riscv_pssha, PSRAI_W, uimm5_neg, v2i32>; def : PatGprImm<riscv_psshar, PSRARI_W, uimm5_neg, v2i32>; - def : PatGprShift<riscv_pssha, PSSHA_WS, v2i32>; - def : PatGprShift<riscv_psshar, PSSHAR_WS, v2i32>; - def : PatGprShift<riscv_psshl, PSSHL_WS, v2i32>; - def : PatGprShift<riscv_psshlr, PSSHLR_WS, v2i32>; + def : PatGprShiftMask<riscv_pssha, PSSHA_WS, shiftMask256, v2i32>; + def : PatGprShiftMask<riscv_psshar, PSSHAR_WS, shiftMask256, v2i32>; + def : PatGprShiftMask<riscv_psshl, PSSHL_WS, shiftMask256, v2i32>; + def : PatGprShiftMask<riscv_psshlr, PSSHLR_WS, shiftMask256, v2i32>; + + // 64-bit shift patterns + def : PatGprImm<riscv_sha, SLLI, uimm6>; + def : PatGprImm<riscv_shar, SLLI, uimm6>; + def : PatGprImm<riscv_sha, SRAI, uimm6_neg>; + def : PatGprImm<riscv_shar, SRARI_RV64, uimm6_neg>; + def : PatGprShiftMask<riscv_sha, SHA, shiftMask256>; + def : PatGprShiftMask<riscv_shar, SHAR, shiftMask256>; + def : PatGprShiftMask<riscv_shl, SHL, shiftMask256>; + def : PatGprShiftMask<riscv_shlr, SHLR, shiftMask256>; // 32-bit packed saturation patterns def : PatGprImm<riscv_sati, PSATI_W, sati_width5, v2i32>; diff --git a/llvm/test/CodeGen/RISCV/rvp-scalar-rv64.ll b/llvm/test/CodeGen/RISCV/rvp-scalar-rv64.ll index bc36f912f77859..fcecd9b622d3d2 100644 --- a/llvm/test/CodeGen/RISCV/rvp-scalar-rv64.ll +++ b/llvm/test/CodeGen/RISCV/rvp-scalar-rv64.ll @@ -1688,3 +1688,115 @@ define signext i32 @ssh1sadd_intrinsic_i32(i32 signext %a, i32 signext %b) nounw %res = call i32 @llvm.riscv.ssh1sadd.i32(i32 %a, i32 %b) ret i32 %res } + +define i64 @test_sha_i64(i64 %a, i32 %shamt) { +; CHECK-LABEL: test_sha_i64: +; CHECK: # %bb.0: +; CHECK-NEXT: sha a0, a0, a1 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.sha.i64(i64 %a, i32 %shamt) + ret i64 %res +} + +define i64 @test_sha_i64_imm(i64 %a) { +; CHECK-LABEL: test_sha_i64_imm: +; CHECK: # %bb.0: +; CHECK-NEXT: slli a0, a0, 3 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.sha.i64(i64 %a, i32 3) + ret i64 %res +} + +define i64 @test_sha_i64_negimm(i64 %a) { +; CHECK-LABEL: test_sha_i64_negimm: +; CHECK: # %bb.0: +; CHECK-NEXT: srai a0, a0, 3 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.sha.i64(i64 %a, i32 -3) + ret i64 %res +} + +define i64 @test_sha_i64_mask(i64 %a, i32 %shamt) { +; CHECK-LABEL: test_sha_i64_mask: +; CHECK: # %bb.0: +; CHECK-NEXT: sha a0, a0, a1 +; CHECK-NEXT: ret + %m = and i32 %shamt, 255 + %res = call i64 @llvm.riscv.sha.i64(i64 %a, i32 %m) + ret i64 %res +} + +define i64 @test_shar_i64(i64 %a, i32 %shamt) { +; CHECK-LABEL: test_shar_i64: +; CHECK: # %bb.0: +; CHECK-NEXT: shar a0, a0, a1 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.shar.i64(i64 %a, i32 %shamt) + ret i64 %res +} + +define i64 @test_shar_i64_imm(i64 %a) { +; CHECK-LABEL: test_shar_i64_imm: +; CHECK: # %bb.0: +; CHECK-NEXT: slli a0, a0, 3 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.shar.i64(i64 %a, i32 3) + ret i64 %res +} + +define i64 @test_shar_i64_negimm(i64 %a) { +; CHECK-LABEL: test_shar_i64_negimm: +; CHECK: # %bb.0: +; CHECK-NEXT: srari a0, a0, 3 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.shar.i64(i64 %a, i32 -3) + ret i64 %res +} + +define i64 @test_shl_u64(i64 %a, i32 %shamt) { +; CHECK-LABEL: test_shl_u64: +; CHECK: # %bb.0: +; CHECK-NEXT: shl a0, a0, a1 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.shl.u64(i64 %a, i32 %shamt) + ret i64 %res +} + +define i64 @test_shlr_u64(i64 %a, i32 %shamt) { +; CHECK-LABEL: test_shlr_u64: +; CHECK: # %bb.0: +; CHECK-NEXT: shlr a0, a0, a1 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.shlr.u64(i64 %a, i32 %shamt) + ret i64 %res +} + +define i64 @test_shl_u64_imm(i64 %a) { +; CHECK-LABEL: test_shl_u64_imm: +; CHECK: # %bb.0: +; CHECK-NEXT: li a1, 3 +; CHECK-NEXT: shl a0, a0, a1 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.shl.u64(i64 %a, i32 3) + ret i64 %res +} + +define i64 @test_shlr_u64_negimm(i64 %a) { +; CHECK-LABEL: test_shlr_u64_negimm: +; CHECK: # %bb.0: +; CHECK-NEXT: li a1, -3 +; CHECK-NEXT: shlr a0, a0, a1 +; CHECK-NEXT: ret + %res = call i64 @llvm.riscv.shlr.u64(i64 %a, i32 -3) + ret i64 %res +} + +define signext i32 @ssha_intrinsic_i32_signext(i32 signext %a, i32 signext %shamt) nounwind { +; CHECK-LABEL: ssha_intrinsic_i32_signext: +; CHECK: # %bb.0: +; CHECK-NEXT: pssha.ws a0, a0, a1 +; CHECK-NEXT: sext.w a0, a0 +; CHECK-NEXT: ret + %res = call i32 @llvm.riscv.ssha.i32(i32 %a, i32 %shamt) + ret i32 %res +} diff --git a/llvm/test/CodeGen/RISCV/rvp-scalar.ll b/llvm/test/CodeGen/RISCV/rvp-scalar.ll index 13579bf2b9a11a..406691c9f9a671 100644 --- a/llvm/test/CodeGen/RISCV/rvp-scalar.ll +++ b/llvm/test/CodeGen/RISCV/rvp-scalar.ll @@ -3938,6 +3938,165 @@ define signext i32 @test_usati_u32_w31_signext(i32 signext %a) { ret i32 %res } +define i32 @test_ssha_i32(i32 %a, i32 %shamt) { +; RV32-LABEL: test_ssha_i32: +; RV32: # %bb.0: +; RV32-NEXT: ssha a0, a0, a1 +; RV32-NEXT: ret +; +; RV64-LABEL: test_ssha_i32: +; RV64: # %bb.0: +; RV64-NEXT: pssha.ws a0, a0, a1 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.ssha.i32(i32 %a, i32 %shamt) + ret i32 %res +} + +define i32 @test_ssha_i32_imm(i32 %a) { +; RV32-LABEL: test_ssha_i32_imm: +; RV32: # %bb.0: +; RV32-NEXT: sslai a0, a0, 3 +; RV32-NEXT: ret +; +; RV64-LABEL: test_ssha_i32_imm: +; RV64: # %bb.0: +; RV64-NEXT: psslai.w a0, a0, 3 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.ssha.i32(i32 %a, i32 3) + ret i32 %res +} + +define i32 @test_ssha_i32_negimm(i32 %a) { +; RV32-LABEL: test_ssha_i32_negimm: +; RV32: # %bb.0: +; RV32-NEXT: srai a0, a0, 3 +; RV32-NEXT: ret +; +; RV64-LABEL: test_ssha_i32_negimm: +; RV64: # %bb.0: +; RV64-NEXT: psrai.w a0, a0, 3 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.ssha.i32(i32 %a, i32 -3) + ret i32 %res +} + +define i32 @test_ssha_i32_mask(i32 %a, i32 %shamt) { +; RV32-LABEL: test_ssha_i32_mask: +; RV32: # %bb.0: +; RV32-NEXT: ssha a0, a0, a1 +; RV32-NEXT: ret +; +; RV64-LABEL: test_ssha_i32_mask: +; RV64: # %bb.0: +; RV64-NEXT: pssha.ws a0, a0, a1 +; RV64-NEXT: ret + %m = and i32 %shamt, 255 + %res = call i32 @llvm.riscv.ssha.i32(i32 %a, i32 %m) + ret i32 %res +} + +define i32 @test_sshar_i32(i32 %a, i32 %shamt) { +; RV32-LABEL: test_sshar_i32: +; RV32: # %bb.0: +; RV32-NEXT: sshar a0, a0, a1 +; RV32-NEXT: ret +; +; RV64-LABEL: test_sshar_i32: +; RV64: # %bb.0: +; RV64-NEXT: psshar.ws a0, a0, a1 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.sshar.i32(i32 %a, i32 %shamt) + ret i32 %res +} + +define i32 @test_sshar_i32_imm(i32 %a) { +; RV32-LABEL: test_sshar_i32_imm: +; RV32: # %bb.0: +; RV32-NEXT: sslai a0, a0, 3 +; RV32-NEXT: ret +; +; RV64-LABEL: test_sshar_i32_imm: +; RV64: # %bb.0: +; RV64-NEXT: psslai.w a0, a0, 3 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.sshar.i32(i32 %a, i32 3) + ret i32 %res +} + +define i32 @test_sshar_i32_negimm(i32 %a) { +; RV32-LABEL: test_sshar_i32_negimm: +; RV32: # %bb.0: +; RV32-NEXT: srari a0, a0, 3 +; RV32-NEXT: ret +; +; RV64-LABEL: test_sshar_i32_negimm: +; RV64: # %bb.0: +; RV64-NEXT: psrari.w a0, a0, 3 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.sshar.i32(i32 %a, i32 -3) + ret i32 %res +} + +define i32 @test_sshl_u32(i32 %a, i32 %shamt) { +; RV32-LABEL: test_sshl_u32: +; RV32: # %bb.0: +; RV32-NEXT: sshl a0, a0, a1 +; RV32-NEXT: ret +; +; RV64-LABEL: test_sshl_u32: +; RV64: # %bb.0: +; RV64-NEXT: psshl.ws a0, a0, a1 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.sshl.u32(i32 %a, i32 %shamt) + ret i32 %res +} + +define i32 @test_sshlr_u32(i32 %a, i32 %shamt) { +; RV32-LABEL: test_sshlr_u32: +; RV32: # %bb.0: +; RV32-NEXT: sshlr a0, a0, a1 +; RV32-NEXT: ret +; +; RV64-LABEL: test_sshlr_u32: +; RV64: # %bb.0: +; RV64-NEXT: psshlr.ws a0, a0, a1 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.sshlr.u32(i32 %a, i32 %shamt) + ret i32 %res +} + +define i32 @test_sshl_u32_imm(i32 %a) { +; RV32-LABEL: test_sshl_u32_imm: +; RV32: # %bb.0: +; RV32-NEXT: li a1, 3 +; RV32-NEXT: sshl a0, a0, a1 +; RV32-NEXT: ret +; +; RV64-LABEL: test_sshl_u32_imm: +; RV64: # %bb.0: +; RV64-NEXT: li a1, 3 +; RV64-NEXT: psshl.ws a0, a0, a1 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.sshl.u32(i32 %a, i32 3) + ret i32 %res +} + +define i32 @test_sshlr_u32_negimm(i32 %a) { +; RV32-LABEL: test_sshlr_u32_negimm: +; RV32: # %bb.0: +; RV32-NEXT: li a1, -3 +; RV32-NEXT: sshlr a0, a0, a1 +; RV32-NEXT: ret +; +; RV64-LABEL: test_sshlr_u32_negimm: +; RV64: # %bb.0: +; RV64-NEXT: li a1, -3 +; RV64-NEXT: psshlr.ws a0, a0, a1 +; RV64-NEXT: ret + %res = call i32 @llvm.riscv.sshlr.u32(i32 %a, i32 -3) + ret i32 %res +} + ; Test that pli.b is rematerialized rather than spilled define i32 @test_pli_b_remat(ptr %p) nounwind { ; RV32-LABEL: test_pli_b_remat: _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
