Optimization Code Optimization ©SoftMoore Consulting.

Slides:



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

Target Code Generation
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.
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
1 CS 201 Compiler Construction Machine Code Generation.
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.
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-
C Chuen-Liang Chen, NTUCS&IE / 321 OPTIMIZATION Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
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.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
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.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Intermediate Code. Local Optimizations
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Improving Code Generation Honors Compilers April 16 th 2002.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
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.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
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.
Chapter 10 Code Optimization Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
Machine Independent Optimizations Topics Code motion Reduction in strength Common subexpression sharing.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
©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.
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.
Component 1.6.
Code Optimization Overview and Examples
Compiler Design (40-414) Main Text Book:
High-level optimization Jakub Yaghob
Code Optimization.
Compiler Construction (CS-636)
Compiler Construction
Princeton University Spring 2016
CSCI1600: Embedded and Real Time Software
Intermediate Representations
Optimizing Transformations Hal Perkins Autumn 2011
Unit IV Code Generation
Intermediate Representations
Optimizing Transformations Hal Perkins Winter 2008
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Chapter 12 Pipelining and RISC
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Lecture 19: Code Optimisation
Compiler Construction
Code Generation Part II
Target Code Generation
CSCI1600: Embedded and Real Time Software
Complexity Analysis (Part I)
CMPE 152: Compiler Design April 30 Class Meeting
Complexity Analysis (Part I)
Code Optimization.
Presentation transcript:

Optimization Code Optimization ©SoftMoore Consulting

Optimization Code Optimization Code generation techniques and transformations that result in a semantically equivalent program that runs more efficiently faster uses less memory or both Often involves a time-space tradeoff. Techniques that make the code faster often require additional memory, and conversely Term “optimization” is actually used improperly generated code is rarely optimal better name might be “code improvements” ©SoftMoore Consulting

Code Optimization (continued) Optimizing compilers May be performed on intermediate representations of the program high level representation such as abstract syntax trees machine code or a low-level representation Local versus global optimizations (DEC Ada PL/I story) Machine-dependent versus machine-independent optimizations “There is no such thing as a machine-independent optimization.” – William A. Wulf ©SoftMoore Consulting

Guidelines for Optimization Make it correct before making it faster. The best source of optimization is often the programmer. better algorithm (bubble sort versus quick sort) profiling to determine areas where optimization matters rewriting time-critical code in assembly language Test compiler both with and without optimizations. Let someone else do it. e.g., use a common, low-level intermediate language (LLVM) Remember that occasionally, especially during development, faster compile times can be more important that more efficient object code. ©SoftMoore Consulting

Code Optimization Issues Often difficult to improve algorithmic complexity Compilers must to support a variety of conflicting objectives cost of implementation – schedule for implementation compilation speed – runtime performance size of object code Overhead of compiler optimization extra work takes time whole-program optimization is time consuming and often difficult or impractical ©SoftMoore Consulting

Common Optimization Themes Optimize the common case even at the expense of a slow path Less code usually results in faster execution lower product cost for embedded systems Exploit the memory hierarchy registers first, then cache, then main memory, then disk Parallelize allow multiple computations to happen in parallel Improve Locality related code and data placed close together in memory ©SoftMoore Consulting

Optimization: Machine-Specific Instructions Use of specific instructions available on the target computer Examples increment and decrement instructions in place of add instructions block move instructions array-addressing instructions pre/post increment instructions ©SoftMoore Consulting

Optimization: Register Allocation Efficient use of registers to hold operands Register allocation – selection of variables that will reside in registers (e.g., a loop index) Register assignment – selection of specific registers for the variables Very hard problem – one common approach uses a “graph coloring” algorithm. ©SoftMoore Consulting

Optimization: Constant Folding Compile-time evaluation of arithmetic expressions involving constants Example: Consider the assignment statement c = 2*PI*r; Assuming PI has been declared as a named constant, evaluation of 2*PI can be performed by the compiler rather computed at runtime, and the resulting product can be used in the expression. ©SoftMoore Consulting

Optimization: Algebraic Identities Use of algebraic identities to simplify certain expressions Examples x + 0 = 0 + x = x x*1 = 1*x = x 0/x = 0 (provided x ≠ 0) x - 0 = x 0 – x = -x ©SoftMoore Consulting

Optimization: Strength Reduction Replacing operations with simpler, more efficient operations Use of machine-specific instructions can be considered a form of strength reduction. Examples i = i + 1 → inc i (use increment instruction) i*2 or 2*i → i + i (replace multiplication by 2 with addition) x/8 → x >> 3 (replace division by 2n with right-shift n) MOV EAX, 0 → XOR EAX (smaller and faster) ©SoftMoore Consulting

Optimization: Common Subexpression Elimination Detecting a common subexpression, evaluating it only once, and then referencing the common value Example: Consider the two following sets of statements a = x + y; a = x + y; … … b = (x + y)/2; b = a/2; These two sets of statement are equivalent provided that x and y do not change values in the intermediate statements. ©SoftMoore Consulting

Optimization: Loop-Invariant Code Motion (a.k.a. Code Hoisting) Move calculations outside of a loop (usually before the loop) without affecting the semantics of the program. also facilitates storing constant values in registers Example (from Wikipedia) while j < maximum - 1 loop j = j + (4+a[k])*PI+5; // a is an array end loop; The calculation of maximum - 1 and (4+a[k])*PI+5 can be moved outside the loop and precalculated. int maxval = maximum - 1; int calcval = (4+a[k])*PI+5; while (j < maxval) loop j = j + calcval; ©SoftMoore Consulting

Peephole Optimization Applied to the generated target machine code or a low-level intermediate representation. Basic idea: Analyze a small sequence of instructions at a time (the peephole) for possible performance improvements. The peephole is a small window into the generated code. Examples of peephole optimizations elimination of redundant loads and stores elimination of branch instructions to other branch instructions algebraic identities and strength reduction (can be easier to detect in the target machine code) ©SoftMoore Consulting

Example: Peephole Optimization Source Code ... loop exit when x > 0; end loop; Target Code L4: ... LDLADDR 0 LOADW LDCINT 0 CMP BG L5 BR L4 L5: BR L9 L8: Optimization: Replace BG L5 BR L4 with BLE L4 peephole ©SoftMoore Consulting

Optimization in CPRL It is possible to perform some optimizations within the abstract syntax tree. Add an optimize() method that “walks” the tree in a manner similar to the checkConstraints() and emit() methods. Add a parent reference to each node in the tree – can simplify some optimizations. The assembler for CPRL performs the following optimizations using a “peephole” approach: constant folding branch reduction (as illustrated in previous slide) strength reduction: use “inc” and “dec” where possible strength reduction: use left (right) shift instead of multiplying (dividing) by powers of 2 where possible ©SoftMoore Consulting