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

Slides:



Advertisements
Similar presentations
Continuing Abstract Interpretation We have seen: 1.How to compile abstract syntax trees into control-flow graphs 2.Lattices, as structures that describe.
Advertisements

SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Interprocedural Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday
3-Valued Logic Analyzer (TVP) Tal Lev-Ami and Mooly Sagiv.
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.
A Deeper Look at Data-flow Analysis Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University.
Lecture 15 – Dataflow Analysis Eran Yahav 1
Lecture 02 – Structural Operational Semantics (SOS) Eran Yahav 1.
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Program analysis Mooly Sagiv html://
CS 536 Spring Global Optimizations Lecture 23.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
From last time: live variables Set D = 2 Vars Lattice: (D, v, ?, >, t, u ) = (2 Vars, µ, ;,Vars, [, Å ) x := y op z in out F x := y op z (out) = out –
1 Iterative Program Analysis Part I Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
Data Flow Analysis Compiler Design Nov. 3, 2005.
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 Iterative Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program 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.
Interprocedural Analysis Noam Rinetzky Mooly Sagiv Tel Aviv University Textbook Chapter 2.5.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
Data Flow Analysis Compiler Design Nov. 8, 2005.
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs, Data-flow Analysis Data-flow Frameworks --- today’s.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
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.
From last lecture We want to find a fixed point of F, that is to say a map m such that m = F(m) Define ?, which is ? lifted to be a map: ? = e. ? Compute.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Overview of program analysis Mooly Sagiv html://
1 Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis: Data-flow frameworks –Classic.
1 Tentative Schedule u Today: Theory of abstract interpretation u May 5 Procedures u May 15, Orna Grumberg u May 12 Yom Hatzamaut u May.
Constant Propagation. The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set.
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
Example x := read() v := a + b x := x + 1 w := x + 1 a := w v := a + b z := x + 1 t := a + b.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:
Compiler Principles Fall Compiler Principles Lecture 11: Loop Optimizations Roman Manevich Ben-Gurion University.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
1 Iterative Program Analysis Mooly Sagiv Tel Aviv University Textbook: Principles of Program.
1 Iterative Program Analysis Abstract Interpretation Mooly Sagiv Tel Aviv University Textbook:
1 Numeric Abstract Domains Mooly Sagiv Tel Aviv University Adapted from Antoine Mine.
CS 598 Scripting Languages Design and Implementation 9. Constant propagation and Type Inference.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis: Data-flow frameworks –Classic.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
1 Iterative Program Analysis Part II Mathematical Background Mooly Sagiv Tel Aviv University
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Chaotic Iterations Mooly Sagiv Tel Aviv University Textbook: Principles of Program Analysis.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Dataflow Analysis CS What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,
Compiler Principles Fall Compiler Principles Lecture 8: Dataflow & Optimizations 1 Roman Manevich Ben-Gurion University of the Negev.
Program Analysis Last Lesson Mooly Sagiv. Goals u Show the significance of set constraints for CFA of Object Oriented Programs u Sketch advanced techniques.
Textbook: Principles of Program Analysis
G. Ramalingam Microsoft Research, India & K. V. Raghavan
Symbolic Implementation of the Best Transformer
Iterative Program Analysis Abstract Interpretation
Another example: constant prop
Data Flow Analysis Compiler Design
Presentation transcript:

Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber 317 Textbook: Dataflow Analysis Chapter 2 & Appendix A Monotone Frameworks and Precision May 19, 10am Exam?

Outline u Monotone Dataflow Frameworks u Precision of Data Flow Analysis u Beyond Monotone Frameworks

Complete Lattices u The Poset (L,  ) such that every subset S  L  S and  S are both defined is called complete lattice u Denoted by (L,  ) = (L, , , , ,  ) –  is the minimum value –  =   =  L –  is the maximum value –  =  L =  

Monotone Frameworks u Generalizes Kill/Gen Problems u A complete lattice (L, , , , ,  ) describes the “potential pieces of information” u The initial value at entry is specified by  L u The effect of every basic block at l is described by a monotone function f l :L  L (transfer function) u Solve the following system of equations (forward)

Monotone Backward Frameworks u A complete lattice (L, , , , ,  ) describes the “potential pieces of information” u The initial value at exit is specified by  L u The effect of every basic block at l is described by a monotone function f l :L  L (transfer function) u Solve the following system of equations

Instances of Monotone Frameworks u Kill/Gen Problems –  =  or  =  –f l (entry(l)) = (entry (l) - kill(l))  gen(l) u Forward –May be uninitialized (garbage) variables –Constant propagation –Sign-analysis –Parity analysis –Points-to analysis u Backward –Truly-live variables

May-be-garbage variables u A variable may-be-garbage at a label l if there may be a path to l in which it is either uninitialized or set using an uninitialized variable [x := 5] 1 ; if [z > 2] 2 then [y := 17] 3 ; else [skip] 4 ; [t := y + x] 5 ;

May-be-garbage variables(cont) u L = (P(Var * ), , , , , Var * ) u Initial value  =Var * u Transfer functions

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

Over Conservative Solution if [y >1] 1 then [z := 1] 2 ; … if [y >1] 3 then [y := z] 4 ;

Constant Propagation u Determine variables with constant values u Information Lattice –Extended integer lattice (L 1,  1,  1,  1,  1,  1 ) » L 1 = Z  {  1,  1 } »  1  1 z  1  1 –Define L = (S  L 1,  ) where S=Var * –Initial value  ? u Transfer functions A cp : AExp  (L  L 1 )

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

[y := 5] 1 ; [x := 8] 2 ; while [x>1] 3 do ( [x:= y+3] 4 ; [x := (y * y) -17] 5 ; )

Parity Analysis u Determine parity of a single variable x u Information Lattice –({E, O, ,  }, , , , ,  ) »   z   » ‘E’ denotes the fact that x is even »‘O’ denotes the fact that is x is odd –Initial value  =  –Transfer functions »Expressions »Statements »Conditions

Example Program while [x !=1] 1 do if [ (x %2) = 0] 2 then [x := x / 2;] 3 else [x := x * 3 + 1;] 4

Example Program while /*[x   ]*/ [x !=1] 1 /*[x   ]*/ do if /*[x   ]*/ [ (x %2) = 0] 2 then /*[x  E]*/ [x := x / 2;] 3 /*[x   ]*/ else /*[x  O]*/ [x := x * 3 + 1;] 4 /*[x  E]*/

The PWhile Programming Language Abstract Syntax a := x | *x | &x | n | a 1 op a a 2 b := true | false | not b | b 1 op b b 2 | a 1 op r a 2 S := [x := a] l | [*x := a] l | [skip] l | S 1 ; S 2 | if [b] l then S 1 else S 2 | while [b] l do S

Points-To Analysis u Determine if a pointer variable p may point to q u L = (P(Var *  Var * ), , , , , Var *  Var * ) u (x, y)  l --- x may hold the address of y u The initial value  =  u Transfer functions

Usage of Points-To-Information u “Adapt” other optimizations –Constant propagation x = 5; *p = 7 ; … x … u Pointer aliases –Variables p and q are may-aliases at l if the points-to set at l contains entries (p, x) and (q, x) u Side effect analysis *p = *q + * * t

[t := &a] 1 ; [y := &b] 2 ; [z := &c] 3 ; if [x> 0] 4 ; then [p:= &y] 5 ; else [p:= &z] 6 ; [*p := t] 7 ;

/*  */ [t := &a] 1 ; /* {(t, a)}*/ /* {(t, a)}*/ [y := &b] 2 ; /* {(t, a), (y, b) }*/ /* {(t, a), (y, b)}*/ [z := &c] 3 ;/* {(t, a), (y, b), (z, c) }*/ if [x> 0] 4 ; then [p:= &y] 5 ; /* {(t, a), (y, b), (z, c), (p, y)}*/ else [p:= &z] 6 ; /* {(t, a), (y, b), (z, c), (p, z)}*/ /* {(t, a), (y, b), (z, c), (p, y), (p, z)}*/ [*p := t] 7 ; /* {(t, a), (y, b), (y, c), (p, y), (p, z), (y, a), (z, a)}*/

Chaotic Iterations for forward problems for l  Lab * do DF entry (l) :=  DF exit (l) :=  DF entry (init(S * )) :=  WL= Lab * while WL !=  do Select and remove an arbitrary l  WL if (temp != DF exit (l)) DF exit (l) := temp for l' such that (l,l')  flow(S*) do DF entry (l') := DF entry (l')  DF exit (l) WL := WL  {l’}

Chaotic Iterations for backward problems for l  Lab * do DF entry (l) :=  DF exit (l) :=  for l  final(S * )) do DF exit (l) :=  WL= Lab * while WL !=  do Select and remove an arbitrary l  WL if (temp != DF entry (l)) DF entry (l) := temp for l' such that (l’, l)  flow(S*) do DF exit (l') := DF exit (l')  DF entry (l) WL := WL  {l’}

Complexity of Chaotic Iterations u Parameters: –|Lab| labels –k is the maximum outdegree of flow(S*) –A lattice of height h –c is the maximum cost of »applying f l »  »L comparisons u Complexity O(|Lab| h * c * k)

Soundness of Chaotic Iterations u define a monotone abstraction  : Collecting- States  L u Show that for every l: –  ({  [b] l  (s) | s  CS })  f l (  (CS)) u Conclude that the DF solution of Chaotic iterations satisfies for every l: –  (CS entry (l))  DF entry (l) –  (CS exit (l))  DF exit (l) u But it may be that Chaotic iterations yield DF entry (l) =  and yet  (CS entry (l))=  u How to measure precision?

Precision of Chaotic Iterations u Optimal –  (CS entry (l)) = DF entry (l) –  (CS exit (l)) = DF exit (l) u Join-over-all-paths - No loss of information w.r.t. straight line code u Relatively optimal (induced) w.r.t. the abstraction  u Compare at run-time u Good enough for the used optimization

The Join-Over-All-Paths (JOP) u Let paths(init(S * ), l) denote the potentially infinite set of paths from init(S * ) to l (written as sequences of labels) u For a sequence of labels [l 1, l 2, …, l n ] define f [l 1, l 2, …, l n ]: L  L by composing the effects of basic blocks –f[l](s)=s –f [l, p](s) = f[p] (f l (s)) u JOP l =  {f[l 1, l 2, …, l](  ) [l 1, l 2, …, l]  paths(init(S * ), l)}

JOP vs. Least Solution u The DF solution obtained by Chaotic iteration satisfies for every l: –JOP l  DF entry (l) u If every f l is additive (distributive) for all the labels l –JOP l = DF entry (l)

Additive (Distributive) Monotone Problems u Kill/Gen Problems u May-be uninitialized u Truly-live variables u Constant Propagation when every expression in the right hand side contains at most one variable u Points-to analysis with one level pointers

Non Distributive Monotone Problems u Points-to-analysis u Constant Propagation on arbitrary expressions u Parity Propagation on arbitrary expressions

Converting into Distributive Frameworks u Consider –a finite lattice (L, , , , ,  ) –An initial value at entry  L –The effect of every basic block at l is described by a monotone function f l :L  L (transfer function) –Define a distributive function on (P(L), , , , , L) by –Solve the following system of equations over P(L)

The Constant Propagation u It is undecidable to find the JOP in the constant propagation problem u A Sketch of a proof while (...) ( if (...) x_1 = x_1 + 1; if (...) x_2 = x_2 + 1;... if (...) x_n = x_n + 1; } y = truncate (1/ (1 + p 2 (x_1, x_2,..., x_n)) /* Is y=0 here? */

Static Analysis problems beyond Monotone Frameworks u Infinite heights –integer intervals –Linear relationships between variables u Bi-directional problems –Partial-redundant expressions –Automatic inference of variable types in imperative (weakly typed) programs x := b[z] a [b[y]] := x u Procedures

Historical Perspective u 1973 Kildall defined the basic framework but required distributive frameworks u 1976 Kam and Ulmann defined Monotone Framework u 1980 Tarjan suggested an almost linear time algorithm for reducible flow graph u 1980 Rosen suggested a linear time algorithm for high level language

Conclusions u Many dataflow problems can be solved via the Chaotic Iteration Algorithm u Provide a tool to understand precision