










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 Dataflow Analysis 1 / 36
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
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
Jingke Li (Portland State University) CS322 Dataflow Analysis 3 / 36
For a given problem, typical dataflow analysis steps are
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
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] =
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
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).
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
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 ] = {}
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
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.
Available expression analysis is useful for detecting common subexpressions.
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
, 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 := 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:
Jingke Li (Portland State University) CS322 Dataflow Analysis 19 / 36
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!
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
Dataflow analysis results can be used for many compiler optimizations. But we need to introduce two new concept first:
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
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]
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
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
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 –
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
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])
gen[S] = φ kill[S] = φ out[S] = gen[S]
(in[S] − kill[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 ]
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
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.