©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.

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

CSC 4181 Compiler Construction Code Generation & Optimization.
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.
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
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.
CS 31003: Compilers Introduction to Phases of Compiler.
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.
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
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.
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.
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.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
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.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
Register Allocation and Spilling via Graph Coloring G. J. Chaitin IBM Research, 1982.
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,
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.
CPSC 388 – Compiler Design and Construction Optimization.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
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:
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.
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.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
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
Code Optimization.
Compiler Construction (CS-636)
Optimization Code Optimization ©SoftMoore Consulting.
Compiler Construction
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.
Code Generation Part II
Target Code Generation
CMPE 152: Compiler Design April 30 Class Meeting
Code Optimization.
Presentation transcript:

©SoftMoore ConsultingSlide 1 Code Optimization

©SoftMoore ConsultingSlide 2 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 Local versus global optimizations Optimizing compilers

©SoftMoore ConsultingSlide 3 Code Optimization (continued) Term “optimization” is actually used improperly –generated code is rarely optimal –better name might be “code improvements” Machine-dependent versus machine-independent optimizations May be performed on intermediate representations of the program –high level representation such as abstract syntax trees –machine code or a low-level representation “There is no such thing as a machine-independent optimization.” – William A. Wulf

©SoftMoore ConsultingSlide 4 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.

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 ConsultingSlide 5

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 ConsultingSlide 6

©SoftMoore ConsultingSlide 7 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 ConsultingSlide 8 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 ConsultingSlide 9 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 ConsultingSlide 10 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 ConsultingSlide 11 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 2 n with right-shift n) MOV EAX, 0 → XOR EAX (smaller and faster)

©SoftMoore ConsultingSlide 12 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 statementsa = 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.

Optimization: Loop-Invariant Code Motion (a.k.a. Code Hoisting) Move statements 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; end loop; ©SoftMoore ConsultingSlide 13

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 ConsultingSlide 14

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

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 ConsultingSlide 16