Idea of Register Allocation x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy}

Slides:



Advertisements
Similar presentations
Great Theoretical Ideas in Computer Science for Some.
Advertisements

Register Allocation COS 320 David Walker (with thanks to Andrew Myers for many of these slides)
Register Allocation Consists of two parts: Goal : minimize spills
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)
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.
COMPILERS Register Allocation hussein suleman uct csc3005h 2006.
Graph-Coloring Register Allocation CS153: Compilers Greg Morrisett.
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.
Carnegie Mellon Lecture 6 Register Allocation I. Introduction II. Abstraction and the Problem III. Algorithm Reading: Chapter Before next class:
Last time: terminology reminder w Simple graph Vertex = node Edge Degree Weight Neighbours Complete Dual Bipartite Planar Cycle Tree Path Circuit Components.
Computability and Complexity 23-1 Computability and Complexity Andrei Bulatov Search and Optimization.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
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 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
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.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
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)
CS745: Register Allocation© Seth Copen Goldstein & Todd C. Mowry Register Allocation.
Objective - To graph linear equations using x-y charts. One Variable Equations Two Variable Equations 2x - 3 = x = 14 x = 7 One Solution.
Supplementary Lecture – Register Allocation EECS 483 University of Michigan.
Graph Coloring Solution in a Deterministic Machine The deterministic solution to coloring problem uses these steps to assign colors to vertices: 1- Given.
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.
9-1 Quadratic Equations and Functions 9-2 Characteristics of Quadratic Functions 9-3 Graphing Quadratic Functions 9-4 Solving Quadratic Equations by Graphing.
ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K.
Graph Colouring L09: Oct 10. This Lecture Graph coloring is another important problem in graph theory. It also has many applications, including the famous.
Final Code Generation and Code Optimization.
More Examples of Abstract Interpretation Register Allocation.
Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.
Register Allocation CS 471 November 12, CS 471 – Fall 2007 Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
Great Theoretical Ideas in Computer Science for Some.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
More Code Generation and Optimization Pat Morin COMP 3002.
ECE 565 High-Level Synthesis—An Introduction
The compilation process
Register Allocation Hal Perkins Autumn 2009
What is the next line of the proof?
Register Allocation Noam Rinetzky Text book:
Abstract Interpretation Continued
Register Allocation Hal Perkins Autumn 2011
Final Code Generation and Code Optimization
Who Wants to be an Equationaire?. Who Wants to be an Equationaire?
5.1 Solving Systems of Equations by Graphing
Compiler Construction
Fall Compiler Principles Lecture 13: Summary
Assignment #2 (Assignment due: Nov. 06, 2018) v1 v2 v3 v4 v5
(via graph coloring and spilling)
Abstract Interpretation: Analysis Domain (D) Lattices
Register Allocation Harry Xu CS 142 (b) 05/08/2018.
CS 201 Compiler Construction
Presentation transcript:

Idea of Register Allocation x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy} {y,z,x,xy} {x,z,xy,yz} {xy,yz,xz} {r,xz} {} live variable analysis result: program:

Color Variables Avoid Overlap of Same Colors x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy} {y,z,x,xy} {x,z,xy,yz} {xy,yz,xz} {r,xz} {} live variable analysis result: program: R1 R2 R3 R4 Each color denotes a register 4 registers are enough for this program

Color Variables Avoid Overlap of Same Colors x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy} {y,z,x,xy} {x,z,xy,yz} {xy,yz,xz} {r,xz} {} live variable analysis result: program: R1 R2 R3 R4 Each color denotes a register 4 registers are enough for this 7-variable program y yz x z xz xy r

How to assign colors to variables? x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy} {y,z,x,xy} {x,z,xy,yz} {xy,yz,xz} {r,xz} {} live variable analysis result: program: For each pair of variables determine if their lifetime overlaps = there is a point at which they are both alive. Construct interference graph y yz x z xz xy r

Edges between members of each set x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy} {y,z,x,xy} {x,z,xy,yz} {xy,yz,xz} {r,xz} {} live variable analysis result: program: For each pair of variables determine if their lifetime overlaps = there is a point at which they are both alive. Construct interference graph y yz x z xz xy r

Final interference graph x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy} {y,z,x,xy} {x,z,xy,yz} {xy,yz,xz} {r,xz} {} live variable analysis result: program: For each pair of variables determine if their lifetime overlaps = there is a point at which they are both alive. Construct interference graph y yz x z xz xy r

