COSE312: Compilers. Lecture 20 Data-Flow Analysis (2)

Similar documents
Lecture 2. Introduction to Data Flow Analysis

Lecture 4 Introduction to Data Flow Analysis

Introduction. Data-flow Analysis by Iteration. CSc 553. Principles of Compilation. 28 : Optimization III

Data Flow Information. already computed

Compiler Optimization and Code Generation

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Data-flow Analysis - Part 2

Lecture 6 Foundations of Data Flow Analysis

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

Why Global Dataflow Analysis?

Lecture 6 Foundations of Data Flow Analysis

Data Flow Analysis. Program Analysis

Review; questions Basic Analyses (2) Assign (see Schedule for links)

CMSC430 Spring 2009 Midterm 2 (Solutions)

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

ELEC 876: Software Reengineering

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

A Domain-Specific Language for Generating Dataflow Analyzers

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

Dataflow analysis (ctd.)

Intro to dataflow analysis. CSE 501 Spring 15

MIT Introduction to Dataflow Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

A main goal is to achieve a better performance. Code Optimization. Chapter 9

Advanced Compilers Introduction to Dataflow Analysis by Example. Fall Chungnam National Univ. Eun-Sun Cho

COSE312: Compilers. Lecture 1 Overview of Compilers

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

Reaching Definitions and u-d Chaining M.B.Chandak

Global Data Flow Analysis of Syntax Tree Intermediate Code

Department of Computer Sciences CS502. Purdue University is an Equal Opportunity/Equal Access institution.

Compiler Design Spring 2017

compile-time reasoning about the run-time ow represent facts about run-time behavior represent eect of executing each basic block

Program Analysis. Readings

CS 6353 Compiler Construction, Homework #3

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

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

Example of Global Data-Flow Analysis

CS 406/534 Compiler Construction Putting It All Together

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

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

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Lecture 4. More on Data Flow: Constant Propagation, Speed, Loops

Mosig M1 - PLSCD Written exam

Register Allocation & Liveness Analysis

Data Flow Analysis and Computation of SSA

Introduction to Compiler Design: optimization and backend issues

Compiler Optimisation

Control flow and loop detection. TDT4205 Lecture 29

Lecture Compiler Middle-End

Control-Flow Graphs & Dataflow Analysis. CS4410: Spring 2013

Lecture 5. Partial Redundancy Elimination

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

Foundations of Dataflow Analysis

Global Optimizations

process variable x,y,a,b,c: integer begin x := b; -- d2 -- while (x < c) loop end loop; end process; d: a := b + c

Lecture Notes: Dataflow Analysis Examples

Homework 5 COSE212, Fall 2018

Real-Time Operating Systems M. Course Syllabus, Spring 2013

Global Optimizations

4. Compilation Basics Semantic Analysis Code Generation Introduction to Optimization

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

Calvin Lin The University of Texas at Austin

Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

Flow Analysis. Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 11, 2015

Program Optimizations using Data-Flow Analysis

The University of Iowa. 22c181: Formal Methods in Software Engineering. Spring Course Overview

Introduction. Inline Expansion. CSc 553. Principles of Compilation. 29 : Optimization IV. Department of Computer Science University of Arizona

A Domain-Specific Language for Generating Dataflow Analyzers

Data-Flow Analysis Foundations

Midterm 2. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: April 18, 2012

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Lecture 3 Local Optimizations, Intro to SSA

Static analysis and all that

COP5621 Exam 4 - Spring 2005

COMP108 Algorithmic Foundations

CS553 Lecture Generalizing Data-flow Analysis 3

This test is not formatted for your answers. Submit your answers via to:

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

COSE212: Programming Languages. Lecture 0 Course Overview

Data Flow Analysis. CSCE Lecture 9-02/15/2018

The Pairwise-Comparison Method

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

Chapter 1 Introduction

Code Optimization. Code Optimization

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Review. Pat Morin COMP 3002

Compilation 2013 Liveness Analysis

Lecture 21 CIS 341: COMPILERS

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Intermediate representation

Languages and Compiler Design II IR Code Optimization

Control Flow Analysis. Reading & Topics. Optimization Overview CS2210. Muchnick: chapter 7

: Compiler Design. 9.6 Available expressions 9.7 Busy expressions. Thomas R. Gross. Computer Science Department ETH Zurich, Switzerland

Pointer Analysis. What is Points-to Analysis? Outline. What is Points-to Analysis? What is Points-to Analysis? What is Pointer Analysis? Rupesh Nasre.

Lecture Notes on Static Semantics

MTAT : Software Testing

