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
(default if --opt_level option not used and
--vectypes=off)
- --opt_level=0 or -O0
(default if --opt_level option not used and
--vectypes=on)
- --opt_level=1 or -O1
Performs all --opt_level=0 (-O0) optimizations, plus:
- --opt_level=2 or -O2
(default if --opt_level option used with no setting)
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.10 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)
- --opt_level=4 or -O4
For details about how the --opt_level
and --opt_for_speed options and various pragmas affect inlining, see Section 3.10.
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 --opt_level optimization level. Instead, use the --opt_for_speed option to control
the code size/performance tradeoff. Higher optimization levels (--opt_level or -O) combined
with low --opt_for_speed levels (0, 1 or 2) result in smaller code sizes.