[llvm-dev] clone function

Pierre Gagelin via llvm-dev llvm-dev at lists.llvm.org
Fri Jul 15 12:42:10 PDT 2016

Hi again,

I finally managed to find the solution looking at CloneFunctionInto

> Not sure what you mean by "you can't clone the function if there is
> arguments”, CloneFunction is supposed to handle this.
I meant that you can't directly splice the function body from a function
with arguments (at least if those argument are used in the function body).
Because that's what I tried first as an "hand-made" solution, then I had to
do the arguments mapping I thought this should probably exist somewhere in

> The instructions using these arguments will have Values refering to
> another function body which triggers errors like:
> Referring to an argument in another function!
>   store i8* %ptr, i8** %ptr.addr, align 8
> for a function taking i8* %ptr as argument.
> If you end up with this by *only* calling CloneFunction, that’s a bug. It
> is supposed to handle this perfectly.

Nope, that happen when arguments weren't remapped. I didn't managed to use
CloneFunction because I didn't get how to build the ValueToValueMapTy
argument (given by "typedef ValueMap< const Value*, WeakVH
> llvm::ValueToValueMapTy").

At last when I looked to the implementation they used it as a
std::map<Value*, Value*>... I'm still confused on that point. Well at least
it does make sense to remap the argument with a map like that. And that was
my initial question by the way.

> But I think CloneFunctionInto allows you to do this kind of argument
> expansion
> Yes, CloneFunction is setting up the call for CloneFunctionInto, if you
> look at the implementation you should be able to copy it, and with some
> adaptation call CallFunctionInto.
> (because that's the only thing after all: adding an argument to the
> function).
> Note: If you just want to *add* an argument and don’t need to preserve the
> old function, cloning it is an expensive solution…
Well I think I don't need to keep the original function around, but how
should I do so ? This doesn't look like trivial because every CallInst to
the original function has to be modified too to match the new signature. Or
doing it with optional arguments ? Never tried those.

But anyway I guess the answer to my original question was just "you can put
some std::map<Value*, Value*> for the VMap argument" ^^.

Thanks for your time, I hope it's the last time I need to ask for help!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160715/e6035a5b/attachment.html>

More information about the llvm-dev mailing list