GHINSU:   Constant propagation

- What is constant propagation?
- Example of constant propagation
- Uses of constant propagation

What is constant propagation?

Constant propagation is a code reduction technique that replaces program variables with constant values. Of course, this is possible only when an instance of a variable at a particular point in the source program is known to have a constant value.

Example of constant propagation

An example of constant propagation is given below. Consider the following segment of an original source program:
x = 7;
y = 2 * x;
z = f (x, y);
We notice that the value of variable x in the segment's last two lines is the constant number 7. This is always true, because of the assignment to x in the segment's first line. Therefore, the two instances of x can be substituted by the constant value 7 (this value propagates through the next two lines). The reduced source program is the following:
x = 7;
y = 2 * 7;
z = f (7, y);

Uses of constant propagation

The application of constant propagation in the example presented above may appear obvious and the example itself may seem oversimplified. It is true that no real-world source program would be like this, because a good programmer would have taken into account the constant value of the variable and would have propagated it in the first place.

However, let us not overlook that when programmers debug a program, they usually start by trying to reproduce the bug for a particular test case, and therefore for a particular set of constant values for program variables. The general source program does not look at all like the oversimplified example presented above. However, the programmer only has to debug the specific program corresponding to the test case that produces the bug. In this program the values of several variables are constant and known and the program itself does not differ much from our example.

What next?

(o) Contents     (o) How does it work?     (o) Expression simplification

This page is maintained by Nikos Papaspyrou.
Please, feel free to send your comments, thoughts or suggestions to

Last updated: Monday May 15 1995, 12:05 EET DST.