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

            Bug ID: 105864
           Summary: storing nullptr_t to memory should not generate any
                    instructions
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vanyacpp at gmail dot com
  Target Milestone: ---

Currently storing a nullptr_t to memory causes 0 to be written to that memory.
As there is no way to read this value back without invoking undefined behavior
I believe GCC can omit storing it.

This will make nullptr_t behave more similar to an empty struct that has only
padding bytes in it.

using nullptr_t = decltype(nullptr);

void test(nullptr_t* p)
{
    *p = nullptr;
}

struct empty
{};

void test(empty* p)
{
    *p = empty();
}

test(decltype(nullptr)*):
        mov     QWORD PTR [rdi], 0
        ret
test(empty*):
        ret

Reply via email to