compilers
play

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


  1. Compilers Spilling Alex Aiken

  2. 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 Alex Aiken

  3. Spilling • What if all nodes have k or more neighbors? • Example: Try to find a 3-coloring of the RIG: a b f c e d Alex Aiken

  4. Spilling • Remove a and get stuck b f c e d Alex Aiken

  5. Spilling • Pick a node as a candidate for spilling – A spilled value “lives” in memory – Assume f is chosen b f c e d Alex Aiken

  6. Spilling • Remove f and continue the simplification – Simplification now succeeds: b, d, e, c b c e d Alex Aiken

  7. 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 r 3 b ? f c r 1 r 2 e r 3 d Alex Aiken

  8. 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 Alex Aiken

  9. Spilling Original code a := b + c d := -a e := d + f b := d + e f := 2 * e e := e - 1 b := f + c Alex Aiken

  10. Spilling a := b + c d := -a The code after spilling f f1 := load fa e := d + f1 f2 := 2 * e b := d + e e := e - 1 store f2, fa f3 := load fa b := f3 + c Alex Aiken

  11. Recompute liveness Spilling a := b + c {a,c,f} d := -a {c,d,f} f1 := load fa {b,c,f} e := d + f1 {c,d,f1} {c,d,e,f} {c,e} f2 := 2 * e b := d + e {c,f2} {b,c,e,f} e := e - 1 store f2, fa {b,c,f} {c,f} {c,f} {b} f3 := load fa {c,f3} b := f3 + c {b} Alex Aiken

  12. 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 Alex Aiken

  13. 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 b f1 f3 c e f2 d Alex Aiken

  14. 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 Alex Aiken

  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 # of occurrences (use or definition) – # of conflicts B := A * 2 + 5 if the node corresponds to a variable used in a loop C := C - B A := B + 1 A A < 16 A B B C C D D := C + 1 D

  16. 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 Alex Aiken

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend