[LLVMdev] Question about insert function or instruction.

Brian R. Gaeke gaeke at uiuc.edu
Wed May 19 15:32:02 PDT 2004


> What I want to do is to insert a call funcation into each basic
> block, which is for statistic some information.

I recommend that you look at the various pieces of code under
llvm/lib/Transforms/Instrumentation, e.g. BlockProfiling.cpp and
TraceBasicBlocks.cpp. They do essentially the same thing as you are
trying to do.

> 1) I implement call function in another c/cpp file and
> can I insert the external call function to existed
> bytecode ? if it is valid. Can you give me a simple
> example to show how to do it or just give me a url
> which can show how to do it? 

Yes; in the case of BlockProfiling, it calls into the library defined
in llvm/runtime/libprofile, which is linked against the resulting LLVM
binary.

> 2) If I'd like to insert the function in the same
> bytecode, how to do it?

In the above example, you would generate a bytecode version of
libprofile using the standard Makefile, then link it with the bytecode
version of your program using gccld or llvm-link.

> 3) for opt commamnd, there have -trace -tracem options
> ,how can I see the detailed direction to use it.

Short answer:

1. Compile the program in question to bytecode using llvmgcc.
Assume the result is 

2. Run the bytecode of the program through opt -tracem, and then
generate C code for the result.
 % opt -tracem bytecode.llvm.bc | llc -f -march=c -o bytecode.tracem.cbe.c

3. Compile the C code with tracing instrumentation using gcc.
 % gcc -o bytecode.tracem.cbe bytecode.tracem.cbe.c
You may have to link it with the libtrace library provided in
llvm/runtime/libtrace, in addition to any other libraries that
the program needs.

>       // here, the problem is how can I get external
> function pointer.

Look at the 'getOrInsertFunction' method of class llvm::Module.

>       //Instruction *callInst = new
> CallInst(pCallfunction, "","");
>       //BB->getInstList().push_back(callInst);

This is basically OK.

>       //BB->getInstList().insert(callInst);

This second insert is not necessary.

-Brian Gaeke

-- 
gaeke at uiuc.edu



More information about the llvm-dev mailing list