Skip to content

Commit 263d2d5

Browse files
authored
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
1 parent 98747d9 commit 263d2d5

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

src/tools/wasm2js.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ static void optimizeJS(Ref ast) {
179179
return false;
180180
};
181181

182+
auto isSignedBitwise = [](Ref node) {
183+
if (node->isArray() && !node->empty() && node[0] == BINARY) {
184+
auto op = node[1];
185+
return op == OR || op == AND || op == XOR || op == RSHIFT || op == LSHIFT;
186+
}
187+
return false;
188+
};
189+
182190
auto isUnary = [](Ref node, IString op) {
183191
return node->isArray() && !node->empty() && node[0] == UNARY_PREFIX &&
184192
node[1] == op;
@@ -275,9 +283,9 @@ static void optimizeJS(Ref ast) {
275283
// x | 0 going into a bitwise op => skip the | 0
276284
node[2] = removeOrZero(node[2]);
277285
node[3] = removeOrZero(node[3]);
278-
// x | 0 | 0 => x | 0
286+
// (x | 0 or similar) | 0 => (x | 0 or similar)
279287
if (isOrZero(node)) {
280-
if (isBitwise(node[2])) {
288+
if (isSignedBitwise(node[2])) {
281289
replaceInPlace(node, node[2]);
282290
}
283291
}

test/wasm2js/conversions-modified.2asm.js.opt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function asmFunc(global, env, buffer) {
6060

6161
function $4($0) {
6262
$0 = Math_fround($0);
63-
return ~~$0 >>> 0;
63+
return ~~$0 >>> 0 | 0;
6464
}
6565

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

7171
function $6($0) {
7272
$0 = +$0;
73-
return ~~$0 >>> 0;
73+
return ~~$0 >>> 0 | 0;
7474
}
7575

7676
function $7($0) {
7777
$0 = Math_fround($0);
7878
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;
79-
return ~~$0 >>> 0;
79+
return ~~$0 >>> 0 | 0;
8080
}
8181

8282
function $9($0) {
8383
$0 = +$0;
8484
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;
85-
return ~~$0 >>> 0;
85+
return ~~$0 >>> 0 | 0;
8686
}
8787

8888
function $11($0) {

test/wasm2js/float-ops.2asm.js.opt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ function asmFunc(global, env, buffer) {
236236

237237
function $41($0) {
238238
$0 = Math_fround($0);
239-
return ~~$0 >>> 0;
239+
return ~~$0 >>> 0 | 0;
240240
}
241241

242242
function $42($0) {
243243
$0 = +$0;
244-
return ~~$0 >>> 0;
244+
return ~~$0 >>> 0 | 0;
245245
}
246246

247247
function $47($0) {

test/wasm2js/i64-rotate.2asm.js.opt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ function asmFunc(global, env, buffer) {
3535
$5 = $6;
3636
$3 = $5 & 31;
3737
if (32 <= $5 >>> 0) {
38-
$3 = -1 >>> $3
38+
$3 = -1 >>> $3 | 0
3939
} else {
40-
$4 = -1 >>> $3;
40+
$4 = -1 >>> $3 | 0;
4141
$3 = (1 << $3) - 1 << 32 - $3 | -1 >>> $3;
4242
}
4343
$5 = $3 & $0;
@@ -66,9 +66,9 @@ function asmFunc(global, env, buffer) {
6666
$1 = $4 & 31;
6767
if (32 <= $4 >>> 0) {
6868
$2 = 0;
69-
$0 = $3 >>> $1;
69+
$0 = $3 >>> $1 | 0;
7070
} else {
71-
$2 = $3 >>> $1;
71+
$2 = $3 >>> $1 | 0;
7272
$0 = ((1 << $1) - 1 & $3) << 32 - $1 | $0 >>> $1;
7373
}
7474
$0 = $0 | $6;
@@ -92,19 +92,19 @@ function asmFunc(global, env, buffer) {
9292
$5 = $6 & 31;
9393
if (32 <= $6 >>> 0) {
9494
$4 = 0;
95-
$6 = $3 >>> $5;
95+
$6 = $3 >>> $5 | 0;
9696
} else {
97-
$4 = $3 >>> $5;
97+
$4 = $3 >>> $5 | 0;
9898
$6 = ((1 << $5) - 1 & $3) << 32 - $5 | $7 >>> $5;
9999
}
100100
$7 = $4;
101101
$3 = 0 - $2 & 63;
102102
$5 = $3 & 31;
103103
if (32 <= $3 >>> 0) {
104104
$4 = 0;
105-
$2 = -1 >>> $5;
105+
$2 = -1 >>> $5 | 0;
106106
} else {
107-
$4 = -1 >>> $5;
107+
$4 = -1 >>> $5 | 0;
108108
$2 = (1 << $5) - 1 << 32 - $5 | -1 >>> $5;
109109
}
110110
$0 = $2 & $0;

test/wasm2js/stack-modified.2asm.js.opt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ function asmFunc(global, env, buffer) {
5252

5353
function _ZN17compiler_builtins3int3mul3Mul3mul17h070e9a1c69faec5bE($0_1, $1, $2, $3) {
5454
var $4 = 0, $5 = 0, $6 = 0, $7 = 0, $8 = 0, $9 = 0;
55-
$4 = $2 >>> 16;
56-
$5 = $0_1 >>> 16;
55+
$4 = $2 >>> 16 | 0;
56+
$5 = $0_1 >>> 16 | 0;
5757
$9 = Math_imul($4, $5);
5858
$6 = $2 & 65535;
5959
$7 = $0_1 & 65535;
6060
$8 = Math_imul($6, $7);
61-
$5 = ($8 >>> 16) + Math_imul($5, $6) | 0;
61+
$5 = ($8 >>> 16 | 0) + Math_imul($5, $6) | 0;
6262
$4 = ($5 & 65535) + Math_imul($4, $7) | 0;
63-
$0_1 = (((Math_imul($1, $2) + $9 | 0) + Math_imul($0_1, $3) | 0) + ($5 >>> 16) | 0) + ($4 >>> 16) | 0;
63+
$0_1 = (Math_imul($1, $2) + $9 | 0) + Math_imul($0_1, $3) + ($5 >>> 16) + ($4 >>> 16) | 0;
6464
$1 = $8 & 65535 | $4 << 16;
6565
i64toi32_i32$HIGH_BITS = $0_1;
6666
return $1;

0 commit comments

Comments
 (0)