Eliminate separate count negations for native and word rotations.
gcc/ChangeLog:
* config/riscv/bitmanip.md
(*<bitmanip_optab><GPR:mode>3_neg_mask): New pattern.
(*<bitmanip_optab>si3_sext_neg_mask): Likewise.
* config/riscv/iterators.md (bitmanip_insn_rev): New attribute.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/zbb-rol-ror-10.c: New test.
---
diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md
index
d710d594def0fcc41a6109c9eadef7704d24486e..912e789782b2982ff80450d8a8e16a8dba91aef7
100644
--- a/gcc/config/riscv/bitmanip.md
+++ b/gcc/config/riscv/bitmanip.md
@@ -513,6 +513,39 @@
"<bitmanip_insn>w\t%0,%1,%2"
[(set_attr "type" "bitmanip")
(set_attr "mode" "DI")])
+
+;; Negating the low bits of a rotate count reverses the direction. The
+;; mask must preserve all count bits used by the rotated value's mode.
+(define_insn "*<bitmanip_optab><GPR:mode>3_neg_mask"
+ [(set (match_operand:GPR 0 "register_operand" "=r")
+ (bitmanip_rotate:GPR
+ (match_operand:GPR 1 "register_operand" "r")
+ (match_operator 4 "subreg_lowpart_operator"
+ [(and:GPR2
+ (neg:GPR2 (match_operand:GPR2 2 "register_operand" "r"))
+ (match_operand 3 "<GPR:shiftm1>" "<GPR:shiftm1p>"))])))]
+ "TARGET_ZBB || TARGET_ZBKB"
+{
+ if (E_<GPR:MODE>mode != word_mode)
+ return "<bitmanip_insn_rev>w\t%0,%1,%2";
+ return "<bitmanip_insn_rev>\t%0,%1,%2";
+}
+ [(set_attr "type" "bitmanip")
+ (set_attr "mode" "<GPR:MODE>")])
+
+(define_insn "*<bitmanip_optab>si3_sext_neg_mask"
+ [(set (match_operand:DI 0 "register_operand" "=r")
+ (sign_extend:DI
+ (bitmanip_rotate:SI
+ (match_operand:SI 1 "register_operand" "r")
+ (match_operator 4 "subreg_lowpart_operator"
+ [(and:GPR
+ (neg:GPR (match_operand:GPR 2 "register_operand" "r"))
+ (match_operand 3 "const_si_mask_operand" "DsS"))]))))]
+ "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
+ "<bitmanip_insn_rev>w\t%0,%1,%2"
+ [(set_attr "type" "bitmanip")
+ (set_attr "mode" "SI")])
;; orc.b (or-combine) is added as an unspec for the benefit of the support
;; for optimized string functions (such as strcmp).
diff --git a/gcc/config/riscv/iterators.md b/gcc/config/riscv/iterators.md
index
b53cb961ba068befa0286cdc6d3361372f5d4bcc..0d424eb4ee28f1e52de375535e6c5eb33313f943
100644
--- a/gcc/config/riscv/iterators.md
+++ b/gcc/config/riscv/iterators.md
@@ -436,6 +436,8 @@
(rotate "rol")
(rotatert "ror")])
+(define_code_attr bitmanip_insn_rev [(rotate "ror") (rotatert "rol")])
+
;; -------------------------------------------------------------------
;; Int Iterators.
;; -------------------------------------------------------------------
diff --git a/gcc/testsuite/gcc.target/riscv/zbb-rol-ror-10.c
b/gcc/testsuite/gcc.target/riscv/zbb-rol-ror-10.c
new file mode 100644
index
0000000000000000000000000000000000000000..2db53c6887fdf719dbcb0f0c2e52ae3691e4d704
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zbb-rol-ror-10.c
@@ -0,0 +1,44 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zbb -mabi=lp64" { target { rv64 } } } */
+/* { dg-options "-march=rv32gc_zbb -mabi=ilp32" { target { rv32 } } } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */
+
+unsigned long
+rol_neg (unsigned long x, unsigned long n)
+{
+ return __builtin_stdc_rotate_left (x, 0UL - (n & 255UL));
+}
+
+unsigned long
+ror_neg (unsigned long x, unsigned long n)
+{
+ return __builtin_stdc_rotate_right (x, 0UL - (n & 255UL));
+}
+
+/* This mask changes the effective count and must be retained. */
+unsigned long
+ror_neg_mask7 (unsigned long x, unsigned long n)
+{
+ return __builtin_stdc_rotate_right (x, 0UL - (n & 7UL));
+}
+
+#if __riscv_xlen == 64
+unsigned int
+rolw_neg (unsigned int x, unsigned int n)
+{
+ return __builtin_stdc_rotate_left (x, 0U - (n & 31U));
+}
+
+unsigned int
+rorw_neg (unsigned int x, unsigned int n)
+{
+ return __builtin_stdc_rotate_right (x, 0U - (n & 31U));
+}
+#endif
+
+/* { dg-final { scan-assembler-times "rol\t" 2 } } */
+/* { dg-final { scan-assembler-times "ror\t" 1 } } */
+/* { dg-final { scan-assembler-times "rolw\t" 1 { target { rv64 } } } } */
+/* { dg-final { scan-assembler-times "rorw\t" 1 { target { rv64 } } } } */
+/* { dg-final { scan-assembler-times "andi\t" 1 } } */
+/* { dg-final { scan-assembler-not "negw?\t" } } */