Analysis and Optimizations

Analysis and Optimizations

Analysis and Optimizations • Program Analysis P3 / 2006 – Discovers properties of a program • Optimizations Using Program Analysis – Use analysis results to transform program for Optimization – Goal: improve some aspect of program • number of executed instructions, number of cycles • cache hit rate • memory space (code or data) • power consumption – Has to be safe: Keep the semantics of the program Kostis Sagonas 2 Spring 2006 Control Flow Graph Control Flow Graph entry int add(n, k) { • Nodes represent computation s = 0; a = 4; i = 0; – Each node is a Basic Block if (k == 0) b = 1; s = 0; a = 4; i = 0; k == 0 – Basic Block is a sequence of instructions with else b = 2; • No branches out of middle of basic block while (i < n) { b = 1; b = 2; • No branches into middle of basic block s = s + a*b; • Basic blocks should be maximal i = i + 1; i < n } – Execution of basic block starts with first instruction return s; s = s + a*b; – Includes all instructions in basic block return s } i = i + 1; • Edges represent control flow Kostis Sagonas 3 Spring 2006 Kostis Sagonas 4 Spring 2006 Two Kinds of Variables Basic Block Optimizations • Temporaries introduced by the compiler • Common Sub- • Copy Propagation – Transfer values only within basic block Expression Elimination – a = x+y; b = a; c = b+z; – Introduced as part of instruction flattening – a = (x+y)+z; b = x+y; – a = x+y; b = a; c = a+z; – Introduced by optimizations/transformations – t = x+y; a = t+z; b = t; • Program variables • Constant Propagation • Dead Code Elimination – x = 5; b = x+y; – a = x+y; b = a; c = a+z; – Declared in original program – b = 5+y; – a = x+y; c = a+z – May transfer values between basic blocks • Algebraic Simplification • Strength Reduction – a = x * 1; – t = i * 4; – a = x; – t = i << 2; Kostis Sagonas 5 Spring 2006 Kostis Sagonas 6 Spring 2006 Value Numbering Value Numbering for CSE • Normalize basic block so that all statements are of the form • As we simulate execution of program – var = var op var (where op is a binary operator) • Generate a new version of program – var = op var (where op is a unary operator) – Each new value assigned to temporary – var = var • a = x+y; becomes a = x+y; t = a; • Simulate execution of basic block – Temporary preserves value for use later in program even if original variable rewritten – Assign a virtual value to each variable • a = x+y; a = a+z; b = x+y becomes – Assign a virtual value to each expression • a = x+y; t = a; a = a+z; b = t; – Assign a temporary variable to hold value of each computed expression Kostis Sagonas 7 Spring 2006 Kostis Sagonas 8 Spring 2006 New Basic CSE Example Original Basic Block Block a = x+y a = x+y • Original • After CSE t1 = a b = a+z b = a+z a = x+y a = x+y b = b+y t2 = b b = a+z b = a+z c = a+z b = b+y b = b+y t = b t3 = b c = a+z b = b+y Var to Val c = t2 • Issues c = t x ç v1 y ç v2 Exp to Val Exp to Tmp – Temporaries store values for use later a ç v3 v1+v2 ç v3 v1+v2 ç t1 z ç v4 – CSE with different names v3+v4 ç v5 v3+v4 ç t2 b ç v5 b v6 ç v5+v2 ç t3 • a = x; b = x+y; c = a+y; c ç v5 v5+v2 v6 – Excessive Temp Generation and Use Kostis Sagonas 9 Spring 2006 Kostis Sagonas 10 Spring 2006 Problems Copy Propagation • Algorithm has a temporary for each new value • Once again, simulate execution of program – a = x+y; t1 = a • If possible, use the original variable instead of a • Introduces temporary – lots of temporaries – a = x+y; b = x+y; – lots of copy statements to temporaries – After CSE becomes a = x+y; t = a; b = t; • In many cases, temporaries and copy statements – After CP becomes a = x+y; b = a; are unnecessary • Key idea: determine when original variables are • So we eliminate them with copy propagation NOT overwritten between computation of stored and dead code elimination value and use of stored value Kostis Sagonas 11 Spring 2006 Kostis Sagonas 12 Spring 2006 Copy Propagation Maps Copy Propagation Example • Maintain two maps Original After CSE After CSE and – tmp to var: tells which variable to use instead of a Copy Propagation given temporary variable a = x+y a = x+y a = x+y b = a+z t1 = a t1 = a – var to set (inverse of tmp to var): tells which temps b = a+z c = x+y b = a+z are mapped to a given variable by tmp to var t2 = b a = b t2 = b c = t1 c = a a = b a = b Kostis Sagonas 13 Spring 2006 Kostis Sagonas 14 Spring 2006 Copy Propagation Example Copy Propagation Example Basic Block Basic Block After Basic Block Basic Block After After CSE CSE and Copy Prop After CSE CSE and Copy Prop a = x+y a = x+y a = x+y a = x+y t1 = a t1 = a t1 = a t1 = a b = a+z b = a+z t2 = b t2 = b tmp to var var to set tmp to var var to set t1 ç a a ç{t1} t1 ç a a ç{t1} t2 ç b b ç{t2} Kostis Sagonas 15 Spring 2006 Kostis Sagonas 16 Spring 2006 Copy Propagation Example Copy Propagation Example Basic Block Basic Block After Basic Block Basic Block After After CSE CSE and Copy Prop After CSE CSE and Copy Prop a = x+y a = x+y a = x+y a = x+y t1 = a t1 = a t1 = a t1 = a b = a+z b = a+z b = a+z b = a+z t2 = b t2 = b t2 = b t2 = b c = t1 c = t1 c = a tmp to var var to set tmp to var var to set t1 ç a a ç{t1} t1 ç a a ç{t1} t2 ç b b ç{t2} t2 ç b b ç{t2} Kostis Sagonas 17 Spring 2006 Kostis Sagonas 18 Spring 2006 Copy Propagation Example Copy Propagation Example Basic Block Basic Block After Basic Block Basic Block After After CSE CSE and Copy Prop After CSE CSE and Copy Prop a = x+y a = x+y a = x+y a = x+y t1 = a t1 = a t1 = a t1 = a b = a+z b = a+z b = a+z b = a+z t2 = b t2 = b t2 = b t2 = b c = t1 c = a c = t1 c = a a = b a = b a = b a = b tmp to var var to set tmp to var var to set t1 ç a a ç{t1} t1 ç t1 a ç{} t2 ç b b ç{t2} t2 ç b b ç{t2} Kostis Sagonas 19 Spring 2006 Kostis Sagonas 20 Spring 2006 Dead Code Elimination Dead Code Elimination • Copy propagation keeps all temps around • Basic Idea • There may be temps that are never read – Process code in reverse execution order • Dead Code Elimination (DCE) removes them – Maintain a set of variables that are needed later in computation Basic Block After Basic Block After – On encountering an assignment to a temporary that CSE + Copy Prop CSE + Copy Prop + DCE is not needed, we remove the assignment a = x+y a = x+y t1 = a b = a+z b = a+z c = a t2 = b a = b c = a a = b Kostis Sagonas 21 Spring 2006 Kostis Sagonas 22 Spring 2006 Basic Block After Basic Block After CSE and Copy Prop CSE and Copy Prop a = x+y a = x+y t1 = a t1 = a b = a+z b = a+z t2 = b t2 = b c = a c = a a = b a = b Assume that initially Needed Set Needed Set {a, c} {a, b, c} Kostis Sagonas 23 Spring 2006 Kostis Sagonas 24 Spring 2006 Basic Block After Basic Block After CSE and Copy Prop CSE and Copy Prop a = x+y a = x+y t1 = a t1 = a b = a+z b = a+z t2 = b c = a c = a a = b a = b Needed Set Needed Set {a, b, c} {a, b, c} Kostis Sagonas 25 Spring 2006 Kostis Sagonas 26 Spring 2006 Basic Block After Basic Block After CSE and Copy Prop CSE and Copy Prop a = x+y a = x+y t1 = a t1 = a b = a+z b = a+z c = a c = a a = b a = b Needed Set Needed Set {a, b, c, z} {a, b, c, z} Kostis Sagonas 27 Spring 2006 Kostis Sagonas 28 Spring 2006 Basic Block After Basic Block after CSE Copy Propagation CSE and Copy Prop and Dead Code Elimination a = x+y a = x+y b = a+z b = a+z c = a c = a a = b a = b Needed Set Needed Set {a, b, c, z} {a, b, c, z} Kostis Sagonas 29 Spring 2006 Kostis Sagonas 30 Spring 2006 Basic Block after Interesting Properties CSE + Copy Propagation + Dead Code Elimination a = x+y • Analysis and Optimization Algorithms b = a+z Simulate Execution of Program – CSE and Copy Propagation go forward c = a – Dead Code Elimination goes backwards a = b • Optimizations are stacked Needed Set – Group of basic transformations {a, b, c, z} – Work together to get good result – Often, one transformation creates inefficient code that is cleaned up by subsequent transformations Kostis Sagonas 31 Spring 2006 Kostis Sagonas 32 Spring 2006 Other Basic Block Transformations Dataflow Analysis • Constant Propagation • Used to determine properties of programs that • Strength Reduction involve multiple basic blocks – a << 2 = a * 4; • Typically used to enable transformations – a + a + a = 3 * a; – common sub-expression elimination • Algebraic Simplification – constant and copy propagation – a = a * 1; – dead code elimination – b = b + 0; • Analysis and transformation often come in pairs • Unified transformation framework Kostis Sagonas 33 Spring 2006 Kostis Sagonas 34 Spring 2006 Reaching Definitions Reaching Definitions • Concept of definition and use s = 0; a = 4; – z = x+y i = 0; – is a definition of z k == 0 – is a use of x and y • A definition reaches a use if b = 1; b = 2; – value written by definition i < n – may be read by use s = s + a*b; i = i + 1; return s Kostis Sagonas 35 Spring 2006 Kostis Sagonas 36 Spring 2006 Reaching Definitions and Constant Is a constant in s = s+a*b? Propagation • Is a use of a variable a constant? s = 0; Yes! – Check all reaching definitions a = 4; i = 0; On all reaching – If all assign variable to same constant k == 0 definitions – Then use is in fact a constant a = 4 • Can replace variable with constant b = 1; b = 2; i < n s = s + a*b; i = i + 1; return s Kostis Sagonas 37 Spring 2006 Kostis Sagonas 38 Spring 2006 Constant Propagation Transform Is b constant in s = s+a*b? s = 0; Yes! s = 0; No! a = 4; a = 4; i = 0; On all reaching i = 0; One reaching k == 0 definitions k == 0 definition with a = 4 b = 1 b = 1; b = 2; b = 1; b = 2; One reaching definition with i < n i < n b = 2 s = s + 4*b; s = s + a*b; i = i + 1; return s i = i + 1; return s Kostis Sagonas 39 Spring

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us