[llvm-dev] function call replacement

John Criswell via llvm-dev llvm-dev at lists.llvm.org
Mon Jun 20 07:45:10 PDT 2016

On 6/20/16 6:17 AM, Pierre Gagelin via llvm-dev wrote:
> Hi everyone,
> I am trying to replace the call of a certain function with a call to 
> another function. It would for example replace the following:
> %call = tail call noalias i8* @func(i64 10)
> by
> %call = tail call noalias i8* @other_func(i64 10)
> I managed to declare other_func correctly but I am having troubles to 
> understand how I should proceed to do the replacement.
> I tried to use ReplaceInstWithInst function as follows:
> CallInst *call_to_other_func_inst = 
> IRBuilder.CreateCall(ptr_to_other_func, args);
> ReplaceInstWithInst(call_to_func_inst, newI);
> LLVM builds correctly but the instrumentation crashes at optimization 
> time. I know this isn't the correct way to do it because IRBuilder 
> generates IR and I just want to have an instance of a CallInst. But I 
> don't see how it is supposed to be done?

Do you have assertions enabled?  If so, is the crash an assertion 
failure and, if so, which assertion is failing?  It's nearly impossible 
to tell what the problem is just by knowing that it is crashing.

> There are methods to create CallInst in the Instruction.h file but 
> those needs to give an inserting point. Shoud I insert the call to 
> other_func before the one to func and just remove the call instruction 
> to func?

Yes.  You need to place the new call instruction within the same basic 
block as the old call instruction.  Placing it right before the old call 
instruction is a good place.

The code that Ashutosh provided is what I'd use to do what you're doing.


John Criswell

> Thanks for your help,
> Pierre
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

John Criswell
Assistant Professor
Department of Computer Science, University of Rochester

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160620/4145fdaa/attachment.html>

More information about the llvm-dev mailing list