17 #include "mlir/IR/ImplicitLocOpBuilder.h"
18 #include "mlir/Pass/Pass.h"
22 #define GEN_PASS_DEF_LOWERMATCHES
23 #include "circt/Dialect/FIRRTL/Passes.h.inc"
27 using namespace circt;
28 using namespace firrtl;
31 class LowerMatchesPass
32 :
public circt::firrtl::impl::LowerMatchesBase<LowerMatchesPass> {
33 void runOnOperation()
override;
40 auto numCases = match->getNumRegions();
46 ImplicitLocOpBuilder b(match.getLoc(), match);
47 auto input = match.getInput();
48 for (
size_t i = 0; i < numCases - 1; ++i) {
50 auto condition = b.create<IsTagOp>(input, match.getFieldIndexAttr(i));
51 auto when = b.create<WhenOp>(condition,
true);
54 auto *thenBlock = &when.getThenBlock();
55 auto *caseBlock = &match.getRegion(i).front();
56 caseBlock->moveBefore(thenBlock);
60 b.setInsertionPointToStart(caseBlock);
61 auto data = b.create<SubtagOp>(input, match.getFieldIndexAttr(i));
62 caseBlock->getArgument(0).replaceAllUsesWith(
data);
63 caseBlock->eraseArgument(0);
66 b.setInsertionPointToStart(&when.getElseBlock());
73 auto data = b.create<SubtagOp>(input, match.getFieldIndexAttr(numCases - 1));
77 auto *caseBlock = &match->getRegions().back().front();
78 caseBlock->getArgument(0).replaceAllUsesWith(
data);
79 auto *defaultBlock = b.getInsertionBlock();
80 defaultBlock->getOperations().splice(b.getInsertionPoint(),
81 caseBlock->getOperations());
85 void LowerMatchesPass::runOnOperation() {
87 getOperation()->walk([&changed](MatchOp op) {
92 return markAllAnalysesPreserved();
93 markAnalysesPreserved<InstanceGraph>();
97 return std::make_unique<LowerMatchesPass>();
static void lowerMatch(MatchOp match)
std::unique_ptr< mlir::Pass > createLowerMatchesPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.