4.20 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
4.19 Inline functions
4.21 Automatic function inlining and static functions
4.22 Inline functions and removal of unused out-of-line functions at link time
4.23 Automatic function inlining and multifile compilation
4.25 Compiler modes and inline functions
4.26 Inline functions in C++ and C90 mode
4.27 Inline functions in C99 mode
4.28 Inline functions and debugging
Related reference
4.24 Restriction on overriding compiler decisions about function inlining
7.11 --autoinline, --no_autoinline
7.63 --forceinline
9.6 __forceinline
9.8 __inline
7.80 --inline, --no_inline
7.111 -Onum
7.115 -Ospace
7.116 -Otime
Non-ConfidentialPDF file icon PDF versionARM DUI0375E
Copyright © 2007, 2008, 2011, 2012, 2014 ARM. All rights reserved.