Download Available and Reaching Definitions and Expressions Analysis in Compiler Optimization - Pro and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!
EECS 583 – Class 7 Classical Optimization
University of Michigan September 26, 2007
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.
Next class
“Analysis Techniques for Predicated Code”, R. Johnson and M. Schlansker, MICRO-29, 1996
Next week
I will be out of town attending the CASES conference
Class will be held as usual - 2 of my PhD students will be guestspeakers
Reaching vs Available Definitions
1:r1 = r2 + r 2:r6 = r4 – r
3:r4 = 44:r6 = 8
5:r6 = r2 + r 6:r7 = r4 – r
1,2,3,4 reach 1 available
1,2 reach 1,2 available
1,3,4 reach 1,3,4 available
1,2 reach 1,2 available
Available Definition Analysis (Adefs)
A definition d is available
at a point p if along all
paths
from d to p, d is not killed
Remember, a definition of a variable is killed
between 2
points when there is another definition of that variablealong the path
r1 = r2 + r3 kills previous definitions of r
Algorithm
Forward dataflow analysis as propagation occurs from defsdownwards
Use the Intersect function as the meet operator to guarantee theall-path requirement
GEN/KILL/IN/OUT similar to reaching defs
y
Initialization of IN/OUT is the tricky part
Compute IN/OUT Sets for all BBs (Adefs)
U = universal set of all operations in the Procedure IN(0) = 0 OUT(0) = GEN(0) for each basic block in procedure, W, (W != 0), do
IN(W) = 0 OUT(W) = U – KILL(W)
change = 1 while
(change) do
change = 0 for
each basic block in procedure, X, do
old_OUT = OUT(X) IN(X) = Intersect(OUT(Y)) for all predecessors Y of X OUT(X) = GEN(X) + (IN(X) – KILL(X)) if
(old_OUT != OUT(X)) then
change = 1
endif
endfor
endfor
Example Adefs Calculation
1: r1 = MEM[r2+0]2: r2 = r2 + 13: r3 = r1 * r
4: r1 = r1 + 55: r3 = r5 – r16: r8 = r3 * 2
7: r7 = 08: r1 = r1 + 59: r7 = r1 - 6
10: r8 = r7 + r811: r1 = r3 – r812: r3 = r1 * 2
Class Problem - Aexprs Calculation
1: r1 = r6 * r92: r2 = r2 + 13: r5 = r3 * r
4: r1 = r2 + 15: r3 = r3 * r46: r8 = r3 * 2
7: r7 = r3 * r48: r1 = r1 + 59: r7 = r1 - 6
10: r8 = r2 + 111: r1 = r3 * r412: r3 = r6 * r
Some Things to Think About
Liveness and rdefs are basically the same thing
All dataflow is basically the same with a few parameters
y
Meaning of gen/kill – src vs dest, variable vs operation
y
Backward / Forward
y
All paths / some paths (must/may)
So far, we have looked at may analysis algorithms
How do you adjust to do must algorithms?
Dataflow can be slow
How to implement it efficiently?
How to represent the info?
Predicates
Throw a monkey wrench into this stuff (discuss next Monday)
So, how are predicates handled?
A Tour Through the Classical Optimizations
For this class – Go over concepts of a small subset of theoptimizations
What it is, why its useful
When can it be applied (set of conditions that must be satisfied)
How it works
Give you the flavor but don’t want to beat you over the head
Challenges
How do predicates affect things?
Register pressure?
ILP verses operation count
Dead Code Elimination
Remove any operation who’sresult is never consumed
Rules
X can be deleted
y
no stores or branches
DU chain empty or destregister not live
This misses some dead code!!
Especially in loops
Critical operation
y
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 = 3
r2 = 10
r4 = r4 + 1 r7 = r1 * r
r2 = 0
r3 = r3 + 1
r3 = r2 + r store (r1, r3)
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
y
When dest(X) is a Macroreg, BRL destroys the value
Note, ignore operation formatissues, so all operations canhave literals in either operandposition
r1 = 5 r2 = ‘_x’ r3 = 7 r4 = r4 + r1 r1 = r1 + r2 r1 = r1 + 1 r3 = 12 r8 = r1 - r2 r9 = r3 + r5 r3 = r2 + 1 r10 = r3 – r
Global Constant Propagation
Consider 2 ops, X and Y indifferent BBs
- X is a move
- src1(X) is a literal
- Y consumes dest(X)
- X is in a_in(BB(Y))
- Dest(x) is not modified betweenthe top of BB(Y) and Y
- No danger betw X and Y
y
When dest(X) is a Macro reg,BRL destroys the value
r1 = 5 r2 = ‘_x’
r1 = r1 + r
r7 = r1 – r
r8 = r1 * r
r9 = r1 + r
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
y
When dest(X) is a Macro reg, BRLdestroys the value
r1 = 5 if T r2 = ‘_x’ if T r3 = 7 if T p1,p2 = cmppUNUC r5 < 0 if T r4 = r4 + r1 if p1 r1 = r1 + r2 if p1 r1 = r1 + 1 if p1 r3 = 12 if p1 r8 = r1 - r2 if p2 r9 = r3 + r5 if p2 r3 = r1 + 1 if p2 r10 = r3 – r1 if p
Class Problem
r1 = 0r2 = 10 r4 = 1r7 = r1 * 4r6 = 8p1,p2 = cmppUNUC r3 > 0r2 = 0 if p1r3 = r4 * r6 if p2r3 = r3 + r2 if p2r6 = r6 * r7 if Tr3 = r2 / r6 if p1r2 = r2 + 1 if Tr1 = r1 + 1 if T store (r1, r3)
Optimize this applying 1. constant propagation 2. constant folding