SlideShare a Scribd company logo
1 of 46
CODE OPTIMIZATION
PRSENTED BY:
SANJEEV KUMAR……
DEPT:-IT.
ASSAM UNIVERSITY ,SILCHAR
Design Of a Compiler
Lexical Analysis
Syntax Analysis
Intermediate Code Generation
Code Generation
Code Optimization
Table
Mgmt
Routine
Error
Handling
Routine
Source code
Object code
What is optimization?
 In computing, optimization is the process of modifying a system to
make some aspect of it work more efficiently or use fewer resources.
For instance, a computer program may be optimized so that it
executes more rapidly, or is capable of operating with less memory
storage or other resources, or draw less power. The system may be a
single computer program, a collection of computers or even an entire
network such as the internet.
Levels' of optimization
Optimization can occur at a number of 'levels':
 Design level
At the highest level, the design may be optimized to make best
use of the available resources. The implementation of this
design will benefit from the use of suitable efficient algorithms
and the implementation of these algorithms will benefit from
writing good quality code. The architectural design of a system
overwhelmingly affects its performance. The choice of
algorithm affects efficiency more than any other item of the
design.
 Compile level
Use of an optimizing compiler tends to ensure that the
executable program is optimized at least as much as the
compiler can predict.
 Assembly level
At the lowest level, writing code using an Assembly language
designed for a particular hardware platform will normally produce
the most efficient code since the programmer can take advantage of
the full repertoire of machine instructions. The operating systems of
most machines has been traditionally written in Assembler code for
this reason.
 Runtime
Just In Time Compiler and assembler programmers are able to
perform runtime optimization.
When to optimize ?
Optimization is often performed at the end of the
development stage since it
• reduces readability
• adds code that is used to improve the performance.
Criteria For optimization
An optimization must preserve the meaning of a
program :
-Cannot change the output produced for any input
-Can not introduce an error
optimization should, on average, speed up
programs
Transformation should be worth the effort
Improvements can be made at various phases:
Source Code:
-Algorithms transformations can produce spectacular improvements
-Profiling can be helpful to focus a programmer’s attention on important
code.
Intermediate Code:
-Compiler can improve loops, procedure calls and address calculations
-Typically only optimizing compilers include this phase
Target Code:
-Compilers can use registers efficiently
-Peephole transformation can be applied
Types of Code optimization
 Common Sub-expression Removal
 Dead Code Optimization
 Loop Optimization
