0% found this document useful (0 votes)
30 views26 pages

Data-Flow Analysis - Part 2: Y.N. Srikant

This document discusses data-flow analysis techniques for deriving information about the flow of data along program execution paths. It describes the reaching definitions problem, which determines which variable definitions may reach a given program point. An iterative algorithm is presented to solve the reaching definitions problem using a bit vector representation. Use-definition chains are also discussed, which store reaching definitions linked to each variable use. Available expression analysis and live variable analysis are also introduced as other examples of data-flow analysis problems.

Uploaded by

Dilip TheLip
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views26 pages

Data-Flow Analysis - Part 2: Y.N. Srikant

This document discusses data-flow analysis techniques for deriving information about the flow of data along program execution paths. It describes the reaching definitions problem, which determines which variable definitions may reach a given program point. An iterative algorithm is presented to solve the reaching definitions problem using a bit vector representation. Use-definition chains are also discussed, which store reaching definitions linked to each variable use. Available expression analysis and live variable analysis are also introduced as other examples of data-flow analysis problems.

Uploaded by

Dilip TheLip
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Data-ow Analysis - Part 2

Y.N. Srikant
Department of Computer Science Indian Institute of Science Bangalore 560 012

NPTEL Course on Compiler Design

Y.N. Srikant

Data-ow Analysis

Data-ow analysis
These are techniques that derive information about the ow of data along program execution paths An execution path (or path) from point p1 to point pn is 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 pi is the end of some block and pi +1 is the beginning of a successor block

In general, there is an innite number of paths through a program and there is no bound on the length of a path Program analyses summarize all possible program states that can occur at a point in the program with a nite set of facts No analysis is necessarily a perfect representation of the state
Y.N. Srikant Data-ow Analysis

Uses of Data-ow Analysis

Program debugging
Which are the denitions (of variables) that may reach a program point? These are the reaching denitions

Program optimizations
Constant folding Copy propagation Common sub-expression elimination etc.

Y.N. Srikant

Data-ow Analysis

Data-Flow Analysis Schema


A data-ow value for a program point represents an abstraction of the set of all possible program states that can be observed for that point The set of all possible data-ow values is the domain for the application under consideration
Example: for the reaching denitions problem, the domain of data-ow values is the set of all subsets of of denitions in the program A particular data-ow value is a set of denitions

IN [s] and OUT [s]: data-ow values before and after each statement s The data-ow problem is to nd a solution to a set of constraints on IN [s] and OUT [s], for all statements s

Y.N. Srikant

Data-ow Analysis

Data-Flow Analysis Schema (2)


Two kinds of constraints
Those based on the semantics of statements (transfer functions) Those based on ow of control

A DFA schema consists of


A control-ow graph A direction of data-ow (forward or backward) A set of data-ow values A conuence operator (normally set union or intersection) Transfer functions for each block

We always compute safe estimates of data-ow values A decision or estimate is safe or conservative, if it never leads to a change in what the program computes (after the change) These safe values may be either subsets or supersets of actual values, based on the application
Y.N. Srikant Data-ow Analysis

The Reaching Denitions Problem


We kill a denition of a variable a, if between two points along the path, there is an assignment to a A denition 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 Unambiguous and ambiguous denitions of a variable a := b+c (unambiguous denition of a) ... *p := d (ambiguous denition of a, if p may point to variables other than a as well; hence does not kill the above denition of a) ... a := k-m (unambiguous denition of a; kills the above denition of a)
Y.N. Srikant Data-ow Analysis

The Reaching Denitions Problem(2)

Sets of denitions constitute the domain of data-ow values We compute supersets of denitions as safe values It is safe to assume that a denition reaches a point, even if it does not. In the following example, we assume that both a=2 and a=4 reach the point after the complete if-then-else statement, even though the statement a=4 is not reached by control ow if (a==b) a=2; else if (a==b) a=4;

Y.N. Srikant

Data-ow Analysis

The Reaching Denitions Problem (3)


The data-ow equations (constraints) IN [B ] =
P is a predecessor of B

OUT [P ] (IN [B ] KILL[B ])

OUT [B ] = GEN [B ]

IN [B ] = , for all B (initialization only ) If some denitions reach B1 (entry), then IN [B1 ] is initialized to that set Forward ow DFA problem (since OUT [B ] is expressed in terms of IN [B ]), conuence operator is GEN [B ] = set of all denitions inside B that are visible immediately after the block - downwards exposed denitions KILL[B ] = union of the denitions in all the basic blocks of the ow graph, that are killed by individual statements in B
Y.N. Srikant Data-ow Analysis

Reaching Denitions Analysis: An Example - Pass 1

Y.N. Srikant

Data-ow Analysis

Reaching Denitions Analysis: An Example - Pass 2

Y.N. Srikant

Data-ow Analysis

Reaching Denitions Analysis: An Example - Final

Y.N. Srikant

Data-ow Analysis

An Iterative Algorithm for Computing Reaching Denitions


for each block B do { IN [B ] = ; OUT [B ] = GEN [B ]; } change = true; while change do { change = false; for each block B do { IN [B ] =
P a predecessor of B

OUT [P ];

oldout

= OUT [B ]; (IN [B ] KILL[B ]);

OUT [B ] = GEN [B ]

if (OUT [B ] = oldout ) change = true; } } GEN , KILL, IN , and OUT are all represented as bit vectors with one bit for each denition in the ow graph
Y.N. Srikant Data-ow Analysis

Reaching Denitions: Bit Vector Representation

Y.N. Srikant

Data-ow Analysis

Use-Denition Chains (u-d chains)


Reaching denitions may be stored as u-d chains for convenience A u-d chain is a list of a use of a variable and all the denitions that reach that use u-d chains may be constructed once reaching denitions are computed case 1: If use u 1 of a variable b in block B is preceded by no unambiguous denition of b, then attach all denitions of b in IN [B ] to the u-d chain of that use u 1 of b case 2: If any unambiguous denition of b preceeds a use of b, then only that denition is on the u-d chain of that use of b case 3: If any ambiguous denitions of b precede a use of b, then each such denition for which no unambiguous denition of b lies between it and the use of b, are on the u-d chain for this use of b
Y.N. Srikant Data-ow Analysis

Use-Denition Chain Construction

Y.N. Srikant

Data-ow Analysis

Use-Denition Chain Example

Y.N. Srikant

Data-ow Analysis

Available Expression Computation


Sets of expressions constitute the domain of data-ow values Forward ow problem Conuence operator is An expression x + y is available at a point p, if every path (not necessarily cycle-free) from the initial 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 x + y , if it assigns (or may assign) to x or y and does not subsequently recompute x + y . A block generates x + y , if it denitely evaluates x + y , and does not subsequently redene x or y

Y.N. Srikant

Data-ow Analysis

Available Expression Computation(2)


Useful for global common sub-expression elimination 4 i is a CSE in B 3, if it is available at the entry point of B 3 i.e., if i is not assigned a new value in B 2 or 4 i is recomputed after i is assigned a new value in B 2 (as shown in the dotted box)

Y.N. Srikant

Data-ow Analysis

Available Expression Computation (3)


The data-ow equations IN [B ] =
P is a predecessor of B

OUT [P ], B not initial (IN [B ] e_kill [B ])

OUT [B ] = e_gen[B ] IN [B 1] =

IN [B ] = U , for all B = B 1 (initialization only ) B 1 is the intial or entry block and is special because nothing is available when the program begins execution IN [B 1] is always U is the universal set of all expressions Initializing IN [B ] to for all B = B 1, is restrictive

Y.N. Srikant

Data-ow Analysis

Computing e_gen and e_kill


For statements of the form x = a, step 1 below does not apply The set of all expressions appearing as the RHS of assignments in the ow graph is assumed to be available and is represented using a hash table and a bit vector

Y.N. Srikant

Data-ow Analysis

Available Expression Computation - An Example

Y.N. Srikant

Data-ow Analysis

Available Expression Computation - An Example (2)

Y.N. Srikant

Data-ow Analysis

An Iterative Algorithm for Computing Available Expressions


for each block B = B 1 do {OUT [B ] = U e_kill [B ]; } /* You could also do IN [B ] = U ;*/ /* In such a case, you must also interchange the order of */ /* IN [B ] and OUT [B ] equations below */ change = true; while change do { change = false; for each block B = B 1 do { IN [B ] =
P a predecessor of B

OUT [P ];

oldout

= OUT [B ]; (IN [B ] e_kill [B ]);

OUT [B ] = e_gen[B ] } }
Y.N. Srikant

if (OUT [B ] = oldout ) change = true;

Data-ow Analysis

Initializing IN[B] to for all B can be restrictive

Y.N. Srikant

Data-ow Analysis

Live Variable Analysis


The variable x is live at the point p, if the value of x at p could be used along some path in the ow graph, starting at p; otherwise, x is dead at p Sets of variables constitute the domain of data-ow values Backward ow problem, with conuence operator IN [B ] is the set of variables live at the beginning of B OUT [B ] is the set of variables live just after B DEF [B ] is the set of variables denitely assigned values in B , prior to any use of that variable in B USE [B ] is the set of variables whose values may be used in B prior to any denition of the variable OUT [B ] = IN [S ]
S is a successor of B

IN [B ] = USE [B ]

(OUT [B ] DEF [B ])

IN [B ] = , for all B (initialization only )


Y.N. Srikant Data-ow Analysis

Live Variable Analysis: An Example

Y.N. Srikant

Data-ow Analysis

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy