Algorithm Search⌘K

Theoremresearch

NP-hardness of precise flow-insensitive alias analysis

Also called Horwitz alias hardness.

Deciding precise flow-insensitive may-alias is NP-hard, even with no dynamic memory, when statements may dereference a pointer many times.

Precise flow-insensitive may-alias analysis is NP-hard. A flow-insensitive analysis may run the statements of a program in any order, any number of times, and the precise question asks whether some such run makes two expressions name the same location. Horwitz proved in 1997 that this question is NP-hard even when the program has no dynamic memory. So the fast flow-insensitive analyses, Andersen's in cubic time and Steensgaard's in almost linear time, cannot be precise for this semantics unless P = NP. The proof needs statements that dereference a pointer many times. This page proves the theorem by its own short reduction from the Hamiltonian path problem, not by Horwitz's construction.

Statement

Programs. A program is a finite set of statements over a finite set VV of variables. There are no procedures and no dynamic allocation. At each moment a variable holds nothing or the address of one variable. Write ∗kp*^k p for pp with k≥0k \ge 0 stars in front. A statement has the form ∗ip=&a*^i p = \&a or ∗ip=∗jq*^i p = *^j q with i,j≥0i, j \ge 0. In a state σ\sigma, the expression ∗0p=p*^0 p = p names the location pp, and ∗k+1p*^{k+1} p names the location σ(ℓ)\sigma(\ell) held by the location ℓ\ell that ∗kp*^k p names. If some location on the way holds nothing, the expression names nothing. The statement ∗ip=e*^i p = e writes the value of ee into the location that ∗ip*^i p names. Here the value of &a\&a is aa, and the value of ∗jq*^j q is the content of the location that ∗jq*^j q names.

Flow-insensitive semantics. A run starts in the state where no variable holds anything, and it executes any finite sequence of the statements, with repeats. A statement whose expressions name nothing, or whose right side has no value, does not run. Every path of a real control flow graph over these statements is such a sequence, so this semantics covers every order of the statements. It ignores the order; it does not ignore which statements the program has.

The problem. Precise flow-insensitive may-alias: given a program and two expressions e1e_1 and e2e_2, is there a run after which e1e_1 and e2e_2 name the same location? The points-to form asks whether pp may hold the address of aa, which is the alias question for ∗p*p and aa.

Theorem (Horwitz 1997). Precise flow-insensitive may-alias is NP-hard. It is NP-hard even for programs where every statement but one has the form u=&vu = \&v, the remaining statement has the form r=∗ksr = *^k s, and the question is whether rr may point to a given variable tt.

In plain words

Pretend that the statements of a program can run in any order, as often as you like. Can two pointer expressions ever end up naming the same memory cell? With no memory allocation, the number of possible states is finite, so this question can be answered by a search. But the search can take a very long time. The theorem says that the question is as hard as the hardest search problems in NP, such as finding a route that visits every city once. The proof turns a route question into a program. Each road becomes a statement that makes one city point to the next. One long statement follows the pointers from the start city many times. It reaches the end city only when the pointers form a route through all the cities.

Proof

The source problem. The directed Hamiltonian path problem with fixed ends asks, for a directed graph G=(N,E)G = (N, E) and distinct nodes ss and tt, whether GG has a path from ss to tt that visits every node exactly once. It is NP-complete (Garey and Johnson, problems GT37 to GT39). For example, the directed Hamiltonian circuit problem reduces to it: pick a node vv, split it into a node voutv_{out} with the out-edges of vv and a node vinv_{in} with the in-edges of vv, and ask for a path from voutv_{out} to vinv_{in}.

The construction. Let n=∣N∣≥2n = |N| \ge 2. Make one variable for each node, and one more variable rr. The program has the statements

  • u=&vu = \&v for each edge (u,v)∈E(u, v) \in E with u≠tu \ne t, and
  • r=∗n−2sr = *^{n-2} s.

The question is whether rr may point to tt. The program has ∣E∣+1|E| + 1 statements and size O(∣E∣+n)O(|E| + n), so it is built in polynomial time.

In the state σ\sigma where the last statement runs, put y0=sy_0 = s and yk+1=σ(yk)y_{k+1} = \sigma(y_k). Then ∗ks*^k s names yky_k, so the value of ∗n−2s*^{n-2} s is yn−1y_{n-1}, and the statement gives rr the value yn−1y_{n-1}.

Claim. Some run makes rr point to tt if and only if GG has a Hamiltonian path from ss to tt.

If. Let s=x0,x1,…,xn−1=ts = x_0, x_1, \ldots, x_{n-1} = t be a Hamiltonian path. For i<n−1i < n - 1 the node xix_i is not tt, so the program has the statement xi=&xi+1x_i = \&x_{i+1}. Run these n−1n - 1 statements, and then r=∗n−2sr = *^{n-2} s. In that state yk=xky_k = x_k for every k≤n−1k \le n - 1, so rr gets the value xn−1=tx_{n-1} = t.

Only if. The variable rr gets a value only from the last statement, and no statement takes the address of rr. So in some run, the last statement runs in a state σ\sigma with yn−1=ty_{n-1} = t, where y0,…,yn−1y_0, \ldots, y_{n-1} are all defined, and y1,…,yn−1y_1, \ldots, y_{n-1} are node variables. Each value σ(u)=v\sigma(u) = v of a node variable comes from a statement u=&vu = \&v, so (yk,yk+1)∈E(y_k, y_{k+1}) \in E for every k<n−1k < n - 1.

