Code Optimization.

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
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.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
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.
1 CS 201 Compiler Construction Machine Code Generation.
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-
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 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.
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.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Code Generation Professor Yihjia Tsai Tamkang University.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
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.
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.
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.
5.3 Machine-Independent Compiler Features
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?
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”
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.
©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 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.
Basic Block Optimizations
Princeton University Spring 2016
Code Generation Part III
Unit IV Code Generation
TARGET CODE GENERATION
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
Compiler Construction
EECS 583 – Class 9 Classic and ILP Optimization
Code Generation Part II
CSc 453 Final Code Generation
CSc 453 Interpreters & Interpretation
Live Variables – Basic Block
Basic Block Optimizations
Presentation transcript:

Code Optimization

Potential Improvement

Intermediate Form of the Program Representation of the executable instructions with a sequence of quadruples: operation, op1, op2, result For example:

Intermediate Code

Quadruple Analysis for Code Optimization Intermediate results can be assigned to registers or to temporary variables to make their use as efficient as possible. Quadruples can be rearranged to eliminate redundant load and store operations.

Assignment and Use of Registers as Instruction Operands We would prefer to keep in registers all variables and intermediate results that will be used later in the program. Consider “VALUE” in quadruples 7 and 9, “MEAN” in quadruples 16 and 18. Register selection for replacement: Scan the program for the next point at which each register value would be used. Select the one whose value will not be needed for the longest time. Save the value of the selected register to a temporary variable if necessary. Be careful about the control flow of the program when assigning and using registers: Consider “SUM” in quadruples 1 and 7.

Basic Blocks One way to deal with the control flow is to divide the program into basic blocks. A basic block is a sequence of quadruples with one entry point (beginning of the block), one exit point (end of the block), and no jumps within the block. Assignment and use of registers within a basic block can follow the method previously described.

Basic Blocks A B C D E

Rearrangement of Quadruples DIV SUMSQ #100 i1 * MEAN MEAN i2 - i1 i2 i3 := i3 VARIANCE LDA SUMSQ DIV #100 STA T1 LDA MEAN MUL MEAN STA T2 LDA T1 SUB T2 STA VARIANCE * MEAN MEAN i2 DIV SUMSQ #100 i1 - i1 i2 i3 := i3 VARIANCE LDA MEAN MUL MEAN STA T1 LDA SUMSQ DIV #100 SUB T1 STA VARIANCE

Common Subexpression Elimination

Loop Invariant Elimination

Reducing in Strength of Operations

Code Optimization Some optimization can be obtained by rewriting the source program, e.g., T1 := 2 * J; T2 := T1 – 1; FOR I := 1 TO 10 DO X[I, T2] := Y[I, T1] However, this would achieve only a part of the benefits of code optimization. An optimizing compiler should allow the programmer to write source code that is clear and easy to read, and it should compile such a program into machine code that is efficient to execute.

Another Example

Three-Address Code

Flow Graph

Local Common Subexpression Elimination

Global Common Subexpression Elimination

Copy Propagation Improve the code in B5 by eliminating x: x := t3 a[t2] := t5 a[t4] := t3 goto B2 The idea is to use g for f, wherever possible after the copy statement f:=g This may not appear to be an improvement, but it gives us the opportunity to eliminate the assignment to x.

Dead-Code Elimination A variable is live at a point in a program if its value can be used subsequently; otherwise, it is dead (or useless) at that point. Copy propagation followed by dead-code elimination removes the assignment to x: a[t2] := t5 a[t4] := t3 goto B2

Loop Optimizations The running time of a program may be improved if we decrease the number of instructions in an inner loop. Three techniques are import for loop optimization: Code motion Moves code outside a loop Reduction in strength Replaces an expensive operation by a cheaper one Induction-variable elimination Eliminates variable from the inner loop

Strength Reduction

Induction-Variable Elimination induction variables induction variables

Code Optimization Result