diff options
author | Alon Zakai <alonzakai@gmail.com> | 2017-08-27 08:18:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-27 08:18:32 -0700 |
commit | 8c89a7baae740013b038865fc4e290439b91eb6f (patch) | |
tree | 5fe7c1b6d559040740ee8387efed0dbb6c0264b7 /src/passes/RemoveUnusedBrs.cpp | |
parent | e60fcd0ba97ed75440c6f838619455be7a5e90a3 (diff) | |
parent | 9592b881bd1d17dfa24cfee5aea31f6f9d8312d5 (diff) | |
download | binaryen-8c89a7baae740013b038865fc4e290439b91eb6f.tar.gz binaryen-8c89a7baae740013b038865fc4e290439b91eb6f.tar.bz2 binaryen-8c89a7baae740013b038865fc4e290439b91eb6f.zip |
Merge pull request #1147 from WebAssembly/betterfuzz
Fuzzer improvements and some small fixes
Diffstat (limited to 'src/passes/RemoveUnusedBrs.cpp')
-rw-r--r-- | src/passes/RemoveUnusedBrs.cpp | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index 8994c7fe2..e627ce138 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -31,6 +31,8 @@ namespace wasm { // condition and possible value, and the possible value must // not have side effects (as they would run unconditionally) static bool canTurnIfIntoBrIf(Expression* ifCondition, Expression* brValue, PassOptions& options) { + // if the if isn't even taken, this is all dead code anyhow + if (ifCondition->type == unreachable) return false; if (!brValue) return true; EffectAnalyzer value(options, brValue); if (value.hasSideEffects()) return false; @@ -72,7 +74,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { flows.push_back(currp); self->valueCanFlow = true; // start optimistic } else { - self->valueCanFlow = false; + self->stopValueFlow(); } } else if (curr->is<Return>()) { flows.clear(); @@ -80,6 +82,11 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { self->valueCanFlow = true; // start optimistic } else if (curr->is<If>()) { auto* iff = curr->cast<If>(); + if (iff->condition->type == unreachable) { + // avoid trying to optimize this, we never reach it anyhow + self->stopFlow(); + return; + } if (iff->ifFalse) { assert(self->ifStack.size() > 0); for (auto* flow : self->ifStack.back()) { @@ -88,7 +95,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { self->ifStack.pop_back(); } else { // if without else stops the flow of values - self->valueCanFlow = false; + self->stopValueFlow(); } } else if (curr->is<Block>()) { // any breaks flowing to here are unnecessary, as we get here anyhow @@ -126,16 +133,31 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { } } else if (curr->is<Nop>()) { // ignore (could be result of a previous cycle) - self->valueCanFlow = false; + self->stopValueFlow(); } else if (curr->is<Loop>()) { // do nothing - it's ok for values to flow out } else { // anything else stops the flow - flows.clear(); - self->valueCanFlow = false; + self->stopFlow(); } } + void stopFlow() { + flows.clear(); + valueCanFlow = false; + } + + void stopValueFlow() { + flows.erase(std::remove_if(flows.begin(), flows.end(), [&](Expression** currp) { + auto* curr = *currp; + if (auto* ret = curr->dynCast<Return>()) { + return ret->value; + } + return curr->cast<Break>()->value; + }), flows.end()); + valueCanFlow = false; + } + static void clear(RemoveUnusedBrs* self, Expression** currp) { self->flows.clear(); } @@ -171,6 +193,10 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { auto* iff = (*currp)->dynCast<If>(); if (iff) { + if (iff->condition->type == unreachable) { + // avoid trying to optimize this, we never reach it anyhow + return; + } self->pushTask(doVisitIf, currp); if (iff->ifFalse) { // we need to join up if-else control flow, and clear after the condition |