summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2018-11-16 10:44:32 -0800
committerGitHub <noreply@github.com>2018-11-16 10:44:32 -0800
commit82685aec4167a8c7557b1c1fe0bca44feb7a540b (patch)
treeb08df0a50ee5bab275e25574b1a604c5de916767
parentc0cf612ccab6b45867b2a4ab3e3565d7dd1b741a (diff)
downloadbinaryen-82685aec4167a8c7557b1c1fe0bca44feb7a540b.tar.gz
binaryen-82685aec4167a8c7557b1c1fe0bca44feb7a540b.tar.bz2
binaryen-82685aec4167a8c7557b1c1fe0bca44feb7a540b.zip
wasm-reduce: reduce switch targets (#1752)
This tries to reduce by replacing targets with the default, and by shrinking the list of targets.
-rw-r--r--src/tools/wasm-reduce.cpp23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp
index bee6ba621..3c507bd20 100644
--- a/src/tools/wasm-reduce.cpp
+++ b/src/tools/wasm-reduce.cpp
@@ -429,6 +429,25 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
handleCondition(select->condition);
} else if (auto* sw = curr->dynCast<Switch>()) {
handleCondition(sw->condition);
+ // Try to replace switch targets with the default
+ for (auto& target : sw->targets) {
+ if (target != sw->default_) {
+ auto old = target;
+ target = sw->default_;
+ if (!tryToReplaceCurrent(curr)) {
+ target = old;
+ }
+ }
+ }
+ // Try to shorten the list of targets.
+ while (sw->targets.size() > 1) {
+ auto last = sw->targets.back();
+ sw->targets.pop_back();
+ if (!tryToReplaceCurrent(curr)) {
+ sw->targets.push_back(last);
+ break;
+ }
+ }
} else if (auto* block = curr->dynCast<Block>()) {
if (!shouldTryToReduce()) return;
// replace a singleton
@@ -445,13 +464,13 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
for (Index j = i; j < list.size() - 1; j++) {
list[j] = list[j + 1];
}
- list.resize(list.size() - 1);
+ list.pop_back();
if (writeAndTestReduction()) {
std::cerr << "| block-nop removed\n";
noteReduction();
return;
}
- list.resize(list.size() + 1);
+ list.push_back(nullptr);
// we failed; undo
for (Index j = list.size() - 1; j > i; j--) {
list[j] = list[j - 1];