1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 34: Code Optimization COMP 144 Programming Language Concepts Spring 2002 Felix.

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:
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.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Computer Architecture Lecture 7 Compiler Considerations and Optimizations.
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
Code optimization: –A transformation to a program to make it run faster and/or take up less space –Optimization should be safe, preserve the meaning of.
Jeffrey D. Ullman Stanford University. 2  A never-published Stanford technical report by Fran Allen in  Fran won the Turing award in  Flow.
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 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 36: Global Optimization, Loop Improvement and Profiling COMP 144 Programming Language.
Introduction to Optimizations
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002.
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
Complexity Analysis (Part I)
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 31: Building a Runnable Program COMP 144 Programming Language Concepts Spring 2002.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 32: The Java Virtual Machine COMP 144 Programming Language Concepts Spring 2002.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Parallelizing Compilers Presented by Yiwei Zhang.
Intermediate Code. Local Optimizations
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
30-Jun-15 Profiling. Optimization Optimization is the process of making a program as fast (or as small) as possible Here’s what the experts say about.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 35: In-line Expansion and Local Optimization COMP 144 Programming Language Concepts.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
Week 2 CS 361: Advanced Data Structures and Algorithms
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.
1 ENERGY 211 / CME 211 Lecture 26 November 19, 2008.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Compilation in More Detail Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Principles of Programming Languages.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
CS 3500 L Performance l Code Complete 2 – Chapters 25/26 and Chapter 7 of K&P l Compare today to 44 years ago – The Burroughs B1700 – circa 1974.
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
1 Performance Issues CIS*2450 Advanced Programming Concepts.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
Programming for Performance CS 740 Oct. 4, 2000 Topics How architecture impacts your programs How (and how not) to tune your code.
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.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Complexity Analysis (Part I)
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
Code Optimization.
Lecture 16: Introduction to Data Types
Optimization Code Optimization ©SoftMoore Consulting.
Princeton University Spring 2016
CSCI1600: Embedded and Real Time Software
Coding Design, Style, Documentation and Optimization
Optimizing Transformations Hal Perkins Autumn 2011
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
Advanced Compiler Techniques
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
8 Code Generation Topics A simple code generator algorithm
An Introduction to Programming with C++ Fifth Edition
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
CSCI1600: Embedded and Real Time Software
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Code Optimization.
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 34: Code Optimization COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos April 17 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos Optimization We will discuss code optimization from the point of view of the compiler and the programmerWe will discuss code optimization from the point of view of the compiler and the programmer Code improvement is an important phase in production compilersCode improvement is an important phase in production compilers –Generate code that runs fast (CPU-oriented) »In some case, optimize for memory requirements or network performance Optimization is an important task when developing resource-intensive applicationOptimization is an important task when developing resource-intensive application –E.g. Games, Large-scale services

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Optimization Two lectures on optimizationTwo lectures on optimization ReferencesReferences –Scott’s Chapter 13 –Doug Bell, Make Java fast: Optimize! » optimize_p.html optimize_p.htmlhttp:// optimize_p.html –HyperProf- Java profile browser » –Python Performance Tips » –Python Patterns - An Optimization Anecdote »

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Golden Rules of Optimization Premature Optimization is Evil Donald Knuth, premature optimization is the root of all evilDonald Knuth, premature optimization is the root of all evil –Optimization can introduce new, subtle bugs –Optimization usually makes code harder to understand and maintain Get your code right first, then, if really needed, optimize itGet your code right first, then, if really needed, optimize it –Document optimizations carefully –Keep the non-optimized version handy, or even as a comment in your code

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Golden Rules of Optimization The 80/20 Rule In general, 80% percent of a program’s execution time is spent executing 20% of the codeIn general, 80% percent of a program’s execution time is spent executing 20% of the code 90%/10% for performance-hungry programs90%/10% for performance-hungry programs Spend your time optimizing the important 10/20% of your programSpend your time optimizing the important 10/20% of your program Optimize the common case even at the cost of making the uncommon case slowerOptimize the common case even at the cost of making the uncommon case slower

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos The Golden Rules of Optimization Good Algorithms Rule The best and most important way of optimizing a program is using good algorithmsThe best and most important way of optimizing a program is using good algorithms –E.g. O(n*log) rather than O(n 2 ) However, we still need lower level optimization to get more of our programsHowever, we still need lower level optimization to get more of our programs In addition, asymthotic complexity is not always an appropriate metric of efficiencyIn addition, asymthotic complexity is not always an appropriate metric of efficiency –Hidden constant may be misleading –E.g. a linear time algorithm than runs in 100*n+100 time is slower than a cubic time algorithm than runs in n time if the problem size is small

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Asymptotic Complexity Hidden Constants

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos General Optimization Techniques Strength reductionStrength reduction –Use the fastest version of an operation –E.g. x >> 2 instead of x / 4 x << 1 instead of x * 2 Common sub expression eliminationCommon sub expression elimination –Eliminate redundant calculations –E.g. double x = d * (lim / max) * sx; double y = d * (lim / max) * sy; double depth = d * (lim / max); double depth = d * (lim / max); double x = depth * sx; double x = depth * sx; double y = depth * sy; double y = depth * sy;

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos General Optimization Techniques Code motionCode motion –Invariant expressions should be executed only once –E.g. for (int i = 0; i < x.length; i++) x[i] *= Math.PI * Math.cos(y); x[i] *= Math.PI * Math.cos(y); double picosy = Math.PI * Math.cos(y); for (int i = 0; i < x.length; i++) x[i] *= picosy; x[i] *= picosy;

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos General Optimization Techniques Loop unrollingLoop unrolling –The overhead of the loop control code can be reduced by executing more than one iteration in the body of the loop –E.g. double picosy = Math.PI * Math.cos(y); for (int i = 0; i < x.length; i++) x[i] *= picosy; x[i] *= picosy; double picosy = Math.PI * Math.cos(y); for (int i = 0; i < x.length; i += 2) { x[i] *= picosy; x[i] *= picosy; x[i+1] *= picosy; x[i+1] *= picosy;} A efficient “+1” in array indexing is required

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Compiler Optimizations Compilers try to generate good codeCompilers try to generate good code –I.e. Fast Code improvement is challengingCode improvement is challenging –Many problems are NP-hard Code improvement may slow down the compilation processCode improvement may slow down the compilation process –In some domains, such as just-in-time compilation, compilation speed is critical

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation The first three phases are language- dependentThe first three phases are language- dependent The last two are machine- dependentThe last two are machine- dependent The middle two dependent on neither the language nor the machineThe middle two dependent on neither the language nor the machine

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Generating highly optimized is a complicated processGenerating highly optimized is a complicated process We will concentrate on execution speed optimizationWe will concentrate on execution speed optimization Phases

