peephole optimization - rwth aachen...
TRANSCRIPT
![Page 1: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/1.jpg)
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo
21. Juli 2014
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 1/31
![Page 2: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/2.jpg)
1 IntroductionContext and Motivation
2 Peephole OptimizationGeneralReplacement RulesPattern Matching
3 Demo4 Replacement Rules Derivation
Finding replacement RulesAutomatic rules derivationValidation of EquivalenceCost Comparison
5 ConclusionReferencesPeephole Optimization in LLVMExecution Test on other Intermediate LanguageBoolean Test
Introduction - Context and Motivation
Table of Contents
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 2/31
![Page 3: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/3.jpg)
I Improve code quality and efficiencyI Increase PerformanceI Reduce code size
I Optimization on different abstraction levelsI High-level programming languageI Intermediate codeI Machine code
I Optimization techniquesI Local optimizationI Global optimizationI Peephole optimizationI . . .
Introduction - Context and Motivation
Code Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 3/31
![Page 4: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/4.jpg)
I Improve code quality and efficiencyI Increase PerformanceI Reduce code size
I Optimization on different abstraction levelsI High-level programming languageI Intermediate codeI Machine code
I Optimization techniquesI Local optimizationI Global optimizationI Peephole optimizationI . . .
Introduction - Context and Motivation
Code Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 3/31
![Page 5: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/5.jpg)
I Improve code quality and efficiencyI Increase PerformanceI Reduce code size
I Optimization on different abstraction levelsI High-level programming languageI Intermediate codeI Machine code
I Optimization techniquesI Local optimizationI Global optimizationI Peephole optimizationI . . .
Introduction - Context and Motivation
Code Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 3/31
![Page 6: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/6.jpg)
1 IntroductionContext and Motivation
2 Peephole OptimizationGeneralReplacement RulesPattern Matching
3 Demo4 Replacement Rules Derivation
Finding replacement RulesAutomatic rules derivationValidation of EquivalenceCost Comparison
5 ConclusionReferencesPeephole Optimization in LLVMExecution Test on other Intermediate LanguageBoolean Test
Peephole Optimization - General
Table of Contents
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 4/31
![Page 7: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/7.jpg)
Usage Context
I Used within modern compiler (e.g. GCC, LLVM, ACK, . . . )I Typically applied on intermediate or machine code
Optimization precedure
I Optimize code in a small moving window (peephole)I Iterative Optimazation
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 5/31
![Page 8: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/8.jpg)
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 6/31
![Page 9: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/9.jpg)
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 7/31
![Page 10: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/10.jpg)
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 8/31
![Page 11: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/11.jpg)
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 9/31
![Page 12: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/12.jpg)
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 10/31
![Page 13: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/13.jpg)
1 IntroductionContext and Motivation
2 Peephole OptimizationGeneralReplacement RulesPattern Matching
3 Demo4 Replacement Rules Derivation
Finding replacement RulesAutomatic rules derivationValidation of EquivalenceCost Comparison
5 ConclusionReferencesPeephole Optimization in LLVMExecution Test on other Intermediate LanguageBoolean Test
Peephole Optimization - Replacement Rules
Table of Contents
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 11/31
![Page 14: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/14.jpg)
I Replace Sequence of Instruction with faster and/or fewer InstructionsI Many different Replacement Techniques
Peephole Optimization - Replacement Rules
Replacement Rules
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 12/31
![Page 15: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/15.jpg)
Null Sequences
I Remove useless instructions
ExampleLOV ALOC 0ADD
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 13/31
![Page 16: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/16.jpg)
Null Sequences
I Remove useless instructions
ExampleLOV A
→LOV A
LOC 0ADD
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 13/31
![Page 17: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/17.jpg)
Constant folding
I Replace constant expressions with its result
ExampleLOC 8LOC 2ADD
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 14/31
![Page 18: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/18.jpg)
Constant folding
I Replace constant expressions with its result
ExampleLOC 8
→LOC 10
LOC 2ADD
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 14/31
![Page 19: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/19.jpg)
Algebraic Laws
I Use algebraic laws to simplify expressions
ExampleLOV ALOC 7ADDLOC 5SUB(A+ 7)− 5
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 15/31
![Page 20: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/20.jpg)
Algebraic Laws
I Use algebraic laws to simplify expressions
ExampleLOV A
→
LOV ALOC 7 LOC 7ADD LOC 5LOC 5 SUBSUB ADD(A+ 7)− 5 = A+ (7− 5)
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 15/31
![Page 21: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/21.jpg)
Strength reduction
I Replace stronger operations with weaker onesI Often applicable to multiplication or exponentiation in loops
ExampleLOV ALOC 2MUL
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 16/31
![Page 22: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/22.jpg)
Strength reduction
I Replace stronger operations with weaker onesI Often applicable to multiplication or exponentiation in loops
ExampleLOV A
→LOV A
LOC 2 DUP 2MUL ADD
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 16/31
![Page 23: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/23.jpg)
Strength reduction
I Replace stronger operations with weaker onesI Often applicable to multiplication or exponentiation in loops
ExampleLOV A
→LOV A
→LOV A
LOC 2 DUP 2 LOC 1MUL ADD SHL
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 16/31
![Page 24: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/24.jpg)
Special instructions
I Replace Instructions with specialized InstructionI Usually more relevant on machine code
ExampleLOV ALOC 1ADDSTV A
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 17/31
![Page 25: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/25.jpg)
Special instructions
I Replace Instructions with specialized InstructionI Usually more relevant on machine code
ExampleLOV A
→
INV ALOC 1ADDSTV A
Peephole Optimization - Replacement Rules
Common Techniques
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 17/31
![Page 26: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/26.jpg)
1 IntroductionContext and Motivation
2 Peephole OptimizationGeneralReplacement RulesPattern Matching
3 Demo4 Replacement Rules Derivation
Finding replacement RulesAutomatic rules derivationValidation of EquivalenceCost Comparison
5 ConclusionReferencesPeephole Optimization in LLVMExecution Test on other Intermediate LanguageBoolean Test
Peephole Optimization - Pattern Matching
Table of Contents
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 18/31
![Page 27: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/27.jpg)
I Many instruction sequences have same semanticsI Generalize rules to avoid (semantically) identical rulesI Different kinds of matching strategies
ExampleLOC 7
→
LOC 7ADD LOC 5LOC 5 SUBSUB ADD
Peephole Optimization - Pattern Matching
Pattern Matching
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 19/31
![Page 28: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/28.jpg)
I Many instruction sequences have same semanticsI Generalize rules to avoid (semantically) identical rulesI Different kinds of matching strategies
ExampleLOC 7
→
LOC 7 LOC 6
→
LOC 6ADD LOC 5 ADD LOC 5LOC 5 SUB LOC 5 SUBSUB ADD SUB ADD
Peephole Optimization - Pattern Matching
Pattern Matching
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 19/31
![Page 29: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/29.jpg)
I Many instruction sequences have same semanticsI Generalize rules to avoid (semantically) identical rulesI Different kinds of matching strategies
ExampleLOC 7
→
LOC 7 LOC 6
→
LOC 6ADD LOC 5 ADD LOC 5LOC 5 SUB LOC 5 SUBSUB ADD SUB ADD . . .
Peephole Optimization - Pattern Matching
Pattern Matching
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 19/31
![Page 30: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/30.jpg)
I Many instruction sequences have same semanticsI Generalize rules to avoid (semantically) identical rulesI Different kinds of matching strategies
ExampleLOC %0
→
LOC %0ADD LOC %1LOC %1 SUBSUB ADD
Peephole Optimization - Pattern Matching
Pattern Matching
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 19/31
![Page 31: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/31.jpg)
Demo
Demo -
Demo
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 20/31
![Page 32: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/32.jpg)
1 IntroductionContext and Motivation
2 Peephole OptimizationGeneralReplacement RulesPattern Matching
3 Demo4 Replacement Rules Derivation
Finding replacement RulesAutomatic rules derivationValidation of EquivalenceCost Comparison
5 ConclusionReferencesPeephole Optimization in LLVMExecution Test on other Intermediate LanguageBoolean Test
Replacement Rules Derivation - Finding replacement Rules
Table of Contents
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 21/31
![Page 33: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/33.jpg)
Manual rule creation
I Replacement rules written by handI Using common techniques
Automatic rules derivation
I Deriving rules using superoptimization technique
Replacement Rules Derivation - Finding replacement Rules
How to find replacement rules?
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 22/31
![Page 34: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/34.jpg)
1 IntroductionContext and Motivation
2 Peephole OptimizationGeneralReplacement RulesPattern Matching
3 Demo4 Replacement Rules Derivation
Finding replacement RulesAutomatic rules derivationValidation of EquivalenceCost Comparison
5 ConclusionReferencesPeephole Optimization in LLVMExecution Test on other Intermediate LanguageBoolean Test
Replacement Rules Derivation - Automatic rules derivation
Table of Contents
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 23/31
![Page 35: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/35.jpg)
Superoptimization
I Given a set of instruction sequences to optimizeI Calculate every possible instruction sequence out of a set of
instructionsI Compare outcome of calculated sequences to original sequencesI Check if new found equivalent sequence has lower cost
Replacement Rules Derivation - Automatic rules derivation
Automatic rules derivation
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 24/31
![Page 36: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/36.jpg)
Superoptimization
I Given a set of instruction sequences to optimizeI Calculate every possible instruction sequence out of a set of
instructionsI Compare outcome of calculated sequences to original sequencesI Check if new found equivalent sequence has lower cost
Replacement Rules Derivation - Automatic rules derivation
Automatic rules derivation
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 24/31
![Page 37: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/37.jpg)
Superoptimization
I Given a set of instruction sequences to optimizeI Calculate every possible instruction sequence out of a set of
instructionsI Compare outcome of calculated sequences to original sequencesI Check if new found equivalent sequence has lower cost
Replacement Rules Derivation - Automatic rules derivation
Automatic rules derivation
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 24/31
![Page 38: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/38.jpg)
Superoptimization
I Given a set of instruction sequences to optimizeI Calculate every possible instruction sequence out of a set of
instructionsI Compare outcome of calculated sequences to original sequencesI Check if new found equivalent sequence has lower cost
Replacement Rules Derivation - Automatic rules derivation
Automatic rules derivation
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 24/31
![Page 39: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/39.jpg)
1 IntroductionContext and Motivation
2 Peephole OptimizationGeneralReplacement RulesPattern Matching
3 Demo4 Replacement Rules Derivation
Finding replacement RulesAutomatic rules derivationValidation of EquivalenceCost Comparison
5 ConclusionReferencesPeephole Optimization in LLVMExecution Test on other Intermediate LanguageBoolean Test
Replacement Rules Derivation - Validation of Equivalence
Table of Contents
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 25/31
![Page 40: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/40.jpg)
Execution Test: Process
I Run both instruction sequences on machineI Use multiple vectors as inputI Compare outcomeI Discard sequences with not matching results
Execution Test: Property
I Stochastical testI Accuracy depends on test effort
Replacement Rules Derivation - Validation of Equivalence
Equivalence Test
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 26/31
![Page 41: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/41.jpg)
ExampleSequence
Original ReplacementLOV X LOV XLOV Y LOV YADD SUB
Input Vector Stack(X,Y) Original Replacement(0,0) [0] [0] 3
(1,1) [2] [0] 7
Replacement Rules Derivation - Validation of Equivalence
Execution Test
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 27/31
![Page 42: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/42.jpg)
ExampleSequence
Original ReplacementLOV X LOV XLOC 2 SHRDIV SHLLOC 2MUL
Input Vector Stack(X) Original Replacement(0) [0] [0] 3
(1) [0] [0] 3
(65535) [65534] [65534] 3
Replacement Rules Derivation - Validation of Equivalence
Execution Test
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 28/31
![Page 43: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/43.jpg)
1 IntroductionContext and Motivation
2 Peephole OptimizationGeneralReplacement RulesPattern Matching
3 Demo4 Replacement Rules Derivation
Finding replacement RulesAutomatic rules derivationValidation of EquivalenceCost Comparison
5 ConclusionReferencesPeephole Optimization in LLVMExecution Test on other Intermediate LanguageBoolean Test
Replacement Rules Derivation - Cost Comparison
Table of Contents
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 29/31
![Page 44: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/44.jpg)
Cost Functions
I RuntimeI Code sizeI Memory usage
Replacement Rules Derivation - Cost Comparison
Cost Comparison
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 30/31
![Page 45: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/45.jpg)
I Optimization technique used in modern compilersI Improve code quality and efficiencyI Using replacement rules for optimizationI Generalize with patternsI Automatic generation of rules possible
I Generated optimizer speedup of 1.7 to factor of 10 over conventionaloptimizer
Conclusion -
Conclusion
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 31/31
![Page 46: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/46.jpg)
I Tanenbaum, Andrew S. and van Staveren, Hans and Stevenson,Johan W. Using Peephole Optimization on Intermediate Code. ACMTrans. Program. Lang. Syst., pp. 21-36
I Bansal, Sorav and Aiken, Alex. Automatic Generation of PeepholeSuperoptimizers. IGARCH Comput. Archit. News.
I Elif Aktolga and Supervisor Dr. Des Watson. Pattern MatchingStrategies for Peephole Optimisation
I Henry Massalin. Department of Computer Science ColumbiaUniversity New York, NY 10027. Superoptimizer - A Look at theSmallest Program
Appendix - References
References
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 32/31
![Page 47: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/47.jpg)
Example
int vadd(int a){
return a*2}
llvm IR code%a.addr = alloca i32, align 4store i32 %a, i32* %a.addr, align 4%0 = load i32* %a.addr, align 4%mul = mul nsw i32 %0, 2 → %mul = shl i32 %0, 1ret i32 %mul
Appendix - Peephole Optimization in LLVM
Peephole Optimization in LLVM
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 33/31
![Page 48: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/48.jpg)
ExampleOriginal Sequence Replacement Candidate
ld r0, x ld r0, xld r1, y ld r1, y
add r0, r1vector (x,y) Original Sequence Replacement Candidate
(0,0) (0,0) (0,0) 3
(1,1) (2,1) (1,1) 7
Appendix - Execution Test on other Intermediate Language
Execution Test
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 34/31
![Page 49: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/49.jpg)
I Generate boolean formula for machine statesI RegisterI MemoryI Stack
I Express each instruction as formula that changes machine stateI Express equivalence relation of final machine states as constraintI Use SAT solver to check if constraint holds
Appendix - Boolean Test
Boolean Test
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 35/31
![Page 50: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/50.jpg)
I Remove multiplication from a loopI Occurs often during array access
Example
f o r ( I = 0 , A = 0; I < 10; ++ I ) {A = I ∗ 4;. . .
}. . .
Appendix - Strength Reduction in Loops
Strength Reduction in Loops
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 36/31
![Page 51: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/51.jpg)
I Remove multiplication from a loopI Occurs often during array access
Example
f o r ( I = 0 , A = 0; I < 10; ++ I ) {. . .A += 4;
}. . .
Appendix - Strength Reduction in Loops
Strength Reduction in Loops
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 37/31
![Page 52: Peephole Optimization - RWTH Aachen Universityhpac.rwth-aachen.de/teaching/sem-accg-14/Peephole-optimization.pdf · Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 0/31 Peephole](https://reader031.vdocuments.us/reader031/viewer/2022030420/5aa6d7ba7f8b9ab4788ef6eb/html5/thumbnails/52.jpg)
ExampleZRV I
→
ZRV IZRV A ZRV ALOV I LOV ILOC 10 LOC 10BGE 7 BGE 7LOC 4 . . .LOV I LOC 4MUL LOV ASTV A ADD. . . STV AINV I INV IBRA -8 BRA -8. . . . . .
Appendix - Strength Reduction in Loops
Strength Reduction in Loops
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 38/31