Hi,<div><br><div>I am new to LLVM and interested in using LLVM to work on a research project for my Master's degree.</div><div>My idea is following -</div><div><br></div><div>1. I have a simple C program basic.c -</div>
<div>#include <stdio.h></div><div>int main() {</div><div>   printf("Hello World");</div><div>   return 0;</div><div>}</div><div><br></div><div>2. I will generate IR byte code for this basic.c using -</div>
<div>llvm-gcc -emit-llvm -S basic.c</div><div><br></div><div>This will give me basic.s which has IR byte code like this -</div><div><br></div><div><div><div>; ModuleID = 'basic.c'</div><div>target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"</div>
<div>target triple = "x86_64-apple-darwin10.8"</div><div><br></div><div>@.str = private constant [12 x i8] c"hello world\00", align 1 ; <[12 x i8]*> [#uses=1]</div><div><br></div><div>define i32 @main() nounwind ssp {</div>
<div>entry:</div><div>  %retval = alloca i32                            ; <i32*> [#uses=2]</div><div>  %0 = alloca i32                                 ; <i32*> [#uses=2]</div><div>  %"alloca point" = bitcast i32 0 to i32          ; <i32> [#uses=0]</div>
<div>  %1 = call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @.str, i64 0, i64 0)) nounwind ; <i32> [#uses=0]</div><div>  store i32 0, i32* %0, align 4</div><div>  %2 = load i32* %0, align 4                      ; <i32> [#uses=1]</div>
<div>  store i32 %2, i32* %retval, align 4</div><div>  br label %return</div><div><br></div><div>return:                                           ; preds = %entry</div><div>  %retval1 = load i32* %retval                    ; <i32> [#uses=1]</div>
<div>  ret i32 %retval1</div><div>}</div><div><br></div><div>declare i32 @puts(i8*)</div></div></div><div><br></div><div>3. I want to write a pass to insert a call foo(int a) after the printf() call, so that it looks like this -</div>
<div><div>#include <stdio.h></div><div>int main() {</div><div>   printf("Hello World");</div><div>   foo(1);</div><div>   return 0;</div><div>}</div></div><div><br></div><div>void foo(int a) {</div><div>   printf("a + 5 is : %d", a + 5);</div>
<div>}</div><div><br></div><div>How can I achieve this? I looked online but couldn't find a definite source which tells me how to do this.</div><div>I think I can use Cloning.h and CloneFunction.cpp to clone a given function and then insert my call to foo().</div>
<div>But I'm not sure if this is the right way to do this. The documentation obviously did not say this explicitly.</div><div>Any thoughts / pointers ?</div><div><br></div><div>Thanks in advance !</div><div>- Teja</div>
</div>