Thanks Ciao<div><br></div><div>I am following your instruction  and able to write some code. Instead of foo() my aim is to add  </div><div><br></div><div>int mul_add(int x, int y, int z)</div><div>{</div><div>return (x*y+z)</div>
<div>}</div><div><br></div><div>Please find the attached source code in the email.</div><div><br></div><div>Issue I am facing now it is crashing and backtrace is </div><div><br></div><div><div>Assertion failed: (V->getParent() == 0 && "Value already in a container!!"), function addNodeToList, file SymbolTableListTraitsImpl.h, line 68.</div>
<div>0  opt               0x00000001007989b3 PrintStackTrace(void*) + 51</div><div>1  opt               0x0000000100798f1d SignalHandler(int) + 333</div><div>2  libSystem.B.dylib 0x00007fff8731d1ba _sigtramp + 26</div><div>
3  libSystem.B.dylib 0x0000000700000018 _sigtramp + 2026778232</div><div>4  opt               0x00000001000a0c2f raise + 31</div><div>5  opt               0x00000001000a0c60 abort + 16</div><div>6  opt               0x00000001000a0d2d __assert_rtn + 189</div>
<div>7  opt               0x0000000100063077 llvm::SymbolTableListTraits<llvm::Instruction, llvm::BasicBlock>::addNodeToList(llvm::Instruction*) + 101</div><div>8  LLVMHello.dylib   0x0000000101688a3c llvm::iplist<llvm::Instruction, llvm::ilist_traits<llvm::Instruction> >::insert(llvm::ilist_iterator<llvm::Instruction>, llvm::Instruction*) + 178</div>
<div>9  LLVMHello.dylib   0x00000001016877df (anonymous namespace)::Hello::doInitialization(llvm::Module&) + 2639</div><div>10 opt               0x000000010070ebd4 llvm::FPPassManager::doInitialization(llvm::Module&) + 86</div>
<div>11 opt               0x000000010070dfe5 llvm::FPPassManager::runOnModule(llvm::Module&) + 35</div><div>12 opt               0x000000010071317b llvm::MPPassManager::runOnModule(llvm::Module&) + 613</div><div>13 opt               0x0000000100714526 llvm::PassManagerImpl::run(llvm::Module&) + 150</div>
<div>14 opt               0x00000001007145cb llvm::PassManager::run(llvm::Module&) + 39</div><div>15 opt               0x00000001000b5a50 main + 7552</div><div>16 opt               0x00000001000a7578 start + 52</div><div>
Stack dump:</div><div><br></div><div><br></div><div>Also I am not sure how to send parameters to function mul_add() when I am calling it from main() function. i.e. how and where to set x = 1, y = 2 and z = 3 so that call from main() will be mul_add(1, 2, 3).</div>
<div><br></div><div>Please let me know</div><div><br></div><div>Thanks,</div><div>--Teja</div><br><div class="gmail_quote">On Mon, Oct 8, 2012 at 2:26 AM, Duncan Sands <span dir="ltr"><<a href="mailto:baldrick@free.fr" target="_blank">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 Teja,<div><div class="h5"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I am new to LLVM and interested in using LLVM to work on a research project for<br>
my Master's degree.<br>
My idea is following -<br>
<br>
1. I have a simple C program basic.c -<br>
#include <stdio.h><br>
int main() {<br>
    printf("Hello World");<br>
    return 0;<br>
}<br>
<br>
2. I will generate IR byte code for this basic.c using -<br>
llvm-gcc -emit-llvm -S basic.c<br>
<br>
This will give me basic.s which has IR byte code like this -<br>
<br>
; ModuleID = 'basic.c'<br>
target datalayout =<br>
"e-p:64:64:64-i1:8:8-i8:8:8-<u></u>i16:16:16-i32:32:32-i64:64:64-<u></u>f32:32:32-f64:64:64-v64:64:64-<u></u>v128:128:128-a0:0:64-s0:64:64-<u></u>f80:128:128-n8:16:32:64"<br>
target triple = "x86_64-apple-darwin10.8"<br>
<br>
@.str = private constant [12 x i8] c"hello world\00", align 1 ; <[12 x i8]*><br>
[#uses=1]<br>
<br>
define i32 @main() nounwind ssp {<br>
entry:<br>
   %retval = alloca i32                            ; <i32*> [#uses=2]<br>
   %0 = alloca i32                                 ; <i32*> [#uses=2]<br>
   %"alloca point" = bitcast i32 0 to i32          ; <i32> [#uses=0]<br>
   %1 = call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @.str, i64 0, i64<br>
0)) nounwind ; <i32> [#uses=0]<br>
   store i32 0, i32* %0, align 4<br>
   %2 = load i32* %0, align 4                      ; <i32> [#uses=1]<br>
   store i32 %2, i32* %retval, align 4<br>
   br label %return<br>
<br>
return:                                           ; preds = %entry<br>
   %retval1 = load i32* %retval                    ; <i32> [#uses=1]<br>
   ret i32 %retval1<br>
}<br>
<br>
declare i32 @puts(i8*)<br>
<br>
3. I want to write a pass to insert a call foo(int a) after the printf() call,<br>
so that it looks like this -<br>
#include <stdio.h><br>
int main() {<br>
    printf("Hello World");<br>
    foo(1);<br>
    return 0;<br>
}<br>
<br>
void foo(int a) {<br>
    printf("a + 5 is : %d", a + 5);<br>
}<br>
</blockquote>
<br></div></div>
you need to do:<br>
(1) insert a declaration of "foo" into the module;<br>
(2) find the spot in the IR where you want to put it, i.e. find the printf<br>
(3) insert the call<br>
<br>
For (1), use Function::Create.<br>
For (2), use Module::getFunction to lookup "main", then iterate over the basic<br>
blocks, and within each basic block iterate over instructions, looking for a<br>
call instruction, using isa<CallInst> to test if an instruction is a call.<br>
For (3), create an IRBuilder, set the insertion point to the instruction after<br>
the printf, and use the CreateCall method.<br>
<br>
Ciao, Duncan.<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
<br>
How can I achieve this? I looked online but couldn't find a definite source<br>
which tells me how to do this.<br>
I think I can use Cloning.h and CloneFunction.cpp to clone a given function and<br>
then insert my call to foo().<br>
But I'm not sure if this is the right way to do this. The documentation<br>
obviously did not say this explicitly.<br>
Any thoughts / pointers ?<br>
<br>
Thanks in advance !<br>
- Teja<br>
<br>
<br></div>
______________________________<u></u>_________________<br>
LLVM Developers mailing list<br>
<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><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvmdev</a><br>
<br>
</blockquote>
<br>
______________________________<u></u>_________________<br>
LLVM Developers mailing list<br>
<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><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvmdev</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Regards,<br>Teja<br>
</div>