ssa form register allocation
play

SSA-Form Register Allocation Foundations Sebastian Hack Compiler - PowerPoint PPT Presentation

SSA-Form Register Allocation Foundations Sebastian Hack Compiler Construction Course Winter Term 2009/2010 saarland university computer science Overview 1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance -functions 3


  1. SSA-Form Register Allocation Foundations Sebastian Hack Compiler Construction Course Winter Term 2009/2010 saarland university computer science

  2. Overview 1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance φ -functions 3 Interference Graphs Non-SSA Interference Graphs Perfect Elimination Orders Chordal Graphs 4 Interference Graphs of SSA-form Programs Dominance and Liveness Dominance and Interference Spilling Implementing φ -functions 5 Intuition 2

  3. Overview 1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance φ -functions 3 Interference Graphs Non-SSA Interference Graphs Perfect Elimination Orders Chordal Graphs 4 Interference Graphs of SSA-form Programs Dominance and Liveness Dominance and Interference Spilling Implementing φ -functions 5 Intuition 3

  4. Complete Graphs and Cycles Complete Graph K 5 Cycle C 5 4

  5. Induced Subgraphs Graph with a C 4 Graph with a C 4 subgraph induced subgraph 5

  6. Induced Subgraphs Graph with a C 4 Graph with a C 4 subgraph induced subgraph Note Induced complete graphs are called cliques 5

  7. Clique number and Chromatic number Definition ω ( G ) Size of the largest clique in G χ ( G ) Number of colors in a minimum coloring of G 6

  8. Clique number and Chromatic number Definition ω ( G ) Size of the largest clique in G χ ( G ) Number of colors in a minimum coloring of G Corollary ω ( G ) ≤ χ ( G ) holds for each graph G 6

  9. Clique number and Chromatic number Definition ω ( G ) Size of the largest clique in G χ ( G ) Number of colors in a minimum coloring of G Corollary ω ( G ) ≤ χ ( G ) holds for each graph G ω ( G ) 3 2 2 3 χ ( G ) 3 2 3 3 6

  10. Perfect Graphs Definition G is perfect ⇐ ⇒ χ ( H ) = ω ( H ) for each induced subgraph H of G 7

  11. Perfect Graphs Definition G is perfect ⇐ ⇒ χ ( H ) = ω ( H ) for each induced subgraph H of G perfect? 7

  12. Perfect Graphs Definition G is perfect ⇐ ⇒ χ ( H ) = ω ( H ) for each induced subgraph H of G perfect? � � 7

  13. Chordal Graphs Definition G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 8

  14. Chordal Graphs Definition G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 chordal? 8

  15. Chordal Graphs Definition G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 chordal? � � Theorem Chordal graphs are perfect 8

  16. Chordal Graphs Definition G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 chordal? � � Theorem Chordal graphs are perfect Theorem Chordal graphs can be colored optimally in O ( | V | · ω ( G )) 8

  17. Overview 1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance φ -functions 3 Interference Graphs Non-SSA Interference Graphs Perfect Elimination Orders Chordal Graphs 4 Interference Graphs of SSA-form Programs Dominance and Liveness Dominance and Interference Spilling Implementing φ -functions 5 Intuition 9

  18. Dominance Definition Every use of a variable is dominated by its definition start v ← · · · · · · ← v 10

  19. Dominance Definition Every use of a variable is dominated by its definition You cannot reach the use without start passing by the definition v ← · · · Else, you could use uninitialized variables Dominance induces a tree on the control flow graph Sometimes called strict SSA · · · ← v 10

  20. What do φ -functions mean? z 1 ← x 1 z 1 ← y 1 z 2 ← x 2 z 2 ← y 2 z 3 ← x 3 z 3 ← y 3 z 1 ← φ ( x 1 , y 1 ) z 2 ← φ ( x 2 , y 2 ) z 3 ← φ ( x 3 , y 3 ) Frequent misconception Put a sequence of copies in the predecessors 11

  21. What do φ -functions mean? z 1 ← x 1 z 1 ← y 1 z 2 ← x 2 z 2 ← y 2 z 3 ← x 3 z 3 ← y 3 z 1 ← φ ( x 1 , y 1 ) z 2 ← φ ( x 2 , y 2 ) z 3 ← φ ( x 3 , y 3 ) Frequent misconception Put a sequence of copies in the predecessors 11

  22. What do φ -functions mean? Lost Copies B B z 1 ← y 1 z 2 ← y 2 z 3 ← y 3 A C A C z 1 ← φ ( · , y 1 ) z 2 ← φ ( · , y 2 ) z 3 ← φ ( · , y 3 ) Cannot simply push copies in predecessor Copies are also executed if we jump from B to C Need to remove critical edges (edge from B to A ) 12

  23. What do φ -functions mean? Lost Copies B B z 1 ← y 1 z 2 ← y 2 z 3 ← y 3 A C A C z 1 ← φ ( · , y 1 ) z 2 ← φ ( · , y 2 ) z 3 ← φ ( · , y 3 ) Cannot simply push copies in predecessor Copies are also executed if we jump from B to C Need to remove critical edges (edge from B to A ) 12

  24. What do φ -functions mean? φ -swap z 1 ← z 2 z 2 ← z 1 z 1 ← φ ( · , z 2 ) z 2 ← φ ( · , z 1 ) z 1 overwritten before used 13

  25. What do φ -functions mean? φ -swap z 1 ← z 2 z 2 ← z 1 z 1 ← φ ( · , z 2 ) z 2 ← φ ( · , z 1 ) z 1 overwritten before used 13

  26. What do φ -functions mean? ( z 1 , z 2 , z 3 ) ← ( x 1 , x 2 , x 3 ) ( z 1 , z 2 , z 3 ) ← ( y 1 , y 2 , y 3 ) z 1 ← φ ( x 1 , y 1 ) z 2 ← φ ( x 2 , y 2 ) z 3 ← φ ( x 3 , y 3 ) The Reality φ -functions correspond to parallel copies on the incoming edges 14

  27. φ -functions and uses z 1 ← φ ( x 1 , y 1 ) z 2 ← φ ( x 2 , y 2 ) z 3 ← φ ( x 3 , y 3 ) Does not fulfill dominance property φ s do not use their operands in the φ -block Uses happen in the predecessors 15

  28. φ -functions and uses φ r ( x 1 , x 2 , x 3 ) φ r ( y 1 , y 2 , y 3 ) z 1 ← φ ( x 1 , y 1 ) z 2 ← φ ( x 2 , y 2 ) ( z 1 , z 2 , z 3 ) ← φ w z 3 ← φ ( x 3 , y 3 ) Split φ -functions in two parts: Does not fulfill dominance Split critical edges property Read part ( φ r ) in the φ s do not use their operands in predecessors the φ -block Write part ( φ w ) in the block Uses happen in the predecessors Correct modelling of liveness 15

  29. Overview 1 Graph Theory Perfect Graphs Chordal Graphs 2 SSA Form Dominance φ -functions 3 Interference Graphs Non-SSA Interference Graphs Perfect Elimination Orders Chordal Graphs 4 Interference Graphs of SSA-form Programs Dominance and Liveness Dominance and Interference Spilling Implementing φ -functions 5 Intuition 16

  30. Non-SSA Interference Graphs An inconvenient property Program Interference Graph a ← 1 b c b ← a + a d ← 1 a c ← a + 1 e ← a + 1 e ← b + 1 e ← d ← c d The number of live variables at each instruction (register pressure) is 2 However, we need 3 registers for a correct register allocation In theory, this gap can be arbitrarily large (Mycielski Graphs) 17

  31. Graph-Coloring Register Allocation [Chaitin ’80, Briggs ’92, Appel & George ’96, Park & Moon ’04] program changed Spill coloring heuristic failed Build IG Coalesce Color Every undirected graph can occur as an interference graph = ⇒ Finding a k -coloring is NP-complete Color using heuristic = ⇒ Iteration necessary Might introduce spills although IG is k -colorable Rebuilding the IG each iteration is costly 18

  32. Graph-Coloring Register Allocation [Chaitin ’80, Briggs ’92, Appel & George ’96, Park & Moon ’04] program changed Spill coloring heuristic failed Build IG Coalesce Color Spill-code insertion is crucial for the program’s performance Hence, it should be very sensitive to the structure of the program ◮ Place load and stores carefully ◮ Avoid spilling in loops! Here, it is merely a fail-safe for coloring 18

  33. Coloring Subsequently remove the nodes from the graph d e elimination order a c b 19

  34. Coloring Subsequently remove the nodes from the graph d e elimination order d, a c b 19

  35. Coloring Subsequently remove the nodes from the graph d e elimination order d, e, a c b 19

  36. Coloring Subsequently remove the nodes from the graph d e elimination order d, e, c, a c b 19

  37. Coloring Subsequently remove the nodes from the graph d e elimination order d, e, c, a, a c b 19

  38. Coloring Subsequently remove the nodes from the graph d e elimination order d, e, c, a, b a c b 19

  39. Coloring Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e elimination order d, e, c, a, b a c b 19

  40. Coloring Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e elimination order d, e, c, a, a c b 19

  41. Coloring Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e elimination order d, e, c, a c b 19

  42. Coloring Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e elimination order d, e, a c b 19

  43. Coloring Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e elimination order d, a c b 19

  44. Coloring Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e elimination order a c b 19

  45. Coloring Subsequently remove the nodes from the graph Re-insert the nodes in reverse order Assign each node the next possible color d e elimination order a c b But. . . this graph is 3-colorable. We obviously picked a wrong order. 19

Recommend


More recommend