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:
“In my Pantheon, Pan still reigns in his pristine glory, with his ruddy face, his flowing beard, and his shaggy body, his pipe and his crook, his nymph Echo, and his chosen daughter Iambe; for the great god Pan is not dead, as was rumored. No god ever dies. Perhaps of all the gods of New England and of ancient Greece, I am most constant at his shrine.”
—Henry David Thoreau (18171862)