Compiler Construction Recap

36
Compiler Construction Recap Omer Tripp

description

Compiler Construction Recap. 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 - PowerPoint PPT Presentation

Transcript of Compiler Construction Recap

Page 1: Compiler Construction Recap

Compiler ConstructionRecap

Omer Tripp

Page 2: Compiler Construction Recap

Register Allocation(via graph coloring)

Taken from

Page 3: Compiler Construction Recap

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

Page 4: Compiler Construction Recap

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

Page 5: Compiler Construction Recap

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 !

Page 6: Compiler Construction Recap

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

Page 7: Compiler Construction Recap

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

Page 8: Compiler Construction Recap

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

Page 9: Compiler Construction Recap

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

Page 10: Compiler Construction Recap

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

Page 11: Compiler Construction Recap

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

Page 12: Compiler Construction Recap

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

Page 13: Compiler Construction Recap

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

Page 14: Compiler Construction Recap

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

Page 15: Compiler Construction Recap

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

Page 16: Compiler Construction Recap

Graph Coloring Example (1)

โ€ข Remove a and then d

a

f

e

d

c

b

โ€ข Start with the RIG and with k = 4:

Stack: {}

Page 17: Compiler Construction Recap

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}

Page 18: Compiler Construction Recap

Graph Coloring Example (2)โ€ข Start assigning colors to: f, e, b, c, d, a

ba

e c r4

fr1

r2

r3

r2

r3

d

Page 19: Compiler Construction Recap

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

Page 20: Compiler Construction Recap

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

Page 21: Compiler Construction Recap

What if the Heuristic Fails?

โ€ข Remove f and continue the simplificationโ€“ Simplification now succeeds: b, d, e, c

e

d

c

b

Page 22: Compiler Construction Recap

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

?

Page 23: Compiler Construction Recap

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

Page 24: Compiler Construction Recap

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

Page 25: Compiler Construction Recap

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}

Page 26: Compiler Construction Recap

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

Page 27: Compiler Construction Recap

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

Page 28: Compiler Construction Recap

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

Page 29: Compiler Construction Recap

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

Page 30: Compiler Construction Recap

Exam Questions

Page 31: Compiler Construction Recap

,' ื‘ 2011-12ืžื•ืขื“

(15 )3ืฉืืœื” ื ืงื•ื“ื•ืช ืขื ืžื›ื•ื ื” . kืขื‘ื•ืจ ืจื’ื™ืกื˜ืจื™ื

'( 9ื. ) ืขื ืชืœื•ื™ื•ืช ื’ืจืฃ ื ืชื•ืŸ ืชืœื•ื™ kื ืง ืฆื•ืžืช ืฉื›ืœ ื›ืš , ื‘ื›ืœืฆืžืชื™ื ื›ืœื•ืžืจ ) ื”ืื—ืจื™ื ื”ืฆืžืชื™ืื™ืฉ ืฆื•ืžืช ื•ืœื›ืœ ืงืœื™ืง ื”ื•ื (. k-1ื”ื’ืจืฃ ืฉื›ื ื™ื

, ื™ืชื‘ืฆืข ืคืขืžื™ื ื›ืžื” ืจื’ื™ืกื˜ืจื™ื ืœื”ืงืฆืืช ื”ืืœื’ื•ืจื™ืชื ืจื™ืฆืช ืคืขืžื™ื? potential spillื‘ืžื”ืœืš ื›ืžื”.actual spillื™ืชื‘ืฆืข ื ืžืงื™?

'( 9ื‘. ) ืขื ืชืœื•ื™ื•ืช ื’ืจืฃ ื ืชื•ืŸ ืชืœื•ื™ k+1ื ืง ืฆื•ืžืช ืฉื›ืœ ื›ืš , ื‘ื›ืœืฆืžืชื™ื ื›ืœื•ืžืจ ) ื”ืื—ืจื™ื ื”ืฆืžืชื™ืื™ืฉ ืฆื•ืžืช ื•ืœื›ืœ ืงืœื™ืง ื”ื•ื (.kื”ื’ืจืฃ ืฉื›ื ื™ื

, ื™ืชื‘ืฆืข ืคืขืžื™ื ื›ืžื” ืจื’ื™ืกื˜ืจื™ื ืœื”ืงืฆืืช ื”ืืœื’ื•ืจื™ืชื ืจื™ืฆืช ืคืขืžื™ื? potential spillื‘ืžื”ืœืš ื›ืžื”. actual spillื™ืชื‘ืฆืข ื ืžืงื™?

Page 32: Compiler Construction Recap

,' ื 2011-12ืžื•ืขื“

