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 /// Concatenate two value ranges into a larger range. Useful for declarative
19 /// rewrites.
20 static SmallVector<Value> concatValues(ValueRange a, ValueRange b) {
21  SmallVector<Value> v;
22  v.append(a.begin(), a.end());
23  v.append(b.begin(), b.end());
24  return v;
25 }
26 
27 /// Inline all `ConcatOp`s in a range of values.
28 static SmallVector<Value> flattenConcats(ValueRange values) {
29  SmallVector<Value> flatInputs;
30  for (auto value : values) {
31  if (auto concatOp = value.getDefiningOp<ConcatOp>()) {
32  auto inputs = concatOp.getInputs();
33  flatInputs.append(inputs.begin(), inputs.end());
34  } else {
35  flatInputs.push_back(value);
36  }
37  }
38  return flatInputs;
39 }
40 
41 //===----------------------------------------------------------------------===//
42 // Declarative Rewrites
43 //===----------------------------------------------------------------------===//
44 
45 namespace patterns {
46 #include "circt/Dialect/LTL/LTLFolds.cpp.inc"
47 } // namespace patterns
48 
49 //===----------------------------------------------------------------------===//
50 // DelayOp
51 //===----------------------------------------------------------------------===//
52 
53 OpFoldResult DelayOp::fold(FoldAdaptor adaptor) {
54  // delay(s, 0, 0) -> s
55  if (adaptor.getDelay() == 0 && adaptor.getLength() == 0 &&
56  isa<SequenceType>(getInput().getType()))
57  return getInput();
58 
59  return {};
60 }
61 
62 void DelayOp::getCanonicalizationPatterns(RewritePatternSet &results,
63  MLIRContext *context) {
64  results.add<patterns::NestedDelays>(results.getContext());
65  results.add<patterns::MoveDelayIntoConcat>(results.getContext());
66 }
67 
68 //===----------------------------------------------------------------------===//
69 // ConcatOp
70 //===----------------------------------------------------------------------===//
71 
72 OpFoldResult ConcatOp::fold(FoldAdaptor adaptor) {
73  // concat(s) -> s
74  if (getInputs().size() == 1)
75  return getInputs()[0];
76 
77  return {};
78 }
79 
80 void ConcatOp::getCanonicalizationPatterns(RewritePatternSet &results,
81  MLIRContext *context) {
82  results.add<patterns::FlattenConcats>(results.getContext());
83 }
84 
85 //===----------------------------------------------------------------------===//
86 // RepeatLikeOps
87 //===----------------------------------------------------------------------===//
88 
89 namespace {
90 struct RepeatLikeOp {
91  static OpFoldResult fold(uint64_t base, uint64_t more, Value input) {
92  // repeat(s, 1, 0) -> s
93  if (base == 1 && more == 0 && isa<SequenceType>(input.getType()))
94  return input;
95 
96  return {};
97  }
98 };
99 } // namespace
100 
101 OpFoldResult RepeatOp::fold(FoldAdaptor adaptor) {
102  auto more = adaptor.getMore();
103  if (more.has_value())
104  return RepeatLikeOp::fold(adaptor.getBase(), *more, getInput());
105  return {};
106 }
107 
108 OpFoldResult GoToRepeatOp::fold(FoldAdaptor adaptor) {
109  return RepeatLikeOp::fold(adaptor.getBase(), adaptor.getMore(), getInput());
110 }
111 
112 OpFoldResult NonConsecutiveRepeatOp::fold(FoldAdaptor adaptor) {
113  return RepeatLikeOp::fold(adaptor.getBase(), adaptor.getMore(), getInput());
114 }
static SmallVector< Value > flattenConcats(ValueRange values)
Inline all ConcatOps in a range of values.
Definition: LTLFolds.cpp:28
static SmallVector< Value > concatValues(ValueRange a, ValueRange b)
Concatenate two value ranges into a larger range.
Definition: LTLFolds.cpp:20
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: ltl.py:1