Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get function pointer in LLVM

I need to insert IR instruction to call pthread_create in my LoopPass, so I need to pass the actual function as an argument that pthread_create is supposed to call on the new thread.

Currently I have defined the function to run on the new thread as

Function *worker_func = Function::Create(funcType,
                                      GlobalValue::ExternalLinkage,
                                      "worker_func",
                                      CurrentModule);

And I got the pointer to pthread_create by the following:

Function *func_pthread_create = dyn_cast<Function>(
    TheModule->getOrInsertFunction("pthread_create", pthreadCreateTy));

And I need to pass an array of Type* as the arguments to pthread_create like below.

Value* pthread_create_call = builder.CreateCall(
    func_pthread_create, args, "pthread_create");

with the args as:

Value* args[4];
args[0] = pthread_t_ld
args[1] = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(getGlobalContext())->getPointerTo());

args[2] = ??? // supposed to be the pointer to worker_func`

args[3] = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(getGlobalContext())->getPointerTo());

So how can I get the pointer to this worker_func function to pass into pthread_create?

like image 392
user2958862 Avatar asked Nov 12 '13 00:11

user2958862


1 Answers

You need to bitcast func_pthread_create to the type the function expects and pass the result of that bitcast as the 3rd argument. You can use the static ConstantExpr::getBitCast method for that, passing it the function as the first argument and the type of the third parameter of func_pthread_create as the second argument.

like image 172
Oak Avatar answered Sep 29 '22 05:09

Oak