2/22/2016© 2002-08 Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.

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)
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.
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.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
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.
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.
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.
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.
Register Allocation Recap Mooly Sagiv html:// Special Office Hours Wednesday 12-14, Thursday 12-14, Schriber.
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Register Allocation Mooly Sagiv html://
Register Allocation and Spilling via Graph Coloring G. J. Chaitin IBM Research, 1982.
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.
Local Register Allocation & Lab Exercise 1 Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
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.
Compiler Principles Fall Compiler Principles Lecture 12: Register Allocation Roman Manevich Ben-Gurion University.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Global Register Allocation Based on
Mooly Sagiv html://
Register Allocation Hal Perkins Autumn 2009
Local Register Allocation & Lab Exercise 1
Register Allocation Noam Rinetzky Text book:
Register Allocation Hal Perkins Autumn 2011
Instruction Scheduling Hal Perkins Summer 2004
Instruction Scheduling Hal Perkins Winter 2008
CS 201 Compiler Construction
Register Allocation Hal Perkins Summer 2004
Register Allocation Hal Perkins Autumn 2005
Final Code Generation and Code Optimization
Local Register Allocation & Lab Exercise 1
Lecture 16: Register Allocation
Instruction Scheduling Hal Perkins Autumn 2005
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Fall Compiler Principles Lecture 13: Summary
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Instruction Scheduling Hal Perkins Autumn 2011
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008

2/22/2016© Hal Perkins & UW CSEP-2 Agenda Register allocation constraints Top-down and bottom-up local allocation Global allocation – register coloring

2/22/2016© Hal Perkins & UW CSEP-3 k Intermediate code typically assumes infinite number of registers Real machine has k registers available Goals Produce correct code that uses k or fewer registers Minimize added loads and stores Minimize space needed for spilled values Do this efficiently – O(n), O(n log n), maybe O(n 2 )

2/22/2016© Hal Perkins & UW CSEP-4 Register Allocation Task At each point in the code, pick the values to keep in registers Insert code to move values between registers and memory No additional transformations – scheduling should have done its job Minimize inserted code, both dynamically and statically

2/22/2016© Hal Perkins & UW CSEP-5 Allocation vs Assignment Allocation: deciding which values to keep in registers Assignment: choosing specific registers for values Compiler must do both

2/22/2016© Hal Perkins & UW CSEP-6 Basic Blocks A basic block is a maximal length segment of straight-line code (i.e., no branches) Significance If any statement executes, they all execute Barring exceptions or other unusual circumstances Execution totally ordered Many techniques for improving basic blocks – simplest and strongest methods

2/22/2016© Hal Perkins & UW CSEP-7 Local Register Allocation Transformation on basic blocks Produces decent register usage inside a block Need to be careful of inefficiencies at boundaries between blocks Global register allocation can do better, but is more complex

2/22/2016© Hal Perkins & UW CSEP-8 Allocation Constraints Allocator typically won’t allocate all registers to IR values Generally reserve some minimal set of registers F used only for spilling (i.e., don’t dedicate to a particular value

2/22/2016© Hal Perkins & UW CSEP-9 Liveness A value is live between its definition and use. Find definitions (x = …) and uses ( … = … x …) Live range is the interval from definition to last use Can represent live range as an interval [i,j] in the block

2/22/2016© Hal Perkins & UW CSEP-10 Top-Down Allocator Idea Keep busiest values in a dedicated registers Use reserved set, F, for the rest Algorithm Rank values by number of occurrences Allocate first k-F values to registers Add code to move other values between reserved registers and memory

2/22/2016© Hal Perkins & UW CSEP-11 Bottom-Up Allocator Idea Focus on replacement rather than allocation Keep values used “soon” in registers Algorithm Start with empty register set Load on demand When no register available, free one Replacement Spill value whose next use is farthest in the future Prefer clean value to dirty value Sound familiar?

2/22/2016© Hal Perkins & UW CSEP-12 Bottom-Up Allocator Invented about once per decade Sheldon Best, 1955, for Fortran I Laslo Belady, 1965, for analyzing paging algorithms William Harrison, 1975, ECS compiler work Chris Fraser, 1989, LCC compiler Vincenzo Liberatore, 1997, Rutgers Will be reinvented again, no doubt Many arguments for optimality of this

2/22/2016© Hal Perkins & UW CSEP-13 Global Register Allocation A standard technique is graph coloring Use control and dataflow graphs to derive interference graph Nodes are virtual registers (the infinite set) Edge between (t1,t2) when t1 and t2 cannot be assigned to the same register Most commonly, t1 and t2 are both live at the same time Can also use to express constraints about registers, etc. Then color the nodes in the graph Two nodes connected by an edge may not have same color If more than k colors are needed, insert spill code Disclaimer: this works great if there are “enough” registers – not as good on x86 machines

Coloring by Simplification Linear-time approximation that generally gives good results 1. Build: Construct the interference graph 2. Simplify: Color the graph by repeatedly simplification 3. Spill: If simplify cannot reduce the graph completely, mark some node for spilling 4. Select: Assign colors to nodes in the graph 2/22/2016© Hal Perkins & UW CSEP-14

1. Build Construct the interference graph using dataflow analysis to compute the set of temporaries simultaneously live at each program point Add an edge in the graph for each pair of temporaries in the set Repeat for all program points 2/22/2016© Hal Perkins & UW CSEP-15

2. Simplify Heuristic: Assume we have K registers Find a node m with fewer than K neighbors Remove m from the graph. If the resulting graph can be colored, then so can the original graph (the neighbors of m have at most K-1 colors among them) Repeat by removing and pushing on a stack all nodes with degree less than K Each simplification decreases other node degrees – more simplifications possible 2/22/2016© Hal Perkins & UW CSEP-16

3. Spill If simplify stops because all nodes have degree ≥ k, mark some node for spilling This node is in memory during execution  Spilled node no longer interferes with remaining nodes, reducing their degree. Continue by removing spilled node and push on the stack (optimistic – hope that spilled node does not interfer with remaining nodes) 2/22/2016© Hal Perkins & UW CSEP-17

4. Select Assign nodes to colors in the graph: Start with empty graph Rebuild original graph by repeatedly adding node from top of the stack (When we do this, there must be a color for it) When a potential spill node is popped it may not be colorable (neighbors may have k colors already). This is an actual spill – no color assigned 2/22/2016© Hal Perkins & UW CSEP-18

5. Start Over If Select phase cannot color some node (must be a potential spill node), add to the program loads before each use and stores after each definition Creates new temporaries with tiny live ranges Repeat from beginning Iterate until Simplify succeeds In practice a couple of iterations are enough 2/22/2016© Hal Perkins & UW CSEP-19

Complications Need to deal with irregularities in the register set Some operations require dedicated registers (idiv in x86, split address/data registers in M68k and othres) Register conventions like function results, use of registers across calls, etc. Model by precoloring nodes, adding constraints in the graph 2/22/2016© Hal Perkins & UW CSEP-20

2/22/2016© Hal Perkins & UW CSEP-21 Coming Attractions Dataflow and Control flow analysis Overview of optimizations