Program Analysis. Readings

Similar documents
Class 6. Review; questions Assign (see Schedule for links) Slicing overview (cont d) Problem Set 3: due 9/8/09. Program Slicing

Review; questions Basic Analyses (2) Assign (see Schedule for links)

Data Flow Information. already computed

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Lecture 4 Introduction to Data Flow Analysis

Lecture 2. Introduction to Data Flow Analysis

Data-flow Analysis - Part 2

Today s Lecture. A Sample Ada Program to Test. Flow Graphs. Lecture 25: Data Flow and Dependence Graphs. White-Box Testing.

Why Global Dataflow Analysis?

Example (cont.): Control Flow Graph. 2. Interval analysis (recursive) 3. Structural analysis (recursive)

Data Flow Analysis. CSCE Lecture 9-02/15/2018

Department of Computer Sciences CS502. Purdue University is an Equal Opportunity/Equal Access institution.

An Efficient Algorithm for Computing all Program Static Slices

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

ELEC 876: Software Reengineering

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Lecture 6 Foundations of Data Flow Analysis

Lecture 6 Foundations of Data Flow Analysis

Compiler Optimization and Code Generation

Program Representations. Xiangyu Zhang

Introduction. Data-flow Analysis by Iteration. CSc 553. Principles of Compilation. 28 : Optimization III

Data Flow Analysis. Program Analysis

A main goal is to achieve a better performance. Code Optimization. Chapter 9

Understanding Data Dependences in the Presence of Pointers

Dataflow analysis (ctd.)

Compiler Construction 2009/2010 SSA Static Single Assignment Form

Static Slicing of Threaded Programs

COSE312: Compilers. Lecture 20 Data-Flow Analysis (2)

An Approach to Regression Testing using Slicing

Control Flow Analysis. Reading & Topics. Optimization Overview CS2210. Muchnick: chapter 7

PROGRAM SLICING OF EXPLICITLY PARALLEL PROGRAMS. by Matthew Bridges

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

Barrier Slicing and Chopping

Program Slicing for Refactoring

Control-Flow Analysis

Why Data Flow Models? Dependence and Data Flow Models. Learning objectives. Def-Use Pairs (1) Models from Chapter 5 emphasized control

A classic tool: slicing. CSE503: Software Engineering. Slicing, dicing, chopping. Basic ideas. Weiser s approach. Example

Static Interprocedural Slicing of Shared Memory Parallel Programs *

Slicing of Object-Oriented Software

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Control flow and loop detection. TDT4205 Lecture 29

A Domain-Specific Language for Generating Dataflow Analyzers

A PDG-Based Tool and Its Use in Analyzing Program Control Dependences *

Conditioned Slicing. David Jong-hoon An. May 23, Abstract

Reaching Definitions and u-d Chaining M.B.Chandak

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Program Structure. Simple Statements. Program consists of blocks Block. Can be represented by a flow graph. Sequence of statements

Program Structure. Simple Statements. Program consists of blocks Block. Can be represented by a flow graph. Sequence of statements

Lecture Notes: Dataflow Analysis Examples

Slicing. Rupesh Nasre. CS6843 Program Analysis IIT Madras Jan 2016

Data Flow Analysis. Daqing Hou

Static Slicing in the Presence of GOTO Statements. IBM T. J. Watson Research Center. P. O. Box 704. Yorktown Heights, NY

Advanced Compilers Introduction to Dataflow Analysis by Example. Fall Chungnam National Univ. Eun-Sun Cho

Applications. Slicing. Program with Multiple Functionality. Outline. Definition. Program with Single Functionality

MIT Introduction to Dataflow Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Interprocedural Slicing Using Dependence Graphs

Student Project: System Dependency Graphs in Earendil

CSCE 747 Assignment 2: Structural and Data Flow Testing

Liveness Analysis and Register Allocation. Xiao Jia May 3 rd, 2013

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

CS 510/12. Xiangyu Zhang

Live Variable Analysis. Work List Iterative Algorithm Rehashed

