Constant Folding - Constant Propagation

Constant propagation is the process of substituting the values of known constants in expressions at compile time. Such constants include those defined above, as well as intrinsic functions applied to constant values. Consider the following pseudocode:

int x = 14; int y = 7 - x / 2; return y * (28 / x + 2);

Propagating x yields:

int x = 14; int y = 7 - 14 / 2; return y * (28 / 14 + 2);

Continuing to propagate yields the following (which would likely be further optimized by dead code elimination of both x and y.)

int x = 14; int y = 0; return 0;

Constant propagation is implemented in compilers using reaching definition analysis results. If a variable's all reaching definitions are the same assignment which assigns a same constant to the variable, then the variable has a constant value and can be replaced with the constant.

Constant propagation can also cause conditional branches to simplify to one or more unconditional statements, when the conditional expression can be evaluated to true or false at compile time to determine the only possible outcome.

Read more about this topic:  Constant Folding

Famous quotes containing the word constant:

    The Mind that lies fallow but a single Day, sprouts up in Follies that are only to be killed by a constant and assiduous Culture.
    Joseph Addison (1672–1719)