
























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!

























© Harry H. Porter, 2006
**- !Ignore all other basic blocks
**- !Ignore all other basic blocks
**- !Store all LIVE variables in memory between basic blocks.
© Harry H. Porter, 2006
CS-322 Target Generation, Part 2 © Harry H. Porter, 2006
“x” is in R7 “x” is in R ??
© Harry H. Porter, 2006
Perform live variable analysis beforehand (during optimization phase) (^) We’ll only store LIVE variables at the end of each Basic Block CS-322 Target Generation, Part 2 © Harry H. Porter, 2006
Perform live variable analysis beforehand (during optimization phase)
Assume every variable is live at the end of every basic block We’ll only store LIVE variables at the end of each Basic Block
© Harry H. Porter, 2006
Perform live variable analysis beforehand (during optimization phase)
Assume every variable is live at the end of every basic block
Distinguish temporaries from normal variables... Assume temps are not live between blocks. Assume normal variables are live between blocks. (For more precision, we may want to distinguish which variables are in any Use(Bi) sets We’ll only store LIVE variables at the end of each Basic Block CS-322 Target Generation, Part 2 © Harry H. Porter, 2006
A “Definition” of variable x is an instruction that changes the value x := ... A “Use” of x is an instruction that reads or uses the value ... := ... x ... This statement defines “y”... ...if control flow could allow this value to reach these uses. What are the next uses of the variable it defines? (stmts 106, 109, 112) 104: y := a + 5 105: ... 106: b := y * b 107: ... 108: ... 109: x := b * y 110: b := b - x 111: ... 112: c := y + b
© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t1:L(2) i:L(3) t2:L(5) a:L(0) t1:D t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0)
L(4) Live; next-use in statement 4 L(0) Live; no next-use in this block D Dead CS-322 Target Generation, Part 2 © Harry H. Porter, 2006
**- !Identify all variables used in this block.
- !Work through the block in reverse order
- !Update the table, as we go upward.
(Implementation Idea: Add fields to “VarDecl” to hold this info)
© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t1: D t2: D t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(0) prod: L(0) i: L(0) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... (^) i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(0) prod: L(0) i: L(0)
© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: L(9) a: L(0) b: L(0) prod: L(0) i: D CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: L(9) a: L(0) b: L(0) prod: L(0) i: D
© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(0) prod: L(0) i: L(8) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(0) prod: L(0) i: L(8)
© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: L(6) t6: D t7: D a: L(0) b: L(0) prod: L(6) i: L(8) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: D t3: D t4: D t5: L(6) t6: D t7: D a: L(0) b: L(0) prod: L(6) i: L(8)
© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: L(5) t3: D t4: L(5) t5: D t6: D t7: D a: L(0) b: L(0) prod: L(6) i: L(8) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: L(5) t3: D t4: L(5) t5: D t6: D t7: D a: L(0) b: L(0) prod: L(6) i: L(8)
© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: L(5) t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(4) prod: L(6) i: L(3) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t2:L(5) a:L(0) t1:D t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: D t2: L(5) t3: D t4: D t5: D t6: D t7: D a: L(0) b: L(4) prod: L(6) i: L(3)
© Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t2:L(5) a:L(0) t1:D t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: L(2) t2: D t3: D t4: D t5: D t6: D t7: D a: L(2) b: L(4) prod: L(6) i: L(3) CS-322 Target Generation, Part 2 © Harry H. Porter, 2006 1: t1 := 4 * i 2: t2 := a[t1] 3: t3 := 4 * i 4: t4 := b[t3] 5: t5 := t2 * t 6: t6 := prod + t 7: prod := t 8: t7 := i + 1 9: i := t 10: if i <= 20 goto... t1:L(2) i:L(3) t2:L(5) a:L(0) t1:D t3:L(4) i:L(8) t4:L(5) b:L(0) t3:D t5:L(6) t2:D t4:D t6:L(7) prod:D t5:D prod:L(0) t6:D t7:L(9) i:D i:L(10) t7:D i:L(0) t1: L(2) t2: D t3: D t4: D t5: D t6: D t7: D a: L(2) b: L(4) prod: L(6) i: L(3)
© Harry H. Porter, 2006
**- !Generate code for each Basic Block in isolation.
From statement to statement, we need to remember... For each variable: Is it in a register? Which one? For each register: Which variable(s) does it contain, if any? CS-322 Target Generation, Part 2 © Harry H. Porter, 2006
t1 := 43 * a t1 := t1 + 7 a := t1 * 4
© Harry H. Porter, 2006
t1 := 43 * a t1 := t1 + 7 a := t1 * 4
LD a,R MUL 43,R ST R1,t LD t1,R ADD 7,R ST R1,t LD t1,R MUL 4,R ST R1,a CS-322 Target Generation, Part 2 © Harry H. Porter, 2006
t1 := 43 * a t1 := t1 + 7 a := t1 * 4
LD a,R MUL 43,R ST R1,t LD t1,R ADD 7,R ST R1,t LD t1,R MUL 4,R ST R1,a
LD a,R MUL 43,R ADD 7,R ST R1,t MUL 4,R ST R1,a