Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.

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

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.
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.
19 Classic Examples of Local and Global Code Optimizations Local Constant folding Constant combining Strength reduction.
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 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
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.
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.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
Partial Redundancy Elimination. Partial-Redundancy Elimination Minimize the number of expression evaluations By moving around the places where an expression.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
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.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
CS 536 Spring Global Optimizations Lecture 23.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
Lecture 6 Program Flow Analysis Forrest Brewer Ryan Kastner Jose Amaral.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
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.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
2015/6/29\course\cpeg421-08s\Topic4-a.ppt1 Topic-I-C Dataflow Analysis.
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.
Ben Livshits Based in part of Stanford class slides from
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
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.
What’s in an optimizing compiler?
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.
Λλ 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.
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 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Optimization Simone Campanoni
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
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.
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
University Of Virginia
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Topic-4a Dataflow Analysis 2019/2/22 \course\cpeg421-08s\Topic4-a.ppt.
Static Single Assignment
Code Optimization.
Presentation transcript:

Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1

Code optimization Elimination of unnecessary instructions Replacement of one sequence of instructions by a faster sequence of instructions Local optimization Global optimizations –based on data flow analyses CS308 Compiler Theory2

The Principal Sources of Optimization Optimization –Preserves the semantics of the original program –Applies relatively low-level semantic transformations CS308 Compiler Theory3

Causes of Redundancy Redundant operations are –at the source level –a side effect of having written the program in a high-level language Each of high-level data-structure accesses expands into a number of low-level arithmetic operations Programmers are not aware of these low-level operations and cannot eliminate the redundancies themselves. By having a compiler eliminate the redundancies –The programs are both efficient and easy to maintain. CS308 Compiler Theory4

A Running Example: Quicksort CS308 Compiler Theory5

6

Semantics-Preserving Transformations A number of ways in which a compiler can improve a program without changing the function it computes –Common-sub expression elimination –Copy propagation –Dead-code elimination –Constant folding CS308 Compiler Theory7

Common Subexpressions Common subexpression –Previously computed –The values of the variables not changed Local: CS308 Compiler Theory8

Common Subexpressions Global CS308 Compiler Theory9

10

Copy Propagation Copy statements or Copies –u = v CS308 Compiler Theory11

Dead-Code Elimination Live variable –A variable is live at a point in a program if its value can be used subsequently; –otherwise, it is dead at that point. Constant folding –Deducing at compile time that the value of an expression is a constant and using the constant instead CS308 Compiler Theory12

CS308 Compiler Theory13

Code Motion An important modification that decreases the amount of code in a loop Loop-invariant computation –An expression that yields the same result independent of the number of times a loop is executed Code Motion takes loop-invariant computation before its loop CS308 Compiler Theory14 while (i <= limit-2) t = limit -2 while (i <= t)

Induction Variables and Reduction in Strength Induction variable –For an induction variable x, there is a positive or negative constant c such that each time x is assigned, its value increases by c Induction variables can be computed with a single increment (addition or subtraction) per loop iteration Strength reduction –The transformation of replacing an expensive operation, such as multiplication, by a cheaper one, such as addition Induction variables lead to –strength reduction –eliminate computation CS308 Compiler Theory15

Now We have: CS308 Compiler Theory16 Inside-out

CS308 Compiler Theory17

Test yourself E CS308 Compiler Theory18

Data-Flow Analysis Techniques that derive information about the flow of data along program execution paths Examples –One way to implement global common sub expression elimination requires us to determine whether two identical expressions evaluate to the same value along any possible execution path of the program. –If the result of an assignment is not used along any subsequent execution path, then we can eliminate the assignment as dead code. CS308 Compiler Theory19

The Data-Flow Abstraction Execution paths –Within one basic block, the program point after a statement is the same as the program point before the next statement. –If there is an edge from block B1 to block B2, then the program point after the last statement of B1 may be followed immediately by the program point before the first statement of B2. Define an execution path from point P1 to point Pn to be a sequence of points P1, P2,..., Pn such that for each i = 1, 2,..., n - 1, either 1. Pi is the point immediately preceding a statement and Pi+1 is the point immediately following that same statement, or 2. Pi is the end of some block and Pi+1 is the beginning of a successor block. Reaching definition –The definitions that may reach a program point along some path CS308 Compiler Theory20

The Data-Flow Analysis Schema data-flow value –represents an abstraction of the set of all possible program states that can be observed for a program point Domain –The set of possible data-flow values for the application. –Example: the domain of data-flow values for reaching definitions is the set of all subsets of definitions in the program. Denote the data-flow values before and after each statement s by IN[S] and OUT[S] Data-flow problem –to find a solution to a set of constraints on the IN [S] 'S and OUT[S] 'S, for all statements S. –Two sets of constraints: those based on the semantics of the statements ( "transfer functions" ) and those based on the flow of control. CS308 Compiler Theory21

