Code Optimization Overview and Examples

Slides:



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

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.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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
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.
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 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
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)
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Intermediate Code. Local Optimizations
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.
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.
What’s in an optimizing compiler?
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
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.
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.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Credible Compilation With Pointers Martin Rinard and Darko Marinov Laboratory for Computer Science Massachusetts Institute of Technology.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
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.
High-level optimization Jakub Yaghob
Code Optimization.
Simone Campanoni Loop transformations Simone Campanoni
Optimization Code Optimization ©SoftMoore Consulting.
Basic Block Optimizations
Princeton University Spring 2016
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Code Generation Part III
Optimizing Transformations Hal Perkins Autumn 2011
Unit IV Code Generation
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Optimizing Transformations Hal Perkins Winter 2008
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
EECS 583 – Class 8 Classic Optimization
Fall Compiler Principles Lecture 10: Loop Optimizations
9. Optimization Marcus Denker.
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Lecture 19: Code Optimisation
Introduction to Optimization
Basic Block Optimizations
Code Optimization.
Presentation transcript:

Code Optimization Overview and Examples

Code Optimization Why Target Types Reduce programmers’ burden Allow programmers to concentrate on high level concept Without worrying about performance issues Target Reduce execution time Reduce space Sometimes, these are tradeoffs Types Intermediate code level We are looking at this part now Machine code level Instruction selection, register allocation, etc.

Code Optimization Scope Peephole analysis Local analysis Within one or a few instructions Local analysis Within a basic block Global analysis Entire procedure or within a certain scope Inter-procedural analysis Beyond a procedure, consider the entire program

Code Optimization Techniques Constant propagation Constant folding Algebraic simplification, strength reduction Copy propagation Common subexpression elimination Unreachable code elimination Dead code elimination Loop Optimization Function related Function inlining, function cloning

Code Optimization Techniques Constant propagation If the value of a variable is a constant, then replace the variable by the constant It is not the constant definition, but a variable is assigned to a constant The variable may not always be a constant E.g. N := 10; C := 2; for (i:=0; i<N; i++) { s := s + i*C; }  for (i:=0; i<10; i++) { s := s + i*2; } If (C) go to …  go to … The other branch, if any, can be eliminated by other optimizations Requirement: After a constant assignment to the variable Until next assignment of the variable Perform data flow analysis to determine the propagation

Code Optimization Techniques Constant folding In a statement x := y op z or x := op y If y and z are constants Then the value can be computed at compilation time Example #define M 10 x := 2 * M  x := 20 If (M < 0) goto L  can be eliminated y := 10 * 5  y := 50 Difference: constant propagation and folding Propagation: only substitute a variable by its assigned constant Folding: Consider variables whose values can be computed at compilation time and controls whose decision can be determined at compilation time

Code Optimization Techniques Algebraic simplification More general form of constant folding, e.g., x + 0  x x – 0  x x * 1  x x / 1  x x * 0  0 Repeatedly apply the rules (y * 1 + 0) / 1  y Strength reduction Replace expensive operations E.g., x := x * 8  x := x << 3

Code Optimization Techniques Copy propagation Extension of constant propagation After y is assigned to x, use y to replace x till x is assigned again Example x := y;  s := y * f(y) s := x * f(x) Reduce the copying If y is reassigned in between, then this action cannot be performed

Code Optimization Techniques Common subexpression elimination Example: a := b + c a := b + c c := b + c  c := a d := b + c d := b + c Example in array index calculations c[i+1] := a[i+1] + b[i+1] During address computation, i+1 should be reused Not visible in high level code, but in intermediate code

Code Optimization Techniques Unreacheable code elimination Construct the control flow graph Unreachable code block will not have an incoming edge After constant propagation/folding, unreachable branches can be eliminated Dead code elimination Ineffective statements x := y + 1 (immediately redefined, eliminate!) y := 5  y := 5 x := 2 * z x := 2 * z A variable is dead if it is never used after last definition Eliminate assignments to dead variables Need to do data flow analysis to find dead variables

Code Optimization Techniques Function inlining Replace a function call with the body of the function Save a lot of copying of the parameters, return address, etc. Function cloning Create specialized code for a function for different calling parameters

Code Optimization Techniques Loop optimization Consumes 90% of the execution time  a larger payoff to optimize the code within a loop Techniques Loop invariant detection and code motion Induction variable elimination Strength reduction in loops Loop unrolling Loop peeling Loop fusion

Code Optimization Techniques Loop invariant detection and code motion If the result of a statement or expression does not change within a loop, and it has no external side-effect Computation can be moved to outside of the loop Example for (i=0; i<n; i++) a[i] := a[i] + x/y; Three address code for (i=0; i<n; i++) { c := x/y; a[i] := a[i] + c; }  c := x/y; for (i=0; i<n; i++) a[i] := a[i] + c;

Code Optimization Techniques Strength reduction in loops Example s := 0; for (i=0; i<n; i++) { v := 4 * i; s := s + v; )  s := 0; for (i=0; i<n; i++) { v := v + 4; s := s + v; ) Induction variable elimination If there are multiple induction variables in a loop, can eliminate the ones which are used only in the test condition s := 0; for (i=0; i<n; i++) { s := 4 * i; … } -- i is not referenced in loop  s := 0; e := 4*n; while (s < e) { s := s + 4; }

Code Optimization Techniques Loop unrolling Execute loop body multiple times at each iteration Get rid of the conditional branches, if possible Allow optimization to cross multiple iterations of the loop Especially for parallel instruction execution Space time tradeoff Increase in code size, reduce some instructions Loop peeling Similar to unrolling But unroll the first and/or last few iterations

Code Optimization Techniques Loop fusion Example for i=1 to N do A[i] = B[i] + 1 endfor C[i] = A[i] / 2 D[i] = 1 / C[i+1] for i=1 to N do A[i] = B[i] + 1 C[i] = A[i] / 2 D[i] = 1 / C[i+1] endfor Is this correct? Actually, cannot fuse the third loop Before Loop Fusion

Code Optimization Will cover a subset of the techniques Control flow graph To facilitate data flow analysis for optimization To facilitate loop identification Data flow analysis Reachability analysis, copy propagation, expression propagation Constant folding Liveliness analysis, dead code elimination Loop optimization Function optimization Alias analysis (pointers) Depend on the time, some topics may be skipped

Code Optimization -- Summary Read Section 9.1