Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/tools/wasm2js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/wasm2js/conversions-modified.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function asmFunc(global, env, buffer) {

function $4($0) {
$0 = Math_fround($0);
return ~~$0 >>> 0;
return ~~$0 >>> 0 | 0;
}

function $5($0) {
Expand All @@ -70,19 +70,19 @@ function asmFunc(global, env, buffer) {

function $6($0) {
$0 = +$0;
return ~~$0 >>> 0;
return ~~$0 >>> 0 | 0;
}

function $7($0) {
$0 = Math_fround($0);
i64toi32_i32$HIGH_BITS = Math_fround(Math_abs($0)) >= Math_fround(1.0) ? ($0 > Math_fround(0.0) ? ~~Math_fround(Math_min(Math_fround(Math_floor(Math_fround($0 / Math_fround(4294967296.0)))), Math_fround(4294967296.0))) >>> 0 : ~~Math_fround(Math_ceil(Math_fround(Math_fround($0 - Math_fround(~~$0 >>> 0 >>> 0)) / Math_fround(4294967296.0)))) >>> 0) : 0;
return ~~$0 >>> 0;
return ~~$0 >>> 0 | 0;
}

function $9($0) {
$0 = +$0;
i64toi32_i32$HIGH_BITS = Math_abs($0) >= 1.0 ? ($0 > 0.0 ? ~~Math_min(Math_floor($0 / 4294967296.0), 4294967295.0) >>> 0 : ~~Math_ceil(($0 - +(~~$0 >>> 0 >>> 0)) / 4294967296.0) >>> 0) : 0;
return ~~$0 >>> 0;
return ~~$0 >>> 0 | 0;
}

function $11($0) {
Expand Down
4 changes: 2 additions & 2 deletions test/wasm2js/float-ops.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ function asmFunc(global, env, buffer) {

function $41($0) {
$0 = Math_fround($0);
return ~~$0 >>> 0;
return ~~$0 >>> 0 | 0;
}

function $42($0) {
$0 = +$0;
return ~~$0 >>> 0;
return ~~$0 >>> 0 | 0;
}

function $47($0) {
Expand Down
16 changes: 8 additions & 8 deletions test/wasm2js/i64-rotate.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ function asmFunc(global, env, buffer) {
$5 = $6;
$3 = $5 & 31;
if (32 <= $5 >>> 0) {
$3 = -1 >>> $3
$3 = -1 >>> $3 | 0
} else {
$4 = -1 >>> $3;
$4 = -1 >>> $3 | 0;
$3 = (1 << $3) - 1 << 32 - $3 | -1 >>> $3;
}
$5 = $3 & $0;
Expand Down Expand Up @@ -66,9 +66,9 @@ function asmFunc(global, env, buffer) {
$1 = $4 & 31;
if (32 <= $4 >>> 0) {
$2 = 0;
$0 = $3 >>> $1;
$0 = $3 >>> $1 | 0;
} else {
$2 = $3 >>> $1;
$2 = $3 >>> $1 | 0;
$0 = ((1 << $1) - 1 & $3) << 32 - $1 | $0 >>> $1;
}
$0 = $0 | $6;
Expand All @@ -92,19 +92,19 @@ function asmFunc(global, env, buffer) {
$5 = $6 & 31;
if (32 <= $6 >>> 0) {
$4 = 0;
$6 = $3 >>> $5;
$6 = $3 >>> $5 | 0;
} else {
$4 = $3 >>> $5;
$4 = $3 >>> $5 | 0;
$6 = ((1 << $5) - 1 & $3) << 32 - $5 | $7 >>> $5;
}
$7 = $4;
$3 = 0 - $2 & 63;
$5 = $3 & 31;
if (32 <= $3 >>> 0) {
$4 = 0;
$2 = -1 >>> $5;
$2 = -1 >>> $5 | 0;
} else {
$4 = -1 >>> $5;
$4 = -1 >>> $5 | 0;
$2 = (1 << $5) - 1 << 32 - $5 | -1 >>> $5;
}
$0 = $2 & $0;
Expand Down
8 changes: 4 additions & 4 deletions test/wasm2js/stack-modified.2asm.js.opt
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ function asmFunc(global, env, buffer) {

function _ZN17compiler_builtins3int3mul3Mul3mul17h070e9a1c69faec5bE($0_1, $1, $2, $3) {
var $4 = 0, $5 = 0, $6 = 0, $7 = 0, $8 = 0, $9 = 0;
$4 = $2 >>> 16;
$5 = $0_1 >>> 16;
$4 = $2 >>> 16 | 0;
$5 = $0_1 >>> 16 | 0;
$9 = Math_imul($4, $5);
$6 = $2 & 65535;
$7 = $0_1 & 65535;
$8 = Math_imul($6, $7);
$5 = ($8 >>> 16) + Math_imul($5, $6) | 0;
$5 = ($8 >>> 16 | 0) + Math_imul($5, $6) | 0;
$4 = ($5 & 65535) + Math_imul($4, $7) | 0;
$0_1 = (((Math_imul($1, $2) + $9 | 0) + Math_imul($0_1, $3) | 0) + ($5 >>> 16) | 0) + ($4 >>> 16) | 0;
$0_1 = (Math_imul($1, $2) + $9 | 0) + Math_imul($0_1, $3) + ($5 >>> 16) + ($4 >>> 16) | 0;
$1 = $8 & 65535 | $4 << 16;
i64toi32_i32$HIGH_BITS = $0_1;
return $1;
Expand Down