Lecture 4 Introduction to Data Flow Analysis

Similar documents
Lecture 2. Introduction to Data Flow Analysis

Compiler Optimization and Code Generation

Lecture 6 Foundations of Data Flow Analysis

Lecture 6 Foundations of Data Flow Analysis

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

Data-flow Analysis - Part 2

Data Flow Information. already computed

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

Data Flow Analysis. Program Analysis

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

Why Global Dataflow Analysis?

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

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

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

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

ELEC 876: Software Reengineering

Dataflow analysis (ctd.)

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

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

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

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

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

Lecture 5. Partial Redundancy Elimination

Intro to dataflow analysis. CSE 501 Spring 15

Program Analysis. Readings

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

More Dataflow Analysis

Homework Assignment #1 Sample Solutions

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

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

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

Lecture 10: Lazy Code Motion

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

CMSC430 Spring 2009 Midterm 2 (Solutions)

Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006)

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

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

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

20b -Advanced-DFA. J. L. Peterson, "Petri Nets," Computing Surveys, 9 (3), September 1977, pp

Why Data Flow Models? Dependence and Data Flow Models. Learning objectives. Def-Use Pairs (1) Models from Chapter 5 emphasized control

Page # 20b -Advanced-DFA. Reading assignment. State Propagation. GEN and KILL sets. Data Flow Analysis

Lecture 8: Induction Variable Optimizations

Example of Global Data-Flow Analysis

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

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

CS 406/534 Compiler Construction Putting It All Together

Compiler Optimisation

CS 6353 Compiler Construction, Homework #3

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

Compiler Design Spring 2017

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

Live Variable Analysis. Work List Iterative Algorithm Rehashed

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

A Domain-Specific Language for Generating Dataflow Analyzers

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

Global Data Flow Analysis of Syntax Tree Intermediate Code

Lecture 3 Local Optimizations, Intro to SSA

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

Data Flow Analysis and Computation of SSA

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

Compiler Optimisation

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

Compilation 2013 Liveness Analysis

EECS 583 Class 6 Dataflow Analysis

Introduction to Compiler Design: optimization and backend issues

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

Lecture Compiler Middle-End

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

Introduction to Machine-Independent Optimizations - 4

Data Flow Analysis. Daqing Hou

HW/SW Codesign. WCET Analysis

EECS 583 Class 6 Dataflow Analysis

Introduction to Machine-Independent Optimizations - 6

CS 4120 Lecture 31 Interprocedural analysis, fixed-point algorithms 9 November 2011 Lecturer: Andrew Myers

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

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Control flow and loop detection. TDT4205 Lecture 29

Induction Variable Identification (cont)

Topic 9: Control Flow

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

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Thursday, December 23, The attack model: Static Program Analysis

Simone Campanoni Alias Analysis

Lecture 9: Loop Invariant Computation and Code Motion

CS553 Lecture Generalizing Data-flow Analysis 3

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

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

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

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

Calvin Lin The University of Texas at Austin

Program Optimizations using Data-Flow Analysis

CoVaC: Compiler Verification by Program Analysis of the Cross-Product. Anna Zaks, Amir Pnueli. May 28, FM 08, Turku, Finland

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Dataflow Analysis. Xiaokang Qiu Purdue University. October 17, 2018 ECE 468

Global Optimizations

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

EECS 583 Class 6 Dataflow Analysis

Program Static Analysis. Overview

EECS 583 Class 6 More Dataflow Analysis

Transcription:

Lecture 4 Introduction to Data Flow Analysis I. Structure of data flow analysis II. Example 1: Reaching definition analysis III. Example 2: Liveness analysis IV. Generalization What is Data Flow Analysis? Local analysis (e.g. value numbering) analyze effect of each instruction compose effects of instructions to derive information from beginning i of basic block to each instruction ti Data flow analysis analyze effect of each basic block compose effects of basic blocks to derive information at basic block boundaries from basic block boundaries, apply local technique to generate information on instructions 15-745: Intro to Data Flow 1 15-745: Intro to Data Flow 2 What is Data Flow Analysis? (Cont.) Data flow analysis: Flow-sensitive: sensitive to the control flow in a function intraprocedural analysis Examples of optimizations: Constant propagation Common subexpression elimination Dead code elimination a = b + c d = 7 e = b + c a = 243 e = d+3 g = a Value of x? Which h definition iti defines x? Is the definition still meaningful (live)? B3 Static Program vs. Dynamic Execution a = 10 if input() b = a a = 11 Statically: Finite program Dynamically: Can have infinitely many possible execution paths Data flow analysis abstraction: For each point in the program: combines information of all the instances of the same program point. Example of a data flow question: Which definition defines the value used in statement b = a? 15-745: Intro to Data Flow 3 15-745: Intro to Data Flow 4 1

