Thanks again Duncan.<div>As you said just prototype is enough. So in mypass.c I write my pass and declare func(x,y,op) and in mypass.h I write my definition for func(x,y,op). Kindly let me know my understanding is correct? So next when ever i replace instruction with op uses with this func automatically correct code in intermediate llvm IR representation would be inserted. </div>

<div><br></div><div>Writing function definition for func(x,y,op) using IR builder will be painful I guess.</div><div><br></div><div>Regards,<br>Manish</div><div><br><div class="gmail_quote">On Thu, May 12, 2011 at 2:05 AM, Duncan Sands <span dir="ltr"><<a href="mailto:baldrick@free.fr">baldrick@free.fr</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi Manish,<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Great!! Thanks Duncan.<br>
By step 1 that you mentioned it seems I can write my function func(x,y,op) in<br>
mypass.cpp itself. Please elaborate how can add it to the module?<br>
</blockquote>
<br></div>
the minimum is to add a prototype for "func" to the module.  For that you can<br>
use getOrInsertFunction.  If you want to insert the code defining "func" too,<br>
then insert the prototype, add a basic block to it, use an IRBuilder to add<br>
instructions to the basic block.<br>
<br>
Ciao, Duncan.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
<br>
<br>
On Thu, May 12, 2011 at 1:22 AM, Duncan Sands <<a href="mailto:baldrick@free.fr" target="_blank">baldrick@free.fr</a><br></div><div class="im">
<mailto:<a href="mailto:baldrick@free.fr" target="_blank">baldrick@free.fr</a>>> wrote:<br>
<br>
    Hi Manish,<br>
<br>
     > I am planning to extract every operation as function.<br>
     ><br>
     > eg. z=x+y; ==> z=func(x,y,op);<br>
     ><br>
     > I wish to write a custom definition of op. Which I would like to define<br>
    in func.<br>
     ><br>
     > It would be really helpful if someone could suggest passes already<br>
    available in<br>
     > llvm to look for or point some references how should one proceed on it.<br>
<br>
    I think you should write your own pass that<br>
    (1) adds a declaration of "func" to the module<br>
    (2) iterates over all functions, and all instructions in the function.  When it<br>
    sees a binary operator ("isa<BinaryOperator>(Instruction)") it needs to first<br>
    insert a call just in front of the instruction (see an IRBuilder to do this;<br>
    use "SetInsertPoint(Instruction)" to tell it where to add the call; use<br>
    CreateCall3 to create the call).  Then use replaceAllUsesWith to replace every<br>
    use of the binary operator with the call you just created.  Finally, delete the<br>
    original instruction (eraseFromParent).<br>
<br>
    Ciao, Duncan.<br>
<br>
     ><br>
     > Is codeExtractor.cpp useful for this. I was also thinking of going like<br>
     > loop-extract way. any other suggestion would be extremely helpful<br>
     ><br>
     ><br>
     ><br>
     > _______________________________________________<br>
     > LLVM Developers mailing list<br></div>
     > <a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <mailto:<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><div class="im">

<br>
     > <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br>
    _______________________________________________<br>
    LLVM Developers mailing list<br></div>
    <a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <mailto:<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><div class="im">

<br>
    <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br>
<br>
</div></blockquote>
<br>
</blockquote></div><br></div>