By default, the compiler inlines certain library functions, meaning that it replaces the function call with the actual code for the function at the point where the call was made. These library functions are called intrinsic or built-in functions.
You can also request that the compiler inline the code for your own functions. There
are benefits and drawbacks of, and restrictions on, inlining user code.
There are two ways to inline user code:
You should use inlining only for very small functions. See Q or -qinline for more information about the inlining option.
Note: | Requesting that a function be inlined makes it a candidate for inlining but does not necessarily mean that the function will be inlined. In all cases, the compiler ultimately decides whether a function is inlined. |
Benefits of Inlining
Inlining user code eliminates the overhead of the function call and linkage, and also
exposes the function's code to the optimizer, resulting in faster code performance.
Inlining produces the best results when:
For example, given the following function:
void glen(int a, int b) { if (a == 10) { switch(b) { case 1 : . : case 20: puts("b is 20"); break; case 30: . : default: . : } } }
and assuming your program calls glen several times with constant arguments, for example, glen(10, 20);, each call to glen causes the if and switch expressions to be evaluated. If glen is inlined, the compiler can then optimize the function. The evaluation of the if and switch statements can be done at compile time, and the function code can then be reduced to only the puts statement from case 20.
The best candidates for inlining are small functions that are called often. Use the Performance Analyzer or a profiler to determine which functions to inline to obtain the best results.
Drawbacks of Inlining
Inlining user code usually results in a larger executable module because the code for the
function is included at each call site. Because of the extra optimizations that can be
performed, the difference in size may be less than the size of the function multiplied by
the number of calls.
Inlining can also result in slower program performance, especially if you use auto-inlining. Because auto-inlining looks only at the number of ACUs for a function, the functions that are inlined are not always the best candidates for inlining. As much as possible, use the _Inline or inline keyword to choose the functions to be inlined.
When you use inlining, you need more stack space. When a function is called, its local storage is allocated at the time of the call and freed when it returns to the calling function. If that same function is inlined, its storage is allocated when the function that calls it is entered, and is not freed until that calling function ends. Ensure that you have enough stack space for the local storage of the inlined functions, in order to avoid a stack overflow.
Restrictions on Inlining
The following restrictions apply to inlining:
You cannot inline functions that use a variable number of arguments, unless files are compiled and linked using the -qipa compiler option.
If the definition and reference to a given function reside in different files, all such files must be compiled and linked using the -qipa compiler option. To inline across source files, you must place the function definition (qualified with _Inline) in a header file that is included by all source files where the function is to be inlined.
Turn off inlining if you plan to debug your executable module. Inlining can make debugging difficult. For example, if you set an entry breakpoint for a function call but the function is inlined, the breakpoint will not work.
The Performance Analyzer treats an inlined function as part of the function in which it is inlined.
A function is not inlined during an inline expansion of itself. For a function that is directly recursive, the call to the function from within itself is not inlined. For example, given three functions to be inlined, A, B, and C, where:
the following inlining takes place:
Program Optimization with IBM C and C++ Compilers
Writing Optimized Code... Overview Variables Pointers Functions Function Arguments Expressions Critical Loops Conversions Arithmetic Conversions