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