19#include "mlir/IR/PatternMatch.h"
20#include "mlir/Transforms/WalkPatternRewriteDriver.h"
21#include "llvm/Support/LogicalResult.h"
28#define GEN_PASS_DEF_SIMPLIFYTRUTHTABLE
29#include "circt/Dialect/Comb/Passes.h.inc"
37 return llvm::any_of(op->getOperands(), [op](
auto operand) {
38 return operand.getDefiningOp() == op;
44 using OpRewritePattern::OpRewritePattern;
46 LogicalResult matchAndRewrite(TruthTableOp op,
47 PatternRewriter &rewriter)
const override {
51 const auto inputs = op.getInputs();
52 const auto table = op.getLookupTable();
53 size_t numInputs = inputs.size();
56 bool allSame = llvm::all_equal(table);
58 bool firstValue = table[0];
66 APInt truthTable(table.size(), 0);
67 for (
size_t i = 0; i < table.size(); ++i)
68 truthTable.setBitVal(i, table[i]);
74 int dependentInput = -1;
75 unsigned numDependencies = 0;
76 APInt negativeCofactor, positiveCofactor;
78 for (
size_t bitPos = 0; bitPos < numInputs; ++bitPos) {
79 std::tie(negativeCofactor, positiveCofactor) =
83 if (negativeCofactor != positiveCofactor) {
85 dependentInput = numInputs - 1 - bitPos;
89 if (numDependencies > 1)
95 if (numDependencies != 1)
100 size_t bitPositionInTable = numInputs - 1 - dependentInput;
101 size_t idxWhen1 = 1ull << bitPositionInTable;
102 bool isIdentity = table[idxWhen1];
105 if (!isIdentity && numInputs == 1)
109 Value input = inputs[dependentInput];
117 rewriter, op, ValueRange{input}, ArrayRef<bool>{
true,
false});
123class SimplifyTruthTablePass
124 :
public impl::SimplifyTruthTableBase<SimplifyTruthTablePass> {
126 using SimplifyTruthTableBase::SimplifyTruthTableBase;
127 void runOnOperation()
override;
132void SimplifyTruthTablePass::runOnOperation() {
133 Operation *op = getOperation();
134 MLIRContext *
context = op->getContext();
135 RewritePatternSet patterns(
context);
136 patterns.add<SimplifyTruthTable>(
context);
137 walkAndApplyPatterns(op, std::move(patterns));
static bool isOpTriviallyRecursive(Operation *op)
static std::unique_ptr< Context > context
false return failure()
Parse an optional keyword or string and set instance into 'result'.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
static OpTy replaceOpWithNewOpAndCopyNamehint(PatternRewriter &rewriter, Operation *op, Args &&...args)
A wrapper of PatternRewriter::replaceOpWithNewOp to propagate "sv.namehint" attribute.
void replaceOpAndCopyNamehint(PatternRewriter &rewriter, Operation *op, Value newValue)
A wrapper of PatternRewriter::replaceOp to propagate "sv.namehint" attribute.
std::pair< llvm::APInt, llvm::APInt > computeCofactors(const llvm::APInt &f, unsigned numVars, unsigned varIndex)
Compute cofactor of a Boolean function for a given variable.