create dummy function

Hello,
I have a simple question. How to create "dummy" function which will
have no functionality behind (return nothing and do nothing)?
Currently I'm trying to do this:

llvm::Constant* c = Module.getOrInsertFunction("dummy",
FunctionThatNeedsToBeReplaced.getFunctionType());
llvm::Function* dummy = llvm::cast<llvm::Function>(c);

This way I create new function that do nothing.Then next step is:

FunctionThatNeedsToBeReplaced.replaceAllUsesWith(dummy);

This code is doing what I want to do, but it ends up with exception:
ERROR: Program used external function 'dummy' which could not be resolved!

I think I'm doing something wrong or there is a different way to
create such functions.

Thank you in advance,
Oleg.

Hi Oleg,

Every function needs a BasicBlock, even if the basic block just
contains a return.

The only reason to create a function without a basic block is when the
function is extern'd and imported via JIT, but I guess it's not what
you're attempting.

Try this presentation and let me know if it's helpful:

http://www.systemcall.org/rengolin/stuff/compiler/download/LLVM-pet-project.pdf

cheers,
--renato

Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm

Thank you very much for you help, Renato!

I read through paper you referred and also this document -
http://llvm.org/docs/tutorial/JITTutorial1.html

Following these instructions to create successful function I run into
some problems:
1) llvm::getGlobalContext() does not exists anymore? "llvm/LLVMContext.h" too?
2) creating instance of IRBuilder don't require template (from
tutorial paper - IRBuilder<> builder(block))

So, I believe this is just outdated information? I think it just need
some quick attention with getting materials up to date.

In the end I got following code, that satisfy my needs:

      llvm::Constant* c = M.getOrInsertFunction(dummyFunctionName,
F.getFunctionType());
      llvm::Function* dummy = llvm::cast<llvm::Function>(c);
      dummy->setLinkage(llvm::Function::ExternalLinkage);
      dummy->setCallingConv(llvm::CallingConv::C);
      llvm::BasicBlock* block = llvm::BasicBlock::Create("entry", dummy);
// no context needed
      llvm::IRBuilder builder(block); // no need in <>
      builder.CreateRetVoid();

Cheers,
Oleg.

So, I believe this is just outdated information? I think it just need
some quick attention with getting materials up to date.

Yup, outdated information. If you see the same example on the svn tree
(examples/Kaleidoscope) the code is right and up-to-date.

In the end I got following code, that satisfy my needs:

Return void an a dummy basic block, all you needed. :wink:

Glad to help.

cheers,
--renato

Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm

What exactly is M in that code you posted?

Oleg Knut wrote:

M is llvm::Module instance.

Cheers,
Oleg.