Program Analysis Mooly Sagiv Tel Aviv University 640-6706 Sunday 18-21 Scrieber 8 Monday 10-12 Schrieber.

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

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.
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.
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.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Some Properties of SSA Mooly Sagiv. Outline Why is it called Static Single Assignment form What does it buy us? How much does it cost us? Open questions.
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.
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.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Lecture 15 – Dataflow Analysis Eran Yahav 1
More Dataflow Analysis CS153: Compilers Greg Morrisett.
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,
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Liveness Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html://
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Program analysis Mooly Sagiv html://
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
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.
Abstract Interpretation Part I Mooly Sagiv Textbook: Chapter 4.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Overview of program analysis Mooly Sagiv html://
1 Program Analysis Systematic Domain Design Mooly Sagiv Tel Aviv University Textbook: Principles.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University.
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.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Constant Propagation. The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set.
1 CS 201 Compiler Construction Data Flow Analysis.
1 Data-Flow Analysis Proving Little Theorems Data-Flow Equations Major Examples.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
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.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
1 Iterative Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
CS 598 Scripting Languages Design and Implementation 9. Constant propagation and Type Inference.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Optimization Simone Campanoni
Dataflow Analysis CS What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Data Flow Analysis Suman Jana
Textbook: Principles of Program Analysis
Fall Compiler Principles Lecture 8: Loop Optimizations
Iterative Program Analysis Abstract Interpretation
Topic 10: Dataflow Analysis
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
Static Single Assignment
Live variables and copy propagation
Live Variables – Basic Block
Presentation transcript:

Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber 317 Textbook: Dataflow Analysis Kill/Gen Problems Chapter 2

Kill/Gen Problems u A simple class of static analysis problems u Generalizes the reaching definition problem u The static information consist of sets of “dataflow” facts u Compute information on the history/future of computations u Generate a system of equations –Use union/intersection to merge information along different control flow paths –Find minimal/maximal solutions

The Reaching Definitions (Revisited) u RD exit (l) = (RD entry (l) - kill)  gen –assignments »kill([x := a] l ) = {(x, l’) | l’  Lab * } »gen ([x := a] l ) = {(x, l)} –skip »kill([skip] l ) =  »gen ([skip] l ) =  u RD entry (l) =  {l’  pred(l)} RD exit (l’)

Remark  B  # RD (RD entry (l) )= (RD entry (l) - kill(l))  gen(l)

Front-End Information (Reaching Definitions ) u init(S) - The label that S begins u final(S) - The labels that S end u flow(S) - The control flow graph of S u block(l) - The elementary block associated with l u FV(S) - The variables used in S u S * - The analyzed program

Flow Information in While u init:Stm  Lab * –init([x := a] l ) = l –init([skip] l ) = l –init(S 1 ; S 2 ) = init(S 1 ) –init(if [b] l then S 1 else S 2 ) = l –init(while [b] l do S) = l u final:Stm  P(Lab * ) –final([x := a] l ) = {l} –final([skip] l ) = {l} –final(S 1 ; S 2 ) = final(S 2 ) –final(if [b] l then S 1 else S 2 ) = final(S 1 )  final(S 2 ) –final(while [b] l do S) = {l}

Flow Information in While u flow:Lab  P(Lab *  Lab * ) –flow([x := a] l ) =  –flow([skip] l ) =  –flow(S 1 ; S 2 ) = flow(S 1 )  flow(S 2 )  {(l, l’) | l  final(S 1 ), l’ =init(S 2 )} –flow(if [b] l then S 1 else S 2 ) = flow(S 1 )  flow(S 2 )  {(l, l’) | l’ =init(S 1 )}  {(l, l’) | l’ =init(S 2 )} –flow(while [b] l do S) = flow(S)  {(l, l’) | l’ =init(S)}  {(l’, l) | l’  final(S)}

Elementary Blocks in While u [x := a] l –block(l)= [x := a] l u [skip] l –block(l)= [skip] l u [b] l –block(l)= [b] l

The System of Equations Reaching Definitions

The Power Program [z := 1] 1 ; while [x>0] 2 do ( [z:= z * x] 3 ; [x := x - 1] 4 )

The System of Equations [z := 1] 1 ; while [x>0] 2 do ([z:= z * x] 3 ; [x := x - 1] 4 )

Chaotic Iterations for l  Lab * do RD entry (l) :=  RD exit (l) :=  RDentry(init(S * )) := {(x, ?) | x  FV(S * )} WL= Lab * while WL !=  do Select and remove an arbitrary l  WL if (temp != RD exit (l)) RD exit (l) := temp for l' such that (l,l')  flow(S*) do RD entry (l') := RD entry (l')  RD exit (l) WL := WL  l’

Available Expressions u The computation of a complex program expression e at a program point l can be avoided if: –e is computed on every path to l and none of its arguments are changed –e is side effect free u A simple example u e can be saved in a temporary variable/register u For simplicity only consider “formal” expressions [x := a+b] 1 ; [y := a*b] 2 ; while [y >a+b] 3 do ( [a:= a + 1] 4 ; x := a+b] 5 )

