Algorithm Search⌘K

Techniqueadvanced

Functional approach to interprocedural analysis

Also called procedure summaries, Sharir-Pnueli functional approach.

Computes for each procedure a summary function from input facts to output facts and applies it at each call site.

The functional approach of Sharir and Pnueli (1981) analyzes a procedure once, as a function. For each procedure it computes a summary: a function that maps the fact at the entry of the procedure to the fact at its exit, over all ways through the body, including the calls it makes. At each call site, the analysis applies the summary of the callee to the fact before the call. A fact that comes into a procedure from one caller then goes back to that caller only, so the analysis follows only valid paths, at any depth of calls and with recursion. For distributive frameworks it computes exactly the meet over valid paths (MVP) of interprocedural dataflow analysis. The IFDS and IDE frameworks are efficient forms of it for special kinds of summaries.

In plain words

When you read a program by hand, you do not reread a helper function every time it is called. You read it once, write down what it does ("adds one to its argument", "sets the flag"), and use that note at every call. The functional approach does the same. First it works out a note for each procedure that says, for any fact that holds when the procedure starts, what holds when it returns. A procedure that calls itself needs a few rounds until its note stops changing. Then it starts at the main procedure and uses the notes to carry facts across every call. Because each call gets its own answer from the note, facts from one caller never leak to another.

When it applies

  • The transfer functions have a representation that is closed under composition and pointwise meet, and that has a cheap equality test. Gen-kill pairs, distributive functions on subsets of a finite set (as relations), and the linear functions of IDE all qualify.
  • The space of these functions has finite height, so the iteration of phase 1 stops. For gen-kill functions over uu facts the height is O(u)O(u).
  • For an exact answer, every function is distributive. For monotone functions, the result is still sound, but it can be below MVP.
  • If no compact representation exists but the lattice is finite, the tabulated variant computes each summary only at the input values that actually reach the procedure (see Variations).

The template

Use the meet convention of Kildall: ⊤\top is the start value, the result is the greatest solution, and lower values are less precise. (The join convention of Cousot flips the order.) The supergraph of the interprocedural dataflow analysis page gives, for each call site ss in a procedure that calls qq, a call point csc_s, a return point rsr_s, a function calls\mathit{call}_s on the call edge, a function hsh_s on the return edge, and a function lsl_s on the call-to-return edge, which carries the caller's local facts around the call. Edges inside a procedure carry functions fef_e. The holes are REPR, a representation of functions with the operations COMPOSE (∘\circ), MEET (⊓\sqcap) and APPLY, and the order of the worklist.

  1. SUMMARIES(program, ι)
  2. 1for each point n of each procedure p do φ[n] ← λx.⊤ ▷ phase 1: summaries
  3. 2for each procedure p do φ[entry_p] ← id; add entry_p to W
  4. 3while W is not empty do
  5. 4remove a point m from W
  6. 5for each edge (m, n) inside a procedure, with function f do UPDATE(n, f ∘ φ[m])
  7. 6if m = c_s for a call site s that calls q then
  8. 7UPDATE(r_s, (h_s ∘ φ[exit_q] ∘ call_s ∘ φ[c_s]) ⊓ (l_s ∘ φ[c_s]))
  9. 8if m = exit_q then add c_s to W for every call site s of q
  10. 9for each procedure p do X[entry_p] ← ⊤ ▷ phase 2: entry values
  11. 10X[entry_main] ← ι; add main to W
  12. 11while W is not empty do
  13. 12remove a procedure p from W
  14. 13for each call site s in p, calling q do
  15. 14v ← call_s(φ[c_s](X[entry_p]))
  16. 15if X[entry_q] ⊓ v ≠ X[entry_q] then X[entry_q] ← X[entry_q] ⊓ v; add q to W
  17. 16for each point n of each procedure p do X[n] ← φ[n](X[entry_p])
  18. 17return X

UPDATE(n,g)(n, g) sets φ[n]←φ[n]⊓g\varphi[n] \leftarrow \varphi[n] \sqcap g and adds nn to WW if this changes φ[n]\varphi[n]. After phase 1, φ[n]\varphi[n] maps the fact at the entry of the procedure of nn to the fact at nn, and φ[exitq]\varphi[\mathit{exit}_q] is the summary of qq. Line L8 handles recursion and calls to procedures whose summary is not final yet: when a summary drops, every call site that used it is looked at again.

