[clang] [llvm] [RISCV][P-ext] Add scalar saturating and rounding shift intrinsics (PR #241730)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 00:18:54 PST 2026


https://github.com/sihuan created https://github.com/llvm/llvm-project/pull/241730

Add the scalar intrinsics from the "Saturating and Rounding Shift" section of the P intrinsic spec: `__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`, `__riscv_shlr_u64`.

Shift amounts are a signed value taken from the low 8 bits, so negative amounts shift right. The 32-bit forms reuse the PSSHA/PSSHAR/PSSHL/PSSHLR nodes, which now also accept a scalar: RV32 selects `ssha`/`sshar`/`sshl`/`sshlr`, RV64 uses the `.ws` forms on the low word via `ReplaceNodeResults` like the other scalar i32 intrinsics. The 64-bit forms do not saturate and get new SHA/SHAR/SHL/SHLR nodes. Constant shift amounts select the immediate forms where the spec lists one (`sslai`/`srai`/`srari`, `psslai.w`/`psrai.w`/`psrari.w`, `slli`/`srai`/`srari`).

A dedicated intrinsic is needed instead of `llvm.sshl.sat`: P defines the result for negative shift amounts, while the IR intrinsic treats them as poison.

The packed 32-bit `.ws` saturating shift patterns now use `PatGprShiftMask`. Those instructions only read `rs2[7:0]`, so masking the shift amount is a no-op, and RV32 already folded it.

Tests: new cases in `llvm/test/CodeGen/RISCV/rvp-scalar.ll` and `rvp-scalar-rv64.ll` (variable, immediate, negative-immediate and masked shift amounts, both targets), `clang/test/CodeGen/RISCV/rvp-intrinsics.c` and the intrinsic header test.


>From f6b1844b80bb1d6c439367357aa2596e9d3f7a5f Mon Sep 17 00:00:00 2001
From: SiHuaN <liyongtai at iscas.ac.cn>
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.

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    | 102 +++++++++++
 llvm/test/CodeGen/RISCV/rvp-scalar.ll         | 159 ++++++++++++++++++
 10 files changed, 574 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/Basic/BuiltinsRISCV.td b/clang/include/clang/Basic/BuiltinsRISCV.td
index 19438ec8d6b0bb..a27566e0fec331 100644
--- a/clang/include/clang/Basic/BuiltinsRISCV.td
+++ b/clang/include/clang/Basic/BuiltinsRISCV.td
@@ -279,6 +279,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 19885655bfba0d..4b8b5466cee30c 100644
--- a/clang/lib/CodeGen/TargetBuiltins/RISCV.cpp
+++ b/clang/lib/CodeGen/TargetBuiltins/RISCV.cpp
@@ -1700,6 +1700,32 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
     ID = Intrinsic::riscv_asubu_u32;
     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 4459bd7e715fc0..fee28f8aeb3f35 100644
--- a/clang/lib/Headers/riscv_packed_simd.h
+++ b/clang/lib/Headers/riscv_packed_simd.h
@@ -490,6 +490,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 c91f3b674ab407..d1c0731e0ec86e 100644
--- a/clang/test/CodeGen/RISCV/rvp-intrinsics.c
+++ b/clang/test/CodeGen/RISCV/rvp-intrinsics.c
@@ -420,6 +420,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 2357a1ff1ca5f6..de09a451f03861 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 c5a5d4b4a61c11..113ea5c0739497 100644
--- a/llvm/include/llvm/IR/IntrinsicsRISCV.td
+++ b/llvm/include/llvm/IR/IntrinsicsRISCV.td
@@ -2174,16 +2174,33 @@ 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;
   def int_riscv_asubu_u32 : 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 0d7be09555ed6e..20a1512abe8333 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -12440,6 +12440,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;
   }
 }
 
@@ -12497,7 +12505,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:
@@ -12506,6 +12514,14 @@ static unsigned getRVPScalarOpcode(unsigned IntNo) {
     return ISD::AVGSUBFLOORS;
   case Intrinsic::riscv_asubu_u32:
     return ISD::AVGSUBFLOORU;
+  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:
@@ -13349,7 +13365,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(),
@@ -13493,6 +13515,10 @@ SDValue RISCVTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
   case Intrinsic::riscv_aaddu_u32:
   case Intrinsic::riscv_asub_i32:
   case Intrinsic::riscv_asubu_u32:
+  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:
@@ -18608,6 +18634,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 2ecec70322234a..b7177895e96681 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
@@ -2103,7 +2104,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>;
@@ -2114,6 +2117,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>,
@@ -2608,6 +2618,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)),
@@ -3695,10 +3715,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 ff4d49db1aba7e..a5783c39a09cb2 100644
--- a/llvm/test/CodeGen/RISCV/rvp-scalar-rv64.ll
+++ b/llvm/test/CodeGen/RISCV/rvp-scalar-rv64.ll
@@ -1676,3 +1676,105 @@ define void @merge_wusers_v8i8(<8 x i8> %a, i8 %x, ptr %p) nounwind {
   store i32 %t, ptr %p
   ret void
 }
+
+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
+}
diff --git a/llvm/test/CodeGen/RISCV/rvp-scalar.ll b/llvm/test/CodeGen/RISCV/rvp-scalar.ll
index a68592f7ed7ae9..fc194a6514f0fb 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:



More information about the cfe-commits mailing list