Register Allocation Harry Xu CS 142 (b) 05/08/2018.

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 CS 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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}
Graph-Coloring Register Allocation 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.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
Register Allocation (Slides from Andrew Myers). Main idea Want to replace temporary variables with some fixed set of registers First: need to know which.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
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)
Assembly Language for Intel-Based Computers Chapter 2: IA-32 Processor Architecture Kip Irvine.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Microprocessor Systems Design I Instructor: Dr. Michael Geiger Spring 2014 Lecture 4: x86 memory.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
Register Allocation and Spilling via Graph Coloring G. J. Chaitin IBM Research, 1982.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 2: IA-32 Processor Architecture (c) Pearson Education, All rights reserved. You.
6.828: PC hardware and x86 Frans Kaashoek
Multitasking Mr. Mahendra B. Salunke Asst. Prof. Dept. of Computer Engg., STES SITS, Narhe, Pune-41 STES Sinhgad Institute of Tech. & Science Dept. of.
Intel Pentium II Processor Brent Perry Pat Reagan Brian Davis Umesh Vemuri.
Fall 2012 Chapter 2: x86 Processor Architecture. Irvine, Kip R. Assembly Language for x86 Processors 6/e, Chapter Overview General Concepts IA-32.
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.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
Static Program Analyses of DSP Software Systems Ramakrishnan Venkitaraman and Gopal Gupta.
Other Processors. Having learnt MIPS, we can learn other major processors. Not going to be able to cover everything; will pick on the interesting aspects.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Register Allocation Harry Xu CS 142 (b) 02/11/2013.
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.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
What is a processor ? A “Processor," is a small chip that resides in computers and other electronic devices. It also called “Brain ” of a computer. It.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Chapter Overview General Concepts IA-32 Processor Architecture
Stack Operations Dr. Hadi AL Saadi.
Global Register Allocation Based on
Data Flow Analysis Suman Jana
Assembly language.
Operating Systems Engineering
x86 Processor Architecture
The compilation process
Other Processors.
Basic Microprocessor Architecture
Register Allocation Noam Rinetzky Text book:
CS 301 Fall 2002 Computer Organization
The Microprocessor & Its Architecture
Tutorial No. 11 Module 10.
Computer Architecture CST 250
Final Code Generation and Code Optimization
Low-Level Thread Dispatching on the x86
COMPILERS Liveness Analysis
Fall Compiler Principles Lecture 13: Summary
Computer Architecture and System Programming Laboratory
CS 201 Compiler Construction
Presentation transcript:

Register Allocation Harry Xu CS 142 (b) 05/08/2018

Register Allocation Assigning a large number of program variables to a small number of CPU registers for improved performance Motivation A developer often allocates arbitrarily many variables The compiler must decide how to allocate these variables to a small, finite set of registers

Basic Idea Variable liveness analysis Two variables that will be used at the same time cannot be allocated to the same register Some variables will be held in RAM and loaded in/out for every read/write, a process called spilling Assigning as many variables to registers as possible Memory accesses are slower

Reaching Definition Analysis A classic dataflow analysis to understand what variables are live simultaneously at a given program point A backward analysis that computes dataflow facts from the end of the program Given an (SSA) statement s, there are four important sets the analysis needs to maintain Gen[s]: the set of variables used in s Kill[s]: the set of variables that are assigned values in s Live-in[s]: the set of variables that are simultaneously live before s is executed Live-out[s]: the set of variables that are simultaneously live after s is executed

Liveness Analysis (Cond) Initial setup Live-out[final] = ∅ Gen[a:= b + c] = {b, c} Kill[a:= b + c] = {a} Dataflow equations Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p]

