Passing arguments of a function to a call to another


I am trying to change a call to one function (which might have multiple arguments) into a call to a different function that takes the same arguments, but I’m having trouble passing the arguments to the new call. I saw the post at and am using CallSite, but I cannot see the implementation of the createCallInst wrapper function, so I’m unsure how to use the arg_iterator to give my function the correct signature.

Currently, once I have the arg_iterators, I put them into a vector of arguments and convert that to an ArrayRef, which I then pass into CreateCall, as shown below (I am working with LLVM version 4.0.1).

if (auto *op = dyn_cast(&I)) {

auto function = op->getCalledFunction();

FunctionType *newFunctionType = FunctionType::get(retType, function->getFunctionType()->params(), false);

Function *newFunction = (Function *)(F.getParent()->getOrInsertFunction(“replacement”, newFunctionType));

std::vector<Value *> arguments;

CallSite CS(&I);

CallSite::arg_iterator ait = CS.arg_begin(), aend = CS.arg_end();

while (ait != aend) {

Value *arg = (Value *)(&(*ait));




ArrayRef<Value *> argArray = ArrayRef<Value *>(arguments);

IRBuilder<> builder(op);

Value *newCall = builder.CreateCall(newFunction, argArray);


This throws an error that says that I am calling a function with a bad signature. I think that the CallSite method of retrieving arguments is correct, as that is what was suggested in the previous post, but is passing the output into CreateCall the right approach, or is there a different way of doing this?

Thank you,