CSc 553. Principles of Compilation. 23 : Register Allocation. Department of Computer Science University of Arizona

Similar documents
Liveness Analysis and Register Allocation. Xiao Jia May 3 rd, 2013

Introduction. Data-flow Analysis by Iteration. CSc 553. Principles of Compilation. 28 : Optimization III

CS 406/534 Compiler Construction Putting It All Together

Lecture 6. Register Allocation. I. Introduction. II. Abstraction and the Problem III. Algorithm

Register allocation. Overview

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

CSC D70: Compiler Optimization Register Allocation

Register allocation. instruction selection. machine code. register allocation. errors

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

Register Allocation. Register Allocation. Local Register Allocation. Live range. Register Allocation for Loops

Architectures. Code Generation Issues III. Code Generation Issues II. Machine Architectures I

Fall Compiler Principles Lecture 12: Register Allocation. Roman Manevich Ben-Gurion University

Low-Level Issues. Register Allocation. Last lecture! Liveness analysis! Register allocation. ! More register allocation. ! Instruction scheduling

Register Allocation & Liveness Analysis

Code generation for modern processors

Code generation for modern processors

Variables vs. Registers/Memory. Simple Approach. Register Allocation. Interference Graph. Register Allocation Algorithm CS412/CS413

Liveness Analysis and Register Allocation

Topic 12: Register Allocation

Outline. Register Allocation. Issues. Storing values between defs and uses. Issues. Issues P3 / 2006

Compiler Design. Register Allocation. Hwansoo Han

COSE312: Compilers. Lecture 20 Data-Flow Analysis (2)

Register Allocation. Stanford University CS243 Winter 2006 Wei Li 1

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

Global Register Allocation - Part 2

Register Allocation 3/16/11. What a Smart Allocator Needs to Do. Global Register Allocation. Global Register Allocation. Outline.

Compiler Architecture

Lecture 2. Introduction to Data Flow Analysis

Lecture 21 CIS 341: COMPILERS

Rematerialization. Graph Coloring Register Allocation. Some expressions are especially simple to recompute: Last Time

Compiler Optimization and Code Generation

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Register Allocation via Hierarchical Graph Coloring

Introduction. CSc 453. Compilers and Systems Software. 19 : Code Generation I. Department of Computer Science University of Arizona.

Lecture 15 Register Allocation & Spilling

register allocation saves energy register allocation reduces memory accesses.

Lecture 4 Introduction to Data Flow Analysis

Register Allocation. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Introduction. Inline Expansion. CSc 553. Principles of Compilation. 29 : Optimization IV. Department of Computer Science University of Arizona

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

CSc 453. Code Generation I Christian Collberg. Compiler Phases. Code Generation Issues. Slide Compilers and Systems Software.

The C2 Register Allocator. Niclas Adlertz

EECS 583 Class 15 Register Allocation

Register Allocation (wrapup) & Code Scheduling. Constructing and Representing the Interference Graph. Adjacency List CS2210

Register allocation. TDT4205 Lecture 31

2068 (I) Attempt all questions.

Compiler Code Generation COMP360

Global Register Allocation via Graph Coloring

Register Allocation. CS 502 Lecture 14 11/25/08

Global Register Allocation - 2

Global Register Allocation

Code Generation. CS 540 George Mason University

Global Register Allocation - Part 3

Compiler Optimization and Code Generation

Register Allocation. Preston Briggs Reservoir Labs

Data Flow Analysis. Program Analysis

CSc 453. Compilers and Systems Software. 21 : Code Generation II. Department of Computer Science University of Arizona

Lecture Notes on Register Allocation

ELEC 876: Software Reengineering

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

CSE P 501 Compilers. Register Allocation Hal Perkins Autumn /22/ Hal Perkins & UW CSE P-1

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Data-flow Analysis - Part 2

Today More register allocation Clarifications from last time Finish improvements on basic graph coloring concept Procedure calls Interprocedural

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

CSc 520 Principles of Programming Languages

Finite Math - J-term Homework. Section Inverse of a Square Matrix

CHAPTER 3. Register allocation

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

CHAPTER 3. Register allocation

Compiling Techniques

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

Why Global Dataflow Analysis?

Register Allocation. Lecture 38

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

CSE P 501 Exam 8/5/04

Review. Pat Morin COMP 3002

SSA-Form Register Allocation

Evaluation Scheme L T P Total Credit Theory Mid Sem Exam

Chapter 9. Register Allocation

CSc 453 Compilers and Systems Software

CS2210: Compiler Construction. Compiler Optimization

Lecture 25: Register Allocation

MODEL ANSWERS COMP36512, May 2016

Dataflow analysis (ctd.)

Global Register Allocation

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Register Allocation (via graph coloring) Lecture 25. CS 536 Spring

Lecture Compiler Middle-End

Abstract Interpretation Continued

Lexical Scanning COMP360

COP5621 Exam 4 - Spring 2005

Compilers and Code Optimization EDOARDO FUSELLA

Lecture 6 Foundations of Data Flow Analysis

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS)

