





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















Classical Optimizations
Y
Constant folding, strength reduction
Y
Y
Constant propagation
Forward copy propagation
Backward copy propagation
Constant combining
Operation folding
Y
Invariant code removal
Global variable migration
Induction variable strength reduction
Induction variable elimination
Strength Reduction
y
r1 = r2 * 8
r1 = r2 << 3
y
r1 = r2 / 4
r1 = r2 >> 2
y
r1 = r2 REM 16
r1 = r2 & 15
y
r1 = r2 * 6
X
r100 = r2 << 2; r101 = r2 << 1; r1 = r100 + r
y
r1 = r2 * 7
X
r100 = r2 << 3; r1 = r100 – r
Dead Code Elimination
Y
Y
X can be deleted
y
no stores or branches
DU chain empty or destregister not live
Y
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)
Constant Propagation
Y
rx = L (where L is a literal)
Maximally propagate
Assume no instructionencoding restrictions
Y
SRC: Literal is a hard codedconstant, so never a problem
DEST: Must be available
y
Guaranteed to reach
y
May reach not good enough
r1 = 5r2 = r1 + r
r1 = r1 + r
r7 = r1 + r
r8 = r1 + 3
r9 = r1 + r
Local Constant Propagation
Y
y
When dest(X) is a Macroreg, BRL destroys the value
Class Problem
Optimize this applying 1. constant propagation 2. constant folding 3. strength reduction 4. dead code elimination
r1 = 0
r2 = 10
r4 = 1
r7 = r1 * 4
r6 = 8
r2 = 0
r3 = r2 / r
r3 = r4 * r r3 = r3 + r
r2 = r2 + r r6 = r7 * r
r1 = r1 + 1
store (r1, r3)
Forward Copy Propagation
Y
r1 = r
r4 = r1 + 1
r4 = r2 + 1
Y
Reduce chain of dependences
Eliminate the move
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 = r2 r3 = r
r2 = 0
r6 = r3 + 1
r5 = r2 + r
CSE – Common Subexpression Elimination
Y
Eliminate recomputation of anexpression by reusing the previousresult
»
r1 = r2 * r
»
Æ
r100 = r
»
…
»
r4 = r2 * r
Æ
r4 = r
Y
Benefits
»
Reduce work
»
Moves can get copy propagated
Y
Rules (ops X and Y)
»
X and Y have the same opcode
»
src(X) = src(Y), for all srcs
»
expr(X) is available at Y
»
if X is a load, then there is no storethat may write to address(X) alongany path between X and Y
r1 = r2 * r
r3 = r4 / r
r2 = r2 + 1
r6 = r3 * 7
r5 = r2 * r
r8 = r4 / r r9 = r3 * 7
if op is a load, call it redundant load elimination rather than CSE
Class Problem
Optimize this applying 1. constant propagation 2. constant folding 3. strength reduction 4. dead code elimination 5. forward copy propagation 6. backward copy propagation 7. CSE
r1 = 9 r4 = 4 r5 = 0
r6 = 16
r2 = r3 * r r8 = r2 + r
r9 = r
r7 = load(r2)
r5 = r9 * r
r3 = load(r2)
r10 = r3 / r store (r8, r7)
r11 = r
r12 = load(r11)
store(r12, r3)
Operation Folding
Y
Classic example is MPYADD
r1 = r2 * r
r5 = r1 + r
r5 = r2 * r3 + r
Y
Y
Y
X is an arithmetic operation
dest(X) != any src(X)
Y is an arithmetic operation
Y consumes dest(X)
X and Y can be merged
src(X) not modified in (X…Y)
Loop Optimizations
Invariant Code Removal
Y
Move operations whose sourceoperands do not change within the loopto the loop preheader
»
Execute them only 1x per invocation ofthe loop
Y
Rules
»
X can be moved
»
src(X) not modified in loop body
»
X is the only op to modify dest(X)
»
for all uses of dest(X), X is in theavailable defs set
»
for all exit BB, if dest(X) is live on theexit edge, X is in the available defs seton the edge
»
if X not executed on every iteration,then X must provably not causeexceptions
»
if X is a load or store, then there are nowrites to address(X) in loop
r1 = 3r5 = 0
r4 = load(r5)
r7 = r4 * 3
r8 = r2 + 1 r7 = r8 * r
r3 = r2 + 1
r1 = r1 + r store (r1, r3)
Global Variable Migration
Y
Load in preheader
Store at exit points
Y
X is a load or store
address(X) not modified in theloop
if X not executed on everyiteration, then X must provablynot cause an exception
All memory ops in loop whoseaddress can equal address(X)must always have the sameaddress as X
r4 = load(r5)
r4 = r4 + 1
r8 = load(r5)
r7 = r8 * r
store(r5, r4)
store(r5,r7)