IT lexicon › Programming › Constant folding

Constant folding

Programming På svenska → Updated: 2026-11-05

The compiler evaluates expressions made only of known values at compile time, so the program doesn't have to do it on every run.

Write 60 * 60 * 24 and the binary says 86400. Trivial on its own, but combined with constant propagation — carrying known values through variables — whole branches can turn out to be always true or always false and get deleted. In SSA form both are especially easy, since every variable has a single definition.

The hard part is floating point and cross-compilation. The result must be exactly what the target machine would have computed, not the host, which is why GCC folds math functions using the MPFR library for correct rounding. C++ eventually turned the phenomenon into a language feature with constexpr, where the programmer demands it instead of hoping.

← Back to the lexicon