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 facts the height is .
- 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: 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 in a procedure that calls , a call point , a return point , a function on the call edge, a function on the return edge, and a function on the call-to-return edge, which carries the caller's local facts around the call. Edges inside a procedure carry functions . The holes are REPR, a representation of functions with the operations COMPOSE (), MEET () and APPLY, and the order of the worklist.
SUMMARIES(program, ι)- 1
for each point n of each procedure p do φ[n] ← λx.⊤ ▷ phase 1: summaries - 2
for each procedure p do φ[entry_p] ← id; add entry_p to W - 3
while W is not empty do - 4
remove a point m from W - 5
for each edge (m, n) inside a procedure, with function f do UPDATE(n, f ∘ φ[m]) - 6
if m = c_s for a call site s that calls q then - 7
UPDATE(r_s, (h_s ∘ φ[exit_q] ∘ call_s ∘ φ[c_s]) ⊓ (l_s ∘ φ[c_s])) - 8
if m = exit_q then add c_s to W for every call site s of q - 9
for each procedure p do X[entry_p] ← ⊤ ▷ phase 2: entry values - 10
X[entry_main] ← ι; add main to W - 11
while W is not empty do - 12
remove a procedure p from W - 13
for each call site s in p, calling q do - 14
v ← call_s(φ[c_s](X[entry_p])) - 15
if X[entry_q] ⊓ v ≠ X[entry_q] then X[entry_q] ← X[entry_q] ⊓ v; add q to W - 16
for each point n of each procedure p do X[n] ← φ[n](X[entry_p]) - 17
return X
UPDATE sets and adds to if this changes . After phase 1, maps the fact at the entry of the procedure of to the fact at , and is the summary of . 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 a same-level valid path from to , where is the composition of the functions along .
Lemma 1 (phase 1). If all functions are monotone, . If they are also distributive, .
Proof. Phase 1 is a chaotic iteration from on the inequations , 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 by induction on the length of . If ends with an edge inside a procedure, use the inequation of L5 and monotonicity. If with same-level valid in , the induction gives and , and the inequation of L7 gives . 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 is the meet of over the paths to , and each is the function of a same-level valid path to . The right side of L7 expands in the same way into a meet of functions of same-level valid paths to . So SL is below every right side, and because is the greatest solution.
Lemma 2 (phase 2). A valid path to a point of splits at its last unmatched call edge into a valid path to and a same-level valid path from to . So when the functions are distributive, and the values 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.
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 . If the functions are only monotone, , so the result is sound.
Cost. Let be the height of the space of functions, the number of edges of the supergraph, and the number of call sites. Each drops at most times, and each drop costs work in proportion to the out-edges of , plus the call sites of when . Over all procedures, the call sites revisited at line L8 add up to , so phase 1 takes function operations. Phase 2 does the same with the height of . For gen-kill problems over facts, and each operation costs word operations.
Walk-through: reaching definitions with recursion
The definitions and both assign the global g. A gen-kill function is a pair (gen, kill) with . The meet is on sets and on pairs, and . Since g is global, and are the identity, and , 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, , and so at . The exit of p is the meet of the else branch, , and .
| round | ||
|---|---|---|
| 0 | ||
| 1 | ||
| 2 | , stable |
The summary of p is : after a call of p, may reach, and nothing is killed for sure, because the call may take the else branch. Phase 2 starts with . The call s1 gives at the entry of p. The call s2 then gives , so , and one more round changes nothing. After s1 in main, the summary gives . 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 is fixed by what it does to the empty set and to each single fact. It is a bipartite graph on , 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 .
- 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 only at the inputs that reach . 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 can be huge even for a small , 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 carries them around the call.
- Starting summaries too low. Phase 1 must start the summaries at , 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 drops, every call site of must be processed again (line L8). Without this, recursive procedures and calls processed early keep stale summaries and the result is unsound.
Exercises
- 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 facts is .
- In the walk-through, replace the else branch of
pbyd3: g = 2. Compute the summary ofpand the facts at the return point ofs1. - Give a non-distributive example, with constant propagation, where the functional approach returns for a variable that is a constant on every valid path.
- Show that the tabulated variant for a finite lattice computes only for inputs that reach the entry of on some valid path, and bound its work by the number of such pairs.
- Show that phase 1 with summaries started at the identity can stop at a solution strictly below the greatest one. Use a procedure that only calls itself.
References
- 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
- 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
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag KZA2TR Edit this page History Report an error Markdown