Local Register Allocation (critical content for Lab 2) Comp 412

Similar documents
Software Tools for Lab 1

Lab 3, Tutorial 1 Comp 412

Instruction Selection: Preliminaries. Comp 412

CS415 Compilers Register Allocation. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Runtime Support for Algol-Like Languages Comp 412

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

Implementing Control Flow Constructs Comp 412

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

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Intermediate Representations

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

CS415 Compilers. Intermediate Represeation & Code Generation

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

Instruction Scheduling Beyond Basic Blocks Extended Basic Blocks, Superblock Cloning, & Traces, with a quick introduction to Dominators.

Intermediate Representations

Introduction to Optimization Local Value Numbering

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

The ILOC Virtual Machine (Lab 1 Background Material) Comp 412

COMP 412, Fall 2018 Lab 1: A Front End for ILOC

The Software Stack: From Assembly Language to Machine Code

Syntax Analysis, III Comp 412

Instruction Selection, II Tree-pattern matching

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Parsing II Top-down parsing. Comp 412

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

Instruction Selection: Peephole Matching. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Handling Assignment Comp 412

Instruction Selection and Scheduling

Optimizer. Defining and preserving the meaning of the program

Syntax Analysis, III Comp 412

Lecture 12: Compiler Backend

Combining Optimizations: Sparse Conditional Constant Propagation

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

CS 406/534 Compiler Construction Instruction Selection and Global Register Allocation

Code generation for modern processors

Code generation for modern processors

Naming in OOLs and Storage Layout Comp 412

CS 406/534 Compiler Construction Putting It All Together

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

CSE 504: Compiler Design. Instruction Selection

Arrays and Functions

Code Shape II Expressions & Assignment

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

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

Computing Inside The Parser Syntax-Directed Translation. Comp 412

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

CS 432 Fall Mike Lam, Professor. Compilers. Advanced Systems Elective

CS415 Compilers. Instruction Scheduling and Lexical Analysis

CS 406/534 Compiler Construction Instruction Scheduling

The ILOC Simulator User Documentation

The Processor Memory Hierarchy

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

Building a Parser Part III

CS415 Compilers. Lexical Analysis

MITOCW watch?v=kz7jjltq9r4

(Refer Slide Time: 01:25)

6.001 Notes: Section 15.1

The ILOC Simulator User Documentation

The ILOC Simulator User Documentation

(Refer Slide Time: 1:40)

Global Register Allocation via Graph Coloring

Computer Organization & Assembly Language Programming

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/18/14

Register Allocation. 2. register contents, namely which variables, constants, or intermediate results are currently in a register, and

Register Allocation. Introduction Local Register Allocators

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Computer Architecture Prof. Mainak Chaudhuri Department of Computer Science & Engineering Indian Institute of Technology, Kanpur

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CS /534 Compiler Construction University of Massachusetts Lowell

COMP 3500 Introduction to Operating Systems Project 5 Virtual Memory Manager

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

Introduction to Parsing. Comp 412

The View from 35,000 Feet

EECE.2160: ECE Application Programming

CSC D70: Compiler Optimization Register Allocation

UG3 Compiling Techniques Overview of the Course

Alternatives for semantic processing

Intermediate Representations Part II

Parsing II Top-down parsing. Comp 412

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

Just-In-Time Compilers & Runtime Optimizers

Lecture Compiler Middle-End

MODEL ANSWERS COMP36512, May 2016

Project 1 Balanced binary

DEMO A Language for Practice Implementation Comp 506, Spring 2018

Lecture 7: Efficient Collections via Hashing

Principles of Compiler Design

Lecture Notes on Liveness Analysis

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Loop Invariant Code Mo0on

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Overview of the Class

Transcription:

Updated After Tutorial COMP 412 FALL 2018 Local Register Allocation (critical content for Lab 2) Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. 13.1 & 13.2 in EaC2e

Lab 2 Schedule N.B.: Code checks 1 & 2 are milestones, not exhaustive tests. Many students still find bugs after code check 2, to say nothing of tuning for effectiveness & efficiency. Sunday Monday Tuesday Wednesday Thursday Friday Saturday 9 10 11 12 13 14 Lab 2 Specs available 16 17 18 Tutorial 5 PM TBA 23 24 Tutorial 5 PM TBA 30 1 Deadline: Code Check 2 25 Dan Grove Talk (Dart Group) 19 20 21 Deadline: Code Check 1 2 3 Advice: Pay attention to 4 5 6 the timing / scaling tests 15 22 26 27 28 29 Focus on connecting to Lab 1 IR, and Rename Allocate correctly 7 8 Deadline: Lab 2 Code 9 10 12 Deadline: Lab Report 12 13 Improve performance & allocation COMP 412, Fall 2018 1

The Back End source code IR Front End Optimizer Back End IR target code Register Allocator: Decides, at each operation, which values reside in registers (allocation) Decides, for each enregistered value, which register it occupies (assignment) Inserts code to spill unregistered values to memory Tries to minimize the cost of spills Operates efficiently O(n), O(n log 2 n), maybe O(n 2 ) Not O(2 n ) IR annotations Instruction Selector regs Register Allocator k regs Instruction Scheduler Back End k regs asm asm asm Notation: The literature on register allocation consistently uses COMP k as the 412, number Fall 2018 of registers on the target system. 2

