Tips to Encourage IPO

int *x;
foo() 
{
   int y;
   bar(&y);
}
bar (p) 
{
   x = p
}

Code that discourages IPO

  • Use static variables and static functions, and avoid assigning function addresses or variable addresses to globals. Unless the compiler can detect the whole program, it has no knowledge about the complete use of global variables, external functions, or static variables and static functions whose addresses are taken and assigned to a global variable or function pointer.

  • If IPO doesn’t inline automatically, use the inline keyword in C++, and __inline in C.

  • Avoid passing pointers into a function as a parameter and then assigning them to a global variable. The code on the right discourages IPO. x is a global variable and p is a pointer.

  • Try to provide the whole program to the compiler when using IPO. Most of the IPO optimizations are either not triggered or severely weakened unless the compiler sees the whole program. Although inlining is an exception to this rule, the compiler can inline better in the presence of the whole program.

  • Even in the case of inlining, you should refrain from building libraries out of frequently called small functions when using IPO.