Algorithm Search⌘K

Problemcore

Alias analysis

Also called may-alias analysis, must-alias analysis.

Decide whether two memory references in a program may, or must, refer to the same location.

Alias analysis decides whether two memory references in a program can name the same location. A may-alias answer says that they can in some execution, and a must-alias answer says that they do in every execution that reaches the point. Compilers need both: a store through *p blocks the motion of a load from *q only if the two may alias, and a load can take the value of an earlier store only if the two must alias. The precise question is undecidable once the program can build data structures of any size, so analyses answer it approximately, and nearly all of them work from a points-to analysis or from the type rules of the language.

In plain words

Two names in a program can stand for the same box in memory. A pointer can point to a variable, or two pointers can point to the same object. If the compiler writes into one name and then reads from the other, the order matters. Alias analysis tells the compiler when two names might share a box, and when they surely share it. If they might, the compiler must keep the order of the read and the write. If they surely do not, it is free to move them. If they surely do, it can reuse the value it just wrote. A safe "might" answer can be too cautious, but it must never say "no" when the answer is "yes".

Definition

Input. A program in the toy IR with the pointer instructions p = &x, x = *p, *p = a and p = alloc, a program point xx, and two memory references e1e_1 and e2e_2. A memory reference is a variable a or a dereference *p. In a state σ\sigma, a names the location aa, and *p names the location whose address pp holds; it names nothing if pp holds no address.

The two questions. e1e_1 and e2e_2 alias in σ\sigma when both name the same location.

  • May-alias: is there an executable path from the entry to xx that ends in a state where e1e_1 and e2e_2 alias?
  • Must-alias: do e1e_1 and e2e_2 alias at the end of every executable path from the entry to xx?

A sound may-alias analysis answers "no alias" only when the true answer is no. A sound must-alias analysis answers "must alias" only when the true answer is yes. Many compilers return one of three answers, no alias, may alias and must alias; LLVM adds partial alias for references that overlap in part.

From points-to sets. Let GxG_x be the may points-to graph at xx (points-to analysis) and MxM_x a must points-to graph, where (p,a)∈Mx(p, a) \in M_x means that pp holds the address of the one concrete location aa on every path to xx. Then

  • *p and *q may alias if Gx(p)∩Gx(q)≠∅G_x(p) \cap G_x(q) \ne \emptyset, and *p and a may alias if a∈Gx(p)a \in G_x(p);
  • *p and *q must alias if Mx(p)=Mx(q)={a}M_x(p) = M_x(q) = \{a\} for some aa, and *p and a must alias if Mx(p)={a}M_x(p) = \{a\}.

A heap location hkh_k stands for all cells made at one alloc site, so it never appears in MxM_x: two pointers into the same site may point to different cells.

The lattices. Both analyses below use the order of abstract interpretation in the direction of Cousot, where lower is more precise and the answer is the least fixed point. Turning the order upside down maps them onto the meet convention of Kildall, which some data-flow texts use.

item may-alias (from may points-to) must-alias (from must points-to)
direction forward forward
may or must may must
domain subsets of Loc×LocLoc \times Loc subsets of V×VV \times V in which each variable has at most one target, plus a bottom element
order ⊆\subseteq ⊇\supseteq: more must facts is lower and more precise
join union intersection
bottom and top ∅\emptyset and Loc×LocLoc \times Loc bottom is "not reached", top is ∅\emptyset (no must fact)
boundary value IN[entry]=∅IN[entry] = \emptyset IN[entry]=∅IN[entry] = \emptyset
initial value ∅\emptyset bottom
transfer as in points-to analysis; monotone, not distributive below; monotone, not distributive
height N2N^2 for NN locations at most $

Must transfer functions. "Kill pp" removes the fact of pp. The must analysis reads the may graph GG at the same point, which is computed first.

instruction output
p = &a kill pp, add (p,a)(p, a)
p = alloc kill pp (a heap location is not one concrete location)
p = q kill pp, add (p,a)(p, a) if (q,a)(q, a) is a fact
p = *q kill pp, add (p,b)(p, b) if (q,a)(q, a) and (a,b)(a, b) are facts
*p = q with the fact (p,a)(p, a) kill aa, add (a,b)(a, b) if (q,b)(q, b) is a fact
*p = q otherwise kill every a∈G(p)a \in G(p)
any other instruction that defines xx kill xx

Equations. IN[B]=⨆P∈pred(B)OUT[P]IN[B] = \bigsqcup_{P \in pred(B)} OUT[P] and OUT[B]=fB(IN[B])OUT[B] = f_B(IN[B]), where ⨆\bigsqcup is union for may and intersection for must.

The ideal answer is the join over all paths, MOP(x)=⨆πfπ(IN[entry])MOP(x) = \bigsqcup_\pi f_\pi(IN[entry]) over the paths π\pi from the entry to xx. The computed answer is the least fixed point in the order of the table, by a worklist that starts from the initial values. The functions are monotone, so MOP(x)⊑LFP(x)MOP(x) \sqsubseteq LFP(x): the may graph has at least the pairs of MOP, and the must graph has at most the facts of MOP.

Soundness. The concrete semantics is the set of states that reach each point along the executable paths. Every state that reaches xx along a path π\pi satisfies the facts of fπf_\pi: its address pairs are in the may graph, and every must fact holds in it. This follows by induction along π\pi, one instruction at a time, as on the points-to analysis page. For the must store, the fact (p,a)(p, a) says that the store writes exactly aa; otherwise it may write any a∈G(p)a \in G(p), and every such fact is killed. Every real path is a path of the control flow graph, so a "no alias" from the may graph and a "must alias" from the must graph are both correct.

Precision. In this program the two references *p and *q at block join may alias, along the path through then, but they do not must alias:

entry:
  p = alloc
  br c then else
then:
  q = p
  jump join
else:
  q = alloc
  jump join
join:
  *p = 1
  x = *q
  ret x

The may graph at join has G(p)={h1}G(p) = \{h_1\} and G(q)={h1,h2}G(q) = \{h_1, h_2\}, which meet, so the answer is "may alias". The must graph has no fact for p or q, because both point into the heap. Even if both branches did q = p, the must analysis above would not say "must alias", because h1h_1 is a heap location; an analysis that also tracks equalities of pointer values, such as q = p, recovers that answer. This is the usual price of reasoning through locations only.

Cost. The may graph costs what points-to analysis costs. The must graph has height at most ∣V∣+2|V| + 2 at each point, so the worklist applies O((n+m)∣V∣)O((n + m)|V|) transfer functions for nn instructions and mm edges. A query then takes one set intersection.

Variants

  • May and must. Most analyses answer may-alias only; a must answer is a by-product of must points-to facts or of value numbering of pointers.
  • Flow and context. A flow-insensitive analysis has one answer for the whole program (flow sensitivity); a context-insensitive one merges the facts of all callers of a procedure (context sensitivity).
  • Representation. Alias pairs list the pairs of access paths that may alias, often limited to paths of length kk. Points-to graphs list targets. Equivalence classes, as in Steensgaard's analysis, put references that may alias into one class. The points-to form is the most common, since it is compact and gives the other two.
  • Type-based. In a type-safe language, two references of unrelated types cannot alias (type-based alias analysis). C has the same rule for most types, and compilers use it unless a flag turns it off.
  • Programmer assertions. C99 restrict and the rules on Fortran dummy arguments let the compiler assume that some references do not alias.
  • Fields and arrays. References to different fields of one object never alias (field sensitivity); references to array elements need dependence tests on the index expressions.
  • Heap structure. Shape analysis and separation logic tell apart the cells of one allocation site when they form lists or trees.

Examples

Through one variable. After p = &a, *p and a must alias, and after q = &b with b≠ab \ne a, *p and *q do not alias. A store *q = 1 then does not change a, so a later load of a can reuse an earlier value.

Two parameters. In C, a function f(int *x, int *y) can be called with f(&v, &v), so *x and *y may alias inside f. An intraprocedural analysis must assume this, and an interprocedural one looks at the call sites. With int *restrict x, the programmer promises that they do not.

Types. In Java, a reference of a class A and a reference of an unrelated class B never name the same object, whatever the flow; in C, an int lvalue and a float lvalue may be assumed apart under the strict aliasing rule.

Known algorithms

AlgorithmTimeSpacePropertiesCode
Type-based alias analysisO(t^2 h + m log t) worstO(t^2) extranot context sensitivedeterministicnot flow sensitiveofflineiterative1 language

Bounds

  • Undecidable. With dynamic allocation and recursive data structures, precise may-alias is not decidable and precise must-alias is not recursively enumerable, even when every path is executable (Landi 1992, Ramalingam 1994; undecidability of may-alias analysis).
  • PSPACE-complete without allocation. If the program cannot allocate, precise flow-sensitive may-alias is decidable and PSPACE-complete (as summarized by Rinetzky et al. 2008).
  • NP-hard without order. Precise flow-insensitive may-alias is NP-hard, even with no dynamic memory (Horwitz 1997; NP-hardness of precise flow-insensitive alias analysis).
  • Polynomial approximations. Sound answers come in O(n3)O(n^3) time from Andersen's analysis, in almost linear time from Steensgaard's analysis, and from the types alone, in time polynomial in the number of types, by type-based analysis.

Reductions

Alias and points-to are the same question. *p may alias a exactly when pp may point to aa, and *p may alias *q exactly when pp and qq may point to a common location. So a points-to analysis gives an alias analysis, and an alias oracle for the pairs (*p, a) gives the points-to sets.

PCP reduces to may-alias. A program that grows a chain of cells and walks two pointers down it by the two halves of a Post correspondence instance makes the pointers alias exactly when the instance has a solution (undecidability of may-alias analysis).

Hamiltonian path reduces to flow-insensitive may-alias. One statement per edge and one long dereference chain make a pointer reach the end node exactly when a Hamiltonian path exists (NP-hardness of precise flow-insensitive alias analysis).

Applications

References

  1. William Landi, Undecidability of static analysis, ACM Letters on Programming Languages and Systems 1(4), 323 to 337 (1992), the main theorems (precise may-alias is not recursive and must-alias is not recursively enumerable)
  2. G. Ramalingam, The undecidability of aliasing, ACM Transactions on Programming Languages and Systems 16(5), 1467 to 1471 (1994), whole paper
  3. 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
  4. 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 summary of earlier results (flow-sensitive may-alias without dynamic allocation is PSPACE-complete)
  5. Michael Hind, Pointer analysis, haven't we solved this problem yet?, Proceedings of the ACM SIGPLAN-SIGSOFT Workshop on Program Analysis for Software Tools and Engineering (PASTE '01), 54 to 61, 2001, the relation of alias pairs and points-to sets, and the dimensions of precision
  6. Amer Diwan, Kathryn S. McKinley and J. Eliot B. Moss, Type-based alias analysis, Proceedings of the ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 1998), 106 to 117, alias analysis from the type rules of a type-safe language

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)
WritingW2 basic
  • a contract.yaml