Local Register Allocation & Lab 2 In Lab 2, you will build a register allocator for straight-line ILOC code A sequence of branch-free (or straight-line) code is called a block A basic block is a maximal length sequence of straight-line code Algorithms that operate on one basic block are called local techniques Your code will perform both allocation & assignment on a basic block Allocation determines which values reside in registers at each operation Assignment maps each value allocated a register into a specific register In local allocation, we solve these problems together This problem is conceptually simple and combinatorially hard Realistic versions of the problem, including Lab 2, are NP-Complete Your lab must find an approximate solution Your lab should run in linear time (even though the problem is NPC) Welcome to the world of writing compilers! The lab is designed to have a high ratio of thought to programming 8.3 13 You COMP will 412, reuse Fall 2018 your ILOC front end from Lab 1 in both Lab 2 and Lab 3. 3

Terminology: Values, Virtual Registers, & Physical Registers A virtual register (VR) is a register name that the compiler uses in its internal representation of the code Virtual is used as in virtual memory or virtual address, not virtual reality Using virtual registers in the IR, rather than physical registers, lets the compiler defer allocation decisions & simplifies the compiler Correctly separates concerns over optimization and allocation [Backus] A physical register (PR) is a name for an actual target machine register Limited supply of PRs, cannot add more Valid assembly code cannot name more PRs than machine supports Register allocators (& many compilers) use virtual registers to create a name space where each name corresponds to a value or live range The first thing your allocator should do is find live ranges and rename the code so each live range is a unique VR. The allocator then replaces VRs with PRs. Using VRs that correspond to live ranges simplifies the implementation of the allocator and sidesteps some potential problems in the allocator. COMP We use 412, SR Fall 2018 as an abbreviation for source register 5

Local Register Allocation In x y, x is a definition and y is a use. (standard terminology) An Example Block in ILOC loadi 128 => r0 // r0 addr(a) load r0 => r1 // r1 a loadi 132 => r2 // r2 addr(b) load r2 => r3 // r3 b loadi 136 => r4 // r4 addr(c) load r4 => r5 // r5 c mult r3, r5 => r3 // r3 b * c add r1, r3 => r1 // r1 a b * c store r1 => r0 // a a b * c Allocator decides which values are in registers, and chooses a physical register for each value Easy if values registersi, otherwise hard Must discover values or live ranges Assumptions: Computes a a + b * c Uses Lab 2 ILOC subset a is stored at 128, b at 132, and c at 136 // denotes the start of a comment; the scanner can ignore // and any characters to the end of the line Slides will use r for a register in the input, vr for a virtual register and pr for a physical register Code reuses registers COMP If you 412, find Fall ILOC 2018 confusing, see lecture notes on the ILOC virtual machine. 6

Local Register Allocation In x y, x is a definition and y is a use. (standard terminology) A Critical Point loadi 128 => r0 // r0 addr(a) load r0 => r1 // r1 a loadi 132 => r2 // r2 addr(b) load r2 => r3 // r3 b loadi 136 => r4 // r4 addr(c) load r4 => r5 // r5 c mult r3, r5 => r3 // r3 b * c add r1, r3 => r1 // r1 a b * c store r1 => r0 // a a b * c One clever strategy is to have the parser keep op3 in the op2 slot for stores, and teach the pretty printer to get it right on output. Note Well: store is different than all other opcodes In a store, the second operand is a use, even though it appears to the right of => The second operand is an address; the operation modifies Mem(op2), not op2. Your lab needs to treat both operands as uses, not as defs. This will catch someone. COMP 412, Fall 2018 7

Finding Live Ranges Renaming for Lab 2 Code Check A value is live from its definition (x ) to its last use (y x ) In a basic block, the notion is straightforward The live range is the interval from a definition to the last use of that value In a setting with control flow, the situation is more complex ( 13) # Operation 0 a 1 b 2 c a 3 d b 4 e a 5 f e use a b e def Simple Example a s live range is [0,4] b s live range is [1,3] e s live range is [4,5] Live ranges may, of course, overlap a & b are simultaneously live, so they cannot occupy the same PR a & e can occupy the same PR, as could b & e. An ILOC operation reads its operands before it writes its result COMP 412, Fall 2018 8

Finding Live Ranges Renaming for Lab 2 Code Check A value is live from its definition (x ) to its last use (y x ) In a basic block, the notion is straightforward The live range is the interval from a definition to the last use of that value In a setting with control flow, the situation is more complex # Operation 0 a 1 b 2 c a 3 d b 4 e a 5 f e a b e Observation Let MAXLIVE be the largest number, over all instructions, of live values If MAXLIVE k, allocation is easy If MAXLIVE > k, some values must be spilled to memory To find live ranges & to compute MAXLIVE, we will compute a set LIVE(i) at each operation i COMP 412, Fall 2018 9

