










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Thomas VanDrunen and Antony L. Hosking
Purdue University
Abstract. Partial redundancy elimination (PRE) is a program trans- formation that identifies and eliminates expressions that are redundant on at least one (but not necessarily all) execution paths. Global value numbering (GVN) is a program analysis and transformation that iden- tifies operations that compute the same value and eliminates operations that are redundant. A weakness of PRE is that it traditionally consid- ers only expressions that are lexically equivalent. A weakness of GVN is that it traditionally considers only operations that are fully redun- dant. In this paper, we examine the work that has been done on PRE and GVN and present a hybrid algorithm that combines the strengths of each. The contributions of this work are a framework for thinking about expressions and values without source-level lexical constraints, a system of data-flow equations for determining insertion points, and a practical algorithm for extending a simple hash-based GVN for PRE. Our imple- mentation subsumes GVN statically and, on most benchmarks, in terms of performance.
The goal of a compiler’s optimization phases is transforming the program to make it more efficient. Accordingly, researchers have given much attention to methods for eliminating redundant computations; both approaches and termi- nology to describe them have proliferated. In the progeny of this research, two primary genera can be identified (some cross-breeding not withstanding): Partial Redundancy Elimination (PRE) and Global Value Numbering (GVN). The goal of the present work is to present an algorithm for a hybrid subsuming these two approaches synthesized from known techniques and tools.
1.1 Motivation
PRE considers the control flow of the program and identifies operations that are redundant on some but not necessarily all traces of the program that include that operation, and hence are partially redundant. It hoists operations to earlier program points where originally the operation was unknown and removes opera- tions that the hoisting has rendered fully redundant. In Figure 1(a), e ← c + b is partially redundant because of d ← c + b. By hoisting and preserving the result of the operation in temporary variable t, PRE produces the program in Figure 1(b). Because of this hoisting, PRE is sometimes called (an instance of) code
E. Duesterwald (Ed.): CC 2004, LNCS 2985, pp. 167–184, 2004. ©c Springer-Verlag Berlin Heidelberg 2004
168 T. VanDrunen and A.L. Hosking
motion. GVN on the other hand considers the value produced by an operation (in this paper, we always assume value to mean static value; operations in loop bodies or otherwise reentrant code may result in different dynamic values at different points in program execution). If a value has already been computed, subsequent operations can be eliminated even though they may differ lexically. Because of the move c ← a in Figure 1(a), the operations c ← b+a and d ← c+b compute the same value. Accordingly GVN preserves this value in a temporary and eliminates the re-computation, as in Figure 1(c). As this example shows, in their basic form, neither PRE nor GVN is strictly more powerful than the other (see Muchnick p. 343 [25]).
c ← a d ← c + b
e ← c + b
c ← 5
c ← b + a c ← a d ← c + b t ← d
c ← b + a
e ← t
c ← 5 t ← c + b
c ← a d ← t
c ← b + a t ← c
c ← 5
e ← c + b (a) (b) (c)
Fig. 1. Basic example
1.2 Our Result
Answering this challenge, we have found a technique, GVN-PRE, that subsumes both and eliminates redundancies that would be undetected by either working in isolation. We create a system of data flow equations that, using the infrastruc- ture of a simple hash-based value numbering system, calculate insertion points for partially redundant values rather than partially redundant expressions. We present this in a framework that allows clear reasoning about expressions and values and implement it with an algorithm that can easily be reproduced. Af- ter exploring the background to this problem and related work, we explain the framework, give the data flow equations and related algorithm, and explore an implementation and its performance. Our implementation subsumes GVN.
1.3 Related Work
PRE. Partial Redundancy Elimination in general poses several challenges. First, lexically identical expressions at different program points do not neces- sarily compute the same result, since operands may be reassigned, so any PRE algorithm must take into account assignments that kill an expression’s availabil- ity. Furthermore, care must be taken that the hoisting would do no harm: if an
170 T. VanDrunen and A.L. Hosking
c 1 ← a 1 + b 1 c 2 ← a 2 + b 1
a 3 ← φ(a 1 , a 2 ) c 3 ← a 3 + b 1 c^3 ,^ +
a 3 b 1 a 1 a 2
a 3
(a) program (b) value classes as DAG (c) value flow graph
Fig. 2. Example showing weakness of traditional GVN and the use of a VFG
same class, and therefore so are expressions on those versions in the same class, statically and globally. Congruences can be found by hashing [9,6] or by first assuming all expressions to be equal and splitting classes when incongruences are found [3,16,30]. Since identifying an operation as redundant depends on the operation having the same global number as an earlier operation, in traditional GVN such an earlier operation must dominate (i.e., occur on all paths to) the later operation. However, if any given path is considered in isolation, some value classes can be merged. In Figure 2(a), φ(a 1 , a 2 ) merges the variables a 1 and a 2 , so if we consider the left path, a 3 should be in the same class as a 1 , whereas on the right path, a 2 should be in the same class as a 1. Yet the operation a 3 + b 1 will not be eliminated because nothing in its global class has been computed earlier. Since traditional GVN does no motion, it does not eliminate some op- erations that on any given trace of the program are fully redundant—let alone operations that are partially redundant. (Another tradition of global value num- bering has been worked on by Steffen et al [31,32,33,29,22]. Its various versions have tended to be more general and theoretically complete but less amenable to implementation than that of Rosen, Alpern, et al.)
Hybrids. One of the original GVN papers [28] is remarkable because it not only introduces GVN, SSA, and critical edge removal; it also does PRE on values. However, the range of congruences it can identify is small, and subsequent research seems to have ignored this as it wallowed in its lexical mire. The first explicit study of the potential for PRE to work on values (or for GVN to consider partial redundancies) is that of Knoop, R¨uthing, and Steffen’s comparison of code motion and code placement [21]. They distinguish syntactic (lexical) code motion from semantic, which considers values. They note that the collection of value classes can be modeled by a dag, since a value is represented by an expression (a node) which has other values as operands (edges pointing to nodes). The path-based connections among values is captured by what they call the value flow graph (VFG). See Figure 2(b) and (c). Knoop et al. use these constructs to sketch an algorithm for determining safe insertion points for code motion (PRE), but distinguish it from a more powerful optimization which they call code placement and for which determining insertion points traditionally considered safe is not adequate for finding all insertion points necessary for optimality. Bod´ık and Anik, independently of Knoop et al., developed an algorithm that also addressed PRE on values [4]. Their work uses value numbering with back
Value-Based Partial Redundancy Elimination 171
substitution to construct a value name graph (VNG) similar to Knoop et al’s VFG. The VNG considers expressions to be names of values. If the program is not in SSA, expressions represent different values at different program points, and the nodes of the graph are expression / program-point pairs. The edges capture the flow of values from one name to another according to program control flow. Bod´ık et al. then use data flow analysis to identify optimal insertion points for PRE, an algorithm they call VNGPRE.
1.4 Overview
In this paper, we present a new algorithm for PRE on values which subsumes both traditional PRE and GVN. The contributions of our approach are as follows. First, our analysis considers larger program chunks than Bod´ık (ba- sic blocks instead of single instructions). Second, we present a framework for expressions and values that takes full advantage of SSA, completely ignoring source-level lexical constraints and thinking solely in terms of values and expres- sions that represent them. Third, no graph is explicitly constructed nor is any novel structure introduced. Instead, we achieve the same results by synthesizing well-known tools (SSA, control flow graphs, value numbers) and techniques (flow equations, liveness analysis, fixed-point iteration). In fact, our algorithm can be viewed as an extension of a simple hash-based GVN. Finally, we report on an efficient and easily-reproduced implementation for this algorithm. Our intention here is not to make a head-on comparison with the approaches of Bod´ık et al. or Knoop et al., but rather to present an approach that covers both traditional PRE and GVN that is easily realized in implementation. The rest of the paper is organized as follows: Section 2 gives preliminaries, such as assumptions we make about the input program, precise definitions of concepts we use for our analysis, and descriptions of global value numbering infrastructure we assume already available. Section 3 contains the meat of the paper, describing the various phases of the approach both formally and algo- rithmically. Section 4 gives details on our implementation and its results. We conclude by discussing future work in Section 5.
In this section, we present our framework in terms of program structure we assume and the model of expressions and values we use.
2.1 Program Structure
We assume the input programs are in an intermediate representation that uses a control flow graph (CFG) over basic blocks [1]. A basic block is a code segment that has no unconditional jump or conditional branch statements except for pos- sibly the last statement, and none of its statements, except for possibly the first, is a target of any jump or branch statement. A CFG is a graph representation
Value-Based Partial Redundancy Elimination 173
e ::= s | v op v Expressions
Our goal in value numbering is to obtain three pieces of information: value numbers, available expressions, and anticipated expressions. Traditional value numbering requires only the first two. First, we partition all expressions in the program into values. We represent this partition with a map (or value table) on which we can add and lookup the values for expressions. We treat the value table as a black box, but assume it to be used for a simple hashing value number scheme, recognizing expressions by their structure and, if smart enough, algebraic properties, associating them with values. It is worth noting that there is no limit on how smart the function lookup is engineered to be. For example, if v 1 contains 1, v 2 contains 2, v 4 contains v 3 + v 1 for some v 3 , and v 5 contains v 4 + v 1 , then v 5 also should contain v 3 + v 2. To enumerate all such cases would be infinite. Figure 3(a) displays a running example we will refer to throughout this and the next section. Since it is large, we will often discuss only part of it at a time. For the present, consider block 4. The table in Figure 3(b) displays the values referenced in the block and the expressions they contain. For clarity, we assign a value the same subscript as one of its temporaries wherever possible. Values v 1 , v 2 , and v 3 are defined in blocks that dominate block 4, so they are in scope here. The instruction t 4 ← t 2 + t 3 leads us to discover expressions v 2 + v 3 and t 4 as elements of the same value, which we call v 4. Because of the move t 5 ← t 4 , t 5 is also in v 4. Finally, v 1 + v 5 and t 6 are in a value we call v 6. Recall that these values are global, so we are showing only part of the value table for the entire program (in fact, values v 3 and v 4 have more members, discovered in other parts of the program). The second piece of information is what expressions are available to represent the values; or, put differently, what values are already computed and stored in temporaries at a given program point. If more than one temporary of the same value is live at a point, we want to pick one as the leader, which will be used as the source of the value to replace an operation of that value with a move. To be unambiguous, the leader should be the “earliest” temporary available; that is, the temporary whose defining instruction dominates the defining instructions of all other temporaries of that value live at that point. We define the leader set to be the set of leaders representing available values at a program point. Although technically there is a leader set for any program point, for our purposes we will be interested only in the sets at the end of a basic block, AVAIL OUT. The leaders available out of block 4 are those expressions listed first for each value in Figure 3(b). (This notion of availability is sometimes called upsafety [21].) Finally, we want to know what values are anticipated at a program point; that is, it will be computed or used on all paths from that point to program exit. (This notion of anticipation—sometimes called anticability— is essentially the same as that of downsafety.) Just as we want appropriate temporaries to represent values in the leader set, so we want appropriate expressions to represent anticipated values—that is, anticipated values should also have a reverse-flow leader or antileader. An antileader can be a live temporary or a non-simple
174 T. VanDrunen and A.L. Hosking
(a) Program CFG (b) Values, temporaries, and expressions of block 4
(c) Anticipated expressions at block 6, (d) Actual anticipated expressions not considering the kill of t 11 at block 6
Fig. 3. Running example
176 T. VanDrunen and A.L. Hosking
are defined by a phi in b; and TMP GEN(b) for temporaries that are defined by non-phi instructions in b. There are no kill sets for calculating AVAIL OUT, since SSA form implies no temporary is ever killed. For ANTIC IN, TMP GEN acts as a kill set. Since an available expression must be defined in an instruction that dominates the program point in question, so in calculating AVAIL OUT, we consider inherited expressions only from the block’s dominator. In terms of flow equations,
AVAIL IN[b] = AVAIL OUT[dom(b)] (1) AVAIL OUT[b] = canon(AVAIL IN[b] ∪ PHI GEN(b) (2) ∪TMP GEN(b))
where canon is a procedure that, given a set of temporaries, partitions that set into subsets which all have the same value and chooses a leader from each. Of course canon would be inconvenient and inefficient to implement; instead, AVAIL OUT[b] can be calculated easily and efficiently at the same time as the gen sets, as we will show later. For ANTIC IN, handling successors is more complicated. If there is only one successor, we add all its antileaders to AVAIL OUT of the current block; how- ever, we must translate some temporaries based on the phis at the successor. For example, if t 1 is anticipated by block b, and block b has a phi which defines t 1 and has t 2 as an operand from block c, then t 2 , rather than t 1 , is anticipated at the end of block c, and it has a different value. For this we assume a function phi translate which takes a successor block, a predecessor block (i.e., there is an edge from the second block to the first), and a temporary; if the temporary is defined by a phi at the successor, it returns the operand to that phi correspond- ing to the predecessor, otherwise returning the temporary. If there are multiple successors, then there can be no phis (because of critical edge removal), but only values anticipated by all of the successors can be anticipated by the current block. This assures that all anticipated expressions are downsafe—they can be inserted with assurance that they will be used on all paths to program exit. The flow equations for calculating the antileader sets are
ANTIC OUT[b] =
{e|e ∈ ANTIC IN[succ 0 (b)]∧ ∀ b′^ ∈ succ(b), ∃e′^ ∈ ANTIC IN[b′]| lookup(e) = lookup(e′)} if |succ(b)| > 1
phi translate(A[succ(b)], b, succ(b)) if |succ(b)| = 1
ANTIC IN[b] = clean(canone(ANTIC OUT[b] ∪ EXP GEN[b] (4) −TMP GEN(b)))
When phi translate translates expressions through phis, it may involve creating expressions that have not been assigned values yet and therefore require new values to be created. Consider calculating ANTIC OUT[B 5 ] in our example. Value v 10 is represented by v 4 + v 8 , which is translated through the phi to v 4 + v 7. However, that expression does not exist in the program—it needs a new value.
Value-Based Partial Redundancy Elimination 177
Thus sometimes the value table will need to be modified while calculating these sets. canone generalizes canon for expressions. For ANTIC IN, we do not care what expression represents a value as long as it is live, so canone can make any choice as long as it is consistent. This essentially makes the union in the formula for ANTIC IN to be “value-wise”, meaning that given two sets of expressions rep- resenting values, we want every value in each set represented, but by only one expression each. Similarly, the formula for ANTIC OUT when there are multiple successors performs a “value-wise” intersection—only values that are represented in all successors should be represented here, but which expression represents it does not matter. clean kills expressions that depend on values that have been (or should be) killed. Because information will also flow across back edges in the graph, these sets must be calculated using a fixed-point iteration.
Algorithm. Calculating BuildSets consists of two parts. The first is a top- down traversal of the dominator tree. At each block, we iterate forward over the instructions, making sure each expression has a value assigned to it. We also build EXP GEN, PHI GEN, and TMP GEN for that block. Computing canon directly is inconvenient and costly. We avoid computing it by maintaining an invariant on the relevant sets, that they never contain more than one expression for any value. Since the value leader set for the dominator will have already been determined, we can conveniently build the leader set for the current block by initializing it to the leader set of the dominator and, for each instruction, adding the target to the leader set only if its value is not already represented. This way, we never add a temporary to an AVAIL OUT set unless its value is not yet represented. Similarly, we need only one representative in EXP GEN for each value, so we do not add an expression for a value that has already appeared (this way EXP GEN contains only the first appearance of a value, appropriate for an antileader). We do not calculate AVAIL IN sets since it is trivial. The second part calculates flow sets to determine the antileader sets and conducts the fixed-point iteration. Until we conduct a pass on which no ANTIC IN set changes, we perform top-down traversals of the postdominator tree. This helps fast convergence since information flows backward over the CFG. To keep ANTIC IN canonical, we remove the killed temporaries from ANTIC OUT and EXP GEN separately, and then do what amounts to a value-wise union. Both phi translate and clean process elements in such a way that other set elements that they depend on must be processed first. The key to doing this efficiently is to maintain all sets as topological sorts of the dags they model, which can be done by implementing the sets as linked lists. Figure 4 shows a round of this process on block 5, including ANTIC IN[B 5 ] before and after the insertions from S. Notice that the net effect is to replace v 7 : t 7 in ANTIC OUT with v 7 : v 3 + v 100 , and that although the order has changed, it is still a topolog- ical sort. For this to be efficient, the fixed point iteration must converge quickly; convergence depends primarily on CFG structure. In our benchmarks, the max- imum number of iterations needed for any method being compiled was 55; that,
Value-Based Partial Redundancy Elimination 179
value, and we at least create a new leader for that value in the given block. We update that block’s leader set and its new set. Since this information should be propagated to other blocks which the new temporaries reach, for each block we also add all the expressions in its dominator’s new set into the block’s own leader set and new set. This also requires us to make Insert a top-down traversal of the dominator tree, so that a block’s dominator is processed before the block itself. What we have said so far, however, is not completely safe in the sense that it in some cases it will lengthen a computational path by inserting an instruction without allowing one to be eliminated by the next phase. Since v 3 + v 100 is also anticipated in at block 6, being anti-inherited from t 3 ← t 2 + 1 in block 2, we insert t 14 ← t 3 + 1 in block 4. This does not allow any eliminations, but only lengthens any trace through block 4. The value is still not available at the instruction that triggered this in block 1 because block 1 was visited first, at which time it was available on no predecessors and therefore not hoisted. To fix this, we repeat the process until we make a pass where nothing new is added to any new set. On the next pass, v 7 : t 15 is available in block 6, so we hoist t 1 + 1 to block 1. In practice, Insert converges quickly. We have seen only one case where it required 3 rounds. On most benchmarks, the maximum number of required rounds for any method was 2, and on average it took only a single round. Note that we do not need predicates for determining latest and earliest insertion points, since insertions naturally float to the right place. Insertions made too late in the program will themselves become redundant and eliminated in the next phase.
3.3 Eliminate
Eliminate is straightforward. For any instruction, find the leader of the target’s value. If it differs from that target, then there is a constant or an earlier-defined temporary with the same value. The current instruction can be replaced by a move from the leader to the target. The order in which we process this does not matter. The optimized program is shown in Figure 5.
Corner cases. Certain extensions must be made to this algorithm for theoret- ical optimality (such as code placement in Knoop et al [21]). In practice, we have found them to yield no benefit, but they are described in a technical report [35].
We have proven the concept of this approach by implementing it in an open- source compiler; our implementation is not compared against other hybrid ap- proaches which are difficult to implement, but is shown to subsume GVN. Our experiments use Jikes RVM [2,7,27], a virtual machine that executes Java class- files. We have implemented the algorithm described here as a compiler phase for the optimizing compiler and configured Jikes RVM version 2.3.0 to use the
180 T. VanDrunen and A.L. Hosking
6
t 3 ← t 21
t 4 ← t 2 + t 3 t 5 ← t 4 t 6 ← t 1 + t 5
t 7 ← t 3 + 1
t 8 ← φ(t 1 , t 7 )
t 13 ← t 12 + t 3
t 9 ← t 17 t 10 ← t 19 t 11 ← • t 12 ← t 9 + t 11
t 20 ← t 1 + 1
t 21 ← φ(t 20 , t 15 )
t 14 ← t 3 + 1
t 16 ← t 2 + t 3 t 18 ← t 16 + t 7
t 15 ← φ(t 14 , t 7 ) t 17 ← φ(t 4 , t 18 ) t 19 ← φ(t 6 , t 8 )
5
t 1 ← •
t 2 ← φ(t 1 , t 3 )
1
2
3
4
Fig. 5. The optimized program
optimizing compiler only and a copy mark-sweep garbage collector. The opti- mizing compiler performs a series of code transformations on both SSA and non-SSA representations. It already has loop-invariant code motion (LICM) and global common subexpression elimination (GCSE) phases in SSA, which rely on GVN. Placing our phase before these two shows that GVN-PRE completely subsumes GCSE in practice. LICM is not completely subsumed because Jikes RVM’s LICM performs unsafe speculative motion which GVN-PRE does not. GCSE is equivalent to GVN as it is presented in this paper. Figure 6 shows static eliminations performed by each optimization level. The left column in each set represents the number of intermediate representation op- erations eliminated by GCSE, and the second those eliminated by GVN-PRE. In each case, GVN-PRE eliminates more, sometimes twice as much, although the optimization also inserts operations and in some cases may later eliminate an operation it has inserted. The third bar shows the number GVN-PRE elim- inations plus the number of operations GCSE can eliminate after GVN-PRE
182 T. VanDrunen and A.L. Hosking
has run. After GVN-PRE, GCSE finds zero in all but four cases, and never more than 6, compared to hundreds or thousands eliminated otherwise. Thus our optimization subsumes GCSE. All these are normalized to the first bar. Our performance results are in Figure 7. These were obtained on a 1600MHz Intel Pentium 4 with 512 MB of RAM and 256 KB cache, running Red Hat Linux 3.2.2-5. We used three optimization levels: the pre-defined O2 with GCSE and GVN-PRE turned off, O2 with GCSE on but GVN-PRE off, and O2 with GCSE off but GVN-PRE on. In all cases, LICM and load elimination were turned off to isolate the impact of GCSE and GVN-PRE. We used twenty benchmarks in total, eight from the SPECjvm98 suite [10] and twelve from the sequential benchmarks of the Java Grande Forum [14]. For each benchmark at each optimization level we ran the program eleven times in the same invocation of the VM and timed the last nine runs using Java’s System.currentTimeMillis() method. This way we did not incorporate compilation time in the measurements. We also subtracted garbage collection time. The graph shows the best running time of GCSE and GVN-PRE (out of the nine timed runs) normalized by the best running time of running with neither optimization. In only a few benchmarks is there significant performance gain, and sometimes these optimizations do no pay off. Studies have shown that in an environment like Jikes RVM, in order for an instruction elimination optimization to make a significant impact, it must address the removal of redundant loads and stores for objects and arrays [23], and we plan to extend our algorithm to incorporate those.
We have presented an algorithm in which PRE and GVN are extended into a new approach that subsumes both, describing it formally as a dataflow problem and commenting on a practical implementation. This demonstrates that performing PRE as an extension of GVN can be done simply from a software engineering standpoint, and that it is feasible as a phase of an optimizing compiler. For future work, we plan to produce a version that will deliver real performance gains. Studies have shown that in an environment like Jikes RVM, in order for an instruction elimination optimization to make a significant impact, it must address the removal of redundant loads and stores for objects and arrays [23]. Accordingly, work to extend this algorithm for load and store instructions using Array SSA form [18,15] is underway.
Acknowledgments. The authors thank Di Ma for helpful conversation. We also thank the anonymous reviewers for their suggestions. This work is supported by the National Science Foundation under grants Nos. CCR-9711673, IIS-9988637, and CCR-0085792, by the Defense Advanced Research Program Agency, and by gifts from Sun Microsystems and IBM.
Value-Based Partial Redundancy Elimination 183