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

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

Register allocation. TDT4205 Lecture 31

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

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

Topic 12: Register Allocation

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

Instruction Selection. Problems. DAG Tiling. Pentium ISA. Example Tiling CS412/CS413. Introduction to Compilers Tim Teitelbaum

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky

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

Lecture 21 CIS 341: COMPILERS

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

Compilers and Code Optimization EDOARDO FUSELLA

Register allocation. Overview

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

Register Allocation & Liveness Analysis

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

Where we are. Instruction selection. Abstract Assembly. CS 4120 Introduction to Compilers

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Global Register Allocation

Compiler Architecture

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

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

Code Generation. CS 540 George Mason University

Compiler Design. Register Allocation. Hwansoo Han

CSC D70: Compiler Optimization Register Allocation

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

Register Allocation 1

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Register Allocation. Lecture 16

Global Register Allocation

register allocation saves energy register allocation reduces memory accesses.

Lecture 25: Register Allocation

CHAPTER 3. Register allocation

Register Allocation. Lecture 38

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

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

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

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

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

Compiler construction in4303 lecture 9

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

Lecture Compiler Backend

Lecture Notes on Register Allocation

Program Exploitation Intro

CHAPTER 3. Register allocation

x86 Assembly Crash Course Don Porter

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

Register Allocation via Hierarchical Graph Coloring

Second Part of the Course

The Instruction Set. Chapter 5

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

Assembly Language: Function Calls" Goals of this Lecture"

x86 assembly CS449 Spring 2016

Global Register Allocation - Part 3

Assembly Language: Function Calls" Goals of this Lecture"

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

Subprograms: Local Variables

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

Code generation and local optimization

Code generation and local optimization

Abstract Interpretation Continued

Digital Forensics Lecture 3 - Reverse Engineering

Register Allocation. Preston Briggs Reservoir Labs

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction

Computer Organization & Assembly Language Programming

Global Register Allocation via Graph Coloring

Assembly Language: Function Calls

Computer Systems Lecture 9

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Global Register Allocation - Part 2

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

Practical Malware Analysis

Towards the Hardware"

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

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

3. Process Management in xv6

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

Lecture Notes for 04/04/06: UNTRUSTED CODE Fatima Zarinni.

CSC201, SECTION 002, Fall 2000: Homework Assignment #3

Operating Systems and Protection CS 217

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II

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

THEORY OF COMPILATION

(the bubble footer is automatically inserted into this space)

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

x86 assembly CS449 Fall 2017

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

Code generation and optimization

Outline. Unresolved references

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

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

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Chapter 11. Addressing Modes

Linear-Scan Register Allocation. CS 352 Lecture 12 11/28/07

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

Transcription:

Variables vs. Registers/Memory CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 33: Register Allocation 18 Apr 07 Difference between IR and assembly code: IR (and abstract assembly) manipulate data in local and temporary variables Assembly code manipulates data in memory/registers During code generation, compiler must account for this difference Compiler backend must allocate variables to memory or registers in the generated assembly code CS 412/413 Spring 2007 Introduction to Compilers 1 CS 412/413 Spring 2007 Introduction to Compilers 2 Simple Approach Straightforward solution: Allocate each variable in activation record At each instruction, bring values needed into registers, perform operation, then store result to memory mov 16(%ebp), %eax x = y z mov 20(%ebp), %ebx add %ebx, %eax mov %eax, 24(%ebx) Problem: program execution very inefficient moving data back and forth between memory and registers CS 412/413 Spring 2007 Introduction to Compilers 3 Register Allocation Better approach = register allocation: keep variable values in registers as long as possible Best case: keep a variable s value in a register throughout the lifetime of that variable In that case, we don t need to ever store it in memory We say that the variable has been allocated in a register Otherwise allocate variable in activation record We say that variable is spilled to memory Which variables can we allocate in registers? Depends on the number of registers in the machine Depends on how variables are being used Main Idea: cannot allocate two variables to the same register if they are both live at some program point CS 412/413 Spring 2007 Introduction to Compilers 4 Register Allocation Algorithm Hence, basic algorithm for register allocation is: 1. Perform live variable analysis (over abstract assembly code!) 2. Inspect live variables at each program point 3. If two variables are in same live set, they` can t be allocated to the same register they interfere with each other 4. Conversely, if two variables do not interfere with each other, they can be assigned the same register. Interference Graph Nodes = program variables Edges = connect variables that interfere with each other Register allocation = graph coloring eax ebx b a {a} b = a 2; {a,b} c = b*b; {a,c} b = c 1; {a,b} return b*a; c CS 412/413 Spring 2007 Introduction to Compilers 5 CS 412/413 Spring 2007 Introduction to Compilers 6 1

Graph Coloring Questions: Can we efficiently find a coloring of the graph whenever possible? Can we efficiently find the optimum coloring of the graph? Can we assign registers to avoid move instructions? What do we do when there aren t enough colors (registers) to color the graph? Coloring a Graph Assume K = number of registers (take K=3) Try to color graph with K colors Key operation = Simplify: find some node with at most K-1 edges and cut it out of the graph CS 412/413 Spring 2007 Introduction to Compilers 7 CS 412/413 Spring 2007 Introduction to Compilers 8 Coloring a Graph Stack Algorithm Idea: once coloring is found for simplified graph, removed node can be colored using free color Algorithm: simplify until graph contain no nodes Unwind stack, adding nodes back & assigning colors Phase 1: Simplification Repeatedly simplify graph When a variable (i.e., graph node) is removed, push it on a stack Phase 2: Coloring Unwind stack and reconstruct the graph as follows: Pop variable from the stack Add it back to the graph Color the node for that variable with a color that it doesn t interfere with simplify color CS 412/413 Spring 2007 Introduction to Compilers 9 CS 412/413 Spring 2007 Introduction to Compilers 10 Stack Algorithm Failure of Heuristic Example: a b If graph cannot be colored, it will reduce to a graph in which every node has at least K neighbors c d how about: a b? May happen even if graph is colorable in K! c d e f CS 412/413 Spring 2007 Introduction to Compilers 11 Finding K-coloring is NP-hard problem (requires search) CS 412/413 Spring 2007 Introduction to Compilers 12 2

Spilling Once all nodes have K or more neighbors, pick a node and mark it for possible spilling (storage in activation record). Remove it from graph, push it on stack Try to pick node not used much, not in inner loop Optimistic Coloring Spilled node may be K-colorable Try to color it when popping the stack If not colorable, actual spill: assign it a location in the activation record x x CS 412/413 Spring 2007 Introduction to Compilers 13 CS 412/413 Spring 2007 Introduction to Compilers 14 Accessing Spilled Variables Need to generate additional instructions to get spilled variables out of activation record and back in again Simple approach: always keep extra registers handy for shuttling data in and out Better approach: rewrite code introducing a new temporary, rerun liveness analysis and register allocation Rewriting Code Example: add v1, v2 Suppose that v2 is selected for spilling and assigned to activation record location [ebp-24] Add new variable (say t35) for just this instruction, rewrite: mov 24(%ebp), t35 add v1, t35 Advantage: t35 has short lifetime and doesn t interfere with other variables as much as v2 did. Now rerun algorithm; fewer or no variables will spill. CS 412/413 Spring 2007 Introduction to Compilers 15 CS 412/413 Spring 2007 Introduction to Compilers 16 Putting Pieces Together Precolored Nodes Simplify Potential Spill Optimistic coloring Actual Spill Simplification Coloring Some variables are pre-assigned to registers mul instruction has use[i] = eax, def[i] = { eax, edx } result of function call returned in eax To properly allocate registers, treat these register uses as special temporary variables and enter into interference graph as precolored nodes CS 412/413 Spring 2007 Introduction to Compilers 17 CS 412/413 Spring 2007 Introduction to Compilers 18 3

Precolored Nodes Simplify. Never remove a pre-colored node --- it already has a color, i.e., it is a given register Coloring. Once simplified graph is all colored nodes, add other nodes back in and color them using precolored nodes as starting point Optimizing Move Instructions Code generation produces a lot of extra mov instructions mov t5, t9 If we can assign t5 and t9 to same register, we can get rid of the mov --- effectively, copy elimination at the register allocation level. Idea: if t5 and t9 are not connected in inference graph, coalesce them into a single variable; the move will be redundant. CS 412/413 Spring 2007 Introduction to Compilers 19 CS 412/413 Spring 2007 Introduction to Compilers 20 Coalescing When coalescing nodes, take union of edges Hence, coalescing results in high-degree nodes Problem: coalescing nodes can make a graph uncolorable t5 t9 t5/t9 Conservative Coalescing Conservative = ensure that coalescing doesn t make the graph non-colorable (if it was colorable before) Approach 1: coalesce a and b if resulting node ab has less than K neighbors of significant degree Safe because we can simplify graph by removing neighbors with insignificant degree, then remove coalesced node and get the same graph as before Approach 2: coalesce a and b if for every neighbor t of a: either t already interferes with b; or t has insignificant degree Safe because removing insignificant neighbors with coalescing yields a subgraph of the graph obtained by removing those neighbors without coalescing CS 412/413 Spring 2007 Introduction to Compilers 21 CS 412/413 Spring 2007 Introduction to Compilers 22 Simplification Coalescing Full Algorithm Consider M = set of move-related nodes (which appear in the source or destination of a move instruction) and N = all other variables Start by simplifying as many nodes as possible from N Coalesce some pairs of move-related nodes using conservative coalescing; delete corresponding mov instruction(s) Coalescing gives more opportunities for simplification: coalesced nodes may be simplified If can neither simplify nor coalesce, take a node f in M and freeze all the move instructions involving that variable; i.e., move all f-related nodes from M to N; go back to simplify. If all nodes frozen, no simplify possible, spill a variable Simplify Coalesce Freeze Potential Spill Optimistic coloring Actual Spill Simplification Coloring CS 412/413 Spring 2007 Introduction to Compilers 23 CS 412/413 Spring 2007 Introduction to Compilers 24 4

Overall Code Generation Process Start with low-level IR code Build DAG of the computation Access global variables using static addresses Access function arguments using frame pointer Assume all local variables and temporaries are in registers (assume unbounded number of registers) Generate abstract assembly code Perform tiling of DAG Register allocation Live variable analysis over abstract assembly code Assign registers and generate assembly code Program array[int] a function f:(int x) x) { int int i; i; a[xi] = a[xi] 1; 1; } Example Low IR t1 t1 = xi xi t1 t1 = t1*4 t1 t1 = $at1 t2 t2 = t21 t3 t3 = xi xi t3 t3 = t3*4 t3 t3 = $at3 CS 412/413 Spring 2007 Introduction to Compilers 25 CS 412/413 Spring 2007 Introduction to Compilers 26 Accesses to Function Arguments DAG Construction t1 t1 = xi xi t1 t1 = t1*4 t1 t1 = $at1 t2 t2 = t21 t3 t3 = xi xi t3 t3 = t3*4 t3 t3 = $at3 t4 t4 = ebp8 t5 t5 = *t4 *t4 t1 t1 = t5i t5i t1 t1 = t1*4 t1*4 t1 t1 = $at1 t2 t2 = t21 t6=ebp8 t7 t7 = *t6 *t6 t3 t3 = t7i t7i t3 t3 = t3*4 t3*4 t3 t3 = $at3 t4 t4 = ebp8 t5 t5 = *t4 *t4 t1 t1 = t5i t5i t1 t1 = t1*4 t1*4 t1 t1 = $at1 t2 t2 = t21 t6=ebp8 t7 t7 = *t6 *t6 t3 t3 = t7i t7i t3 t3 = t3*4 t3*4 t3 t3 = $at3 store load 1 * $a 4 i load ebp 8 CS 412/413 Spring 2007 Introduction to Compilers 27 CS 412/413 Spring 2007 Introduction to Compilers 28 Tiling Abstract Assembly Generation Find tiles Maximal Munch Dynamic programming store load 1 store load 1 Abstract Assembly Temporaries to transfer values between tiles No temporaries inside any of the tiles r2 * r1 $a r3 4 i load ebp 8 r2 * r1 $a r3 4 i load ebp 8 mov $a, r1 mov 8(%ebp), r3 mov i, r2 add r3, r2 add $1, (r1,r2,4) CS 412/413 Spring 2007 Introduction to Compilers 29 CS 412/413 Spring 2007 Introduction to Compilers 30 5

Register Allocation Register Allocation Abstract Assembly mov $a, r1 mov 8(%ebp), r3 mov i, r2 add r3, r2 add $1, (r1,r2,4) Live Variables {%ebp, i} i} mov $a, $a, r1 r1 {%ebp,r1,i} mov 8(%ebp), r3 r3 {r1, {r1, r3, r3, i} i} mov i, i, r2 r2 {r1,r2,r3} add r3, r3, r2 r2 {r1,r2} add $1, $1, (r1,r2,4) {} {} Live Variables {%ebp, i} i} mov $a, $a, r1 r1 {%ebp,r1,i} mov 8(%ebp), r3 r3 {r1, {r1, r3, r3, i} i} mov i, i, r2 r2 {r1,r2,r3} add r3, r3, r2 r2 {r1,r2} add $1, $1, (r1,r2,4) {} {} Build interference graph i %ebp r1 r3 r2 Allocate registers: eax: r1, ebx: r3 i, r2 spilled to memory CS 412/413 Spring 2007 Introduction to Compilers 31 CS 412/413 Spring 2007 Introduction to Compilers 32 Assembly Code Generation Abstract Assembly mov $a, $a, r1 r1 mov 8(%ebp), r3 r3 mov i, i, r2 r2 add r3, r3, r2 r2 add $1, $1, (r1,r2,4) Assembly Code mov $a, $a, %eax mov 8(%ebp), %ebx mov 12(%ebp), %ecx mov %ecx, -16(%ebp) add %ebx, -16(%ebp) mov 16(%ebp), %ecx add $1, $1, (%eax,%ecx,4) Register allocation results: eax: r1; ebx: r3; i, r2 spilled to memory Where We Are Source Program Optimized Assembly Code CS 412/413 Spring 2007 Introduction to Compilers 33 CS 412/413 Spring 2007 Introduction to Compilers 34 6