[middle-end] Canonicalize bswap (bitreverse x) as bitreverse (bswap x)

Message ID 00c101dd3a44$ec475d70$c4d61850$@nextmovesoftware.com
State Committed
Commit 3456cc8dba75ea71ca099df75372f763bdb86d22
Headers
Series [middle-end] Canonicalize bswap (bitreverse x) as bitreverse (bswap x) |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap success Build passed
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap fail Patch failed to apply

Commit Message

Roger Sayle Sept. 1, 2026, 7:05 p.m. UTC
  I thought it best to split/ping this independent piece out of

my aop_optab patch.

 

This patch implements an RTL simplification (to help bitreverse on cris)

which is for simplify-rtx and combine to canonicalize bswap(bitreverse x)

as bitreverse(bswap x).  The two forms are equivalent, so canonicalizing

simplifies machine descriptions.  The (otherwise arbitrary) choice to

perform BSWAP first, is (1) to aid targets like powerpc that can perform

bswap on load and (2) to place bswap next to bswap on targets that split

bitreverse in a similar order to GCC's default optab expansion.

 

This patch has been tested on x86_64-pc-linux-gnu with make bootstrap

and make -k check, both with and without --target_board=unix{-m32}

with no new failures.  Ok for mainline?

 

 

2026-09-01  Roger Sayle  <roger@nextmovesoftware.com>

 

gcc/ChangeLog

        * simplify-rtx.cc (simplify_unary_operation_1) <case BSWAP>:

        Canonicalize (BSWAP (BITREVERSE x)) as (BITREVERSE (BSWAP x)).

 

Roger

--
  

Comments

Jeff Law Sept. 2, 2026, 1:23 p.m. UTC | #1
On 9/1/26 1:05 PM, Roger Sayle wrote:
>
> I thought it best to split/ping this independent piece out of
>
> my aop_optab patch.
>
> This patch implements an RTL simplification (to help bitreverse on cris)
>
> which is for simplify-rtx and combine to canonicalize bswap(bitreverse x)
>
> as bitreverse(bswap x).  The two forms are equivalent, so canonicalizing
>
> simplifies machine descriptions.  The (otherwise arbitrary) choice to
>
> perform BSWAP first, is (1) to aid targets like powerpc that can perform
>
> bswap on load and (2) to place bswap next to bswap on targets that split
>
> bitreverse in a similar order to GCC's default optab expansion.
>
> This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
>
> and make -k check, both with and without --target_board=unix{-m32}
>
> with no new failures.  Ok for mainline?
>
> 2026-09-01  Roger Sayle <roger@nextmovesoftware.com>
>
> gcc/ChangeLog
>
>         * simplify-rtx.cc (simplify_unary_operation_1) <case BSWAP>:
>
>         Canonicalize (BSWAP (BITREVERSE x)) as (BITREVERSE (BSWAP x)).
>
OK
jeff
  

Patch

diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
index 6f8ee53f209..ec67165101e 100644
--- a/gcc/simplify-rtx.cc
+++ b/gcc/simplify-rtx.cc
@@ -1505,6 +1505,12 @@  simplify_context::simplify_unary_operation_1 (rtx_code code, machine_mode mode,
       /* (bswap (bswap x)) -> x.  */
       if (GET_CODE (op) == BSWAP)
 	return XEXP (op, 0);
+      /* Canonicalize (bswap (bitreverse x)) as (bitreverse (bswap x)).  */
+      if (GET_CODE (op) == BITREVERSE)
+	return simplify_gen_unary (BITREVERSE, mode,
+				   simplify_gen_unary (BSWAP, mode,
+						       XEXP (op, 0), mode),
+				   mode);
       break;
 
     case BITREVERSE: