1 Languages and Compilers (SProg og Oversættere) Lecture 15 (1) Compiler Optimizations Bent Thomsen Department of Computer Science Aalborg University With.

Slides:



Advertisements
Similar presentations
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Hanne Riis Nielson.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
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.
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 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
1 Languages and Compilers (SProg og Oversættere) Lecture 13 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 531 Compiler Construction Final Lecture of the Semester Spring 2010 Marco.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
Program Representations. Representing programs Goals.
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,
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Program analysis Mooly Sagiv html://
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)
An Overview on Static Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of.
Program analysis Mooly Sagiv html://
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Overview of program analysis Mooly Sagiv html://
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.
Improving Code Generation Honors Compilers April 16 th 2002.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Overview of program analysis Mooly Sagiv html://
Ben Livshits Based in part of Stanford class slides from
An Overview on Static Program Analysis Mooly Sagiv.
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.
Department of Computer Science A Static Program Analyzer to increase software reuse Ramakrishnan Venkitaraman and Gopal Gupta.
Array Dependence Analysis COMP 621 Special Topics By Nurudeen Lameed
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.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
Static Program Analyses of DSP Software Systems Ramakrishnan Venkitaraman and Gopal Gupta.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
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.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
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.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Optimization Simone Campanoni
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Code Optimization Overview and Examples
High-level optimization Jakub Yaghob
Code Optimization.
Data Flow Analysis Suman Jana
Textbook: Principles of Program Analysis
Machine-Independent Optimization
University Of Virginia
Languages and Compilers (SProg og Oversættere) Compiler Optimizations
Live variables and copy propagation
Ras Bodik WF 11-12:30 slides adapted from Mooly Sagiv
Presentation transcript:

1 Languages and Compilers (SProg og Oversættere) Lecture 15 (1) Compiler Optimizations Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson and Mooly Sagiv whose slides this lecture is based on.

2 Compiler Optimizations The code generated by the Mini Triangle compiler is not very efficient: We did some optimizations by special code templates, but –It still computes some values at runtime that could be known at compile time –It still computes values more times than necessary –It produces code that will never be executed We can do better! We can do code transformations Code transformations are performed for a variety of reasons among which are: –To reduce the size of the code –To reduce the running time of the program –To take advantage of machine idioms Code optimizations include: –Constant folding –Common sub-expression elimination –Code motion –Dead code elimination Mathematically, the generation of optimal code is undecidable.

3 Criteria for code-improving transformations Preserve meaning of programs (safety) –Potentially unsafe transformations Associative reorder of operands Movement of expressions and code sequences Loop unrolling Must be worth the effort (profitability) and –on average, speed up programs 90/10 Rule: Programs spend 90% of their execution time in 10% of the code. Identify and improve "hot spots" rather than trying to improve everything.

4 Constant folding Consider: The compiler could compute 4 / 3 * pi as before the program runs. This saves how many instructions? What is wrong with the programmer writing * r * r * r ? static double pi = ; double volume = 4/3 * pi * r * r * r;

5 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? Safely? struct { int y, m, d; } holidays[6]; holidays[2].m = 12; holidays[2].d = 25;

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

7 Common sub-expression elimination II int t = (x – y) * (x – y + z); Naïve code: iload x iload y isub iload x iload y isub iload z iadd Imult istore t

8 Common sub-expression elimination II Programmer tries to be clever Naïve code: iload x iload y isub iload x iload y isub iload z iadd Imult istore t New code: iload x iload y isub istore tmp iload tmp iload z iadd Imult istore t int tmp = (x - y) int t = tmp * (tmp + z); Is this code better or worse?

9 Common sub-expression elimination II int t = (x – y) * (x – y + z); Naïve code: iload x iload y isub iload x iload y isub iload z iadd Imult istore t Better code: iload x iload y isub dup iload z iadd Imult istore t

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

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

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

13 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’;

14 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; }

15 Dead code elimination II But consider: Now t is only dead for part of its existence. Hmm… int f(int x, int y, int z) { int t = x * y; int r = t * z; t = (x – y) * (x – y + z); return r; }

