Algorithm Search⌘K

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 GlGl of global variables, and for each procedure pp its local variables and its formal parameters f1,…,fkf_1, \ldots, f_k, passed by reference. Procedures are not nested. Each procedure has assignments and call sites. For each procedure, IMOD(p)IMOD(p) is the set of variables that the statements of pp itself, not its calls, may modify, and IREF(p)IREF(p) is the set they may read. A call site ss in pp calls a procedure qq with actual parameters a1,…,aka_1, \ldots, a_k. An actual is a variable of pp, which is then bound to the formal fif_i of qq, or an expression, which binds nothing.

Binding function. For a call site ss from pp to qq, bsb_s maps a set XX of variables of qq to the variables of pp that they name during the call: each global in XX to itself, each formal fi∈Xf_i \in X to its actual aia_i if that is a variable, and each local of qq to nothing.

Output. For each procedure pp, GMOD(p)GMOD(p), the variables of pp and the globals that an execution of pp may modify, and for each call site ss to qq, DMOD(s)=bs(GMOD(q))DMOD(s) = b_s(GMOD(q)). These are the alias-free sets: they are the least sets that satisfy Banning's equation

GMOD(p)=IMOD(p)∪⋃s in p, calling qbs(GMOD(q)).GMOD(p) = IMOD(p) \cup \bigcup_{s \text{ in } p \text{, calling } q} b_s(GMOD(q)).

The full answer is MOD(s)=DMOD(s)∪{y:x∈DMOD(s) and x,y may be aliases in p}MOD(s) = DMOD(s) \cup \{y : x \in DMOD(s) \text{ and } x, y \text{ may be aliases in } p\}, where the alias pairs come from a separate alias analysis. REF, also called USE, is the same problem with IREFIREF in place of IMODIMOD.

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 IMOD(p)IMOD(p) for each procedure pp
initial value GMOD(p)=IMOD(p)GMOD(p) = IMOD(p)
transfer bsb_s for each call site ss, 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 ∗p=e{*}p = e or p.f=ep.f = e modifies the locations in the points-to set of pp. Then IMODIMOD 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 gg and hh 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 IMOD(main)=∅IMOD(main) = \emptyset, IMOD(p)={a,h}IMOD(p) = \{a, h\}, IMOD(q)={g}IMOD(q) = \{g\} and IMOD(r)={d}IMOD(r) = \{d\}. The binding graph has an edge from bb to cc (site s3) and from cc to dd (site s4). Since dd is modified, cc and bb are modified through these bindings, so RMOD(r)={d}RMOD(r) = \{d\}, RMOD(q)={c}RMOD(q) = \{c\} and RMOD(p)={a,b}RMOD(p) = \{a, b\}. 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 IMOD+IMOD^+ globals modified below it GMODGMOD
r {d}\{d\} none {d}\{d\}
q {g,c}\{g, c\} none {g,c}\{g, c\}
p {a,b,h}\{a, b, h\} {g}\{g\} {a,b,h,g}\{a, b, h, g\}
main {x,y}\{x, y\} {g,h}\{g, h\} {x,y,g,h}\{x, y, g, h\}

So DMOD(s1)={x,y,g,h}DMOD(s_1) = \{x, y, g, h\}, DMOD(s2)={x,g}DMOD(s_2) = \{x, g\}, DMOD(s3)={b,g}DMOD(s_3) = \{b, g\} and DMOD(s4)={c}DMOD(s_4) = \{c\}. If main called p(x, x) instead, aa and bb would be aliases inside p, and the alias step would add bb to every MOD set of p that holds aa, 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 O(N⋅V)O(N \cdot V) rounds of O(E)O(E) set operations in the worst case, for VV 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 O(Nβ+Eβ)O(N_\beta + E_\beta) plus O(N+E)O(N + E) 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 NN and EE are the procedures and call sites, and NβN_\beta and EβE_\beta are the formals and the bindings of formals to formals. A set operation is a union of bit vectors of ∣Gl∣|Gl| bits.

