Peephole optimization is an optimization technique performed on a small set of compiler-generated instructions, known as a peephole or window, that involves replacing the instructions with a logically equivalent set that has better performance.
For example:
The term peephole optimization was introduced by William Marshall McKeeman in 1965.
Peephole optimization replacements include but are not limited to:
Modern compilers often implement peephole optimizations with a pattern-matching algorithm.
The following Java bytecode:
aload 1 aload 1 mul
can be replaced with the following, which executes faster:
aload 1 dup mul
As for most peephole optimizations, this is based on the relative efficiency of different instructions. In this case, <code>dup</code> (which duplicates and pushes the top of the stack) is known/assumed to be more efficient than <code>aload</code> (which loads a local variable and pushes it onto the stack).
The following source code:
a = b + c; d = a + e;
is straightforwardly compiled to
but can be optimized to
If the compiler saves registers on the stack before calling a subroutine and restores them when returning, consecutive calls to subroutines may have redundant stack instructions.
Suppose the compiler generates the following Z80 instructions for each procedure call:
If there were two consecutive subroutine calls, they would look like this:
The sequence <code>POP regs</code> followed by <code>PUSH</code> for the same registers is generally redundant. In cases where it is redundant, a peephole optimization would remove these instructions. In the example, this would cause another redundant <code>POP</code>/<code>PUSH</code> pair to appear in the peephole, and these would be removed in turn. Assuming that subroutine <code>_ADDR2</code> does not depend on previous register values, removing all of the redundant code in the example above would eventually leave the following code: