Algorithm Search⌘K

Definitioncore

Gen-kill dataflow problem

Also called bit-vector dataflow problem, gen-kill framework.

A dataflow problem whose transfer functions have the form f(x) = gen union (x minus kill), solved fast with bit vectors.

A gen-kill dataflow problem is a dataflow analysis whose facts are sets of items from a finite universe and whose transfer functions all have the form f(X)=gen∪(X−kill)f(X) = gen \cup (X - kill). The four classical problems, reaching definitions, live variable analysis, available expressions and very busy expressions, are of this form. The functions are closed under composition, so a whole block has one pair (gen,kill)(gen, kill). They distribute over both union and intersection, so every gen-kill problem is a distributive dataflow framework and its computed answer equals the meet-over-all-paths solution. Each item is independent of the others, so a set fits in a bit vector and one transfer function costs a few word operations per ww items.

Definition

The dataflow pages use the meet convention: (L,⊓)(L, \sqcap) is a meet semilattice, x⊑yx \sqsubseteq y means x⊓y=xx \sqcap y = x, and ⊤\top is the identity of meet and the start value.

Definition. Let UU be a finite set of items, with u=∣U∣u = |U|. A gen-kill problem over UU is a dataflow problem with:

  • the domain $L = $ all subsets of UU;
  • the meet ⊓=∪\sqcap = \cup (a may problem: ⊤=∅\top = \emptyset and the order is ⊇\supseteq) or ⊓=∩\sqcap = \cap (a must problem: ⊤=U\top = U and the order is ⊆\subseteq);
  • a direction, forward or backward, and a boundary value ι⊆U\iota \subseteq U;
  • for each block BB two sets genB,killB⊆Ugen_B, kill_B \subseteq U and the transfer function fB(X)=genB∪(X−killB)f_B(X) = gen_B \cup (X - kill_B).

The height of LL is h=uh = u in both cases: a strict chain adds or removes one item per step.

problem items UU direction meet gengen of x = a op b killkill of x = a op b boundary ι\iota
reaching definitions definitions forward ∪\cup this definition the other definitions of x ∅\emptyset at the entry
live variables variables backward ∪\cup a, b x ∅\emptyset after ret
available expressions expressions forward ∩\cap a op b, unless x is a or b expressions that read x ∅\emptyset at the entry
very busy expressions expressions backward ∩\cap a op b expressions that read x ∅\emptyset after ret

In the two must problems ι=∅=⊥\iota = \emptyset = \bot, which is far from ⊤=U\top = U. In the may problems the usual ι\iota is ∅=⊤\emptyset = \top, but variants change it, such as the pseudo definitions at the entry for reaching definitions.

Composition. Applying f1=(g1,k1)f_1 = (g_1, k_1) and then f2=(g2,k2)f_2 = (g_2, k_2) gives

f2(f1(X))=g2∪((g1∪(X−k1))−k2)=(g2∪(g1−k2))∪(X−(k1∪k2)).f_2(f_1(X)) = g_2 \cup \big((g_1 \cup (X - k_1)) - k_2\big) = \big(g_2 \cup (g_1 - k_2)\big) \cup \big(X - (k_1 \cup k_2)\big).

So the composition is the gen-kill function with gen=g2∪(g1−k2)gen = g_2 \cup (g_1 - k_2) and kill=k1∪k2kill = k_1 \cup k_2. The identity is (∅,∅)(\emptyset, \emptyset). The functions therefore form a set that contains the identity and is closed under composition, as a monotone dataflow framework requires. A block's pair is the composition of its instructions in the direction of the analysis: first to last for a forward problem, last to first for a backward one.

Distributivity. For all X,Y⊆UX, Y \subseteq U:

f(X∪Y)=g∪((X−k)∪(Y−k))=f(X)∪f(Y),f(X \cup Y) = g \cup \big((X - k) \cup (Y - k)\big) = f(X) \cup f(Y),
f(X∩Y)=g∪((X−k)∩(Y−k))=(g∪(X−k))∩(g∪(Y−k))=f(X)∩f(Y),f(X \cap Y) = g \cup \big((X - k) \cap (Y - k)\big) = \big(g \cup (X - k)\big) \cap \big(g \cup (Y - k)\big) = f(X) \cap f(Y),

where the first lines use (X∪Y)−k=(X−k)∪(Y−k)(X \cup Y) - k = (X - k) \cup (Y - k) and (X∩Y)−k=(X−k)∩(Y−k)(X \cap Y) - k = (X - k) \cap (Y - k), and the last step of the second line is the law g∪(A∩B)=(g∪A)∩(g∪B)g \cup (A \cap B) = (g \cup A) \cap (g \cup B). So every gen-kill function distributes over the meet of a may problem and of a must problem alike. A distributive function is monotone, so a gen-kill problem is a distributive framework, and when every block is reachable from the start, the computed fixed point equals MOP. □\square

Which set functions are gen-kill. Call ff on the subsets of UU bitwise if, for each item dd, whether d∈f(X)d \in f(X) depends only on whether d∈Xd \in X. Then ff is a gen-kill function if and only if it is bitwise and monotone.

Proof. A gen-kill function is bitwise: d∈f(X)d \in f(X) holds if d∈gd \in g, fails if d∈k−gd \in k - g, and otherwise holds exactly when d∈Xd \in X. It is monotone because it distributes. Conversely, let ff be bitwise and monotone. For each dd, the map from "d∈Xd \in X" to "d∈f(X)d \in f(X)" is a monotone function from {0,1}\{0, 1\} to {0,1}\{0, 1\}, so it is the constant 0, the constant 1 or the identity; the fourth function, negation, is not monotone. Put dd in gg if it is the constant 1 and in kk if it is the constant 0. Then f(X)=g∪(X−k)f(X) = g \cup (X - k). □\square

So the gen-kill problems are exactly the dataflow problems on sets in which items do not interact. This is what lets each item be solved on its own, as one graph reachability question.

Soundness and precision. Each of the four problems is sound by the argument on its page: every run follows a CFG path, and the fixed point is below the fact of every path. Since MFP = MOP, the only loss of precision is from CFG paths that no run takes.

Cost. Store each set as a vector of uu bits. With word size ww, the operations X∪YX \cup Y, X∩YX \cap Y and g∪(X−k)g \cup (X - k), the last as gen | (x & ~kill), cost O(u/w)O(u / w) word operations each. On a CFG with nn blocks and mm edges, the worklist dataflow algorithm changes each block value at most uu times, so it applies at most n(u+1)n(u + 1) transfer functions and m(u+1)m(u + 1) meets, for O((n+m)u2/w)O((n + m) u^2 / w) time. Gen-kill problems are also rapid, and round-robin iteration in reverse postorder stops after at most d+2d + 2 passes, where dd is the loop-connectedness depth of the CFG. This gives O((d+2)(n+m)u/w)O((d + 2)(n + m) u / w) time; see the Kam-Ullman iteration bound. Space is O(nu/w)O(n u / w) words for the IN and OUT vectors and the gengen and killkill vectors.

Examples and non-examples

Composing a block. In the running example of dataflow analysis, block body is

body:
  a = x * 2
  u = i & 1
  br u odd next

For available expressions, a = x * 2 has gen={x∗2}gen = \{x * 2\} and kill={s+a}kill = \{s + a\}, since s + a reads a. Then u = i & 1 has gen={i & 1}gen = \{i \,\&\, 1\} and kill=∅kill = \emptyset. The composition is gen={i & 1}∪({x∗2}−∅)={x∗2,i & 1}gen = \{i \,\&\, 1\} \cup (\{x * 2\} - \emptyset) = \{x * 2, i \,\&\, 1\} and kill={s+a}kill = \{s + a\}. For live variables the same block is read from the end: the branch reads u, then u = i & 1 writes u and reads i, then a = x * 2 writes a and reads x. The composition is gen={i,x}gen = \{i, x\} and kill={a,u}kill = \{a, u\}; u is not in gengen because the block writes it before the branch reads it.

An instruction that reads what it writes. For available expressions, s = s + a computes s + a and then changes s, so gen={s+a}−kills=∅gen = \{s + a\} - kill_s = \emptyset. For very busy expressions, read backward, the same instruction has gen={s+a}gen = \{s + a\}: the evaluation comes before the write.

Reaching definitions with pseudo definitions. Adding one pseudo definition per variable at the entry keeps the problem gen-kill; only UU and ι\iota change.

Non-example: strongly live variables. The function of x = y + z, f(X)=(X−{x})∪({y,z} if x∈X)f(X) = (X - \{x\}) \cup (\{y, z\} \text{ if } x \in X), is distributive, but whether y∈f(X)y \in f(X) depends on whether x∈Xx \in X. It is not bitwise, so it is not gen-kill, and the items cannot be solved one at a time.

Non-example: points-to sets. In a may points-to analysis the fact is a set of pairs "pp may point to oo". The instruction q = *p adds pairs for q that depend on which pairs of p are in the input. The function is not bitwise.

Non-example: constant propagation. The facts are maps from variables to a flat lattice, not sets, and the function of z = x + y is not even distributive. See constant propagation.

References

  1. Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman, Compilers, Principles, Techniques, and Tools, second edition, Pearson Addison-Wesley, 2007, Section 9.2 (9.2.4 Reaching Definitions with the composition of gen and kill sets, 9.2.5 Live-Variable Analysis, 9.2.6 Available Expressions), Section 9.3.2 (Transfer Functions) and Section 9.6.7 (Speed of Convergence of Iterative Data-Flow Algorithms)
  2. Flemming Nielson, Hanne Riis Nielson and Chris Hankin, Principles of Program Analysis, Springer, 1999, Section 2.1 (the four classical analyses, each given by kill and gen functions) and Section 2.3 (Monotone Frameworks)
  3. John B. Kam and Jeffrey D. Ullman, Global data flow analysis and iterative algorithms, Journal of the ACM 23(1), 1976, 158-171, the whole paper (the analysis of round-robin iteration in depth-first order, which covers the bit-vector problems)
  4. Gary A. Kildall, A unified approach to global program optimization, Proceedings of the 1st ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages (POPL 1973), 194-206, the whole paper (distributive optimizing functions, of which gen-kill functions are the main example)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/M3TQGG/text.yaml