Outline • What is register allocation P3 / 2003 • Webs • Interference Graphs Register Allocation • Graph coloring • Spilling • Live-Range Splitting • More optimizations Kostis Sagonas Spring 2003 2 Storing values between defs and uses Issues • Program computes with values • On a typical RISC architecture – value definitions (where computed) – All computation takes place in registers – value uses (where read to compute new values) – Load instructions and store instructions transfer • Values must be stored between def and use values between memory and registers First Option: • Add two numbers; values in memory • store each value in memory at definition load r1, 4(sp) • retrieve from memory at each use load r2, 8(sp) Second Option: add r3,r1,r2 • store each value in register at definition store r3, 12(sp) • retrieve value from register at each use Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 3 4 Issues Issues • Fewer instructions when using registers • On a typical RISC architecture – Most instructions are register-to-register – All computation takes place in registers – Additional instructions for memory accesses – Load instructions and store instructions transfer • Registers are faster than memory values between memory and registers – wider gap in faster, newer processors • Add two numbers; values in registers – Factor of about 4 bandwidth, factor of about 3 latency – Could be bigger depending on program characteristics • But only a small number of registers available – Usually 32 integer and 32 floating-point registers add r3,r1,r2 – Some of those registers have fixed users (r0, ra, sp, fp) Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 5 6 1
Register Allocation What can be put in a register? • Deciding which values to store in a limited • Values stored in compiler-generated temps number of registers • Language-level values • Register allocation has a direct impact on – Values stored in local scalar variables performance – Big constants – Affects almost every statement of the program – Values stored in array elements and object fields – Eliminates expensive memory instructions • Issue: alias analysis – # of instructions goes down due to direct • Register set depends on the data-type manipulation of registers (no need for load and store instructions) – floating-point values in floating point registers – Probably is the optimization with the most impact! – integer and pointer values in integer registers Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 7 8 Outline Web-Based Register Allocation • Determine live ranges for each value ( web ) • What is register allocation • Webs • Determine overlapping ranges (interference) • Interference Graphs • Compute the benefit of keeping each web in a register (spill cost) • Graph coloring • Decide which webs get a register (allocation) • Spilling • Split webs if needed (spilling and splitting) • Live-Range Splitting • Assign hard registers to webs (assignment) • More optimizations • Generate code including spills (code gen.) Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 9 10 Webs Example • Starting Point: def-use chains (DU chains) s1 def y – Connects definition to all reachable uses • Conditions for putting defs and uses into same def x def x s2 web def y use y – Def and all reachable uses must be in same web s3 – All defs that reach same use must be in same web use x use x use y def x • Use a union-find algorithm s4 use x Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 11 12 2
Webs Outline • Web is unit of register allocation • What is register allocation • If web allocated to a given register R • Webs – All definitions computed into R • Interference Graphs – All uses read from R • Graph coloring • If web allocated to a memory location M • Spilling – All definitions computed into M • Live-Range Splitting – All uses read from M • More optimizations • Issue: instructions compute only from registers • Reserve some registers to hold memory values Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 13 14 Convex Sets and Live Ranges Interference • Concept of convex set • Two webs interfere if their live ranges overlap • A set S is convex if (have a nonempty intersection) – A, B in S and C is on a path from A to B implies • If two webs interfere, values must be stored in – C is in S different registers or memory locations • Concept of live range of a web • If two webs do not interfere, can store values in – Minimal convex set of instructions that includes all same register or memory location defs and uses in web – Intuitively, region in which web’s value is live Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 15 16 Example Interference Graph Webs s1 and s2 interfere s1 def y Representation of webs and their interference Webs s2 and s3 interfere – Nodes are the webs – An edge exists between two nodes if they interfere def x def x s2 def y use y s3 s1 s2 use x use x use y def x s4 s3 s4 use x Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 17 18 3
Example Outline Webs s1 and s2 interfere s1 def y • What is register allocation Webs s2 and s3 interfere • Webs • Interference Graphs def x def x s2 def y use y • Graph coloring • Spilling s3 s1 s2 use x use x • Live-Range Splitting use y def x • More optimizations s4 use x s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 19 20 Register Allocation Using Graph Coloring Graph Coloring • Each web is allocated a register • Assign a color to each node in graph – each node gets a register (color) • Two nodes connected to same edge must have • If two webs interfere they cannot use the same different colors register • Classic problem in graph theory – if two nodes have an edge between them, they cannot • NP complete have the same color s1 s2 – But good heuristics exist for register allocation s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 21 22 Graph Coloring Example Graph Coloring Example 1 Color 2 Colors Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 23 24 4
Graph Coloring Example Graph Coloring Example Still 2 Colors 3 Colors Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 25 26 Heuristics for Register Coloring Heuristics for Register Coloring • Coloring a graph with N colors • Remove nodes that have degree < N • If degree < N (degree of a node = # of edges) – Push the removed nodes onto a stack • When all the nodes have degree >= N – Node can always be colored – After coloring the rest of the nodes, there is at least – Find a node to spill (no color for that node) one color left to color the current node – Push that node into the stack • If degree >= N • When empty, start to color – still may be colorable with N colors – Pop a node from stack back – Assign it a color that is different from its connected nodes (if possible) Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 27 28 Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 29 30 5
Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s1 s2 s2 s4 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 31 32 Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s3 s3 s0 s0 s1 s1 s2 s2 s4 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 33 34 Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s3 s0 s0 s1 s1 s2 s2 s4 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 35 36 6
Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s1 s2 s2 s4 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 37 38 Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s2 s4 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 39 40 Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 41 42 7
Coloring Example Another Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 43 44 Another Coloring Example Another Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s1 s4 s4 s3 s4 s3 s4 s1: Possible Spill Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 45 46 Another Coloring Example Another Coloring Example N = 3 N = 3 s1 s2 s1 s2 s2 s3 s3 s0 s0 s1 s1 s4 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 47 48 8
Another Coloring Example Another Coloring Example N = 3 N = 3 s1 s2 s1 s2 s2 s2 s3 s3 s0 s0 s1 s1 s4 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 49 50 Another Coloring Example Another Coloring Example N = 3 N = 3 s1 s2 s1 s2 s3 s3 s0 s0 s1 s1 s4 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 51 52 Another Coloring Example Another Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s1 s1 s4 s4 s3 s4 s3 s4 Kostis Sagonas Spring 2003 Kostis Sagonas Spring 2003 53 54 9
Recommend
More recommend