diff options
author | Alon Zakai <azakai@google.com> | 2019-05-31 19:56:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-31 19:56:56 -0700 |
commit | c0fba4a898273b9998c7ce09a3663bb35271dbd7 (patch) | |
tree | 5375701ee3cae9dc738401b7950046b576052fc6 | |
parent | c5b50feac3e1faeb51c6910e37357596e4700717 (diff) | |
download | binaryen-c0fba4a898273b9998c7ce09a3663bb35271dbd7.tar.gz binaryen-c0fba4a898273b9998c7ce09a3663bb35271dbd7.tar.bz2 binaryen-c0fba4a898273b9998c7ce09a3663bb35271dbd7.zip |
Un-recursify OptimizeInstructions::optimizeAddedConstants (#2157)
This helps avoid issues with smaller stack sizes on some OSes.
Should fix the last Mac test failure on emscripten-releases CI (other.test_js_function_names_are_minified, which happens to have massively-nested additions of constants).
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index cf764ac49..f8fc21913 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -986,46 +986,57 @@ private: Expression* optimizeAddedConstants(Binary* binary) { uint32_t constant = 0; std::vector<Const*> constants; - std::function<void(Expression*, int)> seek = [&](Expression* curr, - int mul) { + + struct SeekState { + Expression* curr; + int mul; + SeekState(Expression* curr, int mul) : curr(curr), mul(mul) {} + }; + std::vector<SeekState> seekStack; + seekStack.emplace_back(binary, 1); + while (!seekStack.empty()) { + auto state = seekStack.back(); + seekStack.pop_back(); + auto curr = state.curr; + auto mul = state.mul; if (auto* c = curr->dynCast<Const>()) { uint32_t value = c->value.geti32(); if (value != 0) { constant += value * mul; constants.push_back(c); } - return; + continue; } else if (auto* binary = curr->dynCast<Binary>()) { if (binary->op == AddInt32) { - seek(binary->left, mul); - seek(binary->right, mul); - return; + seekStack.emplace_back(binary->right, mul); + seekStack.emplace_back(binary->left, mul); + continue; } else if (binary->op == SubInt32) { // if the left is a zero, ignore it, it's how we negate ints auto* left = binary->left->dynCast<Const>(); + seekStack.emplace_back(binary->right, -mul); if (!left || left->value.geti32() != 0) { - seek(binary->left, mul); + seekStack.emplace_back(binary->left, mul); } - seek(binary->right, -mul); - return; + continue; } else if (binary->op == ShlInt32) { if (auto* c = binary->right->dynCast<Const>()) { - seek(binary->left, mul * Pow2(Bits::getEffectiveShifts(c))); - return; + seekStack.emplace_back(binary->left, + mul * Pow2(Bits::getEffectiveShifts(c))); + continue; } } else if (binary->op == MulInt32) { if (auto* c = binary->left->dynCast<Const>()) { - seek(binary->right, mul * c->value.geti32()); - return; + seekStack.emplace_back(binary->right, mul * c->value.geti32()); + continue; } else if (auto* c = binary->right->dynCast<Const>()) { - seek(binary->left, mul * c->value.geti32()); - return; + seekStack.emplace_back(binary->left, mul * c->value.geti32()); + continue; } } } }; // find all factors - seek(binary, 1); if (constants.size() <= 1) { // nothing much to do, except for the trivial case of adding/subbing a // zero |