Abstract Interpretation Continued

Similar documents
Contents. 8-1 Copyright (c) N. Afshartous

Compiler Design. Register Allocation. Hwansoo Han

Register allocation. Overview

Compilers and Code Optimization EDOARDO FUSELLA

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

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

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Run-time Environments -Part 3

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

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Run-time Environments

Run-time Environments

Compiler Architecture

Run-time Environments - 3

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

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

Memory management COSC346

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

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

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Global Register Allocation - Part 2

CS61C : Machine Structures

CMSC 330: Organization of Programming Languages

Register Allocation & Liveness Analysis

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Intermediate Representations

CA31-1K DIS. Pointers. TA: You Lu

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

CMSC 330: Organization of Programming Languages

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

16 Sharing Main Memory Segmentation and Paging

Global Register Allocation

Lecture 25: Register Allocation

Global Register Allocation via Graph Coloring

LAB C Translating Utility Classes

register allocation saves energy register allocation reduces memory accesses.

The Structure of a Syntax-Directed Compiler

CSC 1600 Memory Layout for Unix Processes"

Run-time Environments - 2

CS 261 Fall Mike Lam, Professor. Virtual Memory

Programming Languages

Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

Memory Management. Dr. Yingwu Zhu

Code Generation. CS 540 George Mason University

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

StackVsHeap SPL/2010 SPL/20

Computer Science Engineering Sample Papers

Register Allocation. Michael O Boyle. February, 2014

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

15 Sharing Main Memory Segmentation and Paging

CODE GENERATION Monday, May 31, 2010

MODEL ANSWERS COMP36512, May 2016

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

Procedure and Object- Oriented Abstraction

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

In Java we have the keyword null, which is the value of an uninitialized reference type

Preview. Memory Management

CS 406/534 Compiler Construction Putting It All Together

Main Memory and the CPU Cache

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Programming. Pointers, Multi-dimensional Arrays and Memory Management

More Code Generation and Optimization. Pat Morin COMP 3002

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Run-Time Environments/Garbage Collection

ARM Architecture and Assembly Programming Intro

CSE 373 OCTOBER 23 RD MEMORY AND HARDWARE

Types II. Hwansoo Han

CIS 341 Final Examination 4 May 2017

CSC D70: Compiler Optimization Register Allocation

System Software Assignment 1 Runtime Support for Procedures

Names, Scope, and Bindings

Compiler Construction

Topic 12: Register Allocation

Lecture 21 CIS 341: COMPILERS

Automatic Memory Management

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Data-Flow Analysis Foundations

CS143 Final Spring 2016

Global Register Allocation

Compiler Construction

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Computer Memory. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION 1

Optimizing Compilers. Vineeth Kashyap Department of Computer Science, UCSB. SIAM Algorithms Seminar, 2014

Run Time Environment

Calvin Lin The University of Texas at Austin

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

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Advanced Programming & C++ Language

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Name, Scope, and Binding. Outline [1]

Compiler construction 2009

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 13: Address Translation

Register Allocation. Stanford University CS243 Winter 2006 Wei Li 1

Topic 7: Activation Records

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam Solutions 29 April 2015

Qualifying Exam in Programming Languages and Compilers

Limitations of the stack

Lecture Overview Register Allocation

Transcription:

Abstract Interpretation Continued

Height of Lattice: Length of Max. Chain height=5 size=14 T height=2 size = T -2-1 0 1 2

Chain of Length n A set of elements x 0,x 1,..., x n in D that are linearly ordered, that is x 0 < x 1 <... < x n A lattice can have many chains. Its height is the maximum n for all the chains If there is no upper bound on lengths of chains, we say lattice has infinite height Any monotonic sequence of distinct elements has length at most equal to lattice height including sequence occuring during analysis! such sequences are always monotonic

In constant propagation, each value can height=2 change only twice size = -2-1 0 1 2 consider value for x before assignment Initially: changes 1 st time to: 1 change 2 nd time to: T total changes: two (height) Total number of changes bounded by: x = 1 n = 1000 while (x < n) { x = x + 2 var facts : Map[Nodes,Map[VarNames,Element]] T height Nodes Vars

Exercise B 32 the set of all 32-bit integers What is the upper bound for number of changes in the entire analysis for: 3 variables, 7 program points for these two analyses: 1) constant propagation for constants from B 32 2) The following domain D: D = { U { [a,b] a,b B 32, a b

Height of B 32 D = { U { [a,b] a,b B 32, a b One possible chain of maximal length: [MinInt,MaxInt]

Initialization Analysis first initialization uninitialized initialized

class Test { What does javac say to this: static void test(int p) { int n; p = p - 1; if (p > 0) { n = 100; while (n!= 0) { System.out.println(n); n = n - p; Test.java:8: variable n might not have been initialized while (n > 0) { ^ 1 error

Program that compiles in java class Test { static void test(int p) { int n; p = p - 1; if (p > 0) { n = 100; else { n = -100; while (n!= 0) { System.out.println(n); n = n - p; We would like variables to be initialized on all execution paths. Otherwise, the program execution could be undesirably affected by the value that was in the variable initially. We can enforce such check using initialization analysis.

What does javac say to this? static void test(int p) { int n; p = p - 1; if (p > 0) { n = 100; System.out.println( Hello! ); if (p > 0) { while (n!= 0) { System.out.println(n); n = n - p;

class Test { static void test(int p) { int n; p = p - 1; if (p > 0) { n = 100; else { n = -100; while (n!= 0) { Initialization Analysis System.out.println(n); n = n - p; T indicates presence of flow from states where variable was not initialized: If variable is possibly uninitialized, we use T Otherwise (initialized, or unreachable): If var occurs anywhere but left-hand side of assignment and has value T, report error

Sketch of Initialization Analysis Domain: for each variable, for each program point: D = {,T At program entry, local variables: T ; parameters: At other program points: each variable: An assignment x = e sets variable x to lub (join, ) of any value with T gives T uninitialized values are contagious along paths value for x means there is definitely no possibility for accessing uninitialized value of x

Run initialization analysis Ex.1 int n; p = p - 1; if (p > 0) { n = 100; while (n!= 0) { n = n - p;

Run initialization analysis Ex.2 int n; p = p - 1; if (p > 0) { n = 100; if (p > 0) { n = n - p;

Liveness Analysis Variable is dead if its current value will not be used in the future. If there are no uses before it is reassigned or the execution ends, then the variable is surely dead at a given point. first initialization last use dead live dead live dead

What is Written and What Read x = y + x Example: if (x > y) Purpose: Register allocation: find good way to decide which variable should go to which register at what point in time.

How Transfer Functions Look

Initialization: Forward Analysis while (there was change) pick edge (v1,statmt,v2) from CFG such that facts(v1) has changed facts(v2)=facts(v2) join transferfun(statmt, facts(v1)) Liveness: Backward Analysis while (there was change) pick edge (v1,statmt,v2) from CFG such that facts(v2) has changed facts(v1)=facts(v1) join transferfun(statmt, facts(v2))

Example x = m[0] y = m[1] xy = x * y z = m[2] yz = y*z xz = x*z res1 = xy + yz m[3] = res1 + xz

Register Machines Better for most purposes than stack machines closer to modern CPUs (RISC architecture) closer to control-flow graphs simpler than stack machine (but register set is finite) Examples: ARM architecture RISC V: http://riscv.org/ Directly Addressable RAM large - GB, slow even with cache A few fast registers R0,R1,,R 31

Basic Instructions of Register Machines R i Mem[R j ] load Mem[R j ] R i store R i R j * R k compute: for an operation * Efficient register machine code uses as few loads and stores as possible.

State Mapped to Register Machine Both dynamically allocated heap and stack expand heap need not be contiguous; can request more memory from the OS if needed stack grows downwards Heap is more general: Can allocate, read/write, and deallocate, in any order Garbage Collector does deallocation automatically Must be able to find free space among used one, group free blocks into larger ones (compaction), Stack is more efficient: allocation is simple: increment, decrement top of stack pointer (SP) is often a register if stack grows towards smaller addresses: to allocate N bytes on stack (push): SP := SP - N to deallocate N bytes on stack (pop): SP := SP + N 1 GB SP 10MB 50kb 0 Stack free memory Heap Constants Static Globals Exact picture may depend on hardware and OS

Stack Machine vs General Register Machine Code Naïve Correct Translation JVM: i32.mul Register Machine: R1 Mem[SP] SP = SP + 4 R2 Mem[SP] R2 R1 * R2 Mem[SP] R2

Register Allocation

How many variables? x,y,z,xy,xz,res1 Do we need 6 distinct registers if we wish to avoid load and stores? x = m[0] y = m[1] xy = x * y z = m[2] yz = y*z xz = x*z res1 = xy + yz m[3] = res1 + xz 7 variables: x,y,z,xy,yz,xz,res1 x = m[0] y = m[1] xy = x * y z = m[2] yz = y*z y = x*z x = xy + yz m[3] = x + y can do it with 5 only! // reuse y // reuse x

Idea of Register Allocation program: x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz live variable analysis result: { {x {x,y {y,x,xy {y,z,x,xy {x,z,xy,yz {xy,yz,xz {r,xz { x y z xy yz xz r

Color Variables Avoid Overlap of Same Colors program: x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz live variable analysis result: { {x {x,y {y,x,xy {y,z,x,xy {x,z,xy,yz {xy,yz,xz {r,xz { x y z xy yz xz r R1 R2 R3 R4 Each color denotes a register 4 registers are enough for this program

Color Variables Avoid Overlap of Same Colors program: x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz live variable analysis result: { {x {x,y {y,x,xy {y,z,x,xy {x,z,xy,yz {xy,yz,xz {r,xz { x y z xy yz xz r R1 R2 R3 R4 x y xy z yz xz r Each color denotes a register 4 registers are enough for this 7-variable program

How to assign colors to variables? program: x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz live variable analysis result: { {x {x,y {y,x,xy {y,z,x,xy {x,z,xy,yz {xy,yz,xz {r,xz { x y z xy yz xz r For each pair of variables determine if their lifetime overlaps = there is a point at which they are both alive. Construct interference graph z y xy x yz xz r

Edges between members of each set program: x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz live variable analysis result: { {x {x,y {y,x,xy {y,z,x,xy {x,z,xy,yz {xy,yz,xz {r,xz { x y z xy yz xz r For each pair of variables determine if their lifetime overlaps = there is a point at which they are both alive. Construct interference graph z y xy x yz xz r

Final interference graph program: x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz live variable analysis result: { {x {x,y {y,x,xy {y,z,x,xy {x,z,xy,yz {xy,yz,xz {r,xz { x y z xy yz xz r For each pair of variables determine if their lifetime overlaps = there is a point at which they are both alive. Construct interference graph z y xy x yz xz r

Coloring interference graph program: x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz live variable analysis result: { {x {x,y {y,x,xy {y,z,x,xy {x,z,xy,yz {xy,yz,xz {r,xz { x y z xy yz xz r Need to assign colors (register numbers) to nodes such that: if there is an edge between nodes, then those nodes have different colors. standard graph vertex coloring problem z:3 xy:4 xz:3 y:2 yz:2 r:4 x:1

Idea of Graph Coloring Register Interference Graph (RIG): indicates whether there exists a point of time where both variables are live look at the sets of live variables at all progrma points after running live-variable analysis if two variables occur together, draw an edge we aim to assign different registers to such these variables finding assignment of variables to K registers: corresponds to coloring graph using K colors

All we need to do is solve graph coloring problem xy xz y yz r z x NP hard In practice, we have heuristics that work for typical graphs If we cannot fit it all variables into registers, perform a spill: store variable into memory and load later when needed

Heuristic for Coloring with K Colors Simplify: If there is a node with less than K neighbors, we will always be able to color it! So we can remove such node from the graph (if it exists, otherwise remove other node) This reduces graph size. It is useful, even though incomplete (e.g. planar can be colored by at most 4 colors, yet can have nodes with many neighbors) xy xz xy xz xy z y x yz r y yz z x z y x yz xy xy z z y z y z y x

Heuristic for Coloring with K Colors Select Assign colors backwards, adding nodes that were removed If the node was removed because it had <K neighbors, we will always find a color if there are multiple possibilities, we can choose any color xy:4 xz:3 xy:4 xz:3 xy:4 z:3 y:2 yz:2 x:1 r:4 z:3 y:2 yz:2 x:1 z:3 y:2 yz:2 x:1 xy:4 xy:4 z:3 z:3 y:2 z:3 y:2 z:3 y:2 x:1

Use Computed Registers x = m[0] y = m[1] xy = x * y z = m[2] yz = y*z xz = x*z r = xy + yz m[3] = res1 + xz z:3 xy:4 xz:3 y:2 yz:2 x:1 r:4 R1 = m[0] R2 = m[1] R4 = R1*R2 R3 = m[2] R2 = R2*R3 R3 = R1*R3 R4 = R4 + R2 m[3] = R4 + R3

Summary of Heuristic for Coloring Simplify (forward, safe): If there is a node with less than K neighbors, we will always be able to color it! so we can remove it from the graph Potential Spill (forward, speculative): If every node has K or more neighbors, we still remove one of them we mark it as node for potential spilling. Then remove it and continue Select (backward): Assign colors backwards, adding nodes that were removed If we find a node that was spilled, we check if we are lucky, that we can color it. if yes, continue if not, insert instructions to save and load values from memory (actual spill). Restart with new graph (a graph is now easier to color as we killed a variable)

Conservative Coalescing Suppose variables tmp1 and tmp2 are both assigned to the same register R and the program has an instruction: tmp2 = tmp1 which moves the value of tmp1 into tmp2. This instruction then becomes R = R which can be simply omitted! How to force a register allocator to assign tmp1 and tmp2 to same register? merge the nodes for tmp1 and tmp2 in the interference graph! this is called coalescing But: if we coalesce non-interfering nodes when there are assignments, then our graph may become more difficult to color, and we may in fact need more registers! Conservative coalescing: coalesce only if merged node of tmp1 and tmp2 will have a small degree so that we are sure that we will be able to color it (e.g. resulting node has degree < K)

Run Register Allocation Ex.3 use 4 registers, coallesce j=i i = 0 s = s + i i = i + b j = i s = s + j + b j = j + 1

{s,b i = 0 {s,i,b s = s + i {s,i,b i = i + b {s,i,b j = i {s,j,b s = s + j + b {j j = j + 1 { Run Register Allocation Ex.3 use 3 registers, coallesce j=i s s j i b coalesce i,j b s:1 i,j:2 color b:3

Run Register Allocation Ex.3 use 4 registers, coallesce j=i s:1 i,j:2 i = 0 s = s + i b:3 i = i + b j = i // puf! s = s + j + b j = j + 1 R2 = 0 R1 = R1 + R2 R2 = R2 + R3 R1 = R1 + R2 + R3 R2 = R2 + 1