Coalescing 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
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}
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.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Procedure Calls Prof. Sirer CS 316 Cornell University.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Intro to Procedures CS153: Compilers Greg Morrisett.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Computer Architecture CSCE 350
The University of Adelaide, School of Computer Science
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.
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Register Allocation Mooly Sagiv html://
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
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.
Register Allocation Mooly Sagiv Make-Up Class Optional Material Friday 10:00-13:00 Schriber 6 html://
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Run time vs. Compile time
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.
Intermediate Code. Local Optimizations
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Improving Code Generation Honors Compilers April 16 th 2002.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
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.
CS745: Register Allocation© Seth Copen Goldstein & Todd C. Mowry Register Allocation.
Supplementary Lecture – Register Allocation EECS 483 University of Michigan.
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.
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.
Computer structure: Procedure Calls
Mooly Sagiv html://
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
UNIT V Run Time Environments.
Procedures and Calling Conventions
Program and memory layout
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Computer Architecture
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

Coalescing Register Allocation CS153: Compilers Greg Morrisett

Last Time: Basic Graph-Coloring Register Allocation Build interference graph G –use liveness analysis 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. –spilling involves rewriting the code, and then start all over with a new interference graph. 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.

Spilling… 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. –simplifies things to reserve a couple of registers.

Coalescing Register Allocation If we have "x := y" and x and y have no edge in the interference graph, we might be able to assign them the same color. –so this would translate to "ri := ri" which we could simplify away. One idea is to optimistically coalesce nodes in the interference graph. –just take the edges to be the union. –but of course, this may make a k-colorable graph uncolorable!

Example from book {live-in: j, k} g := mem[j+12] h := k - 1 f := g * h e := mem[j+8] m := mem[j+16] b := mem[f] 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

Brigg's Strategy: It's safe to coalesce x & y if the resulting node will have fewer than k neighbors with degree >= k.

George's strategy: We can safely coalesce x & y if for every neighbor t of x, either t already interferes with y or t has degree < k.

New Algorithm: Build: construct the interference graph. –label each node as move-related or not move-related. –move-related: source or destination of a move. Simplify: remove a non-move-related node of low degree from the graph & push on stack. Continue until all nodes are move related and/or have high degree.

New Algorithm Continued Coalesce: coalesce nodes on the reduced graph using either Briggs' or George's conservative strategy. –Simplifying will hopefully have reduced the degree on many of the nodes. –Possibly re-mark the nodes that were coalesced as non-move-related. –go back to simplifying non-move-related, low- degree nodes.

New Algorithm Continued Freeze: if we have some nodes x & y of low degree, but they are move-related and cannot be safely coalesced, we freeze the move involving x & y. –i.e., we can't coalesce x & y. –so go back and treat them as non-move- related. –then, hopefully we can remove them with simplify, then do more coalescing, etc.

Algorithm Continued: Spill: we've gotten down to only high- degree nodes. Pick a potential spill candidate and push it on the stack. –We don't actually do the spill yet, but rather record that this node may need to be spilled. –Just assume that it will no longer interfere with any other temp, so remove their edges. –Go back and try to simplify/coalesce/freeze the graph some more.

Algorithm Continued. Select: once we get the empty graph, start popping nodes off the stack and assign them colors. –we may not have a free color when we run into a potential spill nodes. –in this case, record that this node needs to be actually spilled. –if we reserve two registers, then we don't have to iterate, but if we re-use fresh temps, then we need to iterate constructing a fresh interference graph, etc.

Example from book j k h g d c b m f e Stack: j & b,c & d are move-related

Example from book j k h g d c b m f e Stack:

Example from book j k h d c b m f e Stack: g

Example from book j k h d c b m f e Stack: g h

Example from book j k d c b m f e Stack: g h k

Example from book j d c b m f e Stack: g h k f

Example from book j d c b m e Stack: g h k f e

Example from book j d c b m Stack: g h k f e m

Example from book j d c b Stack: g h k f e m At this point, all nodes are move-related. So start coalescing...

Example from book jb d c Stack: g h k f e m At this point, all nodes are move-related. So start coalescing...

Example from book d c Stack: g h k f e m jb

Example from book dc Stack: g h k f e m jb

Example from book Stack: g h k f e m jb dc

Now Select… Stack: g h k f e m jb cd j k h g d c b m f e t1 t2 t3 t4

Now Select… Stack: g h k f e m jb j k h g d c b m f e t1 t2 t3 t4

Now Select… Stack: g h k f e m j k h g d c b m f e t1 t2 t3 t4

Now Select… Stack: g h k f e j k h g d c b m f e t1 t2 t3 t4

Now Select… Stack: g h k f j k h g d c b m f e t1 t2 t3 t4

Now Select… Stack: g h k j k h g d c b m f e t1 t2 t3 t4

Now Select… Stack: g h j k h g d c b m f e t1 t2 t3 t4

Now Select… Stack: g j k h g d c b m f e t1 t2 t3 t4

Now Select… Stack: g j k h g d c b m f e t1 t2 t3 t4

Now Rewrite Code… j k h g d c b m f e t1 t2 t3 t4 g := mem[j+12] h := k - 1 f := g * h e := mem[j+8] m := mem[j+16] b := mem[f] c := e + 8 d := c k := m + 4 j := b

