<div dir="ltr"><div><div>I am not sure that there is a requirement that *this remains a valid object.<br></div>Experimenting with the -O3 behaviour of the following program (with a powerpc64le-unknown-linux-gnu target), it seems the dereferenceable attribute is scope based?<br><br>int a[1], b[1], *cp;<br>void dobadstuff() { delete cp; }<br><br>__attribute__((__noinline__))<br>void foo(int * __restrict__ a, int * __restrict__ b, int &c, int n) {<br>  dobadstuff();<br>  for (int i = 0; i < n; ++i)<br>    if (a[i] > 0)<br>      a[i] = c * b[i];<br>}<br><br>int main(void) {<br>  cp = new int;<br>  foo(a, b, *cp, 1);<br>}<br><br></div>Valgrind reports an invalid read.<br><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 6, 2017 at 8:06 AM, Hal Finkel via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000"><span class="">
    <p><br>
    </p>
    <div class="m_3987557004598263729moz-cite-prefix">On 12/05/2017 01:47 PM, Lei Huang via
      cfe-dev wrote:<br>
    </div>
    <blockquote type="cite">
      
      <div class="m_3987557004598263729socmaildefaultfont" dir="ltr" style="font-family:Arial,Helvetica,sans-serif;font-size:10.5pt">
        <div class="m_3987557004598263729socmaildefaultfont" dir="ltr" style="font-family:Arial,Helvetica,sans-serif;font-size:10.5pt">
          <div dir="ltr"> </div>
          <div dir="ltr"><font style="outline:none" size="3" face="Arial">Hello,</font><br style="outline:none;font-family:"Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;font-size:12px">
            <font style="outline:none" size="3" face="Arial"> </font><br>
            <font style="outline:none" size="3" face="Arial">In the
              discussion on bugzilla 30729, it is mentioned that the <span>'</span></font><span><font style="outline:none" size="3">this'</font></span><font style="outline:none" size="3" face="Arial"> pointer needs to be valid
              upon entry to a non-static method.  Does the standard
              guarantee </font><font style="outline:none" size="3" face="Courier">this</font><font style="outline:none" size="3" face="Arial"> is non-null on entry?</font></div>
        </div>
      </div>
    </blockquote>
    <br></span>
    Yes. You have to call a non-static member function on a valid
    object.<span class=""><br>
    <br>
    <blockquote type="cite">
      <div class="m_3987557004598263729socmaildefaultfont" dir="ltr" style="font-family:Arial,Helvetica,sans-serif;font-size:10.5pt">
        <div class="m_3987557004598263729socmaildefaultfont" dir="ltr" style="font-family:Arial,Helvetica,sans-serif;font-size:10.5pt">
          <div dir="ltr"><font style="outline:none" size="3" face="Arial">  If so, is there a reason we can't use that
              fact to mark '</font><font style="outline:none" size="3" face="Courier New">this'</font><font style="outline:none" size="3" face="Arial"> as '</font><font style="outline:none" size="3" face="Courier New">dereferenceable(sizeof(*this)<wbr>)'</font><font style="outline:none" size="3" face="Arial">?</font><br>
          </div>
        </div>
      </div>
    </blockquote>
    <br></span>
    Yes, this seems like a good idea.<br>
    <br>
     -Hal<br>
    <br>
    <blockquote type="cite"><div><div class="h5">
      <div class="m_3987557004598263729socmaildefaultfont" dir="ltr" style="font-family:Arial,Helvetica,sans-serif;font-size:10.5pt">
        <div class="m_3987557004598263729socmaildefaultfont" dir="ltr" style="font-family:Arial,Helvetica,sans-serif;font-size:10.5pt">
          <div dir="ltr"><font style="outline:none" size="3" face="Arial"> </font><br style="outline:none;font-family:"Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;font-size:12px">
            <font style="outline:none" size="3" face="Arial">There are
              LICM optimizations we can do based on the knowledge that '</font><font style="outline:none" size="3" face="Courier New">this' </font><font style="outline:none" size="3" face="Arial">is non-null
              on entry to a non-static member function. </font><br>
            <font style="outline:none" size="3" face="Arial"> </font><br>
            <font style="outline:none" size="3" face="Arial">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 '</font><font style="outline:none" size="3" face="Courier New">this</font><font style="outline:none" size="3" face="Arial">' 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.</font><br>
            <font style="outline:none" size="3" face="Arial"> </font><br>
            <font style="outline:none" size="3" face="Arial">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 '</font><font style="outline:none" size="3" face="Courier New">dereferenceable(N)'</font><font style="outline:none" size="3" face="Arial">).</font><br>
            <font style="outline:none" size="3" face="Arial"> </font><br>
            <b><font style="outline:none" size="3" face="Courier New">$ cat a.ll</font></b><br>
            <font style="outline:none" size="3" face="Courier New">target
              datalayout = "e-m:e-i64:64-n32:64"<br style="outline:none">
              target triple = "powerpc64le-unknown-linux-<wbr>gnu"</font><br>
            <font style="outline:none" size="3" face="Courier New">%struct.S
              = type { <4 x i32>, <4 x i32> }</font><br>
            <font style="outline:none" size="3" face="Courier New">;
              Function Attrs: norecurse nounwind readonly<br style="outline:none">
              define <4 x i32>
              @_<wbr>ZNK1S20constShouldBeHoistedEmD<wbr>v4_i(%struct.S* nocapture
              readonly %this, i64 %n, <4 x<br style="outline:none">
              i32> %x) align 2 {<br style="outline:none">
              entry:<br style="outline:none">
               %tobool9 = icmp eq i64 %n, 0<br style="outline:none">
               br i1 %tobool9, label %for.end, label %<a href="http://for.body.lr.ph" target="_blank">for.body.lr.ph</a></font><br>
            <font style="outline:none" size="3" face="Courier New"><a href="http://for.body.lr.ph" target="_blank">for.body.lr.ph</a>:
                                                ; preds = %entry<br style="outline:none">
               %k1 = getelementptr inbounds %struct.S, %struct.S* %this,
              i64 0, i32 0<br style="outline:none">
               %k2 = getelementptr inbounds %struct.S, %struct.S* %this,
              i64 0, i32 1<br style="outline:none">
               br label %for.body</font><br style="outline:none;font-family:"Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;font-size:12px">
            <font style="outline:none" size="3" face="Courier New">for.body:
                                                      ; preds =
              %<a href="http://for.body.lr.ph" target="_blank">for.body.lr.ph</a>, %if.end<br style="outline:none">
               %n.addr.011 = phi i64 [ %n, %<a href="http://for.body.lr.ph" target="_blank">for.body.lr.ph</a> ], [ %div,
              %if.end ]<br style="outline:none">
               %x.addr.010 = phi <4 x i32> [ %x, %<a href="http://for.body.lr.ph" target="_blank">for.body.lr.ph</a>
              ], [ %x.addr.1, %if.end ]<br style="outline:none">
               %rem = and i64 %n.addr.011, 15<br style="outline:none">
               %cmp = icmp eq i64 %rem, 0<br style="outline:none">
               br i1 %cmp, label %if.end, label %if.then</font><br>
            <font style="outline:none" size="3" face="Courier New">if.then:
                                                       ; preds =
              %for.body<br style="outline:none">
               </font><b><font style="outline:none" size="3" face="Courier New">%0 =
                load <4 x i32>, <4 x i32>* %k1, align 16</font></b><br style="outline:none">
            <font style="outline:none" size="3" face="Courier New"> %add
              = add <4 x i32> %0, %x.addr.010<br style="outline:none">
               </font><b><font style="outline:none" size="3" face="Courier New">%1 =
                load <4 x i32>, <4 x i32>* %k2, align 16</font></b><br style="outline:none">
            <font style="outline:none" size="3" face="Courier New"> %xor
              = xor <4 x i32> %add, %1<br style="outline:none">
               br label %if.end</font><br style="outline:none;font-family:"Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;font-size:12px">
            <font style="outline:none" size="3" face="Courier New">if.end:
                                                        ; preds =
              %for.body, %if.then<br style="outline:none">
               %x.addr.1 = phi <4 x i32> [ %xor, %if.then ], [
              %x.addr.010, %for.body ]<br style="outline:none">
               %div = lshr i64 %n.addr.011, 4<br style="outline:none">
               %tobool = icmp eq i64 %div, 0<br style="outline:none">
               br i1 %tobool, label %for.end, label %for.body</font><br>
            <font style="outline:none" size="3" face="Courier New">for.end:
                                                       ; preds =
              %if.end, %entry<br style="outline:none">
               %x.addr.0.lcssa = phi <4 x i32> [ %x, %entry ], [
              %x.addr.1, %if.end ]<br style="outline:none">
               ret <4 x i32> %x.addr.0.lcssa<br style="outline:none">
              }</font><br>
            <font style="outline:none" size="3" face="Arial"> </font></div>
          <div dir="ltr"> </div>
          <div dir="ltr">
            <div class="m_3987557004598263729socmaildefaultfont" dir="ltr" style="font-family:Arial,Helvetica,sans-serif;font-size:10.5pt">
              <div class="m_3987557004598263729socmaildefaultfont" dir="ltr" style="font-family:Arial,Helvetica,sans-serif;font-size:10.5pt">
                <div class="m_3987557004598263729socmaildefaultfont" dir="ltr" style="font-family:Arial,Helvetica,sans-serif;font-size:10.5pt">
                  <div dir="ltr"><br>
                    Regards,<br>
                    Lei Huang</div>
                  <div dir="ltr"> </div>
                  <div dir="ltr"> </div>
                  <div dir="ltr">LLVM Development on POWER</div>
                  <div dir="ltr">Internal mail: C2/YGK/8200/MKM<br>
                    Phone: <a href="tel:(905)%20413-4419" value="+19054134419" target="_blank">(905) 413-4419</a><br>
                    TieLine: 969-4419<br>
                    E-mail: <a class="m_3987557004598263729moz-txt-link-abbreviated" href="mailto:lei@ca.ibm.com" target="_blank">lei@ca.ibm.com</a></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <br>
      <br>
      <fieldset class="m_3987557004598263729mimeAttachmentHeader"></fieldset>
      <br>
      </div></div><pre>______________________________<wbr>_________________
cfe-dev mailing list
<a class="m_3987557004598263729moz-txt-link-abbreviated" href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>
<a class="m_3987557004598263729moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><span class="HOEnZb"><font color="#888888">
</font></span></pre><span class="HOEnZb"><font color="#888888">
    </font></span></blockquote><span class="HOEnZb"><font color="#888888">
    <br>
    <pre class="m_3987557004598263729moz-signature" cols="72">-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
  </font></span></div>

<br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">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/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>