03 Data Flow Analysis Handout
03 Data Flow Analysis Handout
◮ Assignments
x = y op z
x = op y
◮ Intraprocedural: Restricted to a single function x=y
◮ Jump/control transfer
◮ Input in 3-address format
goto L
◮ Unless otherwise specified if x relop y goto L
◮ Statements can have label(s)
L: . . .
◮ Arrays, Pointers and Functions to be added later when
needed
Data Flow Analysis Data Flow Abstraction
Data Flow Abstraction: Control Flow Graph Data Flow Abstraction: Program Points
◮ Transfer functions
◮ relationship between the data flow values before and after a
stmt
◮ forward functions: Compute facts after a statement s from ◮ Relationship between the data flow values of two points
the facts available before s. that are related by program execution semantics
◮ General form:
◮ For a basic block having n statements:
OUT[s] = fs (IN[s])
IN[si+1 ] = OUT[si ], i = 1, 2, . . . , n − 1
◮ backward functions: Compute facts before a statement s
from the facts available after s. ◮ IN[s1 ], OUT[sn ] to come later
◮ General form:
IN[s] = fs (OUT[s])
◮ SUCC (B): Set of successor BBs of block B in CFG ◮ Control flow constraints
M
◮ f ◦ g : Composition of functions f and g IN[B] = OUT[P]
L
◮ : An abstract operator denoting some way of combining P∈PRED(B)
◮ Typical Equation
IN(s1 )
d :x =y +z s1
◮ A definition d reaches a point p if
◮ there is a path from the point immediately following d to p OUT(s1 )
◮ d is not “killed” along that path
◮ “Kill” means redefinition of the left hand side (x in the earlier
example) OUT(s1 ) = IN(s1 ) − KILL(s1 ) ∪ GEN(s1 )
GEN(s1 ) = {d}
KILL(s1 ) = Dx − {d}, where Dx : set of all definitions of x
KILL(s1 ) = Dx ? will also work here
but may not work in general
RD Analysis of a Structured Program RD Analysis of a Structured Program
IN(S)
IN(S)
S
s1 S
s1 s2
s2
OUT(S)
OUT(S)
IN(S)
IN(S) S
S
s1 s2
s1
OUT(S)
OUT(S)
◮ Assumption: All paths are feasible.
◮ Example:
GEN(S) = GEN(s1 )
KILL(S) = KILL(s1 ) if (true) s1;
else s2;
OUT(S) = OUT(s1 )
Fact Computed Actual
IN(s1 ) = IN(S) ∪ GEN(s1 )
GEN(S) = GEN(s1 ) ∪ GEN(s2 ) ⊇ GEN(s1 )
KILL(S) = KILL(s1 ) ∩ KILL(s2 ) ⊆ KILL(s1 )
RD Analysis is Approximate RD at BB level
IN(S)
S ◮ A definition d can reach the start of a block from any of its
predecessor
s1 s2 ◮ if it reaches the end of some predecessor
[
IN(B) = OUT(P)
OUT(S) P∈PRED(B)
Solving RD Constraints
◮ Set-theoretic definitions:
[
a bit for each definition: IN(B) = OUT(P)
d1 d2 d3 d4 d5 d6 d7 P∈PRED(B)
Pass# Pt B1 B2 B3 B4
Init IN - - - - OUT(B) = IN(B) − KILL(B) ∪ GEN(B)
OUT 0000000 0000000 0000000 0000000
1 IN 0000000 1110000 0011100 0011110 ◮ Bitvector definitions:
OUT 1110000 0011100 0001110 0010111
_
2 IN 0000000 1110111 0011110 0011110
OUT 1110000 0011110 0001110 0010111
IN(B) = OUT(P)
3 IN 0000000 1110111 0011110 0011110 P∈PRED(B)
OUT 1110000 0011110 0001110 0010111
OUT(B) = IN(B) ∧ ¬KILL(B) ∨ GEN(B)
◮ Bitwise ∨, ∧, ¬ operators
Reaching Definitions: Application Reaching Definitions: Application
Constant Folding
◮ Recall the approximation in reaching definition analysis
while changes occur { true GEN(S) ⊆ analysis GEN(S)
forall the stmts S of the program { true KILL(S) ⊇ analysis KILL(S)
foreach operand B of S { ◮ Can it cause the application to infer
if there is a unique definition of B ◮ an expression as a constant when it is has different values
that reaches S and is a constant C { for different executions?
replace B by C in S; ◮ an expression as not a constant when it is a constant for all
if all operands of S are constant { executions?
replace rhs by eval(rhs); ◮ Safety? Profitability?
mark definition as constant;
}}}}}
dx in B defines variable x and is not ◮ Entry block has to be initialized specially:
◮ GEN(B) = dx
followed by another definition of x in B
◮ KILL(B) = {dx | B contains some definition of x } OUT(Entry ) = EntryInfo
S EntryInfo = ∅
◮ IN(B) = P∈PRED(B) OUT(P)
◮ OUT(B) = IN(B) − KILL(B) ∪ GEN(B) ◮ A better entry info could be:
V
◮ meet ( ) operator: The operator to combine information
coming along different predecessors is ∪ EntryInfo = {x = undefined | x is a variable}
◮ What about the Entry block? ◮ Why?