SlideShare une entreprise Scribd logo
1  sur  21
Introduction to Code 
Optimization 
Dipankar Nalui 
M.SC. Computer Science 
Dept. of Computer Science 
Pondicherry University
Compiler 
• A compiler is a program that accepts a high level language and 
produces a target language as output (Assembly Language). 
Compiler 
Assembly Level 
High Level Language Language
 Phases of Compiler 
1) Lexical Analysis 
2) Syntax Analysis 
3) Semantic Analysis 
4) Intermediate Code Generation 
5)Code Optimization 
6) Code Generation
 Code Optimization 
•Optimization is the process of transforming a 
piece of code to make more efficient (either in 
terms of time or space) without changing its 
output or side-effects.
Why optimization is needed ??? 
• To improve intermediate code 
• Better target code 
• Executes Faster 
• Shorter code 
• Less power 
• Complexity : Time, Space & Cost 
• Efficient memory usage 
• Better performance.
 “Optimization” 
• The only difference visible to the code’s user should be that it runs 
faster and/or consumes less memory. 
• The name implies you are finding an "optimal“ solution— in truth, 
optimization aims to improve, not perfect, the result.
Correctness Above All! 
• Optimization should not change the correctness of the generated 
code. 
• Transforming the code to something that runs faster but incorrectly is 
of little value. 
• It is expected that the un-optimized and optimized variants give the 
same output for all inputs.
position = initial + rate * 60 
id1 = id2 + id3 * 60 
Intermediate Code 
T1= int-to-float(60) 
T2=id3*T1 
T3=id2+T2 
id1=T3 
Eliminate int-to-float by 
replacing 60 by 60.0 
Code Optimization 
T1=id3 * 60.0 
Id1=id2 + t1 
Code Optimizer can deduce 
that the conversion of 60 
from int to float can be done 
once.
 The principal Sources of Optimization 
• Some techniques are applied to the intermediate code, to streamline, 
rearrange, compress, etc. 
• Control-Flow Analysis 
• Local Optimizations 
• Constant Folding 
• Constant Propagation 
• Operator Strength Reduction 
• Copy Propagation 
• Dead Code Elimination 
• Common Subexpression Elimination 
• Global Optimizations, Data-Flow Analysis
Copy Propagation 
Intermediate Code 
tmp2 = tmp1 ; 
tmp3 = tmp2 * tmp1; 
tmp4 = tmp3 ; 
tmp5 = tmp3 * tmp2 ; 
c = tmp5 + tmp4 ; 
The code on the left makes a copy 
of tmp1 in tmp2 and a copy of 
tmp3 in tmp4. 
Code Optimization 
tmp3 = tmp1 * tmp1 ; 
tmp5 = tmp3 * tmp1 ; 
c = tmp5 + tmp3 ; 
we eliminated those 
unnecessary copies
Dead Code Elimination 
• If an instruction’s result is never used, the instruction is considered 
"dead" and can be removed from the instruction stream. 
• tmp1 = tmp2 + tmp3 ; 
• if tmp1 is never used again, we can eliminate this instruction. 
• if tmp1 holds the result of a function call: 
• tmp1 = LCall _Binky; 
• Even if tmp1 is never used again, we cannot eliminate the instruction 
because we can’t be sure that called function has no side-effects.
 Common Sub-expression Elimination 
Two operations are common if they produce 
the same result. 
main() 
{ 
int x, y, z; 
x = (1+20)* -x; 
y = x*x+(x/y); 
y = z = (x/y)/(x*x); 
} 
tmp1 = 1 + 20 ; 
tmp2 = -x ; 
x = tmp1 * tmp2 ; 
tmp3 = x * x ; 
tmp4 = x / y ; 
y = tmp3 + tmp4 ; 
tmp5 = x / y ; 
tmp6 = x * x ; 
z = tmp5 / tmp6 ; 
y = z ;
Intermediate Code 
tmp1 = 1 + 20 ; 
tmp2 = -x ; 
x = tmp1 * tmp2 ; 
tmp3 = x * x ; 
tmp4 = x / y ; 
y = tmp3 + tmp4 ; 
tmp5 = x / y ; 
tmp6 = x * x ; 
z = tmp5 / tmp6 ; 
y = z ; 
Code Optimization 
tmp2 = -x ; 
x = 21 * tmp2 ; 
tmp3 = x * x ; 
tmp4 = x / y ; 
y = tmp3 + tmp4 ; 
tmp5 = x / y ; 
z = tmp5 / tmp3 ; 
y = z ;
Algebraic Simplification 
• x+0 = x 
• 0+x = x 
• x*1 = x 
• 1*x = x 
• 0/x = 0 
• x-0 = x 
• b && true = b 
• b && false = false 
• b || true = true 
• b || false = b
 Example of Algebraic Simplification 
b = 5 + a + 10 ; 
Intermediate Code 
_tmp0 = 5 ; 
_tmp1 = _tmp0 + a ; 
_tmp2 = _tmp1 + 10 ; 
b = _tmp2 ; 
Code Optimization 
_tmp0 = 15 ; 
_tmp1 = a + _tmp0 ; 
b = _tmp1 ;
 Constant Folding 
