Does GCC inline C++ functions without the 'inline' keyword?

Carl Seleborg picture Carl Seleborg · Oct 26, 2009 · Viewed 13.1k times · Source

Does GCC, when compiling C++ code, ever try to optimize for speed by choosing to inline functions that are not marked with the inline keyword?

Answer

AnT picture AnT · Oct 26, 2009

Yes. Any compiler is free to inline any function whenever it thinks it is a good idea. GCC does that as well.

At -O2 optimization level the inlining is done when the compiler thinks it is worth doing (a heuristic is used) and if it will not increase the size of the code. At -O3 it is done whenever the compiler thinks it is worth doing, regardless of whether it will increase the size of the code. Additionally, at all levels of optimization (enabled optimization that is), static functions that are called only once are inlined.

As noted in the comments below, these -Ox are actually compound settings that envelop multiple more specific settings, including inlining-related ones (like -finline-functions and such), so one can also describe the behavior (and control it) in terms of those more specific settings.