Peephole optimization is an
optimization technique performed on a small set of
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
-generated instructions, known as a peephole or window,
that involves replacing the instructions with a logically equivalent set that has better performance.
For example:
* Instead of pushing a register onto the stack and then immediately popping the value back into the register, remove both instructions
* Instead of multiplying ''x'' by 2, do
* Instead of multiplying a floating point register by 8, add 3 to the floating point register's exponent
The term ''peephole optimization'' was introduced by William Marshall McKeeman in 1965.
Replacements
Peephole optimization replacements include but are not limited to:
* Null sequences – Delete useless operations
* Combine operations – Replace several operations with one equivalent
* Algebraic laws – Use algebraic laws to simplify or reorder instructions
* Special case instructions – Use instructions designed for special operand cases
* Address mode operations – Use address modes to simplify code
Implementation
Modern compilers often implement peephole optimizations with a
pattern matching
In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern. In contrast to pattern recognition, the match usually must be exact: "either it will or will not be a ...
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
.
Examples
Replacing slow instructions with faster ones
The following
Java bytecode
Java bytecode is the instruction set of the Java virtual machine (JVM), the language to which Java and other JVM-compatible source code is compiled. Each instruction is represented by a single byte, hence the name bytecode, making it a compact ...
:
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,
dup
(which duplicates and pushes the top of the
stack
Stack may refer to:
Places
* Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group
* Blue Stack Mountains, in Co. Donegal, Ireland
People
* Stack (surname) (including a list of people ...
) is known/assumed to be more efficient than
aload
(which loads a local
variable and pushes it onto the stack).
Removing redundant code
The following
source code
In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer.
Since a computer, at base, only ...
:
a = b + c;
d = a + e;
is straightforwardly compiled to:
MOV b, R0 ; Copy b to the register
ADD c, R0 ; Add c to the register, the register is now b+c
MOV R0, a ; Copy the register to a
MOV a, R0 ; Copy a to the register
ADD e, R0 ; Add e to the register, the register is now a+e b+c)+eMOV R0, d ; Copy the register to d
but can be optimized to:
MOV b, R0 ; Copy b to the register
ADD c, R0 ; Add c to the register, which is now b+c (a)
MOV R0, a ; Copy the register to a
ADD e, R0 ; Add e to the register, which is now b+c+e a)+eMOV R0, d ; Copy the register to d
Removing redundant stack instructions
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
The Zilog Z80 is an 8-bit microprocessor designed by Zilog that played an important role in the evolution of early personal computing. Launched in 1976, it was designed to be software-compatible with the Intel 8080, offering a compelling altern ...
instructions for each procedure call:
PUSH AF
PUSH BC
PUSH DE
PUSH HL
CALL _ADDR
POP HL
POP DE
POP BC
POP AF
If there were two consecutive subroutine calls, they would look like this:
PUSH AF
PUSH BC
PUSH DE
PUSH HL
CALL _ADDR1
POP HL
POP DE
POP BC
POP AF
PUSH AF
PUSH BC
PUSH DE
PUSH HL
CALL _ADDR2
POP HL
POP DE
POP BC
POP AF
The sequence POP regs followed by PUSH 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 POP/PUSH pair to appear in the peephole, and these would be removed in turn. Assuming that subroutine _ADDR2 does not depend on previous register values, removing all of the
redundant code in the example above would eventually leave the following code:
PUSH AF
PUSH BC
PUSH DE
PUSH HL
CALL _ADDR1
CALL _ADDR2
POP HL
POP DE
POP BC
POP AF
See also
*
Object code optimizers, discussion in relation to general
algorithmic efficiency
In computer science, algorithmic efficiency is a property of an algorithm which relates to the amount of computational resources used by the algorithm. Algorithmic efficiency can be thought of as analogous to engineering productivity for a repea ...
*
Capex Corporation – produced the
COBOL
COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural, and, since 2002, object-oriented language. COBOL is primarily ...
optimizer, an early mainframe
object code optimizer for
IBM
International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American Multinational corporation, multinational technology company headquartered in Armonk, New York, and present in over 175 countries. It is ...
Cobol
*
Superoptimization
*
Digital Research XLT86, an optimizing assembly source-to-source compiler
References
External links
The copt general-purpose peephole optimizer by Christopher W. FraserThe original paper
{{Compiler optimizations
Compiler optimizations