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

Code generator
Code generatorCode generator
Code generator
Tech_MX
 

What's hot (20)

Code optimization
Code optimizationCode optimization
Code optimization
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Code generator
Code generatorCode generator
Code generator
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Macro Processor
Macro ProcessorMacro Processor
Macro Processor
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Three Address code
Three Address code Three Address code
Three Address code
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Postfix Notation | Compiler design
Postfix Notation | Compiler designPostfix Notation | Compiler design
Postfix Notation | Compiler design
 
Algorithmic problem solving
Algorithmic problem solvingAlgorithmic problem solving
Algorithmic problem solving
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph 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

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
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)

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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
 
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
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
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...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
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
 
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...
 
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
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
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
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 

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