Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.

Slides:



Advertisements
Similar presentations
8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
Advertisements

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
Lecture 11: Code Optimization CS 540 George Mason University.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Programming Languages and Paradigms
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
1 Compiler Construction Intermediate Code Generation.
The Assembly Language Level
Program Representations. Representing programs Goals.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
CS 536 Spring Global Optimizations Lecture 23.
Peephole Optimization Final pass over generated code: examine a few consecutive instructions: 2 to 4 See if an obvious replacement is possible: store/load.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
CS 536 Spring Code generation I Lecture 20.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
4/18/08Prof. Hilfinger CS 164 Lecture 341 Other Forms of Intermediate Code. Local Optimizations Lecture 34 (Adapted from notes by R. Bodik and G. Necula)
Register Allocation (via graph coloring)
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Intermediate Code. Local Optimizations
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
What’s in an optimizing compiler?
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Compiler Chapter# 5 Intermediate code generation.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
3/2/2016© Hal Perkins & UW CSES-1 CSE P 501 – Compilers Optimizing Transformations Hal Perkins Autumn 2009.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
More Code Generation and Optimization Pat Morin COMP 3002.
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Code Optimization Overview and Examples
High-level optimization Jakub Yaghob
Code Optimization.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Optimization Code Optimization ©SoftMoore Consulting.
Optimizing Transformations Hal Perkins Autumn 2011
Lecture 30 (based on slides by R. Bodik)
Code Optimization Overview and Examples Control Flow Graph
Data Flow Analysis Compiler Design
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Code Generation Part II
Presentation transcript:

Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21

Prof. Fateman CS164 Lecture 212 Lecture Outline Local optimization Next time: global optimizations

Prof. Fateman CS164 Lecture 213 Code Generation Summary We have discussed –Runtime organization –Simple stack machine code generation Our compiler goes directly from AST to assembly language with a brief stop or two –If we preserved environment data from typecheck, use that; –cleanup other minor loose ends perhaps. –Simple-compile.lisp does not perform optimizations Most real compilers use some optimization somewhere (history of Fortran I..)

Prof. Fateman CS164 Lecture 214 When to perform optimizations –On AST Pro: Machine independent Con: Too high level –On assembly language Pro: Exposes more optimization opportunities Con: Machine dependent Con: Must reimplement optimizations when retargetting –On an intermediate language between AST and assembler Pro: Machine independent Pro: Exposes many optimization opportunities

Prof. Fateman CS164 Lecture 215 Intermediate Languages for Optimization Each compiler uses its own intermediate language –IL design is still an active area of research Intermediate language = high-level assembly language –Uses register names, but has an unlimited number –Uses control structures like assembly language –Uses opcodes but some are higher level E.g., push may translate to several assembly instructions Perhaps some opcodes correspond directly to assembly opcodes Usually not stack oriented.

Prof. Fateman CS164 Lecture 216 Texts often consider optimizing based on Three-Address Intermediate Code Computations are reduced to simple forms like x := y op z [3 addresses] or maybe x := op y –y and z can be only registers or constants (not expressions!) –Also need control flow test/jump/call/ New variables are generated, perhaps to be used only once (SSA= static single assignment) The expression x + y * z is translated as t 1 := y * z t 2 := x + t 1 –Each subexpression then has a “home” for its value

Prof. Fateman CS164 Lecture 217 How hard to generate this kind of Intermediate Code? Similar technique to our assembly code generation Major differences –Use any number of IL registers to hold intermediate results –Not stack oriented Same compiler organization..

Prof. Fateman CS164 Lecture 218 Generating Intermediate Code (Cont.) Igen(e, t) function generates code to compute the value of e in register t Example: igen(e 1 + e 2, t) = igen(e 1, t 1 ) ;(t 1 is a fresh register) igen(e 2, t 2 ) ;(t 2 is a fresh register) t := t 1 + t 2 ;(instead of “+”) Unlimited number of registers  simple code generation

Prof. Fateman CS164 Lecture 219 We can define an Intermediate Language formally, too... P  S ; P |  S  id := id op id | id := op id | id := id | push id | id := pop | if id relop id goto L | L: | jump L id’s are register names Constants can replace id’s Typical operators: +, -, *

