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 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
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
Complete Graphs and Cycles Complete Graph K 5 Cycle C 5 4
Induced Subgraphs Graph with a C 4 Graph with a C 4 subgraph induced subgraph 5
Induced Subgraphs Graph with a C 4 Graph with a C 4 subgraph induced subgraph Note Induced complete graphs are called cliques 5
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
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
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
Perfect Graphs Definition G is perfect ⇐ ⇒ χ ( H ) = ω ( H ) for each induced subgraph H of G 7
Perfect Graphs Definition G is perfect ⇐ ⇒ χ ( H ) = ω ( H ) for each induced subgraph H of G perfect? 7
Perfect Graphs Definition G is perfect ⇐ ⇒ χ ( H ) = ω ( H ) for each induced subgraph H of G perfect? � � 7
Chordal Graphs Definition G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 8
Chordal Graphs Definition G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 chordal? 8
Chordal Graphs Definition G is chordal ⇐ ⇒ G contains no induced cycles longer than 3 chordal? � � Theorem Chordal graphs are perfect 8
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
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
Dominance Definition Every use of a variable is dominated by its definition start v ← · · · · · · ← v 10
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
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
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
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
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
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
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
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
φ -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
φ -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
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
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
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
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
Coloring Subsequently remove the nodes from the graph d e elimination order a c b 19
Coloring Subsequently remove the nodes from the graph d e elimination order d, a c b 19
Coloring Subsequently remove the nodes from the graph d e elimination order d, e, a c b 19
Coloring Subsequently remove the nodes from the graph d e elimination order d, e, c, a c b 19
Coloring Subsequently remove the nodes from the graph d e elimination order d, e, c, a, a c b 19
Coloring Subsequently remove the nodes from the graph d e elimination order d, e, c, a, b a c b 19
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
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
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
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
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
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
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