Compiler construction in4303 lecture 8

COMP108 Algorithmic Foundations

Transcription:

COSE312: Compilers Lecture 20 Data-Flow Analysis (2) Hakjoo Oh 2017 Spring Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 1 / 18

Final Exam 6/19 (Mon), 15:30 16:45 (in class) Do not be late. Coverage: semantic analysis, IR, Optimization Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 2 / 18

Liveness Analysis A variable is live at program point p if its value could be used in the future (along some path starting at p). Liveness analysis aims to compute the set of live variables for each basic block of the program. Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 3 / 18

Example: Liveness of Variables We analyze liveness from the future to the past. The live range of b: {2 3, 3 4} The live range of a: {1 2, 4 5 2} (not from 2 3 4) The live range of c: the entire code Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 4 / 18

Example: Liveness of Variables Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 5 / 18

Applications Deadcode elimination Problem: Eliminate assignments whose computed values never get used. Solution: How? Uninitialized variable detection Problem: Detect uninitialized use of variables Solution: How? Register allocation Problem: Rewrite the intermediate code to use no more temporaries than there are machine registers Example: a := c + d r1 := r2 + r3 e := a + b r1 := r1 + r4 f := e - 1 r1 := r1-1 Solution: How? Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 6 / 18

Liveness Analysis The goal is to compute in : Block 2 Var out : Block 2 Var 1 Compute def/use sets. 2 Derive transfer functions for each basic block in terms of def/use sets. 3 Derive the set of data-flow equations. 4 Solve the equation by the iterative fixed point algorithm. Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 7 / 18

Def/Use Sets Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 8 / 18

cf) Def/Use sets are only dynamically computable Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 9 / 18

Data-Flow Equations Intuitions: 1 If a variable is in use(b), then it is live on entry to block B. 2 If a variable is live at the end of block B, and not in def(b), then the variable is also live on entry to B. 3 If a variable is live on enty to block B, then it is live at the end of predecessors of B. Equations: in(b) = use(b) (out(b) def(b)) out(b) = in(s) B S Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 10 / 18

Fixed Point Computation For all i, in(b i ) = out(b i ) = while (changes to any in and out occur) { For all i, update in(b i ) = use(b) (out(b) def(b)) out(b i ) = B S in(s) } Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 11 / 18

Example 1st 2nd 3rd use def out in out in out in 6 {c} {c} {c} {c} 5 {a} {c} {a, c} {a, c} {a, c} {a, c} {a, c} 4 {b} {a} {a, c} {b, c} {a, c} {b, c} {a, c} {b, c} 3 {b, c} {c} {b, c} {b, c} {b, c} {b, c} {b, c} {b, c} 2 {a} {b} {b, c} {a, c} {b, c} {a, c} {b, c} {a, c} 1 {a} {a, c} {c} {a, c} {c} {a, c} {c} Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 12 / 18

Available Expressions Analysis An expression x + y is available at a point p if every path from the entry node to p evaluates x + y, and after the last such evaluation prior to reaching p, there are no subsequent assignments to x or y. Application: common subexpression elimination (i.e., given a program that computes e more than once, eliminate one of the duplicate computations) Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 13 / 18

Available Expressions Analysis The goal is to compute in : Block 2 Expr out : Block 2 Expr 1 Derive the set of data-flow equations. 2 Solve the equation by the iterative fixed point algorithm. Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 14 / 18

Gen/Kill Sets Exercise: gen(b): the set of expressions evaluated and not subsequently killed kill(b): the set of expressions whose variables can be killed What expressions are generated and killed by each of statements? Statement s gen(s) kill(s) x = y + z x = alloc(n) x = y[i] x[i] = y What expressions are generated and killed by the block? a = b + c b = a d c = b + c d = a d Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 15 / 18

1. Set up a set of data-flow equations Intuitions: 1 At the entry, no expressions are available. 2 An expression is available at the entry of a block only if it is available at the end of all its predecessors. Equations: in(entry ) = out(b) = gen(b) (in(b) kill(b)) in(b) = out(b) P B Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 16 / 18

2. Solve the equations Trivial solution: in(b i ) = out(b i ) =. Need to find the greatest solution (i.e., greatest fixed point) of the equation. in(entry ) = For other B i, in(b i ) = out(b i ) = Expr while (changes to any in and out occur) { For all i, update in(b i ) = P B i out(p ) out(b i ) = gen(b i ) (in(b i ) kill(b i )) } Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 17 / 18

Summary: Data-flow Analysis Hakjoo Oh COSE312 2017 Spring, Lecture 20 June 6, 2017 18 / 18