Skip to content

Commit 9dd0c72

Browse files
apinski-caviumstephanosio
authored andcommitted
Fix PR 106601: __builtin_bswap16 code gen could be improved with ZBB enabled
The default expansion for bswap16 is two extractions (shift/and) followed by an insertation (ior) and then a zero extend. This can be improved with ZBB enabled to just full byteswap followed by a (logical) shift right. This patch adds a new pattern for this which does that. OK? Built and tested on riscv32-linux-gnu and riscv64-linux-gnu. gcc/ChangeLog: PR target/106601 * config/riscv/bitmanip.md (bswaphi2): New pattern. gcc/testsuite/ChangeLog: PR target/106601 * gcc.target/riscv/zbb_32_bswap-2.c: New test. * gcc.target/riscv/zbb_bswap-2.c: New test. (cherry picked from commit e5e6983) Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
1 parent b01b6dc commit 9dd0c72

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

gcc/config/riscv/bitmanip.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,30 @@
232232
"rev8\t%0,%1"
233233
[(set_attr "type" "bitmanip")])
234234

235+
;; HI bswap can be emulated using SI/DI bswap followed
236+
;; by a logical shift right
237+
;; SI bswap for TARGET_64BIT is already similarly in
238+
;; the common code.
239+
(define_expand "bswaphi2"
240+
[(set (match_operand:HI 0 "register_operand" "=r")
241+
(bswap:HI (match_operand:HI 1 "register_operand" "r")))]
242+
"TARGET_ZBB"
243+
{
244+
rtx tmp = gen_reg_rtx (word_mode);
245+
rtx newop1 = gen_lowpart (word_mode, operands[1]);
246+
if (TARGET_64BIT)
247+
emit_insn (gen_bswapdi2 (tmp, newop1));
248+
else
249+
emit_insn (gen_bswapsi2 (tmp, newop1));
250+
rtx tmp1 = gen_reg_rtx (word_mode);
251+
if (TARGET_64BIT)
252+
emit_insn (gen_lshrdi3 (tmp1, tmp, GEN_INT (64 - 16)));
253+
else
254+
emit_insn (gen_lshrsi3 (tmp1, tmp, GEN_INT (32 - 16)));
255+
emit_move_insn (operands[0], gen_lowpart (HImode, tmp1));
256+
DONE;
257+
})
258+
235259
(define_insn "<bitmanip_optab><mode>3"
236260
[(set (match_operand:X 0 "register_operand" "=r")
237261
(bitmanip_minmax:X (match_operand:X 1 "register_operand" "r")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-march=rv32gc_zbb -mabi=ilp32" } */
3+
/* { dg-skip-if "" { *-*-* } { "-O0" } } */
4+
5+
int foo(int n)
6+
{
7+
return __builtin_bswap16(n);
8+
}
9+
10+
/* { dg-final { scan-assembler "rev8" } } */
11+
/* { dg-final { scan-assembler "srli" } } */
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-march=rv64gc_zbb -mabi=lp64" } */
3+
/* { dg-skip-if "" { *-*-* } { "-O0" } } */
4+
5+
int foo(int n)
6+
{
7+
return __builtin_bswap16(n);
8+
}
9+
10+
/* { dg-final { scan-assembler "rev8" } } */
11+
/* { dg-final { scan-assembler "srli" } } */
12+

0 commit comments

Comments
 (0)