CMPUT 680 - Compiler Design and Optimization1 Borrowed from J. N. Amaral, slightly modified LIVE-IN: k j.

Slides:



Advertisements
Similar presentations
Ohm’s Law Objective: TSW understand the concepts of Voltage, Current, and Resistance by developing and applying Ohm’s Law.
Advertisements

CSC 4181 Compiler Construction Code Generation & Optimization.
CSE 5317/4305 L9: Instruction Selection1 Instruction Selection Leonidas Fegaras.
Chapter 14 Testing Tactics
Washington WASHINGTON UNIVERSITY IN ST LOUIS Concurrency: Deadlock Detection Fred Kuhns Department.
Load Balancing Parallel Applications on Heterogeneous Platforms.
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
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic E: Software Pipelining José Nelson Amaral
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic P: Reference Analysis José Nelson Amaral
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic F: IA-64 Hardware Support for Software Pipelining José Nelson Amaral
Topic G: Static Single-Assignment Form José Nelson Amaral
CMPUT Compiler Design and Optimization1 CMPUT680 - Fall 2003 Topic J: Wavefront Scheduling José Nelson Amaral
CMPUT Compiler Design and Optimization1 CMPUT680 - Fall 2003 Topic B: Open Research Compiler José Nelson Amaral
SE-292 High Performance Computing
F28DM Database Management Systems Query Optimisation
Machine & Assembly Language. Machine Language  Computer languages cannot be read directly by the computer – they are not in binary.  All commands need.
CPSC 330 Fall 1999 HW #1 Assigned September 1, 1999 Due September 8, 1999 Submit in class Use a word processor (although you may hand-draw answers to Problems.
CMPUT Compiler Design and Optimization
MIPS Assembly Tutorial
Target code Generation Made by – Siddharth Rakesh 11CS30036 Date – 12/11/2013.
Instruction-Level Parallelism
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)
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral
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.
1 CS 201 Compiler Construction Machine Code Generation.
COMPILERS Register Allocation hussein suleman uct csc3005h 2006.
Graph-Coloring Register Allocation CS153: Compilers Greg Morrisett.
Control-Flow Graphs & Dataflow Analysis 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:
Topic 2b Basic Back-End Optimization
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
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)
CMPUT Compiler Design and Optimization1 CMPUT680 - Fall 2003 Topic 7: Register Allocation and Instruction Scheduling José Nelson Amaral
Linear Scan Register Allocation POLETTO ET AL. PRESENTED BY MUHAMMAD HUZAIFA (MOST) SLIDES BORROWED FROM CHRISTOPHER TUTTLE 1.
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.
Graph Theory in Computer Science Greg Stoll November 22, 2008.
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.
6.1.1 – Properties of Exponents. We worked with combining/multiply like terms, mostly in single terms Example. Simplify the following expressions. 1)
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.
Compiler Principles Fall Compiler Principles Lecture 11: Register Allocation Roman Manevich Ben-Gurion University of the Negev.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Global Register Allocation Based on
Mooly Sagiv html://
The compilation process
Register Allocation Noam Rinetzky Text book:
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Register Allocation via Coloring of Chordal Graphs
Fall Compiler Principles Lecture 13: Summary
CS 201 Compiler Construction
Presentation transcript:

CMPUT Compiler Design and Optimization1 Borrowed from J. N. Amaral, slightly modified LIVE-IN: k j 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 j := b k := m + 4 LIVE-OUT: d k j m e f h g kj b c d k j

CMPUT Compiler Design and Optimization2 Example: Simplify (K=4) bmkj gh d c e f (Appel, pp. 237) (h,no-spill) stack

