SlideShare une entreprise Scribd logo
1  sur  17
BIRLA INSTITUTE OF TECHNOLOGY
MESRA, JAIPUR CAMPUS
TOPIC : PEEPHOLE OPTIMIZATION
BY:
MEGHAJ KUMAR MALLICK
(MCA/25017/18)
2ND YEAR 3RD SEMESTER
CONTENTS
2
What is an Optimization
Introduction to Peephole Optimization
Working Flow of Peephole Optimization
Replacement Rules
Functioning of Peephole Optimization & Example
Conclusion
References
What is Optimization ??
 Process of transforming a piece of code to make it more efficient (eitherin
terms of time or space) without changing its output or side-effects.
 Tries to minimize or maximize some attributes of an executable computer
program.
Introduction to Peephole
Optimization ??
 Optimization performed over a very small set of
instructions in a segment of generated code.
 Works by recognizing sets of instructions that can be
replaced by shorter or faster sets of instructions.
 Goals:
 improve performance
 reduce code size
 reduce memory footprint
Why it’s needed ??
After compilation, the code still has some flaws
 Scheduling & allocation really are NP-Complete
 Optimizer may not implement every needed transformation
Curing the problem
 More work on scheduling and allocation
 Implement more optimizations
— or —
 Optimize after compilation
 Peephole optimization
 Link-time optimization
Working Flow of Peephole
Optimization
Replacement Rules
Common techniques applied in peephole optimization:-
 Constant folding
– Evaluate constant sub-expressions in advance.
 Strength reduction
– Replace slow operations with faster equivalents.
 Null sequences
– Delete useless operations.
 Combine operations
– Replace several operations with one equivalent.
 Algebraic laws
– Use algebraic laws to simplify or reorder instructions.
 Special case instructions
– Use instructions designed for special operand cases.
 Address mode operations
– Use address modes to simplify code.
Functioning of Peephole
Optimization
 Replacing slow instructions with faster ones
 Removing redundant code
 Removing redundant stack instructions
Functioning (Contd…)
 Replacing slow instructions with faster ones
The following Java byteCode
...
load 1
load 1
mul
...
Can be replaced by
...
load 1
dup
mul
...
Functioning (Contd…)
 Removing redundant code
 Another example is to eliminate redundant load stores.
a = b + c;
d = a + e;
is straightforwardly implemented as
MOV b,
ADD c,
MOV R0, a
MOV a,
ADD e,
MOV R0, d
R0 # Copy b to the register
R0 # Add c to the register, the register is now b+c
# Copy the register to a
R0 # Copy a to the register
R0 # Add e to the register, the register is now a+e [(b+c)+e]
# Copy the register to d
Functioning (Contd…)
 Removing redundant code
but can be optimized to
MOV b,
ADD c,
MOV R0, a
ADD e, R0
MOV R0, d
R0 # Copy b to the register
R0 # Add c to the register, which is now b+c (a)
# Copy the register to a
# Add e to the register, which is now b+c+e [(a)+e]
# Copy the register to d
Functioning (Contd…)
 Removing redundant stack instructions
PUSH AF PUSH AF
PUSH BC PUSH BC
PUSH DE PUSH DE
PUSH HL PUSH HL
POP HL POP HL
POP DE POP DE
POP BC POP BC
POP AF POP AF
CALL _ADDR1 CALL _ADDR2 
PUSH AF
PUSH BC
PUSH DE
PUSH HL
CALL _ADDR1
CALL_ADDR2
POP HL
POP DE
POP BC
POP AF
Functioning (Contd…)
Source Code:
If ( a<b ) {
a = a + b;
}
 Peephole optimization ofjumps
 Eliminate jumps to jumps
 Eliminate jumps after conditional branches
“Adjacent” instructions = “Adjacent in control flow”
IL Code:
PUSH a
PUSH b
CMP a,b
JUMP L1
EXIT
L1:
ADD a,b

Other Considerations
Control-flow operations
 Can clear simplifier’s window at branch or label
 More aggressive approach: combine across branches
 Same considerations arise with predication
