Download Dataflow Analysis - Lecture Slides - Fall 2004 | EECS 583 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!
Dataflow Analysis + Optimization
Y^ Control flow analysis^ »^ Treat BB as black box^ »^ Just care about branches Y^ Now^ »^ Start looking at ops in BBs^ »^ What’s computed and where Y^ Classical optimizations^ »^ Want to make thecomputation more efficient Y^ Ex: Common SubexpressionElimination (CSE)^ »^ Is r2 + r3 redundant?^ »^ Is r4 – r5 redundant?^ »^ What if there were 1000 BB’s^ »^ Dataflow analysis !!
r1 = r2 + r3^ r6 = r4 – r5^ r4 = 4^ r6 = 8 r6 = r2 + r3^ r7 = r4 – r
Live Variable (Liveness) Analysis^ Y^ Defn: For each point p in a program and each variable y,determine whether y can be used before being redefinedstarting at p^ Y^ Algorithm sketch^ »^ For each BB, y is live if it is used before defined in the BB or it islive leaving the block^ »^ Backward dataflow analysis as propagation occurs from usesupwards to defs^ Y^ 4 sets^ »^ GEN = set of external variables consumed in the BB^ »^ KILL = set of external variable uses killed by the BB^ y^ equivalent to set of variables defined by the BB^ »^ IN = set of variables that are live at the entry point of a BB^ »^ OUT = set of variables that are live at the exit point of a BB
Liveness Example^ r1 = r2 + r3r6 = r4 – r
r2, r3, r4, r5 are all live as they are consumed later r6 is dead as it is redefined later^ r4 is dead, as it is redefined.^ So is r6. r4 = 4 r6 = 8^ r2, r3, r5 are live What does this mean? r6 = r4 – r5 is useless, it produces a dead value !! Get rid of it.
r6 = r2 + r3^ r7 = r4 – r
Compute IN/OUT Sets for all BBs^ initialize IN(X) to 0 for all basic blocks X^ change = 1^ while^ (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) = GEN(X) + (OUT(X) – KILL(X))^ if^ (old_IN != IN(X)) then^ change = 1^ endif^ endfor^ endfor
Liveness in Elcor^ Y^ Calculating the info^ »^ delete_local_analysis_info_for_all_hbs_bbs(Region);^ »^ create_local_analysis_info_for_all_hbs_bbs(Region);^ y^ Analysis/pred_analysis.cpp^ y^ Calculates intra-BB info (i.e., GEN/KILL)^ »^ el_flow_compute_liveness(Region, ANALYZE_ALLREG);^ y^ Analysis/flow_analysis_solver.cpp^ y^ Applies meet/transfer functions to compute IN/OUT^ »^ Generally liveness always done on a Procedure^ Y^ Accessing the info^ »^ Stored as an attribute on Control edges from branches^ »^ Liveness_info live = get_liveness_info(Edge*);^ »^ Liveness_info^ Æ
List » Iterate over it, check for membership, etc.
Class Problem
Compute liveness^ Calculate GEN/KILL for each BB r1 = 3^ Calculate IN/OUT for each BB r2 = r3 r3 = r r1 = r1 + 1 r7 = r1 * r2 r4 = r4 + 1 r4 = r3^ r8 = 8^ r9 = r7 + r
Rdefs (2)^ Y^ Algorithm sketch^ » Forward dataflow analysis as propagation occurs fromdefs downwards^ » (Liveness was backwards!)^ Y^ 4 sets^ » GEN = set of definitions generated in the BB^ » KILL = set of definitions killed in the BB^ » IN = set of definitions reaching the BB entry^ » OUT = set of definitions reaching the BB exit
- EECS 583 – Lecture 6 Dataflow Analysis University of Michigan January 28,
- Example - GEN/KILL Computation BB1 r1 = MEM[r2+0] r2 = r2 + 1 r3 = r1 * r
- GEN(1) = r2,r4 KILL(1) = r1,r
- GEN(2) = r1,r5 r1 = r1 + 5 r3 = r5 – r1 r7 = r3 *
- BB3 r2 = 0 r7 = 23 r1 =
- KILL(2) = r3,r7 BB
- GEN(3) = 0 KILL(3) = r1, r2, r
- BB4 r3 = r3 + r7 GEN(4.3) = r3,r7,r8 r1 = r3 – r8 r3 = r1 *
- KILL(4.3) = r1 GEN(4.2) = r3,r8 KILL(4.2) = r1 GEN(4.1) = r1 KILL(4.1) = r
- Reaching Defs Example 1: r1 = r2 + r32: r6 = r4 – r
- defs 1 and 2 reach this point 3: r4 = 4 4: r6 = 8 defs 1, 3, 4 reach this point def 2 is killed by
- 5: r6 = r2 + r3 6: r7 = r4 – r
- defs 1, 3, 5, 6 reach this point defs 2, 4 are killed by
Example Rdef GEN/KILL Calculation^ r1 = MEM[r2+0]^ r2 = r2 + 1^ r3 = r1 * r4^ r1 = r1 + 5^ r3 = r5 – r1^ r7 = r3 * 2
r2 = 0 r7 = 23 r1 = 4 r8 = r7 + 5 r1 = r3 – r8 r3 = r1 * 2
Compute Rdef IN/OUT Sets for all BBs^ IN = set of definitions reaching the entry of BB^ OUT = set of definitions leaving BB^ initialize IN(X) = 0 for all basic blocks X^ initialize OUT(X) = GEN(X) for all basic blocks X^ change = 1^ while^ (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^ endfor^ endfor
Class Problem
Compute reaching defs^ Calculate GEN/KILL for each BB r1 = 3^ Calculate IN/OUT for each BB r2 = r3 r3 = r r1 = r1 + 1 r7 = r1 * r2 r4 = r4 + 1 r4 = r3^ r8 = 8^ r9 = r7 + r
DU/UD Chains^ Y^ Convenient way to access/use reaching defs info^ Y^ Def-Use chains^ » Given a def, what are all the possible consumers of theoperand produced^ » Maybe consumer^ Y^ Use-Def chains^ » Given a use, what are all the possible producers of theoperand consumed^ » Maybe producer