CMPUT Compiler Design and Optimization3 Example: Simplify (K=4) bmkj g d c e f (Appel, pp. 237) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization4 Example: Simplify (K=4) bmkj d c e f (Appel, pp. 237) (k, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization5 Example: Simplify (K=4) bmj d c e f (Appel, pp. 237) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization6 Example: Simplify (K=4) bmj d c e (Appel, pp. 237) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization7 Example: Simplify (K=4) bmj d c (Appel, pp. 237) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization8 Example: Coalesce (K=4) bj d c (Appel, pp. 237) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack Why can’t we simplify? Cannot simplify move-related nodes.

CMPUT Compiler Design and Optimization9 Example: Coalesce (K=4) bj d c (Appel, pp. 237) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization10 Example: Simplify (K=4) bj c-d (Appel, pp. 237) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization11 Example: Coalesce (K=4) bj (Appel, pp. 237) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization12 Example: Simplify (K=4) greedy-4-colorable b-j (Appel, pp. 237) (b-j, no-spill) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization13 Example: Select (K=4) bmkj gh d c e f (Appel, pp. 237) (b-j, no-spill) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack R1 R2 R3 R4

CMPUT Compiler Design and Optimization14 Example: Select (K=4) bmkj gh d c e f (Appel, pp. 237) (b-j, no-spill) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack R1 R2 R3 R4

CMPUT Compiler Design and Optimization15 Example: Select (K=4) bmkj gh d c e f (Appel, pp. 237) (b-j, no-spill) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack R1 R2 R3 R4

CMPUT Compiler Design and Optimization16 Example: Select (K=4) bmkj gh d c e f (Appel, pp. 237) (b-j, no-spill) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack R1 R2 R3 R4

CMPUT Compiler Design and Optimization17 Example: Select (K=4) bmkj gh d c e f (Appel, pp. 237) (b-j, no-spill) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack R1 R2 R3 R4

CMPUT Compiler Design and Optimization18 Example: Select (K=4) bmkj gh d c e f (Appel, pp. 237) (b-j, no-spill) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack R1 R2 R3 R4

CMPUT Compiler Design and Optimization19 Example: Select (K=4) bmkj gh d c e f (Appel, pp. 237) (b-j, no-spill) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack R1 R2 R3 R4

CMPUT Compiler Design and Optimization20 Example: Select (K=4) bmkj gh d c e f (Appel, pp. 237) (b-j, no-spill) (c-d, no-spill) (m, no-spill) (e, no-spill) (f, no-spill) (k, no-spill) (g, no-spill) (h, no-spill) stack R1 R2 R3 R4

CMPUT Compiler Design and Optimization21 Example: Allocation with 4 registers LIVE-IN: k j 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 j := b k := m + 4 LIVE-OUT: d k j m e f h g kj b c d j k

CMPUT Compiler Design and Optimization22 Example: Allocation with 4 registers LIVE-IN: k j g := mem[j+12] h := k -1 f := g + h e := mem[j+8] m := mem[j+16] j := mem[f] d := e + 8 k := m + 4 LIVE-OUT: d k j m e f h g kj d j k

CMPUT Compiler Design and Optimization23 Could we do the allocation in the previous example with 3 registers?

CMPUT Compiler Design and Optimization24 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237) (h,no-spill) stack

CMPUT Compiler Design and Optimization25 Example: Simplify (K=3) bmkj g d c e f (Appel, pp. 237) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization26 Example: Freeze (K=3) bmkj d c e f (Appel, pp. 237) (g, no-spill) (h, no-spill) stack Coalescing may make things worse (not always). George’s rule would coalesce the move d-c, Briggs’ rule would freeze.