Effect of a statement: a = b+c Effects of a Basic Block Uses variables (b, c) Kills an old definition (old definition of a) new definition (a) Compose effects of statements -> Effect of a basic block A locally exposed use in a b.b. is a use of a data item which is not preceded in the b.b. by a definition of the data item any definition of a data item in the basic block kills all definitions of the same data item reaching the basic block. A locally available definition = last definition of data item in b.b. t1 = r1+r2 r2 = t1 t2 = r2+r1 r1 = t2 t3 = r1*r1 r2 = t3 if r2>100 goto L1 15-745: Intro to Data Flow 5 II. Reaching Definitions B0 d3: x = 1 d4: y = 2 d2: y = 11 if e d5: z = x d6: x = 4 Every assignment is a definition A definition d reaches a point p if there exists path from the point immediately following d to p such that t d is not killed (overwritten) along that t path. Problem statement For each point in the program, determine if each definition in the program reaches the point A bit vector per program point, vector-length = #defs 15-745: Intro to Data Flow 6 Reaching Definitions: Another Example d0: a = x L1: if input() GOTO L2 Data Flow Analysis Schema out[] in[2] d1: b = a d2: a = y GOTO L1 15-745: Intro to Data Flow 7 L2: f 2 f 3 in[] Build a flow graph (nodes = basic blocks, edges = control flow) Set up a set of equations between in[b] and out[b] for all basic blocks b Effect of code in basic block: Transfer function f b relates in[b] and out[b], for same b Effect of flow of control: relates out[b 1 ], in[b 2 ] if b 1 and b 2 are adjacent Find a solution to the equations 15-745: Intro to Data Flow 8 2

in[2] Data Flow Analysis Schema out[] f 2 Build a flow graph (nodes = basic blocks, edges = control flow) Set up a set of equations between in[b] and out[b] for all basic blocks b Effect of code in basic block: Transfer function f b relates in[b] and out[b], for same b Effect of flow of control: relates out[b 1 ], in[b 2 ] if b 1 and b 2 are adjacent Find a solution to the equations 15-745: Intro to Data Flow 9 f 3 in[] Effects of a Statement in[b0] out[b0] d2: y = 11 f d0 f d1 f s : A transfer function of a statement abstracts the execution with respect to the problem of interest For a statement s (d: x = y + z) out[s] = f s (in[s]) = Gen[s] U (in[s]-kill[s]) Gen[s]: definitions generated: Gen[s] = {d Propagated definitions: in[s] - Kill[s], where Kill[s]=set of all other defs to x in the rest of program 15-745: Intro to Data Flow 10 f d2 Effects of a Basic Block in[b0] f d0 f d1 f B = f d2 f d1 f d1 d2: y = 11 f out[b0] d2 Transfer function of a statement s: out[s] = f s (in[s]) = Gen[s] U (in[s]-kill[s]) Transfer function of a basic block B: Composition of transfer functions of statements in B out[b] = f B (in[b]) = f d2 f d1 f d0 (in[b]) = Gen[d 2 ] U (Gen[d 1 ] U (Gen[d 0 ] U (in[b]-kill[d 0 ]))-Kill[d 1 ])) -Kill[d 2 ] = Gen[d 2 ] U (Gen[d 1 ] U (Gen[d 0 ] - Kill[d 1 ]) - Kill[d 2 ]) U in[b] - (Kill[d 0 ] U Kill[d 1 ] U Kill[d 2 ]) = Gen[B] U (in[b] - Kill[B]) Gen[B]: locally exposed definitions (available at end of bb) Kill[B]: set of definitions killed by B 15-745: Intro to Data Flow 11 B0 d3: x = 1 d4: y = 2 Example d2: y = 11 if e d5: z = x d6: x = 4 a transfer function f b of a basic block b: OUT[b] = f b (IN[b]) incoming reaching definitions -> outgoing reaching definitions A basic block b generates definitions: Gen[b], set of locally available definitions in b kills definitions: in[b] - Kill[b], where Kill[b]=set of defs (in rest of program) killed by defs in b out[b] = Gen[b] U (in(b)-kill[b]) 15-745: Intro to Data Flow 12 3

