analysis and optimizations
play

Analysis and Optimizations Program Analysis P3 / 2006 Discovers - PDF document

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


  1. 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 s = 0; a = 4; i = 0; if (k == 0) b = 1; 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 } s = s + a*b; – Includes all instructions in basic block return s; 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; – t = x+y; a = t+z; b = t; – Introduced by optimizations/transformations • Constant Propagation • Dead Code Elimination • Program variables – 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; 5 6 Kostis Sagonas Spring 2006 Kostis Sagonas Spring 2006

  2. Value Numbering Value Numbering for CSE • Normalize basic block so that all statements are • As we simulate execution of program of the form • Generate a new version of program – var = var op var (where op is a binary operator) – Each new value assigned to temporary – var = op var (where op is a unary operator) • a = x+y; becomes a = x+y; t = a; – var = var – Temporary preserves value for use later in program • Simulate execution of basic block 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 t1 = a • Original • After CSE b = a+z b = a+z a = x+y a = x+y b = b+y t2 = b c = a+z b = a+z b = a+z b = b+y t3 = b b = b+y t = b c = t2 Var to Val c = a+z b = b+y x � v1 c = t • Issues y � v2 Exp to Val Exp to Tmp – Temporaries store values for use later a � v3 v1+v2 � v3 v1+v2 � t1 z � v4 v3+v4 � v5 v3+v4 � t2 – CSE with different names b � v5 b � v6 v5+v2 � t3 v5+v2 � v6 • a = x; b = x+y; c = a+y; c � v5 – 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 temporary • Introduces – lots of temporaries – a = x+y; b = x+y; – After CSE becomes a = x+y; t = a; b = t; – lots of copy statements to temporaries – After CP becomes a = x+y; b = a; • In many cases, temporaries and copy statements are unnecessary • Key idea: determine when original variables are NOT overwritten between computation of stored • So we eliminate them with copy propagation value and use of stored value and dead code elimination 11 12 Kostis Sagonas Spring 2006 Kostis Sagonas Spring 2006

  3. Copy Propagation Maps Copy Propagation Example • Maintain two maps After CSE and After CSE Original Copy Propagation – tmp to var: tells which variable to use instead of a a = x+y a = x+y given temporary variable a = x+y t1 = a t1 = a b = a+z – var to set (inverse of tmp to var): tells which temps b = a+z b = a+z c = x+y are mapped to a given variable by tmp to var t2 = b t2 = b a = 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 After Basic Block After Basic Block Basic Block 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 CSE and Copy Prop CSE and Copy Prop After CSE After CSE 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} 17 18 Kostis Sagonas Spring 2006 Kostis Sagonas Spring 2006

  4. 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 = a c = a c = t1 c = t1 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 – Process code in reverse execution order • There may be temps that are never read – Maintain a set of variables that are needed later in • Dead Code Elimination (DCE) removes them 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} 23 24 Kostis Sagonas Spring 2006 Kostis Sagonas Spring 2006

  5. 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} 29 30 Kostis Sagonas Spring 2006 Kostis Sagonas Spring 2006

  6. Basic Block after Interesting Properties CSE + Copy Propagation + Dead Code Elimination a = x+y • Analysis and Optimization Algorithms Simulate Execution of Program b = a+z – 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 involve multiple basic blocks • Strength Reduction • Typically used to enable transformations – a << 2 = a * 4; – common sub-expression elimination – a + a + a = 3 * a; – constant and copy propagation • Algebraic Simplification – dead code elimination – a = a * 1; • Analysis and transformation often come in pairs – b = b + 0; • 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 b = 1; b = 2; • A definition reaches a use if – value written by definition i < n – may be read by use s = s + a*b; return s i = i + 1; 35 36 Kostis Sagonas Spring 2006 Kostis Sagonas Spring 2006

Recommend


More recommend