Chapter 6. Inlining #pragma Directives

Table 6-1 lists the #pragma directives discussed in this chapter, along with a brief description of each and the compiler versions in which the directive is supported.

Table 6-1. Inlining #pragma Directives

#pragmas

Short Description

Compiler Versions

#pragma inline (see “#pragma inline and #pragma noinline”)

Tells the compiler to inline the named functions.

Keywords:

- here (next statement only)

- routine (rest of routine or until corresponding noinline or inline is found)

- global (entire file, or until corresponding noinline or inline is found)

7.1 and later

#pragma noinline (see “#pragma inline and #pragma noinline”)

Tells the compiler not to inline the named functions.

Keywords:

- here (next statement only)

- routine (rest of routine or until corresponding noinline or inline is found)

- global (entire file, or until corresponding noinline or inline is found)

7.1 and later


#pragma inline and #pragma noinline

The #pragma inline and #pragma noinline directives instruct the compiler whether or not to inline the named functions. These directives can have next-line, entire routine, or global scope.

The syntax of the #pragma inline and #pragma noinline directives is as follows:

#pragma [no] inline {here|routine|global} [name1[,name2 ...]]

here, routine, and global are keywords (see “Keywords”).

The optional name1 and name2 are function names. If they are present, they follow these rules:

  • If any functions are named in the directive, it applies only to them.

  • If no function names are given, the pragma applies to all functions.

  • If a specified function does not exist, a warning message is issued, and the pragma is ignored.

If the list of function names is empty, the parentheses around the function names are not required.

Keywords

The following list describes the here, routine, and global keywords. These keywords must appear in lowercase, because function names are case sensitive.

here

The directive applies only to the next statement.

routine

The directive applies to the rest of the routine, or until a corresponding #pragma noinline appears. (Or, if the first directive was a #pragma noinline, until the corresponding #pragma inline.)

global

The directive applies to the entire file, or until toggled with a #pragma noinline directive. (Or, if the first directive was a #pragma noinline, until the corresponding #pragma inline directive.) Typically, #pragma global directives appear only at the top of the source file.

no keyword

The #pragma inline and #pragma noinline directives with no keyword have the same effect as using the here keyword, unless the directives appear at the top of the file, before any lines of source code. In that case, the #pragma directives apply to the entire file, as if the global keyword had been used.


Caution: For C++ code, #pragma inline and #pragma noinline take C++ style function names. If you use mangled names, the results are undefined. The compiler gives a warning if it cannot find the supplied name.


Examples of #pragma inline and #pragma noinline

The following examples illustrate different aspects of the #pragma inline and #pragma noinline directives.

Example 6-1. Using the here keyword with the #pragma noinline directive

This example illustrates the use of the #pragma noinline directive with the here keyword. All occurrences of f1(int) are marked for inlining, except the one directly following #pragma noinline here.

int ig = 0;
double dg = 0.;

inline void f1(int) {ig++;}
void f1(double){dg++;}

void main ()
{
  int i;
  double d;
  f1(i);                 // f1(int) is marked for inlining
  f1(d);

  #pragma noinline here (void f1(int))
  f1(i);                 // f1(int) is not marked for inlining
  f1(d);
  f1(i);                 // f1(int) is marked for inlining

  printf(“Result is %d\n”, ig + (int) dg);
}


Example 6-2. Using the here keyword with the #pragma inline and #pragma noinline directives

This example illustrates the use of the #pragma inline and #pragma noinline directives with the here keyword. All occurrences of f1(int) are marked for inlining, except the one directly following #pragma noinline here . The only occurrence of f1(double) that is marked for inlining is the one directly following #pragma inline here.

int ig = 0;
double dg = 0.;

inline void f1(int) {ig++;}
void f1(double){dg++;}

void main ()
{
  int i;
  double d;

  f1(i);        // f1(int) is marked for inlining
  f1(d);        // f1(double) is not marked for inlining

  #pragma noinline here (void f1(int))
  f1(i);        // f1(int) is not marked for inlining

  #pragma inline here (void f1(double))
  f1(d);        // f1(double) is marked for inlining
  f1(i);        // f1(int) is marked for inlining

  printf(“Result is %d\n”, ig + (int) dg);
}


Example 6-3. Using the global keyword with the #pragma inline directive

This example illustrates the use of the #pragma inline directive with the global keyword. All occurrences of f1(int) following the #pragma inline global are marked for inlining, except the one following the #pragma noinline here.

int ig = 0;
double dg = 0.;

void f1(int) {ig++;}
void f1(double){dg++;}

void main ()
{
  #pragma inline global (void f1(int));
  int i;
  double d;
  f1(i);                 // f1(int) is marked for inlining
  f1(d);                 // f1(double) is not marked for inlining

  #pragma noinline here (void f1(int))
  f1(i);                  // f1(int) is not marked for inlining

  #pragma inline here (void f1(double))
  f1(d);                  // f1(double) is marked for inlining
  f1(i);                  // f1(int) is marked for inlining

  printf(“Result is %d\n”, ig + (int) dg);
}


Example 6-4. Using the routine keyword with the #pragma inline directive

This example illustrates the use of the #pragma inline directive with the routine keyword. All occurrences of f1(int) following #pragma inline routine are marked for inlining, except the one following #pragma noinline here .

int ig = 0;
double dg = 0.;

void f1(int) {ig++;}
void f1(double){dg++;}

void main ()
{
  #pragma inline routine (void f1(int))
  int i;
  double d;
  f1(i);                 // f1(int) is marked for inlining
  f1(d);                 // f1(double) is not marked for inlining

  #pragma noinline here (void f1(int))
  f1(i);                 // f1(int) is not marked for inlining

  #pragma inline here (void f1(double))
  f1(d);                 // f1(double) is marked for inlining
  f1(i);                 // f1(int) is marked for inlining

  printf(“Result is %d\n”, ig + (int) dg);
}


Example 6-5. Using the routine keyword with the #pragma noinline directive

This example illustrates the use of the #pragma noinline directive with the routine keyword. None of the occurrences of f1(int) following #pragma noinline routine are marked for inlining, except the one following #pragma inline here .

int ig = 0;
double dg = 0.;

inline void f1(int) {ig++;}
void f1(double){dg++;}

void main ()
{
  int i;
  double d;

  #pragma noinline routine (void f1(int))
  f1(i);           // f1(int) is not marked for inlining
  f1(d);           // f1(double) is not marked for inlining

  #pragma inline here (void f1(int))
  f1(i);           // f1(int) is marked for inlining

  #pragma noinline here (void f1(double))
  f1(d);           // f1(double) is not marked for inlining
  f1(i);           // f1(int) is not marked for inlining

  printf(“Result is %d\n”, ig + (int) dg);
}