<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 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:Helvetica;
panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
{font-family:Courier;
panose-1:2 7 4 9 2 2 5 2 4 4;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"Book Antiqua";
panose-1:2 4 6 2 5 3 5 3 3 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman",serif;
color:black;}
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;}
p
{mso-style-priority:99;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:12.0pt;
font-family:"Times New Roman",serif;
color:black;}
pre
{mso-style-priority:99;
mso-style-link:"HTML Preformatted Char";
margin:0cm;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";
color:black;}
span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:Consolas;
color:black;}
span.EmailStyle20
{mso-style-type:personal-reply;
font-family:"Book Antiqua",serif;
color:#943634;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
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 bgcolor=white lang=EN-IE link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>I think that the key concept goes all the way back to the original C++ Standard (C++98), where section 5.2.2 “Function Call” states:<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt;text-autospace:none'><span style='font-size:10.0pt;color:windowtext'>The first expression in the postfix expression is then called the <i>object expression</i>, and the call<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt;text-autospace:none'><span style='font-size:10.0pt;color:windowtext'>is as a member of the object pointed to or referred to. In the case of an implicit class member access, the<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt;text-autospace:none'><span style='font-size:10.0pt;color:windowtext'>implied object is the one pointed to by </span><span style='font-size:10.0pt;font-family:Courier;color:windowtext'>this</span><span style='font-size:10.0pt;color:windowtext'>. [<i>Note: </i>a member function call of the form </span><span style='font-size:10.0pt;font-family:Courier;color:windowtext'>f() </span><span style='font-size:10.0pt;color:windowtext'>is interpreted<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;color:windowtext'>as </span><span style='font-size:10.0pt;font-family:Courier;color:windowtext'>(*this).f() </span><span style='font-size:10.0pt;color:windowtext'>(see 9.3.1). ]</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>A NULL pointer does not point to an object, so two things make a NULL ‘</span><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>this</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’ invalid - first the </span><i><span style='font-family:"Book Antiqua",serif;color:black;mso-fareast-language:EN-US'>object expression</span></i><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'> does not refer to an object and is thus undefined; and second, the note which clarifies that it is equivalent to ‘</span><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>(*this).</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’ means that if ‘</span><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>this</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’ is NULL, then it is a NULL pointer dereference which is already undefined behaviour elsewhere in the Standard. I can’t remember if “notes” are normative.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>In “very old C++”, i.e. prior to the introduction of static member functions (circa 1987), the following idiom was not unusual to get the semantic intent of a static member provide that ‘</span><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>this</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’ was neither implicitly nor explicitly used:<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>class T {<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>public:<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'> void wishIwasStatic();<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>};<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>...<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>((T*)0)->wishIwasStatic();<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>Such functions had access to all object of type ‘</span><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>T</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’, but without the need for ‘</span><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>friend</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’ declarations. But this was only a stop-gap until the introduction of static member functions was devised.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>I very much doubt that pre-C++98 code such as this is still part of any production application, and if it is, it really ought to be rewritten.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>Making ‘</span><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>this</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’ not de-referenceable seems to me to be a really good idea semantically, and if it yields performance advantages too, then this is a really good thing. Perhaps it might be a good idea to ping either Bjarne Stroustrup or the C++ Standards committee to be sure - though I expect many of the committee’s members are also participants in this forum.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'> MartinO<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:windowtext'>From:</span></b><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:windowtext'> cfe-dev [mailto:cfe-dev-bounces@lists.llvm.org] <b>On Behalf Of </b>Hal Finkel via cfe-dev<br><b>Sent:</b> 06 December 2017 13:07<br><b>To:</b> Lei Huang <lei@ca.ibm.com>; cfe-dev@lists.llvm.org<br><b>Cc:</b> LLVM on Power <powerllvm@ca.ibm.com><br><b>Subject:</b> Re: [cfe-dev] [RFC] Setting dereferenceable flag on the implicit this parameter for non-static member functions<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><p><o:p> </o:p></p><div><p class=MsoNormal>On 12/05/2017 01:47 PM, Lei Huang via cfe-dev wrote:<o:p></o:p></p></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><div><div><p class=MsoNormal><span style='font-size:10.5pt;font-family:"Arial",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-family:"Arial",sans-serif'>Hello,</span><span style='font-size:9.0pt;font-family:"Helvetica",sans-serif'><br></span><span style='font-family:"Arial",sans-serif'> </span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Arial",sans-serif'>In the discussion on bugzilla 30729, it is mentioned that the 'this' pointer needs to be valid upon entry to a non-static method. Does the standard guarantee </span><span style='font-family:Courier'>this</span><span style='font-family:"Arial",sans-serif'> is non-null on entry?</span><span style='font-size:10.5pt;font-family:"Arial",sans-serif'><o:p></o:p></span></p></div></div></div></blockquote><p class=MsoNormal><br>Yes. You have to call a non-static member function on a valid object.<br><br><br><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><div><div><p class=MsoNormal><span style='font-family:"Arial",sans-serif'> If so, is there a reason we can't use that fact to mark '</span><span style='font-family:"Courier New"'>this'</span><span style='font-family:"Arial",sans-serif'> as '</span><span style='font-family:"Courier New"'>dereferenceable(sizeof(*this))'</span><span style='font-family:"Arial",sans-serif'>?</span><span style='font-size:10.5pt;font-family:"Arial",sans-serif'><o:p></o:p></span></p></div></div></div></blockquote><p class=MsoNormal><br>Yes, this seems like a good idea.<br><br> -Hal<br><br><br><o:p></o:p></p><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><div><div><p class=MsoNormal><span style='font-family:"Arial",sans-serif'> </span><span style='font-size:9.0pt;font-family:"Helvetica",sans-serif'><br></span><span style='font-family:"Arial",sans-serif'>There are LICM optimizations we can do based on the knowledge that '</span><span style='font-family:"Courier New"'>this' </span><span style='font-family:"Arial",sans-serif'>is non-null on entry to a non-static member function. </span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Arial",sans-serif'> </span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Arial",sans-serif'>eg. For the following IR, the two highlighted loads are not being hoisted out of the for loop because we are not able to guarantee that the pointer is non-null. If the '</span><span style='font-family:"Courier New"'>this</span><span style='font-family:"Arial",sans-serif'>' pointer is guaranteed to be non-null on entry and we mark it thus, then the 2 loads within the for-loop body can then be hoisted out into the loop preheader.</span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Arial",sans-serif'> </span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Arial",sans-serif'>This is of course just one example of an optimization we could perform based on this knowledge, but there are probably a number of others (i.e. anything that relies on a pointer being '</span><span style='font-family:"Courier New"'>dereferenceable(N)'</span><span style='font-family:"Arial",sans-serif'>).</span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Arial",sans-serif'> </span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><b><span style='font-family:"Courier New"'>$ cat a.ll</span></b><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Courier New"'>target datalayout = "e-m:e-i64:64-n32:64"<br>target triple = "powerpc64le-unknown-linux-gnu"</span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Courier New"'>%struct.S = type { <4 x i32>, <4 x i32> }</span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Courier New"'>; Function Attrs: norecurse nounwind readonly<br>define <4 x i32> @_ZNK1S20constShouldBeHoistedEmDv4_i(%struct.S* nocapture readonly %this, i64 %n, <4 x<br>i32> %x) align 2 {<br>entry:<br> %tobool9 = icmp eq i64 %n, 0<br> br i1 %tobool9, label %for.end, label %for.body.lr.ph</span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Courier New"'>for.body.lr.ph: ; preds = %entry<br> %k1 = getelementptr inbounds %struct.S, %struct.S* %this, i64 0, i32 0<br> %k2 = getelementptr inbounds %struct.S, %struct.S* %this, i64 0, i32 1<br> br label %for.body</span><span style='font-size:9.0pt;font-family:"Helvetica",sans-serif'><br></span><span style='font-family:"Courier New"'>for.body: ; preds = %for.body.lr.ph, %if.end<br> %n.addr.011 = phi i64 [ %n, %for.body.lr.ph ], [ %div, %if.end ]<br> %x.addr.010 = phi <4 x i32> [ %x, %for.body.lr.ph ], [ %x.addr.1, %if.end ]<br> %rem = and i64 %n.addr.011, 15<br> %cmp = icmp eq i64 %rem, 0<br> br i1 %cmp, label %if.end, label %if.then</span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Courier New"'>if.then: ; preds = %for.body<br> <b>%0 = load <4 x i32>, <4 x i32>* %k1, align 16</b></span><span style='font-size:10.5pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Courier New"'> %add = add <4 x i32> %0, %x.addr.010<br> <b>%1 = load <4 x i32>, <4 x i32>* %k2, align 16</b></span><span style='font-size:10.5pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Courier New"'> %xor = xor <4 x i32> %add, %1<br> br label %if.end</span><span style='font-size:9.0pt;font-family:"Helvetica",sans-serif'><br></span><span style='font-family:"Courier New"'>if.end: ; preds = %for.body, %if.then<br> %x.addr.1 = phi <4 x i32> [ %xor, %if.then ], [ %x.addr.010, %for.body ]<br> %div = lshr i64 %n.addr.011, 4<br> %tobool = icmp eq i64 %div, 0<br> br i1 %tobool, label %for.end, label %for.body</span><span style='font-size:9.0pt;font-family:"Arial",sans-serif'><br></span><span style='font-family:"Courier New"'>for.end: ; preds = %if.end, %entry<br> %x.addr.0.lcssa = phi <4 x i32> [ %x, %entry ], [ %x.addr.1, %if.end ]<br> ret <4 x i32> %x.addr.0.lcssa<br>}</span><span style='font-size:10.5pt;font-family:"Helvetica",sans-serif'><br></span><span style='font-family:"Arial",sans-serif'> </span><span style='font-size:10.5pt;font-family:"Arial",sans-serif'><o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:10.5pt;font-family:"Arial",sans-serif'> <o:p></o:p></span></p></div><div><div><div><div><div><p class=MsoNormal><span style='font-size:10.5pt;font-family:"Arial",sans-serif'><br>Regards,<br>Lei Huang<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:10.5pt;font-family:"Arial",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:10.5pt;font-family:"Arial",sans-serif'> <o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:10.5pt;font-family:"Arial",sans-serif'>LLVM Development on POWER<o:p></o:p></span></p></div><div><p class=MsoNormal><span style='font-size:10.5pt;font-family:"Arial",sans-serif'>Internal mail: C2/YGK/8200/MKM<br>Phone: (905) 413-4419<br>TieLine: 969-4419<br>E-mail: <a href="mailto:lei@ca.ibm.com">lei@ca.ibm.com</a><o:p></o:p></span></p></div></div></div></div></div></div></div><p class=MsoNormal><br><br><br><br><o:p></o:p></p><pre>_______________________________________________<o:p></o:p></pre><pre>cfe-dev mailing list<o:p></o:p></pre><pre><a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><o:p></o:p></pre><pre><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><o:p></o:p></pre></blockquote><p class=MsoNormal><br><br><o:p></o:p></p><pre>-- <o:p></o:p></pre><pre>Hal Finkel<o:p></o:p></pre><pre>Lead, Compiler Technology and Programming Languages<o:p></o:p></pre><pre>Leadership Computing Facility<o:p></o:p></pre><pre>Argonne National Laboratory<o:p></o:p></pre></div></body></html>