210 likes | 431 Views
CS412/413. Introduction to Compilers and Translators April 7, 1999 Lecture 26: Optimizations based on data-flow analysis. Administration. Programming Assignment 4 (Iota + ) is due April 28 -- available on web. Prelim 2 April 16
E N D
CS412/413 Introduction to Compilers and Translators April 7, 1999 Lecture 26: Optimizations based on data-flow analysis
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 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 () CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 = {}, = CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Data-flow equations • Reaching definitions: out[n] = gen[n] (in[n] - kill[n]) in [n] = n’ pred[n] out[n] • Mapping from in to out is flow function: out[n] = Fn(in[n]) Fn(x) = gen[n] (x - kill[n]) • Mapping from out to in is done by the combining operation (meet): in[n] = n’ pred[n]out[n’] = n’ pred[n]out[n] CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Data-flow equations • Let x1…xm be out[n1]…out[nm]: assignments to nodes 1…m • Let pred[i] be predecessors to node ni (assuming forward data-flow analysis) xi = Fi(j pred[i] xj) • Solution is point in Lm : X = (x1,…xm) (x1,…xm) = (F1(j pred[1] x1),…, Fm(j pred[m] xm)) • Total set of equations is X = F(X) where F is the function that propagates information one step through control flow graph. CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 m*height of lattice (sets of n things: m*n) • Worklist algorithm: efficient implementation CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Optimizations • Dead code elimination • Constant propagation • Copy propagation • Common sub-expression elimination CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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) = use[x] (x – def [n]) CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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) = gen[n] (x - kill[n]) • If only definition reaching a use of x isx = c, can replace x with c CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 RD[n] 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) = (gen[n] x) - kill[n] CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
CP gen and kill Let uses(y) be all nodes that use y in x=y node n gen[n] kill[n] a = b { n } uses(a) a = b OP c {} uses(a) a = M[b] {} uses(a) M[a] = 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 RD[n] contains only one definition of x and RHS-AVAIL[n] also contains that definition. CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
CSE Data-flow • forward data-flow analysis AE(n) gives sets of definitions computing expressions that are available at n • lattice value is <d, a OP b> • = , = { all valid pairs <d, a OP b>}, AE(entry) = {},Fn(x) = (xEVAL[n]) – KILL[n] • EVAL[n] is either {} or <n, a OP b> if n has form c = a OP b • KILL[n] = all pairs <d, a OP b> such that n updates a or b CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
Example y = (x + 1)*(x + 1); x = w; z = x + 1; AE 1. a = x + 1 2. b = x + 1 <1, x+1> 3. y = a * b <1, x+1>, <2, x+1> 4. x = w <1, x+1>, <2, x+1> 5. z = x + 1 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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 CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers
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: forward, = CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers