Peephole optimization is a type of Code Optimization performed on a small part of the code. It is performed on the very small set of instructions in a segment of code.
The small set of instructions or small part of code on which peephole optimization is performed is known as peephole or window.
It basically works on the theory of replacement in which a part of code is replaced by shorter and faster code without change in output.
Peephole is the machine dependent optimization.
Objectives of Peephole Optimization:
The objective of peephole optimization is:
- To improve performance
- To reduce memory footprint
- To reduce code size
Peephole Optimization Techniques:
- Redundant load and store elimination:
In this technique the redundancy is eliminated.
Initial code: y = x + 5; i = y; z = i; w = z * 3; Optimized code: y = x + 5; i = y; w = y * 3;
- Constant folding:
The code that can be simplified by user itself, is simplified.
Initial code: x = 2 * 3; Optimized code: x = 6;
- Strength Reduction:
The operators that consume higher execution time are replaced by the operators consuming less execution time.
Initial code: y = x * 2; Optimized code: y = x + x; or y = x << 1; Initial code: y = x / 2; Optimized code: y = x >> 1;
- Null sequences:
Useless operations are deleted.
- Combine operations:
Several operations are replaced by a single equivalent operation.
- Loop Optimization in Compiler Design
- Code Optimization in Compiler Design
- Why FIRST and FOLLOW in Compiler Design?
- Bootstrapping in Compiler Design
- Introduction of Compiler Design
- Semantic Analysis in Compiler Design
- Runtime Environments in Compiler Design
- Types of Parsers in Compiler Design
- Basic Blocks in Compiler Design
- Error Handling in Compiler Design
- Input Buffering in Compiler Design
- Labeling Algorithm in Compiler Design
- Transition diagram for Identifiers in Compiler Design
- Compiler Design | Syntax Directed Definition
- Introduction of Object Code in Compiler Design
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai