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.

Slides:



Advertisements
Similar presentations
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
Advertisements

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
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.
8. Code Generation. Generate executable code for a target machine that is a faithful representation of the semantics of the source code Depends not only.
Optimizing single thread performance Dependence Loop transformations.
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.
Introduction to Optimizations
Peephole Optimization Final pass over generated code: examine a few consecutive instructions: 2 to 4 See if an obvious replacement is possible: store/load.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Instruction Selection Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
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.
CStar Optimizing a C Compiler
Introduction to Program Optimizations Chapter 11 Mooly Sagiv.
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
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.
Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small.
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.
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.
CH10.1 CSE 4100 Chap 10: Optimization Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371 Fairfield Way,
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”
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Chapter 10 Code Optimization Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Compiler Construction Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation:  Have each value in a register when it is used. Instruction selection.
Superoptimization Venkatesh Karthik Srinivasan Guest Lecture in CS 701, Nov. 10, 2015.
Code Obfuscation Tool for Software Protection. Outline  Why Code Obfuscation  Features of a code obfuscator Potency Resilience Cost  Classification.
E Copyright © 2007, Oracle. All rights reserved. Using JDeveloper.
Debugging Watch and Text Output Alice. Debugging Techniques Several techniques are helpful in eliminating errors (bugs) in programs: careful design incremental.
Object Files & Linking. Object Sections Compiled code store as object files – Linux : ELF : Extensible Linking Format – Windows : PE : Portable Execution.
©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.
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
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
Introduction to Optimization
Code Optimization.
Material for course thanks to:
Optimization Code Optimization ©SoftMoore Consulting.
For Example: User level quicksort program Three address code.
Introduction to Optimization
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
Inlining and Devirtualization Hal Perkins Autumn 2011
Inlining and Devirtualization Hal Perkins Autumn 2009
Code Optimization Overview and Examples Control Flow Graph
Introduction to Optimization
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Compiler Construction
Code Generation Part II
TARGET CODE GENERATION
CSc 453 Final Code Generation
Exploitation Part 1.
Code Optimization.
Presentation transcript:

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 a program. –Code optimization is an important component in a compiler –Example: peephole optimization. Peephole: a small moving window in the instruction sequence Technique: examine a short sequence of target instructions (peephole) and try to replace it with a faster or shorter sequence

Peephole optimization: Redundant instruction elimination Flow of control optimization Algebraic simplifications Instruction selection Examples: –Redundant loads and stores MOV R0, a MOV a, R0 –Unreachable code If debug = 1 goto L1 Goto L2 L1: print debugging info L2:

–Examples: Flow of control optimization: goto L1 … L1: goto L2 goto L2 … L1: goto L2 if a < b goto L1 … L1: goto L2 if a<b goto L2 … L1: goto L2 goto L1 … L1: if a < b goto L2 if a < b goto L2 goto L3 … L1: L3:

Algebraic simplification: x : = x+0 x := x*1 ==  nop Reduction in strength X^2  x * x X * 4  x << 2 Instruction selection Sometimes some hardware instructions can implement certain operation efficiently.

Code optimization can either be high level or low level: –High level code optimizations: Loop unrolling, loop fusion, procedure inlining –Low level code optimizations: Instruction selection, register allocation –Some optimization can be done in both levels: Common subexpression elimination, strength reduction, etc. –Flow graph is a common intermediate representation for code optimization.

Basic block: a sequence of consecutive statements with exactly 1 entry and 1 exit. Flow graph: a directed graph where the nodes are basic blocks and block B1  block B2 if and only if B2 can be executed immediately after B1: Algorithm to construct flow graph: –Finding leaders of the basic blocks : The first statement is a leader Any statement that is the target of a conditional or unconditional goto is a leader Any statement that immediately follows a goto or conditional goto statement is a leader –For each leader, its basic block consists all statements up to the next leader. –B1  B2 if and only if B2 can be executed immediately after B1.

Example: 100: sum = 0 101: j = 0 102: goto : t1 = j << 2 104: t2 = addr(a) 105: t3 = t2[t1] 106: sum = sum + t3 107: if j < n goto 103

Optimizations within a basic block is called local optimization. Optimizations across basic blocks is called global optimization. Some common optimizations: –Instruction selection –Register allocation –Common subexpression elimination –Code motion –Strength reduction –Induction variable elimination –Dead code elimination –Branch chaining –Jump elimination –Instruction scheduling –Procedure inlining –Loop unrolling –Loop fusing –Code hoisting

Instruction selection: –Using a more efficient instruction to replace a sequence of instructions (space and speed). –Example: Mov R2, (R3) Add R2, #1, R2 Mov (R3), R2  Add (R3), 1, (R3)

Register allocation: allocate variables to registers (speed) Example: M[R13+sum] = 0 M[R13+j] = 0 GOTO L18 L19: R0 = M[R13+j] * 4 M[R13+sum] = M[R13+sum] +M[R0+_a] M[R13+j] = M[R13+j]+1 L18: NZ = M[R13+j] - M[_n] if NZ < 0 goto L19 R2 = 0 R1 = 0 GOTO L18 L19: R0 = R1 * 4 R2 = R2+M[R0+_a] R1 = R1+1 L18: NZ = R1 - M[_n] if NZ < 0 goto L19

Code motion: move a loop invariant computation before the loop Example: R2 = 0 R1 = 0 GOTO L18 L19: R0 = R1 * 4 R2 = R2+M[R0+_a] R1 = R1+1 L18: R4 = M[_n] NZ = R1 – R4 if NZ < 0 goto L19 R2 = 0 R1 = 0 R4 = M[_n] GOTO L18 L19: R0 = R1 * 4 R2 = R2+M[R0+_a] R1 = R1+1 L18: NZ = R1 – R4 if NZ < 0 goto L19

Strength reduction: replace expensive operation by equivalent cheaper operations Example: R2 = 0 R1 = 0 R4 = M[_n] GOTO L18 L19: R0 = R1 * 4 R2 = R2+M[R0+_a] R1 = R1+1 L18: NZ = R1 – R4 if NZ < 0 goto L19 R2 = 0 R1 = 0 R4 = M[_n] R3 = _a GOTO L18 L19: R2 = R2+M[R3] R3 = R3 + 4 R1 = R1+1 L18: NZ = R1 – R4 if NZ < 0 goto L19

Induction variable elimination: can induce value from another variable. Example: R2 = 0 R1 = 0 R4 = M[_n] R3 = _a GOTO L18 L19: R2 = R2+M[R3] R3 = R3 + 4 R1 = R1+1 L18: NZ = R1 – R4 if NZ < 0 goto L19 R2 = 0 R4 = M[_n] << 2 R3 = _a GOTO L18 L19: R2 = R2+M[R3] R3 = R3 + 4 L18: NZ = R3 – R4 if NZ < 0 goto L19

Common subexpression elimination:an expression was previously calculated and the variables in the expression have not changed. Can avoid recomputing the expression. Example: R1 = M[R13+I] << 2 R1 = M[R1+_b] R2 = M[R13+I] << 2; R2 = M[R2+_b] R1=M[R13+I] << 2 R1 = M[R1+_b] R2 = R1