Using optimization levels

By default, the compiler performs only quick local optimizations such as constant folding and elimination of local common sub-expressions, while still allowing full debugging support. You can optimize your program by specifying various optimization levels, which provide increasing application performance, at the expense of larger program size and debugging support. The options you can specify are summarized in the following table, and more detailed descriptions of the techniques used at each optimization level are provided below.

Table 8. Optimization levels

Option Behavior
-O or -O2 or -qoptimize or -qoptimize=2 Comprehensive low-level optimization; partial debugging support.
-O3 or -qoptimize=3 More extensive optimization; some precision trade-offs.
-O4 or -qoptimize=4 Interprocedural optimization; loop optimization; automatic machine tuning.
-O5 or -qoptimize=5

Techniques used in optimization level 2

At optimization level 2, the compiler is conservative in the optimization techniques it applies and should not affect program correctness. At optimization level 2, the following techniques are used:

Techniques used in optimization level 3

At optimization levels 3 and above, the compiler is more aggressive, making changes to program semantics that will improve performance even if there is some risk that these changes will produce different results. Here are some examples:

Getting the most out of optimization levels 2 and 3 provides some suggestions for mitigating this risk.

At optimization level 3, all of the techniques in optimization level 2 are used, plus the following:

Techniques used in optimization levels 4 and 5

At optimization levels 4 and 5, all of the techniques in optimization levels 2 and 3 are used, plus the following:

Getting the most out of optimization levels 2 and 3

Here is a recommended approach to using optimization levels 2 and 3:

  1. If possible, test and debug your code without optimization before using -O2.
  2. Ensure that your code complies with its language standard.
  3. In C code, ensure that the use of pointers follows the type restrictions: generic pointers should be char* or void*. Also check that all shared variables and pointers to shared variables are marked volatile.
  4. In C, use the -qlibansi compiler option unless your program defines its own functions with the same names as library functions.
  5. Compile as much of your code as possible with -O2.
  6. If you encounter problems with -O2, consider using -qalias=noansi rather than turning off optimization.
  7. Next, use -O3 on as much code as possible.
  8. If you encounter problems or performance degradations, consider using -qstrict or -qcompact along with -O3 where necessary.
  9. If you still have problems with -O3, switch to -O2 for a subset of files, but consider using -qmaxmem=-1, -qnostrict, or both.
IBM Copyright 2003