<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <br>
    <div class="moz-cite-prefix">On 12/01/2014 06:05 PM, Andrew Trick
      wrote:<br>
    </div>
    <blockquote
      cite="mid:A6CB4409-CAB2-4245-80E7-F35E1F394318@apple.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      <br class="">
      <div>
        <blockquote type="cite" class="">
          <div class="">On Dec 1, 2014, at 3:44 PM, Philip Reames <<a
              moz-do-not-send="true"
              href="mailto:listmail@philipreames.com" class="">listmail@philipreames.com</a>>
            wrote:</div>
          <br class="Apple-interchange-newline">
          <div class="">
            <meta content="text/html; charset=windows-1252"
              http-equiv="Content-Type" class="">
            <div bgcolor="#FFFFFF" text="#000000" class=""> (Spawning a
              separate subthread off the 'Optimization hints for
              "constant" loads' discussion for a related question. )<br
                class="">
              <br class="">
              Looking at TBAA again, I was reminded that TBAA also
              contains a third field which indicates that "meaning <tt
                class="literal docutils">pointsToConstantMemory</tt>
              should return true; see <a moz-do-not-send="true"
                class="external reference"
                href="http://llvm.org/docs/AliasAnalysis.html#OtherItfs">other

                useful AliasAnalysis methods</a>".  Looking at this a
              bit, it really seems like this flag has the exact same
              meaning as !invariant.load.  <br class="">
              <br class="">
              pointsToConstantMemory returns a value for a Location. 
              Since it is entirely legal to have two Locations which
              describe the same physical memory, it seems like you'd be
              back to the same semantics as !invariant.load.  <br
                class="">
              <br class="">
              The only uncertainty here is that a Location is clearly
              (??) position/Instruction specific.  Does that also imply
              that the Location is control dependent?  What is the
              semantics of the following code?<br class="">
              if (is_known_invariant) {<br class="">
                load %p, !tbaa is_constant=true<br class="">
              }<br class="">
              <br class="">
              Is the optimizer allowed to lift the load above the
              conditional?  (Assuming it can prove the location is known
              dereferenceable.)  The semantics for !invariant.load
              clearly allow this, but do the semantics for TBAA's
              constant flag?  <br class="">
              <br class="">
              I think the answer to these questions is that the load is
              *not* control dependent on the conditional (assuming it's
              otherwise known dereferenceable).  Given this, why do we
              have both?  Should we canonicalize one into the other?<br
                class="">
            </div>
          </div>
        </blockquote>
        <div><br class="">
        </div>
        <div>It would be very confusing if the two had different
          semantics.</div>
        <div><br class="">
        </div>
        <div>In either case, hoisting the load (without dropping
          metadata) is definitely legit. </div>
      </div>
    </blockquote>
    Agreed up to here.<br>
    <blockquote
      cite="mid:A6CB4409-CAB2-4245-80E7-F35E1F394318@apple.com"
      type="cite">
      <div>
        <div>But conservatively, the invariance is still a path
          sensitive property. The load is invariant w.r.t. any store as
          long as control reaches a use-with-side-effects of the loaded
          value.</div>
      </div>
    </blockquote>
    I disagree with this.  The !invariant.load metadata is not a path
    sensitive property.  It is a property of the pointer value which
    happens to be described on the load.  Consider how we use the
    !invariant.load metadata in LICM.  We look at the load (*not* it's
    uses) and decide to skip all remaining alias checks.  <br>
    <br>
    To be clear, the preceding statement is not true of
    llvm.invariant.start and llvm.invariant.end.  <br>
    <blockquote
      cite="mid:A6CB4409-CAB2-4245-80E7-F35E1F394318@apple.com"
      type="cite">
      <div>
        <div><br class="">
        </div>
        <div>Given:</div>
        <div><br class="">
        </div>
        <div>store %q</div>
        <div>m1 = load %p</div>
        <div>if <something> {</div>
        <div> m2 = load %p, !invariant.load</div>
        <div>}</div>
        <div>m = phi(m1, m2)</div>
        <div><br class="">
        </div>
        <div>We can safely convert to:</div>
        <div>
          <div><br class="">
          </div>
          <div>m2 = load %p, !invariant.load</div>
          <div>store %q</div>
          <div>m1 = load %p</div>
          <div>if <something> {}</div>
          <div>m = phi(m1, m2)</div>
          <div class=""><br class="">
          </div>
          <div class="">But cannot safely convert to:</div>
          <div class=""><br class="">
          </div>
        </div>
        <div>
          <div>m = load %p, !invariant.load</div>
          <div>store %q</div>
          <div><br class="">
          </div>
          <div>I would *really* like to specify more aggressive
            semantics so that we can do that, but haven’t adequately
            proved we can do that safely. I’m don’t think the optimizer
            will do the unsafe thing today, unless there’s an oversight
            somewhere. <br>
          </div>
        </div>
      </div>
    </blockquote>
    We perform nearly exactly this transformation today.  Consider LICM:<br>
    load %p<br>
    for(int i = 0; i < 1; i++) {<br>
      store %p<br>
      load %p !invariant.load<br>
    }<br>
    <br>
    LICM - in isolation - will transform this to:<br>
    load %p<br>
    load %p, !invariant.load<br>
    for(int i = 0; i < 1; i++) { }<br>
    store %p<br>
    <br>
    Can you be more explicit about what you mean by "safely"?  Given the
    semantics I understand for !invariant.load, the transformation
    you've described is entirely legal.  We might not implement it today
    - I don't believe we'd forward the non-invariant load to the
    invariant load in your original example - but that's a limitation of
    the implementation.  <br>
    <br>
    <br>
    <blockquote
      cite="mid:A6CB4409-CAB2-4245-80E7-F35E1F394318@apple.com"
      type="cite">
      <div>
        <div>
          <div><br class="">
          </div>
        </div>
        <blockquote type="cite" class="">
          <div class="">
            <div bgcolor="#FFFFFF" text="#000000" class=""> Looking at
              the current implementations, it appears that TBAA's
              constant flag is more broadly implemented.  On first
              glance, I'm really tempted to just deprecate
              !invariant.load in place of TBAA's constant flag. 
              Thoughts?<br class="">
            </div>
          </div>
        </blockquote>
        <div><br class="">
        </div>
        <div>I don’t have a strong opinion here. I’m fine relying on
          TBAA being enabled to active these sort of optimizations.</div>
      </div>
    </blockquote>
    I'll throw together a patch in a few days.<br>
    <blockquote
      cite="mid:A6CB4409-CAB2-4245-80E7-F35E1F394318@apple.com"
      type="cite">
      <div>
        <div><br class="">
        </div>
        <div>-Andy</div>
        <br class="">
        <blockquote type="cite" class="">
          <div class="">
            <div bgcolor="#FFFFFF" text="#000000" class=""> Philip<br
                class="">
              <br class="">
              <br class="">
              <br class="">
            </div>
          </div>
        </blockquote>
      </div>
      <br class="">
    </blockquote>
    <br>
  </body>
</html>