Peephole Optimization | Compiler Design - Computer Science Engineering (CSE) PDF Download

PEEPHOLE OPTIMIZATION

  •  A statement-by-statement code-generations strategy often produce target code that contains redundant instructions and suboptimal constructs .The quality of such target code can be improved by applying “optimizing” transformations to the target program.
     
  •  A simple but effective technique for improving the target code is peephole optimization, a method for trying to improving the performance of the target program by examining a short sequence of target instructions (called the peephole) and replacing these instructions by a shorter or faster sequence, whenever possible.  
     
  •  The peephole is a small, moving window on the target program. The code in the peephole need not contiguous, although some implementations do require this.it is characteristic of peephole optimization that each improvement may spawn opportunities for additional improvements.  

 

  • We shall give the following examples of program transformations that are characteristic of peephole optimizations:  
     
  • Redundant-instructions elimination  
     
  • Flow-of-control optimizations
     
  • Algebraic simplifications  
     
  • Use of machine idioms  
     
  • Unreachable Code  

Question for Peephole Optimization
Try yourself:
What is peephole optimization?
View Solution

Redundant Loads And Stores:
If we see the instructions sequence
(1) MOV R0,a
(2) MOV a,R0

we can delete instructions (2) because whenever (2) is executed. (1) will ensure that the value of a is already in register R0.If (2) had a label we could not be sure that (1) was always executed immediately before (2) and so we could not remove (2).
 

Unreachable Code:

  • · Another opportunity for peephole optimizations is the removal of unreachable instructions. An unlabeled instruction immediately following an unconditional jump may be removed. This operation can be repeated to eliminate a sequence of instructions. For example, for debugging purposes, a large program may have within it certain segments that are executed only if a variable debug is 1. In C, the source code might look like:  

    #define debug 0 ….

If ( debug ) { Print  debugging information

}

In the intermediate representations the if-statement may be translated as:

debug =1 goto L2 goto  L2

L1: print debugging information

L2:…………………………(a)

  • One obvious peephole optimization is to eliminate jumps over jumps .Thus no matter what the value of debug; (a) can be replaced by:   If debug ≠1 goto L2

    Print debugging information L2:……………………………(b)
     
  • As the argument of the statement of (b) evaluates to a constant true it can be replaced by If debug ≠0 goto L2 Print debugging information L2: ……………………………(c)
     

· As the argument of the first statement of (c) evaluates to a constant true, it can be replaced by goto L2. Then all the statement that print debugging aids are manifestly  unreachable and can be eliminated one at a time.
 

Flows-Of-Control Optimizations:

  • The unnecessary jumps can be eliminated in either the intermediate code or the target code by the following types of peephole optimizations. We can replace the jump sequence  

goto L1 ….

L1: gotoL2 by the sequence goto  L2 ….

L1: goto  L2

  • If there are now no jumps to L1, then it may be possible to eliminate the statement L1:goto L2 provided it is preceded by an unconditional jump .Similarly, the sequence  

 
if a < b  goto L1   ….

L1: goto L2 can be replaced by Ifa < b goto L2 ….

L1: goto L2

  •  Finally, suppose there is only one jump to L1 and L1 is preceded by an unconditional goto. Then the sequence  goto L1 ……..


L1: if a <b goto L2 L3:…………………………………..(1)

  •  Maybe replaced by Ifa<b goto L2  

 
goto L3   …….  
 

The document Peephole Optimization | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Peephole Optimization - Compiler Design - Computer Science Engineering (CSE)

1. What is peephole optimization in computer science engineering?
Peephole optimization in computer science engineering is a technique used to improve the efficiency of code by analyzing a small window or "peephole" of instructions and applying optimizations based on patterns within that window. It involves identifying and replacing suboptimal code sequences with more efficient alternatives.
2. How does peephole optimization work?
Peephole optimization works by analyzing a small window of instructions in the code and identifying patterns or sequences that can be optimized. It typically involves searching for specific combinations of instructions that can be replaced with more efficient alternatives. The optimizations can include removing redundant instructions, simplifying expressions, or reordering instructions for better performance.
3. What are the benefits of peephole optimization in computer science engineering?
Peephole optimization offers several benefits in computer science engineering. It can significantly improve the performance of code by eliminating unnecessary instructions and reducing the number of cycles required to execute the code. This optimization technique also helps in minimizing resource usage, such as memory and processor power, leading to faster and more efficient execution of programs.
4. Can peephole optimization lead to incorrect program behavior?
Yes, there is a possibility that peephole optimization can lead to incorrect program behavior if not implemented carefully. The optimizations made by the compiler may alter the original behavior of the code, causing unexpected results. Therefore, it is crucial to thoroughly test the optimized code and ensure that it still produces the desired output and behaves correctly in all scenarios.
5. Are there any limitations or considerations to keep in mind when using peephole optimization?
Yes, there are some limitations and considerations to keep in mind when using peephole optimization. Firstly, the effectiveness of peephole optimization depends on the size of the window being analyzed. A larger window can capture more complex patterns but may also increase the computational cost. Additionally, peephole optimization is most effective for low-level code and may have limited impact on high-level language constructs. It is important to strike a balance between optimization and maintaining code readability and maintainability.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

shortcuts and tricks

,

Semester Notes

,

Exam

,

Important questions

,

Peephole Optimization | Compiler Design - Computer Science Engineering (CSE)

,

past year papers

,

Viva Questions

,

pdf

,

study material

,

Peephole Optimization | Compiler Design - Computer Science Engineering (CSE)

,

Summary

,

Sample Paper

,

Extra Questions

,

practice quizzes

,

ppt

,

Objective type Questions

,

Previous Year Questions with Solutions

,

Peephole Optimization | Compiler Design - Computer Science Engineering (CSE)

,

video lectures

,

MCQs

,

Free

;