Presentation is loading. Please wait.

Presentation is loading. Please wait.

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University.

Similar presentations


Presentation on theme: "U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University."— Presentation transcript:

1 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University of Massachusetts, Amherst

2 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 2 Data flow analysis Framework for proving facts about program at each point Point: entry or exit from block (or CFG edge) Lots of “small facts” Little or no interaction between facts Based on all paths through program Includes infeasible paths

3 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 3 Infeasible Paths Example a = 1; if (a == 0) { a = 1; } if (a == 0) { a = 2; } Infeasible paths never actually taken by program, regardless of input Undecidable to distinguish from feasible

4 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 4 Data Flow-Based Optimizations Dead variable elimination a = 3; print a; x = 12; halt ) a = 3; print a; halt Copy propagation x = y; … use of x ) …use of y Partial redundancy a = 3*c + d; b = 3*c ) b = 3*c; a=b+d Constant propagation a = 3; b = 2; c = a+b ) a = 3; b = 2; c = 5

5 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 5 Data Flow Analysis Define lattice to represent facts Attach meaning to lattice values Associate transfer function to each node (f:L ! L) Initialize values at each program point Iterate through program until fixed point

6 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 6 Lattice-Related Definitions Meet function: u commutative and associative x u x = x Unique bottom ? and top > element x u ? = ? x u > = x Ordering: x v y iff x u y = x Function f is monotone if 8 x, y: x v y implies f(x) v f(y)

7 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 7 Bit-Vector Lattice Meet = bit-vector logical and 111 011101110 001010100 000 > ? 111 u 111 = 011 u 111 = 000 u 101 = 100 u 011 = 011 u 110 = 001 u 110 =

8 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 8 Constant Propagation Lattice Meet rules: a u > = a a u ? = ? constant u constant = constant (if equal) constant u constant = ? (if not equal) Define obvious transfer functions for arithmetic > … -2 -1 0 1 2 … ?

9 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 9 Iterative Data Flow Analysis Initialize non-entry nodes to > Identity element for meet function If node function is monotone: Each re-evaluation of node moves down the lattice, if it moves at all If height of lattice is finite, must terminate

10 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 10 Constant Propagation Example I x = true; if (x) a = 3; b = 2; a = 2; b = 3; c = a+b; then else Two choices of “point”: Compute at edges maximal information Compute on entry must “meet” data from all incident edges loses information

11 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 11 Constant Propagation Example II Vector for (x,a,b,c) Init values to > Iterate forwards x = true; if (x) a = 3; b = 2; a = 2; b = 3; c = a+b; then else (>,>,>,>)(>,>,>,>)(>,>,>,>)(>,>,>,>) (>,>,>,>)(>,>,>,>)(>,>,>,>)(>,>,>,>) (>,>,>,>)(>,>,>,>) (true, >, >, > ) (true,3,2, > )(true,2,3, > ) (true, ?, ?,5)

12 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 12 Accuracy: MOP vs. MFP We want “meet-over-all-paths” solution, but paths can be infinite if there are loops Best we can do in general: Maximum Fixed Point solution = largest solution, ordered by v, that is fixed point of iterative computation Provides “most information” More conservative than MOP

13 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 13 Distributive Problems f is distributive iff f(x u y) = f(x) u f(y) Doing meet early doesn’t reduce precision Non-distributive problems: constant propagation Distributive problems: MFP = MOP reaching definitions, live variables

14 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 14 Reaching Definitions Definition: each assignment to variable defs(v) represents set of all definitions of v Assume all variables scalars No pointers No arrays A definition reaches given point if 9 path to that point such that variable may have value from definition

15 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 15 Data Flow Functions Kill(S): facts not true after S just because they were true before example: redefinition of variable (assignment) Gen(S): facts true after S regardless of facts true before S example: assigned values not killed in S In(S): dataflow info on entry to S In(S) = [ p 2 PRED(S) Out(p) example: definitions that reach S Out(S): dataflow info on exit from S Out(S) = Gen(S) [ (In(S) – Kill(S)) example: reaching defs after S

16 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 16 For Reaching Definitions For reaching defs, u = [ Gen(d: v = exp) = {d} “on exit from block d, generate new definition” Kill(d: v = exp) = defs(v) “on exit from block d, definitions of v are killed” Computing In(S) If S has one predecessor P, In(S) = Out(P) Otherwise: In(S) = u P in PRED(S) Out(P) Out(Entry) = {}

17 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 17 Reaching Definitions Example parameter a; parameter b; x = a*b; y = a*b; while (y > a+b) { a = a+1; x = a+b; } Entry 3: x=a*b; if y > a+b 5: a = a+1; Exit 4: y=a*b; 2: parameter b; 1: parameter a; 6: x = a+b; defs(x) = defs(y) = defs(a) = defs(b) =

18 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 18 Analysis Direction Forwards analysis: start with Entry, compute towards Exit Backwards analysis: start with Exit, compute towards Entry In(S) = Gen(S) [ (Out(S) – Kill(S)) Out(S) = u F in SUCC(S) In(F) Backwards problems: Live variables: which variables might be read before overwritten or discarded

19 U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 19 Next Time More data flow analysis


Download ppt "U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University."

Similar presentations


Ads by Google