Inlining in LLVM


I am only superficially familiar with LLVM structure.

What I am trying to find out is if functions that have a single invocation are still found in the code during the compilation.

In Open64 and other compilers, there is an early inlining pass that inlines all procedures that have a single invocation site. The reasoning is that if there is a single call to a procedure, then there is not downside to inlining it.

Does LLVM usually does that? Or would that be left to whatever front-end is being used?

Hi Nelson,

What I am trying to find out is if functions that have a single invocation are
still found in the code during the compilation.

I'm pretty sure that static functions with a single invocation are always
inlined. If doing link-time optimization, then pretty much any function
with a single invocation will be inlined.

I'm not sure what this has to do with "early inlining".




As far as I know, there are only 2 inliners available in LLVM (2.7 and 2.8 releases):

InlineSimple: inline callsites according to simple analysis heuristics

InlneAlways: inline callsites whose callee definitions have the attribute((alwayeinline)) attribute on.

For an early inliner, if doesn’t exist, it should be relatively straight forward to write one.


A static function with a single invocation gets a very high weight in the inlining heuristics. But a sufficiently large function still won't get inlined (not sure I've ever seen this in practice), and we don't know how to inline some functions, such as those that use varargs. There are also more complicated cases (I'm not sure we check for this one; it's come up at least twice in practice, but that was before llvm came along, and I think the source was hacked to work around the problem in both cases):
   a() { char x[10000000]; ....}
   b() { a(); .... b(); .... }
It's probably a bad idea to inline a() even if the call shown is the only one; it won't take many recursive calls to b() to blow the stack.

And I guess there's also the case of functions marked "noinline" which
presumably aren't inlined even if static.



There is not one but it is easy to write. LLVM inliner implementation is modular. It is easy to add new inliner based on your home grown heuristics. InlineSimple is standard inliner tuned for the needs of general purpose optimizations. It can be run multiple times. InlineAlways is a very simple instance of special inliner where it only inlines functions that are marked as “always_inline”. It would be very simple to write another special inliner for you needs. All you need to do is identify functions that have single call site. See InlineAlways.cpp for an example of a special inliner.