Getting the most out of optimization levels 2 and 3
Here is a recommended approach to using optimization levels 2 and 3:
- If possible, test and debug your code without optimization before using -O2.
- Ensure that your code complies with its language standard.
-
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.
-
In C, use the -qlibansi compiler option unless your program
defines its own functions with the same names as library functions.
- Compile as much of your code as possible with -O2.
- If you encounter problems with -O2, consider
using -qalias=noansi rather
than turning off optimization.
- Next, use -O3 on as much code as possible.
- 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.
- If you encounter unacceptably large code size, try using -qcompact along
with -O3 where necessary.
- If you encounter unacceptably long compile times, consider
disabling the high-order transformations by using -qnohot.
- If you still have problems with -O3, switch
to -O2 for a subset of files, but consider using -qmaxmem=-1, -qnostrict,
or both.