8

I am writing an LLVM pass, where I clone some functions by calling llvm::CloneFunction. Now I also want to insert those functions in the module. How can I do that?

1
  • I am trying to find an LLVM API function that does that, but still am unable to find one. Commented Feb 4, 2013 at 13:06

3 Answers 3

9
  • Create a new function with Function::Create or by other means. A Function's constructors accept a module into which to insert the new function.
  • Clone a function into that new function with CloneFunctionInto, or just copy over the BBs you need.
Sign up to request clarification or add additional context in comments.

Comments

0

CloneFunction will automatically insert the new Function into the old Function's module. From the doxygen:

Return a copy of the specified function and add it to that function's module.

Comments

-1

You can use CloneFunction and insert it into the module afterwards like so

  Function* duplicateFunction = CloneFunction(F, VMap,
                                              /*ModuleLevelChanges=*/false);
  F->getParent()->getFunctionList().push_back(duplicateFunction);

Example stolen from PartialInlining.cpp in the llvm source.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.