4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
Intermediate Code Generation
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.
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral
1 CS 201 Compiler Construction Machine Code Generation.
Jeffrey D. Ullman Stanford University. 2  A never-published Stanford technical report by Fran Allen in  Fran won the Turing award in  Flow.
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.
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.
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.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
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.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
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?
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.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Register Allocation John Cavazos University.
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.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
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.
©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.
Unit IV Code Generation
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
CSc 453 Final Code Generation
Code Optimization.
Presentation transcript:

4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)

4/23/09Prof. Hilfinger CS 164 Lecture 262 Multi-dimensional Arrays A 2D array is a 1D array of 1D arrays Java uses arrays of pointers to arrays for >1D arrays. But if row size constant, for faster access and compactness, may prefer to represent an MxN array as a 1D array of 1D rows (not pointers to rows): row-major order FORTRAN layout is 1D array of 1D columns: column-major order.

4/23/09Prof. Hilfinger CS 164 Lecture 263 IL for 2D Arrays (Row-Major Order) Again, let S be size of one element, so that a row of length N has size NxS. igen(e 1 [e 2,e 3 ], t) = igen(e 1, t 1 ); igen(e 2,t 2 ); igen(e 3,t 3 ) igen(N, t 4 ) (N need not be constant) t 5 := t 4 * t 2 ; t 6 := t 5 + t 3 ; t 7 := t 6 *S; t 8 := t 7 + t 1 t := *t 8

4/23/09Prof. Hilfinger CS 164 Lecture 264 Array Descriptors Calculation of element address for e 1 [e 2,e 3 ] has form VO + S 1 x e 2 + S 2 x e 3, where –VO (address of e 1 [0,0]) is the virtual origin –S 1 and S 2 are strides –All three of these are constant throughout lifetime of array Common to package these up into an array descriptor, which can be passed in lieu of the array itself.

4/23/09Prof. Hilfinger CS 164 Lecture 265 Array Descriptors (II) By judicious choice of descriptor values, can make the same formula work for different kinds of array. For example, if lower bounds of indices are 1 rather than 0, must compute address of e[1,1] + S 1 x (e 2 -1) + S 2 x (e 3 -1) But some algebra puts this into the form VO + S 1 x e 2 + S 2 x e 3 where VO = address of e[1,1] - S 1 - S 2

4/23/09Prof. Hilfinger CS 164 Lecture 266 Observation These examples show profligate use of registers. Doesn’t matter, because this is Intermediate Code. Rely on later optimization stages to do the right thing.

4/23/09Prof. Hilfinger CS 164 Lecture 267 Code Optimization: Basic Concepts

4/23/09Prof. Hilfinger CS 164 Lecture 268 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 in 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

4/23/09Prof. Hilfinger CS 164 Lecture 269 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?

4/23/09Prof. Hilfinger CS 164 Lecture 2610 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

4/23/09Prof. Hilfinger CS 164 Lecture 2611 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

4/23/09Prof. Hilfinger CS 164 Lecture 2612 Optimization Overview Optimization seeks to improve a program’s utilization of some resource –Execution time (most often) –Code size –Network messages sent –Battery power used, etc. Optimization should not alter what the program computes –The answer must still be the same

4/23/09Prof. Hilfinger CS 164 Lecture 2613 A Classification of Optimizations For languages like C and Cool 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 (method body) in isolation 3.Inter-procedural optimizations Apply across method boundaries Most compilers do (1), many do (2) and very few do (3)

4/23/09Prof. Hilfinger CS 164 Lecture 2614 Cost of Optimizations In practice, a conscious decision is made not to implement the fanciest optimization known Why? –Some optimizations are hard to implement –Some optimizations are costly in terms of compilation time –The fancy optimizations are both hard and costly The goal: maximum improvement with minimum of cost

4/23/09Prof. Hilfinger CS 164 Lecture 2615 Local Optimizations The simplest form of optimizations No need to analyze the whole procedure body –Just the basic block in question Example: algebraic simplification

