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 your application is sensitive to floating-point exceptions or the order of evaluation for floating-point arithmetic, use -qstrict along with -O3 to ensure accurate results, while still gaining most of the performance benefits of -O3.
  9. If you encounter unacceptably large code size, try using -qcompact along with -O3 where necessary.
  10. If you encounter unacceptably long compile times, consider disabling the high-order transformations by using -qnohot.
  11. If you still have problems with -O3, switch to -O2 for a subset of files, but consider using -qmaxmem=-1, -qnostrict, or both.