SlideShare a Scribd company logo
1 of 46
CODE OPTIMIZATION Presented By: Amita das Jayanti bhattacharya Jaistha Upadhyay
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? ,[object Object]
 
Levels' of optimization ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
When to optimize ? ,[object Object],[object Object],[object Object]
Criteria For optimization ,[object Object],[object Object],[object Object],[object Object],[object Object]
Improvements can be made at various phases: ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Types of Code optimization ,[object Object],[object Object],[object Object]
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. ,[object Object],[object Object],[object Object]
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 ,[object Object]
 
 
 
 
 
 
Three Address Code of Quick Sort i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto (5) j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto (9) if i >= j goto (23) t 6  = 4 * i x = a[t 6 ]   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 t 7  = 4 * I t 8  = 4 * j t 9  = a[t 8 ] a[t 7 ] = t 9 t 10  = 4 * j a[t 10 ] = x goto (5) t 11  = 4 * I x = a[t 11 ] t 12   = 4 * i t 13  = 4 * n t 14  = a[t 13 ] a[t 12 ] = t 14 t 15  = 4 * n a[t 15 ] = x 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Find The Basic Block i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto (5) j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto (9) if i >= j goto (23) t 6  = 4 * i x = a[t 6 ]   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 t 7  = 4 * I t 8  = 4 * j t 9  = a[t 8 ] a[t 7 ] = t 9 t 10  = 4 * j a[t 10 ] = x goto (5) t 11  = 4 * i x = a[t 11 ] t 12   = 4 * i t 13  = 4 * n t 14  = a[t 13 ] a[t 12 ] = t 14 t 15  = 4 * n a[t 15 ] = x 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Flow Graph if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 t 6  = 4 * i x = a[t 6 ]   t 7  = 4 * i t 8  = 4 * j t 9  = a[t 8 ] a[t 7 ] = t 9 t 10  = 4 * j a[t 10 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 12   = 4 * i t 13  = 4 * n t 14  = a[t 13 ] a[t 12 ] = t 14 t 15  = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 t 6  = 4 * i x = a[t 6 ]   t 7  = 4 * i t 8  = 4 * j t 9  = a[t 8 ] a[t 7 ] = t 9 t 10  = 4 * j a[t 10 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 12   = 4 * i t 13  = 4 * n t 14  = a[t 13 ] a[t 12 ] = t 14 t 15  = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 t 6  = 4 * i x = a[t 6 ]   t 8  = 4 * j t 9  = a[t 8 ] a[ t 6 ] = t 9 t 10  = 4 * j a[t 10 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 12   = 4 * i t 13  = 4 * n t 14  = a[t 13 ] a[t 12 ] = t 14 t 15  = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 t 6  = 4 * i x = a[t 6 ]   t 8  = 4 * j t 9  = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11  = 4 *i x = a[t 11 ] t 12   = 4 * i t 13  = 4 * n t 14  = a[t 13 ] a[t 12 ] = t 14 t 15  = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 t 6  = 4 * i x = a[t 6 ]   t 8  = 4 * j t 9  = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 12   = 4 * i t 13  = 4 * n t 14  = a[t 13 ] a[t 12 ] = t 14 t 15  = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 t 6  = 4 * i x = a[t 6 ]   t 8  = 4 * j t 9  = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 13  = 4 * n t 14  = a[t 13 ] a[ t 11 ] = t 14 t 15  = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 t 6  = 4 * i x = a[t 6 ]   t 8  = 4 * j t 9  = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 13  = 4 * n t 14  = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 t 6  = 4 * i x = a[t 6 ]   t 8  = 4 * j t 9  = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 13  = 4 * n t 14  = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 x = a[ t 2 ]   t 8  = 4 * j t 9  = a[t 8 ] a[ t 2 ] = t 9 a[ t 8 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 13  = 4 * n t 14  = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 x = t 3  t 8  = 4 * j t 9  = a[t 8 ] a[ t 2 ] = t 9 a[ t 8 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 13  = 4 * n t 14  = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 x = t 3  a[ t 2 ] = t 5 a[ t 4 ] = x goto B 2 t 11  = 4 * i x = a[t 11 ] t 13  = 4 * n t 14  = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Common Subexpression Elimination if i >= j goto B 6 Similarly for B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 x = t 3  a[ t 2 ] = t 5 a[ t 4 ] = x goto B 2 x = t 3 t 14  = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Dead Code Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 x = t 3  a[ t 2 ] = t 5 a[ t 4 ] = x goto B 2 x = t 3 t 14  = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = x B 1 B 2 B 3 B 4 B 5 B 6
Dead Code Elimination if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 a[ t 2 ] = t 5 a[ t 4 ] = t 3 goto B 2 t 14  = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = t 3 B 1 B 2 B 3 B 4 B 5 B 6
Reduction in Strength if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] i = i  + 1 t 2  = 4 * i t 3  = a[t 2 ] if t 3  < v goto B 2 j = j – 1 t 4  = 4 *  j t 5  = a[t 4 ] if t 5  > v goto B 3 a[ t 2 ] = t 5 a[ t 4 ] = t 3 goto B 2 t 14  = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = t 3 B 1 B 2 B 3 B 4 B 5 B 6
Reduction in Strength if i >= j goto B 6 i = m - 1 j = n t 1  =4 *  n v = a[t 1 ] t 2  = 4 *  i t 4  = 4 * j t 2  = t 2  + 4 t 3  = a[t 2 ] if t 3  < v goto B 2 t 4  = t 4  - 4 t 5  = a[t 4 ] if t 5  > v goto B 3 a[ t 2 ] = t 5 a[ t 4 ] = t 3 goto B 2 t 14  = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = t 3 B 1 B 2 B 3 B 4 B 5 B 6

More Related Content

What's hot

Compiler Design
Compiler DesignCompiler Design
Compiler Design
Mir Majid
 

What's hot (20)

Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Run time storage
Run time storageRun time storage
Run time storage
 
Three Address code
Three Address code Three Address code
Three Address code
 
Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assembler
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocks
 

Similar to Code Optimization

Metodologia de la programación - expresiones
Metodologia de la programación - expresionesMetodologia de la programación - expresiones
Metodologia de la programación - expresiones
Mar_Angeles
 
طراحی الگوریتم فصل 1
طراحی الگوریتم فصل 1طراحی الگوریتم فصل 1
طراحی الگوریتم فصل 1
Saeed Sarshar
 
Boas mathematical methods in the physical sciences 3ed instructors solutions...
Boas  mathematical methods in the physical sciences 3ed instructors solutions...Boas  mathematical methods in the physical sciences 3ed instructors solutions...
Boas mathematical methods in the physical sciences 3ed instructors solutions...
Praveen Prashant
 
Complete solutions-mathematical-methods-in-the-physical-sciences-3rd-edition
Complete solutions-mathematical-methods-in-the-physical-sciences-3rd-editionComplete solutions-mathematical-methods-in-the-physical-sciences-3rd-edition
Complete solutions-mathematical-methods-in-the-physical-sciences-3rd-edition
Aba Dula
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithms
Aakash deep Singhal
 
Matematicas iv.
Matematicas iv.Matematicas iv.
Matematicas iv.
gabyjab
 

Similar to Code Optimization (20)

code optimization
code optimization code optimization
code optimization
 
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
 
Solutions manual for business math 10th edition by cleaves
Solutions manual for business math 10th edition by cleavesSolutions manual for business math 10th edition by cleaves
Solutions manual for business math 10th edition by cleaves
 
ブロックチェーン: 「 書き換え不可能な記録」によって 社会はどう変化するか?
ブロックチェーン: 「書き換え不可能な記録」によって社会はどう変化するか? ブロックチェーン: 「書き換え不可能な記録」によって社会はどう変化するか?
ブロックチェーン: 「 書き換え不可能な記録」によって 社会はどう変化するか?
 
Lec38
Lec38Lec38
Lec38
 
Metodologia de la programación - expresiones
Metodologia de la programación - expresionesMetodologia de la programación - expresiones
Metodologia de la programación - expresiones
 
Slides13.pdf
Slides13.pdfSlides13.pdf
Slides13.pdf
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
 
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
 
طراحی الگوریتم فصل 1
طراحی الگوریتم فصل 1طراحی الگوریتم فصل 1
طراحی الگوریتم فصل 1
 
kintone university 03-1. AD+カスタマイズ入門第6版 サンプル
kintone university 03-1. AD+カスタマイズ入門第6版 サンプルkintone university 03-1. AD+カスタマイズ入門第6版 サンプル
kintone university 03-1. AD+カスタマイズ入門第6版 サンプル
 
HIDRAULICA DE CANALES
HIDRAULICA DE CANALESHIDRAULICA DE CANALES
HIDRAULICA DE CANALES
 
Boas mathematical methods in the physical sciences 3ed instructors solutions...
Boas  mathematical methods in the physical sciences 3ed instructors solutions...Boas  mathematical methods in the physical sciences 3ed instructors solutions...
Boas mathematical methods in the physical sciences 3ed instructors solutions...
 
Complete solutions-mathematical-methods-in-the-physical-sciences-3rd-edition
Complete solutions-mathematical-methods-in-the-physical-sciences-3rd-editionComplete solutions-mathematical-methods-in-the-physical-sciences-3rd-edition
Complete solutions-mathematical-methods-in-the-physical-sciences-3rd-edition
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Math worksheet4
Math worksheet4Math worksheet4
Math worksheet4
 
AtCoder Regular Contest 038 解説
AtCoder Regular Contest 038 解説AtCoder Regular Contest 038 解説
AtCoder Regular Contest 038 解説
 
Lecture 12 data structures and algorithms
Lecture 12 data structures and algorithmsLecture 12 data structures and algorithms
Lecture 12 data structures and algorithms
 
Matematicas iv.
Matematicas iv.Matematicas iv.
Matematicas iv.
 
HCCMS 2nd 9 weeks review 2015
HCCMS 2nd 9 weeks review 2015HCCMS 2nd 9 weeks review 2015
HCCMS 2nd 9 weeks review 2015
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Recently uploaded (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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
 
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
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
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
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
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
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

Code Optimization

  • 1. CODE OPTIMIZATION Presented By: Amita das Jayanti bhattacharya Jaistha Upadhyay
  • 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.
  • 4.  
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 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.
  • 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.
  • 23.  
  • 24.  
  • 25.  
  • 26.  
  • 27.  
  • 28.  
  • 29. Three Address Code of Quick Sort i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto (5) j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto (9) if i >= j goto (23) t 6 = 4 * i x = a[t 6 ] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 t 7 = 4 * I t 8 = 4 * j t 9 = a[t 8 ] a[t 7 ] = t 9 t 10 = 4 * j a[t 10 ] = x goto (5) t 11 = 4 * I x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
  • 30. Find The Basic Block i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto (5) j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto (9) if i >= j goto (23) t 6 = 4 * i x = a[t 6 ] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 t 7 = 4 * I t 8 = 4 * j t 9 = a[t 8 ] a[t 7 ] = t 9 t 10 = 4 * j a[t 10 ] = x goto (5) t 11 = 4 * i x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
  • 31. Flow Graph if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 t 6 = 4 * i x = a[t 6 ] t 7 = 4 * i t 8 = 4 * j t 9 = a[t 8 ] a[t 7 ] = t 9 t 10 = 4 * j a[t 10 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 32. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 t 6 = 4 * i x = a[t 6 ] t 7 = 4 * i t 8 = 4 * j t 9 = a[t 8 ] a[t 7 ] = t 9 t 10 = 4 * j a[t 10 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 33. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 t 6 = 4 * i x = a[t 6 ] t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 t 10 = 4 * j a[t 10 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 34. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 t 6 = 4 * i x = a[t 6 ] t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11 = 4 *i x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 35. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 t 6 = 4 * i x = a[t 6 ] t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 12 = 4 * i t 13 = 4 * n t 14 = a[t 13 ] a[t 12 ] = t 14 t 15 = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 36. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 t 6 = 4 * i x = a[t 6 ] t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 t 15 = 4 * n a[t 15 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 37. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 t 6 = 4 * i x = a[t 6 ] t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 38. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 t 6 = 4 * i x = a[t 6 ] t 8 = 4 * j t 9 = a[t 8 ] a[ t 6 ] = t 9 a[ t 8 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 39. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 x = a[ t 2 ] t 8 = 4 * j t 9 = a[t 8 ] a[ t 2 ] = t 9 a[ t 8 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 40. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 x = t 3 t 8 = 4 * j t 9 = a[t 8 ] a[ t 2 ] = t 9 a[ t 8 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 41. Common Subexpression Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 x = t 3 a[ t 2 ] = t 5 a[ t 4 ] = x goto B 2 t 11 = 4 * i x = a[t 11 ] t 13 = 4 * n t 14 = a[t 13 ] a[ t 11 ] = t 14 a[ t 13 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 42. Common Subexpression Elimination if i >= j goto B 6 Similarly for B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 x = t 3 a[ t 2 ] = t 5 a[ t 4 ] = x goto B 2 x = t 3 t 14 = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 43. Dead Code Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 x = t 3 a[ t 2 ] = t 5 a[ t 4 ] = x goto B 2 x = t 3 t 14 = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = x B 1 B 2 B 3 B 4 B 5 B 6
  • 44. Dead Code Elimination if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 a[ t 2 ] = t 5 a[ t 4 ] = t 3 goto B 2 t 14 = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = t 3 B 1 B 2 B 3 B 4 B 5 B 6
  • 45. Reduction in Strength if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] i = i + 1 t 2 = 4 * i t 3 = a[t 2 ] if t 3 < v goto B 2 j = j – 1 t 4 = 4 * j t 5 = a[t 4 ] if t 5 > v goto B 3 a[ t 2 ] = t 5 a[ t 4 ] = t 3 goto B 2 t 14 = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = t 3 B 1 B 2 B 3 B 4 B 5 B 6
  • 46. Reduction in Strength if i >= j goto B 6 i = m - 1 j = n t 1 =4 * n v = a[t 1 ] t 2 = 4 * i t 4 = 4 * j t 2 = t 2 + 4 t 3 = a[t 2 ] if t 3 < v goto B 2 t 4 = t 4 - 4 t 5 = a[t 4 ] if t 5 > v goto B 3 a[ t 2 ] = t 5 a[ t 4 ] = t 3 goto B 2 t 14 = a[t 1 ] a[ t 2 ] = t 14 a[ t 1 ] = t 3 B 1 B 2 B 3 B 4 B 5 B 6