Download Slides on Classic Optimization - Fall 2006 | EECS 583 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!
EECS 583 ā Class 10Classic Optimization
Guest speaker today: Rajiv RavindranUniversity of MichiganFebruary 13, 2006
Reading Material^
Todayās classĀ»
Compilers: Principles, Techniques, and Tools
A. Aho, R. Sethi, and J. Ullman, Addison-Wesley, 1988,9.9, 10.2, 10.3, 10.
Material for the next lectureĀ»
"Compiler Code Transformations for Superscalar-Based High-Performance Systems", S. Mahlke et al., Supercomputing '92,Nov. 1992, pp. 808-817.
Predicate-sensitive dataflow analysisĀ»
Finish this on Wednesday when Scott is back
Classical Optimizations^
Operation-level ā 1 operation in isolationĀ»
Constant folding, strength reduction
Dead code elimination (global, but 1 op at a time)
Local/Global ā Pairs of operationsĀ»
Constant propagation Ā»^
Forward copy propagation Ā»^
Backward copy propagation Ā»^
CSE
Ā»^
Constant combining Ā»^
Operation folding
Loop ā Body of a loopĀ»
Invariant code removal Ā»^
Global variable migration Ā»^
Induction variable strength reduction Ā»^
Induction variable elimination
Caveat^
Traditional compiler classĀ»
Fancy implementations of optimizations, efficient algorithms
Ā»^
Entire papers written on how to do 1 optimization
Ā»^
Spend entire class on 1 optimization
For this class ā Go over concepts of each optimizationĀ»
What it is, why its useful
Ā»^
When can it be applied (set of conditions that must be satisfied)
ChallengesĀ»
How do predicates affect things?
Ā»^
Register pressure?
Ā»^
ILP verses operation count
Strength Reduction^
Replace expensive ops with cheaper onesĀ»
Constant propagation creates opportunities for this
Power of 2 constantsĀ»
Multiply by power of 2, replace with left shift^
r1 = r2 * 8
r1 = r2 << 3
Ā»^
Divide by power of 2, replace with right shift^
r1 = r2 / 4
r1 = r2 >> 2
Ā»^
Remainder by power of 2, replace with logical and^
r1 = r2 REM 16
r1 = r2 & 15
More exoticĀ»
Replace multiply by constant by sequence of shift and adds/subs^
r1 = r2 * 6
^
r100 = r2 << 2; r101 = r2 << 1; r1 = r100 + r
^
r1 = r2 * 7^
r100 = r2 << 3; r1 = r100 ā r
Dead Code Elimination^
Remove any operation whoāsresult is never consumed
RulesĀ»
X can be deleted^
no stores or branches
Ā»^
DU chain empty or destregister not live
This misses some dead code!!Ā»
Especially in loops Ā»^
Critical operation^
store or branch operation
Ā»^
Any operation that does notdirectly or indirectly feed acritical operation is dead Ā»^
Trace UD chains backwardsfrom critical operations Ā»^
Any op not visited is dead
r1 = 3r2 = 10 r4 = r4 + 1r7 = r1 * r
r2 = 0
r3 = r3 + 1
r3 = r2 + r1 store (r1, r3)
Constant Propagation^
Forward propagation of movesof the formĀ»
rx = L (where L is a literal) Ā»^
Maximally propagate Ā»^
Assume no instructionencoding restrictions
When is it legal?Ā»
SRC: Literal is a hard codedconstant, so never a problem Ā»^
DEST: Must be available^
Guaranteed to reach May reach not good enough
r1 = 5r2 = r1 + r
r1 = r1 + r
r7 = r1 + r
r8 = r1 + 3
r9 = r1 + r
Local Constant Propagation^
Consider 2 ops, X and Y in aBB, X is before YĀ»
- X is a move Ā»^
- src1(X) is a literal Ā»^
- Y consumes dest(X) Ā»^
- There is no definition ofdest(X) between X and Y Ā»^
- No danger betw X and Y^
When dest(X) is a Macroreg, BRL destroys the value
Note, ignore operation formatissues, so all operations canhave literals in either operandposition
r1 = 5r2 = ā_xār3 = 7r4 = r4 + r1r1 = r1 + r2r1 = r1 + 1r3 = 12r8 = r1 - r2r9 = r3 + r5r3 = r2 + 1r10 = r3 ā r
What About Predicated Code?^
Use global formulation with predicate-sensitive dataflow
Use local formulation
Ā» Predicate dominates ā predicate of def >=
predicate of use Ā» Intervening writes do not matter if they are on
disjoint predicater1 = 2 if p1.. .r2 = r1 + r3 if p
r1 = 2 if p1.. .r1 = load(r5) if p3.. .r2 = r1 + r3 if p
Predicated Local Constant Propagation^
Consider 2 ops, X and Y in aPredicated BB, X is before YĀ»
- X is a move Ā»^
- src1(X) is a literal Ā»^
- Y consumes dest(X) Ā»^
3.5. pred(X) >= pred(Y) Ā»^
- There is no definition of dest(X), D, between X and Y such that pred(D)
pred(X)
!= False Ā»^
- No danger betw X and Y^
When dest(X) is a Macroreg, BRL destroys the value
r1 = 5 if Tr2 = ā_xā if Tr3 = 7 if Tp1,p2 = cmppUNUC r5 < 0 if Tr4 = r4 + r1 if p1r1 = r1 + r2 if p1r1 = r1 + 1 if p1r3 = 12 if p1r8 = r1 - r2 if p2r9 = r3 + r5 if p2r3 = r1 + 1 if p2r10 = r3 ā r1 if p
Forward Copy Propagation^
Forward propagation of the RHSof movesĀ»
r1 = r Ā»^
Ā»^
r4 = r1 + 1
r4 = r2 + 1
BenefitsĀ»
Reduce chain of dependences Ā»^
Eliminate the move
Rules (ops X and Y)Ā»
X is a move Ā»^
src1(X) is a register Ā»^
Y consumes dest(X) Ā»^
X.dest is an available def at Y Ā»^
X.src1 is an available expr at Y
r1 = r2r3 = r
r2 = 0
r6 = r3 + 1
r5 = r2 + r
Backward Copy Propagation^
Backward propagation of the LHSof movesĀ»
r1 = r2 + r
r4 = r2 + r
Ā»^
⦠»^
r5 = r1 + r
r5 = r4 + r
Ā»^
⦠»^
r4 = r
noop
Rules (ops X and Y in same BB)Ā»
dest(X) is a register Ā»^
dest(X) not live out of BB(X) Ā»^
Y is a move Ā»^
dest(Y) is a register Ā»^
Y consumes dest(X) Ā»^
dest(Y) not consumed in (Xā¦Y) Ā»^
dest(Y) not defined in (Xā¦Y) Ā»^
There are no uses of dest(X) afterthe first redefinition of dest(Y)
r1 = r8 + r9r2 = r9 + r1r4 = r2r6 = r2 + 1r9 = r1r10 = r6r5 = r6 + 1r4 = 0r8 = r2 + r
Class Problem
Optimize this applying 1. dead code elimination2. forward copy propagation3. backward copy propagation4. CSE
r1 = r5 if Truer4 = r1 if Truer6 = r15 if Truer2 = r3 * r4 if Truer8 = r2 + r5 if Truer9 = r3 if Truep1,p2 = cmpp(r2, r8) if Truer7 = load(r2) if Truer5 = r9 * r4 if p1r3 = load(r2) if p2r10 = r3 / r6 if p2r11 = r2 if Truer12 = load(r11) if p1store (r8, r7) if p2store(r12, r3) if True
Be careful of the predicates!
Loop Optimizations^
The most important set of optimizationsĀ»
Because programs spend so much time in loops
Optimize given that you know a sequence of code will berepeatedly executed
OptisĀ»
Invariant code removal
Ā»^
Global variable migration
Ā»^
Induction variable strength reduction
Ā»^
Induction variable elimination