The nodes y0,…,yn−1y_0, \ldots, y_{n-1} are distinct. Suppose yi=yjy_i = y_j with i<j≤n−1i < j \le n - 1, and put d=j−id = j - i. Since σ\sigma is a function, yk+d=yky_{k+d} = y_k for every k≥ik \ge i for which both are defined. Then t=yn−1=yn−1−dt = y_{n-1} = y_{n-1-d}, and i≤n−1−d<n−1i \le n - 1 - d < n - 1, so σ(t)=yn−d\sigma(t) = y_{n-d} is defined. But no statement gives tt a value, because the statements of the out-edges of tt were left out. This is a contradiction.

So y0,…,yn−1y_0, \ldots, y_{n-1} are the nn nodes in some order, joined by edges, from ss to tt: a Hamiltonian path. □\square

The claim gives a polynomial-time reduction from an NP-complete problem, so precise flow-insensitive may-alias is NP-hard. The reduction makes only statements of the two forms in the theorem, and the question is a points-to question. □\square

An upper bound. The problem is in PSPACE. A state maps each of the nn variables to one of n+1n + 1 values, so there are at most (n+1)n(n+1)^n states, and a state fits in O(nlog⁡n)O(n \log n) bits. A nondeterministic search guesses the run one statement at a time, keeps only the current state, and checks the question at each step. By Savitch's theorem, nondeterministic polynomial space is the same as deterministic polynomial space. The theorem gives only the lower bound.

Consequences

Fast analyses are not precise. Andersen's analysis takes O(n3)O(n^3) time and Steensgaard's analysis takes almost linear time. Both are sound for the flow-insensitive semantics, so unless P = NP, neither gives the precise answer on all programs. The loss is already visible with one dereference per statement. Take the program

s = &s
s = &t
s = &a
r = *s

Andersen's analysis gives pts(s)={s,t,a}pts(s) = \{s, t, a\} and pts(r)=pts(s)∪pts(t)∪pts(a)={s,t,a}pts(r) = pts(s) \cup pts(t) \cup pts(a) = \{s, t, a\}, so it says that rr may point to tt. No run does that. When r=∗sr = *s runs, ss holds one value. If it is ss, then ∗s*s is the value of ss, which is ss. If it is tt or aa, then ∗s*s has no value, because tt and aa never get one. So rr can only point to ss. The analysis reads "ss points to ss" and "ss points to tt" as two independent facts, but a run sees one of them at a time.

Splitting statements changes the problem. A compiler usually breaks r=∗∗sr = **s into u=∗su = *s and r=∗ur = *u with a new variable uu. Under the flow-insensitive semantics the two new statements may run at different moments, with other statements between them, so the split program has more runs. Its precise answer can be larger than the precise answer of the original. Andersen's analysis is the inclusion analysis of the split form, which is one reason why it runs in polynomial time.

Flow sensitivity costs more. Without dynamic allocation, precise flow-sensitive may-alias is PSPACE-complete (Landi 1992 and Muth and Debray 2000, as cited by Rinetzky et al. 2008; Chakaravarthy 2003 states it too). With dynamic allocation it is undecidable. So flow-insensitive analysis is easier in theory too, but even the precise flow-insensitive question needs approximation in practice.

Tightness

Many dereferences in one statement. The reduction uses one statement with n−2n - 2 stars. Rinetzky et al. (2008) point out that Horwitz's proof also uses statements with an unbounded number of memory accesses, and they study the complexity of the precise question under other limits. The theorem on this page says nothing about programs whose statements have at most one star on each side, which is the input of Andersen's analysis.

Types. The program of the reduction lets a variable point to a variable that points back, so its pointers have no fixed level of indirection. Chakaravarthy (2003) showed that the precise flow-insensitive question becomes polynomial when the variables have well-defined types, and that it stays NP-hard for programs with scalar variables only and no dynamic memory.

Dynamic memory and order. Adding the order of the statements, or adding dynamic allocation to a flow-sensitive analysis, makes the problem harder, as the consequences above say. Dropping the precision requirement makes it easy: Andersen's least solution is a sound answer in cubic time.

History

Landi (1992) and Ramalingam (1994) showed that precise flow-sensitive may-alias is undecidable once a program can build recursive data structures, which focused attention on approximations. Flow-insensitive analyses such as Andersen's (1994) and Steensgaard's (1996) were the practical answer, and Horwitz (1997) showed that even their semantics has no fast precise solution unless P = NP. Chakaravarthy (2003) extended the hardness results and found a polynomial case for well-typed programs, and Rinetzky, Ramalingam, Sagiv and Yahav (2008) studied the space between flow-insensitive and flow-sensitive analysis.

References

  1. Susan Horwitz, Precise flow-insensitive may-alias analysis is NP-hard, ACM Transactions on Programming Languages and Systems 19(1), 1 to 6 (1997), whole paper (the NP-hardness proof for programs with arbitrary levels of pointers and arbitrary pointer dereferencing)
  2. Venkatesan T. Chakaravarthy, New results on the computability and complexity of points-to analysis, Proceedings of the 30th ACM Symposium on Principles of Programming Languages (POPL 2003), 115 to 125, the results on flow-insensitive analysis (NP-hard with scalar variables and no dynamic memory, polynomial for programs with well-defined types)
  3. Noam Rinetzky, G. Ramalingam, Mooly Sagiv and Eran Yahav, On the complexity of partially-flow-sensitive alias analysis, ACM Transactions on Programming Languages and Systems 30(3), Article 13 (2008), the introduction and the discussion of earlier results (the number of memory accesses per statement in Horwitz's proof, and the PSPACE-completeness of flow-sensitive may-alias without dynamic allocation)
  4. Michael R. Garey and David S. Johnson, Computers and Intractability, A Guide to the Theory of NP-Completeness (1979), the Hamiltonian circuit and Hamiltonian path problems in the graph theory part of the appendix (GT37 to GT39)

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/XYGBP7/text.yaml