Algorithm Search⌘K

Theoremadvanced

NP-completeness of code generation for DAGs

Also called Bruno-Sethi theorem.

Generating optimal code for an expression DAG on a register machine is NP-complete, unlike for trees.

Statement

A program for a basic block computes an expression DAG: a graph whose nodes are operations and whose shared nodes are common subexpressions. For trees, the cheapest code is easy to find. The Sethi-Ullman numbering gives the fewest registers, and the Aho-Johnson algorithm and BURS give the cheapest tiling, in time linear in the tree for a fixed machine. For DAGs, several forms of the problem are NP-complete. Each result holds for a precise machine model, and the models differ.

Theorem 1 (Bruno and Sethi, 1976). Take a machine with one register, an accumulator, and unlimited memory. Its instructions load a memory word into the register, store the register into memory, and combine the register with one operand in memory, leaving the result in the register. The cost of a program is its number of instructions. Given a DAG whose leaves are in memory and an integer KK, it is NP-complete to decide whether some program of at most KK instructions computes all the roots of the DAG.

Theorem 2 (Sethi, 1975). Take a machine with kk registers in which each operation reads its operands from registers and writes its result to a register, each node is computed exactly once, and no value is stored to memory. Given a DAG and kk, it is NP-complete to decide whether the DAG can be computed in this way. This is the register sufficiency problem.

Theorem 3 (Aho, Johnson and Ullman, 1977). The problem of generating optimal code for expressions with common subexpressions stays NP-complete for simple expressions and simple machines. For the one-register machine they give an optimal algorithm whose time is linear in the size of the DAG and exponential only in the amount of sharing.

Theorem 4 (tiling a DAG). Take unlimited registers, a tree grammar that describes the instructions, and a cost for each rule. A value that is computed once can be used by all its parents. Given a DAG, the grammar and an integer KK, it is NP-complete to decide whether the DAG has a cover of cost at most KK. This holds even for the fixed grammar Γ0\Gamma_0 below, with 11 rules of cost 1, each of which covers one node.

Theorem 4 is the form of the instruction selection problem on DAGs. Proebsting gave a proof in 1995, and Koes and Goldstein gave one in 2008. The proof below uses its own gadget, and it is given in full. Theorems 1 to 3 are stated as in the papers.

In plain words

When a compiler turns an expression into machine code, a value that is used in two places is a shared value. Without shared values, the expression is a tree, and the best code for a tree can be found quickly, piece by piece from the bottom.

With shared values, each shared piece must be computed in a way that suits every place that uses it. A choice that is good for one user can be bad for another. These choices interact across the whole expression, and in the worst case no method is known that avoids trying very many combinations.

This is true for several kinds of machines: a machine with one register, a machine with a fixed number of registers and no memory for temporary values, and a machine with as many registers as needed but with a menu of instructions to choose from.

Proof

