[Bug tree-optimization/102738] Failure to optimize right shift of 32-bit int after it's already been shifted by 31

pinskia at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Oct 14 00:49:26 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102738

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-10-14
            Summary|Failure to optimize right   |Failure to optimize right
                   |shift of 128-bit value      |shift of 32-bit int after
                   |after it's already been     |it's already been shifted
                   |shifted by 127              |by 31
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
GCC does not even do the int case:

int a(int f, int g)
{
    return (f >> 31) >> g;
}


Actually this can be simplified down to this really (which clang does not
handle but handles a simular case; see below):
int a(int f, int g)
{
    if (f == 0 || f == -1)
    return f >> g;
    __builtin_unreachable();
}

This should just return f here :).
Basically if GCC knows f is already 0 or -1, then f shifted by any value is
still f.

clang is able to handle this case though
int a1(int f, int g)
{
    if (f == 0 || f == 1)
    return (-f) >> g;
    __builtin_unreachable();
}


More information about the Gcc-bugs mailing list