35 ConversionPatternRewriter &rewriter)
const override {
36 auto loc = switchOp.getLoc();
38 Region &defaultRegion = switchOp.getDefaultRegion();
39 bool hasResults = !switchOp.getResultTypes().empty();
42 scf::IfOp prevIfOp =
nullptr;
44 rewriter.setInsertionPointAfter(switchOp);
45 auto switchCases = switchOp.getCases();
46 for (
size_t i = 0; i < switchCases.size(); i++) {
47 auto caseValueInt = switchCases[i];
49 rewriter.setInsertionPointToStart(&prevIfOp.getElseRegion().front());
52 rewriter.create<arith::ConstantIndexOp>(loc, caseValueInt);
53 Value cond = rewriter.create<arith::CmpIOp>(
54 loc, arith::CmpIPredicate::eq, switchOp.getOperand(), caseValue);
56 auto ifOp = rewriter.create<scf::IfOp>(loc, switchOp.getResultTypes(),
59 Region &caseRegion = switchOp.getCaseRegions()[i];
60 rewriter.eraseBlock(&ifOp.getThenRegion().front());
61 rewriter.inlineRegionBefore(caseRegion, ifOp.getThenRegion(),
62 ifOp.getThenRegion().end());
64 if (i + 1 == switchCases.size()) {
65 rewriter.eraseBlock(&ifOp.getElseRegion().front());
66 rewriter.inlineRegionBefore(defaultRegion, ifOp.getElseRegion(),
67 ifOp.getElseRegion().end());
70 if (prevIfOp && hasResults) {
71 rewriter.setInsertionPointToEnd(&prevIfOp.getElseRegion().front());
72 rewriter.create<scf::YieldOp>(loc, ifOp.getResult(0));
75 if (i == 0 && hasResults)
76 finalResult = ifOp.getResult(0);
82 rewriter.replaceOp(switchOp, finalResult);
84 rewriter.eraseOp(switchOp);