• Evaluate constant expressions at compile time 
• Only possible when side-effect freeness guaranteed 
c:= 1 + 3 c:= 4 
true not false
 Constant Propagation 
• Variables that have constant value, e.g. c := 3 
• Later uses of c can be replaced by the constant 
• If no change of c between! 
b := 3 
c := 1 + b 
d := b + c 
b := 3 
c := 1 + 3 
d := 3 + c
 Strength Reduction 
• Replace expensive operations with simpler ones 
• Example: Multiplications replaced by additions 
y := x * 2 y := x + x
 Conclusion: 
• Why do we optimize programs? 
• Is there an optimal optimizer? 
• Where in a compiler does optimization happen? 
• Does the optimization capture most of the potential improvement 
without an unreasonable amount of effort ? 
• Does the optimization preserve the meaning of the source program ? 
• Does the optimization reduce the time and space?
Questions ??
Thank You …

Contenu connexe

Tendances

Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimizationZongYing Lyu
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Three address code generation
Three address code generationThree address code generation
Three address code generationRabin BK
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generationIffat Anjum
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONAnil Pokhrel
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler designRajkumar R
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 

Tendances (18)

Code optimization
Code optimizationCode optimization
Code optimization
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Code generator
Code generatorCode generator
Code generator
 
Three address code generation
Three address code generationThree address code generation
Three address code generation
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Code Generation
Code GenerationCode Generation
Code Generation
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Three Address code
Three Address code Three Address code
Three Address code
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTION
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
Intermediate code
Intermediate codeIntermediate code
Intermediate code
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Ch9b
Ch9bCh9b
Ch9b
 

En vedette

En vedette (19)

Efficient Programming Practices For AS3
Efficient Programming Practices For AS3Efficient Programming Practices For AS3
Efficient Programming Practices For AS3
 
Reusable Code - For Good or For Awesome!
Reusable Code - For Good or For Awesome!Reusable Code - For Good or For Awesome!
Reusable Code - For Good or For Awesome!
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
Code and memory optimization tricks
Code and memory optimization tricksCode and memory optimization tricks
Code and memory optimization tricks
 
Code optimization
Code optimization Code optimization
Code optimization
 
Instruction Level Parallelism Compiler optimization Techniques Anna Universit...
Instruction Level Parallelism Compiler optimization Techniques Anna Universit...Instruction Level Parallelism Compiler optimization Techniques Anna Universit...
Instruction Level Parallelism Compiler optimization Techniques Anna Universit...
 
Dsip and its biometrics appln
Dsip and its biometrics applnDsip and its biometrics appln
Dsip and its biometrics appln
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
Image pre processing - local processing
Image pre processing - local processingImage pre processing - local processing
Image pre processing - local processing
 
code optimization
code optimization code optimization
code optimization
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Code generation
Code generationCode generation
Code generation
 
Image pre processing
Image pre processingImage pre processing
Image pre processing
 
Embedded C - Optimization techniques
Embedded C - Optimization techniquesEmbedded C - Optimization techniques
Embedded C - Optimization techniques
 
Embedded C
Embedded CEmbedded C
Embedded C
 
Image Processing Basics
Image Processing BasicsImage Processing Basics
Image Processing Basics
 
PHP Optimization
PHP OptimizationPHP Optimization
PHP Optimization
 
Software reuse ppt.
Software reuse ppt.Software reuse ppt.
Software reuse ppt.
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
 

Similaire à Introduction to code optimization by dipankar

Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design LogsAk
 
Compiler presention
Compiler presentionCompiler presention
Compiler presentionFaria Priya
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review ProcessDr. Syed Hassan Amin
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Mohamed El Desouki
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language CourseVivek chan
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtrykapib57390
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDeepikaV81
 

Similaire à Introduction to code optimization by dipankar (20)

Code optimization
Code optimizationCode optimization
Code optimization
 
sCode optimization
sCode optimizationsCode optimization
sCode optimization
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
Code optimization
Code optimizationCode optimization
Code optimization
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
 
Compiler presention
Compiler presentionCompiler presention
Compiler presention
 
Code Optimizatoion
Code OptimizatoionCode Optimizatoion
Code Optimizatoion
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Lecture1
Lecture1Lecture1
Lecture1
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
Optimization
OptimizationOptimization
Optimization
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Lecture1
Lecture1Lecture1
Lecture1
 
Prog1-L2.pptx
Prog1-L2.pptxProg1-L2.pptx
Prog1-L2.pptx
 
04 performance
04 performance04 performance
04 performance
 

Dernier

Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
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 17Celine George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
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.pdfPoh-Sun Goh
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 

Dernier (20)

Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
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
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
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
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 