Finding Live Ranges Renaming for Lab 2 Code Check A value is live from its definition (x ) to its last use (y x ) In a basic block, the notion is straightforward The live range is the interval from a definition to the last use of that value In a setting with control flow, the situation is more complex # Operation 0 a 1 b 2 c a 3 d b 4 a a 5 f a a b a A Subtly Different Case A single name can be part of multiple distinct live ranges. Changed e to a Operation 4: uses the value defined in op 0 creates a new value used in op 5 kills the value from operation 0 Overwriting a value kills it COMP 412, Fall 2018 10

Finding Live Ranges Renaming for Lab 2 Code Check A value is live from its definition (x ) to its last use (y x ) In a basic block, the notion is straightforward The live range is the interval from a definition to the last use of that value In a setting with control flow, the situation is more complex # Operation 0 a 1 b 2 c a 3 d b 4 a a 5 f a a b a Why Use Live Ranges? Consider an SR that comprises > 1 VRs The distinct VRs can be allocated to different PRs Makes allocation easier Each VR now corresponds to a unique definition & value Makes it easier to reason about spilled and restored values Simplifies writing and debugging a register allocator (local or global) COMP 412, Fall 2018 11

Representing ILOC Why are the SRs in the abstract & concrete versions different? To make the exposition of the algorithms more detailed, we need to be a little more concrete, particularly with regard to the IR Abstract drawing (e.g., prev slide) load r3 r3 mult r2 r3 r2 When I implemented Lab 2, the most important design decision I made was to have separate fields for the SR, VR, & PR. Concrete version INDEX SR VR PR NU SR VR PR NU SR VR PR NU 5 load r4 r5 6 6 mult r3 r5 r5 7 NEXT OP Each operand has names for its SR, VR, PR, and Next Use Front end initializes all fields In general, implementation should begin with data structure design. COMP 412, Fall 2018 14 *

VRName 0 // start VR names at 0 for i 0 to max SR number SRToVR[i] invalid LU[i] Renaming (Lab 2 Code Check 1) for i n to 0 by -1 if OPS[i].OP has a def then // handle the def OPS[i].OP3.VR SRToVR[OPS[i].OP[3].SR] OPS[i].OP3.NU LU[OPS[i].OP3.SR] SRToVR[OPS[i].OP3.SR] invalid // kill OP3 LU[OPS[i].OP3.SR] if OPS[i].OP1 is a register then // handle uses Update(OPS[i].OP1,i) if OPS[i].OP2 is a register then Update(OPS[i].OP2,i) if OPS[i].OP is STORE then // STORE is special Update(OPS[i].OP3,i) Update(OP, index) { if SRToVR[OP.SR] = invalid // SR has no VR then SRToVR[OP.SR] VRName++ OP.VR SRToVR[OP.SR] OP.NU LU[OP.SR] LU[OP.SR] index } COMP 412, Fall 2018 Finding live ranges, next uses, and renaming to VRs Algorithm walks block from last op to first op If SR has no VR, assigns one (indicates a last use ) Renames SR to VR at each mention of the SR in the code Tags each mention with the index of the VR s next use SRToVR[] and LU[] need an entry for each SR (keep track during parsing; it must be bounded by the block length) Note: SRToVR and LU are maps over relatively compact sets use a vector. 15

Back to the Example Initial State of the Algorithm index INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 1 1 load r0 r1 2 2 loadi 132 r2 3 3 load r2 r3 4 4 loadi 136 r4 5 5 load r4 r5 6 6 mult r3 r5 r3 7 7 add r1 r3 r1 8 8 store r1 r0 NEXT OP 0 1 2 3 4 5 VRName: 0 SRToVR LU Not a hash map in sight! COMP 412, Fall 2018 16

Back to the Example After Processing Operation 8 INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 1 1 load r0 r1 2 NEXT OP 2 loadi 132 r2 3 3 load r2 r3 4 4 loadi 136 r4 5 5 load r4 r5 6 6 mult r3 r5 r3 7 index 7 add r1 r3 r1 8 8 store r1 vr0 r0 vr1 0 1 2 3 4 5 SRToVR 1 0 LU 8 8 VRName: 2 store has no def Update r1 as a use Update r0 as a use COMP 412, Fall 2018 17

Back to the Example After Processing Operation 7 INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 1 1 load r0 r1 2 NEXT OP 2 loadi 132 r2 3 3 load r2 r3 4 4 loadi 136 r4 5 5 load r4 r5 6 index 6 mult r3 r5 r3 7 7 add r1 vr2 r3 vr3 r1 vr0 8 8 8 store r1 vr0 r0 vr1 0 1 2 3 4 5 SRToVR 1 2 3 LU 8 7 7 VRName: 4 Update r1 as a def & kill it Update r1 as a use Update r3 as a use COMP 412, Fall 2018 18

Back to the Example After Processing Operation 6 index INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 1 1 load r0 r1 2 2 loadi 132 r2 3 3 load r2 r3 4 4 loadi 136 r4 5 5 load r4 r5 6 6 mult r3 vr4 r5 vr5 r3 vr3 7 7 7 add r1 vr2 r3 vr3 r1 vr0 8 8 8 store r1 vr0 r0 vr1 NEXT OP 0 1 2 3 4 5 SRToVR 1 2 4 5 LU 8 7 6 6 VRName: 6 Update r3 as a def & kill it Update r3 as a use Update r5 as a use COMP 412, Fall 2018 19

Back to the Example After Processing Operation 5 INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 1 1 load r0 r1 2 NEXT OP 2 loadi 132 r2 3 3 load r2 r3 4 index 4 loadi 136 r4 5 5 load r4 vr6 r5 vr5 6 6 6 mult r3 vr4 r5 vr5 r3 vr3 7 7 7 add r1 vr2 r3 vr3 r1 vr0 8 8 8 store r1 vr0 r0 vr1 0 1 2 3 4 5 SRToVR 1 2 4 6 LU 8 7 6 5 VRName: 7 Update r5 as a def & kill it Update r4 as a use COMP 412, Fall 2018 20

Back to the Example After Processing Operation 4 INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 1 1 load r0 r1 2 NEXT OP 2 loadi 132 r2 3 index 3 load r2 r3 4 4 loadi 136 r4 vr6 5 5 5 load r4 vr6 r5 vr5 6 6 6 mult r3 vr4 r5 vr5 r3 vr3 7 7 7 add r1 vr2 r3 vr3 r1 vr0 8 8 8 store r1 vr0 r0 vr1 0 1 2 3 4 5 SRToVR 1 2 4 6 LU 8 7 6 5 VRName: 7 Update r4 as a def & kill it No uses COMP 412, Fall 2018 21

Back to the Example After Processing Operation 3 INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 1 1 load r0 r1 2 NEXT OP index 2 loadi 132 r2 3 3 load r2 vr7 r3 vr4 6 4 4 loadi 136 r4 vr6 5 5 5 load r4 vr6 r5 vr5 6 6 6 mult r3 vr4 r5 vr5 r3 vr3 7 7 7 add r1 vr2 r3 vr3 r1 vr0 8 8 8 store r1 vr0 r0 vr1 0 1 2 3 4 5 SRToVR 1 2 7 LU 8 7 3 VRName: 8 Update r3 as a def & kill it Update r2 as a use COMP 412, Fall 2018 22

Back to the Example After Processing Operation 2 index INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 1 1 load r0 r1 2 2 loadi 132 r2 vr7 3 3 NEXT OP 3 load r2 vr7 r3 vr4 6 4 4 loadi 136 r4 vr6 5 5 5 load r4 vr6 r5 vr5 6 6 6 mult r3 vr4 r5 vr5 r3 vr3 7 7 7 add r1 vr2 r3 vr3 r1 vr0 8 8 8 store r1 vr0 r0 vr1 0 1 2 3 4 5 SRToVR 1 2 LU 8 7 VRName: 8 Update r2 as a def & kill it COMP 412, Fall 2018 23

Back to the Example After Processing Operation 1 index INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 1 1 load r0 vr1 8 r1 vr2 7 2 NEXT OP 2 loadi 132 r2 vr7 3 3 3 load r2 vr7 r3 vr4 6 4 4 loadi 136 r4 vr6 5 5 5 load r4 vr6 r5 vr5 6 6 6 mult r3 vr4 r5 vr5 r3 vr3 7 7 7 add r1 vr2 r3 vr3 r1 vr0 8 8 8 store r1 vr0 r0 vr1 0 1 2 3 4 5 SRToVR 1 LU 1 VRName: 8 Update r1 as a def & kill it Update r0 as a use COMP 412, Fall 2018 24

Back to the Example After Processing Operation 0 index INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 vr1 1 1 1 load r0 vr1 8 r1 vr2 7 2 NEXT OP 2 loadi 132 r2 vr7 3 3 3 load r2 vr7 r3 vr4 6 4 4 loadi 136 r4 vr6 5 5 5 load r4 vr6 r5 vr5 6 6 6 mult r3 vr4 r5 vr5 r3 vr3 7 7 7 add r1 vr2 r3 vr3 r1 vr0 8 8 8 store r1 vr0 r0 vr1 0 1 2 3 4 5 SRToVR LU VRName: 8 Update r0 as a def & kill it COMP 412, Fall 2018 25 *

Back to the Example Code renamed so that virtual registers correspond to live ranges INDEX SR VR PR NU SR VR PR NU SR VR PR NU 0 loadi 128 r0 vr1 1 1 1 load r0 vr1 8 r1 vr2 7 2 2 loadi 132 r2 vr7 3 3 3 load r2 vr7 r3 vr4 6 4 4 loadi 136 r4 vr6 5 5 5 load r4 vr6 r5 vr5 6 6 6 mult r3 vr4 r5 vr5 r3 vr3 7 7 7 add r1 vr2 r3 vr3 r1 vr0 8 8 8 store r1 vr0 r0 vr1 NEXT OP What about MAXLIVE? MAXLIVE is the largest number of live entries in the SRToVR table during the algorithm s execution or 4 in this example (at ops 5) COMP 412, Fall 2018 26

