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

            Bug ID: 104316
           Summary: Missing optimization for uninitialized local struct
                    variable
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: roland.illig at gmx dot de
  Target Milestone: ---

~~~c
#include <stdlib.h>

typedef struct Buffer {
    char *buf;
    size_t len;
    size_t cap;
} Buffer;

Buffer Buffer_Init_Return(void);
void Buffer_Init_Ref(Buffer *buf);
void Buffer_Add(Buffer *buf, const char *);

void
Use_Buffers(void)
{
    Buffer buf1 = Buffer_Init_Return();

    Buffer buf2;
    buf2 = Buffer_Init_Return();

    Buffer_Add(&buf1, "1");
    Buffer_Add(&buf2, "2");
}
~~~

https://godbolt.org/z/qTz36qP9r

The code snippets for initializing buf1 and buf2 are almost the same. From an
as-if perspective, they are identical.

GCC 12 generates several copy instructions when initializing buf2, while Clang
and ICC generate the straight-forward code.

Would it be difficult to tell GCC to optimize the above initialization for
buf2, for the case that buf2 is not accessed between the declaration and the
assignment?

Reply via email to