16 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 Is the optimization correct or safe? Is the optimization an improvement? What sort of analyses do we need to perform to get the required information?

17 Basic blocks A basic block is a sequence of instructions entered only at the beginning and left only at the end. A flow graph is a collection of basic blocks connected by edges indicating the flow of control.

18 Finding basic blocks 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

19 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

20 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

21 Optimizations within a BB 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 BB –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

22 Global optimizations Global means “between 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

23 Global live variable analysis We define four sets for each BB –def == variables with defined values –use == variables used before they are defined –in == variables live at the beginning of a BB –out == variables live at the end of a BB 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

24 Solving data flow equations Iterative solution: –Start with empty set –Iteratively apply constraints –Stop when we reach a fixed point For all instructions in[I] = out[I] =  Repeat For each instruction I in[I] = ( out[I] – def[I] )  use[I] For each basic block B out[B] =  in[B’] Until no new changes in sets B’  succ(B)

25 Dead code elimination Armed with global live variable information we redo the local live variable analysis with correct liveness information at the end of the block out[B] Whenever we see an assignment to a variable that is marked dead, we eliminate it.

26 Static Analysis Automatic derivation of static properties which hold on every execution leading to a program location

27 Example Static Analysis Problems Live variables Reaching definitions Expressions that are “available” Dead code Pointer variables that never point into the same location Points in the program in which it is safe to free an object An invocation of a virtual method whose address is unique Statements that can be executed in parallel An access to a variable which must be in cache Integer intervals Security properties …

28 Foundation of Static Analysis Static analysis can be viewed as interpreting the program over an “abstract domain” Execute the program over a larger set of execution paths Guarantee sound results –Every identified constant is indeed a constant –But not every constant is identified as such

29 Abstract (Conservative) interpretation abstract representation Set of states concretization Abstract semantics statement s abstract representation abstraction Operational semantics statement s Set of states

30 Example rule of signs Safely identify the sign of variables at every program location Abstract representation {P, N, ?} Abstract (conservative) semantics of *

31 Abstract (conservative) interpretation {…,,…} concretization Abstract semantics x := x*#y abstraction Operational semantics x := x*y {…, …}

32 Example rule of signs (cont) Safely identify the sign of variables at every program location Abstract representation {P, N, ?}  (C) = if all elements in C are positive then return P else if all elements in C are negative then return N else return ?  (a) = if (a==P) then return{0, 1, 2, … } else if (a==N) return {-1, -2, -3, …, } else return Z

33 Undecidability Issues It is undecidable if a program point is reachable in some execution Some static analysis problems are undecidable even if the program conditions are ignored

34 Coping with undecidabilty Loop free programs Simple static properties Interactive solutions Conservative estimations –Enabled transformation cannot change the meaning of the code –thus some transformations are not enabled –Non optimal code –Every potential error is caught but some “false alarms” may be issued

35 Abstract interpretation cannot always be homomorphic (rules of signs) abstraction abstraction Operational semantics x := x+y Abstract semantics x := x+#y 

36 Optimality Criteria Precise (with respect to a subset of the programs) Precise under the assumption that all paths are executable (statically exact) Relatively optimal with respect to the chosen abstract domain Good enough

37 A somewhat more complex compiler

38 Complementary Approaches Unsound Approaches –Compute under approximation Type checking Just in time and dynamic compilation Profiling Runtime tests Program Verification Better programming language design

39 Learning More about Optimizations Read chapter 9-12 in the new Dragon Book –Compilers: Principles, Techniques, and Tools (2nd Edition) by Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman, Addison-Wesley, ISBN Read the ultimate reference on program analysis –Principles of Program Analysis Flemming Nielson, Hanne Riis Nielson, Chris Hankin: Principles of Program Analysis. Springer (Corrected 2nd printing, 452 pages, ISBN ), Use one of the emerging frameworks: –Soot: a Java Optimization Framework –Phoenix Compiler Backend