Back to the Example Code renamed so that virtual registers correspond to live ranges Operation Live Ranges # LIVE VR0 VR1 VR2 VR3 VR4 VR5 VR6 VR7 @ end of op loadi 128 => vr1 1 load vr1 => vr2 2 loadi 132 => vr7 3 load vr7 => vr4 3 loadi 136 => vr6 4 load vr6 => vr5 4 mult vr4,vr5 => vr3 3 add vr2,vr3 => vr0 2 store vr0 => vr1 Live at the end of the operation 0 For code check 1, your lab must rename registers & print out the renamed ILOC. The COMP code 412, must Fall 2018 be executable by the ILOC simulator (e.g., register names r1 ) 27

Back to the Example Gray dashed lines show points where demand for registers is greatest. Code renamed so that virtual registers correspond to live ranges Operation Live Ranges # LIVE VR0 VR1 VR2 VR3 VR4 VR5 VR6 VR7 @ end of op loadi 128 => vr1 1 load vr1 => vr2 2 loadi 132 => vr7 3 load vr7 => vr4 3 loadi 136 => vr6 4 load vr6 => vr5 4 MaxLive mult vr4,vr5 => vr3 3 add vr2,vr3 => vr0 2 store vr0 => vr1 Live at the end of the operation 0 COMP 412, Fall 2018 28

Lab 2 Code Check 1 Code Check 1 tests to see if your allocator does renaming properly Original Code loadi 128 => r0 load r0 => r1 loadi 132 => r2 load r2 => r3 loadi 136 => r4 load r4 => r5 mult r3,r5 => r3 add r1,r3 => r1 store r1 => r0 Code Check Output loadi 128 => r1 load r1 => r2 loadi 132 => r7 load r7 => r4 loadi 136 => r6 load r6 => r5 mult r4,r5 => r3 add r2,r3 => r0 store r0 => r1 The output code defines each register exactly once. It needs to label registers as r0, r1,, so that the simulator can execute the code. COMP 412, Fall 2018 29 *

The Value of Renaming Local Register Allocation An example that came to my attention in 2009 a 0 a 1 + a 2 a 1 a 2 + a 0 a 2 a 0 + a 1 a 3 a 1 + a 2 a 4 a 2 + a 3 a 5 a 3 + a 4 This block does a series of additions, in a specific pattern that creates a known and controlled demand for registers. The example needs three registers. By increasing the distance between the definition of a value & its last reuse, we can arbitrarily increase the demand for registers. Why would we build this code? Constructing a microbenchmark to measure the number of registers available to the compiled code in essence, the compiler s effectiveness at register allocation COMP 412, Fall 2018 30

The Value of Renaming gcc 4.1, -O3, PowerPC G4 target Local Register Allocation An example that came to my attention in 2009 a 0 a 1 + a 2 a 1 a 2 + a 0 a 2 a 0 + a 1 a 3 a 1 + a 2 a 4 a 2 + a 3 a 5 a 3 + a 4 Without reuse of names, gcc handles 30 values with no spills. a 0 a 1 + a 2 a 1 a 2 + a 0 a 2 a 0 + a 1 a 0 a 1 + a 2 a 1 a 2 + a 0 a 2 a 0 + a 1 If names are reused, gcc spills on patterns with > 20 values. 3-value pattern The use of names, a code shape issue, makes a fundamental difference in the quality of code that gcc generates for this pattern. COMP 412, Fall 2018 llvm/clang handles 30 values either way. 31

gcc 4.1, -O3, PowerPC G4 target The point of this example is that the allocator confused the distinct live ranges with the The same Back name (e.g., Enda 0 ) with the result that it saw the problem as more constrained that it should have been. As a result it began spilling when it had at least 10 spare registers. Local Register Allocation Renaming the virtual registers so that each distinct definition targets a unique An example that came to my attention in 2009 name would avoid this problem. a 0 a 1 + a 2 a 0 a 1 + a 2 3-value In lab one, most 412 students discover that they should immediately rename a 1 a 2 + a 0 a 1 a 2 + a 0 pattern each live range to a unique name because it simplifies the rest of the code. (HINT) a 2 a 0 + a 1 a 2 a 0 + a 1 a 3 a 1 + a 2 a 0 a 1 + a 2 a 4 a 2 + a 3 a 1 a 2 + a 0 a 5 a 3 + a 4 a 2 a 0 + a 1 Without reuse of names, gcc handles 30 values with no spills. If names are reused, gcc spills on patterns with > 20 values. The use of names, a code shape issue, makes a fundamental difference in the quality of code that gcc generates for this pattern. STOP COMP 412, Fall 2018 32

Bottom-up Allocator A bottom-up allocator synthesizes the allocation based on low-level knowledge of the code & the partial solution computed thus far. for i 0 to n if (OP[i].OP1.VR has no PR) get a PR, say x, load OP[i].OP1.VR into x, and set OP[i].OP1.PR x if (OP[i].OP2.VR has no PR) get a PR, say y, load OP[i].OP2.VR into y, and set OP[i].OP2.PR y if either OP1 or OP2 is a last use free the corresponding PR Get a PR, say z, and set OP[i].OP3.PR z Consult the VRToPR map y x unless OP1.VR = OP2.VR if x = y, do not free the PR twice The action get a PR is the heart of the algorithm. If a PR is free, get a PR is easy If no PR is free, code must choose an occupied PR and spill its contents to memory COMP 412, Fall 2018 OP 1 & OP 2 can have the same PR, if & only if they have the same SR 33

Bottom-up Allocator Get a PR If some PR, say prx, is available, can use prx to hold the VR and either Restore the value from memory (for a use), or Use prx as the target (result) register (for a def) If all PR s are in use, the bottom-up allocator must free one Select the PR whose next use is farthest in the future, say pry Spill the current contents of pry to memory (into its spill location ) Use pry to hold the VR, as discussed above For a use, restore the value into pry; for a def, pry becomes the target register Complications will arise Breaking a tie: what happens if two VRs both have the farthest next use? Dirty versus clean values: what happens if farthest VR needs a store and the second farthest one does not? If the allocator needs to spill, it will need a register for the spill address. The simple way to handle that need is to reserve one register to hold the spill address, if MAXLIVE > k. COMP This issue 412, is Fall a 2018 property of the ILOC subset, not a general truth. 34

Back to the Example Bottom-up Allocator on the Example VR PR NU VR PR NU VR PR NU loadi 128 vr1 1 load vr1 8 vr2 7 loadi 132 vr7 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 k = 4 Assume that k = 4. Since MAXLIVE = 4 and k = 4, we would expect the allocator to keep all values in registers and avoid spilling. The allocator does not need to reserve a register for address computations on spilled values. COMP 412, Fall 2018 35

Example k = 4 index Initial State VR PR NU VR PR NU VR PR NU loadi 128 vr1 1 load vr1 8 vr2 7 loadi 132 vr7 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 index tracks i vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr2 pr3 PRToVR COMP 412, Fall 2018 36

Example k = 4 index Operation 0 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 8 vr2 7 loadi 132 vr7 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 needs no PR OP2 needs no PR OP3 needs a PR allocate pr0 to vr1 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr0 pr1 pr2 pr3 PRToVR vr1 COMP 412, Fall 2018 37

Example k = 4 Operation 1 index VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 (vr1) is in pr0 OP2 needs no PR OP3 needs a PR allocate pr1 to vr2 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr0 pr1 pr2 pr3 PRToVR vr1 vr2 COMP 412, Fall 2018 38

Example k = 4 index Operation 2 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 needs no PR OP2 needs no PR OP3 needs a PR allocate pr2 to vr7 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr2 pr0 pr1 pr2 pr3 PRToVR vr1 vr2 vr7 COMP 412, Fall 2018 39

Example If the allocator uses a LIFO discipline with names, it will produce this pattern of reuse within the operation. k = 4 index Operation 3 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 1 OP1 (vr7) is in pr2 OP2 needs no PR vr7 is dead, so free pr2 OP3 needs a PR allocate pr2 to vr4 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 pr0 pr1 pr2 pr3 VRToPR pr0 pr1 pr2 pr2 PRToVR vr1 vr2 vr4 @ end @ start COMP 412, Fall 2018 of op of op 40

Example k = 4 index Operation 4 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr3 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 needs no PR OP2 needs no PR OP3 needs a PR allocate pr3 to vr6 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr2 pr3 pr0 pr1 pr2 pr3 PRToVR vr1 vr2 vr4 vr6 COMP 412, Fall 2018 41

Example k = 4 index Operation 5 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr3 5 load vr6 pr3 vr5 pr3 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 (vr6) in in pr3 OP2 needs no PR vr6 is dead, so free pr3 OP3 needs a PR allocate pr3 to vr5 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr2 pr3 pr3 pr0 pr1 pr2 pr3 PRToVR vr1 vr2 vr4 vr5 COMP 412, Fall 2018 42

Example k = 4 index Operation 6 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr3 5 load vr6 pr3 vr5 pr3 6 mult vr4 pr2 vr5 pr3 vr3 pr2 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 (vr4) in in pr2 OP2 (vr5) is in pr3 Both vr4 & vr5 are dead, so free pr2 & pr3 OP3 needs a PR allocate pr2 to vr5 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr2 pr2 pr3 pr0 pr1 pr2 pr3 PRToVR vr1 vr2 vr3 COMP 412, Fall 2018 43

Example k = 4 index Operation 7 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr3 5 load vr6 pr3 vr5 pr3 6 mult vr4 pr2 vr5 pr3 vr3 pr2 7 add vr2 pr1 vr3 pr2 vr0 pr1 8 store vr0 vr1 OP1 (vr2) in in pr1 OP2 (vr3) is in pr2 Both vr2 & vr3 are dead, so free pr1 & pr2 OP3 needs a PR allocate pr1 to vr0 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr1 pr0 pr1 pr2 pr0 pr1 pr2 pr3 PRToVR vr1 vr0 COMP 412, Fall 2018 44

