Insert CallInst within a function passing same parameters of the calling function.

Hi,

supposing I have a function “foo” like the following:

int foo(int a, int b) {

...
...
}

I want to insert int the LLVM IR a call instructions to a function “bar” that requires the same parameters of foo.
So my function foo will become:

int foo(int a, int b) {
  bar(a,b);
  …
  ...
}

I am using the following code:

bool ThreadSanitizer::runOnFunction(Function &F) {
    ValueToValueMapTy VMap;
    Function *new_function = CloneFunction(&F, VMap, false);
    new_function->setName(functionName + “_newfunction");
    F.getParent()->getFunctionList().push_back(new_function);

    Function::ArgumentListType::iterator it = F.getArgumentList().begin();
    Function::ArgumentListType::iterator end = F.getArgumentList().end();

    std::vector<Value*> args;
    while (it != end) {
      Argument *Args = &(*it);
      args.push_back(Args);
      it++;
    }
                                                                                                                              
    CallInst::Create(new_function->getFunctionType(), new_function, args, functionName + "__swordomp__", &F.getEntryBlock().front());
}

but I am getting the following error:

void llvm::Value::setNameImpl(const llvm::Twine&): Assertion `!getType()->isVoidTy() && "Cannot assign a name to void values!"' failed.

How should I fix it?

Thanks.
Best,
Simone

Looks like I found the solution.

When I call CallInst::Create, the param NameStr needs to be an empty string for void functions.

I thought that value was the name of the function I want to call, but apparently is not.
Why is that then?

Thanks.
Best,
Simone

Howdy Simone,

If my poor memory still serves me correctly, the NameStr parameter here is the name for the Value class that accepts the return value of the CallInst, not the name for the CallInst itself. So when the function of the CallInst is void, it’s illegal to assign the return value to any names

Correct me if I’m wrong.

Also I do wish LLVM documentations could give more details on its function parameters. It could help newcomers a lot.

Regards,
Kevin