Advanced Compilers CMPSCI 710 Spring 2003 Yet more























- Slides: 23
Advanced Compilers CMPSCI 710 Spring 2003 Yet more data flow analysis Emery Berger University of Massachusetts, Amherst UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
Project Stuff n n n 1 to 2 person teams Implement optimization/analysis in: n Jikes RVM (IBM’s research Java compiler) n Broadway (UTexas “metacompiler”) n other (subject to approval) Due dates: n 02/11/03: One-page project description. n 02/25/03: 2 -4 page project design. n 03/25/03: Project implementation review. n 04/29/03: Implementation due. n 05/06 -13/03: In-class presentations. n 05/13/03: Project report. UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 2
Yet More Data Flow Analysis n Last time: n n The iterative worklist algorithm Today: n Live variable analysis n n backwards problem Constant propagation n n algorithms def-use chains UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 3
Live Variable Analysis n Variable x is live at point p if: n used before being redefined along some path starting at p n n Use(p): n n backwards problem variables that may be used starting at p Def(p): n variables that may be defined in p UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 4
Use, Def, Live Variables: Example Use 1: 2: 3: 4: 5: 6: Def Live x = 12; y = 14; z = x; y = 15; q = z + z; halt; UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 5
Defining Live Variable Analysis n n n Lattice elements = In(Exit) = Out(v) = u. P in SUCC(S)In(P) u= In(v) = Use(v) u (Out(v) – Def(v)) x 2 Use(v) iff x may be used before defined n n n Use(d: v = …x…) = Use(d: if (…x…)) = x 2 Def(v) iff x defined before used in v n Def(d: v = exp) = UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 6
Iterative Worklist Algorithm, Live Variables for v 2 V OUT(v) = IN(v) = Use(v) worklist à V while (worklist ) for v 2 worklist oldin(v) = IN(v) OUT(v) = up 2 SUCC(v) IN(p) IN(v) = Use(v) u (OUT(v) – Def(v)) if (oldin(v) IN(v)) worklist à worklist [ PRED(v) UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 7
Live Variables Example Entry 1: parameter a; 2: parameter b; 3: x=a*b; use(1) = use(2) = use(3) = use(4) = use(5) = use(6) = use(7) = def(1) = def(2) = def(3) = def(4) = def(5) = def(6) = def(7) = 4: y=a*b; 5: if y > a+b 6: a = a+1; 7: x = a+b; Exit UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 8
Outline n Today: n Live variable analysis n n backwards problem Constant propagation n n algorithms def-use chains UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 9
Constant Propagation n Discovers constant variables & expressions Propagates them as far forward as possible Uses: n n Evaluate expressions at compile-time Eliminates dead code n n n e. g. , debugging code Improves effectiveness of many optimizations Always a win UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 10
Constant Propagation Lattice, Revisited n Meet rules: n n n > … -2 -1 0 1 2 … ? au>=a au? =? constant u constant = constant (if equal) constant u constant = ? (if not equal) Initialization: n Optimistic assumption: n n all variables unknown constant = > Pessimistic assumption: n all variables not constant = ? UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 11
Wegman & Zadeck: TOPLAS 1991 n n Relates & improves on previous constant propagation algorithms Sparsity n n incorporates info from branches conditional constant faster improves speed Conditional: n simple constant more constants sparse simple constant faster more constants sparse conditional constant UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 12
simple constant Kildall’s Algorithm n Worklist-based: n n faster sparse simple constant conditional constant faster more constants sparse conditional constant add successors of Entry remove and examine a node from worklist evaluate expressions to compute new In and Out if the Out value changes, n n more constants add successors to worklist Finds simple constants: n n no information about direction of branches one value per variable along each path UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 13
simple constant Kildall’s Algorithm: Example more constants faster sparse simple constant conditional constant faster more constants sparse conditional constant iÃ1 i à 1 j à 2 if (j == 2) i à 3 z à i jÃ2 if (j==2) iÃ3 zÃi UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 14
simple constant Kildall’s Algorithm: Analysis n In terms of N, E, V: n n n faster sparse simple constant conditional constant faster more constants sparse conditional constant N = assignments + expressions in branches n n more constants for convenience: N = nodes in CFG E = edges in CFG V = variables Iterations = 2 * V * I (in-edges) n n Runtime = iterations * operations = Space = lattice values = UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 15
simple constant Reif & Lewis n Kildall’s (SC): n n more constants conditional constant faster sparse simple constant faster more constants sparse conditional constant at each node, computes value of all variables at entry and produces set of values for all variables at exit Reif & Lewis (SSC): n n n also finds simple constants, but faster sparse representation original formulation based on def-use graph n revised version based on SSA form UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 16
Def-Use Graph n Graph of def-use chains: connection from definition site (assignment) to use site along path in CFG n n does not pass through another definition Includes infeasible paths n misses some constants UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 17
Def-Use Graph: Example i à 1 j à 2 if (j == 2) i à 3 z à i iÃ1 jÃ2 if (j==2) iÃ3 zÃi UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 18
simple constant Reif and Lewis n Worklist: n n more constants faster sparse simple constant conditional constant faster more constants sparse conditional constant Put root edges from def-use graph in worklist if def site in roots can be evaluated to constant, assign that to variable, otherwise ? assign all other variables > remove def-join edges from worklist: n n propagate value of def to use using meet rules if value is lowered, add node to worklist UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 19
Reif and Lewis: Example UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 20
simple constant Wegman & Zadeck: Conditional Definition n more constants faster sparse simple constant faster more constants Conditional definition: keeps track of conditional branches n n conditional constant sparse conditional constant form of dead code elimination constant expr in branch ) mark appropriate branch as executable use symbolic execution to mark edges ignore non-executable edges at joins when propagating constants UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 21
Def-Use Chains: Problem switch case case n (j) x: i y: i z: i (k) x: a y: b z: c à 1; à 2; à 3; à i; worst-case size of graph = O(? ) UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 22
Next Time n “SSA is a better way” n n n Dominance & dominance frontiers Control dependence Read ACDI Chapter 8, pp. 252— 258 UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 23