Download presentation
Presentation is loading. Please wait.
Published byBruno Powell Modified over 9 years ago
1
Constant Propagation
2
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
3
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: All constants appropriate for the type of the variable The value NAC, which stands for not-a- constant The value UNDEF, which stands for undefined
4
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.
5
Example: An Integer Variable UNDEF … -3 -2 -1 0 1 2 3 … NAC For all value v, UNDEF v = v and NAC v = NAC. For any constant c, c c = c. For any two distinct constants c 1 and c 2, c 1 c 2 = NAC. A data-flow value for this framework is a map m from each variable to one of the values in its constant semilattice.
6
The Meet for the Constant Propagation Framework The semilattice of data-flow values is the product of the semilattices for variables Two maps m 1 and m 2, m 1 m 2, if and only if for all variables v, m 1 ( v ) m 2 ( v ) Two maps m 1 and m 2, m 1 m 2 = m 3, if and only if for all variables v, m 3 ( v ) = m 1 ( v ) m 2 ( v )
7
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 m 0, where m 0 ( v ) = UNDEF, for all variables v
8
Transfer Functions for the Constant Propagation Framework Let f s be the transfer function of statement s, and m 2 = f s ( m 1 ) 1. If s is not an assignment, then f s is I 2. If s is an assignment to variable x = e, then m 2 ( v ) = m 1 ( v ), for all variables v x, and (a) If e is a constant c, then m 2 ( x ) = c (b) If e is of the form y + z, then m 1 ( y ) + m 1 ( z ) if m 1 ( y ) and m 1 ( z ) constant m 2 ( x ) = NAC if either m 1 ( y ) or m 1 ( z ) is NAC UNDEF otherwise (c) If e is any other expression, then m 2 ( x ) = NAC
9
Monotonicity of the Constant Propagation Framework Case 1: I is monotone Case 2(a): m 2 ( x ) = c, f s is monotone Case 2(b): m 1 ( y ) m 1 ( z ) m 2 ( x ) UNDEFUNDEF UNDEF c 2 UNDEF NAC NAC UNDEFUNDEF c 1 c 2 c 1 + c 2 NAC NAC UNDEF NAC NAC c 2 NAC NAC NAC Case 2(c): m 2 ( x ) = NAC, f s is monotone
10
Nondistributivity of the Constant Propagation Framework x = 2 y = 3 x = 3 y = 2 z = x + y EXIT m m ( x ) m ( y ) m ( z ) m 0 UNDEF UNDEF UNDEF f 1 ( m 0 ) 2 3 UNDEF f 2 ( m 0 ) 3 2 UNDEF f 1 ( m 0 ) f 2 ( m 0 ) NAC NAC UNDEF f 3 ( f 1 ( m 0 ) f 2 ( m 0 )) NAC NAC NAC f 3 ( f 1 ( m 0 )) 2 3 5 f 3 ( f 2 ( m 0 )) 3 2 5 f 3 ( f 1 ( m 0 )) f 3 ( f 2 ( m 0 )) NAC NAC 5 f 3 ( f 1 ( m 0 ) f 2 ( m 0 )) < f 3 ( f 1 ( m 0 )) f 3 ( f 2 ( m 0 )) ENTRY
11
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
12
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.
13
Example if Q goto B 2 if Q’ goto B 5 x = 10 = x B2B2 B3B3 B1B1 B4B4 B5B5 B6B6 B7B7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.