From 9592b881bd1d17dfa24cfee5aea31f6f9d8312d5 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 25 Aug 2017 13:48:29 -0700 Subject: fix flow of values stopping in remove-unused-brs: we must remove all flows with a value from the current state, not just set the global state as to whether we can flow or not (as it will be set later by other things) --- src/passes/RemoveUnusedBrs.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index a3ef15639..e627ce138 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -74,7 +74,7 @@ struct RemoveUnusedBrs : public WalkerPass> { flows.push_back(currp); self->valueCanFlow = true; // start optimistic } else { - self->valueCanFlow = false; + self->stopValueFlow(); } } else if (curr->is()) { flows.clear(); @@ -84,6 +84,7 @@ struct RemoveUnusedBrs : public WalkerPass> { auto* iff = curr->cast(); if (iff->condition->type == unreachable) { // avoid trying to optimize this, we never reach it anyhow + self->stopFlow(); return; } if (iff->ifFalse) { @@ -94,7 +95,7 @@ struct RemoveUnusedBrs : public WalkerPass> { self->ifStack.pop_back(); } else { // if without else stops the flow of values - self->valueCanFlow = false; + self->stopValueFlow(); } } else if (curr->is()) { // any breaks flowing to here are unnecessary, as we get here anyhow @@ -132,16 +133,31 @@ struct RemoveUnusedBrs : public WalkerPass> { } } else if (curr->is()) { // ignore (could be result of a previous cycle) - self->valueCanFlow = false; + self->stopValueFlow(); } else if (curr->is()) { // 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 ret->value; + } + return curr->cast()->value; + }), flows.end()); + valueCanFlow = false; + } + static void clear(RemoveUnusedBrs* self, Expression** currp) { self->flows.clear(); } -- cgit v1.2.3