Code optimization
Ideal code optimizer
Code optimization
Code optimization is a method of code
modification
Such that it can improve code quality and
efficiency.
A program may be optimized so that it
becomes
smaller in size
Consumes less memory
Executes more rapidly or performs fewer
input/output operations
Scope of optimization
Definition
Code optimization is a technique which tries
to improve the code by eliminating
unnecessary code lines and arranging the
statements in sequence that speed up
program execution without wasting the
resources.
Advantages:
executes faster
efficient memory usage
better performance
Write 3 address code & construct CFG
Code optimization techniques
Compile time evaluation
Constant folding
Constant propagation
Common sub expression elimination
Strength reduction
Code movement
Dead code elimination
Constant folding
Constant folding is the process of recognizing
and evaluating constant expressions
at compile time rather than computing them
at runtime
Example:
i = 320 * 200 * 32;
Constant folding
i = 320 * 200 * 32;
Most modern compilers would not actually
generate two multiply instructions and a
store for this statement.
identify constructs such as these and
substitute the computed values at compile
time.
In this case the value is 2048000
the resulting code would load the
computed value and store it rather than
Constant propagation
Constant propagation is the process of
substituting the values of known constants in
expressions at compile time.
In the code fragment below, the value of x
can be propagated to the use of x.
x=3;
y= x+4;
Constant propagation
the code fragment after constant propagation
and constant folding.
X=3;
Y=7;
Constant propagation
Common sub expression elimination
Compiler writers distinguish two kinds of CSE:
local common sub expression
elimination works within a single basic block
global common sub expression
elimination works on an entire procedure
Def:
CSE is a compiler optimization that searches for
instances of identical expressions (i.e., they all
evaluate to the same value), and analyzes
whether it is worthwhile replacing them with a
single variable holding the computed value.
Common sub expression elimination
In the following code:
a = b * c + g;
d = b * c * e;
after eliminating common sub expression
tmp = b * c;
a = tmp + g;
d = tmp * e;
Common sub expression elimination
Common sub expression elimination
Common sub expression elimination
Strength reduction
strength reduction is a compiler
optimization where expensive operations are
replaced with equivalent but less expensive
operations
For strength reduction, the compiler is
interested in
loop invariants. These are the values that do
not change within the body of a loop.
induction variables. These are the values that
are being iterated each time through the loop.
Strength reduction
the multiplication of loop invariant c and
induction variable i
c = 7;
for (i = 0; i < N; i++)
{
y[i] = c * i;
}
Strength reduction
can be replaced with successive weaker
additions
c = 7;
k = 0;
for (i = 0; i < N; i++)
{
y[i] = k;
k = k + c;
}
Strength reduction
Code movement
It is a technique of moving a block of code
outside a loop if it won’t have any difference
if it is executed outside or inside of the loop.
Definition:
list of statements or expressions, which can
be moved outside the body of a loop without
affecting the semantics of the program.
Code movement
Code movement
Dead code elimination
Code that is unreachable or that does not
affect the program (e.g. dead stores) can be
eliminated.
Dead code elimination
Dead code elimination