Dataflow Analysis CS 6340. What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Lecture 11: Code Optimization CS 540 George Mason University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
Partial Redundancy Elimination. Partial-Redundancy Elimination Minimize the number of expression evaluations By moving around the places where an expression.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Lecture 15 – Dataflow Analysis Eran Yahav 1
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 More data flow analysis Emery Berger.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
CS 536 Spring Global Optimizations Lecture 23.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Chair of Software Engineering Fundamentals of Program Analysis Dr. Manuel Oriol.
Data Flow Analysis Compiler Design Nov. 3, 2005.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Annotating Abstract Syntax Tree Professor Yihjia Tsai Tamkang University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Data Flow Analysis Compiler Design Nov. 8, 2005.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Prof. Aiken CS 294 Lecture 11 Program Analysis. Prof. Aiken CS 294 Lecture 12 The Purpose of this Course How are the following related? –Program analysis.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
1 CS 201 Compiler Construction Data Flow Analysis.
1 Data-Flow Analysis Proving Little Theorems Data-Flow Equations Major Examples.
Data-Flow Analysis. Approaches Static Analysis Inspections Dependence analysis Symbolic execution Software Verification Data flow analysis Concurrency.
Data Flow Analysis. 2 Source code parsed to produce AST AST transformed to CFG Data flow analysis operates on control flow graph (and other intermediate.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
Compiler Principles Winter Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.
Jeffrey D. Ullman Stanford University. 2 boolean x = true; while (x) {... // no change to x }  Doesn’t terminate.  Proof: only assignment to x is at.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Compilation Lecture 8 Abstract Interpretation Noam Rinetzky 1.
Compiler Principles Fall Compiler Principles Lecture 11: Loop Optimizations Roman Manevich Ben-Gurion University.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
Iterative Dataflow Problems Taken largely from notes of Alex Aiken (UC Berkeley) and Martin Rinard (MIT) Dataflow information used in optimization Several.
DFA foundations Simone Campanoni
COMPILERS Liveness Analysis hussein suleman uct csc3003s 2009.
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Iterative Dataflow Problems
Dataflow Analysis Mayur Naik CIS 700 – Fall 2017 {HEADSHOT}
Topic 10: Dataflow Analysis
University Of Virginia
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Static Single Assignment Form (SSA)
Fall Compiler Principles Lecture 10: Global Optimizations
Data Flow Analysis Compiler Design
Static Single Assignment
Dataflow Analysis, cont.
Flow of Control.
Live variables and copy propagation
COMPILERS Liveness Analysis
Presentation transcript:

Dataflow Analysis CS 6340

What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables, expressions Used by bug-finding tools and compilers

x = 5; y = 1; while (x != 1) { y = x * y; x = x - 1 } The WHILE Language (statement) S ::= x = a | S1 ; S2 | if (b) { S1 } else { S2 } | while (b) { S1 } (arithmetic expression) a ::= x | n | a1 * a2 | a1 - a2 (boolean expression) b ::= true | !b | b1 && b2 | a1 != a2 (integer variable) x (integer constant) n

Control-Flow Graphs x = 5; y = 1; while (x != 1) { y = x * y; x = x - 1 } x = 5 (x != 1) ? exit y=x*y entry y = 1 x=x-1 true false

QUIZ: Control-Flow Graphs x = 5 exit x = x - 1 y = y -1 entry y = x x != 0 y != 0? false true false true

x = 5; while (x != 0) { y = x; x = x - 1; while (y != 0) { y = y - 1 } } QUIZ: Control-Flow Graphs x = 5 exit x = x - 1 y = y -1 entry y = x x != 0 y != 0? false true false true

Soundness, Completeness, Termination Impossible for analysis to achieve all three together Dataflow analysis sacrifices completeness Sound: Will report all facts that could occur in actual runs Incomplete: May report additional facts that can’t occur in actual runs

Abstracting Control-Flow Conditions Abstracts away control-flow conditions with non-deterministic choice (*) Non-deterministic choice => assumes condition can evaluate to true or false Considers all paths possible in actual runs (sound), and maybe paths that are never possible (incomplete). x = 5 (x != 1) ? exit x=x-1 y=x*y entry y = 1 true false

Applications of Dataflow Analysis Reaching Definitions Analysis Find usage of uninitialized variables Available Expressions Analysis Avoid recomputing expressions Very Busy Expressions Analysis Reduce code size Live Variables Analysis Allocate registers efficiently

Reaching Definitions Analysis Goal: Determine, for each program point, which assignments have been made and not overwritten, when execution reaches that point along some path “Assignment” == “Definition” P2 P1 x = y (x != 1) ? exit y=x*y entry y = 1 x=x-1 true false

QUIZ: Reaching Definitions Analysis 1.The assignment y = 1 reaches P1 2.The assignment y = 1 reaches P2 3.The assignment y = x * y reaches P1 P2 P1 x = y (x != 1) ? exit y=x*y entry y = 1 x=x-1 true false

QUIZ: Reaching Definitions Analysis 1.The assignment y = 1 reaches P1 2.The assignment y = 1 reaches P2 3.The assignment y = x * y reaches P1 P2 P1 x = y (x != 1) ? exit y=x*y entry y = 1 x=x-1 true false

Result of Dataflow Analysis (Informally) Set of facts at each program point For reaching definitions analysis, fact is a pair of the form: Examples:, x = y exit x=x-1 y=x*y entry y = 1 true false 2: 1: 3: 4: 7: 5: 6: (x != 1) ?

Result of Dataflow Analysis (Formally) Give distinct label n to each node IN[n] = set of facts at entry of node n OUT[n] = set of facts at exit of node n Dataflow analysis computes IN[n] and OUT[n] for each node Repeat two operations until IN[n] and OUT[n] stop changing –Called “saturated” or “fixed point” x = y exit x=x-1 y=x*y entry y = 1 2: 1: 3: 4: 5: 6: 7: true false (x != 1) ?

Reaching Definitions Analysis: Operation #1 IN[n] = OUT[n’] n’ ∈ predecessors(n) n n1n2 ∪ n3 IN[n] = OUT[n1] ∪ OUT[n2] ∪ OUT[n3]

GEN[n] = ∅ KILL[n] = ∅ n: GEN[n] = { } KILL[n] = { : m != n } OUT[n] = (IN[n] - KILL[n]) ∪ GEN[n] x = a b ? OUT[n] IN[n] Reaching Definitions Analysis: Operation #2

Overall Algorithm: Chaotic Iteration for (each node n): IN[n] = OUT[n] = ∅ OUT[entry] = { : v is a program variable } repeat: for (each node n): IN[n] = OUT[n’] OUT[n] = (IN[n] - KILL[n]) ∪ GEN[n] until IN[n] and OUT[n] stop changing for all n n’ ∈ predecessors(n) ∪

Reaching Definitions Analysis Example nIN[n]OUT[n] 1--{, } 2 ∅ ∅ 3 ∅ ∅ 4 ∅ ∅ 5 ∅ ∅ 6 ∅ ∅ 7 ∅ -- x = y (x != 1) ? exit y=x*y entry y = 1 2: 1: 3: 7: 4: x=x-1 5: 6: true false

Reaching Definitions Analysis Example nIN[n]OUT[n] 1--{, } ∅ ∅ 5 ∅ ∅ 6 ∅ ∅ 7 ∅ -- x = y (x != 1) ? exit y=x*y entry y = 1 2: 1: 3: 7: 4: x=x-1 5: 6: true false

QUIZ: Reaching Definitions Analysis nIN[n]OUT[n] 1--{, } x = y (x != 1) ? exit y=x*y entry y = 1 2: 1: 3: 7: 4: x=x-1 5: 6: true false

QUIZ: Reaching Definitions Analysis nIN[n]OUT[n] 1--{, } 2 3 4{,,, } 5 {,, } 6 {, } 7{,,, } -- x = y (x != 1) ? exit y=x*y entry y = 1 2: 1: 3: 7: 4: x=x-1 5: 6: true false

Does It Always Terminate? Chaotic Iteration algorithm always terminates The two operations of reaching definitions analysis are monotonic => IN and OUT sets never shrink, only grow Largest they can be is set of all definitions in program, which is finite => IN and OUT cannot grow forever => IN and OUT will stop changing after some iteration

Very Busy Expressions Analysis Goal: Determine very busy expressions at the exit from the point. a=0 y=b-a (a!=b) ? exit x=b-a entry y=a-b x=a-b P true false An expression is very busy if, no matter what path is taken, the expression is used before any of the variables occurring in it are redefined

Very Busy Expressions Analysis: Operation #1 OUT[n] = IN[n’] n’ ∈ successors(n) n n1n2 ∪ n3 OUT[n] = IN[n1] ∩ IN[n2] ∩ IN[n3]

Very Busy Expressions Analysis: Operation #2 GEN[n] = ∅ KILL[n] = ∅ n: GEN[n] = { a } KILL[n] = { expr e : e contains x } IN[n] = (OUT[n] - KILL[n]) ∪ GEN[n] x = a b ? IN[n] OUT[n]

Overall Algorithm: Chaotic Iteration for (each node n) IN[n] = OUT[n] = set of all exprs in program IN[exit] = ∅ repeat: for (each node n) OUT[n] = IN[n’] IN[n] = (OUT[n] - KILL[n]) ∪ GEN[n] until IN[n] and OUT[n] stop changing for all n n’ ∈ successors(n) ∪

Very Busy Expressions Analysis Example nIN[n]OUT[n] 1--{ b-a, a-b } ∅ -- a=0 y=b-a (a!=b) ? exit x=b-a entry y=a-b x=a-b 1: 2: 3: 6: 4: 5: 7: 8: true false

nIN[n]OUT[n] 1--{ b-a, a-b } { a-b } ∅ 7 ∅ 8 ∅ -- Very Busy Expressions Analysis Example a=0 y=b-a (a!=b) ? exit x=b-a entry y=a-b x=a-b 1: 2: 3: 6: 4: 5: 7: 8: true false

QUIZ: Very Busy Expressions Analysis nIN[n]OUT[n] ∅ { a-b } 6 ∅ 7 ∅ 8 ∅ -- a=0 y=b-a (a!=b) ? exit x=b-a entry y=a-b x=a-b 1: 2: 3: 6: 4: 5: 7: 8: true false

QUIZ: Very Busy Expressions Analysis nIN[n]OUT[n] 1--{ b-a } 2 3{ b-a, a-b }{ a-b } 4{ b-a } ∅ 5 ∅ { a-b } 6 ∅ 7 ∅ 8 ∅ -- a=0 y=b-a (a!=b) ? exit x=b-a entry y=a-b x=a-b 1: 2: 3: 6: 4: 5: 7: 8: true false

Available Expressions Analysis Goal: Determine, for each program point, which expressions must already have been computed, and not later modified, on all paths to the program point. P y = a*b x = a-b (y != a-b)? exit x=a-b entry a=a-1 true false

Available Expressions Analysis nIN[n]OUT[n] 1-- ∅ 2{a-b, a*b, a-1} y = a*b x = a-b (y != a-b)? x=a-b entry a=a-1 1: 2: 3: 4: 5: 6: exit 7: true false

Available Expressions Analysis nIN[n]OUT[n] 1-- ∅ 2 ∅ {a-b} 3 {a-b, a*b} 4 5 ∅ 6 ∅ {a-b} 7{a-b, a*b}-- y = a*b x = a-b (y != a-b)? x=a-b entry a=a-1 1: 2: 3: 4: 5: 6: exit 7: true false

Available Expressions Analysis y = a*b x = a-b (y != a-b)? x=a-b entry a=a-1 1: 2: 3: 4: 5: 6: true false exit 7: nIN[n]OUT[n] 1-- ∅ 2 ∅ {a-b} 3 {a-b, a*b} 4{a-b} 5 ∅ 6 ∅ 7 --

Live Variables Analysis Goal: Determine for each program point which variables could be live at the point’s exit P x = 2 y = 4 (y!=x) ? exit x=z z=y entry z = y*y true false A variable is live if there is a path to a use of the variable that doesn’t redefine the variable

Live Variables Analysis nIN[n]OUT[n] 1-- ∅ 2 ∅ ∅ 3 ∅ ∅ 4 ∅ ∅ 5 ∅ ∅ 6 ∅ ∅ 7 ∅ ∅ 8 ∅ x = 2 y = 4 (y!=x) ? exit x=z z=y entry z = y*y 1: 2: 3: 4: 5: 6: 7: 8: true false

Live Variables Analysis nIN[n]OUT[n] 1-- ∅ 2 ∅ { y } 3 { x, y } 4 { y } 5 { z } 6{ y }{ z } 7 ∅ 8 ∅ -- x = 2 y = 4 (y!=x) ? exit x=z z=y entry z = y*y 1: 2: 3: 4: 5: 6: 7: 8: true false

Overall Pattern of Dataflow Analysis n’ ∈ (n) [n] = ( [n] - KILL[n]) ∪ GEN[n] [n] = [n’] = IN or OUT = predecessors or successors = ∪ (may) or ∩ (must)

Reaching Definitions Analysis [n] = ( [n] - KILL[n]) ∪ GEN[n] [n] = [n’] OU T IN OU T ∪ = predecessors or successors = IN or OUT n’ ∈ (n) preds = ∪ (may) or ∩ (must)

Very Busy Expression Analysis n’ ∈ (n) [n] = ( [n] - KILL[n]) ∪ GEN[n] [n] = [n’] IN OU T succs IN ∩ = predecessors or successors = IN or OUT = ∪ (may) or ∩ (must)

QUIZ: Available Expressions Analysis [n] = ( [n] - KILL[n]) ∪ GEN[n] [n] = [n’] = IN or OUT = predecessors or successors = ∪ (may) or ∩ (must)

QUIZ: Available Expressions Analysis n’ ∈ (n) [n] = ( [n] - KILL[n]) ∪ GEN[n] [n] = [n’] OU T IN preds OU T ∩ = IN or OUT = predecessors or successors = ∪ (may) or ∩ (must)

QUIZ: Live Variables Analysis [n] = ( [n] - KILL[n]) ∪ GEN[n] [n] = [n’] = IN or OUT n’ ∈ (n) = predecessors or successors = ∪ (may) or ∩ (must)

QUIZ: Live Variables Analysis [n] = ( [n] - KILL[n]) ∪ GEN[n] [n] = [n’] IN OU T IN = IN or OUT n’ ∈ (n) ∪ succs = predecessors or successors = ∪ (may) or ∩ (must)

Reaching Definitions QUIZ: Classifying Dataflow Analyses MayMust Forward Backward Very Busy Expressions Live Variables Available Expressions Match each analysis with its characteristics.

QUIZ: Classifying Dataflow Analyses MayMust Forward Reaching Definitions Available Expressions BackwardLive VariablesVery Busy Expressions Match each analysis with its characteristics.

What Have We Learned? What is dataflow analysis Reasoning about flow of data using control-flow graphs Specifying dataflow analyses using local rules Chaotic iteration algorithm to compute global properties Four classical dataflow analyses Classification: forward vs. backward, may vs. must