[LLVMdev] IR blocks for calling function pointers

Avinash Chiganmi avinash.ca at gmail.com
Mon Mar 23 19:04:41 PDT 2015


I am trying to create IR block for making a call to function pointer.
For creating the IR for a function call to "foo", with "foo" being defined
as "void foo(int)", I can use the "getOrInsertFunction" call from Module
class as follows:

  std::vector<Type*> FooArgs;
  Value *FooFunction = M.getOrInsertFunction(std::string("foo"),
ArrayRef<Type*>(FooArgs), false));
  IRB.CreateCall(FooFunction, IRB.CreateLoad(LenAlloca));

I want to create a similar call, but to the pointer of foo ("foo_ptr" is
defined as "(void) *foo_ptr (int)"). In C code, I would just call
"foo_ptr(var)", and it would just work. But, replacing the function name
with the name of the function pointer, in the IRBlock does not work:

  std::vector<Type*> FooArgs;
  Value *FooFunctionPtr = M.getOrInsertFunction(std::string("foo_ptr"),
ArrayRef<Type*>(FooArgs), false));
  IRB.CreateCall(FooFunctionPtr, IRB.CreateLoad(LenAlloca));

IRBlocks generated from the above crashes.
I see the LLVM blocks generated for a sample call to function pointer. It
looks as follows:

  %4 = load void (i64)** @foo_ptr, align 8, !dbg !26
  %call = call i32 %4(i32 %3), !dbg !26

What LLVM function calls should be used to generate the above IR blocks?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150323/32ebae3f/attachment.html>

More information about the llvm-dev mailing list