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

NCH VideoPad Pro Cracked Version Download

  • 1.
    Code Optimization inCompiler Design • Last Updated: 04 Sep, 2024 • Enhancing performance and efficiency of executable code
  • 2.
    Introduction to CodeOptimization • Improves execution time • Minimizes resource usage • Enhances overall system performance • Applied during compilation without altering program functionality
  • 3.
    Objectives of CodeOptimization • Must preserve program meaning • Increase program speed and performance • Keep compilation time reasonable • Avoid compile-time delays
  • 4.
    When and Whyto 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 CodeOptimization • 1. Machine Independent: • - Improves intermediate code • - No CPU/memory-specific details • 2. Machine Dependent: • - Optimizes target code • - Utilizes registers, absolute memory
  • 6.
    Ways to OptimizeCode • 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 ApplyOptimization? • 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 CodeOptimization • 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 optimizationis vital for efficient compiler design • Includes various techniques (loops, dead code, folding) • Leads to faster, smaller, and efficient code