<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 25, 2013 at 2:43 PM, Marshall Clow <span dir="ltr"><<a href="mailto:mclow.lists@gmail.com" target="_blank" class="cremed">mclow.lists@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Sep 25, 2013, at 2:37 PM, Marshall Clow <<a href="mailto:mclow.lists@gmail.com" class="cremed">mclow.lists@gmail.com</a>> wrote:<br>

<br>
> On Sep 25, 2013, at 2:14 PM, Benjamin Kramer <<a href="mailto:benny.kra@gmail.com" class="cremed">benny.kra@gmail.com</a>> wrote:<br>
><br>
>> Since N3664 was implemented in Clang (r186799) it can't optimize unused pairs of ::operator new and ::operator delete anymore. Calls generated by a new/delete expression are still foldable with the updated wording. This affects optimizing away unnecessary code that would be really nice to get right. For example<br>

>><br>
>> #include <vector><br>
>><br>
>> int main() {<br>
>> std::vector<int> v;<br>
>> v.push_back(1);<br>
>><br>
>> return v[0];<br>
>> }<br>
>><br>
>> This should fold down to "return 1;" with no allocations. The example is of course oversimplified but situations like this easily occur in real world code through inlining.<br>
>><br>
>> The proposed patch replaces "::operator new(x)" with "new char[x]" and adds the necessary casts in the allocator class, as suggested by Richard Smith. This is sufficient to constant fold code like my test case again.<br>

><br>
> Is there some technical reason that clang cannot optimize away unused pairs of ::operator new/delete? (as opposed to "just doesn't do it any more" - not a real quote).<br>
<br>
</div>Added in N3664:<br>
        An implementation is allowed to omit a call to a replaceable global allocation function (18.6.1.1, 18.6.1.2).<br>
<br>
To me, "replaceable global allocation function" --> ::operator new.</blockquote></div><br>This only applies to calls made as part of a new expression -- an explicit call cannot be transformed, it is allowed to have observed side effects according to as-if.</div>
</div>