[PATCH] middle-end: Simplify popcount/parity of bswap/rotate.

Marc Glisse marc.glisse@inria.fr
Sat Aug 22 13:03:43 GMT 2020

On Fri, 21 Aug 2020, Roger Sayle wrote:

> This simple patch to match.pd optimizes away bit permutation
> operations, specifically bswap and rotate, in calls to popcount and
> parity.

Good idea.

> Although this patch has been developed and tested on LP64,
> it relies on there being no truncations or extensions to "marry up"
> the appropriate PARITY, PARITYL and PARITYLL forms with either BSWAP32
> or BSWAP64, assuming this transformation won't fire if the integral
> types have different sizes.

There would be a convert_expr or similar if the sizes were different, so 
you are safe.

I wish we had only generic builtins/ifn instead of
__builtin_parity{,l,ll,imax}, and inconsistently __builtin_bswap{16,32,64,128},
that's very inconvenient to deal with... And genmatch generates a lot of
useless code because of that.

I didn't try but couldn't the transformations be merged to reduce repetition?

(for bitcnt (POPCOUNT PARITY)
    (bitcnt (swap @0))
    (bitcnt @0)))
  (for rot (lrotate rrotate)
    (bitcnt (rot @0 @1))
    (bitcnt @0))))

I assume you skipped BUILT_IN_BSWAP16 because on 32+ bit targets, there
is an intermediate extension, and 16 bit targets are "rare"? And
BUILT_IN_BSWAP128 because on most platforms intmax_t is only 64 bits and
we don't have a 128-bit version of parity/popcount? (we have an IFN, but
it seldom appears by magic)

Marc Glisse

More information about the Gcc-patches mailing list