Register Allocation CS 471 November 12, 2007. CS 471 – Fall 2007 Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.

Slides:



Advertisements
Similar presentations
Register Allocation COS 320 David Walker (with thanks to Andrew Myers for many of these slides)
Advertisements

Register Allocation Consists of two parts: Goal : minimize spills
Compiler Support for Superscalar Processors. Loop Unrolling Assumption: Standard five stage pipeline Empty cycles between instructions before the result.
SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Register allocation Morgensen, Torben. "Register Allocation." Basics of Compiler Design. pp from (
Register Allocation Zach Ma.
Register Allocation CS 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Context-Sensitive Interprocedural Points-to Analysis in the Presence of Function Pointers Presentation by Patrick Kaleem Justin.
Coalescing Register Allocation CS153: Compilers Greg Morrisett.
Register Allocation Mooly Sagiv Schrierber Wed 10:00-12:00 html://
COMPILERS Register Allocation hussein suleman uct csc305w 2004.
1 CS 201 Compiler Construction Machine Code Generation.
Graph-Coloring Register Allocation CS153: Compilers Greg Morrisett.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Quick Review of Apr 10 material B+-Tree File Organization –similar to B+-tree index –leaf nodes store records, not pointers to records stored in an original.
Carnegie Mellon Lecture 6 Register Allocation I. Introduction II. Abstraction and the Problem III. Algorithm Reading: Chapter Before next class:
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Register Allocation (Slides from Andrew Myers). Main idea Want to replace temporary variables with some fixed set of registers First: need to know which.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
1 Register Allocation Consists of two parts: –register allocation What will be stored in registers –Only unambiguous values –register assignment Which.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Register Allocation (via graph coloring)
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Chapter 13 Reduced Instruction Set Computers (RISC) Pipelining.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Intermediate Code. Local Optimizations
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Register Allocation and Spilling via Graph Coloring G. J. Chaitin IBM Research, 1982.
CS745: Register Allocation© Seth Copen Goldstein & Todd C. Mowry Register Allocation.
Supplementary Lecture – Register Allocation EECS 483 University of Michigan.
Spring 2014Jim Hogg - UW - CSE - P501P-1 CSE P501 – Compiler Construction Register allocation constraints Local allocation Fast, but poorer code Global.
1 October 18, October 18, 2015October 18, 2015October 18, 2015 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel:
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Register Allocation John Cavazos University.
CMPE 511 Computer Architecture A Faster Optimal Register Allocator Betül Demiröz.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Final Code Generation and Code Optimization.
Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
Global Register Allocation Based on
Mooly Sagiv html://
The compilation process
Register Allocation Hal Perkins Autumn 2009
Register Allocation Noam Rinetzky Text book:
CSC D70: Compiler Optimization Register Allocation
Register Allocation Hal Perkins Autumn 2011
Unit IV Code Generation
Register Allocation Hal Perkins Summer 2004
Register Allocation Hal Perkins Autumn 2005
Final Code Generation and Code Optimization
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

Register Allocation CS 471 November 12, 2007

CS 471 – Fall 2007 Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer instructions – most instrs are reg  reg Cheaper instructions – accessing memory is expensive Option #1: Keep Values in Memory load r1, 4(sp) load r2, 8(sp) add r3,r1,r2 store r3, 12(sp) Option #2: Keep Values in Registers add r3,r1,r2

CS 471 – Fall 2007 Register Allocation – The Catch Very few registers available!! Some registers must be used for a particular purpose Integer vs. floating-point registers Doubleword registers (odd/even pairs) Branch and predicate registers Stack pointer R0 String manipulation (implicit uses)

CS 471 – Fall 2007 Register Allocation Register allocation – determines which of the values (variables, temporaries, large constants) should be in registers at each execution point Register assignment – determines which register should be used by each allocated value Goal - Want to minimize the traffic between the CPU registers and the memory hierarchy Probably the optimization with the most performance impact!

CS 471 – Fall 2007 A Simple Example Original code x  2 y  4 w  x + y z  x + 1 u  x * y x  z * 2 Symbolic register assignment s1  2 s2  4 s3  s1 + s2 s4  s1 + 1 s5  s1 * s2 s6  s4 * 2 Gen – variable is newly defined Kill – old value no longer accessible SSA – Static single assignment Def – on LHS Use – on RHS

CS 471 – Fall 2007 A Simple Example s1  2 s2  4 s3  s1 + s2 s4  s1 + 1 s5  s1 * s2 s6  s4 * 2 Live RangeInterference S1S4 S3S2 S5 S6 Live – variable will be used again (before it is overwritten) Dead Variable will never be used again Value will be overwritten before next use s1 s2 s3 s4 s5 s6

CS 471 – Fall 2007 A Simple Example s1  2 s2  4 s3  s1 + s2 s4  s1 + 1 s5  s1 * s2 s6  s4 * 2 S1S4 S3S2 S5 S6 r1  2 r2  4 r3  r1 + r2 r3  r1 + 1 r1  r1 * r2 r2  r3 * 2 r1  green r2  blue r3  pink Graph ColoringResult

CS 471 – Fall 2007 Register Allocation Determine live ranges for each value (web) Determine overlapping ranges (interference) Compute the benefit of keeping each web in a register (spill cost) Decide which webs get a register (allocation) Split webs if needed (spilling and splitting) Assign hard registers to webs (assignment) Generate code including spills (code gen)

CS 471 – Fall 2007 Webs Starting Point: def-use chains (DU chains) Connects definition to all reachable uses Conditions for putting defs and uses into same web Def and all reachable uses must be in same web All defs that reach same use must be in same web

CS 471 – Fall 2007 Example def y def x use y def x def y use x def x use x use y

CS 471 – Fall 2007 Interference Two webs interfere if their live ranges overlap (have a nonemtpy intersection) If two webs interfere, values must be stored in different registers or memory locations If two webs do not interfere, can store values in same register or memory location

CS 471 – Fall 2007 Example def y def x use y def x def y use x def x use x use y s1 s2 s3 s4 Webs s1 and s2 interfere Webs s2 and s3 interfere

CS 471 – Fall 2007 Interference Graph Representation of webs and their interference Nodes are the webs An edge exists between two nodes if they interfere s1s2 s3s4

CS 471 – Fall 2007 Example s1s2 s3s4 def y def x use y def x def y use x def x use x use y s1 s2 s3 s4 Webs s1 and s2 interfere Webs s2 and s3 interfere

CS 471 – Fall 2007 Register Allocation Using Graph Coloring Each web is allocated a register each node gets a register (color) If two webs interfere they cannot use the same register if two nodes have an edge between them, they cannot have the same color s1s2 s3s4

CS 471 – Fall 2007 Graph Coloring Assign a color to each node in graph Two nodes connected to same edge must have different colors Classic problem in graph theory NP complete But good heuristics exist for register allocation

CS 471 – Fall 2007 Graph Coloring Example #1

CS 471 – Fall 2007 Graph Coloring Example #2

CS 471 – Fall 2007 Graph Coloring Example #3

CS 471 – Fall 2007 Graph Coloring Example #4

CS 471 – Fall 2007 Heuristics for Register Coloring Coloring a graph with N colors If degree < N (degree of a node = # of edges) Node can always be colored After coloring the rest of the nodes, you’ll have at least one color left to color the current node If degree >= N still may be colorable with N colors

CS 471 – Fall 2007 Heuristics for Register Coloring Remove nodes that have degree < N Push the removed nodes onto a stack When all the nodes have degree >= N Find a node to spill (no color for that node) Remove that node When empty, start to color Pop a node from stack back Assign it a color that is different from its connected nodes (since degree < N, a color should exist)

CS 471 – Fall 2007 Coloring Example #5 s1s2 s3s4 s0 N = 3

CS 471 – Fall 2007 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4

CS 471 – Fall 2007 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1 s3 s0

CS 471 – Fall 2007 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1 s3

CS 471 – Fall 2007 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2 s1

CS 471 – Fall 2007 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4 s2

CS 471 – Fall 2007 Coloring Example #5 s1s2 s3s4 s0 N = 3 s4

CS 471 – Fall 2007 Coloring Example #5 s1s2 s3s4 s0 N = 3

CS 471 – Fall 2007 Another Coloring Example s1s2 s3s4 s0 N = 3

CS 471 – Fall 2007 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

CS 471 – Fall 2007 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

CS 471 – Fall 2007 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

CS 471 – Fall 2007 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2 s0

CS 471 – Fall 2007 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2

CS 471 – Fall 2007 Another Coloring Example s1s2 s3s4 s0 N = 3

CS 471 – Fall 2007 When Coloring Fails? Option 1 Pick a web and allocate value in memory All defs go to memory, all uses come from memory Option 2 Split the web into multiple webs In either case, will retry the coloring

CS 471 – Fall 2007 Which web to choose? One with interference degree >= N One with minimal spill cost (cost of placing value in memory rather than in register) What is spill cost? Cost of extra load and store instructions

CS 471 – Fall 2007 Ideal and Useful Spill Costs Ideal spill cost - dynamic cost of extra load and store instructions. Can’t expect to compute this. Don’t know which way branches resolve Don’t know how many times loops execute Actual cost may be different for different executions Solution: Use a static approximation profiling can give instruction execution frequencies or use heuristics based on structure of control flow graph

CS 471 – Fall 2007 One Way to Compute Spill Cost Goal: give priority to values used in loops So assume loops execute 8 or 10 times Spill cost = sum over all def sites: cost of a store instruction times 10 to the loop nesting depth power, plus sum over all use sites: cost of a load instruction times 10 to the loop nesting depth power Choose the web with the lowest spill cost

CS 471 – Fall 2007 Spill Cost Example def x def y use y def y use x use y Spill Cost For x storeCost + loadCost Spill Cost For y 10*storeCost + 10*loadCost With 1 Register, Which Variable Gets Spilled?

CS 471 – Fall 2007 Outline What is register allocation A simple register allocator Webs Interference Graphs Graph coloring Splitting More optimizations

CS 471 – Fall 2007 Splitting Rather Than Spilling Split the web Split a web into multiple webs so that there will be less interference in the interference graph making it N-colorable Spill the value to memory and load it back at the points where the web is split

CS 471 – Fall 2007 Splitting Example def z use z def x def y use x use y use z x y z

CS 471 – Fall 2007 Splitting Example def z use z def x def y use x use y use z x y z xy z

CS 471 – Fall 2007 Splitting Example def z use z def x def y use x use y use z x y z xy z 2 colorable?

CS 471 – Fall 2007 Splitting Example def z use z def x def y use x use y use z x y z

CS 471 – Fall 2007 Splitting Example def z use z def x def y use x use y use z x y z xy z2 z1 2 colorable?

CS 471 – Fall 2007 Splitting Example def z use z def x def y use x use y use z x y z r1 r2 r1 xy z2 z1 2 colorable? YES!

CS 471 – Fall 2007 Splitting Example def z use z str z def x def y use x use y ld z use z x y z r1 r2 r1 xy z2 z1 2 colorable? YES!

CS 471 – Fall 2007 Splitting Heuristic Identify a program point where the graph is not R-colorable (point where # of webs > N) Pick a web that is not used for the largest enclosing block around that point of the program Split that web at the corresponding edge Redo the interference graph Try to re-color the graph

CS 471 – Fall 2007 Cost and benefit of splitting Cost of splitting a node Proportional to number of times split edge has to be crossed dynamically Estimate by its loop nesting Benefit Increase colorability of the nodes the split web interferes with Can approximate by its degree in the interference graph Greedy heuristic pick the live-range with the highest benefit-to- cost ratio to spill

CS 471 – Fall 2007 Further Optimizations Register coalescing Register targeting (pre-coloring) Presplitting of webs Interprocedural register allocation

CS 471 – Fall 2007 Register Coalescing Find register copy instructions sj = si If sj and si do not interfere, combine their webs Pros reduce the number of instructions Cons may increase the degree of the combined node a colorable graph may become non-colorable

CS 471 – Fall 2007 Register Targeting (pre-coloring) Some variables need to be in special registers at a given time first 4 arguments to a function return value Pre-color those webs and bind them to the correct register Will eliminate unnecessary copy instructions

CS 471 – Fall 2007 Pre-splitting of the webs Some live ranges have very large “dead” regions. Large region where the variable is unused Break-up the live ranges need to pay a small cost in spilling but the graph will be very easy to color Can find strategic locations to break-up at a call site (need to spill anyway) around a large loop nest (reserve registers for values used in the loop)

CS 471 – Fall 2007 Interprocedural register allocation Saving/restoring registers across procedure boundaries is expensive especially for programs with many small functions Calling convention is too general and inefficient Customize calling convention per function by doing interprocedural register allocation

CS 471 – Fall 2007 Summary Register Allocation Store values in registers between def and use Can improve performance substantially Key concepts Webs Interference graphs Colorability Splitting