The C/C++ compiler is able to perform various
optimizations, which are performed by the optimizer and the code generator:
The optimizer performs high-level
optimizations in the stand-alone optimization pass. Use higher optimization levels, such as
--opt_level=2 and --opt_level=3, to achieve optimal code.
The code generator performs several
additional optimizations. These are low-level, target-specific optimizations. It performs
these regardless of whether you invoke the optimizer and are always enabled, though they are
more effective when the optimizer is used.
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 as an alias for the --opt_level option. The n denotes
the level of optimization (0, 1, 2, 3),
which controls the type and degree of optimization.
- --opt_level=off or -Ooff
- --opt_level=0 or -O0
- --opt_level=1 or -O1
Performs all --opt_level=0 (-O0) optimizations, plus:
- --opt_level=2 or -O2
Performs all --opt_level=1 (-O1) optimizations, plus:
- --opt_level=3 or -O3
Performs all --opt_level=2 (-O2) optimizations, plus:
- Removes all functions that are never
called (Section 4.4)
- Simplifies functions with return
values that are never used (Section 4.4)
- Inlines calls to small functions
(Section 3.11 and Section 4.5)
- Reorders function declarations; the
called functions attributes are known when the caller is optimized
- Propagates arguments into function
bodies when all calls pass the same value in the same argument position
- Identifies file-level variable
characteristics (Section 4.4)
- Performs other optimizations (Section 4.3 and Section 4.4)
For details about how the --opt_level
and --opt_for_speed options and various pragmas affect inlining, see Section 3.11.
Debugging is enabled by default,
and the optimization level is unaffected by the generation of debug information.
Note:
Do Not Lower the Optimization Level to Control Code Size: To 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 Optimizer: The --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.