From 263d2d5025c98092f781c8b8d9eb7ac6df6aadab Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 10 Jan 2020 06:37:00 -0800 Subject: wasm2js: Do not convert x >>> 0 | 0 to x >>> 0 (#2581) isBinary was used where we should only accept a signed binary, as removing the | 0 from an unsigned value may be incorrect. This does regress a few small things (as can be seen in the diff). If it's important we can add more sophisticated optimizations here, perhaps like an assumption that the signedness of a local never matters. Fixes emscripten-core/emscripten#10173 --- src/tools/wasm2js.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp index 06ab81263..e7b62c16a 100644 --- a/src/tools/wasm2js.cpp +++ b/src/tools/wasm2js.cpp @@ -179,6 +179,14 @@ static void optimizeJS(Ref ast) { return false; }; + auto isSignedBitwise = [](Ref node) { + if (node->isArray() && !node->empty() && node[0] == BINARY) { + auto op = node[1]; + return op == OR || op == AND || op == XOR || op == RSHIFT || op == LSHIFT; + } + return false; + }; + auto isUnary = [](Ref node, IString op) { return node->isArray() && !node->empty() && node[0] == UNARY_PREFIX && node[1] == op; @@ -275,9 +283,9 @@ static void optimizeJS(Ref ast) { // x | 0 going into a bitwise op => skip the | 0 node[2] = removeOrZero(node[2]); node[3] = removeOrZero(node[3]); - // x | 0 | 0 => x | 0 + // (x | 0 or similar) | 0 => (x | 0 or similar) if (isOrZero(node)) { - if (isBitwise(node[2])) { + if (isSignedBitwise(node[2])) { replaceInPlace(node, node[2]); } } -- cgit v1.2.3