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 of variables. There are no procedures and no dynamic allocation. At each moment a variable holds nothing or the address of one variable. Write for with stars in front. A statement has the form or with . In a state , the expression names the location , and names the location held by the location that names. If some location on the way holds nothing, the expression names nothing. The statement writes the value of into the location that names. Here the value of is , and the value of is the content of the location that 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 and , is there a run after which and name the same location? The points-to form asks whether may hold the address of , which is the alias question for and .
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 , the remaining statement has the form , and the question is whether may point to a given variable .
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 and distinct nodes and , whether has a path from to 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 , split it into a node with the out-edges of and a node with the in-edges of , and ask for a path from to .
The construction. Let . Make one variable for each node, and one more variable . The program has the statements
- for each edge with , and
- .
The question is whether may point to . The program has statements and size , so it is built in polynomial time.
In the state where the last statement runs, put and . Then names , so the value of is , and the statement gives the value .
Claim. Some run makes point to if and only if has a Hamiltonian path from to .
If. Let be a Hamiltonian path. For the node is not , so the program has the statement . Run these statements, and then . In that state for every , so gets the value .
Only if. The variable gets a value only from the last statement, and no statement takes the address of . So in some run, the last statement runs in a state with , where are all defined, and are node variables. Each value of a node variable comes from a statement , so for every .
The nodes are distinct. Suppose with , and put . Since is a function, for every for which both are defined. Then , and , so is defined. But no statement gives a value, because the statements of the out-edges of were left out. This is a contradiction.
So are the nodes in some order, joined by edges, from to : a Hamiltonian path.
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.
An upper bound. The problem is in PSPACE. A state maps each of the variables to one of values, so there are at most states, and a state fits in 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 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 and , so it says that may point to . No run does that. When runs, holds one value. If it is , then is the value of , which is . If it is or , then has no value, because and never get one. So can only point to . The analysis reads " points to " and " points to " as two independent facts, but a run sees one of them at a time.
Splitting statements changes the problem. A compiler usually breaks into and with a new variable . 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 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
- 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)
- 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)
- 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)
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag XYGBP7 Edit this page History Report an error Markdown