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

            Bug ID: 65410
           Summary: "Short local string array" optimization doesn't happen
                    if string has NULs
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vda.linux at googlemail dot com

void f(char *);
void g() {
        char buf[12] = "1234567890";
        f(buf);
}

In the above example, "gcc -O2" creates buf[12] with immediate stores:

        subq    $24, %rsp
        movabsq $4050765991979987505, %rax
        movq    %rsp, %rdi
        movq    %rax, (%rsp)
        movl    $12345, 8(%rsp)
        call    f
        addq    $24, %rsp
        ret

But if buf[] definition has \0 anywhere (for example, at the end where it does
not even change the semantics of the code), optimization is not happening, gcc
allocates a constant string and copies it into buf[]:

void f(char *);
void g() {
        char buf[12] = "1234567890\0";
        f(buf);
}

        .section        .rodata
.LC0:
        .string "1234567890"
        .string ""

        .text
g:
        subq    $24, %rsp
        movq    .LC0(%rip), %rax
        movq    %rsp, %rdi
        movq    %rax, (%rsp)
        movl    .LC0+8(%rip), %eax
        movl    %eax, 8(%rsp)
        call    f
        addq    $24, %rsp
        ret

Reply via email to