Speeding Up Data-Flow Analysis Solving the Data

Speeding Up Data-Flow Analysis
Solving the Data-flow Equations
Last time
– Various optimizations that use data-flow analysis
Algorithm
for each node n in CFG
in[n] = ∅; out[n] = ∅
repeat
for each node n in CFG
in’[n] = in[n]
out’[n] = out[n]
in[n] = use[n] ∪ (out[n] – def[n])
out[n] =
∪
in[s]
Today
– Speeding up data-flow analysis
initialize solutions
save current results
solve data-flow equations
s ∈ succ[n]
until in’[n]=in[n] and out’[n]=out[n] for all n
test for convergence
This is iterative data-flow analysis (for liveness analysis)
CS553 Lecture
Data-Flow Analysis Efficiency
1
CS553 Lecture
Data-Flow Analysis Efficiency
Time Complexity (liveness)
Efficiency (from lattice lecture)
Consider a program of size N
– Has N nodes in the flow graph and at most N variables
– Each live-in or live-out set has at most N elements
– Each set-union operation takes O(N) time
– The for loop body
– constant # of set operations per node
– O(N) nodes ⇒ O(N2) time for the loop
Parameters
– n: Number of nodes in the CFG
– k: Height of lattice
– t: Time to execute one flow function
Complexity
– O(nkt)
– Each iteration of the repeat loop can only make the set larger
– Each set can contain at most N variables ⇒ 2N2 iterations
Worst case:
Typical case:
CS553 Lecture
Relation to previous slide
– n=N
– k = N2 // used as worst-case for repeat loop
– t=N
O(N4)
2 to 3 iterations with good ordering & separability
⇒ O(N) to O(N2)
Data-Flow Analysis Efficiency
2
3
CS553 Lecture
Data-Flow Analysis Efficiency
4
1
Ways to improve data-flow analysis efficiency
Example (liveness)
Node ordering
– Visit nodes in an ordering that most efficiently propagates change
1st
2nd
3rd
4th
5th
6th
7th
node use def in out in out in out in out in out in out in out
#
1
Bitvectors
– Use the tuple of lattices concept to implement the data-flow sets as bit-vectors
Worklist
– Only visit nodes where the input data-flow sets have changed
Basic Blocks
– Group statements with straight-forward control-flow
c ac
c ac
5
1
a := 0
2
b := a + 1
c ac
a
b
3
bc
c
a
bc
bc b
4
b
a
b
b a
c := c + b
a
a a
b a b ac bc ac bc ac bc ac
a ac ac ac ac ac ac ac ac ac ac ac
3
5
6
c
c
c
4
a := b * 2
a bc ac bc ac bc ac bc ac bc ac bc
bc b
bc b
c
bc b
c
∪
s ∈ succ[n]
bc bc bc bc
c
c
c
a < 9?
5
No
6
Yes
return c
in[s]
CS553 Lecture
Data-Flow Analysis Efficiency
6
Iterating Through the Flow Graph Backwards
Data-flow Equations for Liveness
in[n] = use[n] ∪ (out[n] – def[n])
∪
ac
2
out[n] =
Data-Flow Analysis Efficiency
s ∈ succ[n]
a
in[n] = use[n] ∪ (out[n] – def[n])
Example (cont)
out[n] =
a
Data-flow Equations for Liveness
Others
– Structural or interval analysis
– Slotwise analysis
– SSA
CS553 Lecture
a
in[s]
Improving Performance
Consider the (3→4) edge in the graph:
out[4] is used to compute in[4]
in[4] is used to compute out[3] . . .
So we should compute the sets in the
order: out[4], in[4], out[3], in[3], . . .
out[3]
in[4]
out[4]
a := 0
2
b := a + 1
6
c
c
5
a
c ac
c := c + b
4
b
a
ac bc ac bc ac bc
3
bc
c
bc bc bc bc bc bc
a := b * 2
2
a
b
bc ac bc ac bc ac
a
ac c
3
4
1
a < 9?
5
No
6
1st
2nd
3rd
node use def out in out in out in
#
1
c
c
ac ac ac ac
ac c
ac
return c
a := 0
2
b := a + 1
3
c := c + b
4
a := b * 2
c
a < 9?
5
Converges much faster!
Yes
1
No
6
Yes
return c
The order of computation should follow the direction of flow
CS553 Lecture
Data-Flow Analysis Efficiency
7
CS553 Lecture
Data-Flow Analysis Efficiency
8
2
Solving the Data-flow Equations (reprise)
Effects on complexity
Algorithm
Repeat loop
– Conservative upper bound is that each iteration will only make one set larger by one
element.
– A better approximation for separable analyses is height of the lattice (k) times the depth of
the graph (Knuth 1971, depth is 2.75 on average).
– Now repeat loop complexity is k instead of N2 .
for each node n in CFG
Initialize solutions
in[n] = ∅; out[n] = ∅
repeat
for each node n in CFG in reverse post dfs order
in’[n] = in[n]
Save current results
out’[n] = out[n]
out[n] =
∪
in[s]
s ∈ succ[n]
Solve data-flow equations
in[n] = use[n] ∪ (out[n] – def[n])
until in’[n]=in[n] and out’[n]=out[n] for all n
Test for convergence
CS553 Lecture
Data-Flow Analysis Efficiency
– Use the tuple of lattices concept to implement the data-flow sets as bit-vectors.
– Now time to execute one flow function (t) is N/(wordsize).
– For sparse sets, use a sorted list (e.g., linked list)
Worklist
– Only visit nodes where the input data-flow sets have changed.
– Does not change complexity, but makes it unnecessary for convergence check iteration in
IDFA.
9
CS553 Lecture
Data-Flow Analysis Efficiency
Basic Blocks
Concepts
Basic blocks
– Decrease the size of the CFG by merging nodes
that have a single predecessor and a single
successor into basic blocks
Efficient Data-Flow Analysis
– Complexity analysis
– Node ordering
– Bit vector implementation
– Basic blocks
1
a := 0
2
b := a + 1
1
a := 0
3
c := c + b
2
4
a := b * 2
b := a + 1
c := c + 1
a := b * 2
a > 9?
a < 9?
5
No
6
Representation of sets
– For dense sets, use a bit vector representation
No
Yes
3
return c
10
Yes
return c
CS553 Lecture
Data-Flow Analysis Efficiency
11
CS553 Lecture
Data-Flow Analysis Efficiency
12
3
Next Time
Reading
– Ch 18.1, 19.5
Lecture
– Control dependence
– Loops
– Dominators
CS553 Lecture
Data-Flow Analysis Efficiency
13
4