Code Optimization.

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.
Course Outline Traditional Static Program Analysis Software Testing
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
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
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.
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.
Code Optimization I September 24, 2007 Topics Machine-Independent Optimizations Basic optimizations Optimization blockers class08.ppt F’07.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
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.
Optimizing Compilers Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
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.
Optimization software for apeNEXT Max Lukyanov,  apeNEXT : a VLIW architecture  Optimization basics  Software optimizer for apeNEXT  Current.
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?
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
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.
University of Amsterdam Computer Systems – optimizing program performance Arnoud Visser 1 Computer Systems Optimizing program performance.
High-Level Transformations for Embedded Computing
Introduction to ECE 454 Computer Systems Programming Topics: Lecture topics and assignments Profiling rudiments Lab schedule and rationale Cristiana Amza.
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Programming for Performance CS 740 Oct. 4, 2000 Topics How architecture impacts your programs How (and how not) to tune your code.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
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
Optimization Code Optimization ©SoftMoore Consulting.
CS 3114 Many of the following slides are taken with permission from
Code Optimization I: Machine Independent Optimizations
Princeton University Spring 2016
Introduction to Computer Systems
Machine-Independent 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.
Optimizing Transformations Hal Perkins Winter 2008
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Code Optimization I Nov. 25, 2008
Introduction to Computer Systems
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
Program Optimization CSE 238/2038/2138: Systems Programming
CS 3114 Many of the following slides are taken with permission from
Machine-Independent Optimization
Code Optimization.
Presentation transcript:

Code Optimization

Compiler Code Optimizations Introduction Optimized code Executes faster efficient memory usage yielding better performance. Compilers can be designed to provide code optimization. Users should only focus on optimizations not provided by the compiler such as choosing a faster and/or less memory intensive algorithm.

Topics Machine-independent optimizations Code motion Reduction in strength Common subexpression sharing Tuning: Identifying performance bottlenecks Machine-dependent optimizations Pointer code Loop unrolling Enabling instruction-level parallelism Understanding processor optimization Translation of instructions into operations Out-of-order execution Branches Caches and Blocking Advice

Great Reality There’s more to performance than asymptotic complexity Constant factors matter too! Easily see 10:1 performance range depending on how code is written Must optimize at multiple levels: Algorithm, data representations, procedures, and loops Must understand system to optimize performance How programs are compiled and executed How to measure program performance and identify bottlenecks How to improve performance without destroying code modularity, generality, readability

Speed and optimization Programmer Choice of algorithm Intelligent coding Compiler Choice of instructions Moving code Reordering code Strength reduction Must be faithful to original program Processor Pipelining Multiple execution units Memory accesses Branches Caches Rest of system Uncontrollable

Optimizing Compilers Provide efficient mapping of program to machine Register allocation Code selection and ordering Eliminating minor inefficiencies Don’t (usually) improve asymptotic efficiency Up to programmer to select best overall algorithm Big-O savings are (often) more important than constant factors But constant factors also matter Have difficulty overcoming “optimization blockers” Potential memory aliasing Potential procedure side effects

Limitations of Optimizing Compilers Operate Under Fundamental Constraint Must not cause any change in program behavior under any possible condition Often prevents making optimizations that would only affect behavior under pathological conditions Behavior that may be obvious to the programmer can be obfuscated by languages and coding styles E.g., data ranges may be more limited than variable types suggest Most analysis is performed only within procedures Whole-program analysis is too expensive in most cases Most analysis is based only on static information Compiler has difficulty anticipating run-time inputs When in doubt, the compiler must be conservative

Basic Block BB is a sequence of consecutive statements in which the flow control enters at the beginning and leaves at the end w/o halt or possible branching except at the end

Limitations of Optimizing Compilers Operate Under Fundamental Constraint Must not cause any change in program behavior under any possible condition Often prevents making optimizations that would only affect behavior under pathological conditions Behavior that may be obvious to the programmer can be obfuscated by languages and coding styles E.g., data ranges may be more limited than variable types suggest Most analysis is performed only within procedures Whole-program analysis is too expensive in most cases Most analysis is based only on static information Compiler has difficulty anticipating run-time inputs When in doubt, the compiler must be conservative

Basic Block

Principle sources of optimization Local optimization: within a basic block Global optimization: otherwise Mixed

Function-Preserving Transformation Improving performance w/o changing fn. Techniques Common subexpression Elimination Copy Propagation Dead-code elimination Constant folding

Common subexpression Elimination An occurrence of an expression E is common subexpression if E was previously computed and the values of variables in E have not changed since.

Copy Propagation An idea behind this technique is to use g for f whenever possible after the copy of f := g before x := t3 a[t7] := t5 a[t10] := x Goto b2 After x := t3 a[t7] := t5 a[t10] := t3 Goto b2

Dead code elimination Remove unreachable code If (debug) print … Many times, debug := false

Loop optimizations Beyond basic block Three important techniques Code motion Induction-variable elimination Reduction in strength

Code motion after T = limit – 2 While ( I <= t) Move code outside the loop since there are potential many iterations Look for expressions that yeild the same result independent of the iterations. before While ( I <= limit – 2). after T = limit – 2 While ( I <= t)

Induction-variable elimination & Reduction in strength Look for induction variables for strength reductions E.g. a pattern of changes in a lock step B3: j = j - 1 t4 = 4 *j t5 = a [ t4] If t5 > v goto B3 B3: j = j - 1 t4 = t4 -4 t5 = a [ t4] If t5 > v goto B3

Others optimizations Optimizations for Basic blocks Reducible flow graph Global Data flow analysis Machine dependent Optimizations

Basic Blocks

DAG