2

I have seen that GCC compiler on using optimization level 3 inlines a static function but does not inline a non-static function in one case. Whereas in other cases it inlines the functions irrespective of being static or non-static. I would like to know on what parameters does a static or non-static function would be chosen to be inlined.

2
  • 1
    I'm pretty sure that you do not find a complete list outside the source code of GCC. And of course this can vary from version to version, from compiler to compiler. However, the standard includes some limitations. Commented Nov 24, 2016 at 5:36
  • 1
    A static function can be inlined if its address is never returned because the compiler knows all about how it will be used. A non-static function cannot necessarily be inlined because code in some other source file may call it. Or, at least, the non-inline function must be defined so that it can be called from other translation units, even if local uses were inlined. Commented Nov 24, 2016 at 5:51

1 Answer 1

3

From gcc's manual :

-O3 Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-loop-vectorize, -ftree-loop-distribute-patterns, -fsplit-paths -ftree-slp-vectorize, -fvect-cost-model, -ftree-partial-pre and -fipa-cp-clone options.

Seems like your remark comes from the -finline-functions option :

-finline-functions

Consider all functions for inlining, even if they are not declared inline. The compiler heuristically decides which functions are worth integrating in this way.

If all calls to a given function are integrated, and the function is declared "static", then the function is normally not output as assembler code in its own right.

Enabled at level -O3.

In fact all functions are subject to being inlined by gcc in -O3 optimization mode, whether declared inline, static, neither or both.

Here's another chunk of gcc's manpage (-Winline option) :

The compiler uses a variety of heuristics to determine whether or not to inline a function. For example, the compiler takes into account the size of the function being inlined and the amount of inlining that has already been done in the current function.

So gcc uses the size of the function and the amout of inlining done in the function to choose to inline it or not. If you want to know more about these heuristics, i'm afraid you may have to look into gcc's source code :)

Sign up to request clarification or add additional context in comments.

3 Comments

The important heuristic here is for static single-call functions and is by all decent optimizing C compilers. Inlining is effectively a size-vs-speed tradeoff but in case there is effectively nothing to lost as the non-inlined version is unreachable may be dropped. Naturally there are wrinkles and special-cases but that is the general idea.
@doynax The heuristic is not for static only; the difference is that then the function may completely be omitted from the assembly code and thus symbol table.
The take-home message for programmers: Today, there is no need for #defineing "optimized" function-like macros (e.g., max(a,b)) with their multiple-evaluation, precedence and semicolon problems. (They are still useful if you need access to compile-time macros such as __LINE__ or __FILE__ (see assert()) or when symbol concatenation or stringification is needed (e.g. for variable/code generation).

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.