public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/98337] New: Failure to optimize out on-stack array construction when unneeded
@ 2020-12-16 22:25 gabravier at gmail dot com
  2021-01-04 15:44 ` [Bug tree-optimization/98337] " rguenth at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: gabravier at gmail dot com @ 2020-12-16 22:25 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98337
           Summary: Failure to optimize out on-stack array construction
                    when unneeded
           Product: gcc
           Version: 11.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 f(int x) {
    int a[] = {0, 1};
    return a[x];
}

On AMD64, with -O3, this is generated :

f(int):
  mov rax, QWORD PTR .LC0[rip]
  movsx rdi, edi
  mov QWORD PTR [rsp-8], rax
  mov eax, DWORD PTR [rsp-8+rdi*4]
  ret
.LC0:
  .long 0
  .long 1


LLVM generates this :

f(int): # @f(int)
  movsxd rax, edi
  mov eax, dword ptr [4*rax + .L__const.f(int).a]
  ret
.L__const.f(int).a:
  .long 0 # 0x0
  .long 1 # 0x1

It seems to me like not copying the entire array on the stack makes for faster
code.

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

* [Bug tree-optimization/98337] Failure to optimize out on-stack array construction when unneeded
  2020-12-16 22:25 [Bug tree-optimization/98337] New: Failure to optimize out on-stack array construction when unneeded gabravier at gmail dot com
@ 2021-01-04 15:44 ` rguenth at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-01-04 15:44 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-01-04
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
It could be optimized to return x even ...

Note we're forcing the array to the stack because of the variable array access.
We could optimize this on the GIMPLE level from

  MEM <unsigned long> [(int *)&a] = 3547642986509;
  _5 = a[x_4(D)];

to

  3547642986509ul >> (x_4(D)*32)

for example when using more "interesting" constants like

int f(int x) {
    int a[] = {13, 826};
    return a[x];
}

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

end of thread, other threads:[~2021-01-04 15:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-16 22:25 [Bug tree-optimization/98337] New: Failure to optimize out on-stack array construction when unneeded gabravier at gmail dot com
2021-01-04 15:44 ` [Bug tree-optimization/98337] " 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).