outline
play

Outline What is Register Allocation Webs Interference Graphs - PDF document

Outline What is Register Allocation Webs Interference Graphs Graph Coloring Register Allocation Spilling Live-Range Splitting More Variations and Optimizations Kostis Sagonas 2 Spring 2012 Storing values


  1. Outline • What is Register Allocation • Webs • Interference Graphs • Graph Coloring Register Allocation • Spilling • Live-Range Splitting • More Variations and Optimizations Kostis Sagonas 2 Spring 2012 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 3 Spring 2012 Kostis Sagonas 4 Spring 2012 Issues Issues • Fewer instructions when using registers • On a typical RISC architecture – Most instructions are register-to-register – Additional instructions for memory accesses – All computation takes place in registers • Registers are faster than memory – Load instructions and store instructions transfer – Wider gap in faster, newer processors values between memory and registers – Factor of about 4 bandwidth, factor of about 3 latency • Add two numbers; values in registers – Could be bigger depending on program characteristics • But only a small number of registers available – Usually 32 integer and 32 floating-point registers (e.g. on add r3, r1, r2 SPARC, MIPS, or PowerPC), or much less on x86 or AMD64 – Some of those registers have fixed users (r0, ra, sp, fp) Kostis Sagonas 5 Spring 2012 Kostis Sagonas 6 Spring 2012

  2. 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 – Number 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 the optimization with the most impact! – integer and pointer values in integer registers Kostis Sagonas 7 Spring 2012 Kostis Sagonas 8 Spring 2012 Outline Web-Based Register Allocation • What is Register Allocation • Determine live ranges for each value ( web ) • 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 generation) Kostis Sagonas 9 Spring 2012 Kostis Sagonas 10 Spring 2012 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 a union-find algorithm use y def x s4 use x Kostis Sagonas 11 Spring 2012 Kostis Sagonas 12 Spring 2012

  3. 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 Variations and Optimizations • Issue: instructions compute only from registers • Reserve some registers to hold memory values Kostis Sagonas 13 Spring 2012 Kostis Sagonas 14 Spring 2012 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 non-empty 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 15 Spring 2012 Kostis Sagonas 16 Spring 2012 Example Interference Graph Webs s1 and s2 interfere Representation of webs and their interference s1 def y 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 17 Spring 2012 Kostis Sagonas 18 Spring 2012

  4. Example Outline Webs s1 and s2 interfere • What is Register Allocation s1 Webs s2 and s3 interfere def y • Webs • Interference Graphs def x def x s2 def y use y • Graph Coloring • Spilling s3 s1 s2 • Live-Range Splitting use x use x use y def x • More optimizations s4 use x s3 s4 Kostis Sagonas 19 Spring 2012 Kostis Sagonas 20 Spring 2012 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 in general have the same color s1 s2 – But, good heuristics exist for coloring certain types of graphs – Also, register allocation has linear complexity for straight-line code s3 s4 Kostis Sagonas 21 Spring 2012 Kostis Sagonas 22 Spring 2012 Graph Coloring Example Graph Coloring Example 1 Color 2 Colors Kostis Sagonas 23 Spring 2012 Kostis Sagonas 24 Spring 2012

  5. Graph Coloring Example Graph Coloring Example Still 2 Colors 3 Colors Kostis Sagonas 25 Spring 2012 Kostis Sagonas 26 Spring 2012 Heuristics for Register Coloring Heuristics for Register Coloring • Coloring a graph with N colors • Remove nodes that have degree < N – Push the removed nodes onto a stack • If degree < N (degree of a node = # of edges) • 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 graph becomes 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 27 Spring 2012 Kostis Sagonas 28 Spring 2012 Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s4 s3 s4 s3 s4 Kostis Sagonas 29 Spring 2012 Kostis Sagonas 30 Spring 2012

  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 31 Spring 2012 Kostis Sagonas 32 Spring 2012 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 33 Spring 2012 Kostis Sagonas 34 Spring 2012 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 35 Spring 2012 Kostis Sagonas 36 Spring 2012

  7. 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 37 Spring 2012 Kostis Sagonas 38 Spring 2012 Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s2 s4 s4 s3 s4 s3 s4 Kostis Sagonas 39 Spring 2012 Kostis Sagonas 40 Spring 2012 Coloring Example Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s4 s3 s4 s3 s4 Kostis Sagonas 41 Spring 2012 Kostis Sagonas 42 Spring 2012

  8. Coloring Example Another Coloring Example N = 3 N = 3 s1 s2 s1 s2 s0 s0 s3 s4 s3 s4 Kostis Sagonas 43 Spring 2012 Kostis Sagonas 44 Spring 2012 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 45 Spring 2012 Kostis Sagonas 46 Spring 2012 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 47 Spring 2012 Kostis Sagonas 48 Spring 2012

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