The linear-time algorithm. Cooper and Kennedy solve the alias-free problem in three steps.

  1. Formals, on the binding multigraph. The binding graph β\beta has one node per formal parameter, and an edge f→f′f \to f' for each call site where the formal ff of the caller is passed as the actual for the formal f′f' of the callee. A formal ff of qq is in RMOD(q)RMOD(q) if a path in β\beta leads from ff to a formal f′′f'' of some procedure rr with f′′∈IMOD(r)f'' \in IMOD(r). This is one backward graph search from all locally modified formals, in O(Nβ+Eβ)O(N_\beta + E_\beta) time.
  2. The extended local sets. IMOD+(p)=IMOD(p)∪⋃s in p, calling qbs(RMOD(q))IMOD^+(p) = IMOD(p) \cup \bigcup_{s \text{ in } p \text{, calling } q} b_s(RMOD(q)). This adds the variables of pp that it passes to a modified formal.
  3. 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 CC, GG(C)GG(C) is the union of IMOD+(p)∩GlIMOD^+(p) \cap Gl over p∈Cp \in C and of GG(C′)GG(C') over the components C′C' that CC calls. Then GMOD(p)=IMOD+(p)∪GG(C(p))GMOD(p) = IMOD^+(p) \cup GG(C(p)) for each pp in its component C(p)C(p).

Finally DMOD(s)=bs(GMOD(q))DMOD(s) = b_s(GMOD(q)) for each call site ss to qq.

Bounds

Theorem (Cooper and Kennedy). The three steps compute the least solution of Banning's equation, and they take O(Nβ+Eβ+∑p∣IMOD(p)∣)O(N_\beta + E_\beta + \sum_p |IMOD(p)|) time plus O(N+E)O(N + E) unions of sets of globals.

Proof. Let LL be the least solution. Every bsb_s distributes over union and maps a single variable to at most one variable, so by Kleene iteration, v∈L(p0)v \in L(p_0) if and only if there is a chain of call sites p0→s1p1→s2⋯→skpkp_0 \xrightarrow{s_1} p_1 \xrightarrow{s_2} \cdots \xrightarrow{s_k} p_k with k≥0k \ge 0 and a variable w∈IMOD(pk)w \in IMOD(p_k) such that v=bs1(bs2(⋯bsk(w)))v = b_{s_1}(b_{s_2}(\cdots b_{s_k}(w))). Call yk=wy_k = w and yi−1=bsi(yi)y_{i-1} = b_{s_i}(y_i) the variables along the chain, so v=y0v = y_0.

Formals. Let vv be a formal of p0p_0. A binding maps a global to a global and never to a formal, so every yiy_i is a formal, and yi−1→yiy_{i-1} \to y_i is an edge of β\beta. So v∈L(p0)v \in L(p_0) if and only if β\beta has a path from vv to a formal in the IMODIMOD set of its procedure, which is v∈RMOD(p0)v \in RMOD(p_0). □\square

All variables. If a yiy_i is global, so are yi−1,…,y0y_{i-1}, \ldots, y_0. Let tt be the largest index with y0,…,yty_0, \ldots, y_t all global, or t=−1t = -1 if vv is not global. If t=kt = k, then v=wv = w is a global in IMOD(pk)⊆IMOD+(pk)IMOD(p_k) \subseteq IMOD^+(p_k). If t<kt < k, then yt+1y_{t+1} is a formal of pt+1p_{t+1} when t+1≥1t + 1 \ge 1, and it is in L(pt+1)L(p_{t+1}) by the rest of the chain, so it is in RMOD(pt+1)RMOD(p_{t+1}), and yt=bst+1(yt+1)∈IMOD+(pt)y_t = b_{s_{t+1}}(y_{t+1}) \in IMOD^+(p_t). The case t=−1t = -1, k≥1k \ge 1 is the same with t+1=1t + 1 = 1, and gives v∈IMOD+(p0)v \in IMOD^+(p_0). The case t=−1t = -1, k=0k = 0 gives v∈IMOD(p0)v \in IMOD(p_0). So v∈IMOD+(p0)v \in IMOD^+(p_0), or vv is a global in IMOD+(pt)IMOD^+(p_t) for a procedure ptp_t that p0p_0 reaches in the call graph. Both are in IMOD+(p0)∪GG(C(p0))IMOD^+(p_0) \cup GG(C(p_0)), since GG(C)GG(C) collects the globals of IMOD+IMOD^+ over every procedure that CC reaches, including CC itself. Conversely, bs(RMOD(q))⊆bs(L(q))⊆L(p)b_s(RMOD(q)) \subseteq b_s(L(q)) \subseteq L(p) by the formal part and the equation, so IMOD+(p)⊆L(p)IMOD^+(p) \subseteq L(p). A global in IMOD+(q)IMOD^+(q) for qq reachable from pp is in L(q)L(q), and each bsb_s keeps it, so it is in L(p)L(p). □\square

Time. Step 1 is one search of β\beta. Step 2 visits each actual parameter once. Step 3 is Tarjan's algorithm, O(N+E)O(N + E), and one union per procedure and per call graph edge between components. Computing all DMODDMOD sets visits each call site once. ■\blacksquare

Lower bounds. Every algorithm must read the call graph and the local sets, so the time is at least linear. With bit vectors of ww-bit words, a union of sets of globals costs O(∣Gl∣/w+1)O(|Gl| / w + 1), which gives O((N+E)(∣Gl∣/w+1))O((N + E)(|Gl| / w + 1)) for step 3.

With pointers. When stores go through pointers, IMODIMOD 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 GMOD(p)GMOD(p) holds the global of qq if and only if qq is reachable from pp. 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 IMODIMOD entries for the targets of its pointer, and each load gives IREFIREF 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 MOD(s)MOD(s) survives the call ss, 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

  1. 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
  2. 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.

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 known algorithm
  • a contract.yaml