Example k = 4 index Operation 8 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr3 5 load vr6 pr3 vr5 pr3 6 mult vr4 pr2 vr5 pr3 vr3 pr2 7 add vr2 pr1 vr3 pr2 vr0 pr1 8 store vr0 pr1 vr1 pr0 OP1 (vr0) in in pr1 OP2 needs no PR OP3 is a use (vr1) that is in pr0 Both vr0 & vr1 are dead, so free pr0 & pr1 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr1 pr0 pr0 pr1 pr2 pr3 PRToVR COMP 412, Fall 2018 45 *

Example k = 4 Final Code VR PR NU VR PR NU VR PR NU loadi 128 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 136 vr6 pr3 5 load vr6 pr3 vr5 pr3 6 mult vr4 pr2 vr5 pr3 vr3 pr2 7 add vr2 pr1 vr3 pr2 vr0 pr1 8 store vr0 pr1 vr1 pr0 As expected, the code allocates easily into 4 physical registers. COMP 412, Fall 2018 46

Example with a Reserved Spill Register Local Allocator, Same Example, 1 Register Reserved for Spills VR PR NU VR PR NU VR PR NU loadi 128 vr1 1 load vr1 8 vr2 7 loadi 132 vr7 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 Now, lets look at what happens if we reserve a register for spilling, so the allocator has only 3 registers to hold VRs. COMP 412, Fall 2018 47

Example k = 4, with 1 register reserved for spilling index Initial State VR PR NU VR PR NU VR PR NU loadi 128 vr1 1 load vr1 8 vr2 7 loadi 132 vr7 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 index tracks i vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr2 PRToVR COMP 412, Fall 2018 48

Example k = 4, with 1 register reserved for spilling index Operation 0 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 8 vr2 7 loadi 132 vr7 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 needs no PR OP2 needs no PR OP3 needs a PR allocate pr0 to vr1 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr0 pr1 pr2 PRToVR vr1 COMP 412, Fall 2018 49

Example k = 4, with 1 register reserved for spilling index Operation 1 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 (vr1) is in pr0 OP2 needs no PR OP3 needs a PR allocate pr1 to vr2 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr0 pr1 pr2 PRToVR vr1 vr2 COMP 412, Fall 2018 50

Example k = 4, with 1 register reserved for spilling index Operation 2 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 vr4 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 needs no PR OP2 needs no PR OP3 needs a PR allocate pr2 to vr7 So far, no difference vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr2 pr0 pr1 pr2 PRToVR vr1 vr2 vr7 COMP 412, Fall 2018 51

Example k = 4, with 1 register reserved for spilling index Operation 3 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 (vr7) is in pr2 OP2 needs no PR vr7 is dead, so free pr2 OP3 needs a PR allocate pr2 to vr4 Works just as before vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr2 pr2 pr0 pr1 pr2 PRToVR vr1 vr2 vr4 COMP 412, Fall 2018 52

Example k = 4, with 1 register reserved for spilling index Operation 4 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 needs no PR OP2 needs no PR OP3 needs a PR none is available choose one to spill * vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 VRToPR pr0 pr1 pr2 pr0 pr1 pr2 PRToVR vr1 vr2 vr4 COMP 412, Fall 2018 53

Example k = 4, with 1 register reserved for spilling spill vr1 Operation 4 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 needs no PR OP2 needs no PR OP3 needs a PR none is available choose one to spill Algorithm says to pick the VR with the farthest NextUse Spill vr1 to free pr0 before operation 4 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 pr0 pr1 pr2 VRToPR pr0 pr1 pr2 PRToVR vr1 vr2 vr4 NextUse 8 7 6 COMP 412, Fall 2018 54

Example k = 4, with 1 register reserved for spilling spill vr1 Operation 4 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr0 5 load vr6 vr5 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 needs no PR OP2 needs no PR OP3 needs a PR none is available choose one to spill Algorithm says to pick the PR with the farthest NextUse Spill vr1 to free pr0 before operation 4 Allocate pr0 to vr6 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 pr0 pr1 pr2 VRToPR pr0 pr1 pr2 pr0 PRToVR vr6 vr2 vr4 NextUse 5 7 6 COMP 412, Fall 2018 55

Example k = 4, with 1 register reserved for spilling spill vr1 index Operation 5 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr0 5 load vr6 pr0 vr5 pr0 6 mult vr4 vr5 vr3 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 (vr6) is in pr0 OP2 needs no PR vr6 is dead, so free pr0 OP3 needs a PR Allocate pr0 to vr5 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 pr0 pr1 pr2 VRToPR pr1 pr2 pr0 pr0 PRToVR vr6 vr2 vr4 NextUse 6 7 6 COMP 412, Fall 2018 56

Example k = 4, with 1 register reserved for spilling spill vr1 index Operation 6 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr0 5 load vr6 pr0 vr5 pr0 6 mult vr4 pr2 vr5 pr0 vr3 pr0 7 add vr2 vr3 vr0 8 store vr0 vr1 OP1 (vr4) is in pr2 OP2 (vr5) is in pr0 Both vr4 & vr5 are dead, so free pr2 & pr0 OP3 needs a PR Allocate pr0 to vr3 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 pr0 pr1 pr2 VRToPR pr1 pr0 pr2 pr0 PRToVR vr3 vr2 NextUse 7 7 COMP 412, Fall 2018 57

