<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64494>64494</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Clang always optimizes away standard placement new operator calls (even with -fno-builtin)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          strimo378
      </td>
    </tr>
</table>

<pre>
    The current behavior of clang for placement new operator is that all calls to a global placement new function with void* return value and void* 2nd argument is optimized away.

See the following example https://godbolt.org/z/8bKcMMsxv

```
struct x1{
    x1();
};

struct x2{
    x2();
};

void* operator new(__SIZE_TYPE__, x1*);
void* operator new(__SIZE_TYPE__, void*);

void func1(x1* data) {
         new (data) x1();
}

void func2(x2* data) {
         // Call to operator new is optimized away
         new (data) x2();
}

```

>From my point of view that is an optimization that assumes a standard implementation of the global placement new function as it is provided e.g. by libcxx. However, my understanding of the option `-fno-builtin` is that these assumptions are deactivated. I think the optimization should be deactivated when -fno-builtin` is active. 

The optimization is triggered in `CodeGenFunction::EmitCXXNewExpr` by checking `allocator->isReservedGlobalPlacementOperator()` (FunctionDecl::isReservedGlobalPlacementOperator).

I can write a patch if my assumption is correct...
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVU2TozYQ_TXypWsoLDCGA4fZmfFmK7XJVnYOm1ymhNSAskKiJAH2_vqUwB_jiSdZlwsb6K_3-rWaOScbjViSzQeyeVyxwbfGls5b2Zlkm68qIw7lc4vAB2tRe6iwZaM0FkwNXDHdQG0s9Ipx7MJ7jROYHi3zxoJ04FvmgSkFnCnlwBtg0ChTMfXGqR4099JomKRvYTRSEHoPFv1gNYxMDQhMi_MLqgUw2wyzv3Rgei87-QMFsIkdIhI_kvh-uX5FBN8i1EYpM0ndAO5Z1yuE1vvekeSe0B2hu8aIyigfGdsQuvtB6C6vfuWfP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