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

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

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

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

Register allocation. Overview

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

Topic 12: Register Allocation

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

register allocation saves energy register allocation reduces memory accesses.

Register Allocation & Liveness Analysis

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

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

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

Compiler Design. Register Allocation. Hwansoo Han

CSC D70: Compiler Optimization Register Allocation

Register Allocation III. Interference Graph Allocators. Coalescing. Granularity of Allocation (Renumber step in Briggs) Chaitin

Global Register Allocation via Graph Coloring

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

CS 406/534 Compiler Construction Putting It All Together

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

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

Lecture 21 CIS 341: COMPILERS

Code generation for modern processors

Code generation for modern processors

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

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

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

Register Allocation III. Interference Graph Allocators. Computing the Interference Graph (in MiniJava compiler)

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

Global Register Allocation

Register allocation. TDT4205 Lecture 31

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

The C2 Register Allocator. Niclas Adlertz

Lecture Notes on Register Allocation

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

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

Register Allocation via Hierarchical Graph Coloring

CS153: Compilers Lecture 8: Compiling Calls

LECTURE 19. Subroutines and Parameter Passing

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Run-time Environment

Register Allocation. Lecture 38

Global Register Allocation - Part 3

Functions and Procedures

Lecture 4: Instruction Set Design/Pipelining

Liveness Analysis and Register Allocation

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

CSCE 5610: Computer Architecture

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

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

Register Allocation. Preston Briggs Reservoir Labs

Register Allocation 1

Chapter 2A Instructions: Language of the Computer

Compiler Architecture

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky

Global Register Allocation - Part 2

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

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

Compilers and Code Optimization EDOARDO FUSELLA

Global Register Allocation

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

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

CS64 Week 5 Lecture 1. Kyle Dewey

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

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Run-time Environments - 2

CS453 Register Allocation

Lecture 5. Announcements: Today: Finish up functions in MIPS

Compilation 2014 Activation Records (Part 1)

Global Register Allocation Based on Graph Fusion

MIPS Procedure Calls. Lecture 6 CS301

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Lecture 25: Register Allocation

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

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

System Software Assignment 1 Runtime Support for Procedures

EECS 583 Class 15 Register Allocation

Course Administration

CS 316: Procedure Calls/Pipelining

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

CSE Lecture In Class Example Handout

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

Lecture 3: The Instruction Set Architecture (cont.)

Lecture 3: The Instruction Set Architecture (cont.)

CA Compiler Construction

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

Programming Studio #9 ECE 190

Register Allocation. Introduction Local Register Allocators

CHAPTER 3. Register allocation

ECE 468, Fall Midterm 2

CHAPTER 3. Register allocation

ECE260: Fundamentals of Computer Engineering

Functions in MIPS. Functions in MIPS 1

Review of Activation Frames. FP of caller Y X Return value A B C

Code generation for superscalar RISCprocessors. Characteristics of RISC processors. What are RISC and CISC? Processors with and without pipelining

Instruction Set Principles and Examples. Appendix B

CSC 2400: Computer Systems. Using the Stack for Function Calls

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Transcription:

Low-Level Issues Last lecture! Liveness analysis! Register allocation!today! More register allocation!later! Instruction scheduling CS553 Lecture Register Allocation I 1 Register Allocation!Problem! Assign an unbounded number of symbolic registers to a fixed number of architectural registers! Simultaneously live data must be assigned to different architectural registers!goal! Minimize overhead of accessing data! Memory operations (loads & stores)! Register moves CS553 Lecture 2

Improvement #1: Simplification Phase [Chaitin 81]!Idea! Nodes with < k neighbors are guaranteed colorable!remove them from the graph first! Reduces the degree of the remaining nodes!must spill only when all remaining nodes have degree! k!referred to as pessimistic spilling CS553 Lecture 3 Simplifying Graph Allocators Chaitin Briggs CS553 Lecture 4

