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:
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.
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.
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.
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,
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.
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,
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
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”
Chapter 10 Code Optimization Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
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
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
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.
Code Analysis and Optimization Pat Morin COMP 3002.
More Code Generation and Optimization Pat Morin COMP 3002.
Code Optimization Overview and Examples
High-level optimization Jakub Yaghob
Code Optimization.
Material for course thanks to:
Optimization Code Optimization ©SoftMoore Consulting.
Fall Compiler Principles Lecture 8: Loop Optimizations
Code Generation Part III
Optimizing Transformations Hal Perkins Autumn 2011
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
3.
9. Optimization Marcus Denker.
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
CMPE 152: Compiler Design April 30 Class Meeting
Code Optimization.
Presentation transcript:

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

Optimization Components

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

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

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

Function Preserving Transformations Common subexpression eliminations Copy propagations Dead and unreachable code elimination Constant Folding

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);

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

Example: 3AC 01) i := m - 1 16) t7 := 4 * i 02) j := n 17) t8 := 4 * j 03) t1 := 4 * n 18) t9 := a[t8] 04) v := a[t1] 19) a[t7] := t9 05) i := i + 1 20) t10 := 4 * j 06) t2 := 4 * i 21) a[t10] := x 07) t3 := a[t2] 22) goto 5 08) if t3 < v goto 5 23) t11 := 4 * i 09) j := j – 1 24) x := a[t11] 10) t4 := 4 * j 25) t12 := 4 * i 11) t5 := a[t4] 26) t13 := 4 * n 12) if t5 > v goto 9 27) t14 := a[t13] 13) if i >= j goto 23 28) a[t12] := t14 14) t6 := 4 * i 29) t15 := 4 * n 15) x := a[t6] 30) a[t15] := x

Basic Blocks i := m – 1 j := n t1 := 4 * n v := a[t1] i := i + 1 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 B6 B5 t11 := 4 * i x := a[t11] t12 := 4 * i t13 := 4 * n t14 := a[t13] a[t12] := t14 t15 := 4 * n a[t15] := x

Local Optimizations Block5 before Block5 after t6 := 4 * i t6 := 4 * i x := a[t6] x := a[t6] t7 := 4 * i t8 := 4 * j t8 := 4 * j t9 := a[t8] t9 := a[t8] a[t6] := t9 a[t7] := t9 a[t8] := x t10 := 4 * j goto Block2 a[t10] := x goto Block2 redundant calculations removed

Local Optimizations Block6 before Block6 after t11 := 4 * i t11 := 4 * i x := a[t11] x := a[t11] t12 := 4 * i t13 := 4 * n t13 := 4 * n t14 := a[t13] t14 := a[t13] a[t11] := t14 a[t12] := t14 a[t13] := x t15 := 4 * n a[t15] := x redundant calculations removed

Global Optimizations 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 t14 := a[t1] a[t2] := t14 a[t1] := x if i >= j goto B6 B4 B6 B5 After removing redundant calculations over all blocks

Loop Optimization Code Motion Reduction in Strength Induction Variables elimination

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*/

Reduction in Strength Before After i := m - 1 i := m - 1 j := n j := n 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). Before After i := m - 1 i := m - 1 j := n j := n t1 := 4 * n t1 := 4 * n v := a[t1] v := a[t1] t2 := 4 * i t4 := 4 * j Block2: Block2: i := i + 1 i := i + 1 t2 := 4 * i t2 := t2 + 4 t3 := a[t2] t3 := a[t2] if t3 < v goto B2 if t3 < v goto B2 Block3: Block3: j := j - 1 j := j - 1 t4 := 4 * j t4 := t4 - 4 t5 := a[t4] t5 := a[t4]

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 Before After Block4: Block4: if i >= j goto B6 if t2 >= t4 goto B6

Loop Optimization 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 B6 B5 After reduction in strength and induction-variable elimination

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

Others 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 just inc a Using shift to left instead of multiplication by powers of 2 Using shift to right instead of division into powers of 2