Advanced Compilers CMPSCI 710 Spring 2003 Common Subexpression
Advanced Compilers CMPSCI 710 Spring 2003 Common Subexpression Elimination Emery Berger University of Massachusetts, Amherst UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science
Topics n Last time n n Dynamic storage allocation, garbage collection This time n Common subexpression elimination n Value numbering n Global CSE UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 2
Determining Equivalence n Goal: eliminate redundant computations n Sparse conditional constant propagation: n n n Eliminates multiple computations Eliminates unnecessary branches Can we eliminate equivalent expressions without constants? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 3
Common Subexpression Elimination n Recognizes textually identical (or commutative) redundant computations n n Replaces second computation by result of the first How do we do this efficiently? UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 4
Value Numbering n Each variable, expression, and constant: unique value number n n n Same number ) computes same value Based on information from within block Use hash functions to compute these UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 5
Computing Value Numbers n Assign values to variables n n Map expressions to values n n a = 3 ) value(a) = 3 a = b + 2 ) value(a) = hash(+, value(b), 2) Use appropriate hash function n Plus: commutative n n hashc(+, value(b), 2) = hashc(+, 2, value(b)) Minus: not commutative n hash(-, value(b), 2) hash(-, 2, value(b)) UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 6
Value Numbering Summary n n Forward symbolic execution of basic block Each new value assigned to temporary n Preserves value for later use even if original variable rewritten n n a = x+y; a = a+z; b = x+y ) a = x+y; t = a; a = a+z; b = t; Maps n Var to Val n n Exp to Val n n specifies symbolic value for each variable specifies value of each evaluated expression Exp to Tmp n specifies tmp that holds value of each evaluated expression UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 7
Map Usage n Var to Val n n Exp to Tmp n n Used to compute symbolic value of y and z when processing statement of form x = y + z Used to determine which temp to use if value(y) + value(z) previously computed when processing statement of form x = y+z Exp to Val n Used to update Var to Val when n processing statement of the form x = y + z, and n value(y) + value(z) previously computed UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 8
Computing Value Numbers, Example New Basic Block Original Basic Block a = x+y b = a+z b = b+y c = a+z Var Val Exp Tmp x v 1 y v 2 a v 3 z v 4 b v 5 v 6 c v 5 v 1+v 2 v 3+v 4 v 5+v 2 v 6 v 1+v 2 t 1 v 3+v 4 t 2 v 5+v 2 t 3 UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science a = x+y t 1 = a b = a+z t 2 = b b = b+y t 3 = b c = t 2 9
Interesting Properties n Finds common subexpressions even if they use different variables in expressions n n y = a+b; x = b; z = a+x ) y = a+b; t = y; x = b; z = t Finds common subexpressions even if variable that originally held the value was overwritten n y = a+b; x = b; y = 1; z = a+x ) y = a+b; t = y; x = b; y = 1; z = t UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 10
Problems n Algorithm has a temporary for each new value n n Introduces n n n a = x+y; t 1 = a; lots of temporaries lots of copy statements to temporaries In many cases, temporaries and copy statements are unnecessary n Eliminate with copy propagation and dead code elimination UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 11
Global CSE n n Value numbering eliminates some subexpressions but not all l’s value is not always equal to j’s or k’s value UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 12
Available Expressions n Global CSE requires computation of available expressions for blocks b: n n n Expressions on every path in cfg from entry to b No operand in expression redefined Then use appropriate temp variable for used available expressions UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 13
Available Expressions: Dataflow Equations n For a block b: n n n AEin(b) = expressions available on entry to b KILL(b) = expressions killed in b EVAL(b) = expressions defined in b and not subsequently killed in b UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 14
Available Expressions, Example n n Build control-flow graph Solve dataflow problem n Initialize AEin(i) = universal set of expressions n AEin(b) = Åj 2 Pred(i)AEout(j) n AEout(b) = EVAL(i) [ (AEin(i) – KILL(i)) UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 15
Next Time n n Partial Redundancy Elimination Read ACDI: n Ch. 13 UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 16
Value Numbering Example n Step 1: insert temps for conditionals UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 17
Value Numbering Example n Step 2: n n Add entries for each rhs Remove entries when dependent variable changes UNIVERSITY OF MASSACHUSETTS, AMHERST • Department of Computer Science 18
- Slides: 18