Peephole Optimization in Compiler Design

Peephole Optimization in Compiler Design

[21]

It is a simple and effective technique for locally improving target 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. Peephole optimization typically occurs during the code generation phase of the compilation process. It's one of the final steps before the target machine code is generated. ‘Peephole’ refers to the small window of instructions (usually 3-5) that the optimizer examines at a time. The optimizer "peeks" at this small set of instructions to identify and apply optimizations.

Objectives of Peephole Optimization in Compiler Design

  • Increasing code speed: Peephole optimization seeks to improve the execution speed of generated code by removing redundant instructions or unnecessary instructions.

  • Reduced code size: Peephole optimization seeks to reduce generated code size by replacing the long sequence of instructions with shorter ones.

  • Getting rid of dead code: Peephole optimization seeks to get rid of dead code, such as unreachable code, redundant assignments, or constant expressions that have no effect on the output of the program.

  • Simplifying code: Peephole optimization also seeks to make generated code more understandable and manageable by removing unnecessary complexities.

Characteristics of Peephole Optimization

Redundant Instruction 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;

Flow of control optimization

Unnecessary jumps on jumps can be eliminated.

if a < b goto test
...
test: goto done
...
done:

Can be written as:

if a < b goto done
...
test: goto done
...
done:

Algebraic Simplification and Strength Reduction

Simplification and reordering of the instructions using algebraic laws.

For example, r1 := r2

r3 := r1

becomes r3 := r2

Another example, statements such as x := x+0 or x := x * 1 are often produced by straightforward intermediate code-generation algorithms, and they can be eliminated easily through peephole optimization.

Reduction-in-strength transformations can be applied in the peephole to replace expensive operations by equivalent cheaper ones on the target machine. Certain machine instructions are considerably cheaper than others and can often be used as special cases of more expensive operators. For example, x2 is invariably cheaper to implement as x * x than as a call to an exponentiation routine.

Use of machine idioms

The target machine may have hardware instructions to implement certain specific operations efficiently. Detecting situations that permit the use of these instructions can reduce execution time significantly. For example, some machines have auto-increment and auto-decrement addressing modes. These add or subtract one from an operand before or after using its value. The use of the modes greatly improves the quality of code when pushing or popping a stack, as in parameter passing. These modes can also be used in code for statements like x = x + 1.

Conculsion

Peephole optimization has a limited scope. It only examines small windows of instructions and may not be able to detect or optimize more complex patterns that require a broader view of the code. The effectiveness of peephole optimization can vary significantly depending on the target machine architecture and its instruction set. Despite its limitations, peephole optimization is a valuable technique for enhancing code quality. By focusing on local improvements and addressing common inefficiencies, it contributes significantly to generating more efficient and compact machine code, ultimately improving program performance.