Graph-Coloring Register Allocation CS153: Compilers Greg Morrisett.

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
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.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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 (
Lecture 11: Code Optimization CS 540 George Mason University.
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.
Compiler Principles Winter Compiler Principles Loop Optimizations and Register Allocation Mayer Goldberg and Roman Manevich Ben-Gurion University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
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.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Intro to Procedures CS153: Compilers Greg Morrisett.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
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.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
Register Allocation (Slides from Andrew Myers). Main idea Want to replace temporary variables with some fixed set of registers First: need to know which.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
CS 536 Spring Code generation I Lecture 20.
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.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
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.
Data Flow Analysis Compiler Baojian Hua
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Register Allocation John Cavazos University.
Register Allocation Harry Xu CS 142 (b) 02/11/2013.
Final Code Generation and Code Optimization.
More on Loop Optimization Data Flow Analysis CS 480.
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 Liveness analysis and Register Allocation Cheng-Chia Chen.
Optimization Simone Campanoni
Global Register Allocation Based on
Data Flow Analysis Suman Jana
Mooly Sagiv html://
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
University Of Virginia
Liveness Analysis and Register Allocation
Final Code Generation and Code Optimization
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
Register Allocation Harry Xu CS 142 (b) 05/08/2018.
CS 201 Compiler Construction
Presentation transcript:

Graph-Coloring Register Allocation CS153: Compilers Greg Morrisett

Last Time: Dataflow analysis on CFG's Find iterative solution to equations: –Available Expressions (forwards): Din[L] = Dout[L 1 ]  …  Dout[L n ] where pred[L] = {L 1,…,L n } Dout[L] = (Din[L] - Kill[L])  Gen[L] –live variable sets (backwards): LiveIn[L] = Gen[L]  (LiveOut[L] - Kill[L]) LiveOut[L] = LiveIn[L 1 ]  …  LiveIn[L n ] where succ[L] = {L 1,…,L n }

Register Allocation Goal is to assign each temp to one of k registers. (The general problem is NP-complete.) So we use a heuristic that turns out to be poly-time for the particular situation we are in. Build interference graph G –G(x,y)=true if x & y are live at same point. Simplify the graph G –If x has degree < k, push x and simplify G-{x} –if no such x, then we need to spill some temp. Once graph is empty, start popping temps and assigning them registers. –Always have a free register since sub-graph G-{x} can't have >= k interfering temps.

Example from book {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) b := *(f+0) c := e + 8 d := c k := m + 4 j := b {live-out: d,j,k} j k h g d c b m f e

Simplification (4 regs) Stack: j k h g d c b m f e

Simplification Stack: g j k h g d c b m f e

Simplification Stack: g j k h d c b m f e

Simplification Stack: g h j k h d c b m f e

Simplification Stack: g h j k d c b m f e

Simplification Stack: g h k j k d c b m f e

Simplification Stack: g h k j d c b m f e

Simplification Stack: g h k d j d c b m f e

Simplification Stack: g h k d j c b m f e

Simplification Stack: g h k d j j c b m f e

Simplification Stack: g h k d j e c b m f e

Simplification Stack: g h k d j e f c b m f

Simplification Stack: g h k d j e f b c b m

Simplification Stack: g h k d j e f b c c m

Simplification Stack: g h k d j e f b c m

Select: Stack: g h k d j e f b c m j k h g d c b m f e

Select: Stack: g h k d j e f b c j k h g d c b m f e

Select: Stack: g h k d j e f b j k h g d c b m f e

Select: Stack: g h k d j e f j k h g d c b m f e

Select: Stack: g h k d j e j k h g d c b m f e

Select: Stack: g h k d j j k h g d c b m f e

Select: Stack: g h k d j k h g d c b m f e

Select: Stack: g h k j k h g d c b m f e

Select: Stack: g h j k h g d c b m f e

Select: Stack: g j k h g d c b m f e

Use coloring to codegen: g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) b := *(f+0) c := e + 8 d := c k := m + 4 j := b j k h g d c b m f e t1 t2 t3 t4

