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:
“Henry David Thoreau, who never earned much of a living or sustained a relationship with any woman that wasnt brotherlywho lived mostly under his parents roof ... who advocated one days work and six days off as the weekly round and was considered a bit of a fool in his hometown ... is probably the American writer who tells us best how to live comfortably with our most constant companion, ourselves.”
—Edward Hoagland (b. 1932)