public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/106244] New: Failure to optimize (1 << x) & 1 to `x == 0` if separated into multiple statements
@ 2022-07-10  8:56 gabravier at gmail dot com
  2022-07-10 20:22 ` [Bug tree-optimization/106244] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: gabravier at gmail dot com @ 2022-07-10  8:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106244
           Summary: Failure to optimize (1 << x) & 1 to `x == 0` if
                    separated into multiple statements
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

#include <stdint.h>

int8_t f(int8_t x)
{
    int8_t sh = 1 << x;
    return sh & 1;
}

This can be optimized to `return x == 0;`. This transformation is done by LLVM,
but not by GCC.

PS: For some reason GCC manages to do this optimization if I replace `f` with
`return (1 << x) & 1;` instead of having it spelled out in 2 statements.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug tree-optimization/106244] Failure to optimize (1 << x) & 1 to `x == 0` if separated into multiple statements
  2022-07-10  8:56 [Bug tree-optimization/106244] New: Failure to optimize (1 << x) & 1 to `x == 0` if separated into multiple statements gabravier at gmail dot com
@ 2022-07-10 20:22 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-10 20:22 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-07-10
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Actually it is not optimized if it was in one statement:
int8_t f1(int8_t x)
{
    return ((int8_t)(1 << x)) & 1;
}

The IR has:
  _4 = (int) x_1(D);
  _5 = 1 << _4;
  sh_6 = (int8_t) _5;
  _7 = sh_6 & 1;

We optimize directly "(1 << x) & 1" but not with a cast.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-07-10 20:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-10  8:56 [Bug tree-optimization/106244] New: Failure to optimize (1 << x) & 1 to `x == 0` if separated into multiple statements gabravier at gmail dot com
2022-07-10 20:22 ` [Bug tree-optimization/106244] " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).