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

            Bug ID: 127674
           Summary: new[]/delete[] pair kept when we use
                    __builtin_assume_aligned
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: saicharan at astrome dot co
  Target Milestone: ---

The unused new[]/delete[] pair is not eliminated at -O3 when the pointer
is passed through __builtin_assume_aligned.

void f() {
    unsigned char *p = new unsigned char[23];
    p = static_cast<unsigned char *>(__builtin_assume_aligned(p, 8));
    delete[] p;
}

Assembly generated for x86-64:

"f()":
        sub     rsp, 8
        mov     edi, 23
        call    "operator new[](unsigned long)"
        add     rsp, 8
        mov     rdi, rax
        jmp     "operator delete[](void*)"

Removing the __builtin_assume_aligned line eliminates the redundant pair.
Compiler Explorer link: https://godbolt.org/z/K78ovdYrr

Reply via email to