Skip to content

[Wasm2JS] More optimal JS codegen for some special cases #4078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
55 changes: 51 additions & 4 deletions src/wasm2js.h
Original file line number Diff line number Diff line change
Expand Up @@ -1766,11 +1766,28 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m,
ret = ValueBuilder::makeBinary(left, PLUS, right);
break;
case SubInt32:
ret = ValueBuilder::makeBinary(left, MINUS, right);
if (parent->options.optimizeLevel != 0 && left->isNumber() &&
left->getNumber() == 0) {
// special case
// 0 - x => -x
ret = ValueBuilder::makeUnary(MINUS, right);
} else {
ret = ValueBuilder::makeBinary(left, MINUS, right);
}
break;
case MulInt32: {
if (curr->type == Type::i32) {
// TODO: when one operand is a small int, emit a multiply
// We can avoid Math.imul call if RHS is small constant
//
// `2 ** 53 - 1` is maximum safe integer in fp domain
// so
// `2 ** (53 - 32) - 1 or 0x1FFFFF` is maximum safe
// multiplicand
if (parent->options.optimizeLevel != 0 && right->isNumber() &&
std::abs(right->getNumber()) <= (double)0x1FFFFF) {
ret = ValueBuilder::makeBinary(left, MUL, right);
break;
}
return ValueBuilder::makeCall(MATH_IMUL, left, right);
} else {
return ValueBuilder::makeBinary(left, MUL, right);
Expand Down Expand Up @@ -1798,21 +1815,49 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m,
break;
case AndInt32:
ret = ValueBuilder::makeBinary(left, AND, right);
// All bitwise and shift operations already implicitly coerced
// to integer, so we can skip the explicit coercing in case when
// output type is i32.
if (curr->type == Type::i32) {
return ret;
}
break;
case OrInt32:
ret = ValueBuilder::makeBinary(left, OR, right);
if (curr->type == Type::i32) {
return ret;
}
break;
case XorInt32:
ret = ValueBuilder::makeBinary(left, XOR, right);
if (parent->options.optimizeLevel != 0 && right->isNumber() &&
right->getNumber() == -1) {
// special case
// x ^ -1 => ~x
ret = ValueBuilder::makeUnary(B_NOT, left);
} else {
ret = ValueBuilder::makeBinary(left, XOR, right);
}
if (curr->type == Type::i32) {
return ret;
}
break;
case ShlInt32:
ret = ValueBuilder::makeBinary(left, LSHIFT, right);
if (curr->type == Type::i32) {
return ret;
}
break;
case ShrUInt32:
ret = ValueBuilder::makeBinary(left, TRSHIFT, right);
if (curr->type == Type::i32) {
return ret;
}
break;
case ShrSInt32:
ret = ValueBuilder::makeBinary(left, RSHIFT, right);
if (curr->type == Type::i32) {
return ret;
}
break;
case EqInt32: {
return ValueBuilder::makeBinary(makeSigning(left, ASM_SIGNED),
Expand Down Expand Up @@ -1971,8 +2016,10 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m,
return ValueBuilder::makeReturn(Ref());
}
Ref val = visit(curr->value, EXPRESSION_RESULT);
// TODO: also avoid coercion if val is Math_fround
bool needCoercion =
parent->options.optimizeLevel == 0 || standaloneFunction ||
(!(curr->value->is<Unary>() || curr->value->is<Binary>()) &&
(parent->options.optimizeLevel == 0 || standaloneFunction)) ||
parent->functionsCallableFromOutside.count(func->name);
if (needCoercion) {
val = makeAsmCoercion(val, wasmToAsmType(curr->value->type));
Expand Down
2 changes: 1 addition & 1 deletion test/wasm2js.asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function check1() {

if (!check1()) throw 'assertion failed: ( assert_return ( invoke empty ) )';
function check2() {
return (retasmFunc0.add(1 | 0, 1 | 0) | 0 | 0) == (2 | 0) | 0;
return (retasmFunc0.add(1 | 0, 1 | 0) | 0 | 0) == (2 | 0);
}

if (!check2()) throw 'assertion failed: ( assert_return ( invoke add ( i32.const 1 ) ( i32.const 1 ) ) ( i32.const 2 ) )';
2 changes: 1 addition & 1 deletion test/wasm2js.traps.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function check1() {

if (!check1()) throw 'assertion failed: ( assert_return ( invoke empty ) )';
function check2() {
return (retasmFunc0.add(1 | 0, 1 | 0) | 0 | 0) == (2 | 0) | 0;
return (retasmFunc0.add(1 | 0, 1 | 0) | 0 | 0) == (2 | 0);
}

if (!check2()) throw 'assertion failed: ( assert_return ( invoke add ( i32.const 1 ) ( i32.const 1 ) ) ( i32.const 2 ) )';
Expand Down
40 changes: 20 additions & 20 deletions test/wasm2js/br.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,13 +574,13 @@ function asmFunc(env) {
i64toi32_i32$2 = i64toi32_i32$0;
i64toi32_i32$0 = 0;
i64toi32_i32$3 = 32;
i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0;
if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) {
i64toi32_i32$4 = i64toi32_i32$3 & 31;
if (32 >>> 0 <= (i64toi32_i32$3 & 63) >>> 0) {
i64toi32_i32$0 = 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4;
} else {
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0;
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4;
$7_1 = (((1 << i64toi32_i32$4) - 1 | 0) & i64toi32_i32$1) << (32 - i64toi32_i32$4 | 0) | i64toi32_i32$2 >>> i64toi32_i32$4;
}
setTempRet0($7_1 | 0);
i64toi32_i32$0 = $0$hi;
Expand All @@ -596,13 +596,13 @@ function asmFunc(env) {
i64toi32_i32$2 = i64toi32_i32$0;
i64toi32_i32$0 = 0;
i64toi32_i32$3 = 32;
i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0;
if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) {
i64toi32_i32$4 = i64toi32_i32$3 & 31;
if (32 >>> 0 <= (i64toi32_i32$3 & 63) >>> 0) {
i64toi32_i32$0 = 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4;
} else {
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0;
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4;
$7_1 = (((1 << i64toi32_i32$4) - 1 | 0) & i64toi32_i32$1) << (32 - i64toi32_i32$4 | 0) | i64toi32_i32$2 >>> i64toi32_i32$4;
}
setTempRet0($7_1 | 0);
i64toi32_i32$0 = $0$hi;
Expand All @@ -618,13 +618,13 @@ function asmFunc(env) {
i64toi32_i32$2 = i64toi32_i32$0;
i64toi32_i32$0 = 0;
i64toi32_i32$3 = 32;
i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0;
if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) {
i64toi32_i32$4 = i64toi32_i32$3 & 31;
if (32 >>> 0 <= (i64toi32_i32$3 & 63) >>> 0) {
i64toi32_i32$0 = 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4;
} else {
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0;
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4;
$7_1 = (((1 << i64toi32_i32$4) - 1 | 0) & i64toi32_i32$1) << (32 - i64toi32_i32$4 | 0) | i64toi32_i32$2 >>> i64toi32_i32$4;
}
setTempRet0($7_1 | 0);
i64toi32_i32$0 = $0$hi;
Expand All @@ -640,13 +640,13 @@ function asmFunc(env) {
i64toi32_i32$2 = i64toi32_i32$0;
i64toi32_i32$0 = 0;
i64toi32_i32$3 = 32;
i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0;
if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) {
i64toi32_i32$4 = i64toi32_i32$3 & 31;
if (32 >>> 0 <= (i64toi32_i32$3 & 63) >>> 0) {
i64toi32_i32$0 = 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4;
} else {
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0;
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4;
$7_1 = (((1 << i64toi32_i32$4) - 1 | 0) & i64toi32_i32$1) << (32 - i64toi32_i32$4 | 0) | i64toi32_i32$2 >>> i64toi32_i32$4;
}
setTempRet0($7_1 | 0);
i64toi32_i32$0 = $0$hi;
Expand Down
40 changes: 20 additions & 20 deletions test/wasm2js/br_table.2asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13271,13 +13271,13 @@ function asmFunc(env) {
i64toi32_i32$2 = i64toi32_i32$0;
i64toi32_i32$0 = 0;
i64toi32_i32$3 = 32;
i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0;
if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) {
i64toi32_i32$4 = i64toi32_i32$3 & 31;
if (32 >>> 0 <= (i64toi32_i32$3 & 63) >>> 0) {
i64toi32_i32$0 = 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4;
} else {
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0;
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4;
$7_1 = (((1 << i64toi32_i32$4) - 1 | 0) & i64toi32_i32$1) << (32 - i64toi32_i32$4 | 0) | i64toi32_i32$2 >>> i64toi32_i32$4;
}
setTempRet0($7_1 | 0);
i64toi32_i32$0 = $0$hi;
Expand All @@ -13293,13 +13293,13 @@ function asmFunc(env) {
i64toi32_i32$2 = i64toi32_i32$0;
i64toi32_i32$0 = 0;
i64toi32_i32$3 = 32;
i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0;
if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) {
i64toi32_i32$4 = i64toi32_i32$3 & 31;
if (32 >>> 0 <= (i64toi32_i32$3 & 63) >>> 0) {
i64toi32_i32$0 = 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4;
} else {
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0;
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4;
$7_1 = (((1 << i64toi32_i32$4) - 1 | 0) & i64toi32_i32$1) << (32 - i64toi32_i32$4 | 0) | i64toi32_i32$2 >>> i64toi32_i32$4;
}
setTempRet0($7_1 | 0);
i64toi32_i32$0 = $0$hi;
Expand All @@ -13315,13 +13315,13 @@ function asmFunc(env) {
i64toi32_i32$2 = i64toi32_i32$0;
i64toi32_i32$0 = 0;
i64toi32_i32$3 = 32;
i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0;
if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) {
i64toi32_i32$4 = i64toi32_i32$3 & 31;
if (32 >>> 0 <= (i64toi32_i32$3 & 63) >>> 0) {
i64toi32_i32$0 = 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4;
} else {
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0;
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4;
$7_1 = (((1 << i64toi32_i32$4) - 1 | 0) & i64toi32_i32$1) << (32 - i64toi32_i32$4 | 0) | i64toi32_i32$2 >>> i64toi32_i32$4;
}
setTempRet0($7_1 | 0);
i64toi32_i32$0 = $0$hi;
Expand All @@ -13337,13 +13337,13 @@ function asmFunc(env) {
i64toi32_i32$2 = i64toi32_i32$0;
i64toi32_i32$0 = 0;
i64toi32_i32$3 = 32;
i64toi32_i32$4 = i64toi32_i32$3 & 31 | 0;
if (32 >>> 0 <= (i64toi32_i32$3 & 63 | 0) >>> 0) {
i64toi32_i32$4 = i64toi32_i32$3 & 31;
if (32 >>> 0 <= (i64toi32_i32$3 & 63) >>> 0) {
i64toi32_i32$0 = 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = i64toi32_i32$1 >>> i64toi32_i32$4;
} else {
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4 | 0;
$7_1 = (((1 << i64toi32_i32$4 | 0) - 1 | 0) & i64toi32_i32$1 | 0) << (32 - i64toi32_i32$4 | 0) | 0 | (i64toi32_i32$2 >>> i64toi32_i32$4 | 0) | 0;
i64toi32_i32$0 = i64toi32_i32$1 >>> i64toi32_i32$4;
$7_1 = (((1 << i64toi32_i32$4) - 1 | 0) & i64toi32_i32$1) << (32 - i64toi32_i32$4 | 0) | i64toi32_i32$2 >>> i64toi32_i32$4;
}
setTempRet0($7_1 | 0);
i64toi32_i32$0 = $0$hi;
Expand Down
Loading