1 / 23

Advanced Compilers CMPSCI 710 Spring 2003 Yet more data flow analysis

Explore live variable analysis, constant propagation, and algorithms in the context of advanced compiler optimizations. Study iterative worklist algorithms and def-use chains. Discover techniques to optimize code and improve compiler efficiency.

cannona
Download Presentation

Advanced Compilers CMPSCI 710 Spring 2003 Yet more data flow analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced CompilersCMPSCI 710Spring 2003Yet more data flow analysis Emery Berger University of Massachusetts, Amherst

  2. Project Stuff • 1 to 2 person teams • Implement optimization/analysis in: • Jikes RVM (IBM’s research Java compiler) • Broadway (UTexas “metacompiler”) • other (subject to approval) • Due dates: • 02/11/03: One-page project description. • 02/25/03: 2-4 page project design. • 03/25/03: Project implementation review. • 04/29/03: Implementation due. • 05/06-13/03: In-class presentations. • 05/13/03: Project report.

  3. Yet More Data Flow Analysis • Last time: • The iterative worklist algorithm • Today: • Live variable analysis • backwards problem • Constant propagation • algorithms • def-use chains

  4. Live Variable Analysis • Variable x is live at point p if: • used before being redefined along some path starting at p • backwards problem • Use(p): • variables that may be used starting at p • Def(p): • variables that may be defined in p

  5. Use, Def, Live Variables:Example Use Def Live 1: x = 12; 2: y = 14; 3: z = x; 4: y = 15; 5: q = z + z; 6: halt;

  6. Defining Live Variable Analysis • Lattice elements = • In(Exit) = • Out(v) = uP inSUCC(S)In(P) • u= • In(v) = Use(v)u(Out(v) – Def(v)) • x 2Use(v) iff x may be used before defined • Use(d: v = …x…) = • Use(d: if (…x…)) = • x 2Def(v) iff x definedbefore used in v • Def(d: v = exp) =

  7. 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)

  8. Live Variables Example Entry 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) = 1: parameter a; 2: parameter b; 3: x=a*b; 4: y=a*b; 5: if y > a+b 6: a = a+1; Exit 7: x = a+b;

  9. Outline • Today: • Live variable analysis • backwards problem • Constant propagation • algorithms • def-use chains

  10. Constant Propagation • Discovers constant variables & expressions • Propagates them as far forward as possible • Uses: • Evaluate expressions at compile-time • Eliminates dead code • e.g., debugging code • Improves effectiveness of many optimizations • Always a win

  11. Constant Propagation Lattice,Revisited > … -2 -1 0 1 2 … ? • Meet rules: • a u> = a • a u? = ? • constant u constant = constant (if equal) • constant u constant = ?(if not equal) • Initialization: • Optimistic assumption: • all variables unknown constant = > • Pessimistic assumption: • all variables not constant = ?

  12. Wegman & Zadeck:TOPLAS 1991 • Relates & improves on previous constant propagation algorithms • Sparsity • improves speed • Conditional: • incorporates info from branches moreconstants simpleconstant conditionalconstant faster faster moreconstants sparsesimpleconstant sparseconditionalconstant

  13. simpleconstant moreconstants conditionalconstant Kildall’s Algorithm faster faster sparseconditionalconstant sparsesimpleconstant moreconstants • Worklist-based: • add successors of Entry • remove and examine a node from worklist • evaluate expressions to compute new In and Out • if the Out value changes, • add successors to worklist • Finds simple constants: • no information about direction of branches • one value per variable along each path

  14. simpleconstant moreconstants conditionalconstant Kildall’s Algorithm:Example faster faster sparseconditionalconstant sparsesimpleconstant moreconstants i à 1 i à 1 j à 2 if (j == 2) i à 3 z à i j à 2 if (j==2) i à 3 z à i

  15. simpleconstant moreconstants conditionalconstant Kildall’s Algorithm:Analysis faster faster sparseconditionalconstant sparsesimpleconstant moreconstants • In terms of N, E, V: • N = assignments + expressions in branches • for convenience: N = nodes in CFG • E = edges in CFG • V = variables • Iterations = 2 * V * I (in-edges) • Runtime = iterations * operations = • Space = lattice values =

  16. simpleconstant moreconstants conditionalconstant Reif & Lewis faster faster sparseconditionalconstant sparsesimpleconstant moreconstants • Kildall’s (SC): • at each node, computes value of all variables at entry and produces set of values for all variables at exit • Reif & Lewis (SSC): • also finds simple constants, but faster • sparse representation • original formulation based on def-use graph • revised version based on SSA form

  17. Def-Use Graph • Graph of def-use chains:connection from definition site (assignment) to use site along path in CFG • does not pass through another definition • Includes infeasible paths • misses some constants

  18. Def-Use Graph:Example i à 1 i à 1 j à 2 if (j == 2) i à 3 z à i j à 2 if (j==2) i à 3 z à i

  19. simpleconstant moreconstants conditionalconstant Reif and Lewis faster faster sparseconditionalconstant sparsesimpleconstant moreconstants • Worklist: • 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: • propagate value of def to use using meet rules • if value is lowered, add node to worklist

  20. Reif and Lewis:Example

  21. simpleconstant moreconstants conditionalconstant Wegman & Zadeck:Conditional Definition faster faster sparseconditionalconstant sparsesimpleconstant moreconstants • Conditional definition:keeps track of conditional branches • 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

  22. Def-Use Chains: Problem • worst-case size of graph = O(?) switch (j) case x: i à 1; case y: i à 2; case z: i à 3; switch (k) case x: a à i; case y: b à i; case z: c à i;

  23. Next Time • “SSA is a better way” • Dominance & dominance frontiers • Control dependence • Read ACDI Chapter 8, pp. 252—258

More Related