Compiler Principles Fall 2014-2015 Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
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.
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.
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-
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.
Optimization Compiler Baojian Hua
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
CS 536 Spring Global Optimizations Lecture 23.
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 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
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.
Code Generation Professor Yihjia Tsai Tamkang University.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
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.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
What’s in an optimizing compiler?
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
Compiler Optimizations ECE 454 Computer Systems Programming Topics: The Role of the Compiler Common Compiler (Automatic) Code Optimizations Cristiana Amza.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
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.
Compiler Principles Fall Compiler Principles Lecture 8: Intermediate Representation Roman Manevich Ben-Gurion University.
Optimization Simone Campanoni
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 8: Static Analysis II Roman Manevich Ben-Gurion University.
Review 1.Structure of the course Lexical Analysis Syntax Analysis Grammar & Language RG & DFA Top-down LL(1) Parsing Bottom-Up LR Layered Automation Semantic.
Compiler Principles Fall Compiler Principles Lecture 9: Dataflow & Optimizations 2 Roman Manevich Ben-Gurion University of the Negev.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Compiler Principles Fall Compiler Principles Lecture 8: Dataflow & Optimizations 1 Roman Manevich Ben-Gurion University of the Negev.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
High-level optimization Jakub Yaghob
Code Optimization.
Optimization Code Optimization ©SoftMoore Consulting.
Fall Compiler Principles Lecture 6: Dataflow & Optimizations 1
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Topic 10: Dataflow Analysis
Optimizations Noam Rinetzky
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Local Optimizations.
Fall Compiler Principles Lecture 6: Dataflow & Optimizations 1
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Lecture 19: Code Optimisation
Compiler Construction
Fall Compiler Principles Lecture 13: Summary
Live Variables – Basic Block
Code Optimization.
Presentation transcript:

Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University

Tentative syllabus Front End Scanning Top-down Parsing (LL) Bottom-up Parsing (LR) Intermediate Representation Lowering Optimizations Local Optimizations Dataflow Analysis Loop Optimizations Code Generation Register Allocation Instruction Selection 2 mid-termexam

Previously The need for Intermediate Representations Three-Address Code Lowering abstract syntax trees (AST) to IR Sethi-Ullman algorithm for efficient lowering 3

agenda Introduction to optimizations Formalisms for program analysis – Basic blocks – Control flow graphs Local program analyses and optimizations – Available expressions  common sub-expression elimination + copy propagation – Live variables  dead code elimination 4

5 Introduction to optimizations

Optimization points 6 source code Front end IR Code generator target code User profile program change algorithm Compiler apply IR optimizations Compiler register allocation instruction selection peephole transformations today and next week

Overview of IR optimization Formalisms and Terminology – Control-flow graphs – Basic blocks Local optimizations – Optimizing small pieces of a function Global optimizations – Optimizing functions as a whole The dataflow framework – Defining and implementing a wide class of optimizations 7

Semantics-preserving optimizations An optimization is semantics-preserving if it does not alter the semantics of the original program Examples: – Eliminating unnecessary statements – Computing values that are known statically at compile-time instead of runtime – Evaluating constant expressions outside of a loop instead of inside Non-examples: – Reordering side-effecting computations – Replacing bubble sort with quicksort (why?) The optimizations we will consider in this class are all semantics-preserving How can we find opportunities for optimizations? 8

Program analysis In order to optimize a program, the compiler has to be able to reason about the properties of that program An analysis is called sound if it never asserts an incorrect fact about a program All the analyses we will discuss in this class are sound – (Why?) 9

Soundness 10 int x; int y := get(); if (y < 5) x := 137; else x := 42; Print(x); “At this point in the program, x holds some integer value”

Soundness 11 int x; int y := get(); if (y < 5) x := 137; else x := 42; Print(x); “At this point in the program, x is either 137 or 42”

Soundness 12 int x; int y := get(); if (y < 5) x := 137; else x := 42; Print(x); “At this point in the program, x is 137”

Soundness 13 int x; int y := get(); if (y < 5) x := 137; else x := 42; Print(x); “At this point in the program, x is either 137, 42, or 271”

14 Control flow graphs

Visualizing IR 15 main: _tmp0 := Call _ReadInteger; a := _tmp0; _tmp1 := Call _ReadInteger; b := _tmp1; _L0: _tmp2 := 0; _tmp3 := b == _tmp2; _tmp4 := 0; _tmp5 := _tmp3 == _tmp4; IfZ _tmp5 Goto _L1; c := a; a := b; _tmp6 := c % a; b := _tmp6; Goto _L0; _L1: Push a; Call _PrintInt;

Visualizing IR 16 main: _tmp0 := Call _ReadInteger; a := _tmp0; _tmp1 := Call _ReadInteger; b := _tmp1; _L0: _tmp2 := 0; _tmp3 := b == _tmp2; _tmp4 := 0; _tmp5 := _tmp3 == _tmp4; IfZ _tmp5 Goto _L1; c := a; a := b; _tmp6 := c % a; b := _tmp6; Goto _L0; _L1: Push a; Call _PrintInt;

Visualizing IR 17 main: _tmp0 := Call _ReadInteger; a := _tmp0; _tmp1 := Call _ReadInteger; b := _tmp1; _L0: _tmp2 := 0; _tmp3 := b == _tmp2; _tmp4 := 0; _tmp5 := _tmp3 == _tmp4; IfZ _tmp5 Goto _L1; c := a; a := b; _tmp6 := c % a; b := _tmp6; Goto _L0; _L1: Push a; Call _PrintInt; _tmp0 := Call _ReadInteger; a := _tmp0; _tmp1 := Call _ReadInteger; b := _tmp1; _tmp2 := 0; _tmp3 := b == _tmp2; _tmp4 := 0; _tmp5 := _tmp3 == _tmp4; IfZ _tmp5 Goto _L1; c := a; a := b; _tmp6 := c % a; b := _tmp6; Goto _L0; Push a; Call _PrintInt; start end

Basic blocks A basic block is a sequence of IR instructions where – There is exactly one spot where control enters the sequence, which must be at the start of the sequence – There is exactly one spot where control leaves the sequence, which must be at the end of the sequence Informally, a sequence of instructions that always execute as a group 18

Control-flow graphs A control-flow graph (CFG) is a graph of the basic blocks in a function – From here on CFG stands for “control-flow graph” and not “context free grammar” Each edge from one basic block to another indicates that control can flow from the end of the first block to the start of the second block Dedicated nodes for the start and end of a function 19

Scope of optimizations An optimization is local if it works on just a single basic block An optimization is global if it works on an entire control-flow graph An optimization is interprocedural if it works across the control-flow graphs of multiple functions – We won't talk about this in this course 20

21 Examples of control flow graphs and optimizations