Transcription:

CSc 553 Principles of Compilation 3 : egister Allocation Department of Computer Science University of Arizona collberg@gmail.com Copyright c 0 Christian Collberg

Introduction

Lexing, Parsing Semantic Analysis, Intermediate Code eneration Intermediate Code X is defined here: X is used here: Separation into Basic Blocks; Flow analysis; Next use information computation; raph Coloring Peephole Optimization Instruction Selection Instruction Scheduling Tree matching/ Dynamic Programming egister Allocation egister Assignment egister Spilling Machine Code

egister Allocation by raph Coloring

egister Allocation egister allocation is difficult: Machines have weird instruction sets, register pairs (two consecutive registers that are the source or destination in an instruction), register classes (address, integer, index, floating),... Optimal solutions to the register allocation problem is NP-complete. Most compilers use complicated ad hoc heuristic register allocation algorithms. It would be helpful if we had a good model for register allocation the way we have finite automata for lexical analysis, attribute grammars for semantic analysis, etc. We can model register allocation using undirected graphs.

raph Coloring Model register allocation as a graph coloring problem. Each color represents an available register. Create a graph node for each variable. If variables a and b are active (live) at the same point, they cannot be assigned to the same register. Add an edge (a,b) to the graph. Look for a k-coloring (k =# registers) of the graph. Assign colors so that neighboring nodes have different colors. If we cannot k-color our graph, we: Select a node (variable) n whose value we re willing to spill, Insert spill code, 3 Delete node n and its edges, 4 Look for a k-coloring.

The Interference raph I The interference graph (an undirected graph where the nodes are the variables of the program) models which variables cannot be allocated to the same register. Connect a and b if a is live at a point where b is defined. () a := 5 () d := 9 + a (3) e := a + d (4) b := d + a (5) f := e + 6 (6) c := b + f B Z A V B X B

The Interference raph II egister allocation is a bit like room scheduling. oom scheduling: We have a set of rooms (registers). We have a set of classes (variables) to fit into the rooms. 3 Two classes that meet at the same time cannot be allocated to the same room. The difference is that in room scheduling there can be no spilling; no-one gets to have their lecture in the park! A variable s live range starts at the point in the code where the variable receives a value, and ends where that value is used for the last time.

The Interference raph III e g i s t e r A l l o c a t i o n x y w z [] z := [] x := * z [3] y := 3 * z [4] w := x + y [5] PINT y + z [6] x := y * w First attempt: 3? Second attempt: 3 x s and w s live ranges don t overlap! They can therefore be assigned to the same register ()! o o m A l l o c a t i o n x y w z [09am] Class z starts [0am] Class x starts [am] Class y starts [noon] Class x ends, w starts [0pm] Class z ends [0pm] Classes y & w end First attempt: 3? Second attempt: 3 Class x and w can be assigned to the same room ()!

Chaitin s Coloring Algorithm Calculate Spill Costs Chose node to spill, mark, and remove from graph All nodes have degree >= k Build raph FO EACH node n with degree < k DO emove n from graph; emove n s edges from graph; push(n) END; is empty generate spill code for all nodes marked for spilling yes nodes have been spilt? no Pop each node n from stack: reinsert n into graph; give n different color than its neighbors

Coloring Example I (a) k = 3 B 3 4 5 3 4 5 3 4 5 4 5 3 3 5 3 4 5 3 3 4 3 4 5 3 4 5 4 5 5 4 B B

Coloring Example I (b) k = 3 4 5 3 4 5 5 5 4 4 5 4 5 4 5 4 4 5 4 5 4 5 spill

Coloring Example II k = 3 4 3 4 No node has degree <. 3 3 4 4 3 4 3 4 4 3 But the graph is colorable! 3 4 Spill 4 3

Precoloring

Precolored Nodes I Sometimes we will want to express that a particular variable must reside in a particular register. For example, if variable a is being passed as argument to procedure P on the SPAC, we d want to express that a must reside in register %o0, and nowhere else. Similarly, sometimes we want to express that a particular variable must not reside in a particular register. For example, a floating point variable should not be in an integer register. Such variables are precolored. We augment the interference graph with nodes for each available register, and an edge between variable a and register r if a cannot be allocated to r.

Precolored Nodes II VA x, y : INTEE; VA a, b : EAL; x := 00; a :=.0; b := a + 5.; y := x + 50; P(y,a); We have two integer registers r and r, and two FP registers f and f. Procedure actuals are passed in registers: y in r and a in f. a r f x b r f y

Precolored Nodes III VA x, y : INTEE; VA a, b : EAL; x := 00; a :=.0; b := a + 5.; y := x + 50; P(y,a); We color y and r red (), x and r green (). We color a and f blue (B), b and f yellow (Y). a B r B f x b Y Y r f y

egister Coalescing

egister Coalescing egister coalescing is a kind of copy propagation that removes register copies. Search the intermediate code for copies S j S i such that S j and S i don t interfere with each other. Modify any instruction S i to S j and merge the interference graph nodes for S j and S i. x := 00; a := x * ; y := x; z := y + a; PINT y,z; x := 00; a := x * ; z := x + a; PINT x,z; x a a x y y z z

Splitting Live anges

Splitting Live anges If we use the same variable for several unique tasks (e.g. i for all for-loops) the interference graph is overly constrained. Instead we let each graph node represent a unique use of a variable. x := 00; a := x * ; PINT a; x := 00; b := x + 5; PINT b; x := 00; a := x * ; PINT a; x := 00; b := x + 5; PINT b; a x a x b x b

Building the Interference raph

Building the Interference raph I We start by performing a liveness analysis. in[b] Variables live on entrance to B. out[b] Variables live on exit from B. def[b] Variables assigned values in B before the variable is used. use[b] Variables whose values are used before being assigned to. Data-flow Equations: in[b] = use[b] (out[b] def[b]) out[b] = in[s] succs S of B

Building the Interference raph II Then we build the graph. For efficiency, we store it both as an adjacency matrix, and as adjacency lists. FO all basic blocks b in the program DO live := out[b]; FO all instructions I b, in reverse order DO FO all d def(i) DO FO all l live def(i) DO add the interference graph edge l,d ; live := use(i) (live def(i));

Building the Interference raph III out=in[b] in[b3] ={a,c} B a := 5 c := 9 def={ a,c } use={ } in={ } def={ b } use= a out=in[b4]={b,c} in={a } ({b,c}-{b}) ={a,c} out={a,b} B b:=a + B4 a := b + c c := 5 + b B3 c := 0 b := 7 def={ c, b } use={ } out=in[b4]={b,c} in={} ({b,c}-{b,c}) ={ } in={b,c} ({a,b}-{a }) def={ a } use={ b,c } out={ } in={a,b} PINT a PINT b use={a,b} def={ }

Building the Interference raph IV b,c live={a } def={b} live={c,b} live={c,a } B b:=a + a := 5 c := 9 B c := 0 b := 7 def={a } def={c} B3 def={c} def={b} c,a live={c} live={b,c} b,c live={c,b,a } live={b,a } B4 a := b + c c := 5 + b def={a } def={c} a,b a,c c,b c,a live={b,a } live={b} PINT a PINT b

Building the Interference raph V Here s the finished interference graph: a a b c a a b c

Summary

eadings and eferences ead the Tiger Book, Chapter, egister Allocation. The Dragon book: 53 5, 58 546, 554 559. Preston Briggs thesis: egister Allocation via Coloring, http://cs-tr.cs.rice.edu:80 /Dienst/epository/.0/Body/ ncstrl.rice cs/t9-83/postscript. Steven Muchnick, Advanced Compiler Design and Implementation, Chapter 6, pp. 48 55.

Summary raph coloring can be used to model register allocation. Each variable becomes a node in the graph. If two variables can t reside in the same register, we add en edge between them. The coloring algorithm assigns colors so that no neighboring nodes receive the same color. Optimal coloring is NP-complete (at least for global register allocation), so we need a heuristic algorithm that produces a good approximation.

Homework

egister Allocation by raph Coloring

Homework III raph Coloring Construct the interference graph for the basic block below, and show the coloring produced by Chaitin s algorithm when two and three registers are available. Spill costs are X=3,Y=,Z=,V=. X := 5; Y := X + 3; Z := X + 5; V := Y + 6; X := X + Y; X := V + Z;

Homework IV raph Coloring Construct the flow-graph and the interference graph for the procedure body below, and show the global coloring produced by Chaitin s algorithm when two and three registers are available. Spill costs are X=,Y=,Z=3,W=,V=. BEIN X := ; Z := ; IF e THEN Z := ; ELSE Y := ; ENDIF; := X; := Y; W := ; V := ; IF e THEN := W; := Z; ELSE := V; ENDIF; := V + W;

Exam Problem I(a) (45.430 95) Consider the following basic block: X := 5; A := X + 5; B := X + 3; V := A + B; A := X + 5; Z := V + A; PINT Z, V, A; Construct the register interference graph for the block. A B Z V X How many colors are necessary to color the graph optimally without register spills?

Exam Problem I(b) (45.430 95) X := 5; A := X + 5; B := X + 3; V := A + B; A := X + 5; Z := V + A; PINT Z, V, A; 3 Show the graph after it has been colored with Chaitin s algorithm using colors (ed and Blue). The spill-costs are: A=, Z=, B=3, V=, X=4. Z A B V X

Exam Problem II/a (45.730 96) Consider the following basic block: A := 5; F := A + ; E := F + 5; B := F * A; PINT B + E + A; D := E + 5; PINT E; C := D + B; PINT E + C; Construct the register interference graph for the block. B C A D F E

Exam Problem II/b (45.730 96) How many colors are necessary to color the graph optimally without register spills? 3 Show such an optimal coloring! B C A D F E 4 Show the graph after it has been colored with Chaitin s algorithm using colors (ed and Blue). The spill-costs are: C=, D=, E=3, B=A=4, F=5. B C A D F E