I want to write some code that, given an LLVM function F, creates an exact copy in the same module (so the copy can be manipulated later while preserving the original). I want to do this with the CloneFunctionInto method.
My current attempt involves trying to insert each (new arg,old arg) pair into the VMap. Previously I've tried inserting an uninitialised VMap and putting the pair the other way round. Impressively, all 3 have resulted in the exact same error message:
Assertion `VMap.count(&I) && "No mapping from source argument specified!"' failed.
//F and S are defined higher up in the code
FunctionType *FType = F->getFunctionType();
Function *new_F = cast<Function>(M->getOrInsertFunction(S,FType));
std::vector<Type*> ArgTypes;
ValueToValueMapTy VMap;
Function::arg_iterator old_args = F->arg_begin();
for (Function::arg_iterator new_args = new_F->arg_begin(), new_args_end = new_F->arg_end();new_args != new_args_end; new_args++) {
std::pair<Value*,Value*> pair(&*new_args,&*old_args);
VMap.insert(pair);
if (VMap.count(&*new_args)>0) {
errs().write_escaped("Mapping added") << '\n';
}
old_args++;
}
SmallVector<ReturnInst*, 8> Returns;
CloneFunctionInto(new_F, F, VMap, false, Returns, "_new", 0, 0);
In use, the 'mapping added' message is printed the correct number of times (i.e. once for each argument), so I'm really unsure where the error is.
You can use
CloneFunction
instead ofCloneFunctionInto
when you just want to clone a function.Also
CloneFunction
shows you how to handle aValueToValueMap
for cloning:From
CloneFunction.cpp
: