Prev Up Next
Go backward to Announcements
Go up to Top
Go forward to The Reaching Definitions Problem

Global Common Sub-expression Elimination

  1. To determine which expressions are available at each program point, we will associate a variable, AVAIL(p), with each program point. The value of AVAIL(p) can be any subset of the distinct expressions found in the procedure being processed. Our goal is to specify the equations relating the values of the AVAIL(p) variables in such a way that a solution to the equations will assign to each AVAIL(p) variable a conservative approximation to the set of expressions actually available at that program point.
  2. We can do this by providing rules used to generate equations relating the values of avail(p) at different program points based on how these program points relate to the statement structure of the program.
    assignment statements
    Given an assignment of the form
    x := exp
    If p1 is the program point just before the assignment and p2 is the point just after the assignment it is clear that
    AVAIL(p2) = ( AVAIL(p1) + { sub-expression of exp } - KILL(x))
    In the remaining cases, I will indicate where the program points I wish to talk about are by putting their names in angle brackets at the appropriate points. Thus, the assignment would become:
    < p1 > x := exp < p2 >
    if statement
    Given an if statement of the form:
    < p0 > if exp then < p1 > stmt1 < p3 >
    else < p2 > stmt2 < p4 >
    end < p5 >
    AVAIL( p5 ) = AVAIL( p3 ) &AVAIL( p4 )
    AVAIL( p1 ) = AVAIL( p2 ) = AVAIL( p0 ) + {  expressions appearing in exp }
    while loop
    Given a while loop of the form:
    < p0 > while < p1 > exp do
    < p2 > stmt < p3 >
    end < p4 >
    AVAIL( p1 ) = ( AVAIL( p0 ) &AVAIL( p3 ))
    AVAIL( p2 ) = AVAIL( p4 ) = AVAIL( p1 ) + {  expressions appearing in exp }
  3. We can solve the equations for AVAIL (and for many other similar problems that arise in global optimization) by an iterative technique.
    1. Start by setting all the AVAIL(p) sets to the empty set.
    2. execute all the "equations" as assignment statements.
    3. If any of the AVAIL sets changed when all the equations were executed, do it again.
  4. I'd like to quickly show an example of how these techniques can be applied to a simple sample program. To make the example work, I have annotated the program with program point names below:
    {} x := y*z;
    <p0> m := z/n;
    <p1> while <p2> y*z > 0 do
    <p3> if z/n > l then
    <p4> z := y*z <p6>
    else
    <p5> z := y*z - 1 <p7>
    end;
    <p8> m := z/n <p9>
    end <p10>
  5. There are only two expressions that appear more than once in this example, y*z and z/n. So, we need only consider these expressions (it would make sense to ignore expressions that only appear once in a real compiler too).
  6. The KILL sets associated with the variables that may be changed by assignments in the fragment are KILL(x) = {} , KILL(z) = {y*z,z/n} and KILL(m) = {} .
  7. The equations generated are then:
    AVAIL(p0) = {} + {y*z} - {}
    AVAIL(p1) = AVAIL(p0) + {z/n} - {}
    AVAIL(p2) = AVAIL(p1) &AVAIL(p9)
    AVAIL(p3) = AVAIL(p10) = AVAIL(p2) + {y*z}
    AVAIL(p4) = AVAIL(p5) = AVAIL(p3) + {z/n}
    AVAIL(p6) = AVAIL(p4) + {y*z} - {y*z,z/n}
    AVAIL(p7) = AVAIL(p5) + {y*z} - {y*z,z/n}
    AVAIL(p8) = AVAIL(p6) &AVAIL(p7)
    AVAIL(p9) = AVAIL(p8) + {z/n} - {}
  8. Repeatedly applying these equations as assignments we obtain:

    p0 p1 p2 p3, p10 p4 ,p5 p6 ,p7 ,p8 p9
    {} {} {} {} {} {} {}
    { y*z } { y*z, z/n } {} { y*z } { y*z } {} {z/n}
    { y*z } { y*z, z/n } {z/n} { y*z, z/n } { y*z, z/n } {} {z/n}
    { y*z } { y*z, z/n } {z/n} { y*z, z/n } { y*z, z/n } {} {z/n}
    (to keep things readable, I have merged variables which clearly must have equal values)

  9. From these results, we can see that the evaluation of z/n in the boolean of the if statement and the instances of y*z in the branches of the if statement are redundant.

Computer Science 434
Department of Computer Science
Williams College

Prev Up Next