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