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:

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

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.

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 never inlined into itself, even if __forceinline is used.