Carnegie Mellon Lecture 6 Register Allocation I. Introduction II. Abstraction and the Problem III. Algorithm Reading: Chapter 8.8.4 Before next class:

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
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 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.
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.
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
Register Allocation Mooly Sagiv html://
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.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
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.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
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.
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.
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.
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.
Great Theoretical Ideas in Computer Science for Some.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 4 Developed By:
COMPSCI 102 Introduction to Discrete Mathematics.
Carnegie Mellon Lecture 8 Software Pipelining I. Introduction II. Problem Formulation III. Algorithm Reading: Chapter 10.5 – 10.6 M. LamCS243: Software.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Global Register Allocation Based on
Mooly Sagiv html://
CSPs: Search and Arc Consistency Computer Science cpsc322, Lecture 12
Great Theoretical Ideas In Computer Science
Register Allocation Hal Perkins Autumn 2009
CSPs: Search and Arc Consistency Computer Science cpsc322, Lecture 12
Register Allocation Noam Rinetzky Text book:
CSC D70: Compiler Optimization Register Allocation
CSPs: Search and Arc Consistency Computer Science cpsc322, Lecture 12
Register Allocation Hal Perkins Autumn 2011
Final Code Generation and Code Optimization
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Code Generation Part II
Discrete Mathematics for Computer Science
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

Carnegie Mellon Lecture 6 Register Allocation I. Introduction II. Abstraction and the Problem III. Algorithm Reading: Chapter Before next class: Chapter CS243: Register Allocation1

Carnegie Mellon Pseudo Registers CS243: Register Allocation2 int g; int foo(int a, int b) { if (a+b) { g++; bar(); g--; } return a+b+g; } preg_a = reg2; preg_b = reg3; preg_0 = &g; preg_1 = preg_a + preg_b; preg_g = *preg_0; if(preg_1 == 0) goto Lab; preg_2 = preg_g + 1; *preg_0 = preg_2; bar(); preg_3 = *preg_0; preg_g= preg_3 - 1; *preg_0 = preg_g; Lab :; reg2 = preg_1 + preg_g; return ; Code Selection Machine instructions using pseudo registers Pseudo registers Like machine registers, but infinite number No aliasing

Carnegie Mellon I. Motivation Problem – Allocation of variables (pseudo-registers) to hardware registers in a procedure Perhaps the most important optimization – Directly reduces running time (memory access  register access) – Other optimizations heavily interact PRE Scheduling Loop optimizations CS243: Register Allocation3

Carnegie Mellon Goal Find an assignment for all pseudo-registers, if possible. If there are not enough registers in the machine, choose registers to spill to memory CS243: Register Allocation4

Carnegie Mellon Example CS243: Register Allocation5 B = … = A D = = B + D L1: C = … = A D = = C + D A = … IF A goto L1

Carnegie Mellon II. An Abstraction for Allocation & Assignment Intuitively – Two pseudo-registers interfere if at some point in the program they cannot both occupy the same register. Interference graph: an undirected graph, where – nodes = pseudo-registers – there is an edge between two nodes if their corresponding pseudo-registers interfere What is not represented – Extent of the interference between uses of different variables – Where in the program is the interference CS243: Register Allocation6

Carnegie Mellon Register Allocation and Coloring A graph is n-colorable if: – every node in the graph can be colored with one of the n colors such that two adjacent nodes do not have the same color. Assigning n register (without spilling) = Coloring with n colors – assign a node to a register (color) such that no two adjacent nodes are assigned same registers(colors) Is spilling necessary? = Is the graph n-colorable? To determine if a graph is n-colorable is NP-complete, for n>2 Too expensive Heuristics CS243: Register Allocation7

Carnegie Mellon III. Algorithm Step 1. Build an interference graph a.refining notion of a node b.finding the edges Step 2. Coloring – use heuristics to try to find an n-coloring Success: – colorable and we have an assignment Failure: – graph not colorable, or – graph is colorable, but it is too expensive to color CS243: Register Allocation8

Carnegie Mellon Step 1a. Nodes in an Interference Graph CS243: Register Allocation9 B = … = A D = = B + D L1: C = … = A D = = D + C A = … IF A goto L1 A = 2 = A

