[LLVMdev] instrument a byte code with llvm

John Criswell criswell at illinois.edu
Tue Apr 5 09:42:56 PDT 2011

On 4/5/11 11:36 AM, Nabila ABDESSAIED wrote:
> 2011/4/5 John Criswell <criswell at illinois.edu 
> <mailto:criswell at illinois.edu>>
>     On 4/5/11 11:22 AM, Nabila ABDESSAIED wrote:
>>     hi,
>>     I'm newer in llvm
>>     i would like to instrument a byte code with a pass and as a
>>     result i would like to get an instrumented byte code.
>     To rephrase, you want to instrument a program so that, when you
>     run it, it computes the number of instructions executed at
>     run-time.  Is this correct?
>     By number of instructions, do you mean LLVM instructions or native
>     code instructions?
> native code instruction a .bc code

Okay.  That's going to make things more difficult but I would think is 

So, the instrumentation at the LLVM IR is easy: you simply add a 
CallInst (i.e., a call instruction) at the end of each basic block 
(technically, before the BasicBlock's terminator instruction) that calls 
an external function.  This external function will be implemented in a 
run-time library that you link with the program after it is compiled 
with LLVM.

The tricky part is to get the count of native code instructions.  For 
that, I think you'll need to write a MachineFunctionPass 
(http://llvm.org/doxygen/classllvm_1_1MachineFunctionPass.html).  This 
sort of pass is part of the code generator and works on the IR 
representing the generated code.  You will need to have it count the 
instructions and then modify the call instruction so that it passes the 
count of machine instructions to the run-time library.

I haven't written a MachineFunctionPass before, so others who have will 
need to answer questions on it.  However, the doxygen docs and the 
existing MachienFunctionPass passes in LLVM may be a good way to learn 
how they work.

Good luck.

-- John T.

>>     i would like that a pass add a method (which calculate the number
>>     of instructions) in the end of each block.
>>     the instrumented code should contain in each block a method that
>>     calculate the number of instructions
>     Have you read the documentation from the web site:
>     How to Write an LLVM Pass
> yes, i have seen them
> actually i had written a pass that anlyse a .bc code and gives as a 
> result the number of instructions and others parameters like the 
> instcount pass
> but what i would like to do it's not to analyse but to instrument (add 
> methods into th bc code that calculate the nuber of instructions)
>     LLVM Programmer's Manual
>     LLVM Language Reference Manual (you just need to skim it to get a
>     basic understanding of what the IR looks like)
>     -- John T.
>>     Please Help me
>>     thank you
>>     -- 
>>     Nabila ABDESSAIED
>>     Tel     : (+216) 22 473 385 / (+216) 55 744 625
>>     Email : nabila.abdessaied at gmail.com
>>     <mailto:nabila.abdessaied at gmail.com>
> -- 
> Tel     : (+216) 22 473 385 / (+216) 55 744 625
> Email : nabila.abdessaied at gmail.com <mailto:nabila.abdessaied at gmail.com>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110405/8d95181a/attachment.html>

More information about the llvm-dev mailing list