Skip to content
Search

Constant folding

lastmod 2016-12-01created 2016-12-011 edits

Constant folding and constant propagation are related compiler optimizations used by many modern compilers. An advanced form of constant propagation known as sparse conditional constant propagation can more accurately propagate constants and simultaneously remove dead code.

Constant propagation is a compiler optimization technique: if the value of a variable is known to be a constant at compile time, the constant is used directly in place of the variable. If this makes an entire expression constant, it can be computed at compile time and the result used directly. (constant folding)

Example

Constant Propagation is a very simple (in the principle) optimization left to compilers.

size_t radius = 5;
size_t diameter = 2*radius;
float perimeter = diameter * 3.1416f;

The program is thus equivalent to:

size_t radius = 5;
size_t diameter = 10;
float perimeter = 31.416f;

Favorite site