public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/59863] New: const array in function is placed on stack
@ 2014-01-17 18:51 sam at gcc dot gnu.org
  2014-01-17 23:26 ` [Bug c/59863] " joseph at codesourcery dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: sam at gcc dot gnu.org @ 2014-01-17 18:51 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863

            Bug ID: 59863
           Summary: const array in function is placed on stack
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sam at gcc dot gnu.org

Created attachment 31875
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=31875&action=edit
Source file to reproduce the issue

This source code

int f(int i) {
  const int a[] = {1, 2, 3, 4};
  return a[i];
}

int g(int i) {
  static const int a[] = {1, 2, 3, 4};
  return a[i];
}

generates different code with GCC 4.8.2 on x86_64 for f() and g(). In f(), the
const array is really created on the stack, and initialized one element at a
time, instead of being placed in .rodata as it is for g().

With GCC on ARM (4.7), both arrays from f() and g() are placed in .rodata, but
the code to access them is different:

    .text
    .align    2
    .global    f
    .type    f, %function
f:
    @ args = 0, pretend = 0, frame = 16
    @ frame_needed = 0, uses_anonymous_args = 0
    @ link register save eliminated.
    str    r4, [sp, #-4]!
    ldr    r3, .L2
    sub    sp, sp, #20
    mov    ip, r0
    ldmia    r3, {r0, r1, r2, r3}
    add    r4, sp, #16
    stmdb    r4, {r0, r1, r2, r3}
    add    ip, r4, ip, asl #2
    ldr    r0, [ip, #-16]
    add    sp, sp, #20
    ldmfd    sp!, {r4}
    bx    lr
.L3:
    .align    2
.L2:
    .word    .LANCHOR0
    .size    f, .-f
    .align    2
    .global    g
    .type    g, %function
g:
    @ args = 0, pretend = 0, frame = 0
    @ frame_needed = 0, uses_anonymous_args = 0
    @ link register save eliminated.
    ldr    r3, .L5
    add    r0, r3, r0, asl #2
    ldr    r0, [r0, #16]
    bx    lr
.L6:
    .align    2
.L5:
    .word    .LANCHOR0
    .size    g, .-g
    .section    .rodata
    .align    2
    .set    .LANCHOR0,. + 0
.LC0:
    .word    1
    .word    2
    .word    3
    .word    4
    .type    a.4057, %object
    .size    a.4057, 16
a.4057:
    .word    1
    .word    2
    .word    3
    .word    4

Note that on x86_64, clang generates the same code for f() and g() and even
unifies both const arrays in .rodata.

Is there anything in the C standard preventing a const array declared in a
function from being put in .rodata or are those missed optimizations?


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

* [Bug c/59863] const array in function is placed on stack
  2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
@ 2014-01-17 23:26 ` joseph at codesourcery dot com
  2014-01-17 23:31 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: joseph at codesourcery dot com @ 2014-01-17 23:26 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863

--- Comment #1 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
On Fri, 17 Jan 2014, sam at gcc dot gnu.org wrote:

> Is there anything in the C standard preventing a const array declared in a
> function from being put in .rodata or are those missed optimizations?

If the object address can escape and the function can be called 
recursively, this would violate the requirement for distinct objects to 
have distinct addresses.  (See discussion on comp.std.c, "uniqueness of 
automatic objects", Nov 2008; I'm not sure if there was a corresponding 
GCC bug report / fix.)


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

* [Bug c/59863] const array in function is placed on stack
  2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
  2014-01-17 23:26 ` [Bug c/59863] " joseph at codesourcery dot com
@ 2014-01-17 23:31 ` pinskia at gcc dot gnu.org
  2014-01-20  9:15 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-01-17 23:31 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is caused by checking TREE_ADDRESSABLE on the a decl which is correct