An Example Gen(L1) = {}, Kill(L1) = {a} L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Gen(L1) = {}, Kill(L1) = {a} Gen(L2) = {}, Kill(L2) = {b} Gen(L3) = {}, Kill(L3) = {d} Gen(L4) = {}, Kill(L4) = {x} Gen(L5) = {a, b}, Kill(L5) = {} Gen(L6) = {a, b}, Kill(L6) = {c} Gen(L7) = {}, Kill(L7) = {d} Gen(L8) = {}, Kill(L8) = {} Gen(L9) = {}, Kill(L9) = {c} Gen(L10) = {b, d, c}, Kill(L5) = {} Worklist = { L10 }

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {L9}

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {b, d, c} Live-in = {b, d} Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {L8}

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {b,d} Live-in = {b, d} Live-out = {b,d,c} Live-in = {b, d} Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {L7, L5}

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {b, d} Live-in = {b} Live-out = {b,d} Live-in = {b, d} Live-out = {b, d, c} Live-in = {b, d} Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {L6, L5}

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {b} Live-in = {b, a} Live-out = {b, d} Live-in = {b} Live-out = {b, d} Live-in = {b, d} Live-out = {b, d, c} Live-in = {b, d} Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {L5}

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {b, a, d} Live-in = {b, a, d} Live-out = {b} Live-in = {b, a} Live-out = {b, d} Live-in = {b} Live-out = {b, d} Live-in = {b, d} Live-out = {b, d, c} Live-in = {b, d} Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {L4}

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {b, a, d} Live-in = {b, a, d} Live-out = {b, a, d} Live-in = {b, a, d} Live-out = {b} Live-in = {b, a} Live-out = {b, d} Live-in = {b} Live-out = {b, d} Live-in = {b, d} Live-out = {b, d, c} Live-in = {b, d} Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {L3}

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {b, a, d} Live-in = {b, a} Live-out = {b, a, d} Live-in = {b, a, d} Live-out = {b, a, d} Live-in = {b, a, d} Live-out = {b} Live-in = {b, a} Live-out = {b, d} Live-in = {b} Live-out = {b, d} Live-in = {b, d} Live-out = {b, d, c} Live-in = {b, d} Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {L2}

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {b, a} Live-in = {a} Live-out = {b, a, d} Live-in = {b, a} Live-out = {b, a, d} Live-in = {b, a, d} Live-out = {b, a, d} Live-in = {b, a, d} Live-out = {b} Live-in = {b, a} Live-out = {b, d} Live-in = {b} Live-out = {b, d} Live-in = {b, d} Live-out = {b, d, c} Live-in = {b, d} Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {L1}

Compute Liveness L1: a = 3; L2: b = 5; L3: d = 4; L4: x = 100; L5: if a > b then L6: c = a + b; L7: d = 2; L8: endif L9: c = 4; L10:return b * d + c; Live-out = {a} Live-in = { } Live-out = {b, a} Live-in = {a} Live-out = {b, a, d} Live-in = {b, a} Live-out = {b, a, d} Live-in = {b, a, d} Live-out = {b, a, d} Live-in = {b, a, d} Live-out = {b} Live-in = {b, a} Live-out = {b, d} Live-in = {b} Live-out = {b, d} Live-in = {b, d} Live-out = {b, d, c} Live-in = {b, d} Live-out = {} Live-in = {b, d, c} Lives-in[s] = Gen[s] ∪(Lives-out[s] – Kill[s]) Lives-out[s] = ∪p∊succ(s) Live-in[p] Worklist = {}

Handling of Loops A fixed-point computation needs to be performed The next statement is not added into the worklist if Live-out and Live-in of the current statement no longer change

Interference Graph Each node represents a variable Each edge connects two variables which can be live at the same time Register allocation is reduced to the problem of k-coloring the interference graph a b x c d

K-Coloring An NP-complete problem For this graph, three colors are sufficient a b x c d

Algorithms Brute-force search Other algorithms Try every of the kn assignments of k colors to n vertices and checks if each assignment is legal The process repeats for k = 1, 2, …, n – 1 Impractical for all but small input graphs Other algorithms Dynamic programming Parallel algorithms

X86 Registers General registers Segment registers Index and pointers EAX EBX ECX EDX Segment registers CS DS ES FS GS SS Index and pointers ESI EDI EBP EIP ESP Indicator EFLAGS Never use EBX and EBP

Output For each register, the set of variables that can be allocated to it The result of this phase will be used by the final code generation phase to guide the actual register allocation