Carnegie Mellon Live Ranges and Merged Live Ranges Motivation: to create an interference graph that is easier to color – Eliminate interference in a variable’s “dead” zones. – Increase flexibility in allocation: can allocate same variable to different registers A live range consists of a definition and all the points in a program (e.g. end of an instruction) in which that definition is live. – How to compute a live range? Two overlapping live ranges for the same variable must be merged CS243: Register Allocation10 a = … … = a

Carnegie Mellon Example (Revisited) CS243: Register Allocation11 B = … = A D = (D 2 ) = B + D L1: C = … = A D = (D 1 ) = D + C A = … (A 1 ) IF A goto L1 A = (A 2 ) = D = A {A}{A 1 } {A,C}{A 1,C} {C}{A 1,C} {C,D}{A 1,C,D 1 } {D}{A 1,C,D 1 } {} {A}{A 1 } {A,B}{A 1,B} {B}{A 1,B} {B,D}{A 1,B,D 2 } {D} {A 1,B,D 2 } {D}{A 1,B,C,D 1,D 2 } {A,D}{A 2,B,C,D 1,D 2 } {A}{A 2,B,C,D 1,D 2 } {}{A 2,B,C,D 1,D 2 } (Does not use A, B, C, or D.) liveness reaching-def

Carnegie Mellon Merging Live Ranges Merging definitions into equivalence classes – Start by putting each definition in a different equivalence class – For each point in a program: if (i) variable is live, and (ii) there are multiple reaching definitions for the variable, then: – merge the equivalence classes of all such definitions into one equivalence class From now on, refer to merged live ranges simply as live ranges CS243: Register Allocation12

Carnegie Mellon Step 1b. Edges of Interference Graph Intuitively: – Two live ranges (necessarily of different variables) may interfere if they overlap at some point in the program. – Algorithm: At each point in the program: – enter an edge for every pair of live ranges at that point. An optimized definition & algorithm for edges: – Algorithm: check for interference only at the start of each live range – Faster – Better quality CS243: Register Allocation13

Carnegie Mellon Example 2 CS243: Register Allocation14 A = …L1: B = … IF Q goto L1 IF Q goto L2 L2: … = B … = A

Carnegie Mellon Step 2. Coloring Reminder: coloring for n > 2 is NP-complete Observations: – a node with degree < n  can always color it successfully, given its neighbors’ colors – a node with degree = n  – a node with degree > n  CS243: Register Allocation15

Carnegie Mellon Coloring Algorithm Algorithm: – Iterate until stuck or done Pick any node with degree < n Remove the node and its edges from the graph – If done (no nodes left) reverse process and add colors Example (n = 3): Note: degree of a node may drop in iteration Avoids making arbitrary decisions that make coloring fail CS243: Register Allocation16 B CEA D

Carnegie Mellon What Does Coloring Accomplish? Done: – colorable, also obtained an assignment Stuck: – colorable or not? CS243: Register Allocation17 B CEA D

Carnegie Mellon What if Coloring Fails? Use heuristics to improve its chance of success and to spill code Build interference graph Iterative until there are no nodes left If there exists a node v with less than n neighbor place v on stack to register allocate else v = node chosen by heuristics (least frequently executed, has many neighbors) place v on stack to register allocate (mark as spilled) remove v and its edges from graph While stack is not empty Remove v from stack Reinsert v and its edges into the graph Assign v a color that differs from all its neighbors (guaranteed to be possible for nodes not marked as spilled) CS243: Register Allocation18

Carnegie Mellon Summary Problems: – Given n registers in a machine, is spilling avoided? – Find an assignment for all pseudo-registers, whenever possible. Solution: – Abstraction: an interference graph nodes: live ranges edges: presence of live range at time of definition – Register Allocation and Assignment problems equivalent to n-colorability of interference graph  NP-complete – Heuristics to find an assignment for n colors successful: colorable, and finds assignment not successful: colorability unknown & no assignment CS243: Register Allocation19

Carnegie Mellon Extra Scope of Problem: – Example Showed Whole Function – Separate Inner Loops Interaction with scheduling Don’t want to spill in the inner loop – Inter-procedural Preferencing – Avoid copies on function calls, entries and returns – Glue together inner loop allocation with global Rematerialization CS243: Register Allocation20