Skip to content

Commit

Permalink
wasm2js: optimize loops and eqz (#2051)
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken committed Apr 25, 2019
1 parent 0994588 commit ef6020c
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 213 deletions.
17 changes: 10 additions & 7 deletions src/wasm2js.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,9 +823,14 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, Function* func, bool standalo
Ref visitLoop(Loop* curr) {
Name asmLabel = curr->name;
continueLabels.insert(asmLabel);
Ref body = blockify(visit(curr->body, result));
flattenAppend(body, ValueBuilder::makeBreak(fromName(asmLabel, NameScope::Label)));
Ref ret = ValueBuilder::makeDo(body, ValueBuilder::makeInt(1));
Ref body = visit(curr->body, result);
// if we can reach the end of the block, we must leave the while (1) loop
if (curr->body->type != unreachable) {
assert(curr->body->type == none); // flat IR
body = blockify(body);
flattenAppend(body, ValueBuilder::makeBreak(fromName(asmLabel, NameScope::Label)));
}
Ref ret = ValueBuilder::makeWhile(ValueBuilder::makeInt(1), body);
return ValueBuilder::makeLabel(fromName(asmLabel, NameScope::Label), ret);
}

Expand Down Expand Up @@ -1207,10 +1212,8 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, Function* func, bool standalo
<< std::endl;
WASM_UNREACHABLE();
case EqZInt32:
return ValueBuilder::makeBinary(
makeAsmCoercion(visit(curr->value,
EXPRESSION_RESULT), ASM_INT), EQ,
makeAsmCoercion(ValueBuilder::makeInt(0), ASM_INT));
// XXX !x does change the type to bool, which is correct, but may be slower?
return ValueBuilder::makeUnary(L_NOT, visit(curr->value, EXPRESSION_RESULT));
case ReinterpretFloat32: {
ABI::wasm2js::ensureScratchMemoryHelpers(module, ABI::wasm2js::SCRATCH_STORE_F32);
ABI::wasm2js::ensureScratchMemoryHelpers(module, ABI::wasm2js::SCRATCH_LOAD_I32);
Expand Down
2 changes: 1 addition & 1 deletion test/wasm2js/block.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function asmFunc(global, env, buffer) {

function $8() {
dummy();
return (13 | 0) == (0 | 0) | 0;
return !13 | 0;
}

function $9() {
Expand Down
10 changes: 4 additions & 6 deletions test/wasm2js/br_table_to_loop.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function asmFunc(global, env, buffer) {
var infinity = global.Infinity;
function $0() {
block : {
loop : do {
loop : while (1) {
switch (1 | 0) {
case 0:
break block;
Expand All @@ -32,14 +32,13 @@ function asmFunc(global, env, buffer) {
default:
break block;
};
break loop;
} while (1);
};
}
}

function $1() {
block : {
loop : do {
loop : while (1) {
switch (1 | 0) {
case 0:
continue loop;
Expand All @@ -48,8 +47,7 @@ function asmFunc(global, env, buffer) {
default:
continue loop;
};
break loop;
} while (1);
};
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/wasm2js/call.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function asmFunc(global, env, buffer) {
$0$hi = $0$hi | 0;
var i64toi32_i32$5 = 0, i64toi32_i32$2 = 0, i64toi32_i32$0 = 0, $8 = 0, $8$hi = 0, i64toi32_i32$3 = 0, $6 = 0, $6$hi = 0;
i64toi32_i32$0 = $0$hi;
if (($0 | i64toi32_i32$0) == (0 | 0)) {
if (!($0 | i64toi32_i32$0)) {
{
i64toi32_i32$0 = 0;
$8 = 1;
Expand Down Expand Up @@ -184,7 +184,7 @@ function asmFunc(global, env, buffer) {
$1$hi = $1$hi | 0;
var i64toi32_i32$5 = 0, i64toi32_i32$2 = 0, i64toi32_i32$0 = 0, $11 = 0, $11$hi = 0, i64toi32_i32$3 = 0, $6 = 0, $6$hi = 0, $9 = 0, $9$hi = 0;
i64toi32_i32$0 = $0$hi;
if (($0 | i64toi32_i32$0) == (0 | 0)) {
if (!($0 | i64toi32_i32$0)) {
{
i64toi32_i32$0 = $1$hi;
$11 = $1;
Expand Down Expand Up @@ -286,7 +286,7 @@ function asmFunc(global, env, buffer) {
$0$hi = $0$hi | 0;
var i64toi32_i32$0 = 0, i64toi32_i32$5 = 0, $6 = 0, i64toi32_i32$3 = 0;
i64toi32_i32$0 = $0$hi;
if (($0 | i64toi32_i32$0) == (0 | 0)) {
if (!($0 | i64toi32_i32$0)) {
$6 = 44
} else {
{
Expand All @@ -305,7 +305,7 @@ function asmFunc(global, env, buffer) {
$0$hi = $0$hi | 0;
var i64toi32_i32$0 = 0, i64toi32_i32$5 = 0, $6 = 0, i64toi32_i32$3 = 0;
i64toi32_i32$0 = $0$hi;
if (($0 | i64toi32_i32$0) == (0 | 0)) {
if (!($0 | i64toi32_i32$0)) {
$6 = 99
} else {
{
Expand Down
6 changes: 3 additions & 3 deletions test/wasm2js/call_indirect.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function asmFunc(global, env, buffer) {
$0$hi = $0$hi | 0;
var i64toi32_i32$5 = 0, i64toi32_i32$2 = 0, i64toi32_i32$0 = 0, $8 = 0, $8$hi = 0, i64toi32_i32$3 = 0, $6 = 0, $6$hi = 0, wasm2js_i32$0 = 0, wasm2js_i32$1 = 0, wasm2js_i32$2 = 0;
i64toi32_i32$0 = $0$hi;
if (($0 | i64toi32_i32$0) == (0 | 0)) {
if (!($0 | i64toi32_i32$0)) {
{
i64toi32_i32$0 = 0;
$8 = 1;
Expand Down Expand Up @@ -319,7 +319,7 @@ function asmFunc(global, env, buffer) {
function even($0) {
$0 = $0 | 0;
var $6 = 0, wasm2js_i32$0 = 0, wasm2js_i32$1 = 0;
if (($0 | 0) == (0 | 0)) {
if (!$0) {
$6 = 44
} else {
$6 = ((wasm2js_i32$1 = $0 - 1 | 0, wasm2js_i32$0 = 15), FUNCTION_TABLE[wasm2js_i32$0](wasm2js_i32$1 | 0) | 0)
Expand All @@ -330,7 +330,7 @@ function asmFunc(global, env, buffer) {
function odd($0) {
$0 = $0 | 0;
var $6 = 0, wasm2js_i32$0 = 0, wasm2js_i32$1 = 0;
if (($0 | 0) == (0 | 0)) {
if (!$0) {
$6 = 99
} else {
$6 = ((wasm2js_i32$1 = $0 - 1 | 0, wasm2js_i32$0 = 14), FUNCTION_TABLE[wasm2js_i32$0](wasm2js_i32$1 | 0) | 0)
Expand Down
2 changes: 1 addition & 1 deletion test/wasm2js/f32.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function asmFunc(global, env, buffer) {
var var$1 = Math_fround(0), var$2 = Math_fround(0), wasm2js_f32$0 = Math_fround(0), wasm2js_f32$1 = Math_fround(0), wasm2js_i32$0 = 0;
var$1 = Math_fround(Math_floor(var$0));
var$2 = Math_fround(var$0 - var$1);
if ((var$2 < Math_fround(.5) | 0) == (0 | 0)) {
if (!(var$2 < Math_fround(.5))) {
{
var$0 = Math_fround(Math_ceil(var$0));
if (var$2 > Math_fround(.5)) {
Expand Down
2 changes: 1 addition & 1 deletion test/wasm2js/f64.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function asmFunc(global, env, buffer) {
var var$1 = 0.0, var$2 = 0.0, wasm2js_f64$0 = 0.0, wasm2js_f64$1 = 0.0, wasm2js_i32$0 = 0;
var$1 = Math_floor(var$0);
var$2 = var$0 - var$1;
if ((var$2 < .5 | 0) == (0 | 0)) {
if (!(var$2 < .5)) {
{
var$0 = Math_ceil(var$0);
if (var$2 > .5) {
Expand Down
14 changes: 6 additions & 8 deletions test/wasm2js/fac.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function asmFunc(global, env, buffer) {
$2_1 = 1;
$2$hi = i64toi32_i32$0;
block : {
loop_in : do {
loop_in : while (1) {
i64toi32_i32$0 = $1$hi;
i64toi32_i32$2 = $1;
i64toi32_i32$1 = 0;
Expand Down Expand Up @@ -133,8 +133,7 @@ function asmFunc(global, env, buffer) {
}
}
continue loop_in;
break loop_in;
} while (1);
};
}
i64toi32_i32$5 = $2$hi;
i64toi32_i32$3 = $2_1;
Expand All @@ -153,7 +152,7 @@ function asmFunc(global, env, buffer) {
res = 1;
res$hi = i64toi32_i32$0;
done : {
loop : do {
loop : while (1) {
i64toi32_i32$0 = i$hi;
i64toi32_i32$2 = i;
i64toi32_i32$1 = 0;
Expand Down Expand Up @@ -181,8 +180,7 @@ function asmFunc(global, env, buffer) {
}
}
continue loop;
break loop;
} while (1);
};
}
i64toi32_i32$5 = res$hi;
i64toi32_i32$3 = res;
Expand Down Expand Up @@ -224,7 +222,7 @@ function asmFunc(global, env, buffer) {
if ($10) {
break block
}
loop_in : do {
loop_in : while (1) {
i64toi32_i32$2 = $1$hi;
i64toi32_i32$2 = $0$hi;
i64toi32_i32$2 = $1$hi;
Expand Down Expand Up @@ -270,7 +268,7 @@ function asmFunc(global, env, buffer) {
continue loop_in
}
break loop_in;
} while (1);
};
}
i64toi32_i32$2 = $1$hi;
i64toi32_i32$5 = $1;
Expand Down
4 changes: 2 additions & 2 deletions test/wasm2js/float_misc.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function asmFunc(global, env, buffer) {
var var$1 = Math_fround(0), var$2 = Math_fround(0), wasm2js_f32$0 = Math_fround(0), wasm2js_f32$1 = Math_fround(0), wasm2js_i32$0 = 0;
var$1 = Math_fround(Math_floor(var$0));
var$2 = Math_fround(var$0 - var$1);
if ((var$2 < Math_fround(.5) | 0) == (0 | 0)) {
if (!(var$2 < Math_fround(.5))) {
{
var$0 = Math_fround(Math_ceil(var$0));
if (var$2 > Math_fround(.5)) {
Expand All @@ -250,7 +250,7 @@ function asmFunc(global, env, buffer) {
var var$1 = 0.0, var$2 = 0.0, wasm2js_f64$0 = 0.0, wasm2js_f64$1 = 0.0, wasm2js_i32$0 = 0;
var$1 = Math_floor(var$0);
var$2 = var$0 - var$1;
if ((var$2 < .5 | 0) == (0 | 0)) {
if (!(var$2 < .5)) {
{
var$0 = Math_ceil(var$0);
if (var$2 > .5) {
Expand Down
9 changes: 4 additions & 5 deletions test/wasm2js/i32.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function asmFunc(global, env, buffer) {

function $18(x) {
x = x | 0;
return (x | 0) == (0 | 0) | 0;
return !x | 0;
}

function $19(x, y) {
Expand Down Expand Up @@ -201,16 +201,15 @@ function asmFunc(global, env, buffer) {
function __wasm_popcnt_i32(var$0) {
var var$1 = 0, $5_1 = 0;
label$1 : {
label$2 : do {
label$2 : while (1) {
$5_1 = var$1;
if ((var$0 | 0) == (0 | 0)) {
if (!var$0) {
break label$1
}
var$0 = var$0 & var$0 - 1;
var$1 = var$1 + 1 | 0;
continue label$2;
break label$2;
} while (1);
};
}
return $5_1;
}
Expand Down
9 changes: 4 additions & 5 deletions test/wasm2js/i64-ctz.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function asmFunc(global, env, buffer) {
function __wasm_ctz_i64(var$0, var$0$hi) {
var i64toi32_i32$0 = 0, i64toi32_i32$3 = 0, i64toi32_i32$5 = 0, i64toi32_i32$4 = 0, i64toi32_i32$2 = 0, i64toi32_i32$1 = 0, $10 = 0, $5$hi = 0, $8$hi = 0;
i64toi32_i32$0 = var$0$hi;
if (((var$0 | i64toi32_i32$0) == (0 | 0) | 0) == (0 | 0)) {
if (!!(var$0 | i64toi32_i32$0)) {
{
i64toi32_i32$0 = var$0$hi;
i64toi32_i32$2 = var$0;
Expand Down Expand Up @@ -199,10 +199,10 @@ function asmFunc(global, env, buffer) {
function __wasm_popcnt_i64(var$0, var$0$hi) {
var i64toi32_i32$0 = 0, i64toi32_i32$2 = 0, i64toi32_i32$5 = 0, i64toi32_i32$4 = 0, i64toi32_i32$3 = 0, i64toi32_i32$1 = 0, var$1$hi = 0, var$1 = 0, $5 = 0, $5$hi = 0, $4 = 0, $9$hi = 0;
label$1 : {
label$2 : do {
label$2 : while (1) {
i64toi32_i32$0 = var$1$hi;
i64toi32_i32$0 = var$0$hi;
$4 = (var$0 | i64toi32_i32$0) == (0 | 0);
$4 = !(var$0 | i64toi32_i32$0);
i64toi32_i32$0 = var$1$hi;
$5 = var$1;
$5$hi = i64toi32_i32$0;
Expand Down Expand Up @@ -237,8 +237,7 @@ function asmFunc(global, env, buffer) {
var$1 = i64toi32_i32$1;
var$1$hi = i64toi32_i32$4;
continue label$2;
break label$2;
} while (1);
};
}
i64toi32_i32$4 = $5$hi;
i64toi32_i32$5 = $5;
Expand Down
25 changes: 12 additions & 13 deletions test/wasm2js/i64.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ function asmFunc(global, env, buffer) {
x$hi = x$hi | 0;
var i64toi32_i32$0 = 0;
i64toi32_i32$0 = x$hi;
return (x | i64toi32_i32$0) == (0 | 0) | 0;
return !(x | i64toi32_i32$0) | 0;
}

function $19(x, x$hi, y, y$hi) {
Expand Down Expand Up @@ -2990,7 +2990,7 @@ function asmFunc(global, env, buffer) {
{
i64toi32_i32$1 = var$1$hi;
var$3 = var$1;
if ((var$3 | 0) == (0 | 0)) {
if (!var$3) {
break label$11
}
i64toi32_i32$1 = var$1$hi;
Expand All @@ -3010,7 +3010,7 @@ function asmFunc(global, env, buffer) {
}
}
var$4 = $38;
if ((var$4 | 0) == (0 | 0)) {
if (!var$4) {
break label$9
}
var$2 = Math_clz32(var$4) - Math_clz32(var$2) | 0;
Expand Down Expand Up @@ -3057,10 +3057,10 @@ function asmFunc(global, env, buffer) {
}
var$3 = $39;
i64toi32_i32$1 = var$0$hi;
if ((var$0 | 0) == (0 | 0)) {
if (!var$0) {
break label$7
}
if ((var$3 | 0) == (0 | 0)) {
if (!var$3) {
break label$6
}
var$4 = var$3 + -1 | 0;
Expand Down Expand Up @@ -3103,7 +3103,7 @@ function asmFunc(global, env, buffer) {
return i64toi32_i32$3;
}
var$4 = var$3 + -1 | 0;
if ((var$4 & var$3) == (0 | 0)) {
if (!(var$4 & var$3)) {
break label$5
}
var$2 = (Math_clz32(var$3) + 33 | 0) - Math_clz32(var$2) | 0;
Expand Down Expand Up @@ -3231,7 +3231,7 @@ function asmFunc(global, env, buffer) {
}
var$8 = i64toi32_i32$4;
var$8$hi = i64toi32_i32$5;
label$15 : do {
label$15 : while (1) {
i64toi32_i32$5 = var$5$hi;
i64toi32_i32$2 = var$5;
i64toi32_i32$1 = 0;
Expand Down Expand Up @@ -3360,7 +3360,7 @@ function asmFunc(global, env, buffer) {
continue label$15
}
break label$15;
} while (1);
};
break label$13;
}
}
Expand Down Expand Up @@ -3409,7 +3409,7 @@ function asmFunc(global, env, buffer) {
function __wasm_ctz_i64(var$0, var$0$hi) {
var i64toi32_i32$0 = 0, i64toi32_i32$3 = 0, i64toi32_i32$5 = 0, i64toi32_i32$4 = 0, i64toi32_i32$2 = 0, i64toi32_i32$1 = 0, $10_1 = 0, $5$hi = 0, $8$hi = 0;
i64toi32_i32$0 = var$0$hi;
if (((var$0 | i64toi32_i32$0) == (0 | 0) | 0) == (0 | 0)) {
if (!!(var$0 | i64toi32_i32$0)) {
{
i64toi32_i32$0 = var$0$hi;
i64toi32_i32$2 = var$0;
Expand Down Expand Up @@ -3519,10 +3519,10 @@ function asmFunc(global, env, buffer) {
function __wasm_popcnt_i64(var$0, var$0$hi) {
var i64toi32_i32$0 = 0, i64toi32_i32$2 = 0, i64toi32_i32$5 = 0, i64toi32_i32$4 = 0, i64toi32_i32$3 = 0, i64toi32_i32$1 = 0, var$1$hi = 0, var$1 = 0, $5_1 = 0, $5$hi = 0, $4_1 = 0, $9$hi = 0;
label$1 : {
label$2 : do {
label$2 : while (1) {
i64toi32_i32$0 = var$1$hi;
i64toi32_i32$0 = var$0$hi;
$4_1 = (var$0 | i64toi32_i32$0) == (0 | 0);
$4_1 = !(var$0 | i64toi32_i32$0);
i64toi32_i32$0 = var$1$hi;
$5_1 = var$1;
$5$hi = i64toi32_i32$0;
Expand Down Expand Up @@ -3557,8 +3557,7 @@ function asmFunc(global, env, buffer) {
var$1 = i64toi32_i32$1;
var$1$hi = i64toi32_i32$4;
continue label$2;
break label$2;
} while (1);
};
}
i64toi32_i32$4 = $5$hi;
i64toi32_i32$5 = $5_1;
Expand Down
Loading

0 comments on commit ef6020c

Please sign in to comment.