The Largest Conservative Solution u It is undecidable to compute the exact available expressions u Compute a conservative approximation u We are looking for a maximal set of available expressions u Example [x := a +b] 1 while [true] 2 do [skip] 3 u Minimal set of “missed” expressions u The problem is a must problem –An expression e is available at l if e must be computed on all the paths leading to l

Front-End Information (Available Expressions ) u init(S) - The label that S begins u final(S) - The labels that S end u flow(S) - The control flow graph of S u block(l) - The elementary block associated with l u AExp(S) - The set of formal expressions in S u FV(e) - The variables used in the expression e u S * - The analyzed program

The System of Equations Available Expressions

Remark  B  # AE (AE entry (l) )= (AE entry (l) - kill(l))  gen(l)

An Example [x := a+b] 1 ; [y := a*b] 2 ; while [x>0] 3 do ( [z:= z * x] 4 ; [x := x - 1] 5 )

Chaotic Iterations for l  Lab * do AE entry (l) := AExp(S * ) AE exit (l) := AExp(S * ) AE entry (init(S * )) :=  WL= Lab * while WL !=  do Select and remove an arbitrary l  WL if (temp != AE xit (l)) AE exit (l) := temp for l' such that (l,l')  flow(S*) do AE entry (l') := AE entry (l')  AE exit (l) WL := WL  l’

Dual Available Expressions u It is possible to compute available expressions representing those expressions that may-not-be available u An expression e is not-available at l if e may-not be computed on some of the paths leading to l u This is may problem u The smallest set is computed

The System of Equations Dual Available Expressions

Backward(Future) Data Flow Problems u So far we saw two examples of analysis problems where we collected information about past computations u Many interesting analysis problems we are interested to know about the future u Hard for dynamic analysis!

Liveness Data Flow Problems u A variable x may be live at l if there may be an execution path from l in which the value of x is used prior to assignment u An Example: [x := 2;] 1 [y := 4;] 2 [x := 1;] 3 if [ y>x] 4 then [z :=y] 5 else [z := y*y] 6 [x :=z] 7 u Usage of liveness –register allocation –dead code elimination –more precise garbage collection –uninitialized variables

The System of Liveness Equations

Example [x := 2;] 1 [y := 4;] 2 [x := 1;] 3 if [y>x] 4 then [z :=y] 5 else [z := y*y] 6 [x :=z] 7

Chaotic Iterations for l  Lab * do LV entry (l) :=  LV exit (l) :=  for l  final(S *) do LV exit (l) :=  WL= Lab * while WL !=  do Select and remove an arbitrary l  WL if (temp != LV entry (l)) LV entry (l) := temp for l' such that (l’,l)  flow(S*) do LV exit (l') := LV eexit (l')  LV entry (l) WL := WL  l’

Characterization of Dataflow Problems u Direction of flow – forward –backward u Initial value u Control flow merge –May(union) –Must (intersection) u Solution –Smallest –Largest

Backward (Future)Must Problem Very Busy Expressions u An expression e is very busy at l if its value at l must be used in all the paths from l u Example program if [a>b] 1 then [x :=b-a] 2 [y := a-b] 3 else [y :=b-a] 4 [x := a-b] 5 u Usage of Very Busy Expressions –Reduce code size –Increase speed u Find the largest solution

The System of Very Busy Equations

Chaotic Iterations for l  Lab * do VB ntry (l) := AExp(S * ) VB exit (l) := AExp(S * ) for l  final(S *) do VB exit (l) :=  WL= Lab * while WL !=  do Select and remove an arbitrary l  WL if (temp != VB entry (l)) VB entry (l) := temp for l' such that (l’,l)  flow(S*) do VB exit (l') := VB eexit (l')  VB entry (l) WL := WL  l’

Bit-Vector Problems u Kill/Gen problems are also called Bit-Vector problems u Y = (X - Kill)  Gen u Y[i]=(X[i]  Kill[i])  Gen[i] u Every component can be computed individually (in linear time)

Non Kill/Gen Problems u truly-live variables –A variable v may be truly live at l if there may be an execution path to l in which v is (transitively) used prior to assignment in a print statement u May be “garbage” (uninitialized) –A variable v may be garbage at l if there may be an execution path to l in which v is either not initialized or assigned using an expression with occurrences of uninitialized variables

Non Kill/Gen Problems(Cont) u May-points-to –A pointer variable p may point to a variable q at l if there may be an execution path to l in which p holds the address of q u Sign Analysis –A variable v must be positive/negative l if on all execution paths to l v has positive/negative value u Constant Propagation –A variable v must be a constant at l if on all execution paths to l v has a constant value

Conclusions u Kill/Gen Problems are simple useful subset of dataflow analysis problems u Easy to implement