

























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!


























Schedule
» Today 12/4 – Finish optimizations » Wednes 12/6 – Register allocation, Exam 2
review » Mon 12/11 – Exam 2 in class » Wednes 12/13 – No class (Project 3 due)
Reading for today’s class
» 10.
r1 = 0 r4 = r1 | -1r7 = r1 * 4
r6 = r
r3 = 8 / r
r3 = 8 * r6r3 = r3 + r
r2 = r2 + r1r6 = r7 * r6r1 = r1 + 1 store (r1, r3)
Optimize this applying 1. constant folding 2. strength reduction 3. dead code elimination
Forward propagation ofmoves of the form
When is it legal?
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
Consider 2 ops, X and Y indifferent BBs
Rules 4/5 guarantee X is available
r1 = 5r2 = ‘_x’
r1 = r1 + r
r7 = r1 – r
r8 = r1 * r
r9 = r1 + r
Note: checks for subroutine calls whenever SP/FP/etc. are involved is required for all optis. I will omit the check from here on!
1: r1 = 02: r2 = 10 3: r4 = 1 4: r7 = r1 * 3
5: r6 = 7
6: r2 = 0 7: r3 = r2 / r
8: r3 = r4 * r69: r3 = r3 + r
10: r2 = r2 + r111: r6 = r7 * r612: r1 = r1 + 1 13: store (r1, r3)
Optimize this applying 1. constant propagation 2. constant folding 3. dead code elimination
X: r1 = r2 + r
r4 = r2 + r
r5 = r1 + r
r5 = r4 + r
Y: r4 = r
noop
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) after the firstredefinition of dest(Y)
X: r1 = r2 * r »^
r100 = r
Y: r4 = r2 * r
r4 = r
Reduce work »^
Moves can get copy propagated
X and Y have the same opcode »^
src(X) = src(Y), for all srcs »^
for all srcs(X) no defs of srci in [X ... Y) »^
if X is a load, then there is no store thatmay write to address(X) between X and Y
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 = 9r4 = 4r5 = 0r6 = 16 r2 = r3 * r4r8 = r2 + r
r9 = r r7 = load(r2)r5 = r9 * r4r3 = load(r2)r10 = r3 / r6store (r8, r7)
r11 = r r12 = load(r11)store(r12, r3)
The most important set of optimizations
» Because programs spend so much time in
loops
Optimize given that you know a sequenceof code will be repeatedly executed
Optis
» Invariant code removal » Global variable migration » Induction variable strength reduction » Induction variable elimination
X can be moved »^
src(X) not modified in loop body »^
X is the only op to modifydest(X) »^
for all uses of dest(X), X is in theavailable defs set »^
for all exit BB, if dest(X) is liveon the exit edge, X is in theavailable defs set on the edge »^
if X not executed on everyiteration, then X must provablynot cause exceptions »^
if X is a load or store, then thereare no writes to address(X) inloop
r1 = 3r5 = 0 r4 = load(r5)r7 = r4 * 3
r8 = r2 + 1r7 = r8 * r
r3 = r2 + 1
r1 = r1 + r7 store (r1, r3)
Load in preheader »^
Store at exit points
X is a load or store »^
address(X) not modified inthe loop »^
if X not executed on everyiteration, then X mustprovably not cause anexception »^
All memory ops in loopwhose address can equaladdress(X) must always havethe same address as X
r4 = load(r5)r4 = r4 + 1
r8 = load(r5)r7 = r8 * r
store(r5, r4)
store(r5,r7)
Create basic inductionvariables from derivedinduction variables
Rules
r5 = r4 - 3r4 = r4 + 1
r7 = r4 * r
r6 = r4 << 2
Insert the following into the bottomof preheader^ y
new_reg = RHS(X)
»^
if opcode(X) is not add/sub, insertto the bottom of the preheader^ y
new_inc = inc(src1(X)) opcode(X)src2(X)
»^
else^ y
new_inc = inc(src1(X))
»^
Insert the following at each updateof src1(X)^ y
new_reg += new_inc
»^
Change X
dest(X) = new_reg
r5 = r4 - 3r4 = r4 + 1
r7 = r4 * r
r6 = r4 << 2