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 , and two memory references and . A memory reference is a variable a or a dereference *p. In a state , a names the location , and *p names the location whose address holds; it names nothing if holds no address.
The two questions. and alias in when both name the same location.
- May-alias: is there an executable path from the entry to that ends in a state where and alias?
- Must-alias: do and alias at the end of every executable path from the entry to ?
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 be the may points-to graph at (points-to analysis) and a must points-to graph, where means that holds the address of the one concrete location on every path to . Then
*pand*qmay alias if , and*pandamay alias if ;*pand*qmust alias if for some , and*pandamust alias if .
A heap location stands for all cells made at one alloc site, so it never appears in : 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 | subsets of in which each variable has at most one target, plus a bottom element |
| order | : more must facts is lower and more precise | |
| join | union | intersection |
| bottom and top | and | bottom is "not reached", top is (no must fact) |
| boundary value | ||
| initial value | bottom | |
| transfer | as in points-to analysis; monotone, not distributive | below; monotone, not distributive |
| height | for locations | at most $ |
Must transfer functions. "Kill " removes the fact of . The must analysis reads the may graph at the same point, which is computed first.
| instruction | output |
|---|---|
p = &a |
kill , add |
p = alloc |
kill (a heap location is not one concrete location) |
p = q |
kill , add if is a fact |
p = *q |
kill , add if and are facts |
*p = q with the fact |
kill , add if is a fact |
*p = q otherwise |
kill every |
| any other instruction that defines | kill |
Equations. and , where is union for may and intersection for must.
The ideal answer is the join over all paths, over the paths from the entry to . 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 : 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 along a path satisfies the facts of : its address pairs are in the may graph, and every must fact holds in it. This follows by induction along , one instruction at a time, as on the points-to analysis page. For the must store, the fact says that the store writes exactly ; otherwise it may write any , 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 and , 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 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 at each point, so the worklist applies transfer functions for instructions and 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 . 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
restrictand 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 , *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
- From points-to sets: Andersen's analysis, Steensgaard's analysis, Das's one-level flow, and their faster solvers; demand-driven points-to analysis for one query at a time.
- From types: type-based alias analysis, with one union-find pass over the assignments and then a table over pairs of types.
- From local reasoning: distinct local variables whose address is not taken never alias; a base pointer plus two different constant offsets never alias. Compilers chain such cheap tests before the costly ones. LLVM, for example, asks a list of alias analyses in turn and keeps the first definite answer.
- From heap structure: shape analysis and separation logic.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Type-based alias analysis | O(t^2 h + m log t) worst | O(t^2) extra | not context sensitivedeterministicnot flow sensitiveofflineiterative | 1 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 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 may point to , and *p may alias *q exactly when and 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
- Reordering. Instruction scheduling may swap a load and a store only if they do not alias.
- Redundancy. Common subexpression elimination and partial redundancy elimination reuse a loaded value only if no store between may alias it; store-to-load forwarding needs a must answer.
- Code motion. Loop-invariant code motion hoists a load only if no store in the loop may alias it.
- Dead stores. Dead code elimination removes a store that a later store must overwrite before any load that may alias reads it.
- Registers and aggregates. Scalar replacement of aggregates keeps fields in registers when no other reference may alias them.
- Side effects. Mod-ref analysis finds the locations that a call may read or write, from the alias facts of its body.
References
- 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)
- G. Ramalingam, The undecidability of aliasing, ACM Transactions on Programming Languages and Systems 16(5), 1467 to 1471 (1994), whole paper
- 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
- 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)
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag ZKS818 Edit this page History Report an error Markdown