Register allocation. TDT4205 Lecture 31

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

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

register allocation saves energy register allocation reduces memory accesses.

Global Register Allocation

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

Register Allocation. Lecture 16

Register Allocation 1

CS 406/534 Compiler Construction Putting It All Together

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

Register Allocation. Lecture 38

Lecture 25: Register Allocation

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

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

Topic 12: Register Allocation

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

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

Lecture 21 CIS 341: COMPILERS

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

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

Register Allocation & Liveness Analysis

Register allocation. Overview

Compiling Techniques

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

CHAPTER 3. Register allocation

CHAPTER 3. Register allocation

Register Allocation, i. Overview & spilling

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

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

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Compiler Design. Register Allocation. Hwansoo Han

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

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

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

CSE 401/M501 Compilers

tokens parser 1. postfix notation, 2. graphical representation (such as syntax tree or dag), 3. three address code

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

The C2 Register Allocator. Niclas Adlertz

CSE P 501 Exam Sample Solution 12/1/11

Register Allocation via Hierarchical Graph Coloring

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

CSE P 501 Exam 12/1/11

Project Compiler. CS031 TA Help Session November 28, 2011

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

CSC D70: Compiler Optimization Register Allocation

Compilation Lecture 11a. Register Allocation Noam Rinetzky. Text book: Modern compiler implementation in C Andrew A.

Compiler Architecture

Front End. Hwansoo Han

Lecture Notes on Register Allocation

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky

April 15, 2015 More Register Allocation 1. Problem Register values may change across procedure calls The allocator must be sensitive to this

Review. Pat Morin COMP 3002

Compilers and Code Optimization EDOARDO FUSELLA

Agenda. CSE P 501 Compilers. Big Picture. Compiler Organization. Intermediate Representations. IR for Code Generation. CSE P 501 Au05 N-1

Instruction Selection: Preliminaries. Comp 412

Fixed-Point Math and Other Optimizations

CS 31: Intro to Systems Operating Systems Overview. Kevin Webb Swarthmore College November 8, 2018

Lecture Compiler Backend

Chapter 9. Register Allocation

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Register Allocation. Introduction Local Register Allocators

CSE 401 Final Exam. December 16, 2010

EECS 583 Class 15 Register Allocation

Calvin Lin The University of Texas at Austin

Introduction to Compiler

Register Allocation. Stanford University CS243 Winter 2006 Wei Li 1

Compiler Design Spring 2018

CS415 Compilers. Intermediate Represeation & Code Generation

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

CSE 431S Final Review. Washington University Spring 2013

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

Control Flow Analysis

Reversing. Time to get with the program

Low-level optimization

Lecture Overview Register Allocation

CSE 373: Data Structures and Algorithms. Memory and Locality. Autumn Shrirang (Shri) Mare

Lesson 9 Transcript: Backup and Recovery

Global Register Allocation via Graph Coloring

The Art and Science of Memory Allocation

CS5363 Final Review. cs5363 1

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Dynamic Programming I Date: 10/6/16

Code generation for modern processors

Code generation for modern processors

Multicycle conclusion

Code Generation. CS 540 George Mason University

Heap Arrays and Linked Lists. Steven R. Bagley

COMP3221: Microprocessors and. and Embedded Systems. Instruction Set Architecture (ISA) What makes an ISA? #1: Memory Models. What makes an ISA?

CS 4120 Lecture 31 Interprocedural analysis, fixed-point algorithms 9 November 2011 Lecturer: Andrew Myers

CST-402(T): Language Processors

CS519: Computer Networks. Lecture 1 (part 2): Jan 28, 2004 Intro to Computer Networking

Data Structure Layout. In HERA/Assembly

Intermediate Code Generation

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

Assembly Language: Function Calls

Abstract Interpretation Continued

Semantic Analysis. Lecture 9. February 7, 2018

LECTURE 3. Compiler Phases

The Structure of a Syntax-Directed Compiler

Functions in MIPS. Functions in MIPS 1

Register Allocation, iii. Bringing in functions & using spilling & coalescing

Transcription:

1 Register allocation TDT4205 Lecture 31

2 Variables vs. registers TAC has any number of variables Assembly code has to deal with memory and registers Compiler back end must decide how to juggle the contents of the memory and registers to fit every variable as necessary

3 Straightforward solution We know how to do this, just Put everything in the activation record For each instruction, shuttle variables into registers Combine registers Put variables back into activation record That s fine and dandy, but it creates Redundant copy instructions Constant memory traffic

4 Register allocation Goal: keep variables in registers as long as possible In the best of cases, a variable can be in a register througout its lifetime If it can t, it ll need a place in the activation record

5 What can go in registers? That depends on the number of registers It also depends on how variables are being used (You can t make a pointer to a register) Main idea: Two variables can t share the same register if they are live simultaneously

6 The basic approach Do live variable analysis Go through the sets of live variables When two variables appear in the same set, they interfere Conversely, when two variables don t interfere, their live ranges are disjoint Pairs like that can share the same register, because they won t need it at the same time

7 Interference graphs An interference graph is a graph where every variable is a node edges connect interfering variables as in this one: b = a + 2 {a} {a, b} c = b * b b = c + 1 return b*a {c,a} {b,a} {} b a c

8 Graph coloring If every register has a color, a register assignment of the interference graph is a mapping where no two neighbors have the same color b a c RAX RBX

9 Um colors? Graph coloring is one of the classic problems of computer science If we have k registers, the question of whether each variable can have one is the same as whether the interference graph is k-colorable K-colorability is an NP complete problem, finding an optimal solution takes exponential time (as far as we know today) We can still approximate it with an imperfect heuristic

10 Practical k-coloring Simplify the graph Find a node with at most k-1 edges Remove it from the graph, put it on a stack Repeat until simplified graph is trivially k-colorable (or, until there are no nodes left, if you prefer) Reintroduce the nodes Add nodes back (in reverse order of the simplification) Color them with colors that they don t interfere with Hope that total number of colors is k or smaller

11 Sometimes it works Is this graph 3-colorable? a b a d c d b c d a b c d a c b (simplify) d a c b d a b c a d b c a d b c (reconstruct)

12 Sometimes it doesn t If the graph can t be colored, it ll find a form where every node has k or more neighbors (otherwise, there d be a color to spare for them)? 3 neighbors spent all 3 colors K or more neighbors doesn t imply uncolorability

13 Spilling When all nodes have k or more neighbors, pick one and mark it for spilling (a place in the activation record) Remove from graph, push on stack Aim for little-used nodes

14 Access to spilled variables Some additional instructions will be needed to move spilled variables back and forth to the activation record Simple: keep a few extra registers for shuttling data in the load-modify-store way Better: rewrite low-ir code with new temporary, redo liveness and register allocation

15 Precolored nodes Some variables need designated registers (e.g. return value goes in RAX ) Treat their temporaries as special, and set their colors in the interference graph Simplification: Never remove pre-colored nodes (They don t need to be reintroduced to get a color anyway) Coloring: Use the pre-colored nodes as starting point when reintroducing the rest

16 Big picture of code generation Start from low-level IR Build DAG of the computation Global variables = static addresses Arguments taken from frame pointer Assume all locals and temporaries in (infinite number of) registers Tile the DAG, obtaining abstract assembly Allocate registers Liveness analysis of abstract assembly Assign registers and generate assembly

17 The whole process Characters Tokens Statements High-level IR Low-level IR Instructions Assembly (binary) optimize optimize (scanner) (parser) (trees, CFGs) (TAC or similar) (instr. selection, register alloc.)

18 The final sessions At the end, we ll revisit all those topics, and speedreview key elements of the techniques we ve covered