Problemadvanced
Mod-ref analysis
Also called side-effect analysis, MOD and REF analysis.
For each call or procedure, find the memory locations it may modify and the ones it may read.
Mod-ref analysis computes, for each call site and each procedure, the set of variables or memory locations that its execution may modify (MOD) and the set it may read (REF). Optimizers need it at every call: a value kept in a register across a call is still valid only if the call cannot modify it, and two calls can be reordered or run in parallel only if neither modifies what the other reads or modifies. Banning (1979) defined the problem for programs with reference parameters and split it into two parts: the alias-free side effects, computed as if no two names referred to the same location, and the aliases, added at the end. Cooper and Kennedy (1988) solved the alias-free part in time linear in the size of the call graph, by splitting it again into a problem on reference formal parameters and a problem on global variables. In languages with pointers, the MOD set of a store depends on alias analysis, and the problem becomes as hard as alias analysis itself.
In plain words
When a program calls a function, the compiler wants to know what that call can change and what it can read. If a call cannot change a variable, the compiler can keep the variable's value in a register while the call runs. If two calls do not touch the same data, they can run in either order. The call can change a variable directly, through a parameter that refers to the caller's variable, or through any function it calls in turn. The analysis collects these changes from the leaves of the call graph back to the callers. It follows how each parameter is passed down from function to function, and it adds the global variables that any called function changes.
Definition
Input. A program with a finite set of procedures, a set of global variables, and for each procedure its local variables and its formal parameters , passed by reference. Procedures are not nested. Each procedure has assignments and call sites. For each procedure, is the set of variables that the statements of itself, not its calls, may modify, and is the set they may read. A call site in calls a procedure with actual parameters . An actual is a variable of , which is then bound to the formal of , or an expression, which binds nothing.
Binding function. For a call site from to , maps a set of variables of to the variables of that they name during the call: each global in to itself, each formal to its actual if that is a variable, and each local of to nothing.
Output. For each procedure , , the variables of and the globals that an execution of may modify, and for each call site to , . These are the alias-free sets: they are the least sets that satisfy Banning's equation
The full answer is , where the alias pairs come from a separate alias analysis. REF, also called USE, is the same problem with in place of .
Soundness and precision. The problem is flow-insensitive: a procedure modifies a variable if some statement of it, or of a procedure it may call, may modify it, whether or not that statement runs on a real path. The least solution of the equation is the most precise flow-insensitive answer. It is sound for every run that only calls along call graph edges, with the call graph taken as given. Calls through pointers or virtual calls need a sound call graph construction first. In the terms of a data-flow framework:
| item | value |
|---|---|
| direction | backward over the call graph, from callees to callers |
| may or must | may |
| domain | a set of variables for each procedure |
| order | inclusion (bottom is precise) |
| join | union |
| bottom and top | every set empty, and every set holding all variables visible in the procedure |
| boundary value | for each procedure |
| initial value | |
| transfer | for each call site , which keeps the globals, maps formals to actuals and drops the callee's locals |
Variants
- MOD and REF. The two problems have the same structure and are solved by the same algorithm with different initial sets. Many tools compute both at once and report for each call whether it modifies, reads, both or neither of a location.
- Alias-free and with aliases. Reference parameters create aliases when one variable is passed twice, or when a global is passed as an argument. Banning adds them after the alias-free sets are known, using the alias pairs of each procedure.
- Flow-sensitive side effects. "Must modify" sets, which say what a call always overwrites, need the paths inside each procedure, and are harder than the may sets. They are the interprocedural form of the kill sets of reaching definitions.
- Programs with pointers. In C or Java, a store or modifies the locations in the points-to set of . Then comes from a points-to analysis, and the variables of the problem are abstract locations instead of names. Locations that do not escape a callee can be dropped from its MOD set, like locals.
- Nested procedures. With nested scopes, a callee can modify the locals of the procedures that enclose it, and the transfer must drop only the variables that are local to the callee. Cooper and Kennedy handle this case with a more careful pass over the call graph.
- Array sections. For parallelizing compilers, the MOD set of an array is a region of the array, for example one row, instead of the whole array.
Examples
Take globals and and these four procedures:
main() { local x, y; call p(x, y); // s1
call q(x); } // s2
p(a, b) { a = 1; h = 2; call q(b); } // s3
q(c) { g = c; call r(c); } // s4
r(d) { d = 0; }
The local sets are , , and . The binding graph has an edge from to (site s3) and from to (site s4). Since is modified, and are modified through these bindings, so , and . The variables of each procedure that are bound to modified formals of its callees join its local set, and then the globals flow up the call graph:
| procedure | globals modified below it | ||
|---|---|---|---|
| r | none | ||
| q | none | ||
| p | |||
| main |
So , , and . If main called p(x, x) instead, and would be aliases inside p, and the alias step would add to every MOD set of p that holds , and the other way round.
Known algorithms
| algorithm | what it solves | time |
|---|---|---|
| iteration of Banning's equation | the alias-free sets on the call graph | rounds of set operations in the worst case, for variables |
| Banning (1979) | the alias-free sets and the aliases, by iteration over the call graph | polynomial |
| Cooper and Kennedy (1988) | the alias-free sets, split into formals and globals | plus set operations |
| points-to based MOD and REF | side effects of stores and loads through pointers | the cost of the points-to analysis plus one pass over the call graph |
Here and are the procedures and call sites, and and are the formals and the bindings of formals to formals. A set operation is a union of bit vectors of bits.
The linear-time algorithm. Cooper and Kennedy solve the alias-free problem in three steps.
- Formals, on the binding multigraph. The binding graph has one node per formal parameter, and an edge for each call site where the formal of the caller is passed as the actual for the formal of the callee. A formal of is in if a path in leads from to a formal of some procedure with . This is one backward graph search from all locally modified formals, in time.
- The extended local sets. . This adds the variables of that it passes to a modified formal.
- Globals, on the call graph. Find the strongly connected components of the call graph with Tarjan's algorithm, and visit them in reverse topological order. For a component , is the union of over and of over the components that calls. Then for each in its component .
Finally for each call site to .
Bounds
Theorem (Cooper and Kennedy). The three steps compute the least solution of Banning's equation, and they take time plus unions of sets of globals.
Proof. Let be the least solution. Every distributes over union and maps a single variable to at most one variable, so by Kleene iteration, if and only if there is a chain of call sites with and a variable such that . Call and the variables along the chain, so .
Formals. Let be a formal of . A binding maps a global to a global and never to a formal, so every is a formal, and is an edge of . So if and only if has a path from to a formal in the set of its procedure, which is .
All variables. If a is global, so are . Let be the largest index with all global, or if is not global. If , then is a global in . If , then is a formal of when , and it is in by the rest of the chain, so it is in , and . The case , is the same with , and gives . The case , gives . So , or is a global in for a procedure that reaches in the call graph. Both are in , since collects the globals of over every procedure that reaches, including itself. Conversely, by the formal part and the equation, so . A global in for reachable from is in , and each keeps it, so it is in .
Time. Step 1 is one search of . Step 2 visits each actual parameter once. Step 3 is Tarjan's algorithm, , and one union per procedure and per call graph edge between components. Computing all sets visits each call site once.
Lower bounds. Every algorithm must read the call graph and the local sets, so the time is at least linear. With bit vectors of -bit words, a union of sets of globals costs , which gives for step 3.
With pointers. When stores go through pointers, itself depends on which locations a pointer may point to. A precise MOD set for a call would decide whether a pointer in the callee may point to a given variable, so precise mod-ref inherits the hardness of alias analysis, which is undecidable in general (may-alias undecidability). Tools use a sound points-to analysis and accept larger sets.
Reductions
- Graph reachability reduces to it. Give each procedure one global that it modifies and calls that follow the edges of a directed graph. Then holds the global of if and only if is reachable from . So the global part is exactly a reachability problem, which is why step 3 needs the component graph.
- Alias-free MOD and aliases are independent. The alias pairs of each procedure can be computed with a separate pass over the call graph, and MOD is the alias-free set closed under these pairs, as in Banning's split.
- Mod-ref reduces to points-to analysis for pointer programs. Each store gives entries for the targets of its pointer, and each load gives entries, after which the problem is the one above on abstract locations.
- It is a special case of interprocedural data-flow analysis. The equation is a flow-insensitive interprocedural data-flow problem over the call graph, with the binding functions as transfer functions.
Applications
- Register allocation and code motion across calls. A value of a variable that is not in survives the call , so the compiler can keep it in a register or move a load of it past the call.
- Interprocedural constant propagation. A constant in a global stays valid across a call that does not modify it (interprocedural constant propagation).
- Dependence and parallelization. Two calls are independent if neither one's MOD set meets the other's MOD or REF set. Parallelizing compilers for Fortran used this, with array sections, to run calls in a loop in parallel. The same sets give the edges of calls in a program dependence graph.
- Compiler alias interfaces. Modern compilers ask their alias analysis, for each call and each memory access, whether the call may modify or read that location, and use the answer for redundancy elimination and scheduling.
References
- John P. Banning, An efficient way to find the side effects of procedure calls and the aliases of variables, Proceedings of POPL 1979, 29 to 41, the MOD and USE problems, their split into an alias-free part and the aliases of variables, and the equations over the call graph with binding functions
- Keith D. Cooper and Ken Kennedy, Interprocedural side-effect analysis in linear time, Proceedings of PLDI 1988, SIGPLAN Notices 23(7), 57 to 66, Banning's alias-free flow-insensitive problem solved with the binding multigraph and depth-first search, in time linear in the size of the call multigraph
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 NW600V Edit this page History Report an error Markdown