Constant folding and constant propagation are related
compiler optimizations used by many modern
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
s.
An advanced form of constant propagation known as
sparse conditional constant propagation can more accurately propagate constants and simultaneously remove
dead code
The term dead code has multiple definitions. Some use the term to refer to code (i.e. instructions in memory) which can never be executed at run-time.
In some areas of computer programming, dead code is a section in the source code of a program whi ...
.
Constant folding
Constant folding is the process of recognizing and evaluating
constant expressions at
compile time
In computer science, compile time (or compile-time) describes the time window during which a language's statements are converted into binary instructions for the processor to execute. The term is used as an adjective to describe concepts relat ...
rather than computing them at runtime. Terms in constant expressions are typically simple literals, such as the
integer literal In computer science, an integer literal is a kind of literal (computer programming), literal for an integer (computer science), integer whose Value (computer science), value is directly represented in source code. For example, in the assignment stat ...
2
, but they may also be variables whose values are known at compile time. Consider the statement:
i = 320 * 200 * 32;
Most compilers would not actually generate two multiply instructions and a store for this statement. Instead, they identify constructs such as these and substitute the computed values at compile time (in this case,
2,048,000
).
Constant folding can make use of arithmetic identities. If
x
is numeric, the value of
0 * x
is zero even if the compiler does not know the value of
x
. (Note that this is not valid for
IEEE floats, since
x
could be Infinity or
NaN.)
Constant folding may apply to more than just numbers. Concatenation of
string literal
string literal or anonymous string is a literal for a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo", where , "foo ...
s and constant strings can be constant folded. Code such as
"abc" + "def"
may be replaced with
"abcdef"
.
Constant folding and cross compilation
In implementing a
cross compiler, care must be taken to ensure that the behaviour of the arithmetic operations on the host architecture matches that on the target architecture, as otherwise enabling constant folding will change the behaviour of the program. This is of particular importance in the case of
floating point
In computing, floating-point arithmetic (FP) is arithmetic on subsets of real numbers formed by a ''significand'' (a signed sequence of a fixed number of digits in some base) multiplied by an integer power of that base.
Numbers of this form ...
operations, whose precise implementation may vary widely.
Constant propagation
Constant propagation is the process of substituting the values of known constants in expressions at compile time. Such constants include those defined above, as well as
intrinsic function
In computer software, in compiler theory, an intrinsic function, also called built-in function or builtin function, is a function ( subroutine) available for use in a given programming language whose implementation is handled specially by the com ...
s applied to constant values. Consider the following pseudocode:
int x = 14;
int y = 7 - x / 2;
return y * (28 / x + 2);
Propagating x yields:
int x = 14;
int y = 7 - 14 / 2;
return y * (28 / 14 + 2);
Continuing to propagate yields the following (which would likely be further optimized by
dead-code elimination
In compiler theory, dead-code elimination (DCE, dead-code removal, dead-code stripping, or dead-code strip) is a compiler optimization to remove dead code (code that does not affect the program results). Removing such code has several benefits: i ...
of both x and y.)
int x = 14;
int y = 0;
return 0;
Constant propagation is implemented in compilers using
reaching definition analysis results. If all a variable's reaching definitions are the same assignment - which assigns a same constant to the variable - then the variable will always have the same value, and can be replaced with the constant.
Constant propagation can also cause conditional branches to simplify to one or more unconditional statements, if the conditional expression can be evaluated to true or false at compile time to determine the only possible outcome.
The optimizations in action
Constant folding and propagation are typically used together to achieve many simplifications and reductions, and their interleaved, iterative application continues until those effects cease.
Consider this unoptimized pseudocode returning a number unknown pending analysis:
int a = 30;
int b = 9 - (a / 5);
int c = b * 4;
if (c > 10)
return c * (60 / a);
Applying constant propagation once, followed by constant folding, yields:
int a = 30;
int b = 3;
int c = b * 4;
if (c > 10)
return c * 2;
Repeating both steps twice produces:
int a = 30;
int b = 3;
int c = 12;
if (true)
return c * 2;
Having replaced all uses of variables
a
and
b
with constants, the compiler's
dead-code elimination
In compiler theory, dead-code elimination (DCE, dead-code removal, dead-code stripping, or dead-code strip) is a compiler optimization to remove dead code (code that does not affect the program results). Removing such code has several benefits: i ...
applies to those variables, leaving:
int c = 12;
if (true)
return c * 2;
(Boolean constructs vary among languages and compilers, but their details—such as the status, origin, and representation of
true
True most commonly refers to truth, the state of being in congruence with fact or reality.
True may also refer to:
Places
* True, West Virginia, an unincorporated community in the United States
* True, Wisconsin, a town in the United States
* ...
—do not affect these optimization principles.)
Traditional constant propagation produces no further optimization; it does not restructure programs.
However, a similar optimization,
sparse conditional constant propagation, goes further by selecting the appropriate conditional branch,
and removing the always-true conditional test. Thus, variable
c
becomes redundant, and only an operation on a constant remains:
return 4;
If that pseudocode constitutes a function body, the compiler knows the function evaluates to integer constant
4
, allowing replacement of calls to the function with
4
, and further increasing program efficiency.
See also
*
Control-flow graph
*
Use-define chain and
SSA form
*
Copy propagation
*
Common subexpression elimination
*
Partial evaluation
References
Further reading
*
{{Compiler optimizations
Compiler optimizations