Register Allocation (Slides from Andrew Myers). Main idea Want to replace temporary variables with some fixed set of registers First: need to know which.

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)
Compiler Principles Winter Compiler Principles Loop Optimizations and Register Allocation Mayer Goldberg and Roman Manevich Ben-Gurion University.
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.
A simple register allocation optimization scheme.
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.
Carnegie Mellon Lecture 6 Register Allocation I. Introduction II. Abstraction and the Problem III. Algorithm Reading: Chapter Before next class:
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.
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.
2005 International Symposium on Code Generation and Optimization Progressive Register Allocation for Irregular Architectures David Koes
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
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 and Spilling via Graph Coloring G. J. Chaitin IBM Research, 1982.
CS745: Register Allocation© Seth Copen Goldstein & Todd C. Mowry Register Allocation.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
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.
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.
ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K.
Graph Colouring L09: Oct 10. This Lecture Graph coloring is another important problem in graph theory. It also has many applications, including the famous.
Register Allocation Harry Xu CS 142 (b) 02/11/2013.
Compilation (Semester A, 2013/14) Lecture 10: Register Allocation Noam Rinetzky Slides credit: Roman Manevich, Mooly Sagiv and Eran Yahav.
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.
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.
1 Assembly Language: Function Calls Jennifer Rexford.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Compiler Principles Fall Compiler Principles Lecture 11: Register Allocation Roman Manevich Ben-Gurion University of the Negev.
COMPILERS Liveness Analysis hussein suleman uct csc3003s 2009.
Compilation (Semester A, 2013/14)
Mooly Sagiv html://
Fall Compiler Principles Lecture 9: Register Allocation
The compilation process
Register Allocation Hal Perkins Autumn 2009
Register Allocation Noam Rinetzky Text book:
Topic 12: Register Allocation
Register Allocation Hal Perkins Autumn 2011
Liveness Analysis and Register Allocation
Register Allocation Hal Perkins Summer 2004
Register Allocation Hal Perkins Autumn 2005
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
COMPILERS Liveness Analysis
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

Register Allocation (Slides from Andrew Myers)

Main idea Want to replace temporary variables with some fixed set of registers First: need to know which variables are live after each instruction –Two simultaneously live variables cannot be allocated to the same register

Register allocation For every node n in CFG, we have out[n] –Set of temporaries live out of n Two variables interfere if –both initially live (ie: function args), or –both appear in out[n] for any n How to assign registers to variables?

Interference graph Nodes of the graph = variables Edges connect variables that interfere with one another Nodes will be assigned a color corresponding to the register assigned to the variable Two colors can’t be next to one another in the graph

Interference graph InstructionsLive vars b = a + 2 c = b * b b = c + 1 return b * a

Interference graph InstructionsLive vars b = a + 2 c = b * b b = c + 1 b,a return b * a

Interference graph InstructionsLive vars b = a + 2 c = b * b a,c b = c + 1 b,a return b * a

Interference graph InstructionsLive vars b = a + 2 b,a c = b * b a,c b = c + 1 b,a return b * a

Interference graph InstructionsLive vars a b = a + 2 b,a c = b * b a,c b = c + 1 b,a return b * a

Interference graph InstructionsLive vars a b = a + 2 a,b c = b * b a,c b = c + 1 a,b return b * a a cb eax ebx color register

Interference graph InstructionsLive vars a b = a + 2 a,b c = b * b a,c b = c + 1 a,b return b * a a cb eax ebx color register

Graph coloring Questions: –Can we efficiently find a coloring of the graph whenever possible? –Can we efficiently find the optimum coloring of the graph? –How do we choose registers to avoid move instructions? –What do we do when there aren’t enough colors (registers) to color the graph?

Coloring a graph Kempe’s algorithm [1879] for finding a K- coloring of a graph Assume K=3 Step 1 (simplify): find a node with at most K-1 edges and cut it out of the graph. (Remember this node on a stack for later stages.)

Coloring a graph Once a coloring is found for the simpler graph, we can always color the node we saved on the stack Step 2 (color): when the simplified subgraph has been colored, add back the node on the top of the stack and assign it a color not taken by one of the adjacent nodes

Coloring b ed eax ebx color register a c stack:

