Lecture Outline: Prof. Aiken CS 143 Lecture 16 1 Prof. Aiken CS 143 Lecture 16 2
Lecture Outline: Prof. Aiken CS 143 Lecture 16 1 Prof. Aiken CS 143 Lecture 16 2
• Cache Management
1
The Register Allocation Problem (Cont.) History
f := 2 * e
b := d + e
{b,c,e,f} • This is the register interference graph (RIG)
e := e - 1
– Two temporaries can be allocated to the same
{c,f} {c,f}
register if there is no edge connecting them
b := f + c {b}
{b}
Prof. Aiken CS 143 Lecture 16 11 Prof. Aiken CS 143 Lecture 16 12
2
Example Notes on Register Interference Graphs
r2 e c r4 b := d + e
f := 2 * e
e := e - 1
d r3
• There is no coloring with less than 4 colors b := f + c
• There are 4-colorings of this graph
Prof. Aiken CS 143 Lecture 16 17 Prof. Aiken CS 143 Lecture 16 18
3
Example After Register Allocation Computing Graph Colorings
• Under this coloring the code becomes: • How do we compute graph colorings?
r2 := r3 + r4
r3 := -r2
r2 := r3 + r1
• It isn’t easy:
1. This problem is very hard (NP-hard). No efficient
algorithms are known.
r3 := r3 + r2 – Solution: use heuristics
r1 := 2 * r2
r2 := r2 - 1 2. A coloring might not exist for a given number of
registers
– Solution: later
r3 := r1 + r4
e c e c
d d
• Remove a • Remove d
4
Graph Coloring Example (3) Graph Coloring Example (4)
e c e
• Remove c • Remove b
f f
Stack: {b, c, d, a} Stack: {e, b, c, d, a}
• Remove e • Remove f
r1 f
Stack: {f, e, b, c, d, a} Stack: {e, b, c, d, a}
5
Graph Coloring Example (9) Graph Coloring Example (10)
b r3
r1 f r1 f
Stack: {b, c, d, a} Stack: {c, d, a}
r2 e r2 e
b r3 b r3
r1 f r1 f
Stack: {d, a} Stack: {a}
r2 e c r4 r2 e c r4
d r3
• d can be in the same register as b
e c
d
Prof. Aiken CS 143 Lecture 16 35 Prof. Aiken CS 143 Lecture 16 36
6
What if the Heuristic Fails? What if the Heuristic Fails?
• Remove a and get stuck (as shown below) • Remove f and continue the simplification
– Simplification now succeeds: b, d, e, c
• Pick a node as a candidate for spilling
– A spilled temporary “lives” in memory
– Assume that f is picked as a candidate
b b
f
e c e c
d d
Prof. Aiken CS 143 Lecture 16 37 Prof. Aiken CS 143 Lecture 16 38
• This is the new code after spilling f • The new liveness information after spilling:
a := b + c
{a,c,f} a := b + c {b,c,f}
d := -a d := -a
f := load fa {c,d,f} f := load fa
e := d + f e := d + f {c,d,e,f}
{c,d,f} {c,e}
f := 2 * e b := d + e f := 2 * e b := d + e
{c,f} {b,c,e,f}
store f, fa e := e - 1 store f, fa e := e - 1
{c,f}
{c,f}
f := load fa f := load fa
b := f + c {c,f} b := f + c {b}
{b}
Prof. Aiken CS 143 Lecture 16 41 Prof. Aiken CS 143 Lecture 16 42
7
Recomputing Liveness Information Recompute RIG After Spilling
• New liveness information is almost as before • Some edges of the spilled node are removed
• In our case f still interferes only with c and d
• f is live only • And the resulting RIG is 3-colorable
– Between a f := load fa and the next instruction
– Between a store f, fa and the preceding instr. a
b
f
• Spilling reduces the live range of f
– And thus reduces its interferences e c
– Which results in fewer RIG neighbors for f
d
Prof. Aiken CS 143 Lecture 16 43 Prof. Aiken CS 143 Lecture 16 44
• Additional spills might be required before a • Compilers are very good at managing registers
coloring is found – Much better than a programmer could be
8
Conclusions