Physical versus logical windows
 Can run optimizer over a logical window
 Logical windows (within block) improve effectiveness
Conclusion
So…
 Peephole optimization remains viable
 Post allocation improvements
 Cleans up rough edges
 Peephole technology works for selection
 Description driven matchers
 Used in several important systems
All of this will work equally well in binary-to-binary translation
References
 https://en.wikipedia.org/wiki/Peephole_optimization
 http://www.iosrjournals.org/iosr-jce/papers/Vol9-Issue4/N0948086.pdf?id=255
 https://class.coursera.org/compilers/lecture/76
 https://www.slideshare.net/AnulChaudhary/peephole-optimization-techniques-in-
compiler-design
Peephole Optimization

Contenu connexe

Tendances

Hardware multithreading
Hardware multithreadingHardware multithreading
Hardware multithreading
Fraboni Ec
 
ENCAPSULATION AND TUNNELING
ENCAPSULATION AND TUNNELINGENCAPSULATION AND TUNNELING
ENCAPSULATION AND TUNNELING
Mohammad Adil
 

Tendances (20)

Reference Model of Real Time System
Reference Model of Real Time SystemReference Model of Real Time System
Reference Model of Real Time System
 
Tree topology
Tree topologyTree topology
Tree topology
 
Ppt of routing protocols
Ppt of routing protocolsPpt of routing protocols
Ppt of routing protocols
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
Computer architecture register transfer languages rtl
Computer architecture register transfer languages rtlComputer architecture register transfer languages rtl
Computer architecture register transfer languages rtl
 
Clock-8086 bus cycle
Clock-8086 bus cycleClock-8086 bus cycle
Clock-8086 bus cycle
 
TCP/ IP
TCP/ IP TCP/ IP
TCP/ IP
 
Process state in OS
Process state in OSProcess state in OS
Process state in OS
 
Operating System-Process Scheduling
Operating System-Process SchedulingOperating System-Process Scheduling
Operating System-Process Scheduling
 
Hardware multithreading
Hardware multithreadingHardware multithreading
Hardware multithreading
 
FAST ETHERNET
FAST ETHERNET FAST ETHERNET
FAST ETHERNET
 
Round Robin Scheduling Algorithm
Round Robin Scheduling AlgorithmRound Robin Scheduling Algorithm
Round Robin Scheduling Algorithm
 
Operating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management ConceptsOperating Systems 1 (9/12) - Memory Management Concepts
Operating Systems 1 (9/12) - Memory Management Concepts
 
Multiprotocol label switching (mpls) - Networkshop44
Multiprotocol label switching (mpls)  - Networkshop44Multiprotocol label switching (mpls)  - Networkshop44
Multiprotocol label switching (mpls) - Networkshop44
 
Dhcp
DhcpDhcp
Dhcp
 
Ether channel fundamentals
Ether channel fundamentalsEther channel fundamentals
Ether channel fundamentals
 
ENCAPSULATION AND TUNNELING
ENCAPSULATION AND TUNNELINGENCAPSULATION AND TUNNELING
ENCAPSULATION AND TUNNELING
 
Process scheduling
Process schedulingProcess scheduling
Process scheduling
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
chapter 2 architecture
chapter 2 architecturechapter 2 architecture
chapter 2 architecture
 

Similaire à Peephole Optimization

EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
PRADEEP
 
May2010 hex-core-opt
May2010 hex-core-optMay2010 hex-core-opt
May2010 hex-core-opt
Jeff Larkin
 
Cray XT Porting, Scaling, and Optimization Best Practices
Cray XT Porting, Scaling, and Optimization Best PracticesCray XT Porting, Scaling, and Optimization Best Practices
Cray XT Porting, Scaling, and Optimization Best Practices
Jeff Larkin
 

Similaire à Peephole Optimization (20)

Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Compiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flatteningCompiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flattening
 
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal ShahzadFunction Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
Function Overloading,Inline Function and Recursion in C++ By Faisal Shahzad
 
Optimization Techniques
Optimization TechniquesOptimization Techniques
Optimization Techniques
 
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
 
Computer Architecture Assignment Help
Computer Architecture Assignment HelpComputer Architecture Assignment Help
Computer Architecture Assignment Help
 
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization ApproachesPragmatic Optimization in Modern Programming - Ordering Optimization Approaches
Pragmatic Optimization in Modern Programming - Ordering Optimization Approaches
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
May2010 hex-core-opt
May2010 hex-core-optMay2010 hex-core-opt
May2010 hex-core-opt
 
Cray XT Porting, Scaling, and Optimization Best Practices
Cray XT Porting, Scaling, and Optimization Best PracticesCray XT Porting, Scaling, and Optimization Best Practices
Cray XT Porting, Scaling, and Optimization Best Practices
 
Compiler optimization techniques
Compiler optimization techniquesCompiler optimization techniques
Compiler optimization techniques
 
Matopt
MatoptMatopt
Matopt
 
Compiler presention
Compiler presentionCompiler presention
Compiler presention
 
ISA.pptx
ISA.pptxISA.pptx
ISA.pptx
 
Performance tips for Symfony2 & PHP
Performance tips for Symfony2 & PHPPerformance tips for Symfony2 & PHP
Performance tips for Symfony2 & PHP
 

Plus de Meghaj Mallick

Plus de Meghaj Mallick (20)

24 partial-orderings
24 partial-orderings24 partial-orderings
24 partial-orderings
 
PORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSSPORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSS
 
Introduction to Software Testing
Introduction to Software TestingIntroduction to Software Testing
Introduction to Software Testing
 
Introduction to System Programming
Introduction to System ProgrammingIntroduction to System Programming
Introduction to System Programming
 
MACRO ASSEBLER
MACRO ASSEBLERMACRO ASSEBLER
MACRO ASSEBLER
 
Icons, Image & Multimedia
Icons, Image & MultimediaIcons, Image & Multimedia
Icons, Image & Multimedia
 
Project Tracking & SPC
Project Tracking & SPCProject Tracking & SPC
Project Tracking & SPC
 
Routing in MANET
Routing in MANETRouting in MANET
Routing in MANET
 
Macro assembler
 Macro assembler Macro assembler
Macro assembler
 
Architecture and security in Vanet PPT
Architecture and security in Vanet PPTArchitecture and security in Vanet PPT
Architecture and security in Vanet PPT
 
Design Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software EngineeringDesign Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software Engineering
 
Text Mining of Twitter in Data Mining
Text Mining of Twitter in Data MiningText Mining of Twitter in Data Mining
Text Mining of Twitter in Data Mining
 
DFS & BFS in Computer Algorithm
DFS & BFS in Computer AlgorithmDFS & BFS in Computer Algorithm
DFS & BFS in Computer Algorithm
 
Software Development Method
Software Development MethodSoftware Development Method
Software Development Method
 
Secant method in Numerical & Statistical Method
Secant method in Numerical & Statistical MethodSecant method in Numerical & Statistical Method
Secant method in Numerical & Statistical Method
 
Motivation in Organization
Motivation in OrganizationMotivation in Organization
Motivation in Organization
 
Communication Skill
Communication SkillCommunication Skill
Communication Skill
 
Partial-Orderings in Discrete Mathematics
 Partial-Orderings in Discrete Mathematics Partial-Orderings in Discrete Mathematics
Partial-Orderings in Discrete Mathematics
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Complexity Analysis of Recursive Function
Complexity Analysis of Recursive FunctionComplexity Analysis of Recursive Function
Complexity Analysis of Recursive Function
 

Dernier

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Dernier (20)

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Ữ Â...
 
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
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
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
 
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
 
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
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
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
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
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
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
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...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
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.
 

