Optimization algorithms using SSA

download Optimization algorithms using SSA

of 27

  • date post

    31-Dec-2015
  • Category

    Documents

  • view

    21
  • download

    0

Embed Size (px)

description

Optimization algorithms using SSA. Software Optimizations & Restructuring Research Group School of Electronical Engineering Seoul National University 2006-21166 wonsub Kim. Using static single assignment form (SSA). Review of SSA translation Place Φ function using dominance frontier - PowerPoint PPT Presentation

Transcript of Optimization algorithms using SSA

  • Optimization algorithms using SSASoftware Optimizations & Restructuring Research GroupSchool of Electronical EngineeringSeoul National University2006-21166 wonsub Kim

    Y. Paek

  • Using static single assignment form (SSA)Review of SSA translationPlace function using dominance frontierOverview of SSA form (why SSA form? )SSA form transform stepRole of SSA form, benefit of SSA formFurther optimization using SSA formConstant propagation, dead code elimination, induction variable reduction and other issues.

    Y. Paek

  • Review of SSA form translationObservationNode X does not dominates ZNode x dominates a immediate dominator of ZKey observationinsert the function on the first node Z that is common to another path originating in a node Y with an assignment to VNode z definition is same as dominance frontier definitionPlace function in the domi-nance frontier nodes of the nodes where each def of V is

    Y. Paek

  • Review of SSA form translationDominance frontier definitionDefinition node sets which dominates the immediate predecessor of node Y, but do not dominate node Y.From A's point of view, these are the nodes at which other control paths that don't go through A make their earliest appearance.Case 1 definition dominate node definition reachableCase 2 case 1 node dominance frontier other definition flow .How about the assignments in the loop?it also needs to be use function which merge multiple definitions

    Y. Paek

  • Static single assignment (SSA) formSingle assignment formIn MA form, variable is memory location, not a value. In SA form, variable is a value. Simplifying property of variableData structure - only a single assignment to a variable (single def-inition), but many uses of it (only one def site, lists of use site)A def must dominate all of its uses!!Variables are renamed to remove multiple assignmentsRole of SSA form in optimizationData flow analysis and compiler optimizations are more efficient with SA form and SSA form simplify many optimizationsThe need for use-def chain is removed & that info explicitly appear.Quadratic number of use-def chain O(n^2)-> linear number O(n)Eliminates false dependences (simplifying context)

    Y. Paek

  • Why SSA form?SSA form provides the compiler with a solution to the question which definitions of a variable reach the points where it is used? -> Make def-use chains explicitevery definition knows its uses and every use know its single definitionMakes dataflow optimization more easier and fasterFor most optimizations reduce space/time requirementsDU chains in SSA form save more space, but spend moreIn MA form, # of chains for V variablesDef-use chains are so expensive!!Worst case # of chains = O(# of defs (v) * # of uses (v))
  • Space reduction in DU chainDef-use chain structure is more simplified

    Y. Paek

  • SSA transform stepSSA transformation step1. To get some efficiency from SA form, translate the original code into SA form statically.2. SSA form is not executable due to pseudo-instructions (compiler internal form), thus should be translated back to MA form executionOriginal Code(MA form) OptimizationsOptimized Code (MA form)SSA formcode motion, redundancy elimination, constant propagation,

    Y. Paek

  • Static single assignmentformUsing SSA, further possible optimizationsConstant propagation (simple constant, conditional constant)Dead code eliminationInduction variable identificationGlobal Value numbering(p349~355), data dependences..Register allocationOther considerationsMore variables, Increase in code size due to -functionsBut only linearly increased (in practice, SSA is 0.6-2.4 times larger)Some optimizations are more annoyingBut on the whole, a win for compilersHow does the function choose which xi to use?We dont really care about it

    Y. Paek

  • Dead code elimination (1)Problem definitionassignment to variable with no use can be removedSSA structureonly one definition site & a list of use sites ( easy to check liveness)Worklist algorithmW
  • Dead code elimination (2)Just check which variable value Vi do not show upRemove the phi function or statement that creates ViRecurse as new Vi value may now have become dead

    Y. Paek

  • Dead code elimination example x := 4 y := y+3

    L: z := y*5 need extra operation to avoid dangling pointers to du chains for removed statementsDU chains in MA form x1 := 4 y2 := y1+3 goto L on x1

  • Constant propagationEvaluate expression at compile time, eliminate dead code, improve efficacy of other optimizationWhat is constants??Simple constant constant for all paths through a programFaster algorithm - sparse simple constants algorithm using SSA translationConditional constants constant for actual paths throughFaster algorithm - sparse conditional constants algorithm using SSA translationKey points!!sparse SSA edge traverse is more efficient than Normal CFG edge traverse

    Y. Paek

  • Simple constant propagation using SSAStandard worklist algorithmIdentify simple constantsIf variable is defined using only one constant value, it is simple constantIf variable definition uses phi func and func arguments are all same, v is simple constant!!Simple constantsFirst iteration - i1, j1, k1Second iteration j3 addedTraverse all edges in the CFG, and process.
  • Sparse simple constant propagationStandard simple constant propagate algorithm is inefficient!!For each program points (CFG edge connected node), maintain one constant value for each var.O(EV), E # of edge in CFGInefficient, since constant may have to be propagated through irrelevant node -> exploit spare dependenceExploit SSA edges (explicitly connect defs with uses)Iterate over SSA edges instead of over all CFG edges, SSA has fewer edge than def-use graph

    Y. Paek

  • Sparse simple constant propagationSparse simple worklist algorithmW
  • Conditional constant propagationDelete infeasible branch due to discovered constantData flow analysisLattice (over defined, defined, never defined)Executability Is there evidence that block B can ever be executed?Executable assignment assignments in a executable block BProcessed in compile time not in run timeAlgorithmSimultaneously find constants + eliminates infeasible branchFirst find out executable block using following observationIf x < y goto L1 else L2 V[x] = Top or V[y] = Top -> E(L1, L2)=TIf x < y goto L1 else L2 V[x]=c1, V[y]=c2, c1!=c2, L1 or L2 takenSecond, in any assignments, get lattice value for variablesBlock executability and variable lattice is updated repeatedly

    Y. Paek

  • Conditional constant propagation exampleBy analysis, j2, j3, j5 are always 1, else part is not reachableUnreachable code is eliminated by dead code eliminationSource codeAnalysis for conditional constant propagationSSA form transformDead code elimination

    Y. Paek

  • Conditional constant propagation resultB1 :I1
  • Induction variable identificationInduction variable reductionOptimize the SSA graph rather than handling CFG directlySSA graph clarify the link from data use to its definitionInduction variable value is increased/decreased by constant in each iteration.I0 first entry valueI2 value after going through loopRC loop invariant expression

    Y. Paek

  • Induction variable identificationSSA-based algorithmBuild SSA representationIterate from innermost CFG loop to outermost loop (just like loop invariant code motion search.. Innermost -> outermost )finding SSA cycleEach cycle may be basic induction variable if a variable in a cycle is a function of loop invariants and its value on the current iteration (how to detect this condition?)Phi function in the cycle have as one of its inputs a def from inside the loop and a def from outside the loopThe def inside the loop (phi function input) will be part of the cycle and get one operand from the phi function and all others will be loop invariantFind derived induction variable

    Y. Paek

  • Induction variable identification exampleSource code transformation to SSA formBuild SSA value graphFind a cycle from SSA graphCycle phi function basic induction variable!!Variable x caseSSA value graph

    Y. Paek

  • Induction variable identification example1. transform SSA form2. find SSA cycleI2,m2 has a SSA cycle biv!!

    Y. Paek

  • Global Value numberingGlobal Value numbering (GVN)Symbolic evaluation (not run-time evaluation), if symbolic number is same, two computation are equal.Compiler optimization based on the SSA IRBuild value graph from the SSA formprevent the false variable name-value name mappingsmore powerful that global common sub expression (CSE) in some cases

    Y. Paek

  • Value numbering exampleSSA transform

    Y. Paek

  • Dependency issuesIn optimization, parallelization & scheduling, dependency check is important3 data dependence - true ( read after write), anti (write after read), output (write after write) dependencySSA form, true dependence is evident. (def site, use list)There are no anti, output dependence in SSA formSSA form has a single definition of each variableso write after read dependency cant occur, why?so write after write dependency cant occur, why?Variable definition dominates all use of it.

    Y. Paek

  • Other issuesCopy propagationCheck live range of each variable, replace the target variable uses following the copy operation by source variables if the target variable is live.In SSA form, variable values are assured to remain statically singleSo, there is no need to check live range, simply replace it!Register allocation, common sub-expression elimination

    Y. Paek