Compiler Construction Recap
description
Transcript of Compiler Construction Recap
Compiler ConstructionRecap
Omer Tripp
Register Allocation(via graph coloring)
Taken from
The Register Allocation Problem
โข Rewrite the intermediate code to use fewer temporaries than there are machine registers
โข Method: assign multiple temporaries to a registerโ But without changing the program behavior
An Example of register allocation
โข Consider the programa := c + de := a + bf := e - 1
โ with the assumption that a and e die after use
โข Temporary a can be โreusedโ after e := a + b
โข The same: Temporary e can be reused after f := e - 1
โข Can allocate a, e, and f all to one register (r1):r1 := r2 + r3
r1 := r1 + r4
r1 := r1 - 1
Basic Register Allocation Idea
โข The value of a dead temporary is not needed for the rest of the computationโ A dead temporary can be reused
โข Basic rule: โ Temporaries t1 and t2 can share the same register
if at any point in the program at most one of t1 or t2 is live !
Algorithm to minimize the number of registers: Part I
โข Compute live variables for each point:
a := b + cd := -ae := d + f
f := 2 * eb := d + ee := e - 1
b := f + c
{b}
{c,e}
{b}
{c,f} {c,f}
{b,c,e,f}
{c,d,e,f}
{b,c,f}
{c,d,f}{a,c,f}
a
f
e
d
c
b
{a,b} not included in any subset
The Register Interference Graph
โข Two temporaries that are live simultaneously cannot be allocated in the same register
โข We construct an undirected graphโ A node for each temporaryโ An edge between t1 and t2 if they are live simultaneously
at some point in the program
โข This is the register interference graph (RIG)โ Two temporaries can be allocated to the same register if
there is no edge connecting them
Register Interference Graph. Example.โข For our example:
a
f
e
d
c
b
โข E.g., b and c cannot be in the same register
โข E.g., b and d can be in the same register
a := b + cd := -ae := d + f
f := 2 * eb := d + ee := e - 1
b := f + c
Graph Coloring. Definitions.
โข A coloring of a graph is an assignment of colors to nodes, such that nodes connected by an edge have different colors
โข A graph is k-colorable if it has a coloring with k colors
Register Allocation Through Graph Coloring
โข In our problem, colors = registersโ We need to assign colors (registers) to graph
nodes (temporaries)
โข Let k = number of machine registers
โข If the RIG is k-colorable then there is a register assignment that uses no more than k registers
Graph Coloring. Example.
โข Consider the example RIGa
f
e
d
c
b
โข There is no coloring with less than 4 colorsโข There are 4-colorings of this graph
r4
r1
r2
r3
r2
r3
Graph Coloring. Example.โข Under this coloring the code becomes:
r2 := r3 + r4r3 := -r2r2 := r3 + r1
r1 := 2 * r2r3 := r3 + r2
r2 := r2 - 1
r3 := r1 + r4
Computing Graph Colorings
โข The remaining problem is to compute a coloring for the interference graph
โข But:1. This problem is very hard (NP-hard). No efficient
algorithms are known2. A coloring might not exist for a given number or
registersโข The solution to (1) is to use heuristicsโข Weโll consider the other problem later
Graph Coloring Heuristicโข Observation:โ Pick a node t with fewer than k neighbors in RIGโ Eliminate t and its edges from RIGโ If the resulting graph has a k-coloring then so does the
original graph
โข Why?โ Let c1,โฆ,cn be the colors assigned to the neighbors of t in the
reduced graphโ Since n < k, we can pick some color for t that is different from
those of its neighbors
Graph Coloring Heuristic
โข The following works well in practice:โ Pick a node t with fewer than k neighborsโ Put t on a stack and remove it from the RIGโ Repeat until the graph has one node
โข Then start assigning colors to nodes on the stack (starting with the last node added)โ At each step pick a color different from those
assigned to already colored neighbors
Graph Coloring Example (1)
โข Remove a and then d
a
f
e
d
c
b
โข Start with the RIG and with k = 4:
Stack: {}
Graph Coloring Example (2)
โข Now all nodes have fewer than 4 neighbors and can be removed: c, b, e, f
f
e c
b
Stack: {d, a}
Graph Coloring Example (2)โข Start assigning colors to: f, e, b, c, d, a
ba
e c r4
fr1
r2
r3
r2
r3
d
What if the Heuristic Fails?
โข What if during simplification we get to a state where all nodes have k or more neighbors ?
โข Example: try to find a 3-coloring of the RIG:
a
f
e
d
c
b
What if the Heuristic Fails?โข Remove a and get stuck (as shown below)
f
e
d
c
b
โข Pick a node as a candidate for spillingโ A spilled temporary โlivesโ in memory
โข Assume that f is picked as a candidate
What if the Heuristic Fails?
โข Remove f and continue the simplificationโ Simplification now succeeds: b, d, e, c
e
d
c
b
What if the Heuristic Fails?โข On the assignment phase we get to the point
when we have to assign a color to f
โข We hope that among the 4 neighbors of f we use less than 3 colors => optimistic coloring
f
e
d
c
b r3
r1r2
r3
?
Spilling
โข Since optimistic coloring failed we must spill temporary f
โข We must allocate a memory location as the home of fโ Typically this is in the current stack frame โ Call this address fa
โข Before each operation that uses f, insert f := load fa
โข After each operation that defines f, insert store f, fa
Spilling. Example.
โข This is the new code after spilling fa := b + cd := -af := load fae := d + f
f := 2 * estore f, fa
b := d + ee := e - 1
f := load fab := f + c
Recomputing Liveness Information
โข The new liveness information after spilling:a := b + cd := -af := load fae := d + f
f := 2 * estore f, fa
b := d + ee := e - 1
f := load fab := f + c
{b}
{c,e}
{b}
{c,f}{c,f}
{b,c,e,f}
{c,d,e,f}
{b,c,f}
{c,d,f}{a,c,f}
{c,d,f}
{c,f}
{c,f}
Recomputing Liveness Information
โข The new liveness information is almost as before
โข f is live onlyโ Between a f := load fa and the next instructionโ Between a store f, fa and the preceding instruction.
โข Spilling reduces the live range of f
โข And thus reduces its interferences
โข Which result in fewer neighbors in RIG for f
Recompute RIG After Spillingโข The only changes are in removing some of the edges
of the spilled node
โข In our case f still interferes only with c and d
โข And the resulting RIG is 3-colorable
a
f
e
d
c
b
Spilling (Cont.)โข Additional spills might be required before a coloring is found
โข The tricky part is deciding what to spill
โข Possible heuristics:โ Spill temporaries with most conflictsโ Spill temporaries with few definitions and usesโ Avoid spilling in inner loops
โข Any heuristic is correct
Graph Coloring
โข Given K registers, show an interference graph with K+1 nodes (i.e. temp variables) where there is actual spill
โข Given 2 registers, show an interference graph with K+1 (i.e. temp variables) where there is no spill
Exam Questions
,' ื 2011-12ืืืขื
(15 )3ืฉืืื ื ืงืืืืช ืขื ืืืื ื . kืขืืืจ ืจืืืกืืจืื
'( 9ื. ) ืขื ืชืืืืืช ืืจืฃ ื ืชืื ืชืืื kื ืง ืฆืืืช ืฉืื ืื , ืืืืฆืืชืื ืืืืืจ ) ืืืืจืื ืืฆืืชืืืืฉ ืฆืืืช ืืืื ืงืืืง ืืื (. k-1ืืืจืฃ ืฉืื ืื
, ืืชืืฆืข ืคืขืืื ืืื ืจืืืกืืจืื ืืืงืฆืืช ืืืืืืจืืชื ืจืืฆืช ืคืขืืื? potential spillืืืืื ืืื.actual spillืืชืืฆืข ื ืืงื?
'( 9ื. ) ืขื ืชืืืืืช ืืจืฃ ื ืชืื ืชืืื k+1ื ืง ืฆืืืช ืฉืื ืื , ืืืืฆืืชืื ืืืืืจ ) ืืืืจืื ืืฆืืชืืืืฉ ืฆืืืช ืืืื ืงืืืง ืืื (.kืืืจืฃ ืฉืื ืื
, ืืชืืฆืข ืคืขืืื ืืื ืจืืืกืืจืื ืืืงืฆืืช ืืืืืืจืืชื ืจืืฆืช ืคืขืืื? potential spillืืืืื ืืื. actual spillืืชืืฆืข ื ืืงื?
,' ื 2011-12ืืืขื
(25 )1ืฉืืื ื ืงืืืืช ( . ืืกืคืืงืืช ืืงืฆืจื ืฆืืื ื ืืืืืืจืขืืช ืืื ืืื ืืืืจืขืืช ืจืฉืืืช ืืื 1-3ื ืชืื ื ืืกืืจ ืฉืืจืืช
ืคืจืื(:; , , ืืงืืืคืืืืจ ืื ืืืช ืืืื ืื ืงืืืคืืืฆืื ืืืื ืจืืฆื ืืืื ืืชืจืืฉ ืืื ืืื
; , ืืืืืจืข ืืชืจืืฉ ืื ืืงืืืคืืืืจ ืืืกืืื ืืฉืื ืืื ืงืืืคืืืฆืื ืืืื ืืืจืช ืื . ืืจืืื ืืืื ืืืืืืืจืืชืืื ืื ืชืื ืื ืืื ื ืืื
. , ืืืื ืืช ืืกืืืจื ื ืืื ืืช ืชืฉืืืืช ืืกืคืจ ืืืฉ ืืืืื
'( 5ื. ) ื ืขืจื . callee-save registerื ืง ืืืืืกื ืืช ืืฉืืืืจ'( 5ื. ) ืื ืืื ืืืื ืฉืืืืืช ืจืงืืจืกืื ืืืงืืืง ืืฉ . LL(1)ื ืง'( 5ื. ) ืืชืืื ืืช ืืชืืื . ICื ืง ืชืืื /* /* ืกืืืจ ืืื ื ืฉืืชืืืื ืืขืจื: 5ื. ) )' ืืงืืฆืื ืคืื ืงืฆืืืช ืฉืชื ืืฉ ืืฉืืืื ืืชืืื ืฉืคืช ืฉืื ืืื ืง ืืืืชื, Cืฉื ืฉื ืืืชื ืืื ืืฉ
. ืฉืื ื ืืืคืืก ืืืืืจืืช ืื ืืื ืคืจืืืจืื'( 5ื. ) ืืืืืืงื ืื xื ืง ื ืืืฉ ืืื ืืืคื ืืื ืืืฉืจ ืืืื . root setืืฉืืืจืจ
,' ื 2011-12ืืืขืืืคืงืืืืช .longjmp (long jump)ื setjmp (set jump)ืืืืืฉ ืืจืืืกืืจืื ื ืืืื ืขื ืงืฉืืื ืืขืืจืจ
, , ื ื ืืืื ืขื ืืืื ืื ืืฉืคืืข ืืืืืฉื ืืืฆื .callee save resigtersืืกืืืจื: ืงืืืคืืืืจืื ืฉื ืกืืืืช ืฉื ืกืืืื ืืฉื ื ืืชืืืืกื
ื 1. ืื ืืช ืฉืืืจืช ืคืจืืฆืืืจื ืื ืฉืื . callee-save registersืกืืื
ื 2. ืืช ืจืง ืฉืืืจืช ืคืจืืฆืืืจื ืื ืฉืื . callee-save registersืกืืื ืฉืืืืฉ ืืื ืขืืฉื ืฉืืื
setjmp
longjmp
,' ื 2011-12ืืืขื
ืืฉืคืช ) ืืืื ืืคืื ืงืฆืื (:ICื ืชืื ื
boolean foo(int a, int b, int c) { boolean x = (a==(b==c)); return x;}
'( ) 7ื. ) ืืฉืคืช ืืืงืืช ืืื ืืื , ICื ืง ืืืคืจืื(? ืืืคืืกืื ืืืงื ืืืื ืืกืืืจื ืืืงืืช ืืื ื ืืื ืื
.) ืืื) ืืืงืื ืืืคืืจืฉ ืืชืื'( 7ื. ) ืฉื ืฉืืืืคืืก ื ื ืื ? intืืืงืื ) booleanืืื a,b,cื ืง ืืฉ(. ืื ืฉืื ื ืชืืืื ืืชืฉืืื ืืื
. ืืืชื, ืืกืืืจื ืืืื