[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
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
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
@@ -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: