LLVM pass to reliably inline ALL function calls using InlineFunction

Viewed 66

I have an input bitcode file cannot be modified. Say I am looking at a particular function foo. I want to inline all the calls that foo() makes: bar() -> baz() and fez() and several more. That is, I want the resulting output to have no call instructions in foo(). How do I write an LLVM pass that reliably does this function inlining?

I looked at the solution described here. I wrote a ModulePass which first collects all CallInsts in the module and then calls InlineFunction() on them.

/* callInstVector contains all CallInsts in foo() */

for (CallInst *ci : callInstVector) {
  CallBase *cb = dyn_cast<CallBase>(ci);
  if (cb->getParent() && cb->getFunction()){
    InlineFunctionInfo ifi;
    auto res = llvm::InlineFunction(*cb, ifi);
    outs() << res.isSuccess() << "\n";
  }
}

This pattern seems to work, assuming all the called functions have a definition in the module.

  • Is it guaranteed that such an approach could effectively inline all function calls in foo()?
  • What does the success/failure of InlineFunction()'s result depend upon?
  • Is this the correct way to go about this or are any alternatives?
1 Answers

An alternative and more reliable way would be to add the alwaysinline attribute to the function, and then rely on the LLVM Inliner to handle the complexities of inlining for you.

Related