Coloring interference graph x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy} {y,z,x,xy} {x,z,xy,yz} {xy,yz,xz} {r,xz} {} live variable analysis result: program: Need to assign colors (register numbers) to nodes such that: if there is an edge between nodes, then those nodes have different colors.  standard graph vertex coloring problem y:2 yz:2 x:1 z:3 xz:3 xy:4 r:4

Idea of Graph Coloring Register Interference Graph (RIG): – indicates whether there exists a point of time where both variables are live – look at the sets of live variables at all progrma points after running live-variable analysis – if two variables occur together, draw an edge – we aim to assign different registers to such these variables – finding assignment of variables to K registers: corresponds to coloring graph using K colors

All we need to do is solve graph coloring problem NP hard In practice, we have heuristics that work for typical graphs If we cannot fit it all variables into registers, perform a spill: store variable into memory and load later when needed y yz x z xz xy r

Heuristic for Coloring with K Colors Simplify: If there is a node with less than K neighbors, we will always be able to color it! So we can remove such node from the graph (if it exists, otherwise remove other node) This reduces graph size. It is useful, even though incomplete (e.g. planar can be colored by at most 4 colors, yet can have nodes with many neighbors) y yz x z xz xy r y yz x z xz xy y yz x z xy y x z y z y z z

Heuristic for Coloring with K Colors Select Assign colors backwards, adding nodes that were removed If the node was removed because it had <K neighbors, we will always find a color if there are multiple possibilities, we can choose any color y:2 yz:2 x:1 z:3 xz:3 xy:4 y:2 yz:2 x:1 z:3 xy:4 y:2 x:1 z:3 xy:4 y:2 z:3 xy:4 y:2 z:3 y:2 yz:2 x:1 z:3 xz:3 xy:4 r:4

Use Computed Registers x = m[0] y = m[1] xy = x * y z = m[2] yz = y*z xz = x*z r = xy + yz m[3] = res1 + xz y:2 yz:2 x:1 z:3 xz:3 xy:4 r:4 R1 = m[0] R2 = m[1] R4 = R1*R2 R3 = m[2] R2 = R2*R3 R3 = R1*R3 R4 = R4 + R2 m[3] = R4 + R3

Summary of Heuristic for Coloring Simplify (forward, safe): If there is a node with less than K neighbors, we will always be able to color it! so we can remove it from the graph Potential Spill (forward, speculative): If every node has K or more neighbors, we still remove one of them we mark it as node for potential spilling. Then remove it and continue Select (backward): Assign colors backwards, adding nodes that were removed If we find a node that was spilled, we check if we are lucky, that we can color it. if yes, continue if not, insert instructions to save and load values from memory (actual spill). Restart with new graph (a graph is now easier to color as we killed a variable)

Conservative Coalescing Suppose variables tmp1 and tmp2 are both assigned to the same register R and the program has an instruction: tmp2 = tmp1 which moves the value of tmp1 into tmp2. This instruction then becomes R = R which can be simply omitted! How to force a register allocator to assign tmp1 and tmp2 to same register? merge the nodes for tmp1 and tmp2 in the interference graph! this is called coalescing But: if we coalesce non-interfering nodes when there are assignments, then our graph may become more difficult to color, and we may in fact need more registers! Conservative coalescing: coalesce only if merged node of tmp1 and tmp2 will have a small degree so that we are sure that we will be able to color it (e.g. resulting node has degree < K)

Run Register Allocation Ex.3 use 4 registers, coallesce j=i i = 0 s = s + i i = i + b j = i s = s + j + b j = j + 1

Run Register Allocation Ex.3 use 3 registers, coallesce j=i {s,b} i = 0 {s,i,b} s = s + i {s,i,b} i = i + b {s,i,b} j = i {s,j,b} s = s + j + b {j} j = j + 1 {} s i j b s i,j b coalesce color s:1 i,j:2 b:3

Run Register Allocation Ex.3 use 4 registers, coallesce j=i i = 0 s = s + i i = i + b j = i // puf! s = s + j + b j = j + 1 R2 = 0 R1 = R1 + R2 R2 = R2 + R3 R1 = R1 + R2 + R3 R2 = R2 + 1 s:1 i,j:2 b:3

Exam preparation: Last year exam

