[LLVMdev] Passing a function pointer as parameter to function call?

John Criswell jtcriswel at gmail.com
Fri Mar 13 01:52:30 PDT 2015


Dear Kevin,

In the LLVM IR, the function and the pointer to the function are the 
same thing.  Within an LLVM pass, if you have a pointer to a Function 
object:

Function * F = M.getOrInsertFunction(...)

... then you can use F as a parameter to a call instruction:

std::vector<Value *> args;
args.push_back (F);
CallInst * CI = CallInst::Create (AtExit, args, ...);

Regards,

John Criswell

On 3/12/15 11:24 PM, Kevin Hu wrote:
> Hi David,
>
>
> Thanks very much.
>
> I tried and the IR it produces is:
>
> %call = call i32 @atexit(void ()* foo)
>
> The problem is this still doesn't give me any clues how to produce the 
> code inside my LLVM pass. How do I get the pointer to this foo function?
>
> Any hints?
>
>
> Thank you.
> Kevin Hu
>
> On Thu, Mar 12, 2015 at 6:09 PM, David Blaikie <dblaikie at gmail.com 
> <mailto:dblaikie at gmail.com>> wrote:
>
>     Easiest thing to do would be to write the equivalent C code, throw
>     it through Clang, and look at the IR it produces.
>
>     On Thu, Mar 12, 2015 at 3:00 PM, Kevin Hu <hxy9243 at gmail.com
>     <mailto:hxy9243 at gmail.com>> wrote:
>
>         Dear all,
>
>
>         I'm writing an LLVM pass, and I want to insert a call
>         instruction that takes a function pointer as a parameter. The
>         effect would be the same as following:
>
>         atexit(foo);
>
>         Where foo is a function I insert with M.getOrInsertFunction(),
>         which in LLVM is a Function class.
>
>         I searched for a while and did not come up with a satisfying
>         answer. Should I create a Value class of pointer type, or a
>         Int64Ty for a pointer? How do I get the pointer to the
>         function I create? I also tried passing foo as Function* in
>         LLVM as parameter to create CallInst directly, and I doesn't
>         seem to work.
>
>         Any hints and enlightenment is appreciated. Many thanks.
>
>
>         Regards,
>         Yours,
>         Kevin Hu
>
>         _______________________________________________
>         LLVM Developers mailing list
>         LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu>
>         http://llvm.cs.uiuc.edu
>         http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
>
>
>
> -- 
> Yours,
> X. Hu
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


-- 
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
http://www.cs.rochester.edu/u/criswell

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


More information about the llvm-dev mailing list