4/23/09Prof. Hilfinger CS 164 Lecture 2616 Algebraic Simplification Some statements can be deleted x := x + 0 x := x * 1 Some statements can be simplified x := x * 0  x := 0 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!)

4/23/09Prof. Hilfinger CS 164 Lecture 2617 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 –Then y op z can be computed at compile time Example: x :=  x := 4 Example: if 2 < 0 jump L can be deleted When might constant folding be dangerous?

4/23/09Prof. Hilfinger CS 164 Lecture 2618 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)

4/23/09Prof. Hilfinger CS 164 Lecture 2619 Single Assignment Form Some optimizations are simplified if each assignment is to a temporary that has not appeared already in the basic block Intermediate code can be rewritten to be in single assignment form x := a + y a := x  a 1 := x x := a * x x 1 := a 1 * x b := x + a b := x 1 + a 1 (x 1 and a 1 are fresh temporaries)

4/23/09Prof. Hilfinger CS 164 Lecture 2620 Common Subexpression Elimination Assume –Basic block is in single assignment form All assignments with same rhs compute the same value Example: x := y + z …  … w := y + z w := x Why is single assignment important here?

4/23/09Prof. Hilfinger CS 164 Lecture 2621 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 Again, single assignment is important here.

4/23/09Prof. Hilfinger CS 164 Lecture 2622 Copy Propagation and Constant Folding Example: a := 5 x := 2 * a  x := 10 y := x + 6 y := 16 t := x * y t := x << 4

4/23/09Prof. Hilfinger CS 164 Lecture 2623 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 * a x := 2 * b

4/23/09Prof. Hilfinger CS 164 Lecture 2624 Applying Local Optimizations Each local optimization does very little by itself Typically optimizations interact –Performing one optimizations enables other opt. Typical optimizing compilers repeatedly perform optimizations until no improvement is possible –The optimizer can also be stopped at any time to limit the compilation time

4/23/09Prof. Hilfinger CS 164 Lecture 2625 An Example Initial code: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2626 An Example Algebraic optimization: a := x ** 2 b := 3 c := x d := c * c e := b * 2 f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2627 An Example Algebraic optimization: a := x * x b := 3 c := x d := c * c e := b + b f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2628 An Example Copy propagation: a := x * x b := 3 c := x d := c * c e := b + b f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2629 An Example Copy propagation: a := x * x b := 3 c := x d := x * x e := f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2630 An Example Constant folding: a := x * x b := 3 c := x d := x * x e := f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2631 An Example Constant folding: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2632 An Example Common subexpression elimination: a := x * x b := 3 c := x d := x * x e := 6 f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2633 An Example Common subexpression elimination: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2634 An Example Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + d g := e * f

4/23/09Prof. Hilfinger CS 164 Lecture 2635 An Example Copy propagation: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f

4/23/09Prof. Hilfinger CS 164 Lecture 2636 An Example Dead code elimination: a := x * x b := 3 c := x d := a e := 6 f := a + a g := 6 * f

4/23/09Prof. Hilfinger CS 164 Lecture 2637 An Example Dead code elimination: a := x * x f := a + a g := 6 * f This is the final form

4/23/09Prof. Hilfinger CS 164 Lecture 2638 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 (but faster) one

4/23/09Prof. Hilfinger CS 164 Lecture 2639 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 Examples: move $a $b, move $b $a  move $a $b –Works if move $b $a is not the target of a jump addiu $a $b k, lw $c ($a)  lw $c k($b) - Works if $a not used later (is “dead”)

4/23/09Prof. Hilfinger CS 164 Lecture 2640 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 like for local optimizations, peephole optimizations need to be applied repeatedly to get maximum effect

4/23/09Prof. Hilfinger CS 164 Lecture 2641 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

4/23/09Prof. Hilfinger CS 164 Lecture 2642 Local Optimizations. Notes (II). Serious problem: what to do with pointers? –*t may change even if local variable t does not: Aliasing –Arrays are a special case (address calculation) What to do about globals? What to do about calls? –Not exactly jumps, because they (almost) always return. –Can modify variables used by caller Next: global optimizations