public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/102738] New: Failure to optimize right shift of 128-bit value after it's already been shifted by 127
@ 2021-10-14  0:36 gabravier at gmail dot com
  2021-10-14  0:49 ` [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
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gabravier at gmail dot com @ 2021-10-14  0:36 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102738
           Summary: Failure to optimize right shift of 128-bit value after
                    it's already been shifted by 127
           Product: gcc
           Version: 12.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: ---

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

This can be optimized to `return f >> 127;`. This optimization is done by LLVM,
but not by GCC.

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

* [Bug tree-optimization/102738] Failure to optimize right shift of 32-bit int after it's already been shifted by 31
  2021-10-14  0:36 [Bug tree-optimization/102738] New: Failure to optimize right shift of 128-bit value after it's already been shifted by 127 gabravier at gmail dot com
@ 2021-10-14  0:49 ` pinskia at gcc dot gnu.org
  2021-10-14  0:53 ` [Bug tree-optimization/102738] Failure to optimize (signed) right shift if the range is already known to be [-1, 0] pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-14  0:49 UTC (permalink / raw)
  To: gcc-bugs

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();
}

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

* [Bug tree-optimization/102738] Failure to optimize (signed) right shift if the range is already known to be [-1, 0]
  2021-10-14  0:36 [Bug tree-optimization/102738] New: Failure to optimize right shift of 128-bit value after it's already been shifted by 127 gabravier at gmail dot com
  2021-10-14  0:49 ` [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
@ 2021-10-14  0:53 ` pinskia at gcc dot gnu.org
  2021-10-14  1:55 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-14  0:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Failure to optimize right   |Failure to optimize
                   |shift of 32-bit int after   |(signed) right shift if the
                   |it's already been shifted   |range is already known to
                   |by 31                       |be [-1, 0]
           Severity|normal                      |enhancement

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

* [Bug tree-optimization/102738] Failure to optimize (signed) right shift if the range is already known to be [-1, 0]
  2021-10-14  0:36 [Bug tree-optimization/102738] New: Failure to optimize right shift of 128-bit value after it's already been shifted by 127 gabravier at gmail dot com
  2021-10-14  0:49 ` [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
  2021-10-14  0:53 ` [Bug tree-optimization/102738] Failure to optimize (signed) right shift if the range is already known to be [-1, 0] pinskia at gcc dot gnu.org
@ 2021-10-14  1:55 ` pinskia at gcc dot gnu.org
  2021-10-14 18:02 ` cvs-commit at gcc dot gnu.org
  2021-10-14 18:03 ` amacleod at redhat dot com
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-14  1:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So this is interesting, I think clang/LLVM does this optimization late in their
pipeline take:
int a1(int f, int g)
{
    if (f == 0 || f == 1)
    return (f-1) >> g;
    return 0;
}
They don't remove the shift at all.  But if you change return 0 to
__builtin_unreachable(), they remove the shift.

Anyways we should be able to do this optimization even in the above case.

And even here where clang/LLVM misses too:
int a1(int f, int g)
{
    if (f == 6 || f == 7)
    return (f-7) >> g;
    __builtin_unreachable();
}

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

* [Bug tree-optimization/102738] Failure to optimize (signed) right shift if the range is already known to be [-1, 0]
  2021-10-14  0:36 [Bug tree-optimization/102738] New: Failure to optimize right shift of 128-bit value after it's already been shifted by 127 gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2021-10-14  1:55 ` pinskia at gcc dot gnu.org
@ 2021-10-14 18:02 ` cvs-commit at gcc dot gnu.org
  2021-10-14 18:03 ` amacleod at redhat dot com
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-10-14 18:02 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

commit r12-4413-gf0b7d4cc49ddb1c2c7474cc3f61e260aa93a96c0
Author: Andrew MacLeod <amacleod@redhat.com>
Date:   Thu Oct 14 10:43:58 2021 -0400

    Simplification for right shift.

    When the first operand of a signed right shift is zero or negative one, the
    RHS doesn't matter and the shift can be converted to a copy.

            PR tree-optimization/102738
            gcc/
            * vr-values.c (simplify_using_ranges::simplify): Handle
RSHIFT_EXPR.

            gcc/testsuite
            * gcc.dg/pr102738.c: New.

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

* [Bug tree-optimization/102738] Failure to optimize (signed) right shift if the range is already known to be [-1, 0]
  2021-10-14  0:36 [Bug tree-optimization/102738] New: Failure to optimize right shift of 128-bit value after it's already been shifted by 127 gabravier at gmail dot com
                   ` (3 preceding siblings ...)
  2021-10-14 18:02 ` cvs-commit at gcc dot gnu.org
@ 2021-10-14 18:03 ` amacleod at redhat dot com
  4 siblings, 0 replies; 6+ messages in thread
From: amacleod at redhat dot com @ 2021-10-14 18:03 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Macleod <amacleod at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |amacleod at redhat dot com
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #4 from Andrew Macleod <amacleod at redhat dot com> ---
I added this to the simplifier, so we now get all these cases right in EVRP.  
I incorporated all 6 into one test.

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

end of thread, other threads:[~2021-10-14 18:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-14  0:36 [Bug tree-optimization/102738] New: Failure to optimize right shift of 128-bit value after it's already been shifted by 127 gabravier at gmail dot com
2021-10-14  0:49 ` [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
2021-10-14  0:53 ` [Bug tree-optimization/102738] Failure to optimize (signed) right shift if the range is already known to be [-1, 0] pinskia at gcc dot gnu.org
2021-10-14  1:55 ` pinskia at gcc dot gnu.org
2021-10-14 18:02 ` cvs-commit at gcc dot gnu.org
2021-10-14 18:03 ` amacleod at redhat dot com

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).