Skip to content

Commit 82685ae

Browse files
authored
wasm-reduce: reduce switch targets (#1752)
This tries to reduce by replacing targets with the default, and by shrinking the list of targets.
1 parent c0cf612 commit 82685ae

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/tools/wasm-reduce.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,25 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
429429
handleCondition(select->condition);
430430
} else if (auto* sw = curr->dynCast<Switch>()) {
431431
handleCondition(sw->condition);
432+
// Try to replace switch targets with the default
433+
for (auto& target : sw->targets) {
434+
if (target != sw->default_) {
435+
auto old = target;
436+
target = sw->default_;
437+
if (!tryToReplaceCurrent(curr)) {
438+
target = old;
439+
}
440+
}
441+
}
442+
// Try to shorten the list of targets.
443+
while (sw->targets.size() > 1) {
444+
auto last = sw->targets.back();
445+
sw->targets.pop_back();
446+
if (!tryToReplaceCurrent(curr)) {
447+
sw->targets.push_back(last);
448+
break;
449+
}
450+
}
432451
} else if (auto* block = curr->dynCast<Block>()) {
433452
if (!shouldTryToReduce()) return;
434453
// replace a singleton
@@ -445,13 +464,13 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
445464
for (Index j = i; j < list.size() - 1; j++) {
446465
list[j] = list[j + 1];
447466
}
448-
list.resize(list.size() - 1);
467+
list.pop_back();
449468
if (writeAndTestReduction()) {
450469
std::cerr << "| block-nop removed\n";
451470
noteReduction();
452471
return;
453472
}
454-
list.resize(list.size() + 1);
473+
list.push_back(nullptr);
455474
// we failed; undo
456475
for (Index j = list.size() - 1; j > i; j--) {
457476
list[j] = list[j - 1];

0 commit comments

Comments
 (0)