From 82685aec4167a8c7557b1c1fe0bca44feb7a540b Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 16 Nov 2018 10:44:32 -0800 Subject: wasm-reduce: reduce switch targets (#1752) This tries to reduce by replacing targets with the default, and by shrinking the list of targets. --- src/tools/wasm-reduce.cpp | 23 +++++++++++++++++++++-- 1 file 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 WalkerPasscondition); } else if (auto* sw = curr->dynCast()) { 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()) { if (!shouldTryToReduce()) return; // replace a singleton @@ -445,13 +464,13 @@ struct Reducer : public WalkerPass i; j--) { list[j] = list[j - 1]; -- cgit v1.2.3