]> git.ipfire.org Git - thirdparty/gcc.git/commit
[PR target/121778] Improving rotation detection
authorShreya Munnangi <smunnangi1@ventanamicro.com>
Fri, 9 Jan 2026 04:29:38 +0000 (21:29 -0700)
committerJeff Law <jeffrey.law@oss.qualcomm.com>
Fri, 9 Jan 2026 04:31:42 +0000 (21:31 -0700)
commit4fbc0bbc03162f3962ea79bac29d36952867c90f
treee673d154dd48f02d1aa1e0522164bf9adaf9f8ef
parent8265192910c8b05162ae7672d8fac85c1639c0c5
[PR target/121778] Improving rotation detection

In this PR we're getting code like this out of the gimple optimizers:

>   _1 = a_4(D) << 63;
>   _2 = a_4(D) >> 1;
>   _3 = _2 ^ 1;
>   _5 = _1 | _3;

Note the XOR in that sequence.  It spoils our ability to recognize the
rotation.  As a result we get code like this for rv64gcb:

>         srli    a5,a0,1
>         xori    a5,a5,1
>         slli    a0,a0,63
>         or      a0,a5,a0

We can reassociate the operations when the XOR only flips bits resulting from
the right or left shift, but not both.  So after reassociation in gimple we
get:

>   _1 = a_2(D) r>> 1;
>   _3 = _1 ^ 1;

Which results in:

>         rori    a0,a0,1
>         xori    a0,a0,1

We don't bother with the transformation when the XOR is flipping a bit known to
be zero (ie, a high bit of the result of the right shift or a low bit on the
result of the left shift).  For those cases we already figure out that the XOR
is just an IOR and the right things already "just happen".

This triggered some code generation changes on the SH (not surprising because
this BZ was derived from an older SH BZ).  It doesn't seem to significantly
improve the SH code, though it does turn a cmp/pz + rotate through carry with a
rotate + xor with immediate.    That may be a latency win on the SH, I really
don't know.

Shreya did the bulk of the work here.  My contribution was the sister pattern
which has the XOR on the other operand and testcase development.

Bootstrapped and regression tested on x86 & riscv.  Also tested across the
various embedded targets without any regressions.

PR target/121778
gcc/
* match.pd: Add pattern to recognize rotate with one or more
bits flipped via xor.
* config/sh/sh.md (*rotcl); New variant which handles the output
we get after the match.pd change above.

gcc/testsuite/
* gcc.target/riscv/pr121778.c: New test.

Co-Authored-By: Jeff Law <jeffrey.law@oss.qualcomm.com>
gcc/config/sh/sh.md
gcc/match.pd
gcc/testsuite/gcc.target/riscv/pr121778.c [new file with mode: 0644]