Professional Documents
Culture Documents
Code Optimization
Code Optimization
• Constant folding : it reters is the • Constant propagation: They refer to the task of
replacement of expressions that can replacing the name variables with constants
be evaluated at compile time by that have been assigned to them earlier, It is
their computed values an indirect optimization that does not lead to
• For example. the statement any optimization itself but helps in other
A=5+3+A+C can be replaced by optimization
A=8+A+C. Note that the values 5 and • For example:
3 are variables at compile time and PI=3.141592 …..
the sub-expression 5+3 can be pre R=PI/180.0
computed and replaced with 8 in Constnt prapogation would transform this code into
the original expression. leading to PI=3.141592….
A=8+A+C R=3.141592/180.0
After applying folding transformation in the preceding
co the code becomes
PI=3.141502….
R=0.0174644
Common Sub-Expression Elimination
• Code movement is the method in which code from one part of the
program is moved to another part so that the resulting program is
more efficient.
• The modified programs maintain the semantic equivalence even
after modification
• Reduction of execution frequency: It means reducing the execution
frequency of the code being removed.
• It is the transformation that identifies the common pieces of code to
be evaluated at various places in the program and moves them within
the code such the their frequency of execution is reduced.
• It can be done in two ways: code holsting and loop optimization
• Code hoisting moves the pieces of code to some earlier parts of the
program to reduce their frequency of execution.
• On the other hand, loop optimization moves code statement(s) from
the loop body whose values do not depend on loop iterations.
• This is done because these are loop-Invariant statements and keeping
them in the loop will only invite repetitive execution of the same
code.
• code movement is motivated by the following reasons:
• To reduce the code space of the program: It means reducing the
code size by moving the piece of code to earlier parts of the
program so that the code generated is substantially small.
• Consider the following example:
if p > q then
X= a *10
Else
Y = a 10 +20
• We note that a 10 is computed twice. The code can be modified to
compute a *10 only once before the if statement. The resulting code
is as follows:
temp= a*10
if p > q then
X= temp
Else Y = temp +20
• The code for a 10 is generated only once.
Dead Code Elimination:
• The portion of the program statements that is never visited and executed
under any situation is called dead code and can be removed from the
program without affecting program behaviour and performance
• A variable that is defined but not used in the entire program is considered to
be a dead variable
• example
• flag=false;
• ….
• ….
• if(flag)
• a=b+c:
• Data flow analysis reveals that each time the program reaches the if (flag)
statement, the value of flag is always false.
• This means the statement a=b+c would never be executed
• Therefore, the test and the assignment statements are considered to be dead
code and can be safely removed from the object code,
Loop optimization
1 1
s= 0 s= 0
s= s +
do{ a =5
i a =5 do{
s= s + i
i=i+
i=i+1
1
{
while (i < =n) {
while (i < =n)
Bringing a=5 outside the do while loop, is called
code motion.
Loop optimization:Loop unrolling
• Loop unrolling: It is an optimization technique where the code constituting the loop
body, is reproduced a number of times.
• This improves the time efficiency of the execution process
• Loop unrolling increases the program’s speed by eliminating loop control instruction and
loop test instructions.
• For example
int x;
for (x = 0; x < 100; x++)
{
delete(x);
}
After performing loop unrolling • Because of this modification the
int x; new proram has to make only 20
iteration instead of 100
for (x = 0; x < 100; x += 5 )
{
delete(x);
delete(x + 1);
delete(x + 2);
delete(x + 3);
delete(x + 4);
}
Loop optimization: Loop interchange
• This optimization interchanges the • The code fragment given before makes
inner loops with the outer loops to nonlocal references.
improve the nesting structure of the
loops, thereby helping In improving the • The code can be improved by
localization of memory references and interchanging the inner and outer loops
enabling further loop transformations. with better locality of reference.
• Consider the segment of code involving • The code can be rewritten as follows:
arrays in the following loop: for i= 0 to 50 do
for j= 0 to 50 do for j=0 to 50 do
for i=0 to 50 do A[i][j]=..
A[i][j]=…….
• The modified access pattern would be
a[0][0]. a[0] [1]. a[0][2].... etc., which
may offer better locality of references
in many cases.
Loop optimization: Loop splitting
• It refers to breaking the loop into • The preceding code fragment can be
multiple loops over the same index improved for locality in the
range. reference pattern by rewriting it as
• Each loop carries a part of the body follows:
of the loop. for i= 1 to 50 do
x[i] = ..
• For instance, consider the following end loop
loop code: for i= 1 to 50 do
for i= 1 to 50 do y[i] =..
x[i] = end loop
y[i] =
end loop • Note that both loops access
different variables and hence offer
better memory reference
performances
Strength Reduction
• The target machine has many • The quality of the code when
instructions that can be pushing or popping a stack is
implemented directly by the enhanced with the help of these
underlying hardware. modes.
• Identifying specific situations in the • They can also help in implementing
instruction and using such hardware statements such as a = a + 1.
instructions can reduce execution • Consider an example instruction
time significantly. that adds constant 1 to the register
• Auto-increment and auto-decrement R.
instructions are among such • ADD #1,R
advanced hardware-oriented
instructions. • It can be replaced by INC R
• A good optimizing compiler is • The latter instruction is equivalent
expected to implement such faster to the former instruction but costs
instructions. less and more efficient.
Elimination of Multiple Jumps