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 , it is NP-complete to decide whether some program of at most instructions computes all the roots of the DAG.
Theorem 2 (Sethi, 1975). Take a machine with 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 , 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 , it is NP-complete to decide whether the DAG has a cover of cost at most . This holds even for the fixed grammar 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 , and rules with a cost, where is a pattern: a tree of operators whose leaves are nonterminals. A cover of a DAG is a set of pairs , where the pattern of rule matches at node , such that:
- each root of the DAG has a pair whose rule has the left side ;
- for each pair and each nonterminal leaf of the pattern of , matched at a node , there is a pair whose rule has the left side .
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 . The nonterminals are , and . The operators are a leaf , a unary NOT, and a ternary OR3. The rules, each of cost 1, are
and for each of the 7 triples other than .
Membership in NP. A cover never needs the same pair twice. A pattern that matches at is placed by alone, so there are at most pairs for a DAG with nodes and a grammar with 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 have the variables , each of which occurs in some clause, and the clauses . Build the DAG :
- a node with the operator , for each variable ;
- a node , for each variable that occurs negated in some clause;
- a node for each clause, where the child for the literal is and the child for is .
Let be the number of nodes , and let , which is the number of nodes. The roots are the nodes . The construction takes linear time.
Lemma 1. Each cover of has cost at least , with equality only if each node has exactly one pair.
Proof. Each is a root, so it has a pair by condition 1. Each is a child of some , and every OR3 rule has a nonterminal at each child, so has a pair by condition 2. Each is a child of some or of , and the rules of OR3 and NOT have a nonterminal at each child, so has a pair. So each of the nodes has at least one pair, and each pair costs 1.
If is satisfiable, there is a cover of cost . Let be a satisfying assignment. Give the pair with the rule if is true and if it is false. Give the rule if is true and if it is false. Then each node or has the left side exactly when its literal is true, and the rule at finds the nonterminal that it needs at . Give the rule , where is the left side at its -th child. Since satisfies , some is , so the triple is not and the rule exists. Conditions 1 and 2 hold, and there is one pair per node, so the cost is .
If there is a cover of cost at most , then is satisfiable. By Lemma 1, each node has exactly one pair. Let be true if the rule at has the left side , and false otherwise. The only pair at is or . In the first case, condition 2 needs the left side at , so is true, and has the left side . In the second case, has , so is false, and has . So in both cases each literal node has the left side exactly when its literal is true under . The only pair at is , and by condition 2 each is the only left side at the -th child. The triple is not , so some literal of is true. So satisfies every clause.
So is satisfiable if and only if has a cover of cost at most . With membership in NP, this proves Theorem 4.
For example, the clause alone gives the nodes , , , and , and . The assignment with true gives the cover at , any one rule at with the matching rule at , any one rule at , and the OR3 rule with first. If a second clause is added, is shared by both clauses through , and the cover of cost 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, and , 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 , 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
- 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
- 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
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W0 stub |
|
Tag 450AQ5 Edit this page History Report an error Markdown