Topic 10: Dataflow Analysis

Slides:



Advertisements
Similar presentations
SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
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.
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.
Program Representations. Representing programs Goals.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
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.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
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.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
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...
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.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Precision Going back to constant prop, in what cases would we lose precision?
1 CS 201 Compiler Construction Data Flow Analysis.
CSE P501 – Compiler Construction
Data Flow Analysis Compiler Baojian Hua
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.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
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.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Optimization Simone Campanoni
Dataflow Analysis CS What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,
COMPILERS Liveness Analysis hussein suleman uct csc3003s 2009.
Simone Campanoni SSA Simone Campanoni
Data Flow Analysis Suman Jana
Princeton University Spring 2016
Princeton University Spring 2016
Fall Compiler Principles Lecture 8: Loop Optimizations
Code Generation Part III
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.
Code Generation Part III
Static Single Assignment Form (SSA)
EECS 583 – Class 6 More Dataflow Analysis
Optimizations using SSA
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Static Single Assignment
EECS 583 – Class 7 Static Single Assignment Form
Live variables and copy propagation
COMPILERS Liveness Analysis
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Presentation transcript:

Topic 10: Dataflow Analysis COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer

Analysis and Transformation analysis spans multiple procedures single-procedure-analysis: intra-procedural

Dataflow Analysis Motivation

Dataflow Analysis Motivation r2 r3 r4 Assuming only r5 is live-out at instruction 4...

Dataflow Analysis

Iterative Dataflow Analysis Framework

Definitions

Iterative Dataflow Analysis Framework

Definitions for Liveness Analysis

Definitions for Liveness Analysis Remember generic equations:

Smart ordering: visit nodes in reverse order of execution. -- r1 r1 r2 r2, r3 r3 r2 r1 r1 -- r3 -- -- Smart ordering: visit nodes in reverse order of execution.

Smart ordering: visit nodes in reverse order of execution. -- r1 r1 r2 r2, r3 r3 ? r2 r1 r3, r1 r1 -- r3 r3, r1 r3 -- -- r3 Smart ordering: visit nodes in reverse order of execution.

Smart ordering: visit nodes in reverse order of execution. -- r1 r3, r1 r3 r1 r2 r3, r2 r3, r1 r2, r3 r3 r3, r2 r3, r2 ? r2 r1 r3, r1 r3, r2 r1 -- r3 r3, r1 r3 -- -- r3 Smart ordering: visit nodes in reverse order of execution.

Smart ordering: visit nodes in reverse order of execution. -- r1 r3, r1 r3 r1 r2 r3, r2 r3, r1 : r2, r3 r3 r3, r2 r3, r2 : r2 r1 r3, r1 r3, r2 r1 -- r3 r3, r1 r3, r1 r3 -- -- r3 -- r3 Smart ordering: visit nodes in reverse order of execution.

Live Variable Application 1: Register Allocation

Interference Graph r1 r3 ? r2

Interference Graph r1 r3 r2

Live Variable Application 2: Dead Code Elimination of the form of the form

Live Variable Application 2: Dead Code Elimination of the form This may lead to further optimization opportunities, as uses of variables in s disappear.  repeat all / some analysis / optimization passes! of the form

Reaching Definition Analysis

Reaching Definition Analysis (details on next slide)

Reaching definitions: definition-ID’s give each definition point a label (“definition ID”) d: associate each variable t with a set of labels: defs(t) = the labels d associated with a def of t Consider the effects of instruction forms on gen/kill d: t b op c d: t M[b] d: t f(a_1, …, a_n) Statement s Gen[s] Kill[s] d: t <- b op c {d} defs(t) – {d} d: t <- M[b] M[a] <- b {} if a relop b goto l1 else goto L2 Goto L L: f(a_1, …, a_n) d: t <- f(a_1, …, a_n) defs(t)-{d}

Reaching Defs: Example 3: L1: if c > a goto L2 6: L2: a <- c-a 4: c <- c+c 7: c <- 0 5: goto L1 defs(a) = ? defs(c) = ?

Reaching Defs: Example 3: L1: if c > a goto L2 6: L2: a <- c-a 4: c <- c+c 7: c <- 0 5: goto L1 ? defs(a) = {1, 6} defs(c) = {2, 4, 7}

Reaching Defs: Example 3: L1: if c > a goto L2 6: L2: a <- c-a 4: c <- c+c 7: c <- 0 5: goto L1 1 2 4 6 7 ? defs(a) = {1, 6} defs(c) = {2, 4, 7}

Reaching Defs: Example 3: L1: if c > a goto L2 6: L2: a <- c-a 4: c <- c+c 7: c <- 0 5: goto L1 1 2 4 6 7 6 4, 7 2, 7 1 2, 4 defs(a)-{1} defs(c)-{2} defs(c)-{4} defs(a)-{6} defs(c)-{7} defs(a) = {1, 6} defs(c) = {2, 4, 7}

Reaching Defs: Example 3: L1: if c > a goto L2 6: L2: a <- c-a 4: c <- c+c 7: c <- 0 5: goto L1 1 2 4 6 7 6 4, 7 2, 7 1 2, 4 ? Direction: FORWARD, IN/OUT initially {}

Reaching Defs: Example 3: L1: if c > a goto L2 6: L2: a <- c-a 4: c <- c+c 7: c <- 0 5: goto L1 1 2 4 6 7 6 4, 7 2, 7 1 2, 4 { } {1} {1} {1, 2}

Reaching Defs: Example 3: L1: if c > a goto L2 6: L2: a <- c-a 4: c <- c+c 7: c <- 0 5: goto L1 1 2 4 6 7 6 4, 7 2, 7 1 2, 4 { } {1} {1} {1, 2} {1,2} {1, 2} {1,2} {1, 4}

Reaching Defs: Example 3: L1: if c > a goto L2 6: L2: a <- c-a 4: c <- c+c 7: c <- 0 5: goto L1 1 2 4 6 7 6 4, 7 2, 7 1 2, 4 { } {1} {1} {1, 2} {1} {1, 2} {1,2} {1, 4} {1,4} {1, 4} {1,2} {2,6} {2,6} {6,7}

Reaching Defs: Example 3: L1: if c > a goto L2 6: L2: a <- c-a 4: c <- c+c 7: c <- 0 5: goto L1 1 2 4 6 7 6 4, 7 2, 7 1 2, 4 { } {1} { } {1} {1} {1, 2} {1} {1, 2} {1,2} {1, 2} {1,2,4} {1, 2,4} {1,2} {1, 4} {1,2,4} {1, 4} No change {1,4} {1, 4} {1,4} {1, 4} {1,2} {2,6} {1,2,4} {2,4,6} {2,6} {6,7} {2,4,6} {6,7}

Reaching Definition Application 1: Constant Propagation 5 5 10 5 Similarly: copy propagation, replace eg r1 = r2; r3  r1 + 5 with r3  r2 + 5 But often register allocation can already coalesce r1 and r2.

Reaching Definition Application 2: Constant Folding 15

Common Subexpression Elimination CSE

Common Subexpression Elimination CSE

Common Subexpression Elimination CSE Copy Prop.

Definitions entry e r1 = x op y r2 = x op y x:=66 r3 = x op y e must be computed at least once on any path e r1 = x op y r2 = x op y x:=66 no defs of registers used by e here! r3 = x op y Generally, many expressions are available at any program point e available here!

Definitions (ie statements generate/kill availability) entry e e must be computed at least once on any path e r1 = x op y r2 = x op y x:=66 no defs of registers used by e here! r3 = x op y Generally, many expressions are available at any program point e available here!

Definitions (ie statements generate/kill availability) entry e e must be computed at least once on any path e r1 = x op y r2 = x op y x:=66 Statement M[r2] = e - generates no expression (we do availability in register here) - kills any M[ .. ] expression, ie loads no defs of registers used by e here! r3 = x op y Generally, many expressions are available at any program point e available here!

Iterative Dataflow Analysis Framework (forward) OK? expressions containing t Statement s Gen(s) Kill(s) t  b op c {b op c} exp(t)

Iterative Dataflow Analysis Framework (forward) to deal with t = b or t = c expressions containing t Statement s Gen(s) Kill(s) t  b op c {b op c} – kill(s) exp(t) t  M[b] {M[b]} – kill(s) M[a]  b {} “fetches” if a rop b goto L1 else goto L2 Goto L L: f(a1,..,an) ? t  f(a1, .., an) expressions of the form M[ _ ]

Iterative Dataflow Analysis Framework (forward) to deal with t = b or t = c expressions containing t Statement s Gen(s) Kill(s) t  b op c {b op c} – kill(s) exp(t) t  M[b] {M[b]} – kill(s) M[a]  b {} “fetches” if a rop b goto L1 else goto L2 Goto L L: f(a1,..,an) t  f(a1, .., an) exp(t) “fetches” expressions of the form M[ _ ] ∩

Available Expression Analysis only expressions that are out-available at all predecessors of n are in-available at n fact that sets shrink triggers initialization of all sets to U (set of all expressions), except for IN[entry] = {}

Step 1: fill in Kill[n] Node n Gen[n] Kill[n] 1 2 3 4 5 6 7 8 9 no singleton registers (r4 etc) no Boolean exprs (r3>r2) Universe U of expressions: M[r5], M[A], M[B], r1+r2, r1+ 12, r3+r1 exp(r5) “fetches” exp(r2) exp(r1) exp(r3)

Step 2: fill in Gen[n] Node n Gen[n] Kill[n] 1 r1+r2, r1+ 12, r3+r1 2 r1+r2 3 r3+r1 4 - 5 6 7 8 M[r5] 9 M[r5], M[A], M[B] no singleton registers (r4 etc) no Boolean exprs (r3>r2) Universe U of expressions: M[r5], M[A], M[B], r1+r2, r1+ 12, r3+r1 exp(r5) “fetches” exp(r2) exp(r1) exp(r3)

Example Node n Gen[n] Kill[n] 1 M[A] r1+r2, r1+ 12, r3+r1 2 M[B] r1+r2 3 r3+r1 4 - 5 6 7 8 M[r5] 9 M[r5], M[A], M[B] no singleton registers (r4 etc) no Boolean exprs (r3>r2) Universe U of expressions: M[r5], M[A], M[B], r1+r2, r1+ 12, r3+r1 exp(r5) “fetches” exp(r2) exp(r1) exp(r3)

Example: hash expressions Gen[n] Kill[n] 1 M[A] r1+r2, r1+ 12, r3+r1 2 M[B] r1+r2 3 r3+r1 4 - 5 6 7 8 M[r5] 9 M[r5], M[A], M[B] n Gen[n] Kill[n] 1 5 1, 2, 3 2 6 3 4 - 7 8 9 4, 5, 6 r1+r2 1 r1+12 2 r3+r1 3 M[r5] 4 M[A] 5 M[B] 6

: : : U = {1, .., 6} Step 3: DF iteration FORWARD In Out In[n] Out[n] {} U : : : U = {1, .., 6}

U = {1, .., 6} Example FORWARD In Out In[n] Out[n] {} U 5 5,6 5, 6 1, 5, 6 1, 3, 5, 6 1 U = {1, .., 6}

Example r1+r2 1 r1+12 2 r3+r1 3 M[r5] 4 M[A] 5 M[B] 6 In[n] Out[n] {} r1+r2, M[A], M[B] r1+r2, r3+r1, M[A], M[B] r1+r2 r1+r2 1 r1+12 2 r3+r1 3 M[r5] 4 M[A] 5 M[B] 6 In[n] Out[n] {} 5 5, 6 1, 5, 6 1, 3, 5, 6 1

Common Subexpression Elimination (CSE) Can be seen as further analysis: “reaching expressions” Note that the same w is used for all occurrences of x op y: s1: v = x op y s2: u = x op y s1’: w = x op y v = w s2’: w = x op y u=w s: t = x op y s: t = w

CSE Example w ; r3 = w w ; r4 = w w

Copy Propagation

Copy Propagation r4 = r99 + r1 M[r99] = r4

Sets

Basic Block Level Analysis defs uses

Basic Block Level Analysis

Basic Block Level Analysis = = IN[pn] OUT[pn]

Reducible Flow Graphs Revisited irreducible reducible Not a backedge – dest does not dominate src

Reducible Flow Graphs – Structured Programs Subgraph H of CFG, and nodes u є H, v є H such that all edges into H go to u, all edges out of H go to v

Reaching Definitions for Structured Programs Remember: p n l r

Conservative Approximations

Limitation of Dataflow Analysis There are more sophisticated program analyses that can (conservatively) approximate the ranges/sets of “possible values” fit into a general framework of transfer functions and inference by iterated updates until a fixed point is reached: “abstract interpretation” are very useful for eliminating dead branches, showing that array accesses are always within range,… But eventually, they run into the same theoretical limitations Other example:

Implementation issues Representation of data flow info (sets of variables, expressions, labels, …) linked lists, maybe ordered by variable name suitable for sparse analyses (typically, only few variables are live at a program point … ) bit-vectors of length N, if set is of size < 2^N union, intersection implemented by bit-wise OR/AND suitable for dense analyses Speeding up iterations: worklist algorithms instead of traversing all nodes in each iteration, just revisit those nodes for which IN/OUT might change FORWARD: after visiting a node, if OUT[n] was modified, ensure that all successors of n are in the queue (insert if necessary) BACKWARD: similarly, add predecessors of n if IN[n] has changed “single-information-at-a-time” versus “exhaustive” information: “is the (costly-to-compute) expression e available here” versus “give me all available expressions, at all program points”

Use-def chains, def-use chains many optimizations exploit def-use relationship avoid recalculation by introducing a data structure Use-def chain: for each use of a variable store the set of reaching definitions Def-use chain: for each definition d of a variable store all its uses x = … y = ..x.. … = ..x.. … = ..y.. i1 i2 i3 i4 i5 i6 Var Use Defs x i2 i1 i4 i1, i3 i5 y i6 Var Def Uses x i1 i2, i4, i5 i3 i4, i5 y i5 I6 Generalization: static single-assignment (SSA) form – see future lecture.