From fa60ade30e03c6a13bbce26ff81c03ed1ae4da0b Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 20 Jul 2016 20:05:28 -0700 Subject: more RemoveUnusedName opts: merge names when possible, and do block/loop merging based on their names --- src/passes/RemoveUnusedNames.cpp | 47 +++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/passes/RemoveUnusedNames.cpp b/src/passes/RemoveUnusedNames.cpp index 5145cc744..9c6743479 100644 --- a/src/passes/RemoveUnusedNames.cpp +++ b/src/passes/RemoveUnusedNames.cpp @@ -15,7 +15,8 @@ */ // -// Removes names from locations that are never branched to. +// Removes names from locations that are never branched to, and +// merge names when possible (by merging their blocks) // #include @@ -30,17 +31,17 @@ struct RemoveUnusedNames : public WalkerPass branchesSeen; + std::map> branchesSeen; void visitBreak(Break *curr) { - branchesSeen.insert(curr->name); + branchesSeen[curr->name].insert(curr); } void visitSwitch(Switch *curr) { for (auto name : curr->targets) { - branchesSeen.insert(name); + branchesSeen[name].insert(curr); } - branchesSeen.insert(curr->default_); + branchesSeen[curr->default_].insert(curr); } void handleBreakTarget(Name& name) { @@ -54,7 +55,35 @@ struct RemoveUnusedNames : public WalkerPassname.is() && curr->list.size() == 1) { + auto* child = curr->list[0]->dynCast(); + if (child && child->name.is()) { + // we have just one child, this block, so breaking out of it goes to the same place as breaking out of us, we just need one name (and block) + auto& branches = branchesSeen[curr->name]; + for (auto* branch : branches) { + if (Break* br = branch->dynCast()) { + if (br->name == curr->name) br->name = child->name; + } else if (Switch* sw = branch->dynCast()) { + for (auto& target : sw->targets) { + if (target == curr->name) target = child->name; + } + if (sw->default_ == curr->name) sw->default_ = child->name; + } else { + WASM_UNREACHABLE(); + } + } + replaceCurrent(child); + } + } handleBreakTarget(curr->name); + if (curr->name.is() && curr->list.size() == 1) { + auto* child = curr->list[0]->dynCast(); + if (child && !child->out.is()) { + // we have just one child, this loop, and it lacks an out label. So this block's name is doing just that! + child->out = curr->name; + replaceCurrent(child); + } + } } void visitLoop(Loop *curr) { @@ -68,6 +97,14 @@ struct RemoveUnusedNames : public WalkerPasslist.push_back(curr->body); replaceCurrent(block); } + if (curr->in.is() && !curr->out.is()) { + auto* child = curr->body->dynCast(); + if (child && child->name.is()) { + // we have just one child, this block, and we lack an out label. So we can take the block's! + curr->out = child->name; + child->name = Name(); + } + } } void visitFunction(Function *curr) { -- cgit v1.2.3