Now Rewrite Code… j k h g d c b m f e t1 t2 t3 t4 t2 := mem[t4+12] t1 := t1 - 1 t3 := t2 * t1 t1 := mem[t4+8] t2 := mem[t4+16] t4 := mem[t3] t3 := t3 + 8 t3 := t3 t1 := t1 + 4 t4 := t4

…and simplify moves j k h g d c b m f e t1 t2 t3 t4 t2 := mem[t4+12] t1 := t1 - 1 t3 := t2 * t1 t1 := mem[t4+8] t2 := mem[t4+16] t4 := mem[f] t3 := t1 + 8 t1 := t1 + 4

Some Practicalities The IL often includes machine registers –e.g., FP, $a0-a3, $v0-v1 –allows us to expose issues of calling convention over which we don't have control. We can treat the machine registers as pre-colored temps. –Their assignment to a physical register is already determined. –But note that select & coalesce may put a different temp in the same physical register, as long as it doesn't interfere.

Using Physical Registers Within a procedure: –move arguments from $a0-a3 (and Mem[$fp+offset]) into fresh temps, move results into $v0-$v1. –manipulate the temps directly within the procedure body instead of the physical registers, giving the register allocation maximum freedom in assignment, and minimizing the lifetimes of pre-colored nodes. –register allocation will hopefully coalesce the argument registers with the temps, eliminating the moves. –ideally, if we end up spilling a temp corresponding to an argument, we should write it back in the already reserved space on the stack…

Note: We cannot simplify a pre-colored node: –removing a node during simplification happens because we expect to be able to assign it any color that doesn't conflict with the neighbors. –but we don't have a choice for pre-colored nodes. –Trick: treat physical nodes as having "infinite degree" in interference graph. Similarly, we cannot spill a pre-colored node.

Callee-Saves Registers Callee-Saves register r: –it's "defined" upon entry to the procedure –it's "used" upon exit from the procedure. –trick: move it into a fresh temp –ideally, the temp will be coalesced with the callee-saves register (getting rid of the move.) –otherwise, we have the freedom to spill the temp.

Caller Saves Registers Want to assign a temp to a caller-saves register only when it's not live across a function call (for then we have to save/restore it.) So treat a function call as "defining" all of the caller-saves registers. –(callee might move values into them.) –now any temps that are live across the call will interfere, and assignment will try to find different registers to assign the temps.

Example (p. 238 in book) We're compiling the following C procedure: int f(int a, int b) { int d = 0; int e = a; do { d = d+b; e = e-1; } while (e > 0); return d; } Assume we have a target machine with 3 registers, where r1 and r2 are caller-saves and r3 is callee-saves.

Generated CFG: f: c := $r3 ; preserve callee a := $r1 ; move arg1 into a b := $r2 ; move arg2 into b d := 0 e := a loop:d := d + b e := e - 1 if e > 0 loop else end end:r1 := d ; return d r3 := c ; restore callee return ; $r3,$r1 live out

Interference Graph f: c := $r3 a := $r1 b := $r2 d := 0 e := a L:d := d + b e := e - 1 if e > 0 L else E E:r1 := d r3 := c return r3 r1 r2 a b c d e move-related No simplify, freeze, or coalesce is possible…

Spilling: r3 r1 r2 a b c d e Node c is a good candidate for spilling. So push it as a potential spill. Stack: sp(c)

After Spilling c: r3 r1 r2 a b d e Now we can safely coalesce a & e. Stack: sp(c)

After Coalescing a & e: r3 r1 br2 d ae Now we can safely coalesce b & r2. Stack: sp(c) r2 b

After Coalescing b & r2: r3 r1 br2 d ae Now we can safely coalesce r1 & ae. Stack: sp(c)

Constrained Nodes: r3 r1ae br2 d We cannot safely coalesce r1ae & d because they are constrained. When we coalesce, and we have both a non-move edge and a move- edge, we can't drop the non- move edge…

Simplify: r3 r1ae br2 d At this point, we can simplify d. Stack: sp(c)

Start Selecting: r3 r1ae br2 Now we only have pre-colored nodes left… Stack: sp(c), d

Start Selecting: r3 br2 We pop d and assign it a color. Stack: sp(c) r3 r1 r2 a b c d e

Optimism Failed r3 br2 We pop c but find out that we must do an actual spill. Stack: sp(c) r3 r1 r2 a b c d e

Rewrite Code f: c := $r3 a := $r1 b := $r2 d := 0 e := a L:d := d + b e := e - 1 if e > 0 L else E E:r1 := d r3 := c return r3 br2 r3 r1 r2 a b c d e

Rewrite Code f: $res := $r3 Mem[fp+i] := $res $r1 := $r1 $r2 := $r2 $r3 := 0 $r1 := $r1 L:$r3 := $r3 + $r2 $r1 := $r1 - 1 if $r1 > 0 L else E E:$r1 := $r3 $res := Mem[fp+i] $r3 := $res return r3 br2 r3 r1 r2 a b c d e

Alternatively: f: c := $r3 Mem[fp+i] := c a := $r1 b := $r2 d := 0 e := a L:d := d + b e := e - 1 if e > 0 L else E E:r1 := d f := Mem[fp+i] r3 := f return r3 br2 r3 r1 r2 a b c d e

Get Rid of Stupid Moves: f: $res := $r3 Mem[fp+i] := $res $r3 := 0 L:$r3 := $r3 + $r2 $r1 := $r1 - 1 if $r1 > 0 L else E E:$r1 := $r3 $res := Mem[fp+i] $r3 := $res return