Generating New Functions

How can I generate a new function at compile time and insert some instruction
onto it, through llvm pass.
eg.

1) %y = alloca i32, align 4
2) %z = alloca i32, align 4
3) %t = alloca i32, align 4
4) %2 = load i32* %y, align 4
5) %3 = load i32* %z, align 4
6) %add = add nsw i32 %2, %3
7) %4 = load i32* %t, align 4
8) %add1 = add nsw i32 %add, %4
9) store i32 %add1, i32* %x, align 4

whenever I see a store instruction, I want to generate a new function and
insert/copy all instruction (from 1 to 9) on that generated function.

How can I generate a new function at compile time and insert some instruction
onto it, through llvm pass.
eg.

1) %y = alloca i32, align 4
2) %z = alloca i32, align 4
3) %t = alloca i32, align 4
4) %2 = load i32* %y, align 4
5) %3 = load i32* %z, align 4
6) %add = add nsw i32 %2, %3
7) %4 = load i32* %t, align 4
8) %add1 = add nsw i32 %add, %4
9) store i32 %add1, i32* %x, align 4

whenever I see a store instruction, I want to generate a new function and
insert/copy all instruction (from 1 to 9) on that generated function.

You should consult the doxygen documentation on llvm::Function to determine how to create new functions. For most LLVM objects, there is either a new method or a static Create/create method that will create the objects. The doxygen docs contain the list of methods for each class and will usually have the information for which you're looking:

http://llvm.org/doxygen/classllvm_1_1Function.html

As for copying instructions into a new function, look at the functions in lib/Transforms/Utils/CloneFunction.cpp (note that the pathname of the file may have moved; this is its location in LLVM 3.2).

-- John T.

You should consult the doxygen documentation on llvm::Function to
determine how to create new functions. For most LLVM objects, there is
either a new method or a static Create/create method that will create
the objects. The doxygen docs contain the list of methods for each
class and will usually have the information for which you're looking:

http://llvm.org/doxygen/classllvm_1_1Function.html

As for copying instructions into a new function, look at the functions
in lib/Transforms/Utils/CloneFunction.cpp (note that the pathname of the
file may have moved; this is its location in LLVM 3.2).

Thnx for ur reply,
I already visited this links and cpp file, but things are not helping me the
way I wanted to.
Can you be a bit specific on it?