CS 6353 Compiler Construction, Homework #3

Precise Executable Interprocedural Slices

Program Static Analysis. Overview

Inter-procedural static slicing using advanced caching algorithm

The Use of Program Dependence Graphs in Software Engineering

Control Flow Analysis & Def-Use. Hwansoo Han

Example of Global Data-Flow Analysis

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

Compilation 2013 Liveness Analysis

compile-time reasoning about the run-time ow represent facts about run-time behavior represent eect of executing each basic block

Algorithms: Lecture 10. Chalmers University of Technology

Calvin Lin The University of Texas at Austin

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

Lecture 2: Control Flow Analysis

Efficient Program Slicing Algorithms for Measuring Functional Cohesion and Parallelism

Areas related to SW verif. Trends in Software Validation. Your Expertise. Research Trends High level. Research Trends - Ex 2. Research Trends Ex 1

Lecture 4. More on Data Flow: Constant Propagation, Speed, Loops

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

CMSC430 Spring 2009 Midterm 2 (Solutions)

Flow Analysis. Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM

Introduction to Machine-Independent Optimizations - 6

Statements or Basic Blocks (Maximal sequence of code with branching only allowed at end) Possible transfer of control

The Use of Program Dependence Graphs in Software Engineering

Program Integration for Languages with Procedure Calls

Control Flow Analysis

3/18/18. Program Analysis. CYSE 411/AIT 681 Secure Software Engineering. Learning Goal. Program Analysis on Security. Why Program Representations

Topic I (d): Static Single Assignment Form (SSA)

Program Slicing in the Presence of Pointers (Extended Abstract)

Challenges in the back end. CS Compiler Design. Basic blocks. Compiler analysis

More Dataflow Analysis

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

Inter-Procedural Static Slicing Using Advanced Caching Algorithm *

Data Flow Analysis and Computation of SSA

Properties of Criteria. 1. Applicability Property. Criteria

CS202 Compiler Construction

CYSE 411/AIT 681 Secure Software Engineering Topic #14. Program Analysis

Transcription:

Program Analysis Class #2 Readings he Program Dependence Graph and Its Use in Optimization Program slicing A Survey of Program Slicing echniques Dragon book

Program Analysis Data-low Analysis (wrap up) Recall: Dominance (algorithm) Input: N, pred, entry Output: domin: n {n} domin(entry) = {entry} foreach n N - {entry} domin(n) = N label: change = false foreach n N - {entry} = p pred(n) domin(p) D = {n} if D domin(n) change = true domin(n) = D if change = true goto label return domin exit S2 entry S S5 S3 S4 CG S6 2

Dominance as a D Problem Local information: GEN[B] KILL[B] By propagation: IN[B] OU[B] S2 entry S S3 CG. Initialize IN[B], OU[B] sets 2. Iterate over all B until no changes On each iteration, visit all B, and compute IN[B], OU[B] as exit S5 S4 S6 IN[B] B GEN[B] KILL[B] OU[B] Dominance as a D Problem Local information: GEN[B] = node itself KILL[B] = empty By propagation: IN[B] = dominators of nodes in pred(b) OU[B] = dominators of B S2 entry S S3 CG. Initialize IN[B], OU[B] sets 2. Iterate over all B until no changes On each iteration, visit all B, and compute IN[B], OU[B] as exit S5 S4 S6 IN[B] IN[B] = OU(P), P in pred(b) OU[B] = IN(B) U (GEN[B] - KILL[B]) B GEN[B] KILL[B] OU[B] 3

Other D Approaches (worklist) 2 3 4 return m 7 fib(m) f0=0 f= m<= 2 6 return f2 i=2 i<=m 5 Data-flow for nodes, 2, 3 never changes but is computed on every iteration of the algorithm f2=f0+f f0=f f=f2 i=i+ In general, nodes involved in the computation may be a small subset of the nodes in the 8 graph 9 or example, what if I want to compute 0 RD only for f? Other D Approaches (worklist) algorithm RDWorklist Input: GEN[B], KILL[B] for all B Output: reaching definitions for each B Method: initialize IN[B], OU[B] for all B; add successors of B initially involved in computation to worklist W repeat remove B from W Oldout=OU[B] compute IN[B], OU[B] if oldout!= OU[B] then add successors of B to W endif until W is empty 4

Other D Approaches (worklist) 2 3 4 return m 7 fib(m) f0=0 f= m<= 2 6 return f2 i=2 i<=m Compute RD for f using RDWorklist GEN[3] is {3}, GEN[0] is {0}, KILL[3] is {0}, KILL[0] is {3} Add successors of 3, 0 to W remove 4 from W, compute IN[4], OU[4], etc. 5 f2=f0+f f0=f f=f2 9 8 0 i=i+ Program Analysis Dependence Analysis 5

Dependence Analysis Important for Optimization Instruction scheduling Data-cache optimization Software engineering Program understanding Reverse engineering Debugging wo main kinds of dependences Data related Control related Data-Dependence Graph (DDG) A DDG has one node for every variable (basic block) and one edge representing the flow of data between two nodes Different types of data dependences low: def to use Anti: use to def Output: def to def Input: use to use urther classifiable as Loop-carried: requires iterations Loop-independent: occurs anyway B2 B3 X = Z > 2 Y = X + entry Z > B exit B5 B6 X = 2 B4 Z = X 3 X = 4 Z = X + 7 6

Data-Dependence Graph entry entry Z Z Z > B Z > B B2 B3 X = Z > 2 X X X Y = X + Z Y B5 X = 2 B4 X Z = X 3 X = 4 B2 B3 X = Z > 2 Y = X + B5 X = 2 B4 Z = X 3 X = 4 Y B6 X X Z = X + 7 B6 Z = X + 7 exit Z exit Control-Dependence Analysis Definition Let G be a CG, with X and Y nodes in G Y is control-dependent on X iff. here exists a path P from X to Y with any Z in P (excluding X and Y) postdominated by Y and 2. X is not postdominated by Y B2 X = Z > 2 entry Z > B X = 2 B4 (intuitively: there are two edges out of X; traversing one edge always leads to Y, the other may not lead to Y) B3 Y = X + B5 B6 Z = X 3 X = 4 Z = X + 7 exit 7

Control-Dependence Analysis What are the control dependences for each statement in the CG on the right? entry Z > B B2 X = Z > 2 X = 2 B4 B3 Y = X + B5 Z = X 3 X = 4 B6 Z = X + 7 exit Control-Dependence Analysis What are the control dependences for each statement in the CG on the right? Dependences: B, exit entry B2 B B3 B2 B4 B B5 B2, B B6 B2, B B2 B3 X = Z > 2 Y = X + entry Z > B B5 B6 X = 2 B4 Z = X 3 X = 4 Z = X + 7 exit 8

Computing CD Using OW. Construct basic CDG 2. Add region nodes Computing CD Using OW. Construct basic CDG 2. Add region nodes 9

Computing CD Using OW. Build a CG CG En 2 3 4 5 Ex 6 Computing CD Using OW. Build a CG.2 Augment the CG by adding a node Start with edge (Start, entry) labeled and edge (Start, exit) labeled ; call this AugCG CG En 2 4 3 5 6 Ex 0

Computing CD Using OW. Build a CG.2 Augment the CG by adding a node Start with edge (Start, entry) labeled and edge (Start, exit) labeled ; call this AugCG AugCG En Start 2 4 3 5 6 Ex Computing CD Using OW. Build a CG.2 Augment the CG by adding a node Start with edge (Start, entry) labeled and edge (Start, exit) labeled ; call this AugCG.3 Construct the postdominator tree for AugCG AugCG En Start 2 4 3 5 6 Ex

Computing CD Using OW. Build a CG.2 Augment the CG by adding a node Start with edge (Start, entry) labeled and edge (Start, exit) labeled ; call this AugCG.3 Construct the postdominator tree for AugCG Pdom ree Start Ex 2 3 6 5 4 En Computing CD Using OW.4 Consider set S of edges (m, n) in AugCG such that n does not postdominate m Pdom ree Start Ex 2 3 6 5 4 En AugCG En Start 2 4 3 5 6 Ex 2

