Compiler Code Optimizations. Introduction Introduction Optimized codeOptimized code Executes faster Executes faster efficient memory usage efficient memory.

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.
Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
Compiler Support for Superscalar Processors. Loop Unrolling Assumption: Standard five stage pipeline Empty cycles between instructions before the result.
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.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
The Case for a SC-preserving Compiler Madan Musuvathi Microsoft Research Dan Marino Todd Millstein UCLA University of Michigan Abhay Singh Satish Narayanasamy.
CMPUT Compiler Design and Optimization1 CMPUT680 - Winter 2006 Topic 5: Peep Hole Optimization José Nelson Amaral
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
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.
Jeffrey D. Ullman Stanford University. 2  A never-published Stanford technical report by Fran Allen in  Fran won the Turing award in  Flow.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
1 Copy Propagation What does it mean? Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Reduction in Strength CS 480. Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k] * b[k,
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.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Hardware-Software Interface Machine Program Performance = t cyc x CPI x code size X Available resources statically fixed Designed to support wide variety.
1 Lecture 7: Computer Arithmetic Today’s topics:  Chapter 2 wrap-up  Numerical representations  Addition and subtraction Reminder: Assignment 3 will.
Improving Code Generation Honors Compilers April 16 th 2002.
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.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
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.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
What’s in an optimizing compiler?
Putting Pointer Analysis to Work Rakesh Ghiya and Laurie J. Hendren Presented by Shey Liggett & Jason Bartkowiak.
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
Incremental Computation AJ Shankar CS265 Spring 2003 Expert Topic.
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”
Chapter 10 Code Optimization Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
More on Loop Optimization Data Flow Analysis CS 480.
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.
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.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
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.
Introduction To Computer Systems
Optimization Code Optimization ©SoftMoore Consulting.
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Code Generation Part III
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Fall Compiler Principles Lecture 10: Loop Optimizations
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Loop Optimization “Programs spend 90% of time in loops”
Lecture 19: Code Optimisation
Code Optimization.
Presentation transcript:

Compiler Code Optimizations

Introduction Introduction Optimized codeOptimized code Executes faster Executes faster efficient memory usage efficient memory usage yielding better performance. yielding better performance. Compilers can be designed to provide code optimization.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.Users should only focus on optimizations not provided by the compiler such as choosing a faster and/or less memory intensive algorithm.

Compiler Code Optimizations A Code optimizer sits between the front end and the code generator. A Code optimizer sits between the front end and the code generator. Works with intermediate code.Works with intermediate code. Can do control flow analysis.Can do control flow analysis. Can do data flow analysis.Can do data flow analysis. Does transformations to improve the intermediate code.Does transformations to improve the intermediate code.

Compiler Code Optimizations Optimizations provided by a compiler includes: Optimizations provided by a compiler includes: Inlining small functionsInlining small functions Code hoistingCode hoisting Dead store eliminationDead store elimination Eliminating common sub-expressionsEliminating common sub-expressions Loop unrollingLoop unrolling Loop optimizations: Code motion, Induction variable elimination, and Reduction in strength.Loop optimizations: Code motion, Induction variable elimination, and Reduction in strength.

Compiler Code Optimizations Inlining small functions Inlining small functions Repeatedly inserting the function code instead of calling it, saves the calling overhead and enable further optimizations.Repeatedly inserting the function code instead of calling it, saves the calling overhead and enable further optimizations. Inlining large functions will make the executable too large.Inlining large functions will make the executable too large.

Compiler Code Optimizations Code hoisting Code hoisting Moving computations outside loopsMoving computations outside loops Saves computing timeSaves computing time

Compiler Code Optimizations Code hoisting Code hoisting In the following example (2.0 * PI) is an invariant expression there is no reason to recompute it 100 times.In the following example (2.0 * PI) is an invariant expression there is no reason to recompute it 100 times. DO I = 1, 100 ARRAY(I) = 2.0 * PI * I ENDDO By introducing a temporary variable 't' it can be transformed to:By introducing a temporary variable 't' it can be transformed to: t = 2.0 * PI DO I = 1, 100 ARRAY(I) = t * I END DO

Compiler Code Optimizations Dead store elimination Dead store elimination If the compiler detects variables that are never used, it may safely ignore many of the operations that compute their values.If the compiler detects variables that are never used, it may safely ignore many of the operations that compute their values.

Compiler Code Optimizations Eliminating common sub-expressions Eliminating common sub-expressions Optimization compilers are able to perform quite well:Optimization compilers are able to perform quite well: X = A * LOG(Y) + (LOG(Y) ** 2) Introduce an explicit temporary variable t:Introduce an explicit temporary variable t: t = LOG(Y) X = A * t + (t ** 2) Saves one 'heavy' function call, by an elimination of the common sub-expression LOG(Y), the exponentiation now is:Saves one 'heavy' function call, by an elimination of the common sub-expression LOG(Y), the exponentiation now is: X = (A + t) * t

Compiler Code Optimizations Loop unrolling Loop unrolling The loop exit checks cost CPU time.The loop exit checks cost CPU time. Loop unrolling tries to get rid of the checks completely or to reduce the number of checks.Loop unrolling tries to get rid of the checks completely or to reduce the number of checks. If you know a loop is only performed a certain number of times, or if you know the number of times it will be repeated is a multiple of a constant you can unroll this loop.If you know a loop is only performed a certain number of times, or if you know the number of times it will be repeated is a multiple of a constant you can unroll this loop.

Compiler Code Optimizations Loop unrolling Loop unrolling Example:Example: // old loop for(int i=0; i<3; i++) { color_map[n+i] = i; } // unrolled version int i = 0; colormap[n+i] = i; i++; i++;

Compiler Code Optimizations Code Motion Code Motion Any code inside a loop that always computes the same value can be moved before the loop.Any code inside a loop that always computes the same value can be moved before the loop. Example:Example: while (i <= limit-2) do {loop code} where the loop code doesn't change the limit variable. The subtraction, limit-2, will be inside the loop. Code motion would substitute: t = limit-2; while (i <= t) do {loop code}

Compiler Code Optimizations Conclusion Conclusion Compilers can provide some code optimization.Compilers can provide some code optimization. Programmers do have to worry about such optimizations.Programmers do have to worry about such optimizations. Program definition must be preserved.Program definition must be preserved.