Docsity

Dataflow Analysis and Optimization in Computer Science: Liveness Analysis Example - Prof. , Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Dataflow Analysis and Optimization in Computer Science: Liveness Analysis Example - Prof. and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

EECS 583 – Lecture 7 Dataflow Analysis

University of Michigan January 29, 2003

  • 1 - 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 the computationmore efficient » Get rid of redundancy » Simplify Y Ex: Common SubexpressionElimination (CSE) » Is r2 + r3 redundant? » Is r4 – r5 redundant? » What if there were 1000 BB’s » Dataflow analysis !! r1 = r2 + r3r6 = r4 – r r4 = 4r6 = 8 r6 = r2 + r3r7 = r4 – r
  • 3 - 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

USE = set of external variables consumed in the BB

DEF = set of variables defined in 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

  • 4 - 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 = 4 r6 = 8 r4 is dead, as it is redefined. So is r6. 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
  • 6 - Example USE/DEF Calculation r1 = MEM[r2+0] r2 = r2 + 1 r3 = r1 * r r1 = r1 + 5 r3 = r5 – r1^ r7 = r3 * 2 r2 = 0 r7 = 23 r1 = 4 r8 = r7 + 5 r1 = r3 – r8^ r3 = r1 * 2
  • 7 - Compute IN/OUT Sets for all BBs IN = set of variables that are live when the BB is entered OUT = set of variables that are live when the BB is exited 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) = USE(X) + (OUT(X) – DEF(X)) if (old_IN != IN(X)) then change = 1 endif endfor endfor
  • 9 - Class Problem r1 = 3 r2 = r3 r3 = r Compute liveness Calculate USE/DEF for each BB Calculate IN/OUT for each BB r1 = r1 + 1 r7 = r1 * r r2 = 0 r2 = r2 + 1 r4 = r2 + r1 r9 = r4 + r
  • 10 - Reaching Definition Analysis (rdefs) Y

A definition

of a variable x is an operation that assigns, or may

assign, a value to x

Y

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 thatpath

Y

A definition of a variable is killed

between 2 points when there is

another definition of that variable along the path

r1 = r2 + r3 kills previous definitions of r Y

Algorithm sketch

Forward dataflow analysis as propagation occurs from defs downwards Y

4 sets

GEN = set of definitions generated in the BB (ops not vars !!) » KILL = set of definitions killed in the BB » IN = set of definitions reaching the BB entry » OUT = set of definitions reaching the BB exit

  • 12 - Compute GEN/KILL Sets for each BB gen = set of definitions created by an operation kill = set of definitions destroyed by an operation Assume each operation only has 1 destination for simplicity so just keep track of “ops”. Compiler uses “refs” for a more general solution. for 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 endfor endfor
  • 13 - Example GEN/KILL Calculation r1 = MEM[r2+0] r2 = r2 + 1 r3 = r1 * r r1 = r1 + 5 r3 = r5 – r1^ r7 = r3 * 2 r2 = 0 r7 = 23 r1 = 4 r8 = r7 + 5 r1 = r3 – r8^ r3 = r1 * 2
  • 15 - Example IN/OUT Calculation r1 = MEM[r2+0] r2 = r2 + 1 r3 = r1 * r r1 = r1 + 5 r3 = r5 – r1^ r7 = r3 * 2 r2 = 0 r7 = 23 r1 = 4 r8 = r7 + 5 r1 = r3 – r8^ r3 = r1 * 2
  • 16 - Class Problem Reaching definitions Calculate GEN/KILL for each BB Calculate IN/OUT for each BB r1 = 3 r2 = r3 r3 = r r1 = r1 + 1 r7 = r1 * r r2 = 0 r2 = r2 + 1 r4 = r2 + r1 r9 = r4 + r
  • 18 - Generalizing Dataflow Analysis

Y

Transfer function

How information is changed by “something” (BB)

OUT = GEN + (IN – KILL) /* forward analysis */

IN = GEN + (OUT – KILL) /* backward analysis */

Y

Meet function

How information from multiple paths is combined

IN = Union(OUT(predecessors)) /* forward analysis */

OUT = Union(IN(successors)) /* backward analysis */

Y

Generalized dataflow algorithm

while (change)

y change = false y for each BB X apply meet function X apply transfer functions X if any changes Æ change = true

  • 19 - Liveness Using GEN/KILL

Y

Liveness = 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 endfor endfor