Correctness. Call a path same-level valid if its call and return edges match as brackets, as on the interprocedural dataflow analysis page. A path may also take a call-to-return edge. Let SL(n)=⊓{fπ:π\mathrm{SL}(n) = \mathop{\large\sqcap} \{ f_\pi : \pi a same-level valid path from entryp\mathit{entry}_p to n}n \}, where fπf_\pi is the composition of the functions along π\pi.

Lemma 1 (phase 1). If all functions are monotone, φ[n]⊑SL(n)\varphi[n] \sqsubseteq \mathrm{SL}(n). If they are also distributive, φ[n]=SL(n)\varphi[n] = \mathrm{SL}(n).

Proof. Phase 1 is a chaotic iteration from ⊤\top on the inequations φ[entryp]⊑id\varphi[\mathit{entry}_p] \sqsubseteq \mathrm{id}, φ[n]⊑f∘φ[m]\varphi[n] \sqsubseteq f \circ \varphi[m] and the inequation of line L7. In a lattice of finite height it stops at the greatest solution, as in the Kleene fixed-point theorem. For the first part, show φ[n]⊑fπ\varphi[n] \sqsubseteq f_\pi by induction on the length of π\pi. If π\pi ends with an edge inside a procedure, use the inequation of L5 and monotonicity. If π=π′⋅(cs→entryq)⋅ρ⋅(exitq→rs)\pi = \pi' \cdot (c_s \to \mathit{entry}_q) \cdot \rho \cdot (\mathit{exit}_q \to r_s) with ρ\rho same-level valid in qq, the induction gives φ[cs]⊑fπ′\varphi[c_s] \sqsubseteq f_{\pi'} and φ[exitq]⊑fρ\varphi[\mathit{exit}_q] \sqsubseteq f_\rho, and the inequation of L7 gives φ[rs]⊑hs∘fρ∘calls∘fπ′=fπ\varphi[r_s] \sqsubseteq h_s \circ f_\rho \circ \mathit{call}_s \circ f_{\pi'} = f_\pi. The call-to-return edge is the same. For the second part, show that SL is a solution. Distributive functions commute with meets, and in a lattice of finite height every meet is a finite meet. So f∘SL(m)f \circ \mathrm{SL}(m) is the meet of f∘fπf \circ f_\pi over the paths π\pi to mm, and each f∘fπf \circ f_\pi is the function of a same-level valid path to nn. The right side of L7 expands in the same way into a meet of functions of same-level valid paths to rsr_s. So SL is below every right side, and SL⊑φ\mathrm{SL} \sqsubseteq \varphi because φ\varphi is the greatest solution. □\square

Lemma 2 (phase 2). A valid path to a point nn of pp splits at its last unmatched call edge into a valid path to entryp\mathit{entry}_p and a same-level valid path from entryp\mathit{entry}_p to nn. So MVP(n)=SL(n)(MVP(entryp))\mathrm{MVP}(n) = \mathrm{SL}(n)(\mathrm{MVP}(\mathit{entry}_p)) when the functions are distributive, and the values MVP(entryq)\mathrm{MVP}(\mathit{entry}_q) form the greatest solution of the inequations of lines L10 to L15. The proof is the same as for Lemma 1, with values in place of functions. □\square

Theorem (interprocedural coincidence; Sharir and Pnueli 1981, Knoop and Steffen 1992). If every function is distributive and the function space has finite height, SUMMARIES stops and returns X=MVPX = \mathrm{MVP}. If the functions are only monotone, X⊑MVPX \sqsubseteq \mathrm{MVP}, so the result is sound.

Cost. Let HH be the height of the space of functions, EE the number of edges of the supergraph, and CC the number of call sites. Each φ[n]\varphi[n] drops at most HH times, and each drop costs work in proportion to the out-edges of nn, plus the call sites of qq when n=exitqn = \mathit{exit}_q. Over all procedures, the call sites revisited at line L8 add up to HCH C, so phase 1 takes O(H(E+C))O(H (E + C)) function operations. Phase 2 does the same with the height of LL. For gen-kill problems over uu facts, H=O(u)H = O(u) and each operation costs O(u/w)O(u / w) word operations.

Walk-through: reaching definitions with recursion

The definitions d1d_1 and d2d_2 both assign the global g. A gen-kill function is a pair (gen, kill) with f(x)=(x∖kill)∪genf(x) = (x \setminus \mathit{kill}) \cup \mathit{gen}. The meet is ∪\cup on sets and (g1∪g2,k1∩k2)(g_1 \cup g_2, k_1 \cap k_2) on pairs, and (g2,k2)∘(g1,k1)=(g2∪(g1∖k2),k1∪k2)(g_2, k_2) \circ (g_1, k_1) = (g_2 \cup (g_1 \setminus k_2), k_1 \cup k_2). Since g is global, calls\mathit{call}_s and hsh_s are the identity, and ls=λx.⊤=(∅,all)l_s = \lambda x.\top = (\emptyset, \text{all}), because no fact is about a local.

main:  d1: g = 0
       s1: call p()
p:     if c then
           d2: g = 1
           s2: call p()
       return

Phase 1 for p. After d2, φ=({d2},{d1})\varphi = (\{d_2\}, \{d_1\}), and so at cs2c_{s2}. The exit of p is the meet of the else branch, id=(∅,∅)\mathrm{id} = (\emptyset, \emptyset), and φ[rs2]\varphi[r_{s2}].

round φ[rs2]=φ[exitp]∘φ[cs2]\varphi[r_{s2}] = \varphi[\mathit{exit}_p] \circ \varphi[c_{s2}] φ[exitp]=id⊓φ[rs2]\varphi[\mathit{exit}_p] = \mathrm{id} \sqcap \varphi[r_{s2}]
0 ⊤=(∅,all)\top = (\emptyset, \text{all}) (∅,∅)(\emptyset, \emptyset)
1 (∅,∅)∘({d2},{d1})=({d2},{d1})(\emptyset, \emptyset) \circ (\{d_2\}, \{d_1\}) = (\{d_2\}, \{d_1\}) ({d2},∅)(\{d_2\}, \emptyset)
2 ({d2},∅)∘({d2},{d1})=({d2},{d1})(\{d_2\}, \emptyset) \circ (\{d_2\}, \{d_1\}) = (\{d_2\}, \{d_1\}) ({d2},∅)(\{d_2\}, \emptyset), stable

The summary of p is x↦x∪{d2}x \mapsto x \cup \{d_2\}: after a call of p, d2d_2 may reach, and nothing is killed for sure, because the call may take the else branch. Phase 2 starts with X[entrymain]=∅X[\mathit{entry}_{main}] = \emptyset. The call s1 gives φ[cs1](∅)={d1}\varphi[c_{s1}](\emptyset) = \{d_1\} at the entry of p. The call s2 then gives φ[cs2]({d1})={d2}\varphi[c_{s2}](\{d_1\}) = \{d_2\}, so X[entryp]={d1,d2}X[\mathit{entry}_p] = \{d_1, d_2\}, and one more round changes nothing. After s1 in main, the summary gives {d1}∪{d2}={d1,d2}\{d_1\} \cup \{d_2\} = \{d_1, d_2\}. Each recursion depth is a different valid path, and the summary covers all of them at once.

Classic examples

  • Gen-kill problems. Interprocedural reaching definitions, live variables and available expressions over globals and parameters use (gen, kill) pairs as summaries, as in the walk-through.
  • IFDS. A distributive function on subsets of a finite set DD is fixed by what it does to the empty set and to each single fact. It is a bipartite graph on D∪{0}D \cup \{0\}, and composition is the composition of relations. The IFDS framework stores the summaries of phase 1 as path edges and summary edges and finds them by graph reachability.
  • IDE. For environments that map each fact to a value, such as linear constant propagation, the IDE framework labels each edge of the IFDS graph with a function on values, such as λl. al+b\lambda l.\, a l + b.
  • Side effects. A mod-ref summary is the set of locations that a procedure and its callees may modify or read. It is a gen-only summary, so it is a union over the call graph.
  • Jump functions. Interprocedural constant propagation after Callahan, Cooper, Kennedy and Torczon (1986) summarizes how a procedure passes constants on to its callees. These are partial summaries that trade precision for speed.

Variations

  • Tabulated summaries. When functions have no compact form but the lattice is finite, compute φq\varphi_q only at the inputs that reach qq. Keep a table from (procedure, input value) to output value, and fill it on demand from the calls. Sharir and Pnueli describe this variant for finite lattices. Padhye and Khedker (2013) call the table entries value contexts. It works for any monotone framework of finite height, and it is the input-as-context case on the context sensitivity page.
  • Bottom-up order. Compute summaries over the strongly connected components of the call graph, callees first, in reverse topological order. Iterate only inside a component with recursion. This is the usual order of compositional analyzers.
  • Elimination inside a procedure. Phase 1 inside one procedure is a path problem on functions, so elimination methods can compute the summaries without iteration on reducible graphs.
  • Pushdown systems. The program is a pushdown system and the summaries are weights in a semiring. Reps, Schwoon, Jha and Melski (2005) compute them with saturation of a weighted automaton, and get witness paths too.
  • Summaries for libraries. A summary written once for a library procedure can stand in for its code, so the analysis of a client does not need the library source.

Pitfalls

  • The function space is too large. The space L→LL \to L can be huge even for a small LL, and constant propagation over all integers has no finite one. Use a restricted class of functions (gen-kill, IFDS relations, IDE micro-functions) or the tabulated variant.
  • Not distributive means not exact. For full constant propagation, the summary of a procedure with two branches is the meet of two functions, and applying it after the meet loses the relation between values. Lemma 1 still gives soundness, but the result can be below MVP.
  • Locals through the callee. A summary must not route the caller's local facts through the callee, or the facts of one caller leak into another through the callee's body. The call-to-return function lsl_s carries them around the call.
  • Starting summaries too low. Phase 1 must start the summaries at λx.⊤\lambda x.\top, not at the identity. A start below the greatest solution still gives a sound result, but a less precise one, since the iteration only goes down.
  • Forgetting to revisit callers. When the summary of qq drops, every call site of qq must be processed again (line L8). Without this, recursive procedures and calls processed early keep stale summaries and the result is unsound.

Exercises

  1. Prove the composition and meet formulas for gen-kill pairs used in the walk-through, and show that the height of the space of pairs over uu facts is 2u2u.
  2. In the walk-through, replace the else branch of p by d3: g = 2. Compute the summary of p and the facts at the return point of s1.
  3. Give a non-distributive example, with constant propagation, where the functional approach returns ⊥\bot for a variable that is a constant on every valid path.
  4. Show that the tabulated variant for a finite lattice LL computes φq(v)\varphi_q(v) only for inputs vv that reach the entry of qq on some valid path, and bound its work by the number of such pairs.
  5. Show that phase 1 with summaries φ[exitq]\varphi[\mathit{exit}_q] started at the identity can stop at a solution strictly below the greatest one. Use a procedure that only calls itself.

References

  1. Micha Sharir and Amir Pnueli, Two approaches to interprocedural data flow analysis, in Steven S. Muchnick and Neil D. Jones (eds.), Program Flow Analysis: Theory and Applications, Prentice-Hall, 1981, the functional approach (summary functions computed for each procedure, then applied at the calls), its exactness for distributive frameworks, and its variant for finite lattices that tabulates the summaries only at the inputs that occur
  2. Jens Knoop and Bernhard Steffen, The interprocedural coincidence theorem, Compiler Construction (CC '92), Lecture Notes in Computer Science, 125 to 140, 1992, the interprocedural fixed point equals the meet over valid paths when every function is distributive
  3. Thomas Reps, Susan Horwitz and Mooly Sagiv, Precise interprocedural dataflow analysis via graph reachability, Proceedings of the 22nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL '95), 49 to 61, 1995, summaries of distributive subset functions as path edges and summary edges in the exploded supergraph
  4. Mooly Sagiv, Thomas Reps and Susan Horwitz, Precise interprocedural dataflow analysis with applications to constant propagation, Theoretical Computer Science 167(1-2), 131 to 170, 1996, summaries made of environment transformers with a compact representation, applied to linear constant propagation
  5. Thomas Reps, Stefan Schwoon, Somesh Jha and David Melski, Weighted pushdown systems and their application to interprocedural dataflow analysis, Science of Computer Programming 58(1-2), 206 to 263, 2005, summaries as weights in a bounded idempotent semiring, computed by pushdown reachability
  6. Rohan Padhye and Uday P. Khedker, Interprocedural data flow analysis in Soot using value contexts, Proceedings of the 2nd ACM SIGPLAN International Workshop on State of the Art in Java Program Analysis (SOAP '13), 31 to 36, 2013, value contexts, a tabulated functional approach that keys each analysis of a procedure by its input value

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)
WritingW3 standard
  • five exercises