Compilers Spilling Alex Aiken Spilling What happens if the graph - - PowerPoint PPT Presentation

compilers
SMART_READER_LITE
LIVE PREVIEW

Compilers Spilling Alex Aiken Spilling What happens if the graph - - PowerPoint PPT Presentation

Compilers Spilling Alex Aiken Spilling What happens if the graph coloring heuristic fails to find a coloring? In this case, we cant hold all values in registers. Some values are spilled to memory Alex Aiken Spilling What if


slide-1
SLIDE 1

Alex Aiken

Compilers

Spilling

slide-2
SLIDE 2

Alex Aiken

Spilling

  • What happens if the graph coloring heuristic fails to

find a coloring?

  • In this case, we can’t hold all values in registers.

– Some values are spilled to memory

slide-3
SLIDE 3

Alex Aiken

Spilling

  • What if all nodes have k or more neighbors?
  • Example: Try to find a 3-coloring of the RIG:

a f e d c b

slide-4
SLIDE 4

Alex Aiken

Spilling

  • Remove a and get stuck

f e d c b

slide-5
SLIDE 5

Alex Aiken

Spilling

  • Pick a node as a candidate for spilling

– A spilled value “lives” in memory – Assume f is chosen

f e d c b

slide-6
SLIDE 6

Alex Aiken

Spilling

  • Remove f and continue the simplification

– Simplification now succeeds: b, d, e, c

e d c b

slide-7
SLIDE 7

Alex Aiken

Spilling

  • Eventually we must 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 r1 r2 r3 ?

slide-8
SLIDE 8

Alex Aiken

Spilling

  • If optimistic coloring fails, we spill f

– Allocate a memory location for f

  • Typically in the current stack frame
  • Call this address fa
  • Before each operation that reads f, insert

f := load fa

  • After each operation that writes f, insert

store f, fa

slide-9
SLIDE 9

Alex Aiken

Spilling

a := b + c d := -a e := d + f f := 2 * e b := d + e e := e - 1 b := f + c

Original code

slide-10
SLIDE 10

Alex Aiken

Spilling

a := b + c d := -a f1 := load fa e := d + f1 f2 := 2 * e store f2, fa b := d + e e := e - 1 f3 := load fa b := f3 + c

The code after spilling f

slide-11
SLIDE 11

Alex Aiken

Spilling

a := b + c d := -a f1 := load fa e := d + f1 f2 := 2 * e store f2, fa b := d + e e := e - 1 f3 := load fa b := f3 + c

Recompute liveness

{b} {c,e} {b} {c,f} {b,c,e,f} {c,d,e,f} {b,c,f} {c,d,f} {a,c,f} {b,c,f} {c,f} {c,d,f1} {c,f2} {c,f3}

slide-12
SLIDE 12

Alex Aiken

Spilling

  • New liveness information is almost as before

– Note f has been split into three temporaries

  • fi is live only

– Between a fi := load fa and the next instruction – Between a store fi, fa and the preceding instr.

  • Spilling reduces the live range of f

– And thus reduces its interferences – Which results in fewer RIG neighbors

slide-13
SLIDE 13

Alex Aiken

Spilling

  • Some edges of the spilled node are removed
  • In our case f still interferes only with c and d
  • And the new RIG is 3-colorable

a f1 e d c b f3 f2

slide-14
SLIDE 14

Alex Aiken

Spilling

  • Additional spills might be required before a coloring is found
  • The tricky part is deciding what to spill

– But any choice is correct

  • Possible heuristics:

– Spill temporaries with most conflicts – Spill temporaries with few definitions and uses – Avoid spilling in inner loops

slide-15
SLIDE 15

Spilling

For the given code fragment and RIG, find the minimum cost spill. In this example, the cost of spilling a node is given by:

A := 1 B := A * 2 C := C - B A := B + 1 A < 16 D := C + 1

A D C B # of occurrences (use or definition) – # of conflicts + 5 if the node corresponds to a variable used in a loop

A B C D

slide-16
SLIDE 16

Alex Aiken

Spilling

  • Register allocation is a “must have” in compilers:

– Because intermediate code uses too many temporaries – Because it makes a big difference in performance

  • Register allocation is more complicated for CISC

machines