<!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 bgcolor="#ffffff" text="#000000">
    On 5/16/11 9:35 AM, Chuck Zhao wrote:
    <blockquote cite="mid:4DD13634.3080300@eecg.toronto.edu" type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=ISO-8859-1">
      <font size="+1">I have the following prototype for a function:<br>
        void bkp_memory(char *, int);<br>
        <br>
        Inside my LLVM IR, I have a callsite looks like the following:<br>
      </font>
      <pre wrap="">tail call void @bkp_memory(i8* bitcast (i32** @P to i8*), i32 4) nounwind


</pre>
      When I try to obtain its 1st argument and check whether it is a
      valid instruction, using:<br>
      Instruction *Inst = dyn_cast<Instruction
      *>(I->getOperand(0));<br>
      <br>
      it gives me a NULL Ptr, which is quite a surprise to me.<br>
      <br>
      Am I doing anything wrong here?<br>
    </blockquote>
    <br>
    The bitcast you see above is not an instruction.  Rather, it is a
    constant expression and is represented by a ConstExpr object in the
    C++ API.  That is most likely because @P is a constant (a function
    pointer, global value, or something similar).<br>
    <br>
    So,<br>
    <br>
    1) Use the stripPointerCasts() method of llvm::Value to strip away
    all pointer casts (whether they are cast instructions or ConstExpr
    casts):<br>
    <br>
    I->getOperand(0)->stripPointerCasts()<br>
    <br>
    2) Realize that not all operands are instructions.  In this case,
    the operand is probably a global variable.  Chances are good that:<br>
    <br>
dyn_cast<Instruction>(I->getOperand(0)->stripPointerCasts())<br>
    <br>
    ... will return NULL because the operand is a constant and not an
    instruction.<br>
    <br>
    <br>
    <blockquote cite="mid:4DD13634.3080300@eecg.toronto.edu" type="cite">
      <br>
      <br>
      Note that in many other cases, the bkp_memory() callsite won't
      have the embedded form. They look like:<br>
      <pre wrap="">  store i32* %21, i32** @P, align 4
  %22 = bitcast i32* %21 to i8*
  tail call void @bkp_memory(i8* %22, i32 4) nounwind

And the dyn_cast<> conversion is fine in these cases.
</pre>
      Would that be the problem?<br>
    </blockquote>
    <br>
    The "embedded" form is a constant expression.  The "unembedded" form
    is an instruction.  LLVM has several constant expressions that are
    modeled after instructions; the difference is that a constant
    expression's operands are all constants, and therefore the constant
    expression can also be used like a constant.<br>
    <br>
    For more details, please see the LLVM Language Reference manual.<br>
    <br>
    -- John T.<br>
    <br>
    <blockquote cite="mid:4DD13634.3080300@eecg.toronto.edu" type="cite">
      <br>
      <br>
      Thank you<br>
      <br>
      Chuck<br>
      <br>
      <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>