(25 )1ืฉืืœื” ื ืงื•ื“ื•ืช ( . ืžืกืคื™ืงื•ืช ื‘ืงืฆืจื” ืฆื™ื™ื ื™ ืžื”ืžืื•ืจืขื•ืช ืื—ื“ ืœื›ืœ ืžืื•ืจืขื•ืช ืจืฉื™ืžืช ืœื›ืœ 1-3ื ืชื•ื ื” ื”ืกื‘ืจ ืฉื•ืจื•ืช

ืคืจื™ื˜(:; , , ื”ืงื•ืžืคื™ื™ืœืจ ื‘ื ื™ื™ืช ื‘ื–ืžืŸ ืื• ืงื•ืžืคื™ืœืฆื™ื” ื‘ื–ืžืŸ ืจื™ืฆื” ื‘ื–ืžืŸ ืžืชืจื—ืฉ ื”ื•ื ื”ืื

; , ื”ืžืื•ืจืข ืžืชืจื—ืฉ ื‘ื• ื‘ืงื•ืžืคื™ื™ืœืจ ื”ืžืกื•ื™ื ื”ืฉืœื‘ ืžื”ื• ืงื•ืžืคื™ืœืฆื™ื” ื‘ื–ืžืŸ ื‘ื—ืจืช ืื . ื”ืจืœื‘ื ื˜ื™ื™ื ื•ื”ืืœื’ื•ืจื™ืชืžื™ื ื”ื ืชื•ื ื™ื ืžื‘ื ื™ ืžื”ื

. , ื›ื•ืœืŸ ืืช ื”ืกื‘ื™ืจื™ ื ื›ื•ื ื•ืช ืชืฉื•ื‘ื•ืช ืžืกืคืจ ื•ื™ืฉ ื‘ืžื™ื“ื” 

'( 5ื. ) ื” ืขืจืš . callee-save registerื ืง ืžื”ืžื—ืกื ื™ืช ืžืฉื•ื—ื–ืจ'( 5ื‘. ) ืœื ื”ื•ื ื•ืœื›ืŸ ืฉืžืืœื™ืช ืจืงื•ืจืกื™ื” ื‘ื“ืงื“ื•ืง ื™ืฉ . LL(1)ื ืง'( 5ื’. ) ื‘ืชื•ื›ื ื™ืช ืžืชื’ืœื” . ICื ืง ืชื•ืื /* /* ืกื•ื’ืจ ืœืœื ื‘ ืฉืžืชื—ื™ืœื” ื”ืขืจื”: 5ื“. ) )' ื‘ืงื‘ืฆื™ื ืคื•ื ืงืฆื™ื•ืช ืฉืชื™ ื™ืฉ ื”ืฉื’ื™ืื” ืžืชื’ืœื” ืฉืคืช ืฉื•ื ื™ืื ืง ื•ืื•ืชื, Cืฉืœ ืฉื ืื•ืชื• ืœื”ืŸ ื™ืฉ

. ืฉื•ื ื” ื˜ื™ืคื•ืก ืžื—ื–ื™ืจื•ืช ื”ืŸ ืื‘ืœ ืคืจืžื˜ืจื™ื'( 5ื”. ) ืื•ื‘ื™ื™ืงื˜ ืžื” xื ืง ื ื’ื™ืฉ ืœืœื ื”ื•ืคืš ื”ื•ื ื›ืืฉืจ ืžื™ื™ื“ . root setืžืฉื•ื—ืจืจ

Page 33: Compiler Construction Recap

,' ื 2011-12ืžื•ืขื“ื”ืคืงื•ื“ื•ืช .longjmp (long jump)ื• setjmp (set jump)ืžื™ืžื•ืฉ ื”ืจื’ื™ืกื˜ืจื™ื ื ื™ื”ื•ืœ ืขืœ ืงืฉื™ื™ื ืžืขื•ืจืจ

, , ื” ื ื™ื”ื•ืœ ืขืœ ื‘ื›ืœืœ ืื ื™ืฉืคื™ืข ืžื™ืžื•ืฉืŸ ื›ื™ืฆื“ .callee save resigtersื”ืกื‘ื™ืจื™: ืงื•ืžืคื™ื™ืœืจื™ื ืฉืœ ืกื›ืžื•ืช ืฉืœ ืกื•ื’ื™ื ืœืฉื ื™ ื”ืชื™ื™ื—ืกื™

ื” 1. ื›ืœ ืืช ืฉื•ืžืจืช ืคืจื•ืฆื“ื•ืจื” ื›ืœ ืฉื‘ื” . callee-save registersืกื›ืžื”

ื” 2. ืืช ืจืง ืฉื•ืžืจืช ืคืจื•ืฆื“ื•ืจื” ื›ืœ ืฉื‘ื” . callee-save registersืกื›ืžื” ืฉื™ืžื•ืฉ ื‘ื”ื ืขื•ืฉื” ืฉื”ื™ื

setjmp

longjmp

Page 34: Compiler Construction Recap

,' ื 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ื ืง ื™ืฉ(. ืื ืฉื•ื ื” ืชื”ื™ื™ื” ื”ืชืฉื•ื‘ื” ื”ืื

. ืื•ืชื•, ื”ืกื‘ื™ืจื™ ื”ื‘ื“ืœ

Page 35: Compiler Construction Recap
Page 36: Compiler Construction Recap