public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/98425] New: Superfluous sign-extend for constrained integer
@ 2020-12-23 10:19 koenigni at gcc dot gnu.org
  2020-12-23 10:28 ` [Bug rtl-optimization/98425] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: koenigni at gcc dot gnu.org @ 2020-12-23 10:19 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98425
           Summary: Superfluous sign-extend for constrained integer
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: koenigni at gcc dot gnu.org
  Target Milestone: ---

Hello everyone,

A small missed optimization I noticed while toying around with the difference
between signed and unsigned integers. The following code

int
baz(int *p, int i) {
  int j;
  if (i >= 0) {
    j = i + 4;
    return p[j];
  } else
    __builtin_unreachable();
}

is compiled with `gcc -O3 -S` to

baz:
  addl  $4, %esi
  movslq  %esi, %rsi
  movl  (%rdi,%rsi,4), %eax
  ret

The movslq instruction is unnecessary since i is constrained to never be
negative and therefore no sign extension is needed. This probably also prevents
the addl instructions to be removed and the offset being put into the movl. 

For comparison, clang (with the same options) compiles the code to 

baz:
  movl %esi, %eax
  movl 16(%rdi, %rax, 4), %eax
  ret

Optimal would probably be

baz:
  movl 16(%rdi, %rsi, 4), %eax
  ret

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

* [Bug rtl-optimization/98425] Superfluous sign-extend for constrained integer
  2020-12-23 10:19 [Bug rtl-optimization/98425] New: Superfluous sign-extend for constrained integer koenigni at gcc dot gnu.org
@ 2020-12-23 10:28 ` pinskia at gcc dot gnu.org
  2020-12-23 10:30 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-12-23 10:28 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ABI

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Actually this is an abi issue. The upper 32bits of the register is undefined by
the x86_64 ABI iirc. If this is true then clang has a bug.

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

* [Bug rtl-optimization/98425] Superfluous sign-extend for constrained integer
  2020-12-23 10:19 [Bug rtl-optimization/98425] New: Superfluous sign-extend for constrained integer koenigni at gcc dot gnu.org
  2020-12-23 10:28 ` [Bug rtl-optimization/98425] " pinskia at gcc dot gnu.org
@ 2020-12-23 10:30 ` pinskia at gcc dot gnu.org
  2020-12-23 10:30 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2020-12-23 10:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note your optimum code is wrong.
movl %esi, %eax
Is a zero extend.

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

* [Bug rtl-optimization/98425] Superfluous sign-extend for constrained integer
  2020-12-23 10:19 [Bug rtl-optimization/98425] New: Superfluous sign-extend for constrained integer koenigni at gcc dot gnu.org
  2020-12-23 10:28 ` [Bug rtl-optimization/98425] " pinskia at gcc dot gnu.org
  2020-12-23 10:30 ` pinskia at gcc dot gnu.org
@ 2020-12-23 10:30 ` jakub at gcc dot gnu.org
  2020-12-23 10:40 ` jakub at gcc dot gnu.org
  2020-12-23 10:48 ` koenigni at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-12-23 10:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Yes, it has been reported to LLVM years ago that they violate the psABI, but
they refused to fix that.

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

* [Bug rtl-optimization/98425] Superfluous sign-extend for constrained integer
  2020-12-23 10:19 [Bug rtl-optimization/98425] New: Superfluous sign-extend for constrained integer koenigni at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2020-12-23 10:30 ` jakub at gcc dot gnu.org
@ 2020-12-23 10:40 ` jakub at gcc dot gnu.org
  2020-12-23 10:48 ` koenigni at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-12-23 10:40 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Sorry, in this case it isn't wrong (that is the passing of char/short case),
the __builtin_unreachable () says that negative values are undefined, therefore
the compiler can use both sign and zero extension interchangeably.
And the undefined signed overflow means when we have:
int i;
...
((unsigned long long) (i + 4)) * 4
we can compute it as
((unsigned long long) (i + 4)) * 4
or
((unsigned long long) i) * 4 + 16
because if i is in [INT_MAX - 3, INT_MAX] it is UB.

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

* [Bug rtl-optimization/98425] Superfluous sign-extend for constrained integer
  2020-12-23 10:19 [Bug rtl-optimization/98425] New: Superfluous sign-extend for constrained integer koenigni at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2020-12-23 10:40 ` jakub at gcc dot gnu.org
@ 2020-12-23 10:48 ` koenigni at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: koenigni at gcc dot gnu.org @ 2020-12-23 10:48 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Nicolas Koenig <koenigni at gcc dot gnu.org> ---
The advantage of using mov over movs for known-positive integers would be that
32bit moves are also move-eliminated, while movs always has to be executed.

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

end of thread, other threads:[~2020-12-23 10:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-23 10:19 [Bug rtl-optimization/98425] New: Superfluous sign-extend for constrained integer koenigni at gcc dot gnu.org
2020-12-23 10:28 ` [Bug rtl-optimization/98425] " pinskia at gcc dot gnu.org
2020-12-23 10:30 ` pinskia at gcc dot gnu.org
2020-12-23 10:30 ` jakub at gcc dot gnu.org
2020-12-23 10:40 ` jakub at gcc dot gnu.org
2020-12-23 10:48 ` koenigni 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).