<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 6/7/13 4:39 PM, Eli Friedman wrote:<br>
    </div>
    <blockquote
cite="mid:CAJdarcGpDp-1QTwvhk+KASo_bAj4Jg=vnjkqxUGRke1P7uaSwQ@mail.gmail.com"
      type="cite">
      <div dir="ltr">On Fri, Jun 7, 2013 at 3:45 PM, Shuxin Yang <span
          dir="ltr"><<a moz-do-not-send="true"
            href="mailto:shuxin.llvm@gmail.com" target="_blank">shuxin.llvm@gmail.com</a>></span>
        wrote:<br>
        <div class="gmail_extra">
          <div class="gmail_quote">
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">Author:
              shuxin_yang<br>
              Date: Fri Jun  7 17:45:21 2013<br>
              New Revision: 183584<br>
              <br>
              URL: <a moz-do-not-send="true"
                href="http://llvm.org/viewvc/llvm-project?rev=183584&view=rev"
                target="_blank">http://llvm.org/viewvc/llvm-project?rev=183584&view=rev</a><br>
              Log:<br>
                Fix an assertion in MemCpyOpt pass.<br>
              <br>
                The MemCpyOpt pass is capable of optimizing:<br>
                    callee(&S); copy N bytes from S to D.<br>
                  into:<br>
                    callee(&D);<br>
              subject to some legality constraints.<br>
              <br>
                Assertion is triggered when the compiler tries to
              evalute "sizeof(typeof(D))",<br>
              while D is an opaque-typed, 'sret' formal argument of
              function being compiled.<br>
              i.e. the signature of the func being compiled is something
              like this:<br>
                T caller(...,%opaque* noalias nocapture sret %D, ...)<br>
              <br>
                The fix is that when come across such situation, instead
              of calling some<br>
              utility functions to get the size of D's type (which will
              crash), we simply<br>
              assume D has at least N bytes as implified by the
              copy-instruction.<br>
              <br>
              rdar://14073661<br>
              <br>
              Modified:<br>
                  llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp<br>
                  llvm/trunk/test/Transforms/MemCpyOpt/memcpy.ll<br>
              <br>
              Modified:
              llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp<br>
              URL: <a moz-do-not-send="true"
href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp?rev=183584&r1=183583&r2=183584&view=diff"
                target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp?rev=183584&r1=183583&r2=183584&view=diff</a><br>
==============================================================================<br>
              --- llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp
              (original)<br>
              +++ llvm/trunk/lib/Transforms/Scalar/MemCpyOptimizer.cpp
              Fri Jun  7 17:45:21 2013<br>
              @@ -626,8 +626,10 @@ bool
              MemCpyOpt::performCallSlotOptzn(Ins<br>
                     return false;<br>
              <br>
                   Type *StructTy =
              cast<PointerType>(A->getType())->getElementType();<br>
              -    uint64_t destSize =
              TD->getTypeAllocSize(StructTy);<br>
              -<br>
              +    // If StructTy is an opaque type, it should have at
              least <cpyLen> bytes,<br>
              +    // as implified by the copy-instruction.<br>
              +    uint64_t destSize = StructTy->isSized() ?<br>
              +                        TD->getTypeAllocSize(StructTy)
              : cpyLen;<br>
                   if (destSize < srcSize)<br>
                     return false;<br>
            </blockquote>
            <div><br>
            </div>
            <div>Why is it safe to assume StructTy is at least cpyLen
              bytes?  <br>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    If not, why is it possible copying cpyLen byte from (part of) src to
    Dest? <br>
    <br>
    <blockquote
cite="mid:CAJdarcGpDp-1QTwvhk+KASo_bAj4Jg=vnjkqxUGRke1P7uaSwQ@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div class="gmail_extra">
          <div class="gmail_quote">
            <div>And if it is safe, why is this check necessary at all?</div>
            <div><br>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
    I guess you confuse cpyLen with SrcLen. <br>
    I asked the same question when I read the code. After the 2nd
    thought, I think it is necessary. <br>
    You never know how the callee access the Src, it could access the
    last byte (the SrcSize-th byte) of the object <br>
    corresponding to the Src, and cpyLen < SrcSize. <br>
    <br>
    <br>
    <br>
    <br>
  </body>
</html>