Register allocation Morgensen, Torben. "Register Allocation." Basics of Compiler Design. pp191-208. from (http://www.diku.dk/~torbenm/Basics/basics_lulu2.pdf)

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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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 CS 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
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.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
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:
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
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.
Code Generation Professor Yihjia Tsai Tamkang University.
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.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
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.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Problem Reduction So far we have considered search strategies for OR graph. In OR graph, several arcs indicate a variety of ways in which the original.
Final Code Generation and Code Optimization.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
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.
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.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
More Code Generation and Optimization Pat Morin COMP 3002.
Global Register Allocation Based on
The compilation process
Register Allocation Hal Perkins Autumn 2009
Register Allocation Noam Rinetzky Text book:
Register Allocation Hal Perkins Autumn 2011
CS 201 Compiler Construction
Final Code Generation and Code Optimization
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Code Generation Part II
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

Register allocation Morgensen, Torben. "Register Allocation." Basics of Compiler Design. pp191-208. from (http://www.diku.dk/~torbenm/Basics/basics_lulu2.pdf) Karina Liles Compiler Construction Spring 2013

Overview 9.2 Liveness 9.3 Liveness Analysis 9.4 Interference 9.5 Graph Coloring 9.6 Spilling 9.7 Heuristics

Chapter 9: Introduction Register Allocation: The process of assigning variables to registers. Only a few registers available Some registers must be used for a particular purpose

9.1 Liveness Q: When can two variables share a register? Definition 9.1 A variable is live at some point in the program if the value it contains at that point might conceivably be used in future computations. Conversely, it is dead if there is no way its value can be used in the future. Q: When can two variables share a register?

A: Two variables may share a register if there is no point in the program where they are both live We can use rules to determine when a variable is live: If an instruction uses the contents of a variable, that variable is live at the start of that instruction. (Generated) If a variable is assigned a value in an instruction, and the same variable is not used as an operand in that instruction, then the variable is dead at the start of the instruction, as the value it has at this time is not used before it is overwritten. (Killed) If a variable is live at the end of an instruction and that instruction does not assign a value to the variable, then the variable is also live at the start of the instruction. (Propagated) A variable is live at the end of an instruction if it is live at the start of any of the immediately succeeding instructions. (Propagated)

9.3 Liveness analysis The instruction numbered j (if any) that is listed just after instruction number i is in succ[i], unless i is a GOTO or IF-THEN-ELSE instruction. If instructions are numbered consecutively, j = i + 1. If instruction number i is of the form GOTO l, (the number of) the instruction LABEL l is in succ[i]. Note that there in a correct program will be exactly one LABEL instruction with the label used by the GOTO instruction. If instruction i is IF p THEN lt ELSE lf, (the numbers of) the instructions LABEL lt and LABEL l f are in succ[i].

gen[i]: lists the variables that may be read by instruction i (live at the start of the instruction) kill[i]: lists the variables that may be assigned a value by the instruction

For each instruction i, we use two sets to hold the actual liveness information: in[i] – holds the variables that are live at the start of i out[i] – holds the variables that are live at the end of i

Reverse order out[13], in[13], out[12], in[12],...,out[1], in[1]

9.4 Interference Definition 9.2 A variable x interferes with a variable y if x≠y and there is an instruction i such that x ∈ kill[i], y ∈ out[i] and instruction i is not x := y.

After x:=y, x and y may be live simultaneously, but as they contain the same value, they can still share a register. It may happen that x is not in out[i] even if x is in kill[i], which means that we have assigned to x a value that is definitely not read from x later on. In this case, x is not technically live after instruction i, but it still interferes with any y in out[i]. This interference prevents an assignment to x overwriting a live variable y.

Figure 9.5: Interference graph for the program in figure 9.2

9.5 Register allocation by graph coloring Graph coloring is the process of assigning colors to nodes, such that nodes connected by an edge have different colors A graph is n-colorable if it has a coloring with n colors Nodes = Variables Colors = Registers

9.5 Register allocation by graph coloring Two variables can share a register if they are not connected by an edge in the interference graph. Hence, we must assign to each node in the interference graph a register number such that: Two nodes that share an edge have different register numbers. The total number of different register numbers is no higher than the number of available registers.

Interference Graph

Heuristics for Register Coloring

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

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)

Algorithm 9.3 Initialize: Start with an empty stack. Simplify: If there is a node with less than N edges, put this on the stack along with a list of the nodes it is connected to, and remove it and its edges from the graph. If there is no node with less than N edges, pick any node and do as above. If there are more nodes left in the graph, continue with simplify, otherwise go to select. Select: Take a node and its list of connected nodes from the stack. If possible, give the node a color that is different from the colors of the connected nodes (which are all colored at this point). If this is not possible, coloring fails and we mark the node for spilling (see below). If there are more nodes on the stack, continue with select.

Graph Coloring Example s1 s2 s0 s3 s4

Initialize N = 3 s1 s2 s0 s3 s4

Simplify N = 3 s1 s2 s0 s4 s3 s4

Simplify N = 3 s1 s2 s0 s2 s4 s3 s4

Simplify N = 3 s1 s2 s0 s1 s2 s4 s3 s4

Simplify N = 3 s1 s2 s3 s0 s1 s2 s4 s3 s4

Simplify N = 3 s1 s2 s0 s3 s0 s1 s2 s4 s3 s4

Select N = 3 s1 s2 s0 s3 s0 s1 s2 s4 s3 s4

Select N = 3 s1 s2 s3 s0 s1 s2 s4 s3 s4

Select N = 3 s1 s2 s0 s1 s2 s4 s3 s4

Select N = 3 s1 s2 s0 s1 s2 s4 s3 s4

Select N = 3 s1 s2 s0 s2 s4 s3 s4

Select N = 3 s1 s2 s0 s2 s4 s3 s4

Select N = 3 s1 s2 s0 s4 s3 s4

Select N = 3 s1 s2 s0 s4 s3 s4

Select N = 3 s1 s2 s0 s3 s4

Select N = 3 s1 s2 s0 s3 s4

9.6 Spilling Initialize: Start with an empty stack. Simplify: If there is a node with less than N edges, put this on the stack along with a list of the nodes it is connected to, and remove it and its edges from the graph. If there is no node with less than N edges, pick any node and do as above. If there are more nodes left in the graph, continue with simplify, otherwise go to select. Select: Take a node and its list of connected nodes from the stack. If possible, give the node a color that is different from the colors of the connected nodes (which are all colored at this point). If this is not possible, coloring fails and we mark the node for spilling (see below). If there are more nodes on the stack, continue with select.

9.6 Spilling Choose a memory address addressx where the value of x is stored. In every instruction i that reads or assigns x, we locally in this instruction rename x to xi. Before an instruction i that reads xi, insert the instruction xi := M[addressx]. After an instruction i that assigns xi, insert the instruction M[addressx] := xi. If x is live at the start of the program, add an instruction M[addressx] := x to the start of the program. Note that we use the original name for x here. If x is live at the end of the program, add an instruction x := M[addressx] to the end of the program. Note that we use the original name for x here.

9.6 Spilling Figure 9.6

9.6 Spilling Figure 9.7

9.6 Spilling Figure 9.8

9.6 Spilling Figure 9.9

9.7 Heuristics We may choose a node with close to N neighbors, as this is likely to be colorable in the select phase anyway. For example, if a node has exactly N neighbors, it will be colorable if just two of its neighbors get the same color. We may choose a node with many neighbors that have close to N neighbors of their own, as spilling this node may allow many of these neighbors to be colored. We may look at the program and select a variable that does not cost so much to spill, e.g., a variable that is not used inside a loop.

9.7 Heuristics 9.7.1 Removing redundant moves 9.7.2 Using explicit register numbers

Conclusion I welcome all questions and comments via email: lileskr@email.sc.edu I encourage you to try the exercises at the end of chapter 9 to increase your understanding