Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
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.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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-
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.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
Jeffrey D. Ullman Stanford University. 2  Generalizes: 1.Moving loop-invariant computations outside the loop. 2.Eliminating common subexpressions. 3.True.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
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)
Code Generation Professor Yihjia Tsai Tamkang University.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Register Allocation (via graph coloring)
Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Intermediate Code. Local Optimizations
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
1 Languages and Compilers (SProg og Oversættere) Lecture 15 (1) Compiler Optimizations Bent Thomsen Department of Computer Science Aalborg University With.
Improving Code Generation Honors Compilers April 16 th 2002.
Compilation 2007 Optimization Michael I. Schwartzbach BRICS, University of Aarhus.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Compiler Code Optimizations. Introduction Introduction Optimized codeOptimized code Executes faster Executes faster efficient memory usage efficient memory.
1 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
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.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Jeffrey D. Ullman Stanford University. 2 boolean x = true; while (x) {... // no change to x }  Doesn’t terminate.  Proof: only assignment to x is at.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Compilers Modern Compiler Design
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
1 Languages and Compilers (SProg og Oversættere) Compiler Optimizations Bent Thomsen Department of Computer Science Aalborg University With acknowledgement.
RealTimeSystems Lab Jong-Koo, Lim
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
©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.
Optimization Code Optimization ©SoftMoore Consulting.
Compiler Code Optimizations
Languages and Compilers (SProg og Oversættere) Compiler Optimizations
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Course Overview PART I: overview material PART II: inside a compiler
Code Generation Part II
CMPE 152: Compiler Design April 30 Class Meeting
Code Optimization.
Presentation transcript:

Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a compiler PART II: inside a compiler 4Syntax analysis 5Contextual analysis 6Runtime organization 7Code generation PART III: conclusion 8Interpretation 9Review Supplementary material: Code optimization

Code Optimization 2 What This Topic is About The code generated by our compiler is not efficient: It computes some values at runtime that could be known at compile time It computes some values more times than necessary We can do better! Constant folding Common sub-expression elimination Code motion Dead code elimination

Code Optimization 3 Constant folding Consider: The compiler could compute 4 * pi / 3 as before the program runs. How many instructions would this save at run time? Why shouldn’t the programmer just write * r * r * r ? static double pi = ; double volume = 4 * pi / 3 * r * r * r;

Code Optimization 4 Constant folding II Consider: If the address of holidays is x, what is the address of holidays[2].m ? Could the programmer evaluate this at compile time? Should the programmer do this? struct { int y, m, d; } holidays[6]; holidays[2].m = 12; holidays[2].d = 25;

Code Optimization 5 Constant folding III An expression that the compiler should be able to compute the value of is called “manifest”. How can the compiler know if the value of an expression is manifest?

Code Optimization 6 Common sub-expression elimination Consider: Computing x – y takes three instructions; could we save some of them? t = (x – y) * (x – y + z);

Code Optimization 7 Common sub-expression elimination II t = (x – y) * (x – y + z); Naïve code: load x load y sub load x load y sub load z add mult store t Better code: load x load y sub dup load z add mult store t

Code Optimization 8 Common sub-expression elimination III Consider: The address of holidays[i] is a common subexpression. struct { int y, m, d; } holidays[6]; holidays[i].m = 12; holidays[i].d = 25;

Code Optimization 9 But, be careful! Is x – y++ still a common sub-expression? Common sub-expression elimination IV t = (x – y++) * (x – y++ + z);

Code Optimization 10 Code motion Consider: Computing the address of name[i][j] is address[name] + (i * 10) + j Most of that computation is constant throughout the inner loop char name[3][10]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 10; j++) { name[i][j] = ‘a’; address[name] + (i * 10)

Code Optimization 11 Code motion II You can think of this as rewriting the original code: as: char name[3][10]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 10; j++) { name[i][j] = ‘a’; char name[3][10]; for (int i = 0; i < 3; i++) { char *x = &(name[i][0]); for (int j = 0; j < 10; j++) { x[j] = ‘a’;

Code Optimization 12 Code motion III However, this might be a bad idea in some cases. Why? Consider very small values of variable k : char name[3][10]; for (int i = 0; i < 3; i++) { for (int j = 0; j < k; j++) { name[i][j] = ‘a’; char name[3][10]; for (int i = 0; i < 3; i++) { char *x = &(name[i][0]); for (int j = 0; j < k; j++) { x[j] = ‘a’;

Code Optimization 13 Dead code elimination Consider: Computing t takes many instructions, but the value of t is never used. We call the value of t “dead” (or the variable t dead) because it can never affect the final value of the computation. Computing dead values and assigning to dead variables is wasteful. int f(int x, int y, int z) { int t = (x – y) * (x – y + z); return 6; }

Code Optimization 14 Dead code elimination II But consider: Now t is only dead for part of its existence. So it requires a careful algorithm to identify which code is dead, and therefore which code can be safely removed. int f(int x, int y, int z) { int t = x * y; int r = t * z; t = (x – y) * (x – y + z); return r; }

Code Optimization 15 Optimization implementation What do we need to know in order to apply an optimization? –Constant folding –Common sub-expression elimination –Code motion –Dead code elimination –Many other kinds of optimizations Is the optimization correct or safe? Is the optimization really an improvement? What sort of analyses do we need to perform to get the required information?

Code Optimization 16 Basic blocks A basic block is a sequence of instructions that is entered only at the beginning and exited only at the end. A flow graph is a collection of basic blocks connected by edges indicating the flow of control.

Code Optimization 17 Finding basic blocks (Example: JVM code) iconst_1 istore 2 iconst_2 istore 3 Label_1: iload 3 iload 1 if_icmplt Label_4 iconst_0 goto Label_5 Label_4: iconst_1 Label_5: ifeq Label_2 iload 2 iload 3 imul dup istore 2 pop Label_3: iload 3 dup iconst_1 iadd istore 3 pop goto Label_1 Label_2: iload 2 ireturn Mark the first instruction, labelled instructions, and following jumps.

Code Optimization 18 Finding basic blocks II Label_2: iload 2 ireturn Label_3: iload 3 dup iconst_1 iadd istore 3 pop goto Label_1 iload 2 iload 3 imul dup istore 2 pop Label_5: ifeq Label_2 Label_4: iconst_1 iconst_0 goto Label_5 Label_1: iload 3 iload 1 if_icmplt Label_4 iconst_1 istore 2 iconst_2 istore 3

Code Optimization 19 Flow graphs 7:iload 2 ireturn 6:iload 3 dup iconst_1 iadd istore 3 pop goto 1 5:iload 2 iload 3 imul dup istore 2 pop 4:ifeq 7 3:iconst_1 2:iconst_0 goto 4 1:iload 3 iload 1 if_icmplt 3 0:iconst_1 istore 2 iconst_2 istore 3

Code Optimization 20 Local optimizations (within a basic block) Everything you need to know is easy to determine For example: live variable analysis –Start at the end of the block and work backwards –Assume everything is live at the end of the basic block –Copy live/dead info for the instruction –If you see an assignment to x, then mark x “dead” –If you see a reference to y, then mark y “live” 5:iload 2 iload 3 imul dup istore 2 pop live: 1, 2, 3 live: 1, 3 live: 1, 2, 3 live: 1, 3 live: 1, 2, 3 live: 1, 3

Code Optimization 21 Global optimizations Global means “across all basic blocks” We must know what happens across block boundaries For example: live variable analysis –The liveness of a value depends on its later uses perhaps in other blocks –What values does this block define and use? 5:iload 2 iload 3 imul dup istore 2 pop Define: 2 Use: 2, 3

Code Optimization 22 Global live variable analysis We define four sets for each basic block B –def[B] = variables defined in B before they are used in B –use[B] = variables used in B before they are defined in B –in[B] = variables live at the beginning of B –out[B] = variables live at the end of B These sets are related by the following equations: –in[B] = use[B]  (out[B] – def[B]) –out[B] =  S in[S] where S is a successor of B

Code Optimization 23 Solving data flow equations We want a fixed-point solution for this system of equations (there are two equations per each basic block). Start with conservative initial values for each in[B] and out[B], and apply the formulas to update the values of each in[B] and out[B]. Repeat until no further changes can occur. –The best conservative initial value is {}, because no variables are live at the end of the program.

Code Optimization 24 Dead code elimination Suppose we have now computed all global live variable information We can redo the local live variable analysis using correct liveness information at the end of each block: out[B] Whenever we see an assignment to a variable that is marked dead, we can safely eliminate it

Code Optimization 25 Dead code examples iload 1 iload 2 imul istore 4 iload 4 iload 3 imul istore 5 iload 1 iload 2 isub iload 1 iload 2 isub iload 3 iadd imul dup istore 4 pop iload 5 ireturn live: live: 5 live: 3, 5 live: 2, 3, 5 live: 1, 2, 3, 5 live: 1, 2, 3 live: 1, 2, 3, 4 live: 1, 2, 3

Code Optimization 26 Code optimization? Code optimization should be called “code improvement” It is not practical to generate absolutely optimal code (too expensive at compile time ==> NP-hard) There is a trade-off between compiler speed and execution speed Many compilers have options that permit the programmer to choose between generating either optimized or non-optimized code Non-optimized => debugging; optimized => release Some compilers even allow the programmer to select which kinds of optimizations to perform