We prove Theorem 4. A grammar has nonterminals, a start nonterminal SS, and rules A→pA \to p with a cost, where pp is a pattern: a tree of operators whose leaves are nonterminals. A cover of a DAG is a set of pairs (v,r)(v, r), where the pattern of rule rr matches at node vv, such that:

  1. each root of the DAG has a pair whose rule has the left side SS;
  2. for each pair (v,r)(v, r) and each nonterminal leaf BB of the pattern of rr, matched at a node uu, there is a pair (u,r′)(u, r') whose rule has the left side BB.

The cost of a cover is the sum of the costs of its rules. A node with two pairs is computed twice, once for each nonterminal, and a node with one pair is computed once and used by all its parents.

The grammar Γ0\Gamma_0. The nonterminals are SS, TT and FF. The operators are a leaf xx, a unary NOT, and a ternary OR3. The rules, each of cost 1, are

T→x,F→x,F→NOT(T),T→NOT(F),T \to x, \qquad F \to x, \qquad F \to \mathrm{NOT}(T), \qquad T \to \mathrm{NOT}(F),

and S→OR3(A1,A2,A3)S \to \mathrm{OR3}(A_1, A_2, A_3) for each of the 7 triples (A1,A2,A3)∈{T,F}3(A_1, A_2, A_3) \in \{T, F\}^3 other than (F,F,F)(F, F, F).

Membership in NP. A cover never needs the same pair twice. A pattern that matches at vv is placed by vv alone, so there are at most ∣V∣⋅∣R∣|V| \cdot |R| pairs for a DAG with ∣V∣|V| nodes and a grammar with ∣R∣|R| rules. A certificate is a set of pairs, and conditions 1 and 2 and the cost are checked in polynomial time.

The reduction. We reduce from 3-SAT with exactly three distinct variables in each clause, which is NP-complete. Let φ\varphi have the variables x1,…,xnx_1, \ldots, x_n, each of which occurs in some clause, and the clauses C1,…,CmC_1, \ldots, C_m. Build the DAG GφG_\varphi:

  • a node XiX_i with the operator xx, for each variable xix_i;
  • a node Ni=NOT(Xi)N_i = \mathrm{NOT}(X_i), for each variable that occurs negated in some clause;
  • a node Rj=OR3(Lj1,Lj2,Lj3)R_j = \mathrm{OR3}(L_{j1}, L_{j2}, L_{j3}) for each clause, where the child for the literal xix_i is XiX_i and the child for ¬xi\neg x_i is NiN_i.

Let qq be the number of nodes NiN_i, and let K=n+q+mK = n + q + m, which is the number of nodes. The roots are the nodes RjR_j. The construction takes linear time.

Lemma 1. Each cover of GφG_\varphi has cost at least KK, with equality only if each node has exactly one pair.

Proof. Each RjR_j is a root, so it has a pair by condition 1. Each NiN_i is a child of some RjR_j, and every OR3 rule has a nonterminal at each child, so NiN_i has a pair by condition 2. Each XiX_i is a child of some RjR_j or of NiN_i, and the rules of OR3 and NOT have a nonterminal at each child, so XiX_i has a pair. So each of the KK nodes has at least one pair, and each pair costs 1. □\square

If φ\varphi is satisfiable, there is a cover of cost KK. Let α\alpha be a satisfying assignment. Give XiX_i the pair with the rule T→xT \to x if α(xi)\alpha(x_i) is true and F→xF \to x if it is false. Give NiN_i the rule F→NOT(T)F \to \mathrm{NOT}(T) if α(xi)\alpha(x_i) is true and T→NOT(F)T \to \mathrm{NOT}(F) if it is false. Then each node XiX_i or NiN_i has the left side TT exactly when its literal is true, and the rule at NiN_i finds the nonterminal that it needs at XiX_i. Give RjR_j the rule S→OR3(A1,A2,A3)S \to \mathrm{OR3}(A_1, A_2, A_3), where ApA_p is the left side at its pp-th child. Since α\alpha satisfies CjC_j, some ApA_p is TT, so the triple is not (F,F,F)(F, F, F) and the rule exists. Conditions 1 and 2 hold, and there is one pair per node, so the cost is KK.

If there is a cover of cost at most KK, then φ\varphi is satisfiable. By Lemma 1, each node has exactly one pair. Let α(xi)\alpha(x_i) be true if the rule at XiX_i has the left side TT, and false otherwise. The only pair at NiN_i is F→NOT(T)F \to \mathrm{NOT}(T) or T→NOT(F)T \to \mathrm{NOT}(F). In the first case, condition 2 needs the left side TT at XiX_i, so α(xi)\alpha(x_i) is true, and NiN_i has the left side FF. In the second case, XiX_i has FF, so α(xi)\alpha(x_i) is false, and NiN_i has TT. So in both cases each literal node has the left side TT exactly when its literal is true under α\alpha. The only pair at RjR_j is S→OR3(A1,A2,A3)S \to \mathrm{OR3}(A_1, A_2, A_3), and by condition 2 each ApA_p is the only left side at the pp-th child. The triple is not (F,F,F)(F, F, F), so some literal of CjC_j is true. So α\alpha satisfies every clause.

So φ\varphi is satisfiable if and only if GφG_\varphi has a cover of cost at most KK. With membership in NP, this proves Theorem 4. ■\blacksquare

For example, the clause x1∨¬x2∨x3x_1 \lor \neg x_2 \lor x_3 alone gives the nodes X1X_1, X2X_2, X3X_3, N2N_2 and R1R_1, and K=5K = 5. The assignment with x1x_1 true gives the cover T→xT \to x at X1X_1, any one rule at X2X_2 with the matching rule at N2N_2, any one rule at X3X_3, and the OR3 rule with TT first. If a second clause ¬x1∨x2∨¬x3\neg x_1 \lor x_2 \lor \neg x_3 is added, X1X_1 is shared by both clauses through N1N_1, and the cover of cost KK must give it one left side that suits both.

Consequences

  • No exact polynomial algorithm, unless P = NP. The tree algorithms do not extend to DAGs. A compiler that wants the cheapest code for a block must accept exponential time in the worst case, or give up the optimum.
  • Splitting into trees. The common answer is to cut the DAG at each shared node, store the shared value in a register, and tile each tree with the tree dynamic program. This is fast, but it fixes the form of each shared value in advance, which is exactly the choice that the proof shows to be hard.
  • Heuristics and exact search. Koes and Goldstein give the NOLTIS heuristic, which tiles the DAG as if it were a tree, then fixes the shared nodes where overlap costs too much, and they report results close to the optimum. Integer programming finds the optimum for small blocks.
  • Register allocation inside a block. Theorem 2 shows that the order of evaluation is already hard on a DAG when there is no memory for spills, while for a tree the Sethi-Ullman numbering finds it in linear time. The NP-completeness of global register allocation is a different result, about coloring the interference graph.

Tightness

  • Trees are easy. If the DAG is a tree, Theorem 4 is solved in linear time for a fixed grammar by the Aho-Johnson dynamic program or by BURS, and Theorem 2 by the Sethi-Ullman numbering. The hardness comes from the sharing.
  • Little sharing is easy. The algorithm of Aho, Johnson and Ullman for the one-register machine is exponential only in the amount of sharing. So for DAGs with a bounded number of shared nodes, it runs in polynomial time.
  • Grammars matter. With one nonterminal and rules that each cover one node, every cover with one pair per node is optimal, and the problem is trivial. The proof needs two nonterminals, TT and FF, so that a shared node must make a choice that suits all its users. Ertl (1999) studied when running the tree dynamic program on the DAG, node by node, still gives an optimum, and gave a test on the grammar for this property. By Theorem 4, no such test can pass for Γ0\Gamma_0, unless P = NP.
  • Fixed grammar. Theorem 4 holds for one fixed grammar of 11 rules, so the hardness does not come from a large machine description.

History

Sethi proved the register sufficiency problem NP-complete in a paper at STOC 1973, and in the SIAM Journal on Computing in 1975. Bruno and Sethi proved the one-register result in the Journal of the ACM in 1976. Aho, Johnson and Ullman presented their results at POPL 1976 and in the Journal of the ACM in 1977, with the analysis of heuristics and the algorithm that is exponential only in the sharing. When code generator generators based on tree grammars spread in the late 1980s and early 1990s, the question came back in the form of tiling. Proebsting wrote a short proof for tiling DAGs in 1995, Ertl described the grammars for which the tree method is still optimal on DAGs in 1999, and Koes and Goldstein published a proof with a near-optimal heuristic in 2008.

References

  1. John Bruno and Ravi Sethi, Code generation for a one-register machine, Journal of the ACM 23(3), 1976, 502-510, the one-register machine and the proof that its shortest program for a DAG is NP-complete to find
  2. A. V. Aho, S. C. Johnson and J. D. Ullman, Code generation for expressions with common subexpressions, Journal of the ACM 24(1), 1977, 146-160, the NP-completeness for simple expressions and machines, the analysis of heuristics, and the algorithm for one-register machines that is exponential only in the amount of sharing
  3. Ravi Sethi, Complete register allocation problems, SIAM Journal on Computing 4(3), 1975, 226-248, the NP-completeness of deciding whether a DAG can be computed with k registers and no stores
  4. M. Anton Ertl, Optimal code selection in DAGs, Proceedings of the 26th ACM Symposium on Principles of Programming Languages (POPL 1999), 242-249, tree parsing on DAGs, and the grammars for which it gives an optimum
  5. David Ryan Koes and Seth Copen Goldstein, Near-optimal instruction selection on DAGs, Proceedings of the 6th IEEE/ACM Symposium on Code Generation and Optimization (CGO 2008), 45-54, the proof that tiling a DAG is NP-complete, by a reduction from 3-SAT, and the NOLTIS heuristic
  6. Todd A. Proebsting, Least-cost instruction selection in DAGs is NP-complete, unpublished note, 1995, the reduction from satisfiability to tiling a DAG with tiles that may overlap

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)
WritingW0 stub
  • the lead paragraph