CMPUT Compiler Design and Optimization27 Example: Simplify (K=3) bmkj d c e f (Appel, pp. 237) (c, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization28 Example: Potential Spill (K=3) bmkj d e f (Appel, pp. 237) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack Neither coalescing nor freezing help us. At this point we should use some profitability analysis to choose a node as may-spill.

CMPUT Compiler Design and Optimization29 Example: Simplify (K=3) bmkj d f (Appel, pp. 237) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization30 Example: Simplify (K=3) bmkj d (Appel, pp. 237) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization31 Example: Coalesce (K=3) bkj d (Appel, pp. 237) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization32 Example: Coalesce (K=3) kj-b d (Appel, pp. 237) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization33 Example: Coalesce (K=3) kj-b (Appel, pp. 237) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization34 Example: Coalesce (K=3) j-b (Appel, pp. 237) (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) stack

CMPUT Compiler Design and Optimization35 Example: Select (K=3) bmkj gh d c e f (Appel, pp. 237) stack R1 R2 R3 (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill)

CMPUT Compiler Design and Optimization36 Example: Select (K=3) bmkj gh d c e f (Appel, pp. 237) stack R1 R2 R3 (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill)

CMPUT Compiler Design and Optimization37 Example: Select (K=3) bmkj gh d c e f (Appel, pp. 237) stack R1 R2 R3 (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill)

CMPUT Compiler Design and Optimization38 Example: Select (K=3) bmkj gh d c e f (Appel, pp. 237) stack R1 R2 R3 (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill)

CMPUT Compiler Design and Optimization39 Example: Select (K=3) bmkj gh d c e f (Appel, pp. 237) stack R1 R2 R3 (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill)

CMPUT Compiler Design and Optimization40 Example: Select (K=3) bmkj gh d c e f (Appel, pp. 237) stack R1 R2 R3 (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill) This is when our optimism could have paid off.

CMPUT Compiler Design and Optimization41 Example: Select (K=3) bmkj gh d c e f (Appel, pp. 237) stack R1 R2 R3 (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill)

CMPUT Compiler Design and Optimization42 Example: Select (K=3) bmkj gh d c e f (Appel, pp. 237) stack R1 R2 R3 (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill)

CMPUT Compiler Design and Optimization43 Example: Select (K=3) bmkj gh d c e f (Appel, pp. 237) stack R1 R2 R3 (j-b, no-spill) (k, no-spill) (d, no-spill) (m, no-spill) (f, no-spill) (e, may-spill) (c, no-spill) (g, no-spill) (h, no-spill)

CMPUT Compiler Design and Optimization44 So, is it possible for K=3? bmkj gh d c e f (Appel, pp. 237)

CMPUT Compiler Design and Optimization45 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237)

CMPUT Compiler Design and Optimization46 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237)

CMPUT Compiler Design and Optimization47 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237)

CMPUT Compiler Design and Optimization48 Example: Simplify (K=3) bmkj gh d c e f (Appel, pp. 237) Impossible! But only 3 variables are live at any time…there may be a way?

CMPUT Compiler Design and Optimization49 Example as basic block: 3 Registers by renaming k & j LIVE-IN: k j 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 j’ := b k’ := m + 4 LIVE-OUT: d k’ j’ m e f h g kj b c d k’ j’

CMPUT Compiler Design and Optimization50 Example as basic block: 3 Registers by renaming k & j LIVE-IN: k j g := mem[j+12] h := k -1 f := g + h e := mem[j+8] m := mem[j+16] j’ := mem[f] d := e + 8 k’ := m + 4 LIVE-OUT: d k’ j’ m e f h g kj d k’ j’

CMPUT Compiler Design and Optimization51 Example as basic block: A 3-coloring of the graph bmkj gh d c e f (Appel, pp. 237) The two assignments of k (resp. j) can be placed in two different registers. k’ j’

CMPUT Compiler Design and Optimization52 Example as a loop: 3 Registers are enough! LIVE-IN: k j 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 j := b k := m + 4 LIVE-OUT: d k j m f h g kj b c d e k j

CMPUT Compiler Design and Optimization53 Example as a loop: 3 Registers are enough! LIVE-IN: k j g := mem[j+12] h := k -1 f := g + h e := mem[j+8] m := mem[j+16] b := mem[f] d := e + 8 k := m + 4 LIVE-OUT: d k j m f h g kj j d e k

CMPUT Compiler Design and Optimization54 Example as a loop: 3 Registers are enough! LIVE-IN: k j g := mem[j+12] h := k -1 f := g + h e := mem[j+8] m := mem[j+16] j := mem[f] k := m + 4 d := e + 8 LIVE-OUT: d k j m f h g kj j e k d

CMPUT Compiler Design and Optimization55 Example as a loop: 3 Registers are enough! LIVE-IN: k j g := mem[j+12] h := k -1 f := g + h j’ := j e := mem[j’+8] m := mem[j’+16] j := mem[f’] k := m + 4 d := e + 8 LIVE-OUT: d k j m f h g kj j d e f’ := f j’ f’ k