[llvm-dev] creating a callinst to an external function

marwayusuf@feng.bu.edu.eg via llvm-dev llvm-dev at lists.llvm.org
Wed Aug 19 15:42:43 PDT 2015

Dear All

I'm making an instrumentation pass. The pass is supposed to modify the given IR in a specefic way. One of the required modifications is to insert a call to a function at a specific location.

This is the signature of the called function:

      void myclass::foo(Function *f, BasicBlock* b)

This function's prototype is in an foofile.h file in include/llvm

And the function definition is in foofile.cpp file in the MCJIT folder.

and running "make" at this folder works fine and the foofile.cpp is compiled with MCJIT.cpp and another function in the same file works just fine as expected.

Not back to the instrumentation pass. How can I insert a callinst to the foo function in the given IR?

Here is the snippet that inserts the call:

           Type* retTy = Type::getInt32Ty(C);

            FunctionType* FuncTy = FunctionType::get(retTy, false);

            PointerType* PtrToFuncTy = PointerType::get(FuncTy, 0);

            Constant *fun = M->getOrInsertFunction("foo", Type::getVoidTy(C), PtrToFuncTy, Type::getLabelTy(C), nullptr);

            Function *dofoo = cast<Function>(fun);

            Instruction* dofooCall = CallInst::Create(fun, Args2, "", bb);

Note: Args2 is an arraylist containing 2 value pointers to a function and a basicblock, bb is the basicblock to insert the call in.

When I run the pass using op on a given IR, it produces a declaration and a call correctly like this:

 declaration:       declare void @foo(i32 ()*, label)

 call:                   call void @foo(i32 ()* @main, label %for.cond)

But when I try to run the resulting .ll file using lli, everything explodes! This the first 2 lines before the stacktrace:

    Can't get register for value!

    UNREACHABLE executed at /home/marwayusuf/llvm-env/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:1158!

I concluded that the problem is it can't find the foo function. If this is the problem, how can I create the callinst correctly?

Marwa Yusuf
Teaching Assistant - Computer Engineering Department
Faculty of Engineering - Benha University
E-JUST PhD Student
Computer Science & Engineering Dept.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150819/39bd273f/attachment.html>

More information about the llvm-dev mailing list