SlideShare a Scribd company logo
Code Optimization in Compiler
Design
• Last Updated: 04 Sep, 2024
• Enhancing performance and efficiency of
executable code
Introduction to Code Optimization
• Improves execution time
• Minimizes resource usage
• Enhances overall system performance
• Applied during compilation without altering
program functionality
Objectives of Code Optimization
• Must preserve program meaning
• Increase program speed and performance
• Keep compilation time reasonable
• Avoid compile-time delays
When and Why to Optimize?
• Performed at development end-stage
• May reduce readability
• Used to:
• - Reduce space and increase compilation
speed
• - Automate tedious optimization tasks
• - Promote code reusability
Types of Code Optimization
• 1. Machine Independent:
• - Improves intermediate code
• - No CPU/memory-specific details
• 2. Machine Dependent:
• - Optimizes target code
• - Utilizes registers, absolute memory
Ways to Optimize Code
• 1. Compile Time Evaluation
• 2. Variable Propagation
• 3. Constant Propagation
• 4. Constant Folding
• 5. Copy Propagation
• 6. Common Sub Expression Elimination
• 7. Dead Code Elimination
• 8. Unreachable Code Elimination
Examples of Optimization
• Compile Time Eval:
• A = 2*(22.0/7.0)*r (computed at compile
time)
• Constant Folding:
• x = 2 * 5 -> x = 10
• Copy Propagation:
• Replace x with a if x = a
Unreachable Code Example
• Before:
• cout << "GFG!";
• return 0;
• cout << num;
• After:
• cout << "GFG!";
• return 0;
Function Optimization Techniques
• Function Inlining
• Function Cloning (e.g., Overloading)
• Strength Reduction:
• a = a * 16 -> a = a << 4
Loop Optimization Techniques
• 1. Code Motion
• 2. Loop Jamming
• 3. Loop Unrolling
Example: Loop Jamming
• Before:
• for(...) x = ...;
• for(...) y = ...;
• After:
• for(...) { x = ...; y = ...; }
Where to Apply Optimization?
• Source Program: Algorithm, loops
• Intermediate Code: Address transformations
• Target Code: Registers, select/move
instructions
Optimization Levels
• Local: Basic blocks (e.g., Value Numbering)
• Regional: Extended blocks (e.g., Loop
Unrolling)
• Global: Functions and loops (e.g., Live Var
Analysis)
• Interprocedural: Across procedures (e.g.,
Inline Substitution)
Advantages of Code Optimization
• Better performance and speed
• Reduced code size and power usage
• Improved portability
• Easier maintenance
Disadvantages of Code
Optimization
• Increases compilation time
• Adds complexity
• Risk of bugs
• Hard to assess benefits
Conclusion
• Code optimization is vital for efficient compiler
design
• Includes various techniques (loops, dead code,
folding)
• Leads to faster, smaller, and efficient code

More Related Content

PDF
Optimization in Programming languages
PPT
Code Optimization Lec#7.ppt Code Optimizer
PPTX
Compiler optimization techniques
PPTX
Fundamental principles of optimization and loop optimization techniques
PPT
basics of optimizations presentation s
PPTX
Code Optimization
PPTX
sCode optimization
PDF
Embedded C - Optimization techniques
Optimization in Programming languages
Code Optimization Lec#7.ppt Code Optimizer
Compiler optimization techniques
Fundamental principles of optimization and loop optimization techniques
basics of optimizations presentation s
Code Optimization
sCode optimization
Embedded C - Optimization techniques

Similar to Code_Optimization_Compiler_Design software .pptx (20)