Prof. Fateman CS164 Lecture 2110 Optimization Concepts Inside Basic Blocks Between/Around Basic Blocks: Control Flow Graphs

Prof. Fateman CS164 Lecture 2111 Definition. Basic Blocks A basic block is a maximal sequence of instructions with: –no labels (except at the first instruction), and –no jumps (except in the last instruction) Idea: –Cannot jump into a basic block (except at beginning) –Cannot jump out of a basic block (except at end) –Each instruction in a basic block is executed after all the preceding instructions have been executed

Prof. Fateman CS164 Lecture 2112 Basic Block Example Consider the basic block 1.L: 2. t := 2 * x 3. w := t + x 4. if w > 0 goto L No way for (3) to be executed without (2) having been executed right before –We can change (3) to w := 3 * x –Can we eliminate (2) as well?

Prof. Fateman CS164 Lecture 2113 Definition. Control-Flow Graphs A control-flow graph is a directed graph with –Basic blocks as nodes –An edge from block A to block B if the execution can flow from the last instruction in A to the first instruction in B –E.g., the last instruction in A is jump L B –E.g., the execution can fall through from block A to block B Frequently abbreviated as CFG... too bad we already used this..

Prof. Fateman CS164 Lecture 2114 Control-Flow Graphs. Example. The body of a method (or procedure) can be represented as a control- flow graph There is one initial node All “return” nodes are terminal x := 1 i := 1 L: x := x * x i := i + 1 if i < 10 goto L

Prof. Fateman CS164 Lecture 2115 Optimization Overview Optimization seeks to improve a program’s utilization of some resource –Execution time (most often) [instructions, memory access] –Code size –Network messages sent, –Battery power used, etc. Optimization should not alter what the program computes –The answers must still be the same (* sometimes relaxed for floating point numbers… a bad idea, though) –Same behavior on bad input (?) e.g. array bounds?

Prof. Fateman CS164 Lecture 2116 A Classification of Optimizations For languages like Java there are three granularities of optimizations 1.Local optimizations Apply to a basic block in isolation 2.Global optimizations Apply to a control-flow graph (function body) in isolation 3.Inter-procedural optimizations Apply across call boundaries Most compilers do (1), many do (2) and very few do (3)

Prof. Fateman CS164 Lecture 2117 Cost of Optimizations In practice, a conscious decision is often not to implement the fanciest optimization known Why? –Some optimizations are hard to implement. Programs are tricky to write/debug –Some optimizations are costly in terms of compilation time. Even exponential time O(2 s ), for program of size s. –Some fancy optimizations are both hard and costly! Depends on goal: –maximum improvement with acceptable cost / debuggability –vs. beat competitive benchmarks

Prof. Fateman CS164 Lecture 2118 Local Optimizations The simplest form of optimizations No need to analyze the whole procedure body –Just the basic block in question Example: algebraic simplification

Prof. Fateman CS164 Lecture 2119 Algebraic Simplification Some statements can be deleted x := x + 0 x := x * 1 Some statements can be simplified x := x * 0  x := 0 ;;x not “infinity” or NaN y := y ^ 2  y := y * y x := x * 8  x := x << 3 x := x * 15  t := x << 4; x := t - x (on some machines << is faster than *; but not on all!)

Prof. Fateman CS164 Lecture 2120 Constant Folding Operations on constants can be computed at compile time In general, if there is a statement x := y op z –And y and z are constants (and op has no side effects) –Then y op z can be computed at compile time [if you are computing on the same machine, at least. Eg. 32 vs 64 bit?] Example: x :=  x := 4 Example: if 2 < 0 jump L can be deleted When might constant folding be dangerous? Why would anyone write such stupid code?

Prof. Fateman CS164 Lecture 2121 Flow of Control Optimizations Eliminating unreachable code: –Code that is unreachable in the control-flow graph –Basic blocks that are not the target of any jump or “fall through” from a conditional –Such basic blocks can be eliminated Why would such basic blocks occur? Removing unreachable code makes the program smaller –And sometimes also faster Due to memory cache effects (increased spatial locality)

Prof. Fateman CS164 Lecture 2122 Using (Static) Single Assignment Form SSA Some optimizations are simplified if each register occurs only once on the left-hand side of an assignment Intermediate code can be rewritten to be in single assignment form x := z + y b := z + y a := x  a := b x := 2 * x x := 2 * b (b is a fresh register) More complicated in general, due to loops

Prof. Fateman CS164 Lecture 2123 Common Subexpression Elimination Assume –Basic block is in single assignment form –A definition x := is the first use of x in a block All assignments with same rhs compute the same value Example: x := y + z …  … w := y + z w := x (the values of x, y, and z do not change in the … code)

Prof. Fateman CS164 Lecture 2124 Copy Propagation If w := x appears in a block, all subsequent uses of w can be replaced with uses of x Example: b := z + y b := z + y a := b  a := b x := 2 * a x := 2 * b This does not make the program smaller or faster but might enable other optimizations –Constant folding –Dead code elimination

Prof. Fateman CS164 Lecture 2125 Copy Propagation and Constant Folding Example: a := 5 x := 2 * a  x := 10 y := x + 6 y := 16 t := x * y t := x << 4

Prof. Fateman CS164 Lecture 2126 Copy Propagation and Dead Code Elimination If w := rhs appears (in a basic block) w does not appear anywhere else in the program Then the statement w := rhs is dead and can be eliminated –Dead = does not contribute to the program’s result Example: (a is not used anywhere else) x := z + y b := z + y b := z + y a := x  a := b  x := 2 * b x := 2 * x x := 2 * b

Prof. Fateman CS164 Lecture 2127 Applying Local Optimizations Each local optimization does very little by itself Often the optimization seems silly “who would write code like that?” Answer: the optimizer, in a previous step! That is: typically optimizations interact so that performing one optimization enables other opts. Typical optimizing compilers repeatedly perform optimizations until no more improvement is produced. The optimizer can also be stopped at any time to limit the compilation time

Prof. Fateman CS164 Lecture 2128 An Example Initial code: a := x ^ 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2129 An Example Algebraic optimization: a := x ^ 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2130 An Example Algebraic optimization: a := x * x b := 3 c := x d := c * c e := b << 1 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2131 An Example Copy propagation: a := x * x b := 3 c := x d := c * c e := b << 1 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2132 An Example Copy propagation: a := x * x b := 3 c := x d := x * x e := 3 << 1 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2133 An Example Constant folding: a := x * x b := 3 c := x d := x * x e := 3 << 1 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2134 An Example Constant folding: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2135 An Example Common subexpression elimination: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2136 An Example Common subexpression elimination: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2137 An Example Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f

Prof. Fateman CS164 Lecture 2138 An Example Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f

Prof. Fateman CS164 Lecture 2139 An Example Dead code elimination: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f

Prof. Fateman CS164 Lecture 2140 An Example Dead code elimination: a := x * x f := a + a g := 6 * f This is the final form

Prof. Fateman CS164 Lecture 2141 Peephole Optimizations on Assembly Code The optimizations presented before work on intermediate code –They are target independent –But they can be applied on assembly language also Peephole optimization is an effective technique for improving assembly code –The “peephole” is a short sequence of (usually contiguous) instructions –The optimizer replaces the sequence with another equivalent one (but faster)

Prof. Fateman CS164 Lecture 2142 Peephole Optimizations (Cont.) Write peephole optimizations as replacement rules i 1, …, i n  j 1, …, j m where the rhs is the improved version of the lhs Example: move $a $b, move $b $a  move $a $b –Works if move $b $a is not the target of a jump Another example addiu $a $a i, addiu $a $a j  addiu $a $a i+j

Prof. Fateman CS164 Lecture 2143 Peephole Optimizations (Cont.) Many (but not all) of the basic block optimizations can be cast as peephole optimizations –Example: addiu $a $b 0  move $a $b –Example: move $a $a  –These two together eliminate addiu $a $a 0 Just as with other local optimizations, peephole optimizations need to be applied repeatedly to get maximum effect

Prof. Fateman CS164 Lecture 2144 Local Optimizations. Notes. Intermediate code is helpful for many optimizations Many simple optimizations can still be applied on assembly language “Program optimization” is grossly misnamed –Code produced by “optimizers” is not optimal in any reasonable sense –“Program improvement” is a more appropriate term Next time: global optimizations