[Bug c++/79578] New: Unnecessary instructions in generated code

maxim.yegorushkin at gmail dot com gcc-bugzilla@gcc.gnu.org
Fri Feb 17 14:02:00 GMT 2017


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

            Bug ID: 79578
           Summary: Unnecessary instructions in generated code
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: maxim.yegorushkin at gmail dot com
  Target Milestone: ---

When the following code is compiled with gcc-7 and earlier versions with flags
--std=c++14 -O3 -pthread:

    #include <new>
    #include <cstdint>

    struct A
    {
        std::uint16_t a, b;
    };

    A* f(char* b) __attribute__((noinline));

    A* f(char* b) {
        auto a = new(b) A{};
        a->a = 1;
        a->b = 2;
        return a;
    }

    int main() {
        char b[sizeof(A)] alignas(A);
        f(b);
    }

The generated code for f contains unnecessary instructions:

    f(char*):
            testq   %rdi, %rdi <--- unnecessary code begin
            movq    %rdi, %rax
            je      .L2
            movl    $0, (%rdi) <--- unnecessary code end
    .L2:
            movl    $131073, (%rax)
            ret

I expect the generated code to be just:

    f(char*):
            movl    $131073, (%rdi)
            ret

Because:

1. operator new(size_t, void*) is an inline function, its definition is
available in this translation unit.
2. The code assigns through that pointer, so the pointer must be valid.
Therefore there is no need to test the result of new for 0.
3. Static single assignment form must notice that the assignment of 0 is
redundant.


More information about the Gcc-bugs mailing list