The C/C++ compiler is able to perform various optimizations. High-level optimizations are performed in the optimizer and low-level, target-specific optimizations occur in the code generator. Use high-level optimization levels, such as --opt_level=2 and --opt_level=3, to achieve optimal code.
The easiest way to invoke optimization is to use the compiler program, specifying the --opt_level=n option on the compiler command line. You can use -On to alias the --opt_level option. The n denotes the level of optimization (0, 1, 2, 3), which controls the type and degree of optimization.
Performs all --opt_level=0 (-O0) optimizations, plus:
Performs all --opt_level=1 (-O1) optimizations, plus:
Performs all --opt_level=2 (-O2) optimizations, plus:
If you use --opt_level=3 (-O3), see Section 4.3 and Section 4.4 for more information.
For information about how the --opt_level option along with the --opt_for_speed option and various pragmas affect inlining, see Section 3.11.
By default, debugging is enabled and the default optimization level is unaffected by the generation of debug information. However, the optimization level used is affected by whether or not the command line includes the -g (--symdebug:dwarf) option and the --opt_level option as shown in the following table:
Optimization | no -g | -g |
---|---|---|
no --opt_level | --opt_level=off | --opt_level=off |
--opt_level | --opt_level=2 | --opt_level=2 |
--opt_level=n | optimized as specified | optimized as specified |
The levels of optimizations described above are performed by the stand-alone optimization pass. The code generator performs several additional optimizations, particularly processor-specific optimizations. It does so regardless of whether you invoke the optimizer. These optimizations are always enabled, although they are more effective when the optimizer is used.
NOTE
Do Not Lower the Optimization Level to Control Code SizeTo reduce code size, do not lower the level of optimization. Instead, use the --opt_for_space option to control the code size/performance tradeoff. Higher optimization levels (--opt_level or -O) combined with high --opt_for_space levels result in the smallest code size. For more information, see Section 4.9.
NOTE
The --opt_level=n (-On) Option Applies to the Assembly OptimizerThe --opt_level=n (-O) option should also be used with the assembly optimizer. The assembly optimizer does not perform all the optimizations described here, but key optimizations such as software pipelining and loop unrolling require the --opt_level option.