Simple type checking Suppose that we have class Phone { def getNumber: Int = {... } def call (n: Int) = {... } } class MobilePhone extends Phone { // more functionality } class AntiquePhone(anno: Int) extends Phone { // more functionality } Type-check the following: var a: Phone var b: MobilePhone a = new AntiquePhone(1981) b = new MobilePhone a.call(b.getNumber)

Γ a = new AntiquePhone(1981): Unit Γ new AntiquePhone(1981): AntiquePhone AntiquePhone <: Phone (a,Phone)  Γ Γ new AntiquePhone(1981): Phone Γ b = new MobilePhone : Unit Γ new MobilePhone: MobilePhone (b,MobilePhone)  Γ

Γ a.call: Int => Unit Γ a.call(b.getNumber): Unit Γ b.getNumber: Int Γ Phone call: Int => UnitΓ a : Phone (a,Phone)  Γ Γ b.getNumber: Int (b,MobilePhone)  Γ Γ b : MobilePhone Γ MobilePhone <: Phone Γ b : Phone Γ Phone getNumber: Int

Typing object-oriented language No primitive value (no Int, no Bool) Expressions: x.methodName(p) standard method call where x and p are variable names; new T() class constructor null special value that can be assigned to any variable Statements: var x: T = e var declaration with assignment x = e assignment

Typing object-oriented language The goal of our type system is preventing null-pointer exceptions. We introduce for each regular object type T the null- annotated types : T + meaning that a variable of this type may also be null T - meaning that a variable of this type cannot be null Of course, T - <: T + The programmer should still only write type T in the code, but the type checker will choose one of T + and T - for type checking. The type of variable is determined at its declaration and always has the same type as the expression that is assigned to it. methods can return null. If a method called on a null object, there is a runtime error. Soundness property: If program type checks, runtime error from dereferencing a null value cannot occur

Typing object-oriented language Give sound type rules for this language. Expressions: x.methodName(p) standard method call where x and p are variable names; new T() class constructor null special value that can be assigned to any variable Statements: var x: T = e var declaration with assignment x = e assignment

Γ null : T + Γ new T(): T - Γ x.m(p): T 3 + Γ x: T 2 - Γ p: T 1 + Γ T2 m: T 1 => T 3 Γ x = e: Unit Γ x: T 1 + Γ e: T 1 + Γ x = e: Unit Γ x: T 1 - Γ e: T 1 - Γ var x: T = e; e2 : T 2 Γ+(x, T + ) e2 : T 2 Γ e: T + Γ var x: T = e; e2 : T 2 Γ+(x, T - ) e2 : T 2 Γ e: T -

Typing object-oriented language Induction proof: P(n): For every tree of height ≤ n, (a) any expression type checking to T - can never be null. (b) If the expression type checks for any type, then it will not raise nullPointerException. P(1): If the rule is for null, then it type checks to T + so (a) is true. (b) is true as well. If the rule is for new, then it type checks to T - and is never null so (a) is true. (b) is true as well.

Typing object-oriented language Induction proof: P(n): For every tree of height ≤ n, (a) any expression type checking to T - can never be null. (b) If the expression type checks for any type, then it will not raise nullPointerException. Suppose the rule true for P(n), and let D be a derivation tree of size n+1 of an expressions which type checks. Depending on the last rule applied: if the rule is for methods, then it type checks to T+ so (a) is true. Because P(n)(a) and x type checks to T-, x cannot be null so (b) is true. if the rule is for assignment +, then (a) will be true because we do not change variable which type is T-. (b) is true.

Typing object-oriented language Induction proof: P(n): For every tree of height ≤ n, (a) any expression type checking to T - can never be null. (b) If the expression type checks for any type, then it will not raise nullPointerException. Suppose the rule true for P(n), and let D be a derivation tree of size n+1 of an expressions which type checks. Depending on the last rule applied: if the rule is for assignment -, then e will not be null according to P(n) so after assignment x will not be null so (a) is true. (b) is also true. similar proof for assignment with simultaneous declaration. Therefore, P(n+1) and we conclude the proof.

Live variable analysis 1. x = input() 2. w = x * y = w + x 4. z = y * x 5. u = y - z 6. v = z - w 7. x = z + w 8. w = x * v 9. y = u - v 10.v = x * w 11.x = y + v −{x} −{w, x} −{w, x, y} −{w, y, z} −{w, z, u} −{w, z, u, v} −{x, u, v} −{x, w, u, v} −{x, w, y} −{y, v} −{x} How many registers do we need?

Liveness table x : WX|X|XR | | WX|X|XR W y : | | WX|XR | | | WX|XR z : | | | WX|X|XR | | | | w : | WX|X|X|X|XR WX|XR | u : | | | | WX|X|X|XR | | v : | | | | | WX|X|XR WXR

Liveness table x : WX|X|XR | | WX|X|XR W y : | | WX|XR | | | WX|XR z : | | | WX|X|XR | | | | w : | WX|X|X|X|XR WX|XR | u : | | | | WX|X|X|XR | | v : | | | | | WX|X|XR WXR Answer is : 4