Common Sub expression elimination
Common Sub expression elimination is a optimization that
searches for instances of identical expressions (i.e they all
evaluate the same value), and analyses whether it is
worthwhile replacing with a single variable holding the
computed value.
a=b * c + g
d=b * c * d
temp=b * c
a=temp + g
d=temp * d
Dead Code elimination is a compiler optimization that removes code that
does not affect a program. Removing such code has two benefits It shrinks
program size, an important consideration in some contexts. It lets the running
program avoid executing irrelevant operations, which reduces its running
time.
Dead Code elimination is of two types
Unreachable Code
Redundant statement
Dead code Optimization:
Unreachable Code
In Computer Programming, Unreachable Code or dead code is code that
exists in the source code of a program but can never be executed.
Program Code
If (a>b)
m=a
elseif (a<b)
m=b
elseif (a==b)
m=0
else
m=-1
Optimized Code
If (a>b)
m=a
elseif (a<b)
m=b
else
m=0
Redundant Code
Redundant Code is code that is executed but has
no effect on the output from a program
main(){
int a,b,c,r;
a=5;
b=6;
c=a + b;
r=2;
r++;
printf(“%d”,c);
}
Adding time & space complexity
Loop optimization
Loop optimization plays an important role in improving
the performance of the source code by reducing overheads
associated with executing loops.
Loop Optimization can be done by removing:
• Loop invariant
• Induction variables
Loop Invariant
i = 1
s= 0
do{
s= s + i
a =5
i = i + 1
{
while (i < =n)
i = 1
s= 0
a =5
do{
s= s + i
i = i + 1
{
while (i < =n)
Bringing a=5 outside the do while loop, is called code
motion.
Induction variables
i = 1
s= 0
S1=0
S2=0
while (i < =n)
{
s= s + a[ i ]
t1 = i * 4
s= s + b[ t1 ]
t2 = t1 +2
s2= s2 + c[ t2 ]
i = i + 1
}
i = 1
s= 0
S1=0
S2=0
t2=0
while (i < =n)
{
s= s + a[ i ]
t1 = t1+ 4
s= s + b[ t1 ]
s2= s2 + c[t1 +2 ]
i = i + 1
}
t1,t2 are induction variables. i is inducing t1 and t1 is inducing t2
“+” replaced “ * ”,
t1 was made
independent of i
Common Sub-expression Removal
 It is used to remove redundant computations which usually
improves the execution time of a program.
Three Address Code of Quick Sort
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto (5)< v goto (5)
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto (9)> v goto (9)
if i >= j goto (23)if i >= j goto (23)
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
11
22
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
tt77 = 4 * I= 4 * I
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[ta[t77] = t] = t99
tt1010 = 4 * j= 4 * j
a[ta[t1010] = x] = x
goto (5)goto (5)
tt1111 = 4 * I= 4 * I
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t ] = x] = x
1616
1717
1818
1919
2020
2121
2222
2323
2424
2525
2626
2727
2828
2929
3030
Find The Basic Block
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto (5)< v goto (5)
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto (9)> v goto (9)
if i >= j goto (23)if i >= j goto (23)
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
11
22
33
44
55
66
77
88
99
1010
1111
1212
1313
1414
1515
tt77 = 4 * I= 4 * I
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[ta[t77] = t] = t99
tt1010 = 4 * j= 4 * j
a[ta[t1010] = x] = x
goto (5)goto (5)
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t ] = x] = x
1616
1717
1818
1919
2020
2121
2222
2323
2424
2525
2626
2727
2828
2929
3030
Flow Graph
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt77 = 4 * i= 4 * i
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[ta[t77] = t] = t99
tt1010 = 4 * j= 4 * j
a[ta[t1010] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt77 = 4 * i= 4 * i
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[ta[t77] = t] = t99
tt1010 = 4 * j= 4 * j
a[ta[t1010] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
tt1010 = 4 *= 4 *
jj
a[ta[t1010] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 *i= 4 *i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1212 = 4 * i= 4 * i
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[ta[t1212] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
tt1515 = 4 * n= 4 * n
a[ta[t1515] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
tt66 = 4 * i= 4 * i
x = a[tx = a[t66]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt66] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = a[x = a[tt22]]
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt22] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = tx = t33
tt88 = 4 * j= 4 * j
tt99 = a[t= a[t88]]
a[a[tt22] = t] = t99
a[a[tt88] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = tx = t33
a[a[tt22] = t] = t55
a[a[tt44] = x] = x
goto Bgoto B22
tt1111 = 4 * i= 4 * i
x = a[tx = a[t1111]]
tt1313 = 4 * n= 4 * n
tt1414 = a[t= a[t1313]]
a[a[tt1111] = t] = t1414
a[a[tt1313] = x] = x
B1
B2
B3
B4
B5 B6
Common Subexpression Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = tx = t33
a[a[tt22] = t] = t55
a[a[tt44] = x] = x
goto Bgoto B22
x = tx = t33
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = x] = x
B1
B2
B3
B4
B5 B6
Similarly for B6
Dead Code Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
x = tx = t33
a[a[tt22] = t] = t55
a[a[tt44] = x] = x
goto Bgoto B22
x = tx = t33
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = x] = x
B1
B2
B3
B4
B5 B6
Dead Code Elimination
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
a[a[tt22] = t] = t55
a[a[tt44] = t] = t33
goto Bgoto B22
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = t] = t33
B1
B2
B3
B4
B5 B6
Reduction in Strength
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
i = i + 1i = i + 1
tt22 = 4 * i= 4 * i
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
j = j – 1j = j – 1
tt44 = 4 * j= 4 * j
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
a[a[tt22] = t] = t55
a[a[tt44] = t] = t33
goto Bgoto B22
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = t] = t33
B1
B2
B3
B4
B5 B6
Reduction in Strength
i = m - 1i = m - 1
j = nj = n
tt11 =4 * n=4 * n
v = a[tv = a[t11]]
tt22 = 4 * i= 4 * i
tt44 = 4 * j= 4 * j
tt22 = t= t22 + 4+ 4
tt33 = a[t= a[t22]]
if tif t33 < v goto B< v goto B22
tt44 = t= t44 - 4- 4
tt55 = a[t= a[t44]]
if tif t55 > v goto B> v goto B33
if i >= j goto B6
a[a[tt22] = t] = t55
a[a[tt44] = t] = t33
goto Bgoto B22
tt1414 = a[t= a[t11]]
a[a[tt22] = t] = t1414
a[a[tt11] = t] = t33
B1
B2
B3
B4
B5 B6

More Related Content

What's hot

Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture NotesFellowBuddy.com
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGJothi Lakshmi
 
Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Daniyal Mughal
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammarmeresie tesfay
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Conditional Random Fields
Conditional Random FieldsConditional Random Fields
Conditional Random Fieldslswing
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequenceKiran K
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptFamiDan
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingR Islam
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 

What's hot (20)

Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammar
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Conditional Random Fields
Conditional Random FieldsConditional Random Fields
Conditional Random Fields
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 

Similar to code optimization

code optimization
code optimizationcode optimization
code optimizationguest9f8315
 
The Inner Secrets of Compilers
The Inner Secrets of CompilersThe Inner Secrets of Compilers
The Inner Secrets of CompilersIT MegaMeet
 
Online partial evaluation
Online partial evaluationOnline partial evaluation
Online partial evaluationEelco Visser
 
CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptssuser0be977
 
Solución guía n°1 operaciones combinadas
Solución guía n°1 operaciones combinadasSolución guía n°1 operaciones combinadas
Solución guía n°1 operaciones combinadasFrancisco Gaete Garrido
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)IoT Code Lab
 
