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.

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

8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Intermediate Code Generation
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
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.
Data Flow Analysis. Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local:
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
1 CS 201 Compiler Construction Machine Code Generation.
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.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
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. 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.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
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,
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.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
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.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
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.
Intermediate Code. Local Optimizations
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.
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.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
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.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
More on Loop Optimization Data Flow Analysis CS 480.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
©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.
More Code Generation and Optimization Pat Morin COMP 3002.
Code Optimization Overview and Examples
Optimization Code Optimization ©SoftMoore Consulting.
Machine-Independent Optimization
Chapter 6 Intermediate-Code Generation
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.
CS 201 Compiler Construction
TARGET CODE GENERATION
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Code Optimization Overview and Examples Control Flow Graph
Interval Partitioning of a Flow Graph
Static Single Assignment
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Code Generation Part II
Code Optimization.
Presentation transcript:

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 (1):create nodes labeled [], a Step (2):find previously node(t 1 ) Step (3):attach label (3)t 3 := 4 * i Here we determine that: node (4) was created node (i) was created node (*) was created just attach t 3 to *. * * t1t1 i0i0 4 * t 1,t 3 i0i0 4 [] t2t2 a0a0

Common Subexpression Elimination Detection: Common subexpressions can be detected by noticing, as a new node m is about to be added, whether there is an existing node n with the same children, in the same order, and with the same operator. if so, n computes the same value as m and may be used in its place. Note: The expressions b+c in (1) and (3) are not confused. Example: (1)a:= b + c (2)b:= a – d (3)c:= b + c (4)d:= a - d a:= b + c b:= a – d c:= b + c d:= b b0b0 c0c0 d0d0 c b,d a

Dead Code Elimination Transfomation: if x is dead (i.e., not live) – never subsequently used – after a point where statement x := y + z appears in a basic block. Then, this statement may be safely removed. Method: Delete from the DAG any root (node with no ancestors) that has no live variable. Weaker form: delete (when multiple) dead variable label. Repeat until we remove all nodes (labels) that correspond to dead code. a:= b+c b:=b-d c:=c+d e:=b+c b0b0 c0c0 d0d0 c b,d a b is not used at the exit of the basic block Examples: a:=b + c d:=a - d c:=d + c e abc c0c0 d0d0 b0b0

Renaming Temporary Variables Normal Form: Each statement that defines a temporary, defines a new temporary. - A basic block can always be transformed into an equivalent block which is in normal form Example: (1) t := b + c(1) u := b + c rename + t b0b0 c0c0 + u b0b0 c0c0 Change (rename) label

Interchange of Statements Example: t 1 := b + c t 2 := x + y Observation: We can interchange the statements without affecting the value of the block if and only if neither x nor y is t 1 and neither b nor c is t 2, i.e. we have two trees. Note: Normal-form basic blocks permit all statement interchanges that are possible. + t1t1 b0b0 c0c0 + t2t2 x0x0 y0y0

Algebraic Transformations Arithmetic Identities: x + 0 = 0 + x = x x – 0 = x x + 1 = 1 + x = x x / 1 = x - Replace left-hand side with simples right hand side. Associative/Commutative laws x + (y + z) = (x + y) + z x + y = y + x Reduction in strength: x ** 2 = x * x 2.0 * x = x + x x / 2 = x Replace an expensive operator with a cheaper one. Constant folding 2 * 3.14 = Evaluate constant expression at compile time` Methodology: Before we create a node of the DAG we check whether such a node exists modulo the above identities.

Global Data Flow Analysis To do code generation and optimization a compiler needs to collect information about the program as a whole and to distribute this information to each block in the flow-graph. Data flow analysis: the process by which the compiler collects data-flow information. - by setting up and solving systems of equations that relate information at various points in the program. Example (generic): out[S] = gen[S] U (in[S] – kill[S]) Information flowing after the execution of S Generated by execution of S Information flowing at the beginning of S Information killed by the exec. of S Note: Sometimes information flows backwards in[S] = … out[S] …

Points and Paths Points between two adjacent statements within a block before the first statement after the last statement Paths Consider all points in all the blocks. A path from p 1 to p n is a sequence of points p 1, …, p n such that for each i [1, n-1]: - pi is the point immediately preceding a statement and p i+1 is the point immediately following that statement in the same block, or - p i is the end of some block and p i+1 is the beginning of a successor block. d1: i:= m-1 d2: j:= n flow-graph B d1d1 d2d2 i:= m-1 j:= n State machine

Example of DFA Problem: Reaching Definitions Definition of variable x: is a statement that assigns or may assign a value to x. - unambiguous definitions: assignment to x or read & store a value in x from an I/O device. - ambiguous definitions: may define the value of x: procedure call with x as a parameter or global variable. assignment through a pointer. 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. Killing a definition of a variable x happens if between two points along the path there is a definition of x.

Reaching Definition (Cont) Inaccuracies: When defining reaching definition we sometimes allow inaccuracies. Undecidability: to decide in general whether each path in a flow graph can be taken is an undecidable problem. Conservative inaccuracy: an inaccuracy is conservative if it never leads to a change in what the program computes. - it is normally conservative to assume that a definition can reach a point even if it may not. - we allow definitions to pass through ambiguous definitions.

DF Analysis of Strongly Structured Programs gen[s] = {d} kill[s] = D a – {d} out[s] = gen[s] (in[s] \ kill[s]) gen[s] = gen[s 2 ] (gen[s1] – kill[s 2 ]) kill[s] = kill[s 2 ] (kill[s1] – gen[s 2 ]) in[s 1 ] = in[s], in[s 2 ] = out[s 1 ] out[s] = out[s 2 ] gen[s] = gen[s 1 ] gen[s 2 ] kill[s] = kill[s 1 ] kill[s 2 ] in[s 1 ] = in[s], in[s 2 ] = in[s] out[s] = out[s 1 ] U out[s 2 ] Sd: a:= b + c S S1S1 S2S2 SS1S1 S2S2 (a) (b) (c)

DFA of Strongly Structured Programs (Cont) Syntax directed definitions: the equations are syntax directed. - synthesized attributes: gen, kill, (out – depends on in) - inherited attributes: in ( in backward dir – out) Note: out[s] gen[s] (gen – definitions reaching end of S without following paths outside S) Loops & Fixpoints: Given gen[s], kill[s], in[s] we cannot simply use in[s] = in[s1].I = J O in[s1] = in[s] out[s1]O = G ( I – K) out[s] = out[s1]Take O 0 = out[s1] = gen[s1] (in[s1] – kill[s1])I 1 = J 1, O 1 = G ( J – K) I 2 = J G, O 2 = O 1 gen[s] = gen[s1] kill[s] = kill[s1] in[s1] = in[s] gen[s1] out[s] = out[s1] S (d) S1S1