[LLVMdev] whether these transformations are doable and how?

John Criswell criswell at uiuc.edu
Fri Apr 9 06:55:09 PDT 2010

Ning Wang wrote:
> Hi folk,
> I'm a newbie to llvm, please first forgive my naive questions. I want 
> to instrument llvm code to do some run-time monitoring work.  After 
> reading some of the llvm documentation, it begins clear to me that I 
> can do the instrumentation in a transformation pass.   There are 
> several things I want to do in the transformation pass, but I'm not 
> sure whether they are doable and how to do them even after I read the 
> documentation.  I would be very appreciate if anyone can answer my 
> questions or give me hints of how to do them.
> 1.  can I add more global memory objects to a module?  any hint how to 
> do it?  do I need to derive a pass from ModulePass?

Yes.  Inside your pass, create a new GlobalVariable object.  Please read 
the LLVM doxygen files (http://www.llvm.org/doxygen/hierarchy.html) to 
get specific information on using the classes below.  Since this is a 
modification at global scope, you need to use a ModulePass.

> 2.  can I add more stack allocated memory objects to a function?  the 
> answer seems yes, any hint how to do it?

Yes.  Inside your pass, create a new AllocaInst  object.  You can 
probably do this in a FunctionPass.

> 3.  can I modify a function to take extra formal parameters? can I 
> update all calls of the original function to take extra actual 
> paramters?  The function might be called across multiple modules.   It 
> seems this has to be done at both ModulePass and FunctionPass levels. 

Since this is a global change, use a ModulePass.

This change is a bit more difficult.  First, to add parameters, I 
believe you can't change the original function; you have to create a 
new, empty function with the new parameters and then clone the body of 
the old function into the new function.  There's a utility function (I 
think it's in llvm/lib/Transform/Utils) that helps do this.

Updating callers is a bit more difficult.  Direct callers are easy; just 
scan through the def/use chain of the function and find them.  Indirect 
callers are a different matter.  You need to use some sort of analysis 
that tells you the potential call targets of an indirect function call.  
I think LLVM has a conservative analysis pass to do that.

-- John T.

> Thanks,
> Neal

More information about the llvm-dev mailing list