5.21 Compiler decisions on function inlining

When function inlining is enabled, the compiler uses a complex decision tree to decide if a function is to be inlined.

The following simplified algorithm is used:

  1. If the function is qualified with __forceinline, the function is inlined if it is possible to do so.

  2. If the function is qualified with __inline and the option --forceinline is selected, the function is inlined if it is possible to do so.

    If the function is qualified with __inline and the option --forceinline is not selected, the function is inlined if it is practical to do so.

  3. If the optimization level is -O2 or higher, or --autoinline is specified, the compiler automatically inlines functions if it is practical to do so, even if you do not explicitly give a hint that function inlining is wanted.

When deciding if it is practical to inline a function, the compiler takes into account several other criteria, such as:

  • The size of the function, and how many times it is called.

  • The current optimization level.

  • Whether it is optimizing for speed (-Otime) or size (-Ospace).

  • Whether the function has external or static linkage.

  • How many parameters the function has.

  • Whether the return value of the function is used.

Ultimately, the compiler can decide not to inline a function, even if the function is qualified with __forceinline. As a general rule:

  • Smaller functions stand a better chance of being inlined.

  • Compiling with -Otime increases the likelihood that a function is inlined.

  • Large functions are not normally inlined because this can adversely affect code density and performance.

A recursive function is inlined into itself only once, even if __forceinline is used.

Related concepts
5.20 Inline functions
5.22 Automatic function inlining and static functions
5.23 Inline functions and removal of unused out-of-line functions at link time
5.24 Automatic function inlining and multifile compilation
5.26 Compiler modes and inline functions
5.27 Inline functions in C++ and C90 mode
5.28 Inline functions in C99 mode
5.29 Inline functions and debugging
Related reference
5.25 Restriction on overriding compiler decisions about function inlining
8.15 --autoinline, --no_autoinline
8.83 --forceinline
10.6 __forceinline
10.8 __inline
8.106 --inline, --no_inline
8.138 -Onum
8.141 -Ospace
8.142 -Otime
Non-ConfidentialPDF file icon PDF versionARM DUI0472J
Copyright © 2010-2013 ARM. All rights reserved.