+ All Categories
Home > Documents > Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing...

Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing...

Date post: 13-Dec-2015
Category:
Upload: amberlynn-murphy
View: 219 times
Download: 1 times
Share this document with a friend
Popular Tags:
52
Data-Flow Analysis (Chapter 8)
Transcript
Page 1: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Data-Flow Analysis(Chapter 8)

Page 2: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Outline• What is Data-Flow Analysis?• Structure of an optimizing compiler• An example: Reaching Definitions• Basic Concepts: Lattices, Flow-

Functions, and Fixed Points• Taxonomy of Data-Flow Problems and

Solutions• Iterative Data-Flow Analysis• Structural Data-Flow Analysis• DU-Chains and SSA

Page 3: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Data-Flow Analysis

• Input: A control flow graph• Output: A control flow graph with

“global” information at every basic blockExamples– Constant expressions: x+y*z– Live variables

Page 4: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Difficulties in Data-Flow Analysis

• Input-dependent information

• Undecidability of program analysis– Reachability of basic blocks– Arithmetic– ...

Page 5: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Conservative data-flow analysis

• Every piece of data-flow information is sound

• Every enabled optimization is correct

• A superset of the execution sequences is considered

• In the reaching definition example a superset of the reaching definitions is computed

Page 6: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Iterative Computation of Reaching Definitions

• Optimistically assume that at every block no definition is reached

• Every basic block “generates” new definitions and “preserves” other definitions

• No definition reaches ENTRY• Accumulate reaching definitions along different

paths• Iteratively compute more and more definitions at

every basic block• The process must terminate• The final solution is unique and conservative

Page 7: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:
Page 8: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Generation Kill Preservation PreservationVector-8 bit

Generation Vector – 8 bit

B1{1,2,3} {6,7} {4,5,8} <00011001> <11100000>

B3{4} {8} {1,2,3,5,6,7} <11101110> <00010000>

B6{5,6,7,8} {2,3,4} {1} <10000000> <00001111>

Page 9: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

We define sets and corresponding bit vectors RCHout (i) that represent the definitions that reach the end of basic block i. as for RCHin(i), it is sufficient to initialize RCHout(i) by

RCHout(i) = 0 or RCHout(i) = <00000000> for all i.

RCHout(i) = GEN(i) U (RCHin(i) ∩ PRSV(i))

Page 10: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

To solve the system of bit vecto equations for RCHint(i) and RCHout(i), we simply initialize the RCHin(i) to the values given above and iterate application of the equations until no further changes result.

Page 11: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

After iterating one more time, we have

And iterating one more time produces no more changes so the above values are the solution.The rules for performing the iteration never change a 1 to a 0, they are monotone, so we guaranteed that the iteration process does terminate.

Page 12: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

• The solution to the data flow equations gives us a global view of which definitions of varibales may reach which uses.

• Ex. It shows that the definition of f0 in basic block B1 may reach the first use of f0 in block B6 and that along the execution path through basic block B2, the variables i and f2 are never defined.

• We might use this information to optimize the program is to avoid allocation of storage for i and f2 along the path through B2.

Page 13: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Basic concepts: Lattices, Flow Functions and Fixed Points

• A data flow analysis is performed by operating on elements of an algebraic structure called lattice.

• It represent abstract properties of variables, expression, or other programming constructs for all possible executions of a procedure-independent of the values of the input data and usually independent of the control flow paths through procedure.

Page 14: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

• We associate with each of the possible control flow and computational constructs in a procedure a so-called flow function that abstracts the effect of the constructor to its effect on the corresponding lattice elements.

• In general, a Lattice L consists of a set of values and two operations called meet, denoted , and join denoted , that satisfy ⊓ ⊔several properties, as follows:

Page 15: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

• 1. for all x, y є L, there exist unique z and w є L such that x y = z and x y = w (closure)⊓ ⊔

• 2. for all x, y є L, x y = y x and x y = y ⊓ ⊓ ⊔ x. (commutatively).⊔

• 3. for x, y, z є L, (x y) z = x (y z) ⊓ ⊓ ⊓ ⊓and (x y) z = x (y z) (associativity).⊔ ⊔ ⊔ ⊔

• 4. There are two unique elements of L called bottom, denoted and top, denoted ⊥ ⊤ , such that for all x є L, x ⊓ ⊥ = ⊥ and x ⊔ ⊤ = ⊤ (existence of unique top and bottom elements).

