Professional Documents
Culture Documents
Peephole Optimization - Wikipedia
Peephole Optimization - Wikipedia
optimization
Peephole optimization involves changing the small set of instructions to an equivalent set that
has better performance.
For example:
instead of pushing register A onto the stack and then immediately popping the value back into
register A, a peephole optimization would remove both instructions;
instead of multiplying a floating point register by 8, a peephole optimization might scale the
floating point register's exponent by 3; and
instead of multiplying an index by 4, adding the result to a base address to get a pointer value,
and then dereferencing the pointer, a peephole optimization might use a hardware addressing
mode that accomplishes the same result with one instruction.
The term peephole optimization was introduced by William Marshall McKeeman in 1965.[2]
Replacement rules
Special case instructions – Use instructions designed for special operand cases.
Examples
...
aload 1
aload 1
mul
...
can be replaced by
...
aload 1
dup
mul
...
This kind of optimization, like most peephole optimizations, makes certain assumptions about
the efficiency of instructions. For instance, in this case, it is assumed that the dup operation
(which duplicates and pushes the top of the stack) is more efficient than the aload X
operation (which loads a local variable identified as X and pushes it on the stack).
a = b + c;
d = a + e;
is straightforwardly implemented as
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:
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
Implementation
Modern compilers often implement peephole optimizations with a pattern matching algorithm.[4]
See also
Capex Corporation – produced the COBOL optimizer, an early mainframe object code
optimizer for IBM Cobol
Superoptimization
References
1. Muchnick, Steven Stanley (1997-08-15). Advanced Compiler Design and Implementation (https://books.g
oogle.com/books?id=Pq7pHwG1_OkC&printsec=frontcover#v=onepage&q=peephole&f=false) .
Academic Press / Morgan Kaufmann. ISBN 978-1-55860-320-2.
2. McKeeman, William Marshall (July 1965). "Peephole optimization". Communications of the ACM. 8 (7):
443–444. doi:10.1145/364995.365000 (https://doi.org/10.1145%2F364995.365000) .
3. Fischer, Charles N.; Cytron, Ron K.; LeBlanc, Jr., Richard J. (2010). Crafting a Compiler (https://web.archiv
e.org/web/20180703050525/http://bank.engzenon.com/download/560e7301-482c-43fd-9f80-16a9c0fe
b99b/Crafting_a_Compiler_by_Fischer_Cytron_and_LeBlanc.pdf) (PDF). Addison-Wesley. ISBN 978-0-
13-606705-4. Archived from the original (http://bank.engzenon.com/download/560e7301-482c-43fd-9f80
-16a9c0feb99b/Crafting_a_Compiler_by_Fischer_Cytron_and_LeBlanc.pdf) (PDF) on 2018-07-03.
Retrieved 2018-07-02.
4. Aho, Alfred Vaino; Lam, Monica Sin-Ling; Sethi, Ravi; Ullman, Jeffrey David (2007). "Chapter 8.9.2 Code
Generation by Tiling an Input Tree". Compilers – Principles, Techniques, & Tools (http://www.informatik.u
ni-bremen.de/agbkb/lehre/ccfl/Material/ALSUdragonbook.pdf) (PDF) (2 ed.). Pearson Education.
p. 540. Archived (https://web.archive.org/web/20180610190208/http://www.informatik.uni-bremen.de/a
gbkb/lehre/ccfl/Material/ALSUdragonbook.pdf) (PDF) from the original on 2018-06-10. Retrieved
2018-07-02.
External links
Retrieved from
"https://en.wikipedia.org/w/index.php?
title=Peephole_optimization&oldid=1004721145"