Example k = 4, with 1 register reserved for spilling spill vr1 index Operation 7 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr0 5 load vr6 pr0 vr5 pr0 6 mult vr4 pr2 vr5 pr0 vr3 pr0 7 add vr2 pr1 vr3 pr0 vr0 pr0 8 store vr0 vr1 OP1 (vr2) is in pr1 OP2 (vr3) is in pr0 Both vr2 & vr3 are dead, so free pr1 & pr0 OP3 needs a PR Allocate pr0 to vr0 vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 pr0 pr1 pr2 VRToPR pr0 pr1 pr0 PRToVR vr0 NextUse 8 COMP 412, Fall 2018 58

Example k = 4, with 1 register reserved for spilling spill vr1 restore vr1 index Operation 8 VR PR NU VR PR NU VR PR NU loadi 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 vr6 pr0 5 load vr6 pr0 vr5 pr0 6 mult vr4 pr2 vr5 pr0 vr3 pr0 7 add vr2 pr1 vr3 pr0 vr0 pr0 8 store vr0 pr0 vr1 pr1 OP1 (vr0) is in pr0 OP2 needs no PR OP3 (vr1) is a use, since the operation is a store vr1 was spilled, so we need to allocate a register & restore it allocate pr1 to vr1 Insert a restore Both vr0 & vr1 are dead, so free pr1 & pr0 OP3 needs a PR vr0 vr1 vr2 vr3 vr4 vr5 vr6 vr7 pr0 pr1 pr2 VRToPR pr0 pr1 PRToVR vr0 vr1 NextUse COMP 412, Fall 2018 59

Spill and Restore In general, if the allocator needs to spill and restore values, it will need to reserve a register for the address of the spill location Reduces the number of available registers by one In example, would have only had pr1 and pr2, not pr0 Can check this case by keeping track of MAXLIVE during renaming and comparing it to k Spill has two real cases: If the value was computed or is the result of a load, it must be stored into a spill location Allocate a memory location to hold it and generate the store to spill it If the value is the result of a loadi, the allocator can restore it without a spill In the example, vr1 is the result of a loadi It can be restored with an identical loadi, so the spill needs no code The restore will consist of a copy of the original loadi COMP 412, Fall 2018 60

Spill and Restore In general, if the allocator needs to spill and restore values, it will need to reserve a register for the address of the spill location Reduces the number of available registers by one In example, would have only had pr1 and pr2, not pr0 Can check this case by keeping track of MAXLIVE during renaming and comparing it to k Similarly, restore has two real cases: If the value was spilled to memory Compute the address of the spill location (a loadi) and load from it If the value did not need a spill Restore the value using the loadi ( rematerialization ) Of course, if all spills are rematerialized, the allocator did not need to reserve a register it gets complex Remember, the problem is NP-Complete COMP 412, Fall 2018 61

Example with the Spill Code k = 4, with 1 register reserved for spilling Final Code VR PR NU VR PR NU VR PR NU loadi 128 128 vr1 pr0 1 load vr1 pr0 8 vr2 pr1 7 loadi 132 132 vr7 pr2 3 load vr7 pr2 vr4 pr2 6 loadi 136 136 vr6 pr0 5 load vr6 pr0 vr5 pr0 6 mult vr4 pr2 vr5 pr0 vr3 pr0 7 add vr2 pr1 vr3 pr0 vr0 pr0 8 loadi 128 128 vr1 pr1 8 store vr0 pr0 vr1 pr1 vr1 is rematerializable spill requires no code restore becomes a duplicate of operation 0 Accuracy doesn t matter on the NextUse; relative magnitude does. We can treat the restore operation as if it were 7.5 COMP 412, Fall 2018 62

Spill and Restore In the example, the allocator spilled vr1 before operation 4. Should it have spilled vr1 earlier? It would have been legal to spill it after its use in operation 1 No further use until operation 8 In this case, it would not have gained us anything, in this case The algorithm spills a value only when it runs out of registers Spilling pre-emptively does not help in straight-line code With loops and conditionals, your mileage may vary Should we work the example with k = 3 and a reserved register? That case is left as an exercise for you Or, code it up and try it with the reference allocator on CLEAR COMP 412, Fall 2018 63

Spill and Restore One more spill case that you might consider (once everything else is working ) Two values with equal next-use distance r12 and r13 have same next use Assume neither is rematerializable No obvious basis to choose If r13 is the result of a load, with a known address (result of a loadi) and r12 is not Might be able to restore r13 with a loadi followed by a load, avoiding the spill A value that can be reloaded from its original location is a CLEAN value Cannot be a store between the original load and the restore The store might change the value in memory No telling how often this happens Need to spill here add r2,r8 => r12 loadi 132 => r10 load r10 => 13 mult r12, r13 => r25 sub r17, r25 => r26 add r12, r13 => r32 STOP COMP 412, Fall 2018 64