Page 16: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

• For x, y, z є L,

• (x y) z = (x z) (y z) and (x ⊓ ⊔ ⊔ ⊓ ⊔ ⊔y) and (x y) z = (x z) (y z) ⊔ ⊓ ⊓ ⊔ ⊓ (Distributive)

•Most of the lattices we use have bit vectors as their elements and meet and join are bitwise AND and OR, respectively.

•The bottom element of such a lattice is the bit vector of all zeroes and top is the vector of all ones.

•We use BVn to denote the lattice of bit vectors of length n.

Page 17: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

• Ex. The 8-bit vector form a join lattice with <00101111> ⊔ <01100001> = <01101111>.

• The join of two bit vector is the bit vector that has a one wherever either of them has a one and a zero

• There are several ways to construct lattices by combining simple ones.– Product Operation : the product of two Lattices

L1 and L2 with meet operators ⊓1 and ⊓2, respectively, which is written L1 X L2 , is {<x1,x2> | X1 є L1, x2 є L2} with meet operation as follows:

– <x1, x2> <y⊓ 1, y2> = <x1 ⊓1 y1, x2 ⊓2 y2>

Page 18: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Dimensions for Data-Flow Problems

• The information provided

• “ralational” Vs. independent attributes

• The type of lattice and functions used

• The direction of information flowforward, backward, bidirectional

Page 19: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Example Data-Flow Problems

• Reaching Definitions

• Available Expressions

• Live Variables

• Upward Exposed Uses

• Copy-Propagation Analysis

• Constant-Propagation Analysis

• Partial-Redundency Analysis

Page 20: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Reaching Definitions

• This determines which definition of a variable may reach each use of the variable in a procedure.

• It is a forward problem that uses a lattice of bit vectors with one bit corresponding to each definition of a variable

Page 21: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Available Expressions

• Determines which expression are available at each point in a procedure.

• It is a forward problem that uses a lattice of bit vectors in which a bit is assigned to each definition of an expression.

Page 22: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Live Variables

• Determines for a given variable and a given point in a program where there is a use of the variable along some path from the point to the exit.

• This is backward problem that uses bit vector in which each used of a variables is assigned a bit position.

Page 23: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Upward Exposed Uses• Determines what uses of

variables at particular points are reached by particular definitions.

• Definition of x in B2 reaches the uses in B4 and B5, while the use in B5 is reached by the definitions in B2 and B3

Page 24: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Copy-Propagation Analysis

• Determines that on every path from a copy assignment, say x y, to a use of variable x there are no assignment to y.

• This is a forward problem that uses bit vectors in which each bit position represents a copy assignment.

Page 25: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Constant-Propagation Analysis

• Determines that on every path from an assignment of a constant to a variable, say, x const, to a use of x the only assignments to x assign it the value const.

Page 26: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Partial-Redundency Analysis

• Determines what computations are performed twice (or more times) on some execution path without the operands being modified between the computation.

Page 27: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

• The flow analysis problems listed above are not the only ones encountered in optimizations, but they are among the most important.

• There are many approaches to solving data – flow problems, including the following:

Page 28: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Data-Flow Analysis Algorithms

• Allen’s strongly connected regions• Kildall’s iterative algorithm• Ullman’s T1-T2 analysis• Kennedy’s node-listing algorithm• Farrow, Kennedy, and Zuconi’s graph

grammar approach• Rosen’s high-level approach• structural analysis• slotwise analysis

Page 29: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

We will concentrate on three approaches:1.The simple iterative approach with several strategies for determining the order of the iterations.2.An elimination or control-tree-based method using intervals3.Another control-tree based method using structural analysis.

Page 30: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Iterative analysis:

•It is the method which we have used in the example of Fibonacci series.•It is easy to implement and as a result, it is frequently used.•Due to Iterative method of forward analysis, we can easily generalize methods for backward and bidirectional analysis.

Page 31: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Control-tree-based data flow analysis:

•The algorithms for control-tree-based data-flow analysis namely, interval analysis and structural analysis are very similar to the control tree based structural analysis studied in chapter 7

•Slightly harder to the iterative analysis

Page 32: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Structural Data-Flow Analysis

• Phase 1: Compute “the effect” of every program construct in a bottom-up fashion on the tree of control flow constructs(control-tree)

