Collectively, the following data flow optimizations replace expressions with less costly ones, detect and remove unnecessary assignments, and avoid operations that produce values that are already computed. The compiler with optimization enabled performs these data flow optimizations both locally (within basic blocks) and globally (across entire functions).
- Copy propagation. Following an assignment
to a variable, the compiler replaces references to the variable with its value.
The value can be another variable, a constant, or a common subexpression. This
can result in increased opportunities for constant folding, common subexpression
elimination, or even total elimination of the variable. This type of
optimization is enabled by the --opt_level=1 and higher optimization
settings.
- Common subexpression elimination. When two
or more expressions produce the same value, the compiler computes the value
once, saves it, and reuses it. This type of optimization is enabled by the
--opt_level=2 and higher optimization settings.
- Redundant assignment elimination. Often,
copy propagation and common subexpression elimination optimizations result in
unnecessary assignments to variables (variables with no subsequent reference
before another assignment or before the end of the function). The compiler
removes these dead assignments. This type of optimization is enabled by the
--opt_level=1 for local assignments and --opt_level=2 for global
assignments.