in[2] Effects of the Edges (acyclic) out[] out[b] = f b (in[b]) Join node: a node with multiple predecessors meet operator: in[b] = out[p 1 ] U out[p 2 ] U... U out[p n ], where p 1,..., p n are all predecessors of b f 2 f 3 in[] 15-745: Intro to Data Flow 13 f Gen Kill 1 {1,2 {3,4,6 2 {3,4 {1,2,6 3 {5,6 {1,3 Cyclic Graphs out[] d1: a = 10 in[2] if e d2: a = 11 in[] Equations still hold out[b] = f b (in[b]) in[b] = out[p 1 ] U out[p 2 ] U... U out[p n ], p 1,..., p n pred. Find: fixed point solution 15-745: Intro to Data Flow 14 Reaching Definitions: Iterative Algorithm input: control flow graph CFG = (N, E, Entry, Exit) // Boundary condition out[entry] = // Initialization for iterative algorithm For each basic block B other than Entry out[b] = // iterate While (Changes to any out[] occur) { For each basic block B other than Entry { in[b] = (out[p]), for all predecessors p of B out[b] = f B (in[b]) // out[b]=gen[b] (in[b]-kill[b]) Reaching Definitions: Worklist Algorithm input: control flow graph CFG = (N, E, Entry, Exit) // Initialize out[entry] = For all nodes i out[i] = ChangedNodes = N // can set out[entry] to special def // if reaching then undefined use // can optimize by out[i]=gen[i] // iterate While ChangedNodes { Remove i from ChangedNodes in[i] = U (out[p]), for all predecessors p of i oldout = out[i] out[i] = f i (in[i]) // out[i]=gen[i]u(in[i]-kill[i]) if (oldout out[i]) { for all successors s of i add s to ChangedNodes 15-745: Intro to Data Flow 15 15-745: Intro to Data Flow 16 4

Example III. Live Variable Analysis B3 d6: a = u2 B4 d1: i = m-1 d2: j = n d3: a = u1 d4: i = i+1 d5: j = j-1 d7: i = u3 Definition A variable v is live at point p if the value of v is used along some path in the flow graph starting at p. Otherwise, the variable is dead. d Motivation e.g. register allocation for i = 0 to n i for i = 0 to n i Problem statement For each basic block determine if each variable is live in each basic block Size of bit vector: one bit for each variable 15-745: Intro to Data Flow 17 15-745: Intro to Data Flow 18 Effects of a Basic Block (Transfer Function) Insight: Trace uses backwards to the definitions an execution path control flow example def IN[b] = f d3: a = 1 b (OUT[b]) d4: b = 1 def b f b OUT[b] d5: c = a use d6: a = 4 in[2] out[] Flow Graph f 2 f 3 f Use Def 1 {e {a,b 2 { {a,b 3 {a {a,c A basic block b can generate live variables: Use[b] set of locally ll exposed uses in b propagate incoming live variables: OUT[b] - Def[b], where Def[b]= set of variables defined in b.b. transfer function for block b: in[b] = Use[b] U (out(b)-def[b]) in[] in[b] = f b (out[b]) Join node: a node with multiple successors meet operator: out[b] = in[s 1 ] U in[s 2 ] U... U in[s n ], where s 1,..., s n are all successors of b 15-745: Intro to Data Flow 19 15-745: Intro to Data Flow 20 5

Liveness: Iterative Algorithm input: control flow graph CFG = (N, E, Entry, Exit) Example // Boundary condition in[exit] = // Initialization for iterative algorithm For each basic block B other than Exit in[b] = d1: i = m-1 d2: j = n d3: a = u1 d4: i = i+1 d5: j = j-1 // iterate While (Changes to any in[] occur) { For each basic block B other than Exit { out[b] = (in[s]), for all successors s of B in[b] = f B (out[b]) // in[b]=use[b] (out[b]-def[b]) B3 d6: a = u2 B4 d7: i = u3 15-745: Intro to Data Flow 21 15-745: Intro to Data Flow 22 IV. Framework Reaching Definitions Live Variables Domain Sets of definitions Sets of variables Direction forward: backward: out[b] = f b (in[b]) in[b] = out[pred(b)] in[b] = f b (out[b]) out[b] = in[succ(b)] Transfer function f b (x) = Gen b (x Kill b ) f b (x) = Use b (x -Def b ) Meet Operation ( ) Boundary Condition out[] = in[] = Initial interior points out[b] = in[b] = Thought Problem 1. Must-Reach Definitions A definition D (a = b+c) must reach point P iff D appears at least once along on all paths leading to P a is not redefined along any path after last appearance of D and before P How do we formulate the data flow algorithm for this problem? 15-745: Intro to Data Flow 23 15-745: Intro to Data Flow 24 6

Problem 2: A legal solution to (May) Reaching Def? Questions d1: b = 1 out[]={ ={ ={ in[2]={d1 ={d1 ={d1 ={d1 in[] Correctness equations are satisfied, if the program terminates. Precision: how good is the answer? is the answer ONLY a union of all possible executions? Convergence: will the analysis terminate? or, will there always be some nodes that change? Speed: how fast is the convergence? how many times will we visit each node? Will the worklist algorithm generate this answer? 15-745: Intro to Data Flow 25 15-745: Intro to Data Flow 26 7