Constant Propagation

Constants assigned to a variable can be propagated through the flow graph and substituted at the use of the variable.

Example:

In the code fragment below, the value of x can be propagated to the use of x.

x = 3;
y = x + 4;
  

Below is the code fragment after constant propagation and constant folding.

x = 3;
y = 7;
  

Notes:

Some compilers perform constant propagation within basic blocks; some compilers perform constant propagation in more complex control flow.

Some compilers perform constant propagation for integer constants, but not floating-point constants.

Few compilers perform constant propagation through bitfield assignments.

Few compilers perform constant propagation for address constants through pointer assignments.