]> git.ipfire.org Git - thirdparty/gcc.git/commit
PR tree-optimization/101403: Incorrect folding of ((T)bswap(x))>>C
authorRoger Sayle <roger@nextmovesoftware.com>
Mon, 12 Jul 2021 07:24:27 +0000 (08:24 +0100)
committerRoger Sayle <roger@nextmovesoftware.com>
Mon, 12 Jul 2021 07:27:57 +0000 (08:27 +0100)
commit5f5fbb550af7d9d6cb56ae8f607fea0eccaa9295
tree8b92e6fbc3230412e2950effaa2fa31386d39f7e
parentd55eee24a9f5e04d2b71e98b77347924a3cebd05
PR tree-optimization/101403: Incorrect folding of ((T)bswap(x))>>C

My sincere apologies for the breakage.  My recent patch to fold
bswapN(x)>>C where the constant C was large enough that the result
only contains bits from the low byte, and can therefore avoid
the byte swap contains a minor logic error.  The pattern contains
a convert? allowing an extension to occur between the bswap and
the shift.  The logic is correct if there's no extension, or the
extension has the same sign as the shift, but I'd mistakenly
convinced myself that these couldn't have different signedness.

(T)bswap16(x)>>12 is (T)((unsigned char)x>>4) or (T)((signed char)x>>4).
The bug is that for zero-extensions to signed type T, we need to use
the unsigned char variant [the signedness of the byte shift is not
(always) the same as the signedness of T and the original shift].

Then because I'm now paranoid, I've also added a clause to handle
the hypothetical (but in practice impossible) sign-extension to an
unsigned type T, which can implemented as (T)(x<<8)>>12.

2021-07-12  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
PR tree-optimization/101403
* match.pd ((T)bswap(X)>>C): Correctly handle cases where
signedness of the shift is not the same as the signedness of
the type extension.

gcc/testsuite/ChangeLog
PR tree-optimization/101403
* gcc.dg/pr101403.c: New test case.
gcc/match.pd
gcc/testsuite/gcc.dg/pr101403.c [new file with mode: 0644]