<div dir="ltr">More generally, a (non-placement) new-expression is not required to call any 'operator new' in particular to get its storage. It's permitted to do so, but it's also permitted to get storage from another place (such as the heap) or combine multiple allocations into a single operator new call, so long as all the relevant side-conditions are satisfied. (Calls to 'operator new' from new-expressions are considered non-observable behavior in some sense.)</div><br><div class="gmail_quote"><div dir="ltr">On Mon, 3 Dec 2018 at 18:25, Finkel, Hal J. via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
<p>(remembering to cc the list this time)<br>
</p>
<div class="m_-1789559969091960880moz-cite-prefix">On 12/3/18 8:02 PM, Hal Finkel wrote:<br>
</div>
<blockquote type="cite">
<div class="m_-1789559969091960880moz-cite-prefix">On 12/3/18 7:44 PM, via cfe-dev wrote:<br>
</div>
<blockquote type="cite">
<div class="m_-1789559969091960880WordSection1">
<p class="MsoNormal">Hi All,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">A commit earlier this year, r325630, changed the behavior of the following program under –O1:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">-----------------------------------------------------------------------------------<u></u><u></u></p>
<p class="MsoNormal">#include <new><u></u><u></u></p>
<p class="MsoNormal">#include <stdlib.h><u></u><u></u></p>
<p class="MsoNormal">extern "C" int printf(const char *, ...);<u></u><u></u></p>
<p class="MsoNormal">int UsedMyCode = 0;<u></u><u></u></p>
<p class="MsoNormal">void * operator new(std::size_t count)<u></u><u></u></p>
<p class="MsoNormal">{<u></u><u></u></p>
<p class="MsoNormal"> UsedMyCode = 1;<u></u><u></u></p>
<p class="MsoNormal"> return malloc(count);<u></u><u></u></p>
<p class="MsoNormal">}<u></u><u></u></p>
<p class="MsoNormal">int main(int argc, char * argv[])<u></u><u></u></p>
<p class="MsoNormal">{<u></u><u></u></p>
<p class="MsoNormal"> int* p = new int();<u></u><u></u></p>
<p class="MsoNormal"> *p = 10;<u></u><u></u></p>
<p class="MsoNormal"> printf("*p=%d. UsedMyCode=%d\n", *p, UsedMyCode);<u></u><u></u></p>
<p class="MsoNormal"> delete p;<u></u><u></u></p>
<p class="MsoNormal"> return 0;<u></u><u></u></p>
<div style="border:none;border-bottom:solid windowtext 1.0pt;padding:0in 0in 1.0pt 0in">
<p class="MsoNormal" style="border:none;padding:0in">}<u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The printed value of UsedMyCode became 0 after that checkin.
<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The compiler is eliminating the new/delete pair, printing just 10 for *p, because the usage
<u></u><u></u></p>
<p class="MsoNormal">inside the function can be fully analyzed.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">But I am wondering whether this is a valid optimization, because the implementation is not<u></u><u></u></p>
<p class="MsoNormal">calling the user supplied replacement as per 15.5.4.6. The complaint is that the side effects of
<u></u><u></u></p>
<p class="MsoNormal">the user supplied replacement are not occurring.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The commit r325630 did not actually implement this optimization, but most likely it enabled<u></u><u></u></p>
<p class="MsoNormal">an already implemented optimization that was being foiled by something.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Any opinions?</p>
</div>
</blockquote>
<p>The implementation is allowed to elide those side effects, and the standard was clarified in recent years to reflect this explicitly. See 8.3.4p10, "An implementation is allowed to omit a call to a replaceable global allocation function (21.6.2.1, 21.6.2.2).
When it does so, the storage is instead provided by the implementation or provided by extending the allocation of another new-expression. The implementation may extend the allocation..."<br>
</p>
<p> -Hal<br>
</p>
<blockquote type="cite">
<div class="m_-1789559969091960880WordSection1">
<p class="MsoNormal"><u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Thanks<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Sunil Srivastava<u></u><u></u></p>
<p class="MsoNormal">Sony Interactive Entertainment.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<br>
<fieldset class="m_-1789559969091960880mimeAttachmentHeader"></fieldset>
<pre class="m_-1789559969091960880moz-quote-pre">_______________________________________________
cfe-dev mailing list
<a class="m_-1789559969091960880moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>
<a class="m_-1789559969091960880moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a>
</pre>
</blockquote>
<pre class="m_-1789559969091960880moz-signature" cols="72">--
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
</blockquote>
<pre class="m_-1789559969091960880moz-signature" cols="72">--
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
</div>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>