Algorithm Search⌘K

Problemadvanced

Boolean matching

Also called Boolean match problem, NPN matching.

Decide whether a library cell implements a given function under a permutation and negation of its inputs, as in technology mapping.

Boolean matching asks whether two Boolean functions are the same up to a renaming of the inputs, a negation of some inputs and, in the common NPN form, a negation of the output. In technology mapping, one function is the function of a small part of the circuit and the other is the function of a library cell, and a match means that the cell can replace that part, with the inputs wired in the found order and with inverters where the found negations are. Unlike structural matching, which compares the shape of a pattern with the circuit, Boolean matching compares functions, so it finds a cell however the part is built. The NPN equivalence classes of functions of nn inputs number 1, 2, 4, 14, 222 and 616126 for n=0n = 0 to 55, so matching reduces a library and the set of functions that a mapper meets to a small number of classes. For functions given as truth tables of a few inputs the problem is easy in practice. For functions given as formulas or circuits it is co-NP-hard and lies in Σ2p\Sigma_2^p.

In plain words

A library cell computes a fixed rule of its inputs. A part of a circuit also computes a rule of its inputs. The question is whether the cell can do the part's job if you are free to choose which wire goes to which input of the cell, to put an inverter on some of the wires, and to put an inverter on the cell's output.

If the answer is yes, the cell can replace the part. Many rules look different but become the same after such changes, so a short list of cells can cover many parts.

Definition

Write x=(x1,…,xn)x = (x_1, \ldots, x_n), a permutation π\pi of {1,…,n}\{1, \ldots, n\}, a vector c∈{0,1}nc \in \{0,1\}^n and a bit c0c_0. The NPN transform T=(π,c,c0)T = (\pi, c, c_0) maps a function gg of nn inputs to

(Tg)(x)=c0⊕g(xπ(1)⊕c1,…,xπ(n)⊕cn).(T g)(x) = c_0 \oplus g\big(x_{\pi(1)} \oplus c_1, \ldots, x_{\pi(n)} \oplus c_n\big).

Input. Two functions ff and gg of nn inputs, given as truth tables, as BDDs, or as formulas or circuits.

Feasible solutions. The transforms T=(π,c,c0)T = (\pi, c, c_0) such that f=Tgf = T g. There are n! 2n+1n! \, 2^{n+1} transforms.

Objective. None: the problem is a decision problem, and a transform with f=Tgf = T g is the answer when one exists.

Decision version. Is there a transform TT with f=Tgf = T g? Then ff and gg are NPN-equivalent.

The transforms form a group under composition, so NPN-equivalence is an equivalence relation, and its classes are the NPN classes. The P, NP and PN forms allow only the permutation, the permutation and the input negations, or the permutation and the output negation.

Variants

  • By the transforms. P-matching (permutations only), NP-matching, NPN-matching. The number of classes for n=0,…,5n = 0, \ldots, 5 is:

    nn P classes NP classes NPN classes
    0 2 2 1
    1 4 3 2
    2 12 6 4
    3 80 22 14
    4 3984 402 222
    5 37333248 1228158 616126

    These values were computed for this page by Burnside's lemma over the n! 2n+1n! \, 2^{n+1} transforms, and checked against an exhaustive count of the classes for n≤4n \le 4.

  • Different numbers of inputs. A function ff of m<nm < n inputs matches a cell of nn inputs when the cell, with n−mn - m inputs tied to constants or to other inputs, computes ff up to a transform.

  • With don't-cares. ff is given with a set of input values where its value does not matter, and TgT g must agree with ff only on the care set. The don't-cares come from the rest of the circuit, as in observability don't-cares.

  • Library matching. Given ff and a library, find every cell that matches ff, or the cheapest one. With a canonical form for each class, this becomes a lookup in a table indexed by the form.

  • Canonical form. Given ff, compute a fixed member of its class, for example the smallest truth table in the class, so that two functions are equivalent exactly when their forms are equal. This is a form of canonical labeling.

  • Multiple outputs. Match several functions at once, with one transform of the shared inputs.

Examples

Truth tables below use the convention of the AIG pages: x1x_1 is the high bit of the row index.

  • AND with a negated input. f=a∧¬bf = a \land \neg b and $g = $ AND2. The transform $\pi = $ identity, c=(0,1)c = (0, 1), c0=0c_0 = 0 gives f=Tgf = T g. In a mapping, this is AND2 with an inverter on bb.
  • OR from NAND. f=a∨bf = a \lor b and $g = $ NAND2: a∨b=NAND(¬a,¬b)a \lor b = \mathrm{NAND}(\neg a, \neg b), so c=(1,1)c = (1, 1), c0=0c_0 = 0. They are NP-equivalent.
  • XOR and XNOR. XOR2 has the table 01100110 and XNOR2 has 10011001. They are equivalent by c0=1c_0 = 1, and also by c=(1,0)c = (1, 0), c0=0c_0 = 0.
  • Multiplexer. f(a,b,c)=ab∨¬acf(a, b, c) = a b \lor \neg a c and g(s,x0,x1)=¬sx0∨sx1g(s, x_0, x_1) = \neg s x_0 \lor s x_1 are P-equivalent, with s=as = a, x1=bx_1 = b and x0=cx_0 = c.
  • A non-example. AND3 and the majority function MAJ3 do not match. A change of inputs keeps the number of rows where the function is 1, and a negation of the output turns kk such rows into 2n−k2^n - k. AND3 has 1 such row, so every function in its class has 1 or 7, and MAJ3 has 4.
  • The two-input classes. The 16 functions of two inputs fall into 4 NPN classes: the constants, the single variables, the AND-type functions (8 of them, such as AND, OR, NAND and a∧¬ba \land \neg b) and the XOR-type functions (XOR and XNOR).