14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases

15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Example Control Flow Graph Basic blocks are maximal-length set of sequential operationsBasic blocks are maximal-length set of sequential operations –Operations on a set of virtual registers »Unlimited »A new one for each computed value Arcs represent interblock control flowArcs represent interblock control flow

16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Simple compiler do not perform machine- independent code improvementSimple compiler do not perform machine- independent code improvement –They generates naïve code It is possible to take the target hole and optimize itIt is possible to take the target hole and optimize it –Sub-optimal sequences of instructions that match an optimization pattern are transformed into optimal sequences of instructions –This technique is known as peephole optimization –Peephole optimization usually works by sliding a window of several instructions (a peephole)

17 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Common Techniques

18 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Common Techniques

19 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Common Techniques

20 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Common Techniques

21 COMP 144 Programming Language Concepts Felix Hernandez-Campos Peephole Optimization Peephole optimization is very fastPeephole optimization is very fast –Small overhead per instruction since they use a small, fixed-size window It is often easier to generate naïve code and run peephole optimization than generating good code!It is often easier to generate naïve code and run peephole optimization than generating good code!

22 COMP 144 Programming Language Concepts Felix Hernandez-Campos Reading Assignment Read ScottRead Scott –Ch. 13 intro –Sect –Sect Doug Bell, Make Java fast: Optimize!Doug Bell, Make Java fast: Optimize! – optimize_p.html optimize_p.htmlhttp:// optimize_p.html