Skip to content

Precompute power if both arguments are constants #1529

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

Closed
wants to merge 8 commits into from
Closed
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
54 changes: 54 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5351,6 +5351,17 @@ export class Compiler extends DiagnosticEmitter {
}
case TypeKind.I32:
case TypeKind.U32: {
if (
getExpressionId(leftExpr) == ExpressionId.Const &&
getExpressionId(rightExpr) == ExpressionId.Const
) {
let leftValue = getConstValueI32(leftExpr);
let rightValue = getConstValueI32(rightExpr);
return module.i32(i64_low(i64_pow(
i64_new(leftValue),
i64_new(rightValue)
)));
}
let instance = this.i32PowInstance;
if (!instance) {
let prototype = this.program.lookupGlobal(CommonNames.ipow32);
Expand All @@ -5377,6 +5388,15 @@ export class Compiler extends DiagnosticEmitter {
}
case TypeKind.I64:
case TypeKind.U64: {
if (
getExpressionId(leftExpr) == ExpressionId.Const &&
getExpressionId(rightExpr) == ExpressionId.Const
) {
let leftValue = i64_new(getConstValueI64Low(leftExpr), getConstValueI64High(leftExpr));
let rightValue = i64_new(getConstValueI64Low(rightExpr), getConstValueI64High(rightExpr));
let result = i64_pow(leftValue, rightValue);
return module.i64(i64_low(result), i64_high(result));
}
let instance = this.i64PowInstance;
if (!instance) {
let prototype = this.program.lookupGlobal(CommonNames.ipow64);
Expand All @@ -5398,6 +5418,24 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.ISIZE:
case TypeKind.USIZE: {
let isWasm64 = this.options.isWasm64;
if (
getExpressionId(leftExpr) == ExpressionId.Const &&
getExpressionId(rightExpr) == ExpressionId.Const
) {
if (isWasm64) {
let leftValue = i64_new(getConstValueI64Low(leftExpr), getConstValueI64High(leftExpr));
let rightValue = i64_new(getConstValueI64Low(rightExpr), getConstValueI64High(rightExpr));
let result = i64_pow(leftValue, rightValue);
return module.i64(i64_low(result), i64_high(result));
} else {
let leftValue = getConstValueI32(leftExpr);
let rightValue = getConstValueI32(rightExpr);
return module.i32(i64_low(i64_pow(
i64_new(leftValue),
i64_new(rightValue)
)));
}
}
let instance = isWasm64 ? this.i64PowInstance : this.i32PowInstance;
if (!instance) {
let prototype = this.program.lookupGlobal(isWasm64 ? CommonNames.ipow64 : CommonNames.ipow32);
Expand All @@ -5422,6 +5460,14 @@ export class Compiler extends DiagnosticEmitter {
return this.makeCallDirect(instance, [ leftExpr, rightExpr ], reportNode);
}
case TypeKind.F32: {
if (
getExpressionId(leftExpr) == ExpressionId.Const &&
getExpressionId(rightExpr) == ExpressionId.Const
) {
let leftValue = getConstValueF32(leftExpr);
let rightValue = getConstValueF32(rightExpr);
return module.f32(f32(Math.pow(leftValue, rightValue)));
}
let instance = this.f32PowInstance;
if (!instance) {
let namespace = this.program.lookupGlobal(CommonNames.Mathf);
Expand Down Expand Up @@ -5451,6 +5497,14 @@ export class Compiler extends DiagnosticEmitter {
}
// Math.pow otherwise (result is f64)
case TypeKind.F64: {
if (
getExpressionId(leftExpr) == ExpressionId.Const &&
getExpressionId(rightExpr) == ExpressionId.Const
) {
let leftValue = getConstValueF64(leftExpr);
let rightValue = getConstValueF64(rightExpr);
return module.f64(Math.pow(leftValue, rightValue));
}
let instance = this.f64PowInstance;
if (!instance) {
let namespace = this.program.lookupGlobal(CommonNames.Math);
Expand Down
34 changes: 1 addition & 33 deletions tests/compiler/resolve-binary.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1292,9 +1292,6 @@
local.get $1
)
(func $start:resolve-binary
(local $0 i32)
(local $1 i32)
(local $2 i32)
i32.const 1056
i32.const 1056
call $~lib/string/String.__eq
Expand Down Expand Up @@ -1672,36 +1669,7 @@
call $~lib/builtins/abort
unreachable
end
i32.const 2
local.set $0
i32.const 2
local.set $1
i32.const 1
local.set $2
loop $while-continue|0
local.get $1
if
local.get $0
local.get $2
i32.mul
local.get $2
local.get $1
i32.const 1
i32.and
select
local.set $2
local.get $1
i32.const 1
i32.shr_u
local.set $1
local.get $0
local.get $0
i32.mul
local.set $0
br $while-continue|0
end
end
local.get $2
i32.const 4
call $~lib/number/I32#toString
i32.const 2752
call $~lib/string/String.__eq
Expand Down
217 changes: 3 additions & 214 deletions tests/compiler/resolve-binary.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4533,211 +4533,6 @@
local.get $0
call $~lib/util/number/dtoa
)
(func $~lib/math/ipow32 (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
i32.const 1
local.set $2
i32.const 0
i32.const 1
i32.lt_s
drop
local.get $1
i32.const 0
i32.le_s
if
local.get $0
i32.const -1
i32.eq
if
i32.const -1
i32.const 1
local.get $1
i32.const 1
i32.and
select
return
end
local.get $1
i32.const 0
i32.eq
local.get $0
i32.const 1
i32.eq
i32.or
return
else
local.get $1
i32.const 1
i32.eq
if
local.get $0
return
else
local.get $1
i32.const 2
i32.eq
if
local.get $0
local.get $0
i32.mul
return
else
local.get $1
i32.const 32
i32.lt_s
if
i32.const 32
local.get $1
i32.clz
i32.sub
local.set $3
block $break|0
block $case4|0
block $case3|0
block $case2|0
block $case1|0
block $case0|0
local.get $3
local.set $4
local.get $4
i32.const 5
i32.eq
br_if $case0|0
local.get $4
i32.const 4
i32.eq
br_if $case1|0
local.get $4
i32.const 3
i32.eq
br_if $case2|0
local.get $4
i32.const 2
i32.eq
br_if $case3|0
local.get $4
i32.const 1
i32.eq
br_if $case4|0
br $break|0
end
local.get $1
i32.const 1
i32.and
if
local.get $2
local.get $0
i32.mul
local.set $2
end
local.get $1
i32.const 1
i32.shr_u
local.set $1
local.get $0
local.get $0
i32.mul
local.set $0
end
local.get $1
i32.const 1
i32.and
if
local.get $2
local.get $0
i32.mul
local.set $2
end
local.get $1
i32.const 1
i32.shr_u
local.set $1
local.get $0
local.get $0
i32.mul
local.set $0
end
local.get $1
i32.const 1
i32.and
if
local.get $2
local.get $0
i32.mul
local.set $2
end
local.get $1
i32.const 1
i32.shr_u
local.set $1
local.get $0
local.get $0
i32.mul
local.set $0
end
local.get $1
i32.const 1
i32.and
if
local.get $2
local.get $0
i32.mul
local.set $2
end
local.get $1
i32.const 1
i32.shr_u
local.set $1
local.get $0
local.get $0
i32.mul
local.set $0
end
local.get $1
i32.const 1
i32.and
if
local.get $2
local.get $0
i32.mul
local.set $2
end
end
local.get $2
return
end
end
end
end
loop $while-continue|1
local.get $1
local.set $3
local.get $3
if
local.get $1
i32.const 1
i32.and
if
local.get $2
local.get $0
i32.mul
local.set $2
end
local.get $1
i32.const 1
i32.shr_u
local.set $1
local.get $0
local.get $0
i32.mul
local.set $0
br $while-continue|1
end
end
local.get $2
)
(func $resolve-binary/Foo#constructor (param $0 i32) (result i32)
local.get $0
i32.eqz
Expand Down Expand Up @@ -5464,9 +5259,7 @@
call $~lib/builtins/abort
unreachable
end
i32.const 2
i32.const 2
call $~lib/math/ipow32
i32.const 4
i32.const 10
call $~lib/number/I32#toString
local.tee $26
Expand All @@ -5481,9 +5274,7 @@
call $~lib/builtins/abort
unreachable
end
f64.const 2
f64.const 2
call $~lib/math/NativeMath.pow
f64.const 4
i32.const 0
call $~lib/number/F64#toString
local.tee $27
Expand All @@ -5498,9 +5289,7 @@
call $~lib/builtins/abort
unreachable
end
f64.const 2
f64.const 2
call $~lib/math/NativeMath.pow
f64.const 4
i32.const 0
call $~lib/number/F64#toString
local.tee $28
Expand Down
Loading