Docsity

Dataflow Analysis: Understanding Program Behavior through Static Code Examination - Prof. , Assignments of Computer Science

Toggle sidebar
bg1
Dataflow Analysis
Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 Dataflow Analysis 1 / 36
Dataflow Analysis
Dataflow analysis derives information about the dynamic behavior of a
program by only examining the static code. More specifically, it analyzes
the dynamic information flow using a program’s control flow graph.
Example: How many registers do we need for this program?
1 a := 0
2 L1: b := a + 1
3 c := c + b
4 a := b * 2
5 if a < N goto L1
6 return c
•An easy upper bound is the number of variables used, 3.
•With dataflow analysis, we can obtain the dynamic liveness
information of all the variables, and conclude that variables aand b
are never alive simultaneously, hence can be assigned to the same
register.
Jingke Li (Portland State University) CS322 Dataflow Analysis 2 / 36
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Dataflow Analysis: Understanding Program Behavior through Static Code Examination - Prof. and more Assignments Computer Science in PDF only on Docsity!

Dataflow Analysis

Jingke Li

Portland State University

Jingke Li (Portland State University) CS322 Dataflow Analysis 1 / 36

Dataflow Analysis

Dataflow analysis derives information about the dynamic behavior of a program by only examining the static code. More specifically, it analyzes the dynamic information flow using a program’s control flow graph.

Example: How many registers do we need for this program?

1 a := 0 2 L1: b := a + 1 3 c := c + b 4 a := b * 2 5 if a < N goto L 6 return c

  • An easy upper bound is the number of variables used, 3.
  • (^) With dataflow analysis, we can obtain the dynamic liveness information of all the variables, and conclude that variables a and b are never alive simultaneously, hence can be assigned to the same register.

CFG-Related Concepts

d d 1 : i := m- d^23 ::^ j := na := u

d d 45 :: i := i+1j := j- if e1 goto B

d 6 : a := u2 d 7 : i := u while e2 goto B

B

B

B3 B B

  • Points — any location in the graph. For a basic block, we can talk about entry (in) point and exit (out) point.
  • Paths — between two points.
  • (^) Loops — (Recall the definition) A set of nodes S, with a header h:
    • no edge from any node outside S to any node in S other than h;
    • there is a path of directed edges from h to any node in S;
    • from any node in S, there is a path of directed edges leading to h.

Jingke Li (Portland State University) CS322 Dataflow Analysis 3 / 36

Dataflow Analysis Framework

For a given problem, typical dataflow analysis steps are

  1. Define information sets to represent dataflow information relevant to the problem;
  2. Set up a system of equations that relate the information at various points in the program;
  3. Solve the system of equations.

Generic Information Sets:

Gen[n] — information originated at node n Kill[n] — information invalidated at node n In[n] — information passed in to node n Out[n] — information ready to be passed out from node n

Solving Dataflow Equations

for (each node B in CFG) in[B] = φ out[B] = φ gen[B] = the local gen set for B kill[B] = the local kill set for B repeat for (each node B in CFG) in’[B] = in[B] out’[B] = out[B] in[B] =

s ∈ pred[B] out[s]

out[B] = gen[B]

(out[B] - kill[B]) until (in’[B] == in[B] and out’[B] == out[B] for all B)

Jingke Li (Portland State University) CS322 Dataflow Analysis 7 / 36

Case Study: Reaching Definitions

In some optimization situations, we need to estimate the possible values for variable x at a given point p in program. For instance, if we know that the only definition of x that reaches point p is an assignment x := 1, we may infer that x has value 1 at that point.

A definition d (i.e. an assignment statement) reaches point p if there is a path from d to p, such that d is not “killed” along the path (i.e. the variable being defined in d is not re-defined).

Reaching Definitions (cont.)

Dataflow Equations (Block-Level):

in[B] =

P∈pred[B] out[P] out[B] = gen[B]

(in[B] − kill[B])

in[B] — definitions that reach the beginning of B gen[B] — definitions appeared inside B that reach the end of B kill[B] — definitions that have been over-shadowed by new definitions in B out[B] — definitions that reach the end of B

Jingke Li (Portland State University) CS322 Dataflow Analysis 9 / 36

A Reaching-Def Example

d1: i := m-1; d2: j := n; d3: a := u1; do d4: i := i+1; d5: j := j-1; if e1 then d6: a := u else d7: i := u while e

d d 1 : i := m- d^23 ::^ j := na := u

d d 45 :: i := i+1j := j- if e1 goto B d 6 : a := u2 d 7 : i := u

while e2 goto B

B

B

B3 B B

gen[B 1 ] = {d 1 , d 2 , d 3 } gen[B 2 ] = {d 4 , d 5 } gen[B 3 ] = {d 6 } gen[B 4 ] = {d 7 } gen[B 5 ] = {}

kill[B 1 ] = {d 4 , d 5 , d 6 , d 7 } kill[B 2 ] = {d 1 , d 2 , d 7 } kill[B 3 ] = {d 3 } kill[B 4 ] = {d 1 , d 4 } kill[B 5 ] = {}

A Reaching-Def Example (cont.)

  • (^) Iteration 4: in[B 2 ] = out[B 1 ]

S out[B 5 ] = {d 1 , d 2 , d 3 , d 4 , d 5 , d 6 , d 7 } in[B 3 ] = out[B 2 ] = {d 3 , d 4 , d 5 , d 6 } in[B 4 ] = out[B 2 ] = {d 3 , d 4 , d 5 , d 6 } in[B 5 ] = out[B 3 ]

S out[B 4 ] = {d 3 , d 4 , d 5 , d 6 , d 7 } out[B 2 ] = gen[B 2 ]

S (in[B 2 ] − kill[B 2 ]) = {d 3 , d 4 , d 5 , d 6 } out[B 3 ] = gen[B 3 ] S^ (in[B 3 ] − kill[B 3 ]) = {d 4 , d 5 , d 6 } out[B 4 ] = gen[B 4 ]

S (in[B 4 ] − kill[B 4 ]) = {d 3 , d 5 , d 6 , d 7 } out[B 5 ] = gen[B 5 ] S^ (in[B 5 ] − kill[B 5 ]) = {d 3 , d 4 , d 5 , d 6 , d 7 }

It can be verified that this is the fixed point solution.

Jingke Li (Portland State University) CS322 Dataflow Analysis 13 / 36

Case Study: Available Expressions

An expression x + y is available at point p if every path from the initial node to p evaluates x + y , and after the last such evaluation prior to reaching p, there is no assignments to x or y.

  • A block kills expression x + y if it assigns (or may assign) x or y and does not subsequently recompute x + y.
  • A block generates expression x + y if it definitely evaluates x + y and does not subsequently redefine x or y.

Available expression analysis is useful for detecting common subexpressions.

Available Expressions (cont.)

Dataflow Equations:

out[B] = e gen[B]

(in[B] − e kill[B]) in[B] =

P∈pred[B] out[P]^ for^ B^ not initial in[B 1 ] = φ where B 1 is the initial block

in[B] — expressions that are available at the beginning of B out[B] — expressions that are available at the end of B e gen[B] — expressions generated by B e kill[B] — expressions killed in B

Jingke Li (Portland State University) CS322 Dataflow Analysis 15 / 36

Available Expressions Algorithm

  1. Compute e gen[B] and e kill[B] for all basic blocks in the given CFG.
  2. Set in[B 1 ] = φ and out[B 1 ] = e gen[B 1 ], since they never change.
  3. Compute in[B] and out[B] using an iterative algorithm: 3.1 Start with the estimate out[B] = U − e kill[B] for all Bs (Note: since the confluence operator is

, the starting estimate for out[B] cannot be φ.); 3.2 Iteratively improve in[B] and out[B]; 3.3 Stop when the set values converge to a fixed point.

A Liveness Example

a := 0

b := a+

c := c+b

a := b*

a<N

return c

1 2 3 4 5 6

Iterative Steps: Init 1st 2nd 3rd n use def in out in out in out (^12) a ab (^) a a bca (^) ac bca 3 bc c bc bc b bc b 45 ba a ba (^) a ba (^) aca (^) acb (^) aca 6 c c c c 4th 5th 6th 7th n in out in out in out in out 1 ac c ac c ac c ac 2 ac bc ac bc ac bc ac bc 34 bcb acb bcbc (^) acb bcbc bcac bcbc bcac 5 ac ac ac ac ac ac ac ac 6 c c c c

Observations:

  • out[4] is used to compute in[4], in[4] is used to compute out[3].
  • We should compute the sets in the reversed order, i.e. the computation should follow the direction of flow.

Jingke Li (Portland State University) CS322 Dataflow Analysis 19 / 36

A Liveness Example (cont.)

Reversing Iteration Direction:

out[n] =

s ∈succ[n]

in[s]

in[n] = use[n]

(out[n] − def[n])

Init 1st 2nd 3rd n succ use def out in out in out in 6 c c c c 5 2,6 a c ac ac ac ac ac 4 5 b a ac bc ac bc ac bc 3 4 bc c bc bc bc bc bc bc 2 3 a b bc ac bc ac bc ac 1 2 a ac c ac c ac c

It converges much faster!

Case Study: Copy Statement

Copy statements are of the form x := y. This analysis is useful for performing copy propagation.

Dataflow Equations:

out[B] = c gen[B]

(in[B] − c kill[B]) in[B] =

P∈pred[B] out[P]^ for^ B^ not initial in[B 1 ] = φ where B 1 is the initial block in[B] — copy statements that are available at the beginning of B out[B] — copy statements that are available at the end of B c gen[B] — copy statements generated in B c kill[B] — copy statements killed in B

Jingke Li (Portland State University) CS322 Dataflow Analysis 21 / 36

DU Chains and UD Chains

Dataflow analysis results can be used for many compiler optimizations. But we need to introduce two new concept first:

  • Def-Use (DU) Chains — For each variable definition, the du-chain is a list containing all possible uses of the definition. UD chains can be computed from the result of reaching definition dataflow analysis.
  • (^) Use-Def (UD) Chains — For each variable use, the ud-chain is a list containing all definitions of the variable that may reach the use. a list. DU chains can be computed from UD chains.

A Global CSE Example (cont.)

Equations:

in[B] =

P out[P] out[B] = e gen[B]

(in[B] − e kill[B])

Iterations:

e gen e kill B 1 m-1,4n,a[t1] a.e.c. i,j,t1, or v B 2 i+1,4i,a[t2] a.e.c. i,t2, or t B 3 j-1,4j,a[t4] a.e.c. j,t4, or t B 4 φ φ B 5 4i,4j,a[t6],a[t8] a.e.c. t6,x,t8, or t B 6 4i,4*n,a[t11],a[t13] a.e.c. t11,x,t13, or t

Jingke Li (Portland State University) CS322 Dataflow Analysis 25 / 36

A Global CSE Example (cont.)

B in out B 1 φ m-1,4n,a[t1] B 2 φ i+1,4i,a[t2] B 3 φ j-1,4j,a[t4] 1 B 4 φ φ B 5 φ 4i,4j,a[t6],a[t8] B 6 φ 4i,4n,a[t11],a[t13] B 2 4n i+1,4i,4n,a[t2] B 3 i+1,4i,a[t2] i+1,j-1,4i,4j,a[t2],a[t4] 2 B 4 j-1,4j,a[t4] j-1,4j,a[t4] B 5 φ 4i,4j,a[t6],a[t8] B 6 φ 4i,4*n,a[t11],a[t13]

A Global CSE Example (cont.)

B in/out Sets B 2 in out 4ni+1,4i,4n,a[t2] B 3 in i+1,4i,4n,a[t2] 3 B^ out^ i+1,j-1,4i,4j,4n,a[t2],a[t4] 4 in out^ i+1,j-1,4i,4j,a[t2],a[t4]i+1,j-1,4i,4j,a[t2],a[t4] B 5 in out j-1,4j,a[t4]j-1,4i,4j,a[t4],a[t6],a[t8] B 6 in out j-1,4j,a[t4]j-1,4i,4j,4n,a[t4],a[t11],a[t13] B 2 in 4n out i+1,4i,4n,a[t2] B 3 in out i+1,4i,4n,a[t2]i+1,j-1,4i,4j,4n,a[t2],a[t4] 4 B 4 in i+1,j-1,4i,4j,4n,a[t2],a[t4] B^ out^ i+1,j-1,4i,4j,4n,a[t2],a[t4] 5 in out^ i+1,j-1,4i,4j,a[t2],a[t4]i+1,j-1,4i,4j,a[t2],a[t4],a[t6],a[t8] B 6 in out i+1,j-1,4i,4j,a[t2],a[t4]i+1,j-1,4i,4j,4n,a[t2],a[t4],a[t11],a[t13] B 2 in 4n B^ out^ i+1,4i,4n,a[t2] 3 in out^ i+1,4i,4n,a[t2]i+1,j-1,4i,4j,4n,a[t2],a[t4] 5 B 4 in out i+1,j-1,4i,4j,4n,a[t2],a[t4]i+1,j-1,4i,4j,4n,a[t2],a[t4] B 5 in i+1,j-1,4i,4j,4n,a[t2],a[t4] B^ out^ i+1,j-1,4i,4j,4n,a[t2],a[t4],a[t6],a[t8] 6 in out^ i+1,j-1,4i,4j,4n,a[t2],a[t4]i+1,j-1,4i,4j,4n,a[t2],a[t4],a[t11],a[t13]

Expressions 4i,4j, and 4*n in B 5 and B 6 are CSEs and can be eliminated.

Jingke Li (Portland State University) CS322 Dataflow Analysis 27 / 36

Application: Copy Propagation

Input: (1) A flow graph G ; (2) du-chain; and (3) results from the above dataflow analysis. Algorithm: For each copy statement S : x := y do

  1. Find use of x reached by S (use du-chain).
  2. Determine for each use found in (1), S is in c in[B] and no assignment to x or y prior to the use in B.
  3. Remove S, and replace all uses of x found in (1) by y.

Example: x := y y := ... x := z ... := x ... := x

B B2 B B B

B 1 B 2 B 3 B 4 B 5 c gen[B] x := y – x := z – – c kill[in[BB] ] x := z— x := yx := y x := yx := y (^) x := z– – – out[B] x := y – x := z x := z –

Structure Programs and Dataflow

The control-flow graphs derived from programs using exclusively structured flow-of-control statements such as if-then-else, while-do, continue, and break statements are called structured control-flow graphs, or reducible control-flow graphs. Many dataflow analysis algorithms work only on reducible control-flow graphs.

Some Reducible Flow Graphs and A Non-reducible Flow Graph:

S 1

S 2

S (^1) S 2 S 1

1

2 3

An important property of reducible flow graphs is that every “loop” has a single entry point.

Jingke Li (Portland State University) CS322 Dataflow Analysis 31 / 36

Hierarchical Dataflow Analysis

for Structured Flow Graphs

  • Case 1: Single Assignment Statement.

S ⇒ d: a:=b+c

gen[S] = {d} kill[S] = Da − {d} (Da is the set of definitions for a.) out[S] = gen[S]

(in[S] − kill[S])

  • Case 2: Single Non-Assignment Statement.

S ⇒ ...

gen[S] = φ kill[S] = φ out[S] = gen[S]

(in[S] − kill[S])

Hierarchical Dataflow Analysis (cont.)

  • Case 3: Sequence of Statements.

S ⇒

S 1

S 2

gen[S] = gen[S 2 ]

(gen[S 1 ] − kill[S 2 ]) kill[S] = kill[S 2 ]

(kill[S 1 ] − gen[S 2 ]) in[S 1 ] = in[S] in[S 2 ] = out[S 1 ] out[S] = out[S 2 ]

  • Case 4: Branch Statement.

S ⇒ S 1 S 2

gen[S] = gen[S 1 ]

gen[S 2 ] kill[S] = kill[S 1 ]

kill[S 2 ] in[S 1 ] = in[S 2 ] = in[S] out[S] = out[S 1 ]

out[S 2 ]

Jingke Li (Portland State University) CS322 Dataflow Analysis 33 / 36

Hierarchical Dataflow Analysis (cont.)

  • Case 5: Loop Statement.

S ⇒ S 1

gen[S] = gen[S 1 ] kill[S] = kill[S 1 ] in[S 1 ] = in[S]

out[S 1 ] out[S] = out[S 1 ] = gen[S 1 ]

(in[S 1 ] − kill[S 1 ])

Note that the definitions of in[S 1 ] and out[S 1 ] are mutually recursive.