Information Technology Reference
In-Depth Information
The transformation statically identifies a repeated computation by locating multiple
occurrences of the same expression. These repeated computations are eliminated by
storing the result of evaluating the expression in a variable and accessing this variable
instead of reevaluating the expression.
17.7.3
Constant Folding
Constant folding is the process of simplifying a group of constants in a program. One
good example of constant folding is as follows:
x
=
y
×
2
.
0
×
2
.
0
which could be simplified to
x
=
y
×
4
.
0
In other words, x has been optimized by combing the 2.0 and the 2.0, and using
4.0 instead.
In some cases, constant folding is similar to a reduction in strength optimizations
and is most easily implemented on a directed acyclic graph (DAG) intermediate
representation. 35 However, it can be performed in almost any stage of compilation.
The compiler seeks any operation that has constant operands and, without side effects,
computes the result replacing the entire expression with instructions to load the result.
In another example of constant folding, if the program uses the expression
/2,
then this value should be precalculated during initialization be a and stored value,
such as pi div 2. This typically saves one floating point load and one floating point
divide instruction, which translates into a time savings of a few microseconds.
17.7.4
Loop Invariant Optimization
If a computation is calculated within a loop but does not need to be, then the calculation
can be moved outside of the loop instead. When a computation in a loop does not
change during the dynamic execution of the loop, this computation can be removed
from the loop to improve execution time performance (Song, et al., 2003). In one
example illustrated below in Figure 17.6, the evaluation of expression ax100 is loop
invariant in (a), and (b) shows a more efficient version of the loop in which the loop
invariant code has been removed from the loop.
17.7.5
Loop Induction Elimination
Some types of code optimizations, such as dead code elimination and common subex-
presssion elimination reduce the execution time by removing redundant computation.
35 http://en.citizendium.org/wiki/Constant folding
Search WWH ::




Custom Search