System dynamics 3rd edition palm solutions manual
System dynamics 3rd edition palm solutions manualSystem dynamics 3rd edition palm solutions manual
System dynamics 3rd edition palm solutions manualSextonMales
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation19magnet
 
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)Anne Lee
 
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)Maho Nakata
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)Mumtaz Ali
 
Algorithms lecture 3
Algorithms lecture 3Algorithms lecture 3
Algorithms lecture 3Mimi Haque
 

Similar to code optimization (20)

code optimization
code optimizationcode optimization
code optimization
 
Slides13.pdf
Slides13.pdfSlides13.pdf
Slides13.pdf
 
The Inner Secrets of Compilers
The Inner Secrets of CompilersThe Inner Secrets of Compilers
The Inner Secrets of Compilers
 
14 recursion
14 recursion14 recursion
14 recursion
 
Online partial evaluation
Online partial evaluationOnline partial evaluation
Online partial evaluation
 
HIDRAULICA DE CANALES
HIDRAULICA DE CANALESHIDRAULICA DE CANALES
HIDRAULICA DE CANALES
 
TP3.pdf
TP3.pdfTP3.pdf
TP3.pdf
 
Code Optimization.ppt
Code Optimization.pptCode Optimization.ppt
Code Optimization.ppt
 
CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.ppt
 
Solución guía n°1 operaciones combinadas
Solución guía n°1 operaciones combinadasSolución guía n°1 operaciones combinadas
Solución guía n°1 operaciones combinadas
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
 
Programming meeting #8
Programming meeting #8Programming meeting #8
Programming meeting #8
 
Recursion.pptx
Recursion.pptxRecursion.pptx
Recursion.pptx
 
System dynamics 3rd edition palm solutions manual
System dynamics 3rd edition palm solutions manualSystem dynamics 3rd edition palm solutions manual
System dynamics 3rd edition palm solutions manual
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
 
GCC
GCCGCC
GCC
 
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
05 lcd slides 1 - CPU SCHEDULING (Powerpoint)
 
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
Algorithms lecture 3
Algorithms lecture 3Algorithms lecture 3
Algorithms lecture 3
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 

