Principles of Programming Languages
h"p://www.di.unipi.it/~andrea/Dida2ca/PLP-16/
- Prof. Andrea Corradini
Department of Computer Science, Pisa
- More on code genera;on
Principles of Programming Languages - - PowerPoint PPT Presentation
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/Dida2ca/PLP-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 14 More on code genera;on Summary Next-use informa;on in basic blocks A Code
2
3
4
5
j: a := b + c k: t := a + b [ live(a) = true, live(b) = true, live(t) = true, nextuse(a) = none, nextuse(b) = none, nextuse(t) = none ]
i: b := b + 1
6
j: a := b + c k: t := a + b [ live(a) = true, live(b) = true, live(t) = true, nextuse(a) = none, nextuse(b) = none, nextuse(t) = none ] live(a) = true nextuse(a) = k live(b) = true nextuse(b) = k live(t) = false nextuse(t) = none
i: b := b + 1
7
j: a := b + c k: t := a + b [ live(a) = true, live(b) = true, live(t) = true, nextuse(a) = none, nextuse(b) = none, nextuse(t) = none ]
[ live(a) = true, live(b) = true, live(c) = true, nextuse(a) = k, nextuse(b) = k, nextuse(c) = none ] i: b := b + 1
8
j: a := b + c k: t := a + b live(a) = false nextuse(a) = none live(b) = true nextuse(b) = j live(c) = true nextuse(c) = j live(t) = false nextuse(t) = none [ live(a) = true, live(b) = true, live(t) = true, nextuse(a) = none, nextuse(b) = none, nextuse(t) = none ] [ live(a) = true, live(b) = true, live(c) = true, nextuse(a) = k, nextuse(b) = k, nextuse(c) = none ]
i: b := b + 1
9
j: a := b + c k: t := a + b [ live(a) = true, live(b) = true, live(t) = true, nextuse(a) = none, nextuse(b) = none, nextuse(t) = none ] [ live(a) = true, live(b) = true, live(c) = true, nextuse(a) = k, nextuse(b) = k, nextuse(c) = none ] i: b := b + 1 [ live(b) = true, nextuse(b) = j]
10
11
12
13
14
15
a b c d
t a,R1 b c d R2
u t c a b c,R3 d R2 R1
u t v a b c d R2 R1 R3
u a,d v R2 b c d,R2 R1 R3
18
d a v R2 b c R1 R3
d a v a,R2 b c d,R1 R3
19
1. If y is stored in a register R, return it as Ry 2. If y is not in a register, but exists R empty, return it as Ry 3. If y is not in a register and no register is empty, consider R and check any variable v ∈ RD(R) a. If AD(v) does not contain only R, OK. b. If v = x and x is not an operand in this instruc;on, OK. c. If v is not used later, then OK. d. Otherwise emit ST v,R this is a spill Choose R that minimizes the number of spills and return it as Ry 4. Same algorithm for determining Rz 5. For Rx, similar algorithm, but a. Any register containing x only is OK b. It is possible to return Ry for Rx if y is no more used and if RD(Ry) ={y}. Similarly for Rz.
1. Choose Ry as above 2. Choose Rx = Ry
20
– All live variables in registers are stored (flushed) at the end of a block: this could be not necessary
– Keeping variables in registers in looping code can result in big savings
– Simpler code generator, but less efficient use of registers
21
22
23
a := read(); b := read(); c := read(); a := a + b + c; if (a < 10) { d := c + 8; write(c); } else if (a < 20) { e := 10; d := e + a; write(e); } else { f := 12; d := f + a; write(f); } write(d);
24
a := read(); b := read(); c := read(); a := a+b+c; a < 20 a < 10 d := c+8; write(c); f := 12; d := f+a; write(f); e := 10; d := e+a; write(e); write(d); a b c e f d d d
Live range
a f b d e c
Interference graph: connected vars have
25
a f b d e c
Interference graph
2 1 3 2 1 1
Solve Three registers: a = r2 b = r3 c = r1 d = r2 e = r1 f = r1
r2 := read(); r3 := read(); r1 := read(); r2 := r2 + r3 + r1; if (r2 < 10) { r2 := r1 + 8; write(r1); } else if (r2 < 20) { r1 := 10; r2 := r1 + r2; write(r1); } else { r1 := 12; r2 := r1 + r2; write(r1); } write(r2);
26
– Uses a syntax-directed transla;on scheme encoding the tree- transla;on scheme – Highly ambiguous. In absence of costs, reduce-reduce conflicts choose the longer reduc;on, shiO-reduce prefer shik
28
29
30
31
t2 t1 b e t3 t4 a
d c
32
33
34
t2 t1 b e t3 t4 a
d c
35
36
t2 t1 b e t3 t4 a
Chapter8:CodeGeneration CS5810Spring2008 26
d c