SlideShare a Scribd company logo
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.
code optimization
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
code optimization
code optimization
code optimization
code optimization
Common Sub-expression Removal
 It is used to remove redundant computations which usually
improves the execution time of a program.
code optimization
code optimization
code optimization
code optimization
code optimization
code optimization
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 (20)

PPTX
Syntax-Directed Translation into Three Address Code
sanchi29
 
PPTX
Automata theory - Push Down Automata (PDA)
Akila Krishnamoorthy
 
PPTX
System Programming Unit II
Manoj Patil
 
PPT
Combinational circuits
SARITHA REDDY
 
PPTX
Intermediate code generation1
Shashwat Shriparv
 
PPTX
Type checking in compiler design
Sudip Singh
 
PPTX
Two pass Assembler
Satyamevjayte Haxor
 
PPTX
NFA Converted to DFA , Minimization of DFA , Transition Diagram
Abdullah Jan
 
PDF
Assembler
SheetalAwate2
 
PPT
Finite automata
Bipul Roy Bpl
 
PPTX
Assemblers
Dattatray Gandhmal
 
PPTX
Deciability (automata presentation)
Sagar Kumar
 
PPTX
Chomsky classification of Language
Dipankar Boruah
 
PPTX
Regular Expression Examples.pptx
GhulamRabani9
 
PPTX
MACRO PROCESSOR
Bhavik Vashi
 
PPTX
Deadlock Prevention
prachi mewara
 
PPTX
Pipelining approach
GopinathD17
 
PPTX
Register Transfer Language
mahesh kumar prajapat
 
PPTX
Finite automata-for-lexical-analysis
Dattatray Gandhmal
 
Syntax-Directed Translation into Three Address Code
sanchi29
 
Automata theory - Push Down Automata (PDA)
Akila Krishnamoorthy
 
System Programming Unit II
Manoj Patil
 
Combinational circuits
SARITHA REDDY
 
Intermediate code generation1
Shashwat Shriparv
 
Type checking in compiler design
Sudip Singh
 
Two pass Assembler
Satyamevjayte Haxor
 
NFA Converted to DFA , Minimization of DFA , Transition Diagram
Abdullah Jan
 
Assembler
SheetalAwate2
 
Finite automata
Bipul Roy Bpl
 
Assemblers
Dattatray Gandhmal
 
Deciability (automata presentation)
Sagar Kumar
 
Chomsky classification of Language
Dipankar Boruah
 
Regular Expression Examples.pptx
GhulamRabani9
 
MACRO PROCESSOR
Bhavik Vashi
 
Deadlock Prevention
prachi mewara
 
Pipelining approach
GopinathD17
 
Register Transfer Language
mahesh kumar prajapat
 
Finite automata-for-lexical-analysis
Dattatray Gandhmal
 

Viewers also liked (6)

PPTX
Fog computing ( foggy cloud)
Iffat Anjum
 
PPTX
Lecture 15 run timeenvironment_2
Iffat Anjum
 
PDF
Compiler unit 2&3
BBDITM LUCKNOW
 
PPT
Code Optimization
guest9f8315
 
PPT
Compiler Design
Mir Majid
 
PPTX
Three address code In Compiler Design
Shine Raj
 
Fog computing ( foggy cloud)
Iffat Anjum
 
Lecture 15 run timeenvironment_2
Iffat Anjum
 
Compiler unit 2&3
BBDITM LUCKNOW
 
Code Optimization
guest9f8315
 
Compiler Design
Mir Majid
 
Three address code In Compiler Design
Shine Raj
 
Ad

Similar to code optimization (20)

PPT
457418.-Compiler-Design-Code-optimization.ppt
Incredible20
 
PPT
code optimization
guest9f8315
 
PPTX
Code optmize.pptx which is related to coding
vamami6395
 
PPT
basics of optimizations presentation s
AnkitKumarSharma26
 
PDF
Optimization
Royalzig Luxury Furniture
 
PDF
Optimization
Royalzig Luxury Furniture
 
PPTX
Principle source of optimazation
Siva Sathya
 
PPTX
Introduction to code optimization by dipankar
Dipankar Nalui
 
PPTX
complier design unit 5 for helping students
aniketsugandhi1
 
PPT
lect23_optimization.ppt
ssuser0be977
 
PPTX
Machine_Learning_JNTUH_R18_UNIT5_CONCEPTS.pptx
Hemavanth1
 
PPT
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
PPTX
Project management
Avay Minni
 
PPTX
Compiler Design_Code Optimization tech.pptx
RushaliDeshmukh2
 
PDF
Numerical Solution of Linear algebraic Equation
payalpriyadarshinisa1
 
PPT
Code optimisation presnted
bhavanatmithun
 
PPTX
Principal Sources of Optimization in compiler design
LogsAk
 
PPT
DynamicProgramming.ppt
DavidMaina47
 
PDF
Dynamic programming
Amit Kumar Rathi
 
PPTX
Principal source of optimization in compiler design
Rajkumar R
 
457418.-Compiler-Design-Code-optimization.ppt
Incredible20
 
code optimization
guest9f8315
 
Code optmize.pptx which is related to coding
vamami6395
 
basics of optimizations presentation s
AnkitKumarSharma26
 
Principle source of optimazation
Siva Sathya
 
Introduction to code optimization by dipankar
Dipankar Nalui
 
complier design unit 5 for helping students
aniketsugandhi1
 
lect23_optimization.ppt
ssuser0be977
 
Machine_Learning_JNTUH_R18_UNIT5_CONCEPTS.pptx
Hemavanth1
 
Code Optimization Lec#7.ppt Code Optimizer
zeeshanmubeen1
 
Project management
Avay Minni
 
Compiler Design_Code Optimization tech.pptx
RushaliDeshmukh2
 
Numerical Solution of Linear algebraic Equation
payalpriyadarshinisa1
 
Code optimisation presnted
bhavanatmithun
 
Principal Sources of Optimization in compiler design
LogsAk
 
DynamicProgramming.ppt
DavidMaina47
 
Dynamic programming
Amit Kumar Rathi
 
Principal source of optimization in compiler design
Rajkumar R
 
Ad

Recently uploaded (20)

PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 

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.
  • 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
  • 22. Common Sub-expression Removal  It is used to remove redundant computations which usually improves the execution time of a program.
  • 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