Computing CD Using OW.4 Consider set S of edges (m, n) in AugCG such that n does not postdominate m AugCG En Start 2 4 3 5 6 Ex Computing CD Using OW.4 Consider set S of edges (m, n) in AugCG such that n does not postdominate m.5 Consider, for each edge (m, n) in S, those nodes in the Pdom tree from n to least common ancestor L of m and n Including L if L is m Excluding L if L is not m AugCG En Start 2 4 3 5 6 Ex 3

Computing CD Using OW.5 Consider, for each edge (m, n) in S, those nodes in the Pdom tree from n to least common ancestor L of m and n Including L if L is m Excluding L if L is not m Edge L Nodes Start, En, 2, 4 Ex 4, 5, 6 2, 3 2, 5 Start Ex 2 3 6 Pdom ree 5 4 En Computing CD Using OW.5 Consider, for each edge (m, n) in S, those nodes in the Pdom tree from n to least common ancestor L of m and n Including L if L is m Excluding L if L is not m Edge L Nodes Start, En Ex En,, 2 Ex 2, 4 Ex 4, 5, 6 2, 3 Ex 3 2, 5 Ex 5, 6 Start Ex 2 3 6 Pdom ree 5 4 En All identified nodes are control dependent on m 4

Computing CD Using OW.5 Consider, for each edge (m, n) in S, those nodes in the Pdom tree from n to least common ancestor L of m and n Including L if L is m Excluding L if L is not m Edge L Nodes CD on Start, En Ex En, Start,, 2 Ex 2,, 4 Ex 4, 5, 6, 2, 3 Ex 3 2, 2, 5 Ex 5, 6 2, Start Ex 2 3 6 Pdom ree 5 4 En All identified nodes are control dependent on m Why is the set we obtain correct? Computing CD Using OW.6 Create the actual CDG En Start 2 CDG 3 6 5 4 Edge L Nodes CD on Start, En Ex En, Start,, 2 Ex 2,, 4 Ex 4, 5, 6, 2, 3 Ex 3 2, 2, 5 Ex 5, 6 2, 5

Program-Dependence Graph (PDG) A PDG for a program P is the combination of the data- and control- dependence graphs for P A PDG contains nodes representing statements in P and edges representing either control or data dependence between nodes PDG exercise x = ; 2 y = 2; 3 if (c) 4 x++; 5 while(d) { 6 x--; 7 y += ; } 8 printf( %d, y); Compute the PDG for the program on the left 6

Program Analysis Slicing Program Slicing Slicing overview ypes of slices, levels of slices Methods for computing slices Interprocedural slicing 7

Some History. Mark Weiser, 98 Experimented with programmers to show that slices are: he mental abstraction people make when they are debugging a program [Weiser] Used Data low Equations 2. Ottenstein & Ottenstein PDG, 984 3. Horowitz, Reps & Binkley SDG, 990 Applications Debugging Program Comprehension Reverse Engineering Program esting Measuring Program Metrics Coverage, Overlap, Clustering Refactoring 8

Static VS Dynamic Slicing Static Slicing Statically available information only No assumptions on input Computed slice in general inaccurate Approximations results may be useless Dynamic Slicing Computed on a given input ypically more precise more useful for applications such as debugging and testing ypes of Slicing (backward) A backward slice of a program with respect to a program point p and set of program variables V consists of all statements and predicates in the program that may affect the value of variables in V at p he program point p and the variables V together form the slicing criterion, usually written <p, V> 9

ypes of Slicing (backward) General approach: backward traversal of program flow Slicing starts from point p (C = (p, V)) Examines statements that are executed before p ( statements that appear before p ) Adds statements that affect value of V at p, or execution of p ransitive dependences are considered ypes of Slicing (backward). read (n) 2. i := 3. sum := 0 4. product := 5. while i <= n do 6. sum := sum + i 7. product := product * i 8. i := i + 9. write (sum) 0. write (product) Criterion <0, product> 20