Known algorithms

  • Exhaustive search over the n! 2n+1n! \, 2^{n+1} transforms, practical for small nn.
  • Pruning with invariants, values that every transform keeps, such as the number of 1 rows and the numbers of 1 rows of the cofactors of each input, before the search.
  • Canonical forms computed for both functions and compared, with precomputed tables of the classes for n≤4n \le 4, as in AIG rewriting.
  • BDD-based matching, as in Mailhot and De Micheli (1993).
  • SAT or QBF solvers on the formula "there is a transform such that for all inputs the two functions agree".

Bounds

Upper bounds (proved).

  • Truth table input of nn inputs. Each transform is checked in O(n2n)O(n 2^n) steps, so the exhaustive search takes O(n! 22n+1n)O(n! \, 2^{2n+1} n) steps. With N=2nN = 2^n the size of a table, this is NO(log⁡log⁡N)N^{O(\log \log N)}, at most quasi-polynomial in the input size.
  • Formula or circuit input. The decision version is in Σ2p\Sigma_2^p, the second level of the polynomial hierarchy: guess TT, then check for all xx that f(x)=(Tg)(x)f(x) = (T g)(x), a co-NP check.
  • For formulas under a permutation of the variables, Agrawal and Thierauf (2000) showed that the problem is not Σ2p\Sigma_2^p-complete unless the polynomial hierarchy collapses. See their paper for the exact class.

Lower bounds (proved, as hardness).

  • Formula or circuit input: the decision version is co-NP-hard, in all of the P, NP and NPN forms. Proof: a formula FF of nn variables is a tautology exactly when F∨yF \lor y, with a new variable yy, is equivalent to the constant 1 of n+1n + 1 inputs. A transform of a constant is a constant, so a function matches the constant 1 exactly when it is constant. If FF is a tautology, F∨y=1F \lor y = 1. If not, F(a)=0F(a) = 0 for some aa, and F∨yF \lor y is 0 at (a,0)(a, 0) and 1 at (a,1)(a, 1), so it is not constant. The map from FF to F∨yF \lor y takes polynomial time, and tautology is co-NP-complete.
  • Formula input: graph isomorphism reduces to P-matching. A graph with edge set EE gives the monotone formula ⋀uv∈E(xu∨xv)\bigwedge_{uv \in E} (x_u \lor x_v). The clauses are the prime implicates of this monotone function, so the function determines EE, and two such formulas match under a permutation exactly when the graphs are isomorphic.

Measured. Nothing on this page. Mappers report the time spent in matching on their benchmark sets.

Reductions

  • From equivalence checking. Boolean equivalence checking is Boolean matching with TT fixed to the identity, so every hardness result for equivalence checking of circuits carries over, as the proof above shows.
  • To canonical forms. A canonical form for NPN classes reduces matching to two form computations and a comparison, and library matching to one form computation and a table lookup.
  • From graph isomorphism. As above, for formula input.
  • To QBF with two levels. "There is TT such that for all xx, f(x)=(Tg)(x)f(x) = (T g)(x)" is a ∃∀\exists\forall formula once TT is encoded in bits, which shows membership in Σ2p\Sigma_2^p.

Applications

  • Technology mapping into cells. Cut-based mappers compute the truth table of each cut and look up the cells that match it.
  • AIG rewriting. Rewriting looks up the best known AIG for the NPN class of each 4-input cut, from a table of the 222 classes.
  • LUT-based structures. Matching cuts against configurable cells or LUT structures with a fixed internal wiring.
  • Verification with unknown input order. Checking two blocks for equality when the correspondence of their inputs is not known.
  • Exact synthesis databases. Tables of the smallest circuits of all functions of up to 4 or 5 inputs are stored per NPN class, as in exact synthesis.

References

  1. Frederic Mailhot and Giovanni De Micheli, Algorithms for technology mapping based on binary decision diagrams and on Boolean operations, IEEE Transactions on Computer-Aided Design 12(5), 1993, pages 599 to 620; Boolean matching of cluster functions against library cells in technology mapping, with BDDs and don't-cares
  2. The formula isomorphism problem, pages 990 to 1009; formula isomorphism is in Sigma_2^p and is not Sigma_2^p-complete unless the polynomial hierarchy collapses (general locator for the exact statements)
  3. Giovanni De Micheli, Synthesis and Optimization of Digital Circuits, McGraw-Hill, 1994, chapter 10, Boolean matching in library binding (general locator, not checked)

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.

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