17 #include "mlir/Pass/Pass.h"
21 #define GEN_PASS_DEF_LOWERMATCHES
22 #include "circt/Dialect/FIRRTL/Passes.h.inc"
26 using namespace circt;
27 using namespace firrtl;
30 class LowerMatchesPass
31 :
public circt::firrtl::impl::LowerMatchesBase<LowerMatchesPass> {
32 void runOnOperation()
override;
39 auto numCases = match->getNumRegions();
45 ImplicitLocOpBuilder b(match.getLoc(), match);
46 auto input = match.getInput();
47 for (
size_t i = 0; i < numCases - 1; ++i) {
49 auto condition = b.create<IsTagOp>(input, match.getFieldIndexAttr(i));
50 auto when = b.create<WhenOp>(condition,
true);
53 auto *thenBlock = &when.getThenBlock();
54 auto *caseBlock = &match.getRegion(i).front();
55 caseBlock->moveBefore(thenBlock);
59 b.setInsertionPointToStart(caseBlock);
60 auto data = b.create<SubtagOp>(input, match.getFieldIndexAttr(i));
61 caseBlock->getArgument(0).replaceAllUsesWith(
data);
62 caseBlock->eraseArgument(0);
65 b.setInsertionPointToStart(&when.getElseBlock());
72 auto data = b.create<SubtagOp>(input, match.getFieldIndexAttr(numCases - 1));
76 auto *caseBlock = &match->getRegions().back().front();
77 caseBlock->getArgument(0).replaceAllUsesWith(
data);
78 auto *defaultBlock = b.getInsertionBlock();
79 defaultBlock->getOperations().splice(b.getInsertionPoint(),
80 caseBlock->getOperations());
84 void LowerMatchesPass::runOnOperation() {
86 getOperation()->walk([&changed](MatchOp op) {
91 return markAllAnalysesPreserved();
92 markAnalysesPreserved<InstanceGraph>();
96 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.