Algorithm [Chaitin81] while interference graph not empty do while " a node n with < k neighbors do Remove n from the graph simplify Push n on a stack if any nodes remain in the graph then { blocked with >= k edges } Pick a node n to spill { lowest spill-cost or } spill Add n to spill set { highest degree } Remove n from the graph if spill set not empty then Insert spill code for all spilled nodes { store after def; load before use } Reconstruct interference graph & start over while stack not empty do Pop node n from stack color or select Allocate n to a register CS553 Lecture 5 Example Attempt to 3-color this graph (,, ) Stack: d c b f a e e a f b d c Possible order: e a f b c d CS553 Lecture 6

! Is! Idea The Problem: Worst Case Assumptions the following graph 2-colorable? s1 s4 s2 s3!clearly 2-colorable!!But Chaitin s algorithm leads to an immediate block and spill!!the algorithm assumes the worst case, namely, that all neighbors will be assigned a different color CS553 Lecture 7 Improvement #2: Optimistic Spilling [Briggs 89] s1 s4 s2 s3! Some neighbors might get the same color! Nodes with k neighbors might be colorable! Blocking does not imply that spilling is necessary! Push blocked nodes on stack (rather than place in spill set)! Check colorability upon popping the stack, when more information is available Defer decision CS553 Lecture 8

Algorithm [Briggs et al. 89] while interference graph not empty do while " a node n with < k neighbors do Remove n from the graph simplify Push n on a stack if any nodes remain in the graph then { blocked with >= k edges } Pick a node n to spill { lowest spill-cost/highest degree } Push n on stack Remove n from the graph defer decision while stack not empty do Pop node n from stack if n is colorable then make decision Allocate n to a register else Insert spill code for n { Store after def; load before use } Reconstruct interference graph & start over CS553 Lecture Register Allocation III 9 Example Attempt to 2-color this graph (, ) Stack: d c b * f* a* e* * blocked node e a f b d c Increasing Weight: e a f b c d CS553 Lecture Register Allocation III 10

! Move! Idea! Problem! Rule Improvement #3: Coalescing instructions! Code generation can produce unnecessary move instructions mov t1, t2! If we can assign t1 and t2 to the same register, we can eliminate the move! If t1 and t2 are not connected in the interference graph, coalesce them into a single variable! Coalescing can increase the number of edges and make a graph uncolorable! Limit coalescing coalesce to avoid uncolorable graphs t1 t2 t1 t2 CS553 Lecture 11 Coalescing Logistics! When building the interference graph, do NOT make virtual registers interfere due to copies.! If the virtual registers s1 and s2 do not interfere and there is a copy statement s1 = s2 then s1 and s2 can be coalesced.! Example CS553 Lecture 12

! Computing! Use! If! Choosing Computing the Interference Graph (in MiniJava compiler) interference graph to enable coalescing results of live variable analysis for each flow graph node n do for each def in def(n) do for each temp in liveout(n) do if (not stmt(n) isa MOVE or use!= temp) then E # E $ (def, temp) CS553 Lecture 13 Register Allocation: Spilling we can t find a k-coloring of the interference graph! Spill variables (nodes) until the graph is colorable variables to spill! Choose least frequently accessed variables! Break ties by choosing nodes with the most conflicts in the interference graph! Yes, these are heuristics! CS553 Lecture 14

! Goal! Static More on Spilling!Chaitin s algorithm restarts the whole process on spill! Necessary, because spill code (loads/stores) uses registers! Okay, because it usually only happens a couple times!alternative! Reserve 2-3 registers for spilling! Don t need to start over! But have fewer registers to work with CS553 Lecture 15 Weighted Interference Graph # f (r)! Weight(s) = f(r) is execution frequency of r " references r of t approximation! Use some reasonable scheme to rank variables! Some possibilities! Weight(t) = num of times t is used in program! Weight(t) = 10 % (# uses in loops) + (# uses in straightline code)! Weight(t) = 20 % (# uses in loops) + 2 % (# uses in straightline code) + (# uses in a branch statement) CS553 Lecture 16

! Problem! Two! Goals! Complicating Register Allocation and Procedure Calls! Register values may change across procedure calls! The allocator must be sensitive to this approaches! Work within a well-defined calling convention! Use interprocedural allocation (not covering this) CS553 Lecture 17 Calling Conventions! Fast calls (pass arguments in registers, minimal register saving/restoring)! Language-independent! Support debugging, profiling, garbage collection, etc. Issues! Varargs! Passing/returning aggregates! Exceptions, non-local returns! setjmp()/longjmp() CS553 Lecture 18

! Partition! Caller-saved! Example! Callee-saved! Example Architecture Review: Caller- and Callee-Saved Registers registers into two categories! Caller-saved! Callee-saved registers! Caller must save/restore these registers when live across call! Callee is free to use them caller foo() { r caller = 4 save r caller goo() restore r caller r caller? } goo() { r caller = 99 } callee goo() is free to modify r caller CS553 Lecture 19 Architecture Review: Caller- and Callee-Saved Registers registers! Callee must save/restore these registers when it uses them! Caller expects callee to not change them caller foo() { r callee = 4 } goo() r callee? callee goo() { save r callee r callee = 99 restore r callee } goo() promises not to modify r callee CS553 Lecture 20

! SPARC! Alpha! MIPS! PPC! StarCore! Insensitive! Sensitive Architectures with Callee and Caller Registers! hardware-saved %i0-%i7, %o0-%o8! 7 callee-saved out of 32 registers! caller-saved: $t0-$t9, $a0-$a3, $v0-$v1! callee-saved: $s0-$s7, $ra, $fp! 18 callee-saved! 14 caller-saved EABI! 4 callee-saved! 28 caller-saved CS553 Lecture 21 Register Allocation and Calling Conventions register allocation! Save all live caller-saved registers before call; restore after! Save all used callee-saved registers at procedure entry; restore at return! Suboptimal foo() { t = = t s = f() g() = s } register allocation A variable that is not live across calls should go in caller-saved registers A variable that is live across multiple calls should go in callee-saved registers! Encode calling convention constraints in the IR and interference graph! How?!Use precolored nodes CS553 Lecture 22

! Add! Express! Callee-saved! Caller-saved Precolored Nodes architectural registers to interference graph! Precolored (mutually interfering)! Not simplifiable! Not spillable allocation constraints! Integers usually can t be stored in floating point registers! Some instructions can only store result in certain registers! Caller-saved and callee-saved registers... s3 r1 s2 s4 r2 f3 s1 floating point integer floating point integer CS553 Lecture 23 Precolored Nodes and Calling Conventions registers! Treat entry as def of all callee-saved registers! Treat exit as use of them all! Allocator must spill callee-saved registers to use them foo() { def(r3) } use(r3) Live range of callee-saved registers registers! Variables live across call interfere with all caller-saved registers CS553 Lecture 24

! Callee-saved! The! r1,! r3 Example foo(): def(r3) t1 := r3 a :=... b :=...... a... call goo... b... r3 := t1 use(r3) return r3 r2 callersaved callee-saved r1 r2 t1 b a CS553 Lecture 25 Tradeoffs registers + Decreases code size: one procedure body may have multiple calls + Small procedures tend to need fewer registers than large ones; callee-save makes sense because procedure sizes are shrinking! May increase execution time: For long-lived variables, may save and restore registers multiple times, once for each procedure, instead of a single end-to-end save/restore larger problem! We re making local decisions for policies that require global information CS553 Lecture 26

! Run-time Problem with Callee-Saved Registers systems (e.g., setjmp()/longjmp() and debuggers) need to know register values in any stack frame!caller-saved registers are on stack frame at known location!callee-saved registers? F4: save r3 F3: r1, r2 caller-saved r3 callee-saved F2: save r1,r2 F1: CS553 Lecture 27 Concepts!Decision tree for register allocation!global approaches centered around an interference graph! greedy coloring! coloring with simplification [Chaitin]! coloring with simplification and optimistic spilling [Briggs]! coloring with simplification, coalescing, and optimistic spilling!register allocation across procedure calls! precolored nodes in the interference graph CS553 Lecture 28

! Lecture Next Time! Instruction scheduling CS553 Lecture 29