<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    On 4/14/11 6:34 PM, Kodakara, Sreekumar V wrote:
    <blockquote
cite="mid:16E8F37F0444C34FB5098B0061DFF0320D75C00F@rrsmsx502.amr.corp.intel.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=ISO-8859-1">
      <meta name="Generator" content="Microsoft Word 12 (filtered
        medium)">
      <style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-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-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@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]-->
      <div class="WordSection1">
        <p class="MsoNormal">Hi All,<o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">I have a question on ConstantExpressions
          and llvm intrinsic memcpy/memset/memmove. I am using llvm-2.8
          release. In one of the C programs that I am compiling using
          clang frontend, the call to memcpy instrinsic looks like the
          following<o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">call void @llvm.memcpy.p0i8.p0i8.i64(i8*
          %tmp2, i8* bitcast (%struct.ta* @tret to i8*), i64 4, i32 4,
          i1 false), !dbg !19<o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">The second argument to memcpy is of type
          “struct ta” and global variable “tret” is defined as follows
          in the code.<o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">struct ta {<o:p></o:p></p>
        <p class="MsoNormal">  int a;<o:p></o:p></p>
        <p class="MsoNormal">};<o:p></o:p></p>
        <p class="MsoNormal">struct ta tret;<o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">Since memcpy takes i8* as its argument, the
          struct.ta* is bitcasted to i8* in the call to llvm.memcpy. For
          a pass that I am working on, I would like to know the original
          type of this pointer. In this example, I would like to know
          that the second argument is a pointer to type struct.ta, and
          hence the size of memory allocated to that pointer is 4 bytes.
        </p>
      </div>
    </blockquote>
    <br>
    You should be able to use the stripPointerCasts() method of
    llvm::Value * to strip off the bitcast.<br>
    <br>
    <blockquote
cite="mid:16E8F37F0444C34FB5098B0061DFF0320D75C00F@rrsmsx502.amr.corp.intel.com"
      type="cite">
      <div class="WordSection1">
        <p class="MsoNormal"><o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">I understand that the second argument to
          memcpy intrinsic  namely i8* bitcast (%struct.ta* @t1 to i8*)
           is a ConstantExpression (CE). When I dump
          CE->getArgument(0), I get the following. <o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">@t1 = global %struct.ta zeroinitializer,
          align 4<o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">But from here, I am not able to find the
          relevant class/API to extract the type of
          CE->getArgument(0) and hence the size of it. When I tried
          CE->getArgument(0)->getType(), I am getting the type as
          Pointer and hence the size to be 8 bytes. </p>
      </div>
    </blockquote>
    <br>
    This is because all global variables in LLVM are pointers to the
    actual data in memory.  What you need to do is:<br>
    <br>
    if (GlobalVariable * GV =
    dyn_cast<GlobalVariable>(CE->getArgument(0)) {<br>
        Type * T = GV->getType()->getContainedType();<br>
    }<br>
    <blockquote
cite="mid:16E8F37F0444C34FB5098B0061DFF0320D75C00F@rrsmsx502.amr.corp.intel.com"
      type="cite">
      <div class="WordSection1">
        <p class="MsoNormal">I would like to get the type as StructTy
          and size to be 4 bytes. I searched through the llvm code <o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">Any help is appreciated. </p>
      </div>
    </blockquote>
    <br>
    -- John T.<br>
    <br>
    <blockquote
cite="mid:16E8F37F0444C34FB5098B0061DFF0320D75C00F@rrsmsx502.amr.corp.intel.com"
      type="cite">
      <div class="WordSection1">
        <p class="MsoNormal"><o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">Thanks<o:p></o:p></p>
        <p class="MsoNormal">Sreekumar<o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
      </div>
      <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a class="moz-txt-link-freetext" href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a>
<a class="moz-txt-link-freetext" href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>