Dataflow I: Dataflow Analysis

Click here to load reader

download Dataflow I: Dataflow Analysis

of 33

  • date post

    08-Jan-2016
  • Category

    Documents

  • view

    74
  • download

    0

Embed Size (px)

description

Dataflow I: Dataflow Analysis. EECS 483 – Lecture 23 University of Michigan Monday, November 27, 2006. Announcements and Reading. Project 3 – should have started work on this Schedule for the rest of the semester Today – Dataflow analysis Wednes 11/29 – Finish dataflow, optimizations - PowerPoint PPT Presentation

Transcript of Dataflow I: Dataflow Analysis

  • Dataflow I:Dataflow AnalysisEECS 483 Lecture 23University of MichiganMonday, November 27, 2006

    - * -

    Announcements and ReadingProject 3 should have started work on thisSchedule for the rest of the semesterToday Dataflow analysisWednes 11/29 Finish dataflow, optimizationsMon 12/4 Optimizations, start on register allocationWednes 12/6 Register allocation, Exam 2 reviewMon 12/11 Exam 2 in classWednes 12/13 No class (Project 3 due)Reading for todays class10.5, 10.6. 10.10, 10.11

    - * -

    From Last Time - Class ProblemL1: if (a < b) goto L11L2: goto L7L3: goto L4L4: stuff4L5: if (c < d) goto L15L6: goto L2L7: if (c < d) goto L13L8: goto L12L9: stuff 9L10: if (a < c) goto L3L11:goto L9L12: goto L2L13: stuff 13L14: if (e < f) goto L11L15: stuff 15L16: rtsMaximally optimize the control flow of this codeL1: if (a < b) goto L9L2: if (c < d) goto L13 goto L2L4: stuff4L5: if (c < d) goto L15L6: if (c < d) goto L13 goto L2L9: stuff 9L10: if (a < c) goto L4L11:goto L9L13: stuff 13L14: if (e < f) goto L9L15: stuff 15L16: rtsBlocks 7, 8, 12 are unreachableBlock 3 is empty

    - * -

    Dataflow Analysis IntroductionWhich VRs contain useful data values? (liveness or upward exposed uses)

    Which definitions may reachthis point? (reaching defns)

    Which definitions are guaranteedto reach this point? (available defns)

    Which uses below are exposed?(downward exposed uses)Pick an arbitrary point in the programr1 = r2 + r3r6 = r4 r5

    r4 = 4r6 = 8r6 = r2 + r3r7 = r4 r5Dataflow analysis Collection of informationthat summarizes the creation/destruction ofvalues in a program. Used to identify legal optimization opportunities.

    - * -

    Live Variable (Liveness) AnalysisDefn: For each point p in a program and each variable y, determine whether y can be used before being redefined starting at pAlgorithm sketchFor each BB, y is live if it is used before defined in the BB or it is live leaving the blockBackward dataflow analysis as propagation occurs from uses upwards to defs4 setsUSE = set of external variables consumed in the BBDEF = set of variables defined in the BBIN = set of variables that are live at the entry point of a BBOUT = set of variables that are live at the exit point of a BB

    - * -

    Liveness Exampler1 = r2 + r3r6 = r4 r5

    r4 = 4r6 = 8r6 = r2 + r3r7 = r4 r5r2, r3, r4, r5 are all live as theyare consumed later, r6 is deadas it is redefined laterr4 is dead, as it is redefined.So is r6. r2, r3, r5 are liveWhat does this mean? r6 = r4 r5 isuseless, it produces a dead value !!Get rid of it!

    - * -

    Compute USE/DEF Sets For Each BBfor each basic block in the procedure, X, do DEF(X) = 0 USE(X) = 0 for each operation in sequential order in X, op, do for each source operand of op, src, do if (src not in DEF(X)) then USE(X) += src endif endfor for each destination operand of op, dest, do DEF(X) += dest endfor endforendfordef is the union of all the LHSsuse is all the VRs that are used before defined

    - * -

    Example USE/DEF Calculationr1 = MEM[r2+0]r2 = r2 + 1r3 = r1 * r4r1 = r1 + 5r3 = r5 r1r7 = r3 * 2r2 = 0r7 = 23r1 = 4r8 = r7 + 5r1 = r3 r8r3 = r1 * 2

    - * -

    Compute IN/OUT Sets For All BBsinitialize IN(X) to 0 for all basic blocks Xchange = 1while (change) do change = 0 for each basic block in procedure, X, do old_IN = IN(X) OUT(X) = Union(IN(Y)) for all successors Y of X IN(X) = USE(X) + (OUT(X) DEF(X)) if (old_IN != IN(X)) then change = 1 endif endforendforIN = set of variables that are live when the BB is enteredOUT = set of variables that are live when the BB is exited

    - * -

    Example IN/OUT Calculationr1 = MEM[r2+0]r2 = r2 + 1r3 = r1 * r4r1 = r1 + 5r3 = r5 r1r7 = r3 * 2r2 = 0r7 = 23r1 = 4r8 = r7 + 5r1 = r3 r8r3 = r1 * 2USE = r2,r4DEF = r1,r2,r3USE = DEF = r1,r2,r7USE = r1,r5DEF = r1,r3,r7USE = r3,r7DEF = r1,r3,r8

    - * -

    Class Problemr1 = 3r2 = r3r3 = r4r1 = r1 + 1r7 = r1 * r2r2 = 0r2 = r2 + 1r4 = r2 + r1r9 = r4 + r8Compute liveness, iecalculate USE/DEFcalculate IN/OUT

    - * -

    Reaching Definition Analysis (rdefs)A definition of a variable x is an operation that assigns, or may assign, a value to x A definition d reaches a point p if there is a path from the point immediately following d to p such that d is not killed along that path A definition of a variable is killed between 2 points when there is another definition of that variable along the pathr1 = r2 + r3 kills previous definitions of r1

    - * -

    Reaching Defs Example1: r1 = r2 + r32: r6 = r4 r5

    3: r4 = 44: r6 = 85: r6 = r2 + r36: r7 = r4 r5defs 1 and 2 reach this pointdefs 1, 3, 4 reach this pointdef 2 is killed by 4 defs 1, 3, 5, 6 reach this pointdefs 2, 4 are killed by 5

    - * -

    Reaching Definition Analysis (rdefs)Algorithm sketchForward dataflow analysis as propagation occurs from defs downwards4 setsGEN = set of definitions generated in the BB (operations not registers like liveness !!)KILL = set of definitions killed in the BBIN = set of definitions reaching the BB entryOUT = set of definitions reaching the BB exit

    - * -

    Compute Rdef GEN/KILL Sets For Each BBfor each basic block in the procedure, X, do GEN(X) = 0 KILL(X) = 0 for each operation in sequential order in X, op, do for each destination operand of op, dest, do G = op K = {all ops which define dest op} GEN(X) = G + (GEN(X) K) KILL(X) = K + (KILL(X) G) endfor endforendforgen = set of definitions created by an operationkill = set of definitions destroyed by an operation Assume each operation only has 1 destinationso just keep track of ops.

    - * -

    Example Rdef GEN/KILL Calculation1: r1 = MEM[r2+0]2: r2 = r2 + 13: r3 = r1 * r44: r1 = r1 + 55: r3 = r5 r16: r7 = r3 * 27: r2 = 08: r7 = 239: r1 = 410: r8 = r7 + 511: r1 = r3 r812: r3 = r1 * 2

    - * -

    Compute Rdef IN/OUT Sets for all BBsinitialize IN(X) = 0 for all basic blocks Xinitialize OUT(X) = GEN(X) for all basic blocks Xchange = 1while (change) do change = 0 for each basic block in procedure, X, do old_OUT = OUT(X) IN(X) = Union(OUT(Y)) for all predecessors Y of X OUT(X) = GEN(X) + (IN(X) KILL(X)) if (old_OUT != OUT(X)) then change = 1 endif endforendforIN = set of definitions reaching the entry of BBOUT = set of definitions leaving BB

    - * -

    Example Rdef IN/OUT Calculation1: r1 = MEM[r2+0]2: r2 = r2 + 13: r3 = r1 * r44: r1 = r1 + 55: r3 = r5 r16: r7 = r3 * 27: r2 = 08: r7 = 239: r1 = 410: r8 = r7 + 511: r1 = r3 r812: r3 = r1 * 2GEN = 1,2,3KILL = 4,5,7,9,11,12 GEN = 7,8,9KILL = 1,2,4,6,11GEN = 10,11,12KILL = 1,3,4,5,9GEN = 4,5,6KILL = 1,3,8,9,11,12

    - * -

    Class Problem1: r1 = 32: r2 = r33: r3 = r44: r1 = r1 + 15: r7 = r1 * r26: r2 = 07: r2 = r2 + 18: r4 = r2 + r19: r9 = r4 + r8Reaching definitions Calculate GEN/KILL Calculate IN/OUT

    - * -

    DU/UD ChainsConvenient way to access/use reaching defs infoDef-Use chainsGiven a def, what are all the possible consumers of the operand producedMaybe consumerUse-Def chainsGiven a use, what are all the possible producers of the operand consumedMaybe producer

    - * -

    Class Problemr1 = 3r2 = r3r3 = r4r1 = r1 + 1r7 = r1 * r2r2 = 0r2 = r2 + 1r4 = r2 + r1r9 = r4 + r8Find the DU/UD Chains

    - * -

    Some Things to Think AboutLiveness and reaching defs are basically the same thing!!!!!!!!!!!!!!!!!!All dataflow is basically the same with a few parametersMeaning of gen/kill (use/def)Backward / ForwardAll paths / some paths (must/may)So far, we have looked at may analysis algorithmsHow do you adjust to do must algorithms?Dataflow can be slowHow to implement it efficiently?How to represent the info?

    - * -

    Generalizing Dataflow AnalysisTransfer functionHow information is changed by something (BB)OUT = GEN + (IN KILL) forward analysisIN = GEN + (OUT KILL) backward analysisMeet functionHow information from multiple paths is combinedIN = Union(OUT(predecessors)) forward analysis OUT = Union(IN(successors)) backward analysisNote, this is only for any path

    - * -

    Generalized Dataflow Algorithmwhile (change)change = falsefor each BBapply meet functionapply transfer functionif any changes change = true

    - * -

    Liveness Using GEN/KILLLiveness = upward exposed uses

    for each basic block in the procedure, X, do up_use_GEN(X) = 0 up_use_KILL(X) = 0 for each operation in reverse sequential order in X, op, do for each destination operand of op, dest, do up_use_GEN(X) -= dest up_use_KILL(X) += dest endfor for each source operand of op, src, do up_use_GEN(X) += src up_use_KILL(X) -= src endfor endforendfor

    - * -

    Example - Liveness with GEN/KILLr1 = MEM[r2+0]r2 = r2 + 1r3 = r1 * r4r1 = r1 + 5r3 = r5 r1r7 = r3 * 2r2 = 0r7 = 23r1 = 4r3 = r3 + r7r1 = r3 r8r3 = r1 * 2up_use_GEN(4.1) = r1 up_use_KILL(4.1) = r3 up_use_GEN(4.2) = r3,r8 up_use_GEN(4.3) = r3,r7,r8 up_use_KILL(4.2) = r1 up_use_KILL(4.3) = r1 up_use_KILL(3) = r1, r2, r7 up_use_KILL(1) = r1,r3 up_use_KILL(2) = r3,r7 up_use_GEN(3) = 0 up_use_GEN(1) = r2,r4 up_use_GEN(2) = r1,r5 meet: OUT = Union(IN(succs))xfer: IN = GEN + (OUT KILL)BB1BB2BB3BB4

    - * -

    Beyond Liveness (Upward Exposed Uses)Upward exposed defsIN = GEN + (OUT KILL)OUT = Union(IN(successors))Walk ops reverse orderGEN += dest; KILL += destDownward exposed usesIN = Union(OUT(predecessors))OUT = GEN + (IN-KILL)Walk ops forward orderGEN += src; KILL -= src;GEN -= dest; KILL += dest;

    Downward exposed defsIN = Union(OUT(predecessors))OUT = GEN + (IN-KILL)Walk ops forward orderGEN