[Bug c/59863] New: const array in function is placed on stack

sam at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Jan 17 18:51:00 GMT 2014


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?



More information about the Gcc-bugs mailing list