On 7/10/25 8:44 AM, Dusan Stojkovic wrote:
This peephole pattern combines the following instructions:
bswap8:
         rev8    a5,a0
         -> li      a4,-65536
         -> srai    a5,a5,32
         -> and     a5,a5,a4
         -> roriw   a5,a5,16
         and     a0,a0,a4
         or      a0,a0,a5
         sext.w  a0,a0
         ret

And emits this assembly:
bswap8:
         rev8    a5,a0
         -> li      a4,-65536
         -> srai    a5,a5,48
         and     a0,a0,a4
         or      a0,a0,a5
         sext.w  a0,a0
         ret

Since the load instruction is required for the rest of the test function
in the PR, the pattern conserves the load.

2025-07-10  Dusan Stojkovic  <dusan.stojko...@rt-rk.com>

         PR target/120920

gcc/ChangeLog:

         * config/riscv/peephole.md: New pattern.

gcc/testsuite/ChangeLog:

         * gcc.target/riscv/zbb_bswap8.c: New test.
So I'm not sure this transformation is correct.


Let's consider the case where a5 has the value 0xffff000000000000 at the "li" instruction.

a5 = 0xff00000000000000

li a4, -65536  // a4 = 0xffffffffffff0000
srai a5,a5,32  // a5 = 0xffffffffff000000
and a5,a5,a4   // a5 = 0xffffffffff000000
roriw a5,a5,16 // a5 = 0x000000000000ff00

So with your change:

a5 = 0xff00000000000000
li a4, -65536 // a4 = 0xffffffffffff0000
srai a5,a5,48 // a5 = 0xfffffffffffff000

Am I missing something?

Jeff

Reply via email to