Code Optimization IV
Code Optimization IV
[email protected] 08/07/2023 43
Transfer Functions Constraints
● The data- flow values before and after a statement are constrained by the
semantics of the statement.
● Suppose our statement of interest is y = x , and the value of x before this
statement is 2.
● So, after we execute the statement y=x, both the values of x, and y will be 2
● This relationship between the data- flow values before and after the
assignment statement is known as a transfer function.
[email protected] 08/07/2023 44
Transfer Functions Constraints
● Through transfer functions information can propagate either to the forward
along with execution path or backwards up the execution path
● In a forward- flow problem, the transfer function fs of a statement s, takes the
data- flow value before the statement and produces a new data- flow value
after the statement as follows:
○ OUT[s] = fs(IN[s])
● In a backward- flow problem, the transfer function fs for statement s converts
a data- flow value after the statement to a new data- flow value before the
statement as follows:
○ IN[s] = fs(OUT[s])
[email protected] 08/07/2023 45
Control-Flow Constraints
● Control Flow Constraints on data- flow values is derived from the flow of
control.
● Within a basic block, control flow is simple.
● If a block B consists of statements s1, s2, …., sn in that order, then the control-
flow value out of si is the same as the control- flow value into si+1 .
○ IN[si+1 ] = OUT[si ], for all i = 1, 2, ….,n-1
● Control- flow edges between basic blocks create more complex constraints
[email protected] 08/07/2023 46
Data-Flow Schemas on Basic Blocks
● Can restate the schema in terms of data- flow values entering and leaving the
blocks.
● Denote the data- flow values immediately before and immediately after each
basic block B by IN[B] and OUT[B], respectively.
● Suppose block B consists of statements s1,....., sn, in that order.
○ If s1 is the first statement of basic block B, then IN[B] = IN[s1],
○ If sn is the last statement of basic block B, then OUT[B] = OUT[sn]
● The transfer function fB of a basic block B , can be derived by composing the
transfer functions of the statements in the block.
● If fs be the transfer function of statement si then
○ fB = fsn ∘…..∘ fs2 ∘ fs1
○ OUT[B] = fB (IN[B])
[email protected] 08/07/2023 47
Data-Flow Schemas on Basic Blocks
● Constraints due to control flow between basic blocks can easily be rewritten
by substituting IN[B] and OUT[B] for IN[s1] and OUT[sn], respectively
● If data- flow values are information about the sets of constants that may be
assigned to a variable then the forward - flow problem is
○ IN[B] = ⋃P a predecessor of B OUT[P]
● In backwards-flow (details in live-variable analysis), the equations are similar,
but with the roles of the IN's and OUT's reversed.
○ IN[B] = fB (OUT[B])
○ OUT[B] = ⋃S a successor of BIN[S]
[email protected] 08/07/2023 48
Reaching Definitions
● One of the most common and useful data- flow schemas.
● When control in a program reaches a point p, knowing the location where
each variable x was defined provides many information about x
○ For instance, compiler then knows if x is a constant or not at point p, or
○ A debugger can tell whether it is possible for x to be an undefined
variable
● A definition d reaches a point p if there is a path from the point immediately
following d to p - and d is not killed along the path
● A definition of a variable x is killed if there is any other definition of x anywhere
along the path.
● If a definition d of of some variable x reaches a point p, the x might be defined
at location d
[email protected] 08/07/2023 49
Reaching Definitions: Example
[email protected] 08/07/2023 51
Reaching Definitions: Drawbacks
● Example:
○ if (a == b) statement 1; else if (a == b) statement 2;
● For no values of a and b can the flow of control actually reach statement 2
● whether each path in a flow graph can be taken is an undecidable problem.
● it is conservative to assume that a definition can reach a point even if it
might not.
● Thus, we may allow paths that are never be traversed in any execution of
the program
● may allow definitions to pass through ambiguous definitions of the same
variable safely.
[email protected] 08/07/2023 52