<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">OK, thanks.  "My program" is actually test/CodeGenCXX/global-init.cpp which is expecting not to see the memset.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Given the '=default-in-C++03' bug, I'll make it work on both 03 and 11, and leave a FIXME about it.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Thanks,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">--paulr<o:p></o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></a></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> metafoo@gmail.com [mailto:metafoo@gmail.com]
<b>On Behalf Of </b>Richard Smith<br>
<b>Sent:</b> Tuesday, December 20, 2016 5:07 PM<br>
<b>To:</b> Robinson, Paul<br>
<b>Cc:</b> cfe-dev@lists.llvm.org<br>
<b>Subject:</b> Re: [cfe-dev] C++11 doing double zero initialization?<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div>
<p class="MsoNormal">On 20 December 2016 at 16:39, Robinson, Paul via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
<p class="MsoNormal">Consider the following source:<br>
<br>
  struct C { C() = default; C(const C&); int n; };<br>
  const C c1 = C();<br>
<br>
then run it through `clang -emit-llvm -std=c++11`,<br>
and I see the following relevant bits:<br>
<br>
  %struct.C = type { i32 }<br>
  @_ZL2c1 = internal global %struct.C zeroinitializer, align 4<br>
  define internal void @__cxx_global_var_init() {{.*}} {<br>
  entry:<br>
    call void @llvm.memset.p0i8.i64(i8* bitcast (%struct.C* @_ZL2c1 to i8*),<br>
      i8 0, i64 4, i32 4, i1 false)<br>
    ret void<br>
  }<br>
<br>
<br>
The question is: Why the memset?  Doesn't 'zeroinitializer'<br>
do the exact same thing?<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Initialization of C++ globals is performed in two stages:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> * in the first stage ("static initialization"), <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">    -- if the object has a constant initializer (which C() is not, see <a href="http://wiki.edg.com/pub/Wg21issaquah2016/CoreWorkingGroup/cwg_active.html#1452">http://wiki.edg.com/pub/Wg21issaquah2016/CoreWorkingGroup/cwg_active.html#1452</a>),
 then object is initialized to that constant value ("constant intiialization")<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">    -- otherwise is initialized to zero ("zero-initialization").<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> * in the second stage ("dynamic initialization", which is skipped if the first stage used constant initialization or if no initializer is provided for a scalar type), the initializer is run.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">What you're seeing here is a direct application of these rules to your program.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">C++ allows dynamic initialization to be converted to constant initialization if the initialization doesn't have side-effects (more or less), and Clang uses that permission to remove some dynamic initializers, but we don't happen to do so
 in this case (in either C++03 or C++11). That's a QoI bug (and core issue 1452 may make it a correctness bug). Either way, we should fix it.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">I don't see the memset with C++03.<o:p></o:p></p>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The input is not valid C++03 code, due to the "= default;", and our allowance of that construct in C++03 as an extension does something a bit strange: the C++11 initialization rules say that -- because C::C() is not user-provided -- we
 first zero-initialize the entire entire object and then call the no-op constructor, which is why you get a memset. However, the C++03 initialization rules are slightly different, and don't require C() to zero-initialize in this case prior to running the constructor
 (you only get the implicit zero-initialization for C() in C++03 if the class doesn't declare any constructors at all).<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I think our =default-in-C++03 extension is actually broken here, and we should emit the memset in both languages. Given:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">  struct A { A() = default; int n; };<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">  int f() { A a = A(); return a.n; }<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">... this should be guaranteed to return 0 even in C++03 mode. (Right now it compiles to 'ret undef' in C++03.)<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>