Choosing Which Functions to Inline

The compiler offers several methods to specify which functions are eligible for inlining.

To specify that a function is eligible to be inlined, precede its definition with the inline , __inline__ , or __inline keyword. To allow these keywords in C source code, turn off ANSI Keywords Only in the CodeWarrior IDE's Properties > C/C++ Build > Settings > Tool Settings > PowerPC Compiler > C/C++ Language panel or turn off the only_std_keywords pragma in your source code.

To verify that an eligible function has been inlined or not, use the Non-Inlined Functions option in the IDE's Warnings panel or the warn_notinlined pragma. Specifying to the compiler that a function may be inlined shows an example.

Figure 1. Specifying to the compiler that a function may be inlined
#pragma only_std_keywords off
inline int attempt_to_inline(void)
{
    return 10;
}

To specify that a function must never be inlined, follow its definition's specifier with __attribute__((never_inline)) . Specifying to the compiler that a function must never be inlined shows an example.

Figure 2. Specifying to the compiler that a function must never be inlined
int never_inline(void) __attribute__((never_inline))
{
   return 20;
}

To specify that no functions in a file may be inlined, including those that are defined with the inline , __inline__ , or __inline keywords, use the dont_inline pragma. Specifying that no functions may be inlined shows an example.

Figure 3. Specifying that no functions may be inlined
#pragma dont_inline on

/* Will not be inlined. */
inline int attempt_to_inline(void)
{
    return 10;
}

/* Will not be inlined. */
int never_inline(void) __attribute__((never_inline))
{
   return 20;
}

#pragma dont_inline off
/* Will be inlined, if possible. */
inline int also_attempt_to_inline(void)
{
    return 10;
}

Some kinds of functions are never inlined:

The compiler will inline functions that need destruction, without any dependency on the ISO C++ templates, if the class has a trivial empty constructor. Inlining function with an empty destructor shows an example.

Figure 4. Inlining function with an empty destructor
struct X {
    int n;

    X(int a) { n = a; }

    ~X() {}

         };



inline X f(X x) { return X(x.n + 1); }



int main()

{

    return f(X(1)).n;

}