public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/106245] New: Failure to optimize (u8)(a << 7) >> 7 pattern to a & 1
@ 2022-07-10  9:30 gabravier at gmail dot com
  2022-07-10 19:46 ` [Bug tree-optimization/106245] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: gabravier at gmail dot com @ 2022-07-10  9:30 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106245
           Summary: Failure to optimize (u8)(a << 7) >> 7 pattern to a & 1
           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 a)
{
    return (uint8_t)(a << 7) >> 7;
}

This can be optimized to `return a & 1;`. This transformation is done by LLVM,
but not by GCC.

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

* [Bug tree-optimization/106245] Failure to optimize (u8)(a << 7) >> 7 pattern to a & 1
  2022-07-10  9:30 [Bug tree-optimization/106245] New: Failure to optimize (u8)(a << 7) >> 7 pattern to a & 1 gabravier at gmail dot com
@ 2022-07-10 19:46 ` pinskia at gcc dot gnu.org
  2023-10-11  7:09 ` cvs-commit at gcc dot gnu.org
  2023-10-20 23:08 ` cvs-commit at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-10 19:46 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-07-10
             Target|                            |x86_64-linux-gnu

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. Note on aarch64, it is optimized down to &1 on the RTL level.

The issue is we don't optimize:
int f(int a)
{
    return (a << 31) >> 31;
}

Into:
int f(int a)
{
    return -(a&1);
}


I thought I had saw a bug report about that but I can't find it.

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

* [Bug tree-optimization/106245] Failure to optimize (u8)(a << 7) >> 7 pattern to a & 1
  2022-07-10  9:30 [Bug tree-optimization/106245] New: Failure to optimize (u8)(a << 7) >> 7 pattern to a & 1 gabravier at gmail dot com
  2022-07-10 19:46 ` [Bug tree-optimization/106245] " pinskia at gcc dot gnu.org
@ 2023-10-11  7:09 ` cvs-commit at gcc dot gnu.org
  2023-10-20 23:08 ` cvs-commit at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-10-11  7:09 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Roger Sayle <sayle@gcc.gnu.org>:

https://gcc.gnu.org/g:c41492423140e1573df68d1c98e825ae7593741f

commit r14-4551-gc41492423140e1573df68d1c98e825ae7593741f
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Wed Oct 11 08:08:04 2023 +0100

    Optimize (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) as (and:SI x 1).

    This patch is the middle-end piece of an improvement to PRs 101955 and
    106245, that adds a missing simplification to the RTL optimizers.
    This transformation is to simplify (char)(x << 7) != 0 as x & 1.
    Technically, the cast can be any truncation, where shift is by one
    less than the narrower type's precision, setting the most significant
    (only) bit from the least significant bit.

    This transformation applies to any target, but it's easy to see
    (and add a new test case) on x86, where the following function:

    int f(int a) { return (a << 31) >> 31; }

    currently gets compiled with -O2 to:

    foo:    movl    %edi, %eax
            sall    $7, %eax
            sarb    $7, %al
            movsbl  %al, %eax
            ret

    but with this patch, we now generate the slightly simpler.

    foo:    movl    %edi, %eax
            sall    $31, %eax
            sarl    $31, %eax
            ret

    2023-10-11  Roger Sayle  <roger@nextmovesoftware.com>

    gcc/ChangeLog
            PR middle-end/101955
            PR tree-optimization/106245
            * simplify-rtx.cc (simplify_relational_operation_1): Simplify
            the RTL (ne:SI (subreg:QI (ashift:SI x 7) 0) 0) to (and:SI x 1).

    gcc/testsuite/ChangeLog
            * gcc.target/i386/pr106245-1.c: New test case.

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

* [Bug tree-optimization/106245] Failure to optimize (u8)(a << 7) >> 7 pattern to a & 1
  2022-07-10  9:30 [Bug tree-optimization/106245] New: Failure to optimize (u8)(a << 7) >> 7 pattern to a & 1 gabravier at gmail dot com
  2022-07-10 19:46 ` [Bug tree-optimization/106245] " pinskia at gcc dot gnu.org
  2023-10-11  7:09 ` cvs-commit at gcc dot gnu.org
@ 2023-10-20 23:08 ` cvs-commit at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-10-20 23:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Roger Sayle <sayle@gcc.gnu.org>:

https://gcc.gnu.org/g:e28869670c9879fe7c67caf6cc11af202509ef78

commit r14-4810-ge28869670c9879fe7c67caf6cc11af202509ef78
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Sat Oct 21 00:06:02 2023 +0100

    PR 106245: Split (x<<31)>>31 as -(x&1) in i386.md

    This patch is the backend piece of a solution to PRs 101955 and 106245,
    that adds a define_insn_and_split to the i386 backend, to perform sign
    extension of a single (least significant) bit using and $1 then neg.

    Previously, (x<<31)>>31 would be generated as

            sall    $31, %eax       // 3 bytes
            sarl    $31, %eax       // 3 bytes

    with this patch the backend now generates:

            andl    $1, %eax        // 3 bytes
            negl    %eax            // 2 bytes

    Not only is this smaller in size, but microbenchmarking confirms
    that it's a performance win on both Intel and AMD; Intel sees only a
    2% improvement (perhaps just a size effect), but AMD sees a 7% win.

    2023-10-21  Roger Sayle  <roger@nextmovesoftware.com>
                Uros Bizjak  <ubizjak@gmail.com>

    gcc/ChangeLog
            PR middle-end/101955
            PR tree-optimization/106245
            * config/i386/i386.md (*extv<mode>_1_0): New define_insn_and_split.

    gcc/testsuite/ChangeLog
            PR middle-end/101955
            PR tree-optimization/106245
            * gcc.target/i386/pr106245-2.c: New test case.
            * gcc.target/i386/pr106245-3.c: New 32-bit test case.
            * gcc.target/i386/pr106245-4.c: New 64-bit test case.
            * gcc.target/i386/pr106245-5.c: Likewise.

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

end of thread, other threads:[~2023-10-20 23:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-10  9:30 [Bug tree-optimization/106245] New: Failure to optimize (u8)(a << 7) >> 7 pattern to a & 1 gabravier at gmail dot com
2022-07-10 19:46 ` [Bug tree-optimization/106245] " pinskia at gcc dot gnu.org
2023-10-11  7:09 ` cvs-commit at gcc dot gnu.org
2023-10-20 23:08 ` cvs-commit 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).