Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Peephole optimization

From Wikipedia, the free encyclopedia
Compiler optimization technique

Peephole optimization is anoptimization technique performed on a small set ofcompiler-generated instructions, known as a peephole or window,[1][2] 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 multiplyingx by 2, dox << 1.
  • Instead of multiplying a floating-point register by 8, add 3 to the floating-point register's exponent.

The termpeephole optimization was introduced by William Marshall McKeeman in 1965.[3]

Replacements

[edit]

Peephole optimization replacements include but are not limited to:[4]

  • 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

[edit]

Modern compilers often implement peephole optimizations with apattern-matchingalgorithm.[5]

Examples

[edit]
icon
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved.(March 2013) (Learn how and when to remove this message)

Replacing slow instructions with faster ones

[edit]

The followingJava bytecode:

aload 1aload 1mul

can be replaced with the following, which executes faster:

aload 1dupmul

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 thestack) is known/assumed to be more efficient thanaload (which loads a localvariable and pushes it onto the stack).

Removing redundant code

[edit]

The followingsource code:

a = b + c;d = a + e;

is straightforwardly compiled to

MOVb,R0; Copy b to the registerADDc,R0; Add  c to the register, the register is now b+cMOVR0,a; Copy the register to aMOVa,R0; Copy a to the registerADDe,R0; Add  e to the register, the register is now a+e [(b+c)+e]MOVR0,d; Copy the register to d

but can be optimized to

MOVb,R0; Copy b to the registerADDc,R0; Add c to the register, which is now b+c (a)MOVR0,a; Copy the register to aADDe,R0; Add e to the register, which is now b+c+e [(a)+e]MOVR0,d; Copy the register to d

Removing redundant stack instructions

[edit]

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 followingZ80 instructions for each procedure call:

PUSHAFPUSHBCPUSHDEPUSHHLCALL_ADDRPOPHLPOPDEPOPBCPOPAF

If there were two consecutive subroutine calls, they would look like this:

PUSHAFPUSHBCPUSHDEPUSHHLCALL_ADDR1POPHLPOPDEPOPBCPOPAFPUSHAFPUSHBCPUSHDEPUSHHLCALL_ADDR2POPHLPOPDEPOPBCPOPAF

The sequencePOP regs followed byPUSH 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 redundantPOP/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 theredundant code in the example above would eventually leave the following code:

PUSHAFPUSHBCPUSHDEPUSHHLCALL_ADDR1CALL_ADDR2POPHLPOPDEPOPBCPOPAF

See also

[edit]

References

[edit]
  1. ^Muchnick, Steven Stanley (1997-08-15).Advanced Compiler Design and Implementation.Academic Press /Morgan Kaufmann.ISBN 978-1-55860-320-2.
  2. ^Grune, Dick;Bal, Henri; Jakobs, Ceriel; Langendoen, Koen (2012-07-20).Modern Compiler Design (2 ed.).Wiley /John Wiley & Sons, Ltd.ISBN 978-0-471-97697-4.
  3. ^McKeeman, William Marshall (July 1965)."Peephole optimization".Communications of the ACM.8 (7):443–444.doi:10.1145/364995.365000.S2CID 9529633.
  4. ^Fischer, Charles N.; Cytron, Ron K.; LeBlanc, Jr., Richard J. (2010).Crafting a Compiler(PDF).Addison-Wesley.ISBN 978-0-13-606705-4. Archived fromthe original(PDF) on 2018-07-03. Retrieved2018-07-02.
  5. ^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(PDF) (2 ed.).Pearson Education. p. 540.Archived(PDF) from the original on 2018-06-10. Retrieved2018-07-02.

External links

[edit]

The dictionary definition ofpeephole optimization at Wiktionary

Basic block
Loop
Data-flow
analysis
SSA-based
Code generation
Functional
Global
Other
Static analysis
Retrieved from "https://en.wikipedia.org/w/index.php?title=Peephole_optimization&oldid=1329643837"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp