Loading [MathJax]/extensions/tex2jax.js
CIRCT 22.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NPNClass.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This header file defines NPN (Negation-Permutation-Negation) canonical forms
10// and binary truth tables for boolean function representation and equivalence
11// checking in combinational logic optimization.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CIRCT_SUPPORT_NPNCLASS_H
16#define CIRCT_SUPPORT_NPNCLASS_H
17
18#include "circt/Support/LLVM.h"
19#include "llvm/ADT/APInt.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/Support/raw_ostream.h"
23
24namespace circt {
25
26/// Represents a boolean function as a truth table.
27///
28/// A truth table stores the output values for all possible input combinations
29/// of a boolean function. For a function with n inputs and m outputs, the
30/// truth table contains 2^n entries, each with m output bits.
31///
32/// Example: For a 2-input AND gate:
33/// - Input 00 -> Output 0
34/// - Input 01 -> Output 0
35/// - Input 10 -> Output 0
36/// - Input 11 -> Output 1
37/// This would be stored as the bit pattern 0001 in the truth table.
39 unsigned numInputs; ///< Number of inputs for this boolean function
40 unsigned numOutputs; ///< Number of outputs for this boolean function
41 llvm::APInt table; ///< Truth table data as a packed bit vector
42
43 /// Default constructor creates an empty truth table.
45
46 /// Constructor for a truth table with given dimensions and evaluation data.
48 const llvm::APInt &table)
50 assert(table.getBitWidth() == (1u << numInputs) * numOutputs &&
51 "Truth table size mismatch");
52 }
53
54 /// Constructor for a truth table with given dimensions, initialized to zero.
58
59 /// Get the output value for a given input combination.
60 llvm::APInt getOutput(const llvm::APInt &input) const;
61
62 /// Set the output value for a given input combination.
63 void setOutput(const llvm::APInt &input, const llvm::APInt &output);
64
65 /// Apply input permutation to create a new truth table.
66 /// This reorders the input variables according to the given permutation.
67 BinaryTruthTable applyPermutation(ArrayRef<unsigned> permutation) const;
68
69 /// Apply input negation to create a new truth table.
70 /// This negates selected input variables based on the mask.
71 BinaryTruthTable applyInputNegation(unsigned mask) const;
72
73 /// Apply output negation to create a new truth table.
74 /// This negates selected output variables based on the mask.
75 BinaryTruthTable applyOutputNegation(unsigned negation) const;
76
77 /// Check if this truth table is lexicographically smaller than another.
78 /// Used for canonical ordering of truth tables.
79 bool isLexicographicallySmaller(const BinaryTruthTable &other) const;
80
81 /// Equality comparison for truth tables.
82 bool operator==(const BinaryTruthTable &other) const;
83
84 /// Debug dump method for truth tables.
85 void dump(llvm::raw_ostream &os = llvm::errs()) const;
86};
87
88/// Represents the canonical form of a boolean function under NPN equivalence.
89///
90/// NPN (Negation-Permutation-Negation) equivalence considers two boolean
91/// functions equivalent if one can be obtained from the other by:
92/// 1. Negating some inputs (pre-negation)
93/// 2. Permuting the inputs
94/// 3. Negating some outputs (post-negation)
95///
96/// Example: The function ab+c is NPN equivalent to !a + b(!c) since we can
97/// transform the first function into the second by negating inputs 'a' and 'c',
98/// then reordering the inputs appropriately.
99///
100/// This canonical form is used to efficiently match cuts against library
101/// patterns, as functions in the same NPN class can be implemented by the
102/// same circuit with appropriate input/output inversions.
103struct NPNClass {
104 BinaryTruthTable truthTable; ///< Canonical truth table
105 llvm::SmallVector<unsigned> inputPermutation; ///< Input permutation applied
106 unsigned inputNegation = 0; ///< Input negation mask
107 unsigned outputNegation = 0; ///< Output negation mask
108
109 /// Default constructor creates an empty NPN class.
110 NPNClass() = default;
111
112 /// Constructor from a truth table.
114
115 NPNClass(const BinaryTruthTable &tt, llvm::SmallVector<unsigned> inputPerm,
116 unsigned inputNeg, unsigned outputNeg)
117 : truthTable(tt), inputPermutation(std::move(inputPerm)),
118 inputNegation(inputNeg), outputNegation(outputNeg) {}
119
120 /// Compute the canonical NPN form for a given truth table.
121 ///
122 /// This method exhaustively tries all possible input permutations and
123 /// negations to find the lexicographically smallest canonical form.
124 ///
125 /// FIXME: Currently we are using exact canonicalization which doesn't scale
126 /// well. For larger truth tables, semi-canonical forms should be used
127 /// instead.
129
130 /// Get input permutation from this NPN class to another equivalent NPN class.
131 ///
132 /// When two NPN classes are equivalent, they may have different input
133 /// permutations. This function computes a permutation that allows
134 /// transforming input indices from the target NPN class to input indices of
135 /// this NPN class.
136 ///
137 /// Returns a permutation vector where result[i] gives the input index in this
138 /// NPN class that corresponds to input i in the target NPN class.
139 ///
140 /// Example: If this has permutation [2,0,1] and target has [1,2,0],
141 /// the mapping allows connecting target inputs to this inputs correctly.
142 void getInputPermutation(const NPNClass &targetNPN,
143 llvm::SmallVectorImpl<unsigned> &permutation) const;
144
145 /// Equality comparison for NPN classes.
146 bool equivalentOtherThanPermutation(const NPNClass &other) const {
147 return truthTable == other.truthTable &&
148 inputNegation == other.inputNegation &&
150 }
151
152 bool isLexicographicallySmaller(const NPNClass &other) const {
153 if (truthTable.table != other.truthTable.table)
155 if (inputNegation != other.inputNegation)
156 return inputNegation < other.inputNegation;
157 return outputNegation < other.outputNegation;
158 }
159
160 /// Debug dump method for NPN classes.
161 void dump(llvm::raw_ostream &os = llvm::errs()) const;
162};
163
164} // namespace circt
165
166#endif // CIRCT_SUPPORT_NPNCLASS_H
assert(baseType &&"element must be base type")
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Represents a boolean function as a truth table.
Definition NPNClass.h:38
BinaryTruthTable()
Default constructor creates an empty truth table.
Definition NPNClass.h:44
BinaryTruthTable(unsigned numInputs, unsigned numOutputs)
Constructor for a truth table with given dimensions, initialized to zero.
Definition NPNClass.h:55
BinaryTruthTable applyOutputNegation(unsigned negation) const
Apply output negation to create a new truth table.
Definition NPNClass.cpp:79
llvm::APInt table
Truth table data as a packed bit vector.
Definition NPNClass.h:41
BinaryTruthTable applyInputNegation(unsigned mask) const
Apply input negation to create a new truth table.
Definition NPNClass.cpp:63
unsigned numInputs
Number of inputs for this boolean function.
Definition NPNClass.h:39
BinaryTruthTable(unsigned numInputs, unsigned numOutputs, const llvm::APInt &table)
Constructor for a truth table with given dimensions and evaluation data.
Definition NPNClass.h:47
void dump(llvm::raw_ostream &os=llvm::errs()) const
Debug dump method for truth tables.
Definition NPNClass.cpp:106
bool isLexicographicallySmaller(const BinaryTruthTable &other) const
Check if this truth table is lexicographically smaller than another.
Definition NPNClass.cpp:95
bool operator==(const BinaryTruthTable &other) const
Equality comparison for truth tables.
Definition NPNClass.cpp:101
llvm::APInt getOutput(const llvm::APInt &input) const
Get the output value for a given input combination.
Definition NPNClass.cpp:27
BinaryTruthTable applyPermutation(ArrayRef< unsigned > permutation) const
Apply input permutation to create a new truth table.
Definition NPNClass.cpp:43
void setOutput(const llvm::APInt &input, const llvm::APInt &output)
Set the output value for a given input combination.
Definition NPNClass.cpp:32
unsigned numOutputs
Number of outputs for this boolean function.
Definition NPNClass.h:40
Represents the canonical form of a boolean function under NPN equivalence.
Definition NPNClass.h:103
NPNClass(const BinaryTruthTable &tt)
Constructor from a truth table.
Definition NPNClass.h:113
unsigned outputNegation
Output negation mask.
Definition NPNClass.h:107
static NPNClass computeNPNCanonicalForm(const BinaryTruthTable &tt)
Compute the canonical NPN form for a given truth table.
Definition NPNClass.cpp:210
void getInputPermutation(const NPNClass &targetNPN, llvm::SmallVectorImpl< unsigned > &permutation) const
Get input permutation from this NPN class to another equivalent NPN class.
Definition NPNClass.cpp:187
BinaryTruthTable truthTable
Canonical truth table.
Definition NPNClass.h:104
void dump(llvm::raw_ostream &os=llvm::errs()) const
Debug dump method for NPN classes.
Definition NPNClass.cpp:253
NPNClass()=default
Default constructor creates an empty NPN class.
bool equivalentOtherThanPermutation(const NPNClass &other) const
Equality comparison for NPN classes.
Definition NPNClass.h:146
llvm::SmallVector< unsigned > inputPermutation
Input permutation applied.
Definition NPNClass.h:105
unsigned inputNegation
Input negation mask.
Definition NPNClass.h:106
bool isLexicographicallySmaller(const NPNClass &other) const
Definition NPNClass.h:152
NPNClass(const BinaryTruthTable &tt, llvm::SmallVector< unsigned > inputPerm, unsigned inputNeg, unsigned outputNeg)
Definition NPNClass.h:115