Advanced Compilers CMPSCI 710 Spring 2003 Common Subexpression EliminationTopicsDetermining EquivalenceCommon Subexpression EliminationValue NumberingComputing Value NumbersValue Numbering SummaryMap UsageSlide 9Interesting PropertiesProblemsGlobal CSEAvailable ExpressionsAvailable Expressions: Dataflow EquationsAvailable Expressions, ExampleNext TimeValue Numbering ExampleSlide 18UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer ScienceEmery BergerUniversity of Massachusetts, AmherstAdvanced CompilersCMPSCI 710Spring 2003Common Subexpression EliminationUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science2TopicsLast timeDynamic storage allocation, garbage collectionThis timeCommon subexpression eliminationValue numberingGlobal CSEUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science3Determining EquivalenceGoal: eliminate redundant computationsSparse conditional constant propagation:Eliminates multiple computationsEliminates unnecessary branchesCan we eliminate equivalent expressions without constants?UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science4Common Subexpression EliminationRecognizes textually identical (or commutative) redundant computationsReplaces second computationby result of the firstHow do we do this efficiently?UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science5Value NumberingEach variable, expression, and constant:unique value numberSame number ) computes same valueBased on information from within blockUse hash functions to compute theseUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science6Computing Value NumbersAssign values to variablesa = 3 ) value(a) = 3Map expressions to valuesa = b + 2 ) value(a) = hash(+,value(b),2)Use appropriate hash functionPlus: commutativehashc(+,value(b),2) = hashc(+,2,value(b))Minus: not commutativehash(-,value(b),2) hash(-,2,value(b))UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science7Value Numbering SummaryForward symbolic execution of basic blockEach new value assigned to temporaryPreserves value for later use even if original variable rewrittena = x+y; a = a+z; b = x+y) a = x+y; t = a; a = a+z; b = t;MapsVar to Valspecifies symbolic value for each variableExp to Valspecifies value of each evaluated expressionExp to Tmpspecifies tmp that holds value of each evaluated expressionUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science8Map UsageVar to Val Used to compute symbolic value of y and z when processing statement of form x = y + zExp to TmpUsed to determine which temp to use if value(y) + value(z) previously computed when processing statement of form x = y + zExp to ValUsed to update Var to Val when processing statement of the form x = y + z, andvalue(y) + value(z) previously computedUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science9b v5b v6a = x+yb = a+zb = b+yc = a+za = x+yt1 = ab = a+zt2 = bb = b+yt3 = bx v1y v2a v3z v4c v5Original Basic BlockNew Basic BlockVarValv1+v2 v3v3+v4 v5ExpValv1+v2 t1v3+v4 t2ExpTmpc = t2v5+v2 v6v5+v2 t3Computing Value Numbers,ExampleUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science10Interesting PropertiesFinds common subexpressions even if they use different variables in expressionsy = a+b; x = b; z = a+x) y = a+b; t = y; x = b; z = tFinds common subexpressions even if variable that originally held the value was overwritteny = a+b; x = b; y = 1; z = a+x) y = a+b; t = y; x = b; y = 1; z = tUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science11ProblemsAlgorithm has a temporary for each new valuea = x+y; t1 = a;Introduceslots of temporarieslots of copy statements to temporariesIn many cases, temporaries and copy statements are unnecessaryEliminate with copy propagation and dead code eliminationUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science12Global CSEValue numbering eliminates some subexpressions but not alll’s value is not always equal to j’s or k’s valueUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science13Available ExpressionsGlobal CSE requires computation of available expressions for blocks b:Expressions on every path in cfg from entry to bNo operand in expression redefinedThen use appropriate temp variable for used available expressionsUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science14Available Expressions:Dataflow EquationsFor a block b:AEin(b) = expressions available on entry to bKILL(b) = expressions killed in bEVAL(b) = expressions defined in b and not subsequently killed in bUUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science15Available Expressions,ExampleBuild control-flow graphSolve dataflow problemInitialize AEin(i) = universal set of expressionsAEin(b) = Åj 2 Pred(i)AEout(j)AEout(b) = EVAL(i) [ (AEin(i) – KILL(i))UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science16Next TimePartial Redundancy
View Full Document