ypes of Slicing (backward). read (n) 2. i := 3. sum := 0 4. product := 5. while i <= n do 6. sum := sum + i 7. product := product * i 8. i := i + 9. write (sum) 0. write (product) Criterion <0, product> ypes of Slicing (executable) A slice is executable if the statements in the slice form a syntactically correct program that can be executed. If the slice is computed correctly (safely), running the executable slice produces the same result for variables in V at p for all inputs. 2

ypes of Slicing (executable). read (n) 2. i := 3. sum := 0 4. product := 5. while i <= n do 6. sum := sum + i 7. product := product * i 8. i := i + 9. write (sum) 0. write (product) Criterion <0, product> ypes of Slicing (executable). read (n) 2. i := 3. sum := 0 4. product := 5. while i <= n do 6. sum := sum + i 7. product := product * i 8. i := i + 9. write (sum) 0. write (product) Criterion <0, product> 22

ypes of Slicing (executable) read (n) i := Criterion <0, product> product := while i <= n do product := product * i i := i + write (product) ypes of Slicing (forward) A forward slice of a program with respect to a program point p and set of program variables V consists of all statements and predicates in the program that may be affected the value of variables in V at p he program point p and the variables V together form the slicing criterion, usually written <p, V> 23

ypes of Slicing (forward) General approach: forward traversal of program flow Slicing starts from p (C = (p, V)) Examine all statements that are executed after p Keep statements that are affected by the values of V at p or by the execution of p ransitive dependences considered Shows downstream code that depend on a specific variable or statement Can show the code affected by a modification ypes of Slicing (forward). read (n) 2. i := 3. sum := 0 4. product := 5. while i <= n do 6. sum := sum + i 7. product := product * i 8. i := i + 9. write (sum) 0. write (product) Criterion <3, sum> 24

ypes of Slicing (forward). read (n) 2. i := 3. sum := 0 4. product := 5. while i <= n do 6. sum := sum + i 7. product := product * i 8. i := i + 9. write (sum) 0. write (product) Criterion <3, sum> ypes of Slicing (forward). read (n) 2. i := 3. sum := 0 4. product := 5. while i <= n do 6. sum := sum + i 7. product := product * i 8. i := i + 9. write (sum) 0. write (product) Criterion <, n> 25

ypes of Slicing (forward). read (n) 2. i := 3. sum := 0 4. product := 5. while i <= n do 6. sum := sum + i 7. product := product * i 8. i := i + 9. write (sum) 0. write (product) Criterion <, n> Methods for Computing Slices Data-flow on the flow graph Intraprocedural: CG Interprocedural: ICG Reachability on a dependence graph Intraprocedural: program-dependence graph (PDG) Interprocedural: system-dependence graph (SDG) 26

Methods for Computing Slices Data-flow on the flow graph Intraprocedural: CG Interprocedural: ICG Reachability on a dependence graph Intraprocedural: program-dependence graph (PDG) Interprocedural: system-dependence graph (SDG) Slicing as a reachability on the PDG Slicing criterion: node n in the PDG Simple method: slice = all nodes from which n is reachable Backward traversal of the PDG 27

Slicing as a reachability on the PDG What is the slice for <y, 8>? x = ; 2 y = 2; 3 if (c) 4 x++; 5 while(d) { 6 x--; 7 y += ; } 8 printf( %d, y); Slicing as a reachability on the PDG What is the slice for <y, 8>? Now let s do it using the PDG x = ; 2 y = 2; 3 if (c) 4 x++; 5 while(d) { 6 x--; 7 y += ; } 8 printf( %d, y); 28

Slicing as a reachability on the PDG What is the slice for <y, 8>? entry 2 3 5 8 4 6 7 Slice = backward reachable nodes x = ; 2 y = 2; 3 if (c) 4 x++; 5 while(d) { 6 x--; 7 y += ; } 8 printf( %d, y); 29