Advanced Compilers CMPSCI 710 Spring 2003 Basic Loop OptimizationsTopicsEasy Detection of Loop Induction VariablesTaxonomy of Induction VariablesStrength ReductionStrength Reduction AlgorithmStrength Reduction ExampleCandidates for Strength ReductionSlide 9Strength Reduction ExamplesLinear Test ReplacementLinear Test Replacement ExampleLoop UnrollingScalar ReplacementScalar Replacement ExampleNext TimeUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCEEmery BergerUniversity of Massachusetts, AmherstAdvanced CompilersCMPSCI 710Spring 2003Basic Loop OptimizationsUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE2TopicsLast timeOptimizations using SSA formConstant propagation & dead code eliminationLoop invariant code motionThis timeLoop optimizationsInduction variableLinear test replacementLoop unrollingScalar replacementUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE3Easy Detectionof Loop Induction VariablesPattern match & check:Search for “i = i + b” in loopi is induction variable if no other assignment to i in loopPros & Cons:+Easy!-Does not catch all loop induction variablese.g., “i = a * c + 2”UUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE4Taxonomy of Induction Variablesbasic induction variable:only definition in loop is assignmentj = j § c, where c is loop invariantmutual induction variable:definition is linear function of other induction variable i‘:i = c1 * i‘ § c2i = i‘ / c1 § c2family of basic induction variable j:set of induction variables i such that i is always assigned linear function of jUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE5Strength ReductionReplace “expensive” op by “cheaper” oneE.g., replace multiply by additionApply to induction variable familiesEspecially: array indexingUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE6Strength Reduction AlgorithmLet i be induction variable in the family of basic induction variable j:i = c1 * j + c2Create new variable i’Initialize in pre-header: i’ = c1*j + c2Track value of j: after j = j + c3, add i’ = i’ + (c1 * c3)UUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE7Strength Reduction ExampleUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE8Candidates for Strength ReductionInduction variable IV multiplied by invariantRecursively:IV * IV, IV mod constant, IV + IVUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE9Strength Reduction AlgorithmUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE10Strength Reduction Examplesbasic induction variable:only definition in loop is assignmentj = j § c, where c is loop invariantmutual induction variable:definition is linear function of other induction variable i‘:i = c1 * i‘ § c2i = i‘ / c1 § c2family of basic induction variable j:set of induction variables i such that i is always assigned linear function of jUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE11Linear Test ReplacementEliminates induction variable!After strength reduction, loop test is often last use of induction variableAlgorithm:If only use of IV is loop test and its own increment, and test is always computedi.e., only one exit from loopReplace test with equivalent one:E.g., “i comp k” ) “i_50 comp k*50”UUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE12Linear Test Replacement ExampleUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE13Loop UnrollingTo reduce loop overhead, we can unroll loopsAdvantages:+Execute fewer total instructions+More fodder for common subexpression elimination, strength reduction, etc.+Move consecutive access closer togetherDisadvantages:-Code bloat-Still updating through memoryUUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE14Scalar ReplacementProblem: register allocators never keep a[i] in registerIdea: trick allocatorLocate patterns of consistent reuseReplace load with copy into temporaryReplace store with copy from temporaryMay need copies at end of loopE.g., when reuse spans > 1 iterationAdvantages:Decreases number of loads and storesKeeps reused values in registersBig performance impact (2x, 3x!)UUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE15Scalar Replacement ExampleScalar replacement exposes the reuse of a[i]Traditional scalar analysis – inadequateUse dependence analysis to understand array references (later)UUNIVERSITYNIVERSITY OFOF M MASSACHUSETTS, ASSACHUSETTS, AAMHERSTMHERST • • D DEPARTMENTEPARTMENT OF OF CCOMPUTER OMPUTER SSCIENCECIENCE16Next TimeCommon Subexpression EliminationRead ACDI:Ch. 12, pp. 343-355Ch. 13, pp.
View Full Document