Basic blocks example 22 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { START: _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; _t1 := y; z := _t1; Goto END: _L0: _t2 := y; x := _t2; END: Divide the code into basic blocks

Control-flow graph example 23 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { START: _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; _t1 := y; z := _t1; Goto END: _L0: _t2 := y; x := _t2; END: Draw the control-flow graph

Control-flow graph example 24 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end

Local optimizations example 25 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end

Local optimizations example 26 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { _t0 := 137; y := _t0 + 3; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end

Local optimizations example 27 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { y := ; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end Copy propagation

Local optimizations example 28 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start _t1 := y; z := _t1; _t2 := y; x := _t2; end Constant folding

Local optimizations example 29 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start _t1 := y; z := y; _t2 := y; x := _t2; end Copy propagation

Local optimizations example 30 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y; _t2 := y; x := _t2; end Dead code elimination

Local optimizations example 31 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y; _t2 := y; x := _t2; end

Local optimizations example 32 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y;x := y; end Copy propagation followed by dead code elimination

Global optimizations example 33 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y;x := y; end

Global optimizations example 34 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := y;x := y; end

Global optimizations example 35 int main() { int x; int y; int z; y := ; if (x == 0) z := y; else x := y; { y := 140; IfZ x Goto _L0; start z := 140;x := 140; end Copy propagation

36 Common Subexpression elimination

CSE Example 37 b := a * a; c := a * a; d := b + c; e := b + b;

CSE Example 38 b := a * a; c := a * a; d := b + c; e := b + b;

CSE Example 39 b := a * a; c := b; d := b + c; e := b + b; Common sub-expression elimination

Common Subexpression Elimination If we have two variable assignments v1 := a op b … v2 := a op b and the values of v1, a, and b have not changed between the assignments, rewrite the code as v1 := a op b … v2 := v1 Eliminates useless recalculation Paves the way for later optimizations 40

41 copy propagation

CP Example 42 b := a * a; c := b; d := b + c; e := b + b;

CP Example 43 b := a * a; c := b; d := b + b; e := b + b; Copy propagation

Copy Propagation If we have a variable assignment v1 := v2 then as long as v1 and v2 are not reassigned, we can rewrite expressions of the form a := … v1 … as a := … v2 … 44

Local optimizations example 45 b := a * a; c := b; d := b + b; e := b + b; Which optimization should we apply here?

Local optimizations example 46 b := a * a; c := b; d := b + b; e := d; Which optimization should we apply here? Common sub-expression elimination (again)

Optimizations and analyses Most optimizations are only possible given some analysis of the program's behavior In order to implement an optimization, we will talk about the corresponding program analyses Program analysis = algorithm that processes program and infers facts – Sound facts = facts that hold for all program executions – Sound analysis = program analysis that infers only sound facts 47

48 Available expressions

Both common subexpression elimination and copy propagation depend on an analysis of the available expressions in a program An expression a = b op c is called available at program location L if variable a holds the value of b op c at that location – Similarly for a = b In common subexpression elimination, we replace an available expression (b op c) by the variable holding its value (a) In copy propagation, we replace the use of a variable (a) by the available expression it holds (b) 49

Finding available expressions Compute for each program location L a set of expressions AE L of the forms a = b op c and a = b that are definitely available there Idea: Iterate across the basic block, beginning with the empty set of expressions and updating available expressions at each statement Whenever we execute a statement a := b op c: – Any expression holding a is invalidated – The expression a = b op c becomes available 50

Available expressions step 51 a := b + c Output Value AE out Input Value AE in AE out = ( AE in \ {e | e contains a })  { a=b+c } Expressions of the forms a =… and x=… a … Note that this is an equation (a fact) – not a statement Provided that a and b and a and c are different pairs of variables

Available expressions example 52 a := b; c := b; d := a + b; e := a + b; d := b; f := a + b; { a = b, c = b, d = b, e = a + b } { a = b, c = b, d = a + b, e = a + b } { a = b, c = b, d = a + b } { a = b, c = b } { a = b } { } { a = b, c = b, d = b, e = a + b, f = a + b }

Optimizing via available expressions Common sub-expression elimination – If {… t = y op z … } x := y op z – Can transform statement into x := t Copy propagation – If {… y = t … } x := y op z – Can transform statement into x := t op z Note: same for x=y 53

Applying CSE + CP 54 a := b; c := b; d := a + b; e := a + b; d := b; f := a + b; { a = b, c = b, d = b, e = a + b } { a = b, c = b, d = a + b, e = a + b } { a = b, c = b, d = a + b } { a = b, c = b } { a = b } { } { a = b, c = b, d = b, e = a + b, f = a + b }

Applying CSE + CP 55 a := b; c := b; d := a + b; e := a + b; d := b; f := a + b; { a = b, c = b, d = b, e = a + b } { a = b, c = b, d = a + b, e = a + b } { a = b, c = b, d = a + b } { a = b, c = b } { a = b } { } { a = b, c = b, d = b, e = a + b, f = a + b }

Applying CSE + CP 56 a := b; c := a; d := a + b; e := d; d := a; f := e; { a = b, c = b, d = b, e = a + b } { a = b, c = b, d = a + b, e = a + b } { a = b, c = b, d = a + b } { a = b, c = b } { a = b } { } { a = b, c = b, d = b, e = a + b, f = a + b }

57 dead code elimination

Dead code elimination a := b; c := a; d := a + b; e := d; d := a; f := e; 58 Print(d); Can we remove this statement?

Dead code elimination An assignment to a variable v is called dead if the value of that assignment is never read anywhere Dead code elimination removes dead assignments from IR Determining whether an assignment is dead depends on assignments preceding it 59

Live variables The analysis corresponding to dead code elimination is called liveness analysis A variable is live at a point in a program if later in the program its value will be read before it is written to again Dead code elimination works by computing liveness for each variable, then eliminating assignments to dead variables 60

Computing live variables To know if a variable will be used at some point, we iterate across the statements in a basic block in reverse order Initially, some small set of values are known to be live (which ones depends on the particular program) – Usually arguments of a function call or a returned variable (temporaries that are pushed on the stack) When we see the statement a := b op c: – Just before the statement, a is not alive, since its value is about to be overwritten – Just before the statement, both b and c are alive, since we're about to read their values – (what if we have a := a + b?) 61

Live variables step 62 a := b + c Output Value LV out Input Value LV in LV in = ( LV out \ { a })  { b,c }

Liveness analysis a := b; c := a; d := a + b; e := d; d := a; f := e; { b, d, e } { a, b, e } { a, b, d } { a, b } { b } { b, d } Which statements are dead? 63 Push b; { d } Push d; { }

Optimizing via liveness analysis Dead code elimination – If x := y op z {v 1,…,v k } – And x  {v 1,…,v k } – We can eliminate x := y op z Note: same for x:=y 64

Dead code elimination a := b; c := a; d := a + b; e := d; d := a; f := e; { b, d, e } { a, b, e } { a, b, d } { a, b } { b } { b, d } - given Which statements are dead? 65

Dead code elimination a := b; d := a + b; e := d; d := a; { b, d, e } { a, b, e } { a, b, d } { a, b } { b } { b, d } 66

Liveness analysis II a := b; d := a + b; e := d; d := a; { b, d } { a, b } { a, b, d } { a, b } { b } Which statements are dead? 67

Liveness analysis II a := b; d := a + b; e := d; d := a; { b, d } { a, b } { a, b, d } { a, b } { b } Which statements are dead? 68

Dead code elimination a := b; d := a + b; e := d; d := a; { b, d } { a, b } { a, b, d } { a, b } { b } Which statements are dead? 69

Dead code elimination a := b; d := a + b; d := a; { b, d } { a, b } { a, b, d } { a, b } { b } 70

Liveness analysis III a := b; d := a + b; d := a; { b, d } { a, b } { b } Which statements are dead? 71

Dead code elimination a := b; d := a + b; d := a; { b, d } { a, b } { b } Which statements are dead? 72

Dead code elimination a := b; d := a; { b, d } { a, b } { b } 73

Dead code elimination a := b; d := a; 74 If we further apply copy propagation this statement can be eliminated too

A combined algorithm Start with initial live variables at end of block Traverse statements from end to beginning For each statement – If assigns to dead variables – eliminate it – Otherwise, compute live variables before statement and continue in reverse 75

A combined algorithm a := b; c := a; d := a + b; e := d; d := a; f := e; 76

A combined algorithm a := b; c := a; d := a + b; e := d; d := a; f := e; { b, d } 77

A combined algorithm a := b; c := a; d := a + b; e := d; d := a; f := e; { b, d } 78

A combined algorithm a := b; c := a; d := a + b; e := d; d := a; { b, d } 79

A combined algorithm a := b; c := a; d := a + b; e := d; d := a; { b, d } { a, b } 80

A combined algorithm 81 a := b; c := a; d := a + b; e := d; d := a; { b, d } { a, b }

A combined algorithm 82 a := b; c := a; d := a + b; d := a; { b, d } { a, b }

A combined algorithm a := b; c := a; d := a + b; d := a; { b, d } { a, b } 83

A combined algorithm a := b; c := a; d := a; { b, d } { a, b } 84

A combined algorithm a := b; c := a; d := a; { b, d } { a, b } 85

A combined algorithm a := b; d := a; { b, d } { a, b } 86

A combined algorithm a := b; d := a; { b, d } { a, b } 87 { b }

A combined algorithm a := b; d := a; 88

89 Notes about optimizations

Applying local optimizations The different optimizations we've seen so far all take care of just a small piece of the optimization – Common subexpression elimination eliminates unnecessary statements – Copy propagation helps identify dead code – Dead code elimination removes statements that are no longer needed To get maximum effect, we may have to apply these optimizations numerous times 90

Optimization path 91 IR Control-Flow Graph CFG builder Program Analysis Annotated CFG Optimizing Transformation Target Code Code Generation (+optimizations) done with IR optimizations IR optimizations

Other types of local optimizations Arithmetic Simplification – Replace “hard” operations with easier ones – e.g. rewrite x := 4 * a; as x := a << 2; Constant Folding – Evaluate expressions at compile-time if they have a constant value. – e.g. rewrite x := 4 * 5; as x := 20; 92

Next lecture: Global Optimizations via Dataflow Analysis