Coloring b ed eax ebx color register a stack: c c

Coloring b ed eax ebx color register a stack: e c c

Coloring b ed eax ebx color register a stack: a e c c

Coloring b ed eax ebx color register a stack: b a e c c

Coloring b ed eax ebx color register a stack: d b a e c c

Coloring b ed eax ebx color register a stack: b a e c c

Coloring b ed eax ebx color register a stack: a e c c

Coloring b ed eax ebx color register a stack: e c c

Coloring b ed eax ebx color register a stack: c c

Coloring b ed eax ebx color register a stack: c

Failure If the graph cannot be colored, it will eventually be simplified to graph in which every node has at least K neighbors Sometimes, the graph is still K-colorable! Finding a K-coloring in all situations is an NP-complete problem –We will have to approximate to make register allocators fast enough

Coloring b ed eax ebx color register a c stack:

Coloring b ed eax ebx color register a c stack: d all nodes have 2 neighbours!

Coloring b ed eax ebx color register a c stack: b d

Coloring b ed eax ebx color register a c stack: c e a b d

Coloring b ed eax ebx color register a c stack: e a b d

Coloring b ed eax ebx color register a c stack: a b d

Coloring b ed eax ebx color register a c stack: b d

Coloring b ed eax ebx color register a c stack: d

Coloring b ed eax ebx color register a c stack: We got lucky!

Coloring b ed eax ebx color register a c stack: c b e a d Some graphs can’t be colored in K colors:

Coloring b ed eax ebx color register a c stack: b e a d Some graphs can’t be colored in K colors:

Coloring b ed eax ebx color register a c stack: e a d Some graphs can’t be colored in K colors:

Coloring b ed eax ebx color register a c stack: e a d Some graphs can’t be colored in K colors: no colors left for e!

Spilling Step 3 (spilling): once all nodes have K or more neighbors, pick a node for spilling –Storage on the stack There are many heuristics that can be used to pick a node –not in an inner loop

Spilling code We need to generate extra instructions to load variables from stack and store them These instructions use registers themselves. What to do? –Stupid approach: always keep extra registers handy for shuffling data in and out: what a waste! –Better approach: rewrite code introducing a new temporary; rerun liveness analysis and register allocation Intuition: you were not able to assign a single register to the variable that was spilled but there may be a free register available at each spot where you need to use the value of that variable

Rewriting code Consider: add t1 t2 –Suppose t2 is selected for spilling and assigned to stack location [ebp-24] –Invent new temporary t35 for just this instruction and rewrite: mov t35, [ebp – 24]; add t1, t35 –Advantage: t35 has a very short live range and is much less likely to interfere. –Rerun the algorithm; fewer variables will spill

Precolored Nodes Some variables are pre-assigned to registers –Eg: mul on x86/pentium uses eax; defines eax, edx –Eg: call on x86/pentium Defines (trashes) caller-save registers eax, ecx, edx Treat these registers as special temporaries; before beginning, add them to the graph with their colors

Precolored Nodes Can’t simplify a graph by removing a precolored node Precolored nodes are the starting point of the coloring process Once simplified down to colored nodes start adding back the other nodes as before

Optimizing Moves Code generation produces a lot of extra move instructions –mov t1, t2 –If we can assign t1 and t2 to the same register, we do not have to execute the mov –Idea: if t1 and t2 are not connected in the interference graph, we coalesce into a single variable

Coalescing Problem: coalescing can increase the number of interference edges and make a graph uncolorable Solution 1 (Briggs): avoid creation of high-degree (>= K) nodes Solution 2 (George): a can be coalesced with b if every neighbour t of a: –already interferes with b, or –has low-degree (< K) t1t2t1/t2 coalesce

Simplify & Coalesce Step 1 (simplify): simplify as much as possible without removing nodes that are the source or destination of a move (move-related nodes) Step 2 (coalesce): coalesce move-related nodes provided low-degree node results Step 3 (freeze): if neither steps 1 or 2 apply, freeze a move instruction: registers involved are marked not move-related and try step 1 again

Overall Algorithm Simplify, freeze and coalesce Mark possible spills Color & detect actual spills Rewrite code to implement actual spills Liveness