]> gcc.gnu.org Git - gcc.git/commit
Improved handling of shifts/rotates in bit CCP.
authorRoger Sayle <roger@nextmovesoftware.com>
Thu, 26 Aug 2021 17:57:00 +0000 (18:57 +0100)
committerRoger Sayle <roger@nextmovesoftware.com>
Thu, 26 Aug 2021 17:57:00 +0000 (18:57 +0100)
commitb2ef23239f245871e9b35b902391f2e94a041627
treeff0f4e31822a2de9031c62cca2bcd3df655b2179
parenta2d9b558299df91e9c34a583eb0f0b14d1cacce9
Improved handling of shifts/rotates in bit CCP.

This patch is the next in the series to improve bit bounds in tree-ssa's
bit CCP pass, this time: bounds for shifts and rotates by unknown amounts.
This allows us to optimize expressions such as ((x&15)<<(y&24))&64.
In this case, the expression (y&24) contains only two unknown bits,
and can therefore have only four possible values: 0, 8, 16 and 24.
From this (x&15)<<(y&24) has the nonzero bits 0x0f0f0f0f, and from
that ((x&15)<<(y&24))&64 must always be zero.

One clever use of computer science in this patch is the use of XOR
to efficiently enumerate bit patterns in Gray code order.  As the
order in which we generate values is not significant, it's faster
and more convenient to enumerate values by flipping one bit at a
time, rather than in numerical order [which would require carry
bits and additional logic].

There's a pre-existing ??? comment in tree-ssa-ccp.c that we should
eventually be able to optimize (x<<(y|8))&255, but this patch takes the
conservatively paranoid approach of only optimizing cases where the
shift/rotate is guaranteed to be less than the target precision, and
therefore avoids changing any cases that potentially might invoke
undefined behavior.  This patch does optimize (x<<((y&31)|8))&255.

2021-08-26  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
* tree-ssa-ccp.c (get_individual_bits): Helper function to
extract the individual bits from a widest_int constant (mask).
(gray_code_bit_flips): New read-only table for effiently
enumerating permutations/combinations of bits.
(bit_value_binop) [LROTATE_EXPR, RROTATE_EXPR]: Handle rotates
by unknown counts that are guaranteed less than the target
precision and four or fewer unknown bits by enumeration.
[LSHIFT_EXPR, RSHIFT_EXPR]: Likewise, also handle shifts by
enumeration under the same conditions.  Handle remaining
shifts as a mask based upon the minimum possible shift value.

gcc/testsuite/ChangeLog
* gcc.dg/tree-ssa/ssa-ccp-41.c: New test case.
gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-41.c [new file with mode: 0644]
gcc/tree-ssa-ccp.c
This page took 0.066534 seconds and 5 git commands to generate.