• Phase 2: Propagates the data-flow value in a top-down fashion into basic blocks

• (This part is covered in the chapeter 7 while discussing the loop structures)

Page 33: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:
Page 34: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Handling Arbitrary CFGs

• Need to handle arbitrary acyclic regions

• Need to to handle irreducible components (improper regions)

Page 35: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Handling Arbitrary CFGs

• Need to handle arbitrary acyclic regions

• Need to handle arbitrary cyclic regions– Reducible regions– irreducible components (improper regions)

Page 36: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Handling Improper Regions

• Ignore

• Node splitting

• Solve iteratively for every initial value

• Solve iteratively over LF

Page 37: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Structural Backward Analysis

• Tricky

• For constructs with single exit “reverse” equation direction

• For acyclic constructs with multiple exits use join

• For cyclic reducible constructs with multiple exits--- break the cycle and use join

• Cyclic improper regions are handled like the forward case

Page 38: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Bottom-Up PhaseBackward Problems

(if-then)

if

then

Fif/Y

Fif/N

Fthen

if-then

Fif-then

Fif-then=(F if/Y° Fthen) Fif/N

Page 39: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Top-Down PhaseBackward Problems

(if-then)

if

then

Fif/Y

Fif/N

Fthen

if-then

Fif-then

out(then)= out(if-then)

out(if) = Fthen(out(then)) out(if-then)

Page 40: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Implementation

• Represent the computation of canonic cases with functions (if-then-else, while)

• Use graphs to represent arbitrary functional computations

Page 41: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Automatic Construction ofData-Flow Analyzers

• Not commonly used so far

• Kildall developed a tool for iterative data flow analysis (1973)

• The PAG (1995) system allows systematic construction of iterative data-flow analysis

• The Sharlit (1992) system generates non-iterative data-flow analyzers– Finds regular “path-expressions” in CFG– Convert into effect functions

Page 42: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Def-Use, Use-Def Chains

• Sparse data-flow information on flow of variables between assignments

• Can be used to improve the efficiency of iterative data-flow analysis

• A du-chain for a variable v connects a definition of v to all the uses of this definition

• A ud-chain for a variable v connects a use of v to all the definitions that may flow to it

• A web for a variable v is the maximal union of interesting du-chains for v

Page 43: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

entry

z>1

x1

z>2

x2

yx+1

zx-3

x4

zx+7

exit

Y

NY

N

Page 44: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Static Single Assignment(SSA)

• A normal form of the program such that def-use is immediate

• A separate variable for every assignment

• A function combines the values of relevant variables

• Simplifies some optimizations

• Increases program’s size

Page 45: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

entry

z1>1

x11

z1>2

x22

y1x1+1

x3 (x1, x2) ; z2x3-3

x44

z3x4+7

exit

Y

NY

N

Page 46: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Flow graph to be translated to minimal SSA formThe iterative characterization of dominance frontiers given , computer for

variable k:DF1 ({entry, B1, B3}) = {B2}DF2 ({entry, B1, B3}) = DF({entry, B1, B2, B3}) = B2

Page 47: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

For variable i:DF1 ({entry, B1, B3, B6}) = {B2, exit}DF2 ({entry, B1, B3, B6}) = DF({entry, B1, B2, B3, B6, exit})

For variable j:DF1({entry, B1, B3}) = {B2}DF2({entry, B1, B3}) = DF ({entry, B1, B2, B3}) = {B2}

Page 48: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:
Page 49: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

B2 requires a function for each of i, j and k and exit needs one for i

Page 50: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

Handling Pointers and Arrays

• Complicated!!!

• Treated conservatively in most compilers

• The frontier of research

• A simple “reduction”

• Direct solutions yield more precise solutions

xa[i]

a[i]4

xaccess(a, i)

aupdate(a, i, 4)

Page 51: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

More Ambitious Data-Flow Analysis

• Data-Flow analysis can yield “interesting” information on program behavior

• Signs of variables

• Non-trivial constant values

• Termination properties

• Complicated bugs

• Partial correctness

Page 52: Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:

int f(int x){if (x > 100) return x -10;

else return f(f(x+11));}void main(){scanf(“%d”, &x);if (x > 100) printf(“%d\n”, 91);

else printf(“%d\n”, f(x));}


Recommended