CS 201 Compiler Construction

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
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.
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.
1 Optimistic Register Coalescing Sobeeh Almukhaizim UC San Diego Computer Science & Engineering Based on the work of: J. Park and S. Moon School of Electrical.
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.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Register Allocation (via graph coloring)
CMPUT Compiler Design and Optimization1 CMPUT680 - Fall 2003 Topic 7: Register Allocation and Instruction Scheduling José Nelson Amaral
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.
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.
Computer architecture Lecture 11: Reduced Instruction Set Computers Piotr Bilski.
ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K.
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.
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.
Global Register Allocation Based on
Topic Register Allocation
Mooly Sagiv html://
Optimizing Compilers Background
The compilation process
Register Allocation Hal Perkins Autumn 2009
Topic Register Allocation
Register Allocation Noam Rinetzky Text book:
CSC D70: Compiler Optimization Register Allocation
Register Allocation Hal Perkins Autumn 2011
Unit IV Code Generation
CS 201 Compiler Construction
CS 201 Compiler Construction
Register Allocation Hal Perkins Summer 2004
Register Allocation Hal Perkins Autumn 2005
Final Code Generation and Code Optimization
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Chapter 12 Pipelining and RISC
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Code Generation Part II
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
Presentation transcript:

CS 201 Compiler Construction Global Register Allocation

Global Register Allocation Global  Across basic block boundaries Approach: Model register allocation as a graph coloring problem. Graph Coloring: Given an undirected graph G, a coloring of G is an assignment of colors to nodes such that any two nodes connected by an edge have different colors. In general, graph coloring is NP-complete. 4-coloring of a graph

Register Allocation Contd.. Register Allocation as a graph coloring problem: Nodes  Variables Edges  Interferences among variables If two variables are simultaneously live at a program point, they interfere. Thus, they must be assigned different registers.

Live Ranges Nodes can be live ranges of variables instead of variables. Live Range: A minimal subset of definitions and uses of a variable such that: no other uses of the same variable can use definitions from the minimal subset; and no uses of the variable in the minimal subset can use values from definitions of the variable that are not part of the minimal subset.

Live Ranges Contd.. X = = X X live-range 1 live-range 2

Global Register Allocation Perform global analysis, identify live ranges, & compute interferences. Build register interference graph. Coalesce nodes in the graph. Attempt n-coloring of the graph. If none found, then modify program & interference graph by introducing “spill code” and repeat the above process till n-coloring is obtained.

Node Coalescing Combine X & Y into a single node: X is live Y is live Y = X = Y = X = Y X Y Y is live at definition point of X Combine X & Y into a single node: X & Y will be assigned the same register X=Y is effectively eliminated. XY

Node Coalescing Contd.. L= S=L T=S =L =S =T SL= T=SL =SL SLT= =SLT Coalescing combines smaller live ranges into bigger ones in order to eliminate copy assignments.

Attempt n-coloring Color the interference graph using R colors where R is the number of registers. Observation: If there is a node n with < R neighbors, then no matter how the neighbors are colored, there will be at least one color left over to color node n. Remove n and its edges to get G’ Repeat the above process to get G’’ ……. If an empty graph results, R-coloring is possible. Assign colors in reverse of the order in which they were removed.

Attempt Coloring Contd.. Input: Graph G Output: N-coloring of G While there exists n in G with < N edges do Eliminate n & all its edges from G; list n End while If G is empty the for each node i in list in reverse order do Add i & its edges back to G; choose color for i endfor End if

Example 5-coloring (A,B,C,D,E) Empty graph 1,2,5,7,10,11 have degree < 5 Now 3,4,6,8,9 have degree < 5

Example Contd.. 5-coloring (A,B,C,D,E)

Spill if needed What if G is not empty ? Must introduce spill code (loads and stores) Remove a node from G and spill its value into memory. The resulting graph will have fewer edges and therefore we may be able to continue removing nodes according to degree < N condition. Which node to spill? Low spill cost (extra instructions) High degree (more likely nodes with degree < N will result)

CISC vs RISC Processor CISC: spilled data is directly referenced from memory. RISC: spilled data must be loaded before use, i.e. register is needed for a short duration. A=B+C Load C, R2 C spilled R0 = R1 + R2 A – R0, B – R1 Live range of C

Revised Algorithm Remove nodes with degree < N iteratively as long as this process can continue. If the graph is not empty then spill a node and go back to step 1. If nodes were spilled then Insert spill code for all spill decisions Rebuild interference graph Go back to step 1 Assign colors in reverse order.

Enhancements Attempt coloring of spilled nodes Live Range Splitting When coloring nodes in reverse order, see if a color is available for a spilled node. Why? The graph may be colorable and this simple heuristic may work. Live Range Splitting One live range can be split into two such that different registers are available for coloring each result live range. At transition point from one range to another, Register-to-Register transfer instruction is required.

Colorability Interference Graphs for straightline code are interval graphs  coloring is not NP-complete for them. A=B+1 X=Y+1 C=A+1 D=C+1 Write X,D A B X Y C D B A A & Y A & X X & C X & D 2-registers are needed

Colorability Contd.. Arbitrary interference graphs cannot be created from straightline code. Cannot create live range D that - does not interfere with C - does interfere with A and B Can if control flow is allowed

Colorability Contd.. A = B = C = = A = C D = = D = B A B C D

Many Other Issues Different register types Overlapping registers of different sizes Instruction may require a register pair Allocating register to array elements (as opposed to scalars) …….

Sample Problems