]> git.ipfire.org Git - thirdparty/gcc.git/commit
[RISC-V][PR target/119971] Avoid losing shift count masking
authorJeff Law <jlaw@ventanamicro.com>
Mon, 5 May 2025 23:14:29 +0000 (17:14 -0600)
committerJeff Law <jlaw@ventanamicro.com>
Fri, 20 Jun 2025 13:43:19 +0000 (07:43 -0600)
commitbf284e8f5edf0d039b1dd9af5d62e355ce85a7ba
tree71e1da70a62d8e5e5f0642a40c6e407d73ad2931
parent6583c3be41a7a6e3404d9eeb823addf3854609f4
[RISC-V][PR target/119971] Avoid losing shift count masking

As is outlined in the PR, we have a few define_insn_and_split patterns which
optimize away explicit masking of shift/bit positions when the masking matches
what the hardware's behavior.

A small number of those define_insn_and_split patterns generate a single
instruction.  It's fairly elegant in that we were essentially just rewriting
the RTL to match an existing pattern.

In one case we'd do the rewriting and later turn a 32bit shift into a bset.
That's not safe because the masking of a 32bit shift uses 0x1f while masking on
bset uses 0x3f on rv64.   The net was incorrect code as seen in the BZ entry.

The fix is pretty simple.  There's no real reason we need to use a
define_insn_and_split.  It was just convenient.  Instead we can use a simple
define_insn.  That avoids a change in the masking behavior for the shift
count/bit position and the masking stays in the RTL.

I quickly scanned the entire port and didn't see any additional
define_insn_and_splits that obviously generated a single instruction outside
the shift/rotate space, though in the vector space that's nontrivial to
ascertain.

This was been run through my tester for the cross configurations, but not the
native bootstrap/regression test (yet).

PR target/119971
gcc/
* config/riscv/bitmanip.md (rotation with masked count): Rewrite
as define_insn patterns.  Fix formatting.
* config/riscv/riscv.md (shift with masked count): Similarly.

gcc/testsuite
* gcc.target/riscv/pr119971.c: New test.
* gcc.target/riscv/zbb-rol-ror-03.c: Adjust test slightly.

(cherry picked from commit 05d75c5bfcf923bc0258b79a08c5861590c5a2b9)
gcc/config/riscv/bitmanip.md
gcc/config/riscv/riscv.md
gcc/testsuite/gcc.target/riscv/pr119971.c [new file with mode: 0644]
gcc/testsuite/gcc.target/riscv/zbb-rol-ror-03.c