Skip to content

Commit c0fba4a

Browse files
authored
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).
1 parent c5b50fe commit c0fba4a

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

src/passes/OptimizeInstructions.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -986,46 +986,57 @@ struct OptimizeInstructions
986986
Expression* optimizeAddedConstants(Binary* binary) {
987987
uint32_t constant = 0;
988988
std::vector<Const*> constants;
989-
std::function<void(Expression*, int)> seek = [&](Expression* curr,
990-
int mul) {
989+
990+
struct SeekState {
991+
Expression* curr;
992+
int mul;
993+
SeekState(Expression* curr, int mul) : curr(curr), mul(mul) {}
994+
};
995+
std::vector<SeekState> seekStack;
996+
seekStack.emplace_back(binary, 1);
997+
while (!seekStack.empty()) {
998+
auto state = seekStack.back();
999+
seekStack.pop_back();
1000+
auto curr = state.curr;
1001+
auto mul = state.mul;
9911002
if (auto* c = curr->dynCast<Const>()) {
9921003
uint32_t value = c->value.geti32();
9931004
if (value != 0) {
9941005
constant += value * mul;
9951006
constants.push_back(c);
9961007
}
997-
return;
1008+
continue;
9981009
} else if (auto* binary = curr->dynCast<Binary>()) {
9991010
if (binary->op == AddInt32) {
1000-
seek(binary->left, mul);
1001-
seek(binary->right, mul);
1002-
return;
1011+
seekStack.emplace_back(binary->right, mul);
1012+
seekStack.emplace_back(binary->left, mul);
1013+
continue;
10031014
} else if (binary->op == SubInt32) {
10041015
// if the left is a zero, ignore it, it's how we negate ints
10051016
auto* left = binary->left->dynCast<Const>();
1017+
seekStack.emplace_back(binary->right, -mul);
10061018
if (!left || left->value.geti32() != 0) {
1007-
seek(binary->left, mul);
1019+
seekStack.emplace_back(binary->left, mul);
10081020
}
1009-
seek(binary->right, -mul);
1010-
return;
1021+
continue;
10111022
} else if (binary->op == ShlInt32) {
10121023
if (auto* c = binary->right->dynCast<Const>()) {
1013-
seek(binary->left, mul * Pow2(Bits::getEffectiveShifts(c)));
1014-
return;
1024+
seekStack.emplace_back(binary->left,
1025+
mul * Pow2(Bits::getEffectiveShifts(c)));
1026+
continue;
10151027
}
10161028
} else if (binary->op == MulInt32) {
10171029
if (auto* c = binary->left->dynCast<Const>()) {
1018-
seek(binary->right, mul * c->value.geti32());
1019-
return;
1030+
seekStack.emplace_back(binary->right, mul * c->value.geti32());
1031+
continue;
10201032
} else if (auto* c = binary->right->dynCast<Const>()) {
1021-
seek(binary->left, mul * c->value.geti32());
1022-
return;
1033+
seekStack.emplace_back(binary->left, mul * c->value.geti32());
1034+
continue;
10231035
}
10241036
}
10251037
}
10261038
};
10271039
// find all factors
1028-
seek(binary, 1);
10291040
if (constants.size() <= 1) {
10301041
// nothing much to do, except for the trivial case of adding/subbing a
10311042
// zero

0 commit comments

Comments
 (0)