analysis and optimizations analysis and optimizations
play

Analysis and Optimizations Analysis and Optimizations Program - PowerPoint PPT Presentation

Analysis and Optimizations Analysis and Optimizations Program Analysis Program Analysis Discovers properties of a program Optimizations Using Program Analysis Use analysis results to transform program Use analysis results


  1. Analysis and Optimizations Analysis and Optimizations • Program Analysis • Program Analysis – Discovers properties of a program • Optimizations Using Program Analysis – Use analysis results to transform program Use analysis results to transform program for Optimization – Goal: improve some aspect of program • number of executed instructions, number of cycles b f d i i b f l • cache hit rate • memory space (code or data) ( d d ) • power consumption – Has to be safe: Keep the semantics of the program Control Flow Graph Control Flow Graph Control Flow Graph Control Flow Graph entry int add(n, k) { int add(n k) { • Nodes represent computation • 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; 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) { 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 i + 1; i < n i – Execution of basic block starts with first instruction } – Includes all instructions in basic block Includes all instructions in basic block s = s + a*b; s = s + a*b; return s; t return s i = i + 1; } • Edges represent control flow

  2. Two Kinds of Variables Two Kinds of Variables Basic Block Optimizations Basic Block Optimizations • Temporaries introduced by the compiler • 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; y – 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 Introduced by optimizations/transformations – t = x+y; a = t+z; b = t; y; ; ; • Constant Propagation • Dead Code Elimination • Program variables – x = 5; b = x+y; x 5; b x+y; – a = x+y; b = a; c = a+z; a x+y; b a; c a+z; – Declared in original program l d i i i l – x = 5; b = 5+y; – a = x+y; c = a+z – May transfer values between basic blocks • Algebraic Simplification • Algebraic Simplification • Strength Reduction • Strength Reduction – a = x * 1; – t = i * 4; – a = x; – t = i << 2; t i << 2 Value Numbering Value Numbering Value Numbering for CSE Value Numbering for CSE • Normalize basic block so that all statements are • As we simulate execution of program • As we simulate execution of program of the form f h f • Generate a new version of program – var = var op var (where op is a binary operator) p ( p y p ) – Each new value assigned to temporary – var = op var (where op is a unary operator) • a = x+y; becomes a = x+y; t = a; a x y; becomes a x y; t a; – var = var var = var – Temporary preserves value for use later in program • Simulate execution of basic block even if the original variable is rewritten even if the original variable is 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 Assign a virtual value to each expression • a = x+y; t = a; a = a+z; b = t; • a = x+y; t = a; a = a+z; b = t; – Assign a temporary variable to hold value of each computed expression computed expression

  3. New Basic CSE Example CSE Example Original Basic g Block Block Block a = x+y a = x+y t1 = a t1 a • Original • Original • After CSE • After CSE b = a+z b = a+z a = x+y a = x+y b = b+y t2 = b c a+z c = a+z b b = a+z + b = a+z b + b = b+y b b t3 = b b = b+y t = b c t2 c = t2 Var to Val Var to Val c = a+z + b = b+y b b+ x → v1 c = t • Issues y → v2 y → v2 Exp to Val Exp to Val Exp to Tmp Exp to Tmp – Temporaries store values for use later a → v3 v1+v2 → v3 v1+v2 → t1 z → v4 v3+v4 → v5 v3+v4 → v5 v3+v4 → t2 v3+v4 → t2 – CSE with different names 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 Excessive Temp Generation and Use Problems Problems Copy Propagation Copy Propagation • Algorithm has a temporary for each new value • Algorithm has a temporary for each new value • Once again simulate execution of program • Once again, simulate execution of program – a = x+y; t1 = a • If possible, use the original variable instead of a temporary • Introduces – a = x+y; b = x+y; a x+y; b x+y; – lots of temporaries lots of temporaries – After CSE becomes a = x+y; t = a; b = t; – lots of copy statements to temporaries – After CP becomes a = x+y; b = a; Af CP b b • In many cases, temporaries and copy statements are unnecessary • Key idea: determine when original variables are y g NOT overwritten between computation of stored • So we eliminate them with copy propagation value and use of stored value value and use of stored value and dead code elimination and dead code elimination

  4. Copy Propagation Maps Copy Propagation Maps Copy Propagation Example Copy Propagation Example • Maintain two maps • Maintain two maps After CSE and After CSE and After CSE After CSE Original O i i l Copy Propagation – tmp to var: tells which variable to use instead of a a = x+y 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 b 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 t1 c = a a = b a = b Copy Propagation Example Copy Propagation Example Copy Propagation Example Copy Propagation Example Basic Block After Basic Block After Basic Block After Basic Block After Basic Block Basic Block Basic Block Basic Block 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 tmp to var var to set tmp to var var to set t1 → a a → {t1} t1 → a a → {t1} t2 → b b → {t2}

  5. Copy Propagation Example Copy Propagation Example Copy Propagation Example Copy Propagation Example Basic Block After Basic Block After Basic Block After Basic Block After Basic Block Basic Block Basic Block Basic Block 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 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} Copy Propagation Example Copy Propagation Example Copy Propagation Example Copy Propagation Example Basic Block After Basic Block After Basic Block After Basic Block After Basic Block Basic Block Basic Block Basic Block 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 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}

  6. Dead Code Elimination Dead Code Elimination Dead Code Elimination Dead Code Elimination • Copy propagation keeps all temps around • Copy propagation keeps all temps around • Basic Idea • 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 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 Basic Block After Basic Block After CSE and Copy Prop CSE and Copy Prop a = x+y a = x+y 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 b a = b b Assume that initially Assume that initially Needed Set Needed Set Needed Set Needed Set {a, c} {a, b, c}

  7. Basic Block After Basic Block After CSE and Copy Prop CSE and Copy Prop a = x+y a = x+y a = x+y a = x+y t1 = a t1 = a b = a+z b = a+z t2 = b c = a c = a a = b b a = b b Needed Set Needed Set Needed Set Needed Set {a, b, c} {a, b, c} Basic Block After Basic Block After CSE and Copy Prop CSE and Copy Prop a = x+y a = x+y a = x+y a = x+y t1 = a t1 = a b = a+z b = a+z c = a c = a a = b b a = b b Needed Set Needed Set Needed Set Needed Set {a, b, c, z} {a, b, c, z}

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend