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

            Bug ID: 121898
           Summary: Zero init via member initialization list produces
                    worse code than memset or default member initializer
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mwinkler at blizzard dot com
  Target Milestone: ---

Created attachment 62365
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62365&action=edit
Shows 3 different implementations with the member init list resulting in poorer
codegen

Godbolt for those who don't want to use the attached file:
https://godbolt.org/z/3bP7Gr49q.

GCC fails to reduce a member initializer list initializing all fields to zero
down to an inlined memset operation. If using a default member initializer or
memset inside the constructor the code is optimised as expected.


```
#include <string.h>

#if USE_DEFAULT_MEMBER_INITIALIZER
#define DMINIT = 0
#else
#define DMINIT
#endif

struct Test
{
    int     a DMINIT;
    int     b DMINIT;
    int     c DMINIT;
    int     d DMINIT;
    int     e DMINIT;
    float   f DMINIT;
    float   g DMINIT;

#if USE_MEMSET
    Test()
    {
        ::memset(this, 0, sizeof(*this));
    }
#elif USE_MEMBER_INIT_LIST
Test()
: a(0)
, b(0)
, c(0)
, d(0)
, e(0)
, f(0)
, g(0)
{
}
#endif
};

Test test()
{
    Test t;
    return t;
}
```

Both the `USE_MEMSET` and `USE_DEFAULT_MEMBER_INITIALIZER` result in two
128-bit stores.

`USE_MEMBER_INIT_LIST` instead results in different code gen leading to 3
stores.

Reply via email to