130 likes | 361 Views
Constant Propagation. Constant Propagation. The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set of possible data-flow values, even for a fixed control flow graph It is not distributive.
E N D
Constant Propagation • The constant propagation framework is different from all the data-flow problems discussed so far, in that • It has an unbounded set of possible data-flow values, even for a fixed control flow graph • It is not distributive
Data-Flow Values for the Constant Propagation Framework • The set of data-flow values is a product lattice, with one component for each variable in a program. The lattice for a variable consists of the following: • Allconstants appropriate for the type of the variable • The value NAC, which stands for not-a-constant • The value UNDEF, which stands for undefined
NAC and UNDEF • A variable is mapped to NAC if it is determined not to have a constant value. The variable may have been assigned an input value, or derived from a variable that is not a constant, or assigned different constants along different paths that lead to the same program point. • A variable is mapped UNDEF if nothing may yet to be asserted; presumably, no definition of the variable has been discovered to reach the point in question.
UNDEF … -3 -2 -1 0 1 2 3 … NAC Example: An Integer Variable For all value v, UNDEF v = v and NAC v = NAC. For any constant c, c c = c. For any two distinct constants c1 and c2, c1 c2 =NAC. A data-flow value for this framework is a map m from each variable to one of the values in its constant semilattice.
The Meet for the Constant Propagation Framework • The semilattice of data-flow values is the product of the semilattices for variables • Two maps m1 and m2, m1 m2, if and only if for all variables v, m1(v) m2(v) • Two maps m1 and m2, m1 m2 = m3, if and only if for all variables v, m3(v) = m1(v) m2(v)
Transfer Functions for the Constant Propagation Framework • The set F consists of certain transfer functions that accept a map of variables to values in the constant semilattice and return another such map • F contains the identity function I • F contains the constant transfer function for the ENTRY node that returns m0, where m0(v) = UNDEF, for all variables v
Transfer Functions for the Constant Propagation Framework • Let fs be the transfer function of statement s, and m2 = fs(m1) • 1. If s is not an assignment, then fs is I • 2. If s is an assignment to variable x = e, then m2(v) = m1(v), for all variables v x, and • (a) If e is a constant c, then m2(x) = c • (b) If e is of the form y + z, thenm1(y) + m1(z) if m1(y) and m1(z) constant m2(x) = NAC if either m1(y) or m1(z) is NAC UNDEF otherwise • (c) If e is any other expression, then m2(x) = NAC
Monotonicity of the Constant Propagation Framework • Case 1: I is monotone • Case 2(a): m2(x) = c, fs is monotone • Case 2(b): m1(y) m1(z) m2(x) UNDEF UNDEF UNDEF c2 UNDEF NAC NAC UNDEF UNDEFc1c2c1 + c2 NAC NAC UNDEF NAC NAC c2 NAC NAC NAC • Case 2(c): m2(x) = NAC, fs is monotone
Nondistributivity of the Constant Propagation Framework m m(x) m(y) m(z) m0 UNDEFUNDEF UNDEF f1(m0) 2 3 UNDEF f2(m0) 3 2 UNDEF f1(m0)f2(m0) NAC NAC UNDEF f3(f1(m0)f2(m0)) NAC NAC NAC f3(f1(m0)) 2 3 5 f3(f2(m0)) 3 2 5 f3(f1(m0))f3(f2(m0)) NAC NAC 5 ENTRY x = 2 y = 3 x = 3 y = 2 z = x + y EXIT f3(f1(m0) f2(m0)) < f3(f1(m0)) f3(f2(m0))
Interpretation of the Results • The value UNDEF is used for two purposes: • Initialize the ENTRY node: variables are undefined at the beginning of the execution • Initialize the interior program points before the iterations: for lack of information at the beginning of the iterative process, we approximate the solution with the top element
Interpretation of the Results • When UNDEF shows up at some program point, it means that no definitions have been observed for that variable along any of the paths leading up to that program point. • If all the definitions reaching a program point have the same constant value, the variable is considered a constant even though it may not be defined along some path.
B1 if Q goto B2 x = 10 B2 B3 B4 if Q’ goto B5 = x B5 B6 B7 Example