Introduction to code optimization by dipankar

  • 1. Introduction to Code Optimization Dipankar Nalui M.SC. Computer Science Dept. of Computer Science Pondicherry University
  • 2. Compiler • A compiler is a program that accepts a high level language and produces a target language as output (Assembly Language). Compiler Assembly Level High Level Language Language
  • 3.  Phases of Compiler 1) Lexical Analysis 2) Syntax Analysis 3) Semantic Analysis 4) Intermediate Code Generation 5)Code Optimization 6) Code Generation
  • 4.  Code Optimization •Optimization is the process of transforming a piece of code to make more efficient (either in terms of time or space) without changing its output or side-effects.
  • 5. Why optimization is needed ??? • To improve intermediate code • Better target code • Executes Faster • Shorter code • Less power • Complexity : Time, Space & Cost • Efficient memory usage • Better performance.
  • 6.  “Optimization” • The only difference visible to the code’s user should be that it runs faster and/or consumes less memory. • The name implies you are finding an "optimal“ solution— in truth, optimization aims to improve, not perfect, the result.
  • 7. Correctness Above All! • Optimization should not change the correctness of the generated code. • Transforming the code to something that runs faster but incorrectly is of little value. • It is expected that the un-optimized and optimized variants give the same output for all inputs.
  • 8. position = initial + rate * 60 id1 = id2 + id3 * 60 Intermediate Code T1= int-to-float(60) T2=id3*T1 T3=id2+T2 id1=T3 Eliminate int-to-float by replacing 60 by 60.0 Code Optimization T1=id3 * 60.0 Id1=id2 + t1 Code Optimizer can deduce that the conversion of 60 from int to float can be done once.
  • 9.  The principal Sources of Optimization • Some techniques are applied to the intermediate code, to streamline, rearrange, compress, etc. • Control-Flow Analysis • Local Optimizations • Constant Folding • Constant Propagation • Operator Strength Reduction • Copy Propagation • Dead Code Elimination • Common Subexpression Elimination • Global Optimizations, Data-Flow Analysis
  • 10. Copy Propagation Intermediate Code tmp2 = tmp1 ; tmp3 = tmp2 * tmp1; tmp4 = tmp3 ; tmp5 = tmp3 * tmp2 ; c = tmp5 + tmp4 ; The code on the left makes a copy of tmp1 in tmp2 and a copy of tmp3 in tmp4. Code Optimization tmp3 = tmp1 * tmp1 ; tmp5 = tmp3 * tmp1 ; c = tmp5 + tmp3 ; we eliminated those unnecessary copies
  • 11. Dead Code Elimination • If an instruction’s result is never used, the instruction is considered "dead" and can be removed from the instruction stream. • tmp1 = tmp2 + tmp3 ; • if tmp1 is never used again, we can eliminate this instruction. • if tmp1 holds the result of a function call: • tmp1 = LCall _Binky; • Even if tmp1 is never used again, we cannot eliminate the instruction because we can’t be sure that called function has no side-effects.
  • 12.  Common Sub-expression Elimination Two operations are common if they produce the same result. main() { int x, y, z; x = (1+20)* -x; y = x*x+(x/y); y = z = (x/y)/(x*x); } tmp1 = 1 + 20 ; tmp2 = -x ; x = tmp1 * tmp2 ; tmp3 = x * x ; tmp4 = x / y ; y = tmp3 + tmp4 ; tmp5 = x / y ; tmp6 = x * x ; z = tmp5 / tmp6 ; y = z ;
  • 13. Intermediate Code tmp1 = 1 + 20 ; tmp2 = -x ; x = tmp1 * tmp2 ; tmp3 = x * x ; tmp4 = x / y ; y = tmp3 + tmp4 ; tmp5 = x / y ; tmp6 = x * x ; z = tmp5 / tmp6 ; y = z ; Code Optimization tmp2 = -x ; x = 21 * tmp2 ; tmp3 = x * x ; tmp4 = x / y ; y = tmp3 + tmp4 ; tmp5 = x / y ; z = tmp5 / tmp3 ; y = z ;
  • 14. Algebraic Simplification • x+0 = x • 0+x = x • x*1 = x • 1*x = x • 0/x = 0 • x-0 = x • b && true = b • b && false = false • b || true = true • b || false = b
  • 15.  Example of Algebraic Simplification b = 5 + a + 10 ; Intermediate Code _tmp0 = 5 ; _tmp1 = _tmp0 + a ; _tmp2 = _tmp1 + 10 ; b = _tmp2 ; Code Optimization _tmp0 = 15 ; _tmp1 = a + _tmp0 ; b = _tmp1 ;
  • 16.  Constant Folding • Evaluate constant expressions at compile time • Only possible when side-effect freeness guaranteed c:= 1 + 3 c:= 4 true not false
  • 17.  Constant Propagation • Variables that have constant value, e.g. c := 3 • Later uses of c can be replaced by the constant • If no change of c between! b := 3 c := 1 + b d := b + c b := 3 c := 1 + 3 d := 3 + c
  • 18.  Strength Reduction • Replace expensive operations with simpler ones • Example: Multiplications replaced by additions y := x * 2 y := x + x
  • 19.  Conclusion: • Why do we optimize programs? • Is there an optimal optimizer? • Where in a compiler does optimization happen? • Does the optimization capture most of the potential improvement without an unreasonable amount of effort ? • Does the optimization preserve the meaning of the source program ? • Does the optimization reduce the time and space?