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

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