Tour of common optimizations

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Code Generation & Optimization.
Advertisements

Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
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.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
19 Classic Examples of Local and Global Code Optimizations Local Constant folding Constant combining Strength reduction.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Computer Architecture Lecture 7 Compiler Considerations and Optimizations.
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.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Recap from last time We saw various different issues related to program analysis and program transformations You were not expected to know all of these.
Feedback: Keep, Quit, Start
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
Administrative info Subscribe to the class mailing list –instructions are on the class web page, click on “Course Syllabus” –if you don’t subscribe by.
Administrative info Subscribe to the class mailing list –instructions are on the class web page, which is accessible from my home page, which is accessible.
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
X := 11; if (x == 11) { DoSomething(); } else { DoSomethingElse(); x := x + 1; } y := x; // value of y? Phase ordering problem Optimizations can interact.
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
Introduction to Program Optimizations Chapter 11 Mooly Sagiv.
Optimization Compiler Optimization – optimizes the speed of compilation Execution Optimization – optimizes the speed of execution.
From last class: CSE Want to compute when an expression is available in a var Domain:
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Code Optimization Witawas Srisa-an CSCE 496: Embedded Systems Design and Implementation.
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Recap from last time We saw various different issues related to program analysis and program transformations You were not expected to know all of these.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Schedule Midterm out tomorrow, due by next Monday Final during finals week Project updates next week.
Recap from last time We saw various different issues related to program analysis and program transformations You were not expected to know all of these.
Optimizing Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Chapter 1 Algorithm Analysis
Compiler Code Optimizations. Introduction Introduction Optimized codeOptimized code Executes faster Executes faster efficient memory usage efficient memory.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Code Optimization. 2 Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.2 ~ 5.6.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
High-Level Transformations for Embedded Computing
Lecture 3 Algorithm Analysis. Motivation Which algorithm to use?
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
1 Code Optimization. 2 Outline Machine-Independent Optimization –Code motion –Memory optimization Suggested reading –5.2 ~ 5.6.
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.
19 March More on Sorting CSE 2011 Winter 2011.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Intro to Optimization CS 671 February 28, CS 671 – Spring The Back End At this point we could generate machine code Output of lowering is.
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.
Control Flow (Chapter 3)
CS 3114 Many of the following slides are taken with permission from
Princeton University Spring 2016
Fall Compiler Principles Lecture 8: Loop Optimizations
Code Generation Part III
Optimizing Transformations Hal Perkins Autumn 2011
Optimizing Transformations Hal Perkins Winter 2008
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Fall Compiler Principles Lecture 10: Loop Optimizations
Why did the programmer quit his job?
Intermediate Code Generation
Loop Optimization “Programs spend 90% of time in loops”
Tour of common optimizations
CS 3114 Many of the following slides are taken with permission from
Tour of common optimizations
Presentation transcript:

Tour of common optimizations

Simple example foo(z) { x := 3 + 6; y := x – 5 return z * y }

Simple example foo(z) { x := 3 + 6; y := x – 5 return z * y }

Another example x := a + b; ... y := a + b;

Another example x := a + b; ... y := a + b;

Another example if (...) { a := read(); x := a + b; print(x); } ... y := a + b;

Another example if (...) { a := read(); x := a + b; print(x); } ... y := a + b;

Another example x := y ... z := z + x

Another example x := y ... z := z + x

Another example x := y ... z := z + y What if we run CSE now?

Another example x := y ... z := z + y What if we run CSE now?

Another example x := y**z ... x := ...

Another example Often used as a clean-up pass x := y**z ... x := ... Copy prop DAE x := y z := z + x x := y z := z + y x := y z := z + y

Another example if (false) { ... }

Another example if (false) { ... }

Another example In Java: a = new int [10]; for (index = 0; index < 10; index ++) { a[index] = 100; }

Another example In “lowered” Java: a = new int [10]; for (index = 0; index < 10; index ++) { if (index < 0 || index >= a.length()) { throw OutOfBoundsException; } a[index] = 0;

Another example In “lowered” Java: a = new int [10]; for (index = 0; index < 10; index ++) { if (index < 0 || index >= a.length()) { throw OutOfBoundsException; } a[index] = 0;

Another example p := &x; *p := 5 y := x + 1;

Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 ???

Another example for j := 1 to N for i := 1 to M a[i] := a[i] + b[j]

Another example for j := 1 to N for i := 1 to M a[i] := a[i] + b[j]

Another example area(h,w) { return h * w } h := ...; w := 4; a := area(h,w)

Another example area(h,w) { return h * w } h := ...; w := 4; a := area(h,w)

Optimization themes Don’t compute if you don’t have to unused assignment elimination Compute at compile-time if possible constant folding, loop unrolling, inlining Compute it as few times as possible CSE, PRE, PDE, loop invariant code motion Compute it as cheaply as possible strength reduction Enable other optimizations constant and copy prop, pointer analysis Compute it with as little code space as possible unreachable code elimination