harishankarv
harishankarv

Reputation: 338

LLVM pass to reliably inline ALL function calls using InlineFunction

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.

Upvotes: 1

Views: 592

Answers (1)

Shraiysh
Shraiysh

Reputation: 177

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.

Upvotes: 0

Related Questions