The Constant Propagation and Constant Folding analyses derive their names from their use in optimising compilers.
Constant Propagation determines which values can reach a given variable at a given location, in case the variable always has a constant value.
Constant Folding computes side-effect-free computations, usually arithmetic, of constant values. For example, in the following program
x := 1;
print(x); // A
print(1 + 2); // B
print(x + 1); // C
y := read_int();
print(x + 2 + y); // D
A
B
C
D
, neither can fully determine the printed value, because y
is statically unknown.However, for D
, constant propagation and constant folding together would be able to
determine that the expression inside the print statement can be simplified to
print(3 + y); // D -- simplified (in an optimising compiler)