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:
“I should say that the most prominent scientific men of our country, and perhaps of this age, are either serving the arts and not pure science, or are performing faithful but quite subordinate labors in particular departments. They make no steady and systematic approaches to the central fact.... There is wanting constant and accurate observation with enough of theory to direct and discipline it. But, above all, there is wanting genius.”
—Henry David Thoreau (18171862)