Recently uploaded (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 

code optimization

  • 1. CODE OPTIMIZATION PRSENTED BY: SANJEEV KUMAR…… DEPT:-IT. ASSAM UNIVERSITY ,SILCHAR
  • 2. Design Of a Compiler Lexical Analysis Syntax Analysis Intermediate Code Generation Code Generation Code Optimization Table Mgmt Routine Error Handling Routine Source code Object code
  • 3. What is optimization?  In computing, optimization is the process of modifying a system to make some aspect of it work more efficiently or use fewer resources. For instance, a computer program may be optimized so that it executes more rapidly, or is capable of operating with less memory storage or other resources, or draw less power. The system may be a single computer program, a collection of computers or even an entire network such as the internet.
  • 4.
  • 5. Levels' of optimization Optimization can occur at a number of 'levels':  Design level At the highest level, the design may be optimized to make best use of the available resources. The implementation of this design will benefit from the use of suitable efficient algorithms and the implementation of these algorithms will benefit from writing good quality code. The architectural design of a system overwhelmingly affects its performance. The choice of algorithm affects efficiency more than any other item of the design.  Compile level Use of an optimizing compiler tends to ensure that the executable program is optimized at least as much as the compiler can predict.
  • 6.  Assembly level At the lowest level, writing code using an Assembly language designed for a particular hardware platform will normally produce the most efficient code since the programmer can take advantage of the full repertoire of machine instructions. The operating systems of most machines has been traditionally written in Assembler code for this reason.  Runtime Just In Time Compiler and assembler programmers are able to perform runtime optimization.
  • 7. When to optimize ? Optimization is often performed at the end of the development stage since it • reduces readability • adds code that is used to improve the performance.
  • 8. Criteria For optimization An optimization must preserve the meaning of a program : -Cannot change the output produced for any input -Can not introduce an error optimization should, on average, speed up programs Transformation should be worth the effort
  • 9. Improvements can be made at various phases: Source Code: -Algorithms transformations can produce spectacular improvements -Profiling can be helpful to focus a programmer’s attention on important code. Intermediate Code: -Compiler can improve loops, procedure calls and address calculations -Typically only optimizing compilers include this phase Target Code: -Compilers can use registers efficiently -Peephole transformation can be applied
  • 10. Types of Code optimization  Common Sub-expression Removal  Dead Code Optimization  Loop Optimization
  • 11. Common Sub expression elimination Common Sub expression elimination is a optimization that searches for instances of identical expressions (i.e they all evaluate the same value), and analyses whether it is worthwhile replacing with a single variable holding the computed value. a=b * c + g d=b * c * d temp=b * c a=temp + g d=temp * d
  • 12. Dead Code elimination is a compiler optimization that removes code that does not affect a program. Removing such code has two benefits It shrinks program size, an important consideration in some contexts. It lets the running program avoid executing irrelevant operations, which reduces its running time. Dead Code elimination is of two types Unreachable Code Redundant statement Dead code Optimization:
  • 13. Unreachable Code In Computer Programming, Unreachable Code or dead code is code that exists in the source code of a program but can never be executed. Program Code If (a>b) m=a elseif (a<b) m=b elseif (a==b) m=0 else m=-1 Optimized Code If (a>b) m=a elseif (a<b) m=b else m=0
  • 14. Redundant Code Redundant Code is code that is executed but has no effect on the output from a program main(){ int a,b,c,r; a=5; b=6; c=a + b; r=2; r++; printf(“%d”,c); } Adding time & space complexity
  • 15. Loop optimization Loop optimization plays an important role in improving the performance of the source code by reducing overheads associated with executing loops. Loop Optimization can be done by removing: • Loop invariant • Induction variables
  • 16. Loop Invariant i = 1 s= 0 do{ s= s + i a =5 i = i + 1 { while (i < =n) i = 1 s= 0 a =5 do{ s= s + i i = i + 1 { while (i < =n) Bringing a=5 outside the do while loop, is called code motion.
  • 17. Induction variables i = 1 s= 0 S1=0 S2=0 while (i < =n) { s= s + a[ i ] t1 = i * 4 s= s + b[ t1 ] t2 = t1 +2 s2= s2 + c[ t2 ] i = i + 1 } i = 1 s= 0 S1=0 S2=0 t2=0 while (i < =n) { s= s + a[ i ] t1 = t1+ 4 s= s + b[ t1 ] s2= s2 + c[t1 +2 ] i = i + 1 } t1,t2 are induction variables. i is inducing t1 and t1 is inducing t2 “+” replaced “ * ”, t1 was made independent of i
  • 18.
  • 19.
  • 20.
  • 21.
  • 22. Common Sub-expression Removal  It is used to remove redundant computations which usually improves the execution time of a program.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29. Three Address Code of Quick Sort i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto (5)< v goto (5) j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto (9)> v goto (9) if i >= j goto (23)if i >= j goto (23) tt66 = 4 * i= 4 * i x = a[tx = a[t66]] 11 22 33 44 55 66 77 88 99 1010 1111 1212 1313 1414 1515 tt77 = 4 * I= 4 * I tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[ta[t77] = t] = t99 tt1010 = 4 * j= 4 * j a[ta[t1010] = x] = x goto (5)goto (5) tt1111 = 4 * I= 4 * I x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t ] = x] = x 1616 1717 1818 1919 2020 2121 2222 2323 2424 2525 2626 2727 2828 2929 3030
  • 30. Find The Basic Block i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto (5)< v goto (5) j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto (9)> v goto (9) if i >= j goto (23)if i >= j goto (23) tt66 = 4 * i= 4 * i x = a[tx = a[t66]] 11 22 33 44 55 66 77 88 99 1010 1111 1212 1313 1414 1515 tt77 = 4 * I= 4 * I tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[ta[t77] = t] = t99 tt1010 = 4 * j= 4 * j a[ta[t1010] = x] = x goto (5)goto (5) tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t ] = x] = x 1616 1717 1818 1919 2020 2121 2222 2323 2424 2525 2626 2727 2828 2929 3030
  • 31. Flow Graph i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt77 = 4 * i= 4 * i tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[ta[t77] = t] = t99 tt1010 = 4 * j= 4 * j a[ta[t1010] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 32. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt77 = 4 * i= 4 * i tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[ta[t77] = t] = t99 tt1010 = 4 * j= 4 * j a[ta[t1010] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 33. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 tt1010 = 4 *= 4 * jj a[ta[t1010] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 34. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 *i= 4 *i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 35. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1212 = 4 * i= 4 * i tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[ta[t1212] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 36. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 tt1515 = 4 * n= 4 * n a[ta[t1515] = x] = x B1 B2 B3 B4 B5 B6
  • 37. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 38. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 tt66 = 4 * i= 4 * i x = a[tx = a[t66]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt66] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 39. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = a[x = a[tt22]] tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt22] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 40. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = tx = t33 tt88 = 4 * j= 4 * j tt99 = a[t= a[t88]] a[a[tt22] = t] = t99 a[a[tt88] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 41. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = tx = t33 a[a[tt22] = t] = t55 a[a[tt44] = x] = x goto Bgoto B22 tt1111 = 4 * i= 4 * i x = a[tx = a[t1111]] tt1313 = 4 * n= 4 * n tt1414 = a[t= a[t1313]] a[a[tt1111] = t] = t1414 a[a[tt1313] = x] = x B1 B2 B3 B4 B5 B6
  • 42. Common Subexpression Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = tx = t33 a[a[tt22] = t] = t55 a[a[tt44] = x] = x goto Bgoto B22 x = tx = t33 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = x] = x B1 B2 B3 B4 B5 B6 Similarly for B6
  • 43. Dead Code Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 x = tx = t33 a[a[tt22] = t] = t55 a[a[tt44] = x] = x goto Bgoto B22 x = tx = t33 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = x] = x B1 B2 B3 B4 B5 B6
  • 44. Dead Code Elimination i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 a[a[tt22] = t] = t55 a[a[tt44] = t] = t33 goto Bgoto B22 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = t] = t33 B1 B2 B3 B4 B5 B6
  • 45. Reduction in Strength i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] i = i + 1i = i + 1 tt22 = 4 * i= 4 * i tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 j = j – 1j = j – 1 tt44 = 4 * j= 4 * j tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 a[a[tt22] = t] = t55 a[a[tt44] = t] = t33 goto Bgoto B22 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = t] = t33 B1 B2 B3 B4 B5 B6
  • 46. Reduction in Strength i = m - 1i = m - 1 j = nj = n tt11 =4 * n=4 * n v = a[tv = a[t11]] tt22 = 4 * i= 4 * i tt44 = 4 * j= 4 * j tt22 = t= t22 + 4+ 4 tt33 = a[t= a[t22]] if tif t33 < v goto B< v goto B22 tt44 = t= t44 - 4- 4 tt55 = a[t= a[t44]] if tif t55 > v goto B> v goto B33 if i >= j goto B6 a[a[tt22] = t] = t55 a[a[tt44] = t] = t33 goto Bgoto B22 tt1414 = a[t= a[t11]] a[a[tt22] = t] = t1414 a[a[tt11] = t] = t33 B1 B2 B3 B4 B5 B6