peephole optimization - hpac.cs.umu.se
TRANSCRIPT
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
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
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
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
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
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
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
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 6/31
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 7/31
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 8/31
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 9/31
Peephole Optimization - General
Peephole Optimization
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 10/31
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Demo
Demo -
Demo
Simon Oehrl and Bao-Loc Nguyen Ngo | 21. Juli 2014 20/31
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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