CH5 1
CH5 1
1
Code Optimization
Code optimization is a technique which tries to improve the code by
elimination unnecessary code lines and arranging the statements in such a
sequence that speed up the program execution without wasting resources.
2
Code Optimization
A code optimizing process must follow the three rules
The output code must not, in any way, change the meaning
of the program.
Optimization should increase the speed of the program and
the program should demand less number of resources.
Optimization should itself be fast and should not delay the
overall compiling process.
3
Code Optimization
Optimization can be made at various levels of compiling the
process.
At the beginning, users can change/rearrange the code or use
better algorithms to write the code.
After generating intermediate code, the compiler can
modify the intermediate code by address calculations and
improving loops.
While producing the target machine code, the compiler
can make use of memory hierarchy and CPU registers.
4
Code Optimization
S1 = 4 x i
S1 = 4 x i
S2 = a[S1]
S2 = a[S1]
S3 = 4 x j
S3 = 4 x j
S4 = 4 x i // Redundant Expression
S5 = n
S5 = n
S6 = b[S1] + S5
S6 = b[S4] + S5
8
Constant Propagations
If some variable has been assigned some constant value, then it replaces that
variable with its constant value in the further program during compilation.
The condition is that the value of variable must not get alter in between.
pi = 3.14
radius = 10
Area of circle = pi x radius x radius
Here,
This technique substitutes the value of variables ‘pi’ and ‘radius’ at compile
time.
It then evaluates the expression 3.14 x 10 x 10.
The expression is then replaced with its result 3.14.
This saves the time at run time.
9
Jump Threading
➢ Major goal is to reduce the number of dynamically
executed jumps on different paths.
if (a > 5) if (a > 5)
goto j; goto somewhere;
stuff (); stuff ();
stuff (); stuff ();
j: j:
goto somewhere; goto somewhere;
10
Dead Code Elimination
➢ It involves eliminating the dead code.
i=0;
if (i == 1)
{ i=0;
a=x+5;
}
11
Strength Reduction
➢ It involves reducing the strength of expressions.
B=Ax2 B=A+A
➢ This is because the cost of multiplication operator is higher
than that of addition operator.
12
Low-level Optimization
It is highly specific to the type of architecture. This
includes the following:
13
Low-level Optimization
Instruction Scheduling – This is used to improve an instruction level
parallelism that in turn improves the performance of machines with
instruction pipelines. It will not change the meaning of the code but
rearranges the order of instructions to avoid pipeline stalls. Semantically
ambiguous operations are also avoided.
14
Low-level Optimization
Branch prediction – Branch prediction techniques help to
guess in which way a branch functions even though it is
not known definitively which will be of great help for the
betterment of results.
16
Machine Independent and Machine
dependent
Optimization can broadly be categorized into two- Machine
Independent and Machine dependent.
17
Machine Independent Optimization
The compiler takes in the intermediate code and
transforms a part of the code that does not involve any
CPU registers and/or absolute memory locations.
do Item = 10;
{ do
item = 10; {
value = value + item; value = value + item;
} }
while(value<100); while(value<100);
18
Machine Independent Optimization
Machine independence includes two types
Function Preserving
Loop optimization
Function preserving
Common Sub Expression Elimination
BO AO
T1 = 4+i T1 = 4+i
T2 = T2 +T1 T2 = T2 +T1
T3 = 4 + i T4 = T2 + T1
T4 = T2 + T3
19
Machine Independent Optimization
Constant folding:- just like constant propagation
Constant folding is the technique of converting an expression
(or part of an expression) by combining multiple
constants into a single constant.
BO AO
T1 = 5/2 T1 = 2.5
Copy Propagation
BO AO
T1 = X T2 = T3 + T2
T2 = T3 + T2 T3 = T1
20
Machine Independent Optimization
Loop Optimization
Code Motion
BO AO
While(i < = limit-2) t1 = limit – 2
{ While (i< =t1)
….. {
…… …..
… ……
} ...
}
21
Machine Independent Optimization
Strength Reduction
The multiplication operator can be easily replaced by left
shift operator a<<1 Division can be replaced by a a>>1
operator.
BO AO
T1 = a * 2 a<<1
T1 = a / 2 a >> 1
22
Machine Independent Optimization
Frequency Reduction
In this case if a expression inside a loop is not dynamically
affected by a loop we calculate it outside the loop and use
the value inside the loop.
23