Skip to content

Precompute power if both arguments are constants (revisit) #2330

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

Merged
merged 3 commits into from
Jun 20, 2022
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
80 changes: 80 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5187,6 +5187,22 @@ export class Compiler extends DiagnosticEmitter {
}
case TypeKind.I32:
case TypeKind.U32: {
if (this.options.willOptimize) {
// Precompute power if LHS and RHS constants
// TODO: move this optimization to AIR
if (
getExpressionId(leftExpr) == ExpressionId.Const &&
getExpressionId(rightExpr) == ExpressionId.Const
) {
let leftValue = getConstValueI32(leftExpr);
let rightValue = getConstValueI32(rightExpr);
this.currentType = type;
return module.i32(i64_low(i64_pow(
i64_new(leftValue),
i64_new(rightValue)
)));
}
}
let instance = this.i32PowInstance;
if (!instance) {
let prototype = this.program.lookup(CommonNames.ipow32);
Expand All @@ -5213,6 +5229,20 @@ export class Compiler extends DiagnosticEmitter {
}
case TypeKind.I64:
case TypeKind.U64: {
if (this.options.willOptimize) {
// Precompute power if LHS and RHS constants
// TODO: move this optimization to AIR
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);
this.currentType = type;
return module.i64(i64_low(result), i64_high(result));
}
}
let instance = this.i64PowInstance;
if (!instance) {
let prototype = this.program.lookup(CommonNames.ipow64);
Expand All @@ -5234,6 +5264,30 @@ export class Compiler extends DiagnosticEmitter {
case TypeKind.ISIZE:
case TypeKind.USIZE: {
let isWasm64 = this.options.isWasm64;
if (this.options.willOptimize) {
// Precompute power if LHS and RHS constants
// TODO: move this optimization to AIR
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);
this.currentType = type;
return module.i64(i64_low(result), i64_high(result));
} else {
let leftValue = getConstValueI32(leftExpr);
let rightValue = getConstValueI32(rightExpr);
this.currentType = type;
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.lookup(isWasm64 ? CommonNames.ipow64 : CommonNames.ipow32);
Expand All @@ -5258,6 +5312,19 @@ export class Compiler extends DiagnosticEmitter {
return this.makeCallDirect(instance, [ leftExpr, rightExpr ], reportNode);
}
case TypeKind.F32: {
if (this.options.willOptimize) {
// Precompute power if LHS and RHS constants
// TODO: move this optimization to AIR
if (
getExpressionId(leftExpr) == ExpressionId.Const &&
getExpressionId(rightExpr) == ExpressionId.Const
) {
let leftValue = getConstValueF32(leftExpr);
let rightValue = getConstValueF32(rightExpr);
this.currentType = type;
return module.f32(f32(Math.pow(leftValue, rightValue)));
}
}
let instance = this.f32PowInstance;
if (!instance) {
let namespace = this.program.lookup(CommonNames.Mathf);
Expand Down Expand Up @@ -5287,6 +5354,19 @@ export class Compiler extends DiagnosticEmitter {
}
// Math.pow otherwise (result is f64)
case TypeKind.F64: {
if (this.options.willOptimize) {
// Precompute power if LHS and RHS constants
// TODO: move this optimization to AIR
if (
getExpressionId(leftExpr) == ExpressionId.Const &&
getExpressionId(rightExpr) == ExpressionId.Const
) {
let leftValue = getConstValueF64(leftExpr);
let rightValue = getConstValueF64(rightExpr);
this.currentType = type;
return module.f64(Math.pow(leftValue, rightValue));
}
}
let instance = this.f64PowInstance;
if (!instance) {
let namespace = this.program.lookup(CommonNames.Math);
Expand Down
Loading