PPTX
Compiler presention
PPTX
Code Optimization In Code Generator In Compiler Design Subject.pptx
PPTX
Code optmize.pptx which is related to coding
PDF
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
PDF
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
PDF
SPCC_Sem6_Chapter 6_Code Optimization part
PPT
Compiler optimization
PPTX
Introduction to code optimization by dipankar
PPTX
compiler design-Intermediate code generation.pptx
PPTX
Code optimization
PPTX
Code optimization
PPTX
complier design unit 5 for helping students
PPTX
Principal Sources of Optimization in compiler design
PPTX
Code optimization
PPTX
Code optimization
PPTX
Compiler Optimization Presentation
PDF
Code Optimizatoion
PDF
Compiler Design- Machine Independent Optimizations
PDF
Performance_Programming
PPTX
Principal source of optimization in compiler design
Compiler presention
Code Optimization In Code Generator In Compiler Design Subject.pptx
Code optmize.pptx which is related to coding
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
SPCC_Sem6_Chapter 6_Code Optimization part
Compiler optimization
Introduction to code optimization by dipankar
compiler design-Intermediate code generation.pptx
Code optimization
Code optimization
complier design unit 5 for helping students
Principal Sources of Optimization in compiler design
Code optimization
Code optimization
Compiler Optimization Presentation
Code Optimizatoion
Compiler Design- Machine Independent Optimizations
Performance_Programming
Principal source of optimization in compiler design
Ad

Recently uploaded (20)

PDF
Operating System & Kernel Study Guide-1 - converted.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Construction Project Organization Group 2.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PPT
Project quality management in manufacturing
PPTX
OOP with Java - Java Introduction (Basics)
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
composite construction of structures.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Construction Project Organization Group 2.pptx
Internet of Things (IOT) - A guide to understanding
Project quality management in manufacturing
OOP with Java - Java Introduction (Basics)
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mechanical Engineering MATERIALS Selection
Lesson 3_Tessellation.pptx finite Mathematics
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
bas. eng. economics group 4 presentation 1.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Structs to JSON How Go Powers REST APIs.pdf
composite construction of structures.pdf
Ad

Code_Optimization_Compiler_Design software .pptx

  • 1. Code Optimization in Compiler Design • Last Updated: 04 Sep, 2024 • Enhancing performance and efficiency of executable code
  • 2. Introduction to Code Optimization • Improves execution time • Minimizes resource usage • Enhances overall system performance • Applied during compilation without altering program functionality
  • 3. Objectives of Code Optimization • Must preserve program meaning • Increase program speed and performance • Keep compilation time reasonable • Avoid compile-time delays
  • 4. When and Why to Optimize? • Performed at development end-stage • May reduce readability • Used to: • - Reduce space and increase compilation speed • - Automate tedious optimization tasks • - Promote code reusability
  • 5. Types of Code Optimization • 1. Machine Independent: • - Improves intermediate code • - No CPU/memory-specific details • 2. Machine Dependent: • - Optimizes target code • - Utilizes registers, absolute memory
  • 6. Ways to Optimize Code • 1. Compile Time Evaluation • 2. Variable Propagation • 3. Constant Propagation • 4. Constant Folding • 5. Copy Propagation • 6. Common Sub Expression Elimination • 7. Dead Code Elimination • 8. Unreachable Code Elimination
  • 7. Examples of Optimization • Compile Time Eval: • A = 2*(22.0/7.0)*r (computed at compile time) • Constant Folding: • x = 2 * 5 -> x = 10 • Copy Propagation: • Replace x with a if x = a
  • 8. Unreachable Code Example • Before: • cout << "GFG!"; • return 0; • cout << num; • After: • cout << "GFG!"; • return 0;
  • 9. Function Optimization Techniques • Function Inlining • Function Cloning (e.g., Overloading) • Strength Reduction: • a = a * 16 -> a = a << 4
  • 10. Loop Optimization Techniques • 1. Code Motion • 2. Loop Jamming • 3. Loop Unrolling
  • 11. Example: Loop Jamming • Before: • for(...) x = ...; • for(...) y = ...; • After: • for(...) { x = ...; y = ...; }
  • 12. Where to Apply Optimization? • Source Program: Algorithm, loops • Intermediate Code: Address transformations • Target Code: Registers, select/move instructions
  • 13. Optimization Levels • Local: Basic blocks (e.g., Value Numbering) • Regional: Extended blocks (e.g., Loop Unrolling) • Global: Functions and loops (e.g., Live Var Analysis) • Interprocedural: Across procedures (e.g., Inline Substitution)
  • 14. Advantages of Code Optimization • Better performance and speed • Reduced code size and power usage • Improved portability • Easier maintenance
  • 15. Disadvantages of Code Optimization • Increases compilation time • Adds complexity • Risk of bugs • Hard to assess benefits
  • 16. Conclusion • Code optimization is vital for efficient compiler design • Includes various techniques (loops, dead code, folding) • Leads to faster, smaller, and efficient code