thing to do but the issue is there is no addressing taking in the code (only an
implicit one with a[i]:
      /* An array that is indexed by a non-constant
     cannot be stored in a register; we must be able to do
     address arithmetic on its address.
     Likewise an array of elements of variable size.  */


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

* [Bug c/59863] const array in function is placed on stack
  2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
  2014-01-17 23:26 ` [Bug c/59863] " joseph at codesourcery dot com
  2014-01-17 23:31 ` pinskia at gcc dot gnu.org
@ 2014-01-20  9:15 ` rguenth at gcc dot gnu.org
  2014-01-20  9:42 ` sam at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-01-20  9:15 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
This is also simply a gimplification issue.  For larger arrays we initialize
the stack with an aggregate copy from the constant pool.


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

* [Bug c/59863] const array in function is placed on stack
  2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2014-01-20  9:15 ` rguenth at gcc dot gnu.org
@ 2014-01-20  9:42 ` sam at gcc dot gnu.org
  2021-09-25  1:34 ` [Bug middle-end/59863] " pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: sam at gcc dot gnu.org @ 2014-01-20  9:42 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59863

--- Comment #4 from Samuel Tardieu <sam at gcc dot gnu.org> ---
Note that clang has this bug "in reverse": it unifies the arrays even when
recursive calls are possible and address escapes the defining function
(http://llvm.org/bugs/show_bug.cgi?id=18557).


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

* [Bug middle-end/59863] const array in function is placed on stack
  2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2014-01-20  9:42 ` sam at gcc dot gnu.org
@ 2021-09-25  1:34 ` pinskia at gcc dot gnu.org
  2021-09-25  1:39 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-25  1:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |zhongyunde at huawei dot com

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 93102 has been marked as a duplicate of this bug. ***

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

* [Bug middle-end/59863] const array in function is placed on stack
  2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2021-09-25  1:34 ` [Bug middle-end/59863] " pinskia at gcc dot gnu.org
@ 2021-09-25  1:39 ` pinskia at gcc dot gnu.org
  2024-03-15  0:07 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-25  1:39 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-09-25
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

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

* [Bug middle-end/59863] const array in function is placed on stack
  2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2021-09-25  1:39 ` pinskia at gcc dot gnu.org
@ 2024-03-15  0:07 ` pinskia at gcc dot gnu.org
  2024-03-19 17:26 ` hiraditya at msn dot com
  2024-03-19 18:14 ` xry111 at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-15  0:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |barry.revzin at gmail dot com

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 99091 has been marked as a duplicate of this bug. ***

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

* [Bug middle-end/59863] const array in function is placed on stack
  2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
                   ` (6 preceding siblings ...)
  2024-03-15  0:07 ` pinskia at gcc dot gnu.org
@ 2024-03-19 17:26 ` hiraditya at msn dot com
  2024-03-19 18:14 ` xry111 at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: hiraditya at msn dot com @ 2024-03-19 17:26 UTC (permalink / raw)
  To: gcc-bugs

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

AK <hiraditya at msn dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hiraditya at msn dot com

--- Comment #9 from AK <hiraditya at msn dot com> ---
*** Bug 114342 has been marked as a duplicate of this bug. ***

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

* [Bug middle-end/59863] const array in function is placed on stack
  2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
                   ` (7 preceding siblings ...)
  2024-03-19 17:26 ` hiraditya at msn dot com
@ 2024-03-19 18:14 ` xry111 at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-03-19 18:14 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xry111 at gcc dot gnu.org
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=114206

--- Comment #10 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Note that if we take a pointer to such an array this optimization will be
invalid.  And unfortunately the mis-optimization is already happening (even for
non-const arrays) with some strange command line switch combinations.  See
PR114206.

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

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

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-17 18:51 [Bug c/59863] New: const array in function is placed on stack sam at gcc dot gnu.org
2014-01-17 23:26 ` [Bug c/59863] " joseph at codesourcery dot com
2014-01-17 23:31 ` pinskia at gcc dot gnu.org
2014-01-20  9:15 ` rguenth at gcc dot gnu.org
2014-01-20  9:42 ` sam at gcc dot gnu.org
2021-09-25  1:34 ` [Bug middle-end/59863] " pinskia at gcc dot gnu.org
2021-09-25  1:39 ` pinskia at gcc dot gnu.org
2024-03-15  0:07 ` pinskia at gcc dot gnu.org
2024-03-19 17:26 ` hiraditya at msn dot com
2024-03-19 18:14 ` xry111 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).