Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

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.
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)
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}
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.
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.
CompSci 102 Discrete Math for Computer Science April 19, 2012 Prof. Rodger Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily.
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.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
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.
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)
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.
Lexical Analysis Uses formalism of Regular Languages – Regular Expressions – Deterministic Finite Automata (DFA) – Non-deterministic Finite Automata (NDFA)
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.
Register Allocation CS 471 November 12, CS 471 – Fall 2007 Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Register Allocation: Graph Coloring Compiler Baojian Hua
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.
COMPSCI 102 Introduction to Discrete Mathematics.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
COMPILERS Liveness Analysis hussein suleman uct csc3003s 2009.
Global Register Allocation Based on
Topic Register Allocation
Mooly Sagiv html://
Register Allocation Hal Perkins Autumn 2009
Register Allocation Noam Rinetzky Text book:
CSC D70: Compiler Optimization Register Allocation
Register Allocation Hal Perkins Autumn 2011
Register Allocation Hal Perkins Summer 2004
Register Allocation Hal Perkins Autumn 2005
Final Code Generation and Code Optimization
Lecture 16: Register Allocation
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Discrete Mathematics for Computer Science
COMPILERS Liveness Analysis
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
Lexical Analysis Uses formalism of Regular Languages
Register Allocation Harry Xu CS 142 (b) 05/08/2018.
CS 201 Compiler Construction
Presentation transcript:

Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register allocation Register allocation Popular techniques Popular techniques Local vs. global Local vs. global Graph coloring Graph coloring Bin packing Bin packing

Local Register Assignment Given Given Control-flow graph of basic blocks Control-flow graph of basic blocks List of 3-addr statements per BB List of 3-addr statements per BB Set of “live” scalar values per stmt Set of “live” scalar values per stmt Sets of scalar values used, defined per stmt Sets of scalar values used, defined per stmt Design a local register assignment/allocation algorithm Design a local register assignment/allocation algorithm

Graph Coloring Assign a color to each node in graph Assign a color to each node in graph Two nodes connected by an edge must have different colors Two nodes connected by an edge must have different colors Classic problem in graph theory Classic problem in graph theory NP complete NP complete But good heuristics exist for register allocation But good heuristics exist for register allocation

Live Ranges def y def x use y def x def y use x def x use x use y

Graph Coloring Register Assign Each value is allocated a (symbolic) register Each value is allocated a (symbolic) register “Variables” interfere iff live ranges overlap “Variables” interfere iff live ranges overlap Two interfering values cannot share register Two interfering values cannot share register How can we tell if two values interfere? How can we tell if two values interfere? s1s2 s3s4

Interference Graph Values and interference Values and interference Nodes are the values Nodes are the values Edge between two nodes iff they interfere Edge between two nodes iff they interfere s1s2 s3s4

Graph Coloring Example

3 Colors

Heuristics for Register Coloring Coloring a graph with N colors Coloring a graph with N colors For each node, m For each node, m If degree(m) < N If degree(m) < N Node can always be colored, because Node can always be colored, because After coloring adjacent nodes, at least one color left for current node After coloring adjacent nodes, at least one color left for current node If degree(m) >= N If degree(m) >= N Still may be colorable with N colors Still may be colorable with N colors

Heuristics for Register Coloring Remove nodes that have degree < N Remove nodes that have degree < N Push the removed nodes onto a stack Push the removed nodes onto a stack When all the nodes have degree >= N When all the nodes have degree >= N Find a node to spill (no color for that node) Find a node to spill (no color for that node) Remove that node Remove that node When graph empty, start to color When graph empty, start to color Pop a node from stack back Pop a node from stack back Color node different from adjacent (colored) nodes Color node different from adjacent (colored) nodes

Another Coloring Example s1s2 s3s4 s0 N = 3

Another Coloring Example s1s2 s3s4 s0 N = 3 s4

Another Coloring Example s1s2 s3s4 s0 N = 3 s4

Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2

Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2

Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2

Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

Another Coloring Example s1s2 s3s4 s0 N = 3 s4

Another Coloring Example s1s2 s3s4 s0 N = 3 s4

Another Coloring Example s1s2 s3s4 s0 N = 3

Another Coloring Example s1s2 s3s4 s0 N = 3

Which value to pick? One with interference degree >= N One with interference degree >= N One with minimal spill cost (cost of placing value in memory rather than in register) One with minimal spill cost (cost of placing value in memory rather than in register) What is spill cost? What is spill cost? Cost of extra load and store instructions Cost of extra load and store instructions

One Way to Compute Spill Cost Goal: give priority to values used in loops Goal: give priority to values used in loops So assume loops execute 10 times So assume loops execute 10 times Spill cost = defCost + useCost Spill cost = defCost + useCost defCost = sum over all definitions of cost of a store times 10 nestingDepthOfLoop defCost = sum over all definitions of cost of a store times 10 nestingDepthOfLoop useCost = sum over all uses of cost of a load times 10 nestingDepthOfLoop useCost = sum over all uses of cost of a load times 10 nestingDepthOfLoop Choose the value with the lowest spill cost Choose the value with the lowest spill cost