David Given
David Given

Reputation: 13701

LLVM: Setting function attributes on a pointer

In some code I'm generating via the LLVM C++ API, at one point I'm given a raw address to a function. I turn this into a function pointer and call it as follows:

llvm::FunctionType* ft = llvm::FunctionType::get(...);
llvm::Constant* iptr = llvm::ConstantInt::get(
    engine->getDataLayout()->getIntPtrType(state.context, 0), (uint64_t) pointer);
llvm::Value* fptr = llvm::ConstantExpr::getIntToPtr(iptr,
    llvm::PointerType::get(ft, 0));

llvm::Value* retval = state.builder.CreateCall(fptr, params);

This works fine --- but I want to be able to set some function attributes to aid in optimisation: specifically, readnone.

Unfortunately the only API I've found to do this is on llvm::Function, and I don't have one. I'd expect the attributes to be a property of the function type because that's how it works in C, but llvm::FunctionType doesn't seem to have an attributes API on it.

Any suggestions on how to do this?

Upvotes: 2

Views: 365

Answers (1)

Nick Lewycky
Nick Lewycky

Reputation: 33

You may annotate the CallInst with any function attribute through its CallInst::addAttribute API (and similar for InvokeInst). This means that you will need to have distinct call sites if you plan to call through one pointer that is readnone and one that is not.

Upvotes: 3

Related Questions