Use coloring to codegen: t2 := *(t4+12) t1 := t1 - 1 t2 := t2 * t1 t3 := *(t4+8) t1 := *(t4+16) t2 := *(t2+0) t3 := t3 + 8 t3 := t3 t1 := t1 + 4 t4 := t2 j k h g d c b m f e Notice that we can simplify away moves such as this one…

Spilling… Suppose all of the nodes in the graph have degree >= k. Pick one of the nodes to spill. –Picking a high-degree temp will make it more likely that we can color the rest of the graph. –Picking a temp that is used infrequently will likely generate better code. e.g., spilling a register used in a loop when we could spill one accessed outside the loop is a bad idea… Rewrite the code: –after definition of temp, write it into memory. –before use of temp, load it into another temp.

Try it with 3 registers… {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) b := *(f+0) c := e + 8 d := c k := m + 4 j := b {live-out: d,j,k} j k h g d c b m f e

Simplify: Stack: h j k h g d c b m f e

Simplify: Stack: h c j k g d c b m f e

Simplify: Stack: h c g j k g d b m f e

Simplify: Stack: h c g j k d b m f e

3 Regs Stack: h c g j k d b m f e We're stuck…

3 Regs j k d b m f e Don't want to spill j, it's used a lot. Don't want to spill f or k, they have relatively low degree. So let's pick m… {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) b := *(f+0) c := e + 8 d := c k := m + 4 j := b {live-out: d,j,k}

Rewrite: {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) *(fp+ ) := m b := *(f+0) c := e + 8 d := c m2 := *(fp+ ) k := m2 + 4 j := b {live-out: d,j,k} Eliminated this chunk of code from m's live range…

New Interference Graph j k h g d c b m f e {live-in: j, k} g := *(j+12) h := k - 1 f := g * h e := *(j+8) m := *(j+16) *(fp+ ) := m b := *(f+0) c := e + 8 d := c m2 := *(fp+ ) k := m2 + 4 j := b {live-out: d,j,k} m2

Simplify: j k h g d c b m f e Stack: m2 c m2

Simplify: j k h g d b m f e Stack: m2 c h

Back to simplification… Stack: m2 c h m j k g d b m f e

Back to simplification… Stack: m2 c h m f j k g d b f e

Back to simplification… Stack: m2 c h m f g j k g d b e

Back to simplification… Stack: m2 c h m f g e j k d b e

Back to simplification… Stack: m2 c h m f g e j j k d b

Back to simplification… Stack: m2 c h m f g e j k k d b

Back to simplification… Stack: m2 c h m f g e j k d d b

Back to simplification… Stack: m2 c h m f g e j k d b b

Then Color Stack: m2 c h m f g e j k d b j k h g d c b m f e m2

Then Color Stack: m2 c h m f g e j k d j k h g d c b m f e m2

Then Color Stack: m2 c h m f g e j k j k h g d c b m f e m2

Then Color Stack: m2 c h m f g e j j k h g d c b m f e m2

Then Color Stack: m2 c h m f g e j k h g d c b m f e m2

Then Color Stack: m2 c h m f g j k h g d c b m f e m2

Then Color Stack: m2 c h m f j k h g d c b m f e m2

Then Color Stack: m2 c h m j k h g d c b m f e m2

Then Color Stack: m2 c h j k h g d c b m f e m2

Then Color Stack: m2 c j k h g d c b m f e m2

Then Color Stack: m2 j k h g d c b m f e

Register Pressure Some optimizations increase live-ranges: –Copy propagation –Common sub-expression elimination –Loop invariant removal In turn, that can cause the allocator to spill. Copy propagation isn't that useful anyway: –Let register allocator figure out if it can assign the same register to two temps! –Then the copy can go away. –And we don't have to worry about register pressure.

Next Time: How to do coalescing register allocation. An optimistic spilling strategy. Some real-world issues: –caller/callee-saves registers –fixed resources (e.g., mflo, mfhi) –allocation of stack slots