public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/102392] New: Failure to optimize out sign extension when input is non-negative
@ 2021-09-17 23:44 gabravier at gmail dot com
  2021-09-17 23:47 ` [Bug middle-end/102392] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: gabravier at gmail dot com @ 2021-09-17 23:44 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102392
           Summary: Failure to optimize out sign extension when input is
                    non-negative
           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: ---

#include <stdint.h>

void f(int64_t x);

void g(int32_t x)
{
    if (x < 0)
        __builtin_unreachable();
    f(x);
}

This can be optimized to avoid the sign extension since x can't be under 0.
This optimization is done by LLVM, but not by GCC.

Sample resulting assembly from GCC:

g:
  movsx rdi, edi
  jmp f

from LLVM:

g:
  mov edi, edi
  jmp f

(PS: I originally found this while looking at the code that led me to
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102391: an error check earlier in
the code (not in the example cited there) wound up making this assumption
possible, and slightly changed the assembly code emitted by LLVM there to be
even more efficient)

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

* [Bug middle-end/102392] Failure to optimize out sign extension when input is non-negative
  2021-09-17 23:44 [Bug tree-optimization/102392] New: Failure to optimize out sign extension when input is non-negative gabravier at gmail dot com
@ 2021-09-17 23:47 ` pinskia at gcc dot gnu.org
  2021-09-17 23:49 ` [Bug target/102392] Failure to optimize a sign extension to a zero extension pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-17 23:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ABI
             Target|                            |X86_64-linux-gnu
          Component|tree-optimization           |middle-end

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Actually this can be done without the __builtin_unreachable due to the abi.

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

* [Bug target/102392] Failure to optimize a sign extension to a zero extension
  2021-09-17 23:44 [Bug tree-optimization/102392] New: Failure to optimize out sign extension when input is non-negative gabravier at gmail dot com
  2021-09-17 23:47 ` [Bug middle-end/102392] " pinskia at gcc dot gnu.org
@ 2021-09-17 23:49 ` pinskia at gcc dot gnu.org
  2021-09-18  0:20 ` [Bug tree-optimization/102392] " pinskia at gcc dot gnu.org
  2021-09-20  8:45 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-17 23:49 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Failure to optimize out     |Failure to optimize a sign
                   |sign extension when input   |extension to a zero
                   |is non-negative             |extension
          Component|middle-end                  |target

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> Actually this can be done without the __builtin_unreachable due to the abi.

I mean this can never be done without it. Also a zero extend vs sign extend
might be the same cost really. So it does not matter in the end.

Note the move you reference is  zero extend.

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

* [Bug tree-optimization/102392] Failure to optimize a sign extension to a zero extension
  2021-09-17 23:44 [Bug tree-optimization/102392] New: Failure to optimize out sign extension when input is non-negative gabravier at gmail dot com
  2021-09-17 23:47 ` [Bug middle-end/102392] " pinskia at gcc dot gnu.org
  2021-09-17 23:49 ` [Bug target/102392] Failure to optimize a sign extension to a zero extension pinskia at gcc dot gnu.org
@ 2021-09-18  0:20 ` pinskia at gcc dot gnu.org
  2021-09-20  8:45 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-18  0:20 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-09-18
             Status|UNCONFIRMED                 |NEW
          Component|target                      |tree-optimization
           Keywords|ABI                         |

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
A better testcase is:
void g(int32_t *x, int64_t *y)
{
    if (*x < 0)
        __builtin_unreachable();
    *y = (*x);
}

To see what LLVM is really doing.
GCC:
        movslq  (%rdi), %rax
        movq    %rax, (%rsi)
        ret

VS LLVM:
        movl    (%rdi), %eax
        movq    %rax, (%rsi)
        retq

There might be a dup of this bug somewhere too.

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

* [Bug tree-optimization/102392] Failure to optimize a sign extension to a zero extension
  2021-09-17 23:44 [Bug tree-optimization/102392] New: Failure to optimize out sign extension when input is non-negative gabravier at gmail dot com
                   ` (2 preceding siblings ...)
  2021-09-18  0:20 ` [Bug tree-optimization/102392] " pinskia at gcc dot gnu.org
@ 2021-09-20  8:45 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-09-20  8:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note w/o explicit ZEXT_EXPR / SEXT_EXPR the GIMPLE for non-"natural" extensions
is more costly (more stmts) than the "natural" extension based on the sign
of the object because it requires an intermediate sign changing conversion.

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

end of thread, other threads:[~2021-09-20  8:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-17 23:44 [Bug tree-optimization/102392] New: Failure to optimize out sign extension when input is non-negative gabravier at gmail dot com
2021-09-17 23:47 ` [Bug middle-end/102392] " pinskia at gcc dot gnu.org
2021-09-17 23:49 ` [Bug target/102392] Failure to optimize a sign extension to a zero extension pinskia at gcc dot gnu.org
2021-09-18  0:20 ` [Bug tree-optimization/102392] " pinskia at gcc dot gnu.org
2021-09-20  8:45 ` rguenth 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).