CS412413 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CS412413

Description:

Want to find least conservative assignment of values that is still safe -- as ... Least conservative assignment means minimal lists of reaching definitions, so: ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 22
Provided by: andrew433
Category:

less

Transcript and Presenter's Notes

Title: CS412413


1
CS412/413
  • Introduction to
  • Compilers and Translators
  • April 7, 1999
  • Lecture 26 Optimizations based on
    data-flow analysis

2
Administration
  • Programming Assignment 4 (Iota)is due April 28
    -- available on web.
  • Prelim 2 April 16
  • static semantics, IR and assembly code
    generation, object-oriented languages, data-flow
    analysis, optimization
  • non-graded HW3 will be given out Friday
  • No class April 19

3
Review
  • Last time introduced quadruple IR for data-flow
    analysis, optimization.
  • Data-flow analysis framework
  • values in L to be propagated through program
    representing information about the program (e.g.
    live variables) either backward or forward
  • flow functions defining effect of program nodes
    on propagated information
  • Space of propagated values is a lattice with join
    (?), meet (?) operations, top (?) and bottom (?)

4
Propagating information
  • Each node n applies its flow function Fn to
    entering information
  • Entering information must take meet of
    information coming from all nodes that affect it
    -- least conservative information consistent with
    nodes affecting it
  • Want to find least conservative assignment of
    values that is still safe -- as high in lattice
    as possible

5
Reaching definitions
  • Reaching definitions output is for each node,
    list of definitions (assignments) that might
    still be in effect when the node is reached
  • L is all sets of defining nodes in call flow
    graph. Least conservative assignment means
    minimal lists of reaching definitions, so
  • Top (?) is the empty set, bottom (?) is the set
    of all nodes, meet (?) is set union (?) -- dual
    lattice to previous lattice example, with ? ,
    ? ?

6
Data-flow equations
  • Reaching definitions
  • outn genn ? (inn - killn)
  • in n ?n? predn outn
  • Mapping from in to out is flow function
  • outn Fn(inn)
  • Fn(x) genn ? (x - killn)
  • Mapping from out to in is done by the combining
    operation (meet)
  • inn ?n ? prednoutn ?n? prednoutn

7
Data-flow equations
  • Let x1xm be outn1outnm assignments to
    nodes 1m
  • Let predi be predecessors to node ni (assuming
    forward data-flow analysis)
  • xi Fi(?j ? predi xj)
  • Solution is point in Lm X (x1,xm)
  • (x1,xm) (F1(?j ? pred1 x1),, Fm(?j ?
    predm xm))
  • Total set of equations is X F(X) where F is the
    function that propagates information one step
    through control flow graph.

8
Fixed points
  • A fixed point of a function is a value that is
    mapped to itself X F(X)
  • Wanted maximal fixed point (least conservative
    satisfying assignment to all xi)
  • Iterative data-flow analysis initialize all xi
    with top of lattice (X ?), apply F(X) until
    fixed point is reached F( F(F(F(X))))
  • F is monotonic each iteration moves lower in
    lattice steps to fixed point cannot exceed
    mheight of lattice (sets of n things mn)
  • Worklist algorithm efficient implementation

9
Summary
  • Now have way of characterizing a data-flow
    analysis problem and producing an efficient
    solving algorithm
  • Specify meet operation over value lattice
  • Specify individual flow functions for different
    node types
  • Compute repeated application of F iteratively
    using simple work-list algorithm presented
    earlier avoids recomputing xi unnecessarily
  • Given proper lattice properties, convergence and
    maximal fixed point are guaranteed

10
Optimizations
  • Dead code elimination
  • Constant propagation
  • Copy propagation
  • Common sub-expression elimination

11
Dead code elimination
  • Optimization remove statements (quadruples) that
    assign to variables that are not live-out
  • Analysis live-variable analysis
  • backward data-flow analysis
  • values are sets of live variables? ?, ?
    ,Fn(x) usex ? (x def n)

12
Constant propagation
  • Optimization observing some assignment x c,
    replace later uses of x with constant c -- allows
    more constant folding, better register
    allocation, dead code, instruction selection
  • Analysis reaching definitions
  • forward data-flow analysis
  • values are sets of reaching definitions
  • ? ?, ? , Fn(x) genn ? (x - killn)
  • If only definition reaching a use of x isx c,
    can replace x with c

13
Copy propagation
  • Optimization observing some assignment x y,
    replace later uses of x with variable y -- better
    register allocation, creates dead code
  • Analysis starts with reaching definitions
  • Let RDn be definitions reaching beginning of n.
    If only one definition of x reaches n and y is
    still available, can substitute y for x.
  • Additional forward analysis availability. Values
    are sets of definitions with available RHS.
  • ? ?, ? all defns,Fn(x) (genn ? x) -
    killn

14
CP gen and kill
  • Let uses(y) be all nodes that use y in xy
  • node n genn killn
  • a b n uses(a)
  • a b OP c uses(a)
  • a Mb uses(a)
  • Ma b
  • goto L
  • if a goto L1 else L2
  • a f(b,c,d) uses(a)
  • f(b,c,d)
  • Can substitute y for x at node n if RDn
    contains only one definition of x and
    RHS-AVAILn also contains that definition.

15
CP example
  • RD RHS-AVAIL
  • 1. a b
  • 2. d a 1 1 1
  • 3. if c goto L1 else L2 1,2 1
  • 4. L1 e a 1,2 1
  • 5. goto L2 1,2,4 1,4
  • 6. L2 a f 2 1,2 1
  • 7. g a 2,6

16
Common sub-expression elimination
  • Idea if same expression is computed twice(a b
    OP c), replace second computation with temporary
    that original was stored into
  • a b OP c d b OP c ?t b OP c a t
    d t
  • Need to know which expressions b OP c are still
    available (generalization of previous analysis)
  • Available if neither b nor c have been reassigned
    and every path to node includes assignment --
    computable using data-flow

17
CSE Data-flow
  • forward data-flow analysis AE(n) gives sets of
    definitions computing expressions that are
    available at n
  • lattice value is ltd, a OP bgt
  • ? ? , ? all valid pairs ltd, a OP bgt,
    AE(entry) ,Fn(x) (x ? EVALn) KILLn
  • EVALn is either or ltn, a OP bgt if n has form
    c a OP b
  • KILLn all pairs ltd, a OP bgt such that n
    updates a or b

18
Example
  • y (x 1)(x 1) x w z x 1
  • AE
  • 1. a x 1
  • 2. b x 1 lt1, x1gt
  • 3. y a b lt1, x1gt, lt2, x1gt
  • 4. x w lt1, x1gt, lt2, x1gt
  • 5. z x 1

19
Optimizations
  • t x 1
  • a t
  • b t
  • y a b
  • x w
  • z x 1
  • t x 1
  • a t
  • b t
  • y t t
  • x w
  • z w 1

Common sub-expression elimination
Dead code elimination(assuming a, b, x not used
after)
t x 1 y t t z w 1
Copy Propagation
20
Uninitialized variables
  • Can use data-flow analysis for semantic checking
    too detect uninitialized variables (use before
    def )
  • Use reaching definitions analysis
  • If use occurs with no reaching definition,
    variable cannot have been initialized -- likely
    an error
  • More restrictive check require that all paths
    include definition (Java rule) check that
    variable is available instead

21
Summary
  • Standard optimizations require data-flow analyses
    that fit into lattice analysis framework can
    also be used for semantic analyses
  • Live variable analysis backward, ? ?
  • Reaching definitions forward, ? ?
  • RHS availability, general availability forwar
    d, ? ?
Write a Comment
User Comments (0)
About PowerShow.com