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.

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:
Goal: Write Programs in Assembly
1 Optimization Optimization = transformation that improves the performance of the target code Optimization must not change the output must not cause errors.
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
Course Outline Traditional Static Program Analysis Software Testing
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.
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.
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.
Jeffrey D. Ullman Stanford University. 2  A never-published Stanford technical report by Fran Allen in  Fran won the Turing award in  Flow.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
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 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Peephole Optimization Final pass over generated code: examine a few consecutive instructions: 2 to 4 See if an obvious replacement is possible: store/load.
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)
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
Code Generation Professor Yihjia Tsai Tamkang University.
Introduction to Program Optimizations Chapter 11 Mooly Sagiv.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
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.
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.
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.
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
CH10.1 CSE 4100 Chap 10: Optimization Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371 Fairfield Way,
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.
Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Compilers Modern Compiler Design
3/2/2016© Hal Perkins & UW CSES-1 CSE P 501 – Compilers Optimizing Transformations Hal Perkins Autumn 2009.
©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.
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.
Material for course thanks to:
Optimization Code Optimization ©SoftMoore Consulting.
Machine-Independent Optimization
Code Generation Part III
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.
Unit IV Code Generation
Optimizing Transformations Hal Perkins Winter 2008
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Lecture 19: Code Optimisation
Code Generation Part II
Code Optimization.
Presentation transcript:

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 they consume Compilers that apply code-improving transformations are called Optimizing Compilers Machine Independent improvements address the logic of the program Machine Dependent improvements utilize special features of the target instruction set, including registers and special addressing modes

2 Optimization Components

3 Organization of a code optimizer Control-flow analysis Data-flow analysis Transformations Front-endCode generator Code optimizer

4 Flow Analysis Loops represent the most computationally intensive part of a program. Improvements to loops will produce the most significant effect Local Optimizations are performed on basic blocks of code Global Optimizations are performed on the whole code A Basic Block is a sequence of instructions that is only entered at the start and exited at the end, with no jumps into or out of the middle. That is, a basic block begins at a procedure or the target of a jump A basic block ends at the start of the next basic block or the end of the program

5 Criteria for code-improvement Transformations 1. Transformations must preserve the meaning of programs 2. A transformation must, on the average, speed up programs by a measurable amount 3. A transformation must be worth the effort

6 Function Preserving Transformations 1. Common subexpression eliminations 2. Copy propagations 3. Dead and unreachable code elimination 4. Constant Folding

7 Example: C code void quicksort(m, n) int m, n; { int I, j; if (n <= m ) return; /* fragment begins here */ i = m-1; j = n; v = a[n]; while(1){ do i = i+1; while( a[i] < v ); do j = j-1; while( a[j] > v ); if( i >= j ) break; x = a[i]; a[i] = a[j];a[j] = x; } x = a[i]; a[i] = a[n]; a[n]= x; /* fragment ends here */ quicksort(m, j); quicksort(i+1, n); }

8 Augmented 3AC An augmented 3 address code language to simplify the code... Let a be an array of integers starting at byte address a 0 a[add] on the left-hand-side of an assignment is the address a 0 +add a[add] on the right-hand-side of an assignment is the value of the element of the array at address a 0 +add Since integers are stored in 4 bytes the offset address of an element a[i] is 4*i

9 Example: 3AC Example: 3AC 01) i := m - 116) t7 := 4 * i 02) j := n17) t8 := 4 * j 03) t1 := 4 * n18) t9 := a[t8] 04) v := a[t1]19) a[t7] := t9 05) i := i ) t10 := 4 * j 06) t2 := 4 * i21) a [t 10 ] := x 07) t3 := a[t2]22) goto 5 08) if t3 < v goto 523) t11 := 4 * i 09) j := j – 124) x := a[t11] 10) t4 := 4 * j25) t12 := 4 * i 11) t5 := a[t4]26) t13 := 4 * n 12) if t5 > v goto 927) t14 := a[t13] 13) if i >= j goto 2328) a[t12] := t14 14) t6 := 4 * i29) t15 := 4 * n 15) x := a[t6]30) a[t15] := x

10 Basic Blocks i := m – 1 j := n t1 := 4 * n v := a[t1] B1 i := i + 1 t2 := 4 * i t3 := a[t2] if t3 < v goto B2 B2 j := j - 1 t4 := 4 * j t5 := a[t4] if t5 > v goto B3 B3 t6 := 4 * i x := a[t6] t7 := 4 * i t8 := 4 * j t9 := a[t8] a[t7] := t9 t10 := 4 * j a[t10] := x goto B2 if i >= j goto B6 B4 B6B5 t11 := 4 * i x := a[t11] t12 := 4 * i t13 := 4 * n t14 := a[t13] a[t12] := t14 t15 := 4 * n a[t15] := x

11 Local Optimizations Block5 beforeBlock5 aftert6 := 4 * ix := a[t6] t7 := 4 * it8 := 4 * j t8 := 4 * jt9 := a[t8] t9 := a[t8]a[t6] := t9 a[t7] := t9a[t8] := x t10 := 4 * jgoto Block2 a[t10] := x goto Block2redundant calculations removed

12 Local Optimizations Block6 beforeBlock6 aftert11 := 4 * ix := a[t11] t12 := 4 * it13 := 4 * n t13 := 4 * nt14 := a[t13] t14 := a[t13]a[t11] := t14 a[t12] := t14a[t13] := x t15 := 4 * n a[t15] := xredundant calculations removed

13 Global Optimizations After removing redundant calculations over all blocks i := m – 1 j := n t1 := 4 * n v := a[t1] B1 i := i + 1 t2 := 4 * i t3 := a[t2] if t3 < v goto B2 B2 j := j - 1 t4 := 4 * j t5 := a[t4] if t5 > v goto B3 B3 x := t3 a[t2] := t5 a[t4] := x goto B2 x := t3 t14 := a[t1] a[t2] := t14 a[t1] := x if i >= j goto B6 B4 B6B5

14 Loop Optimization 1. Code Motion 2. Reduction in Strength 3. Induction Variables elimination

15 Code Motion Code Motion decreases the amount of code in a loop Before while ( i <= limit + 2) /*statement does not change limit */ After t = limit + 2 while ( i <= t) /*statement does not change limit or t*/

16 Reduction in Strength In Block2 whenever i increases by 1, t2 increases by 4 In Block3 whenever j decreases by 1, t4 decreases by 4 Addition and Subtraction can be used instead of the more computationally expensive multiplication (t2 and t4 must be initialized). BeforeAfteri := m - 1j := nt1 := 4 * nv := a[t1] t2 := 4 * i t4 := 4 * j Block2: i := i + 1 t2 := 4 * i t2 := t2 + 4 t3 := a[t2] if t3 < v goto B2 Block3: j := j - 1 t4 := 4 * j t4 := t4 - 4 t5 := a[t4]

17 Induction Variables elimination In Block2 whenever i increases by 1, t2 increases by 4, i and t2 are called induction variables. In Block3 whenever j decreases by 1, t4 decreases by 4, j and t4 are induction variables, too. If there are two or more induction variables in a loop, it may be possible to get rid of all but one BeforeAfter Block4: if i >= j goto B6 if t2 >= t4 goto B6

18 Loop Optimization After reduction in strength and induction-variable elimination i := m – 1 j := n t1 := 4 * n v := a[t1] t2 := 4 * i t4 := 4 * j B1 t2 := t2 + 4 t3 := a[t2] if t3 < v goto B2 B2 t4 := t4 - 4 t5 := a[t4] if t5 > v goto B3 B3 a[t2] := t5 a[t4] := t3 goto B2 t14 := a[t1] a[t2] := t14 a[t1] := t3 if t2 >= t4 goto B6 B4 B6B5

19 Peephole Optimization Removing Redundant Load and Stores 3AC a := b + c; d := a - e; Machine Code mov registera b add registera c mov a registera-- redundant if mov registera a-- a is no longer used sub registera e mov d registera

20Others Algebraic Simplification x := x + 0 x := x * 1 Use of Registers to store the most used variables because access time is much quicker than memory Use of specialized instructions mov a add registera 1 mov a registera could be justinc a Using shift to left instead of multiplication by powers of 2 Using shift to right instead of division into powers of 2