Low-Level Issues Register Allocation Last lecture Problem – Liveness analysis – Assign an unbounded number of symbolic registers to a fixed number of architectural registers Today – Simultaneously live data must be assigned to different architectural – Register allocation registers Later Goal – More register allocation – Instruction scheduling – Minimize overhead of accessing data – Memory operations (loads & stores) – Register moves CS553 Lecture Register Allocation I 2 CS553 Lecture Register Allocation I 3 Scope of Register Allocation Granularity of Allocation What is allocated to registers? Expression – Variables Local – Live ranges/Webs ( i.e., du-chains with common uses) Loop – Values ( i.e., definitions; same as variables with SSA) Global Interprocedural b 1 t 1 : x := 5 Variables: 2 ( x & y ) Live Ranges/Web: 3 (t 1 → t 2 ,t 4 ; t 2 : y := x t 4 : ... x ... t 2 → t 3 ; b 2 b 3 t 3 : x := y+1 t 5 : x := 3 t 3 ,t 5 → t 6 ) Values: 4 (t 1 , t 2 , t 3 , t 5 , φ (t 3 ,t 5 )) t 6 : ... x ... b 4 What are the tradeoffs? Each allocation unit is given a symbolic register name ( e.g., s1 , s2 , etc .) CS553 Lecture Register Allocation I 4 CS553 Lecture Register Allocation I 5 1
Global Register Allocation by Graph Coloring Interference Graph Example (Variables) Idea [Cocke 71], First allocator [Chaitin 81] a := ... b := ... 1. Construct interference graph G=(N,E) c := ... – Represents notion of “simultaneously live” ... a ... – Nodes are units of allocation ( e.g., variables, live ranges) d := ... a – ∃ edge (n 1 ,n 2 ) ∈ E if n 1 and n 2 are simultaneously live – Symmetric (not reflexive nor transitive) ... c ... ... d ... e b 2. Find k -coloring of G (for k registers) a := ... a := ... – Adjacent nodes can’t have same color ... d ... 3. Allocate the same register to all allocation units of the same color – Adjacent nodes must be allocated to distinct registers d c ... d ... c := ... e := ... s2 ... a ... ... e ... ... b ... s1 s3 CS553 Lecture Register Allocation I 6 CS553 Lecture Register Allocation I 7 Computing the Interference Graph Allocating Registers Using the Interference Graph Use results of live variable analysis K -coloring – Color graph nodes using up to k colors for each symbolic-register s i do – Adjacent nodes must have different colors for each symbolic-register s j ( j < i ) do for each def ∈ {definitions of s i } do Allocating to k registers ≡ finding a k -coloring of the interference graph if ( s j is live at def) then – Adjacent nodes must be allocated to distinct registers E ← E ∪ ( s i , s j ) But. . . Options – Optimal graph coloring is NP-complete – treat all instructions the same – Register allocation is NP-complete, too (must approximate) – treat MOVE instructions special – What if we can’t k -color a graph? (must spill ) – which is better? CS553 Lecture Register Allocation I 8 CS553 Lecture Register Allocation I 9 2
Register Allocation: Spilling Spilling (Original CFG and Interference Graph) a := ... If we can’t find a k-coloring of the interference graph b := ... – Spill variables (nodes) until the graph is colorable c := ... ... a ... d := ... Choosing variables to spill – Choose arbitrarily or a b – Choose least frequently accessed variables ... c ... ... d ... f := ... – Break ties by choosing nodes with the most conflicts in the interference f := ... ... d ... graph e c – Yes, these are heuristics! ... d ... c := ... e := ... f d ... f ... ... e ... ... b ... CS553 Lecture Register Allocation I 10 CS553 Lecture Register Allocation I 11 Spilling (After spilling b ) Simple Greedy Algorithm for Register Allocation a := ... b 1 := ... for each n ∈ N do { select n in decreasing order of weight } M[fp+4] := b 1 if n can be colored then c := ... do it { reserve a register for n } ... a ... d := ... else b 1 Remove n (and its edges) from graph { allocate n to stack (spill) } a b 2 ... d ... ... c ... f := ... f := ... (After spilling b ) a := ... ... d ... e c r1 := ... M[fp+4] := r1 a c := ... ... d ... ... d ... c := ... ... a ... e := ... e := ... f d d := ... ... f ... ... f ... e c ... e ... ... e ... r2 = M[fp+4] b 2 = M[fp+4] ... r2 ... ... b 2 ... f d CS553 Lecture Register Allocation I 12 CS553 Lecture Register Allocation I 13 3
Example Example Attempt to 3-color this graph ( , , ) Attempt to 2-color this graph ( , ) a b Weighted order: Weighted order: a a c b b e c c c d f b f d a e What if you use a different order? CS553 Lecture Register Allocation I 14 CS553 Lecture Register Allocation I 15 Improvement #1: Simplification Phase [Chaitin 81] Simplifying Graph Allocators Chaitin Idea Briggs – Nodes with < k neighbors are guaranteed colorable Remove them from the graph first – Reduces the degree of the remaining nodes Must spill only when all remaining nodes have degree ≥ k CS553 Lecture Register Allocation I 16 CS553 Lecture Register Allocation I 17 4
Algorithm [Chaitin81] Example while interference graph not empty do Attempt to 3-color this graph ( , , ) while ∃ a node n with < k neighbors do simplify Remove n from the graph Push n on a stack if any nodes remain in the graph then { blocked with >= k edges } Stack: a b Possible order: d e Pick a node n to spill { lowest spill-cost or } spill c a Add n to spill set { highest degree } b f e c Remove n from the graph f b if spill set not empty then a c e d Insert spill code for all spilled nodes { store after def; load before use } f d Reconstruct interference graph & start over while stack not empty do Pop node n from stack color or select Allocate n to a register CS553 Lecture Register Allocation I 18 CS553 Lecture Register Allocation I 19 More on Spilling Concepts Chaitin’s algorithm restarts the whole process on spill Register allocation – Necessary, because spill code (loads/stores) uses registers – scope of allocation – Okay, because it usually only happens a couple times – granularity: what is being allocated to a register – order that allocation units are visited in matters in all heuristic algorithms Alternative – Reserve 2-3 registers for spilling Global approaches – Don’t need to start over – greedy coloring – But have fewer registers to work with – coloring with simplification CS553 Lecture Register Allocation I 20 CS553 Lecture Register Allocation I 21 5
Next Time Lecture – More register allocation – Allocation across procedure calls CS553 Lecture Register Allocation I 22 6
Recommend
More recommend