CIRCT  19.0.0git
LTLFolds.cpp
Go to the documentation of this file.
1 //===- LTLFolds.cpp -------------------------------------------------------===//
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 
10 #include "circt/Dialect/HW/HWOps.h"
12 #include "mlir/IR/PatternMatch.h"
13 
14 using namespace circt;
15 using namespace ltl;
16 using namespace mlir;
17 
18 /// Check if an attribute is an integer zero.
19 static bool isConstantZero(Attribute attr) {
20  if (!attr)
21  return false;
22  if (auto intAttr = dyn_cast<IntegerAttr>(attr))
23  return intAttr.getValue().isZero();
24  return false;
25 }
26 
27 /// Concatenate two value ranges into a larger range. Useful for declarative
28 /// rewrites.
29 static SmallVector<Value> concatValues(ValueRange a, ValueRange b) {
30  SmallVector<Value> v;
31  v.append(a.begin(), a.end());
32  v.append(b.begin(), b.end());
33  return v;
34 }
35 
36 /// Inline all `ConcatOp`s in a range of values.
37 static SmallVector<Value> flattenConcats(ValueRange values) {
38  SmallVector<Value> flatInputs;
39  for (auto value : values) {
40  if (auto concatOp = value.getDefiningOp<ConcatOp>()) {
41  auto inputs = concatOp.getInputs();
42  flatInputs.append(inputs.begin(), inputs.end());
43  } else {
44  flatInputs.push_back(value);
45  }
46  }
47  return flatInputs;
48 }
49 
50 //===----------------------------------------------------------------------===//
51 // Declarative Rewrites
52 //===----------------------------------------------------------------------===//
53 
54 namespace patterns {
55 #include "circt/Dialect/LTL/LTLFolds.cpp.inc"
56 } // namespace patterns
57 
58 //===----------------------------------------------------------------------===//
59 // DelayOp
60 //===----------------------------------------------------------------------===//
61 
62 OpFoldResult DelayOp::fold(FoldAdaptor adaptor) {
63  // delay(s, 0, 0) -> s
64  if (adaptor.getDelay() == 0 && adaptor.getLength() == 0 &&
65  !isa<SequenceType>(getResult().getType()))
66  return getInput();
67 
68  return {};
69 }
70 
71 void DelayOp::getCanonicalizationPatterns(RewritePatternSet &results,
72  MLIRContext *context) {
73  results.add<patterns::NestedDelays>(results.getContext());
74  results.add<patterns::MoveDelayIntoConcat>(results.getContext());
75 }
76 
77 //===----------------------------------------------------------------------===//
78 // ConcatOp
79 //===----------------------------------------------------------------------===//
80 
81 OpFoldResult ConcatOp::fold(FoldAdaptor adaptor) {
82  // concat(s) -> s
83  if (getInputs().size() == 1)
84  return getInputs()[0];
85 
86  return {};
87 }
88 
89 void ConcatOp::getCanonicalizationPatterns(RewritePatternSet &results,
90  MLIRContext *context) {
91  results.add<patterns::FlattenConcats>(results.getContext());
92 }
93 
94 //===----------------------------------------------------------------------===//
95 // DisableOp
96 //===----------------------------------------------------------------------===//
97 
98 OpFoldResult DisableOp::fold(FoldAdaptor adaptor) {
99  // disable(p, false) -> p
100  if (isConstantZero(adaptor.getCondition()))
101  return getInput();
102 
103  return {};
104 }
static SmallVector< Value > flattenConcats(ValueRange values)
Inline all ConcatOps in a range of values.
Definition: LTLFolds.cpp:37
static SmallVector< Value > concatValues(ValueRange a, ValueRange b)
Concatenate two value ranges into a larger range.
Definition: LTLFolds.cpp:29
llvm::SmallVector< StringAttr > inputs
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21
static bool isConstantZero(Attribute operand)
Determine whether a constant operand is a zero value.
Definition: FoldUtils.h:27
Definition: ltl.py:1