2. CODE OPTIMIZATION
Optimization is a program transformation technique,
which tries to improve the code by making it consume less
resources (i.e. CPU, Memory) and deliver high speed.
Optimization can be categorized broadly into two types :
1. machine independent and
2. machine dependent.
4. The Principal Sources of Optimization
A transformation of a program is called local if it can
be performed by looking only at the statements in a
basic block, otherwise it is called global.
Function Preserving Transformations:
Common Sub expression Elimination
Copy Propagation
Dead-Code Elimination
Constant Folding
5. An ‘occurrence’ of an expression E is called a common
subexpression if E was previously computed and the
values of variable in E have not changed since the
previous computation
6. Flow graph before and after eliminating the
local common sub expressions in B2
7. Copy Propagation
Copy propagation transformation is to use ‘y’ for ‘x’ wherever
possible after the copy statement x := y.
For example copy propagation applied to following block B
B2
x := t3
a[t4] := x
goto B2
yields the resultant block, as follows:
B2
x := t3
a[t4] := t3
goto B2
8. Dead Code Elimination
A variable is live at a point in a program if its value can be
used subsequently otherwise it is dead at that point
For Example: Dead code elimination removes the assignment
B2
x := t3
a[t4] := x
goto B2
Into
B2
a[t4] := t3
goto B2
9. In this technique, it involves eliminating the
dead code.
The statements of the code which either never
executes or are unreachable or their output is
never used are eliminated.
Example-
10. Reduction in strength
It replaces expensive operation by a cheaper one
such as multiplication by an addition.
For Example: The block B in the following flow
graph.
11. Thus the resultant flow graph after reduction in strength
appears as follows:
12. In this technique, it involves reducing the
strength of expressions.
This technique replaces the expensive and costly
operators with the simple and cheaper ones.
Example-
13. Loop Optimizations
Running time of a program may be improved if
we decrease the number of instructions in an
inner loop, even if we increase the amount of
code outside that loop.
Three techniques are important for loop
optimizataion
1. Code Motion
2. Induction Variable Elimination
3. Reduction in Strength
15. BASIC BLOCKS
Source codes which are always executed in sequence
are considered as the basic blocks of the code.
These basic blocks do not have any jump statements
among them
A program can have various constructs as basic
blocks, like IF-THEN-ELSE,
SWITCH-CASE conditional statements and
loops such as DO-WHILE, FOR, and REPEAT-
UNTIL, etc.
16. Basic block identification
Search header statements of all the basic blocks
from where a basic block starts:
First statement of a program.
Statements that are target of any branch
(conditional/unconditional).
Statements that follow any branch statement.
Header statements and the statements following
them form a basic block.
A basic block does not include any header
statement of any other basic block.
18. DAG representation of Basic Blocks
Directed a cyclic graphs (dags) are useful data
structures for implementing transformation on
basic blocks.
The Dag for the statement x := y + z is:
19. The Dag for the statements.
t1:= 4 * i
t2:= a[t ] is
20. Peephole Optimization
Techniques for “locally improving” the largest
code is peephole optimization
Method for trying to improve the performance of
the target program by examining a short
sequence of target instructions (called the
peephole)
replacing these instructions by a shorter or faster
sequence hidden ever possible.
21. Characteristics of Peephole Optimizations:
Redundant instruction elimination
Flow of control optimizations
Algebraic simplifications
Use of machine idioms.
22. Optimization of Basic Blocks
Code improving transformations for basic blocks
includes:
1. Common subexpression elimination
2. Dead code elimination
3. Reduction in strength.
Many of the structure preserving transformation
can be implemented by constructing a dag for a
basic block.
23. Common Subexpression Elimination
The dag for the following block:
a := b + c
b := a - d
c := b + c
d := a - d is shown as follows:
24. If b is not live on exit we can eliminate the
common subexpressions and transform the
block as follows:
a := b + c
d := a - d
c := d + c
Corresponding dag is:
25. BASIC BLOCKS AND FLOW GRAPHS
A graph representation of 3 address statements
is called a flow graph.
Nodes in the flow graph represent computations
and the edges represents the flow of control.
26. Basic Blocks
A basic block is a sequence of consecutive
statements in which flow of control enters at the
beginning and leaves at the end without halt or
possibility of branching except at the end.
The following sequence of three address,
statements forms a basic block:
t:= a * a
t:= a * b
t:= 2 * t
t:= t+ t
27. Algorithm
Partition into basic blocks
Input: A sequence of three address statements.
Output: A list of basic blocks with each three
address statement in exactly one block.
28. Source code to compute the sum of 2 arrays
begin
sum := 0
i := 1
do begin
sum := sum + a[i] + b[i];
i = i + 1;
end
While i < - 10
end
29. A list of 3 address statements is:
1) sum := 0
2) i = 1
3) t1 := 4 * i
4) t2 := a[t1]
5) t3 := 4 * i
6) t4 := b[t3]
7) t5 := t2 + t4
8) t6 := sum + t5
9) sum := t6
10) t7 := i + 1
11) i = t7
12) i < = 10 goto 3
31. Transformations on Basic Blocks
Two important classes of local transformations
that can be applied to basic blocks are
Structure preserving transformations and
Algebraic transformation.
32. Structure Preserving Transformations:
The primary structure preserving transformations
are:
Common subexpression elimination
Dead code elimination
Renaming of temporary variables
Intercharge of two independent adjacent
statements
33. Common Subexpression Elimination
Consider the basic block
a := b + c
d := b + c.
As both the statements performs the same
computation in the block, it can be transformed
into an equivalent block as shown next
a := b + c
d := a.
34. The Principal Sources of Optimization
Types:
1. Function Preserving Transformations
2. Loop Optimizations
1. Function Preserving Transformations
Common Sub expression Elimination
Copy Propagation
Dead-Code Elimination
Constant Folding
2. Loop Optimizations
Code Motion
Induction Variable Elimination
Reduction in Strength