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?

- 13,125
- 11
- 58
- 70
-
12According to spec, the `inline` keyword in C++ has nothing to do with the compiler optimization with the same name. The keyword simply means that the linker should expect to see multiple definitions of the function. That just so happens to make the inline *optimization* easier (because the full definition can be made visible in multiple translation units), but that's as close as they get. The compiler can inline functions that aren't marked as `inline`, and functions marked as `inline` are not necessarily inlined by the compiler. – jalf Oct 26 '09 at 21:43
-
4Note that methods defined inside the class definition (i.e. between `{ ... };` are `inline` by default, even without the keyword. – MSalters Oct 27 '09 at 09:43
4 Answers
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.

- 312,472
- 42
- 525
- 765
-
1GCC says in regards to `-finline-functions` "Consider all functions for inlining, even if they are not declared inline. " I infer from that that functions declared with `inline` will be considered for inlining even if they make the code size larger? Many claim that the hint from `inline` to the compiler is obsolete. But this would mean with `-O2` the hint would not be obsolete since it would only inline functions that make the code larger if they have the `inline` keyword. However wtih `-O3` it would be obsolete (I mean the hint...the `inline` keyword has other purposes). – Z boson Dec 28 '15 at 16:29
-
1I am having trouble create a function which is to large to be inlined with `-O2`. Every function I create which is inlined with -O3 is also inlined with -O2. GCC in regards to `-finline-small-function` says "Integrate functions into their callers when their body is smaller than expected function call code (so overall size of program gets smaller)". However, when I look at the assembly even with -O2 it clearly makes the code larger. Do you have an example where O3 inlines but O2 does not? – Z boson Dec 28 '15 at 16:55
-
@AnT how to prevent the compiler from inlining a function which are called only once. – bharath Jan 19 '18 at 10:36
Yes, especially if you have a high level of optimizations enabled.
There is a flag you can provide to the compiler to disable this: -fno-inline-functions.

- 12,245
- 9
- 42
- 49
-
2Actually, -fno-inline-functions suppresses automatic inlining, and -fno-inline suppresses all inlining (source : http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Switches-for-gcc.html) – KeatsPeeks Oct 26 '09 at 17:58
If you use '-finline-functions' or '-O3' it will inline functions. You can also use '-finline_limit=N' to tune how much inlining it does.

- 2,920
- 15
- 10
Yes, it does, although it will also generate a non-inlined function body for non-static
non-inline
functions as this is needed for calls from other translation units.
For inline
functions, it is an error to fail to provide a function body if the function is used in any particular translation unit so this isn't a problem.

- 755,051
- 104
- 632
- 656