chharvey / counterpoint

A robust programming language.
GNU Affero General Public License v3.0
2 stars 0 forks source link

Compiler Technique: Constant Propagation #35

Closed chharvey closed 3 years ago

chharvey commented 4 years ago

Constant Propagation is a technique in which the compiler removes and replaces variables of a static constant value before run time, thus speeding up execution.

Given the source code

let a = 3;
let b = a + 5;

the compiler will essentially transform this into

let b = 3 + 5;

(And combined with Constant Folding (#34), it would reduce to let b = 8;.)

With constant propagation, the static fixed constant is removed all throughout the scope it’s in, and replaced by its value.

If the variable is unfixed however, this operation will not be performed, since its value could change.

let unfixed a = 3;
let b = a + 5;
a = 2;
% (no constant propagation here)