Transfer Functions The data-flow values before and after a statement are constrained by the semantics of the statement. transfer function –Both a and b will have the same value after the b=a statement. –Transfer function of a statement s is denoted as f s Two flavors of transfer function –Information propagate forward along execution paths –Flow backwards up the execution paths CS308 Compiler Theory22

Control-Flow Constraints Simple for within a basic block –if a block B consists of statements Sl, S2,..., Sn in that order, then the control-flow value out of Si is the same as the control-flow value into Si+1. Complicated for between basic blocks CS308 Compiler Theory23

Data-Flow Schemas on Basic Blocks IN[B], OUT[B] –denote the data-flow values immediately before and immediately after basic block B IN[B] = IN[S1], OUT[B] = OUT[Sn] –Suppose block B consists of statements Sl,..., Sn, in that order. f B = f Sn ○ ○ f S2 ○ f S1 CS308 Compiler Theory24

Reaching Definitions A definition d reaches a point p if there is a path from the point immediately following d to p, such that d is not "killed" along that path. A definition of a variable x is killed if there is any other definition of x anywhere along the path. Conservative –if we do not know whether a statement s is assigning a value to x, we must assume that it may assign to it. CS308 Compiler Theory25

Transfer Equations for Reaching Definitions Generates a definition d of variable u and Kills all other definitions in the program that define variable u Transfer function of definition d can be expressed as where gen d = {d}, the set of definitions generated by the statement, and kill d is the set of all other definitions of u in the program. CS308 Compiler Theory26

Transfer Equations for Reaching Definitions If Then CS308 Compiler Theory27

Transfer Equations for Reaching Definitions Suppose block B has n statements, with transfer functions for Then CS308 Compiler Theory28

Transfer Equations for Reaching Definitions The gen set contains all the definitions inside the block that are "visible" immediately after the block Downwards exposed –A definition is downwards exposed in a basic block only if it is not "killed" by a subsequent definition to the same variable inside the same basic block. –A basic block's kill set is simply the union of all the definitions killed by the individual statements. –kill=kill1 U kill2={d1,d2} –gen=gen2 U (gen1-kill2) = {d2} –f(x)={d2} U (x-{d1,d2}) //always includes d2 CS308 Compiler Theory29

Control-Flow Equations OUT[P] IN[B] whenever there is a control-flow edge from P to B. IN[B] needs to be no larger than the union of the reaching definitions of all the predecessor blocks CS308 Compiler Theory30

Iterative Algorithm for Reaching Definitions The reaching definitions problem is defined by the following equations: for all basic blocks B other than ENTRY CS308 Compiler Theory31

Iterative Algorithm for Reaching Definitions Algorithm : Reaching definitions. INPUT: A flow graph for which kill B and gen B have been computed for each block B. OUTPUT: IN[B ] and OUT[B] METHOD: CS308 Compiler Theory32

CS308 Compiler Theory33

Live-Variable Analysis In live-variable analysis we wish to know for variable x and point p whether the value of x at p could be used along some path in the flow graph starting at p. If so, we say x is live at p; otherwise, x is dead at p. Definitions: 1. def B : the set of variables defined in B prior to any use of that variable in B 2. use B : the set of variables whose values may be used in B prior to any definition of the variable. CS308 Compiler Theory34

Live-Variable Analysis Equations relating def and use: for all basic blocks B other than EXIT CS308 Compiler Theory35

Live-Variable Analysis Algorithm: Live-variable analysis. INPUT: A flow graph with def and use computed for each block. OUTPUT: IN[B] and OUT[B]. METHOD: CS308 Compiler Theory36

Available Expressions An expression x + y is available at a point p : –if every path, from the entry node to p evaluates x + y, and after the last such evaluation prior to reaching p, there are no subsequent assignments to x or y. A block kills expression x + y : –if it assigns (or may assign) x or y and does not subsequently recompute x + y. A block generates expression x + y : –if it definitely evaluates x + y and does not subsequently define x or y. CS308 Compiler Theory37

Available Expressions The primary use of available-expression information is for detecting global common subexpressions. CS308 Compiler Theory38

Available Expressions Computation of the set of generated expressions –At point p set S of expressions is available, and q is the point after p, with statement x=y+z 1. Add to S the expression y + z. 2. Delete from S any expression involving variable x. Example : CS308 Compiler Theory39

Available Expressions Let IN[B] be the set of expressions that are available before B OUT[B] be the same for the point following the end of B e_genB be the expressions generated by B e_killB be the set of expressions killed in B Then For all basic blocks B other than ENTRY CS308 Compiler Theory40

Available Expressions Algorithm: Available expressions. INPUT: A flow graph with e_killB and e_genB computed for each block B. The initial block is B1. OUTPUT: IN[B] and OUT[B]. METHOD: CS308 Compiler Theory41

Test yourself gen, kill, IN, OUT sets for each block e_gen, e_kill, IN, OUT sets for available expressions. def, use, IN, OUT sets for live variable analysis. CS308 Compiler Theory42