Peephole Optimization

  • 1. BIRLA INSTITUTE OF TECHNOLOGY MESRA, JAIPUR CAMPUS TOPIC : PEEPHOLE OPTIMIZATION BY: MEGHAJ KUMAR MALLICK (MCA/25017/18) 2ND YEAR 3RD SEMESTER
  • 2. CONTENTS 2 What is an Optimization Introduction to Peephole Optimization Working Flow of Peephole Optimization Replacement Rules Functioning of Peephole Optimization & Example Conclusion References
  • 3. What is Optimization ??  Process of transforming a piece of code to make it more efficient (eitherin terms of time or space) without changing its output or side-effects.  Tries to minimize or maximize some attributes of an executable computer program.
  • 4. Introduction to Peephole Optimization ??  Optimization performed over a very small set of instructions in a segment of generated code.  Works by recognizing sets of instructions that can be replaced by shorter or faster sets of instructions.  Goals:  improve performance  reduce code size  reduce memory footprint
  • 5. Why it’s needed ?? After compilation, the code still has some flaws  Scheduling & allocation really are NP-Complete  Optimizer may not implement every needed transformation Curing the problem  More work on scheduling and allocation  Implement more optimizations — or —  Optimize after compilation  Peephole optimization  Link-time optimization
  • 6. Working Flow of Peephole Optimization
  • 7. Replacement Rules Common techniques applied in peephole optimization:-  Constant folding – Evaluate constant sub-expressions in advance.  Strength reduction – Replace slow operations with faster equivalents.  Null sequences – Delete useless operations.  Combine operations – Replace several operations with one equivalent.  Algebraic laws – Use algebraic laws to simplify or reorder instructions.  Special case instructions – Use instructions designed for special operand cases.  Address mode operations – Use address modes to simplify code.
  • 8. Functioning of Peephole Optimization  Replacing slow instructions with faster ones  Removing redundant code  Removing redundant stack instructions
  • 9. Functioning (Contd…)  Replacing slow instructions with faster ones The following Java byteCode ... load 1 load 1 mul ... Can be replaced by ... load 1 dup mul ...
  • 10. Functioning (Contd…)  Removing redundant code  Another example is to eliminate redundant load stores. a = b + c; d = a + e; is straightforwardly implemented as MOV b, ADD c, MOV R0, a MOV a, ADD e, MOV R0, d R0 # Copy b to the register R0 # Add c to the register, the register is now b+c # Copy the register to a R0 # Copy a to the register R0 # Add e to the register, the register is now a+e [(b+c)+e] # Copy the register to d
  • 11. Functioning (Contd…)  Removing redundant code but can be optimized to MOV b, ADD c, MOV R0, a ADD e, R0 MOV R0, d R0 # Copy b to the register R0 # Add c to the register, which is now b+c (a) # Copy the register to a # Add e to the register, which is now b+c+e [(a)+e] # Copy the register to d
  • 12. Functioning (Contd…)  Removing redundant stack instructions PUSH AF PUSH AF PUSH BC PUSH BC PUSH DE PUSH DE PUSH HL PUSH HL POP HL POP HL POP DE POP DE POP BC POP BC POP AF POP AF CALL _ADDR1 CALL _ADDR2  PUSH AF PUSH BC PUSH DE PUSH HL CALL _ADDR1 CALL_ADDR2 POP HL POP DE POP BC POP AF
  • 13. Functioning (Contd…) Source Code: If ( a<b ) { a = a + b; }  Peephole optimization ofjumps  Eliminate jumps to jumps  Eliminate jumps after conditional branches “Adjacent” instructions = “Adjacent in control flow” IL Code: PUSH a PUSH b CMP a,b JUMP L1 EXIT L1: ADD a,b 
  • 14. Other Considerations Control-flow operations  Can clear simplifier’s window at branch or label  More aggressive approach: combine across branches  Same considerations arise with predication Physical versus logical windows  Can run optimizer over a logical window  Logical windows (within block) improve effectiveness
  • 15. Conclusion So…  Peephole optimization remains viable  Post allocation improvements  Cleans up rough edges  Peephole technology works for selection  Description driven matchers  Used in several important systems All of this will work equally well in binary-to-binary translation
  • 16. References  https://en.wikipedia.org/wiki/Peephole_optimization  http://www.iosrjournals.org/iosr-jce/papers/Vol9-Issue4/N0948086.pdf?id=255  https://class.coursera.org/compilers/lecture/76  https://www.slideshare.net/AnulChaudhary/peephole-optimization-techniques-in- compiler-design