Skip to content

Commit 291f9b6

Browse files
authored
Precompute power if both arguments are constants (#2330)
1 parent c5bc37e commit 291f9b6

File tree

2 files changed

+80
-588
lines changed

2 files changed

+80
-588
lines changed

src/compiler.ts

+80
Original file line numberDiff line numberDiff line change
@@ -5187,6 +5187,22 @@ export class Compiler extends DiagnosticEmitter {
51875187
}
51885188
case TypeKind.I32:
51895189
case TypeKind.U32: {
5190+
if (this.options.willOptimize) {
5191+
// Precompute power if LHS and RHS constants
5192+
// TODO: move this optimization to AIR
5193+
if (
5194+
getExpressionId(leftExpr) == ExpressionId.Const &&
5195+
getExpressionId(rightExpr) == ExpressionId.Const
5196+
) {
5197+
let leftValue = getConstValueI32(leftExpr);
5198+
let rightValue = getConstValueI32(rightExpr);
5199+
this.currentType = type;
5200+
return module.i32(i64_low(i64_pow(
5201+
i64_new(leftValue),
5202+
i64_new(rightValue)
5203+
)));
5204+
}
5205+
}
51905206
let instance = this.i32PowInstance;
51915207
if (!instance) {
51925208
let prototype = this.program.lookup(CommonNames.ipow32);
@@ -5213,6 +5229,20 @@ export class Compiler extends DiagnosticEmitter {
52135229
}
52145230
case TypeKind.I64:
52155231
case TypeKind.U64: {
5232+
if (this.options.willOptimize) {
5233+
// Precompute power if LHS and RHS constants
5234+
// TODO: move this optimization to AIR
5235+
if (
5236+
getExpressionId(leftExpr) == ExpressionId.Const &&
5237+
getExpressionId(rightExpr) == ExpressionId.Const
5238+
) {
5239+
let leftValue = i64_new(getConstValueI64Low(leftExpr), getConstValueI64High(leftExpr));
5240+
let rightValue = i64_new(getConstValueI64Low(rightExpr), getConstValueI64High(rightExpr));
5241+
let result = i64_pow(leftValue, rightValue);
5242+
this.currentType = type;
5243+
return module.i64(i64_low(result), i64_high(result));
5244+
}
5245+
}
52165246
let instance = this.i64PowInstance;
52175247
if (!instance) {
52185248
let prototype = this.program.lookup(CommonNames.ipow64);
@@ -5234,6 +5264,30 @@ export class Compiler extends DiagnosticEmitter {
52345264
case TypeKind.ISIZE:
52355265
case TypeKind.USIZE: {
52365266
let isWasm64 = this.options.isWasm64;
5267+
if (this.options.willOptimize) {
5268+
// Precompute power if LHS and RHS constants
5269+
// TODO: move this optimization to AIR
5270+
if (
5271+
getExpressionId(leftExpr) == ExpressionId.Const &&
5272+
getExpressionId(rightExpr) == ExpressionId.Const
5273+
) {
5274+
if (isWasm64) {
5275+
let leftValue = i64_new(getConstValueI64Low(leftExpr), getConstValueI64High(leftExpr));
5276+
let rightValue = i64_new(getConstValueI64Low(rightExpr), getConstValueI64High(rightExpr));
5277+
let result = i64_pow(leftValue, rightValue);
5278+
this.currentType = type;
5279+
return module.i64(i64_low(result), i64_high(result));
5280+
} else {
5281+
let leftValue = getConstValueI32(leftExpr);
5282+
let rightValue = getConstValueI32(rightExpr);
5283+
this.currentType = type;
5284+
return module.i32(i64_low(i64_pow(
5285+
i64_new(leftValue),
5286+
i64_new(rightValue)
5287+
)));
5288+
}
5289+
}
5290+
}
52375291
let instance = isWasm64 ? this.i64PowInstance : this.i32PowInstance;
52385292
if (!instance) {
52395293
let prototype = this.program.lookup(isWasm64 ? CommonNames.ipow64 : CommonNames.ipow32);
@@ -5258,6 +5312,19 @@ export class Compiler extends DiagnosticEmitter {
52585312
return this.makeCallDirect(instance, [ leftExpr, rightExpr ], reportNode);
52595313
}
52605314
case TypeKind.F32: {
5315+
if (this.options.willOptimize) {
5316+
// Precompute power if LHS and RHS constants
5317+
// TODO: move this optimization to AIR
5318+
if (
5319+
getExpressionId(leftExpr) == ExpressionId.Const &&
5320+
getExpressionId(rightExpr) == ExpressionId.Const
5321+
) {
5322+
let leftValue = getConstValueF32(leftExpr);
5323+
let rightValue = getConstValueF32(rightExpr);
5324+
this.currentType = type;
5325+
return module.f32(f32(Math.pow(leftValue, rightValue)));
5326+
}
5327+
}
52615328
let instance = this.f32PowInstance;
52625329
if (!instance) {
52635330
let namespace = this.program.lookup(CommonNames.Mathf);
@@ -5287,6 +5354,19 @@ export class Compiler extends DiagnosticEmitter {
52875354
}
52885355
// Math.pow otherwise (result is f64)
52895356
case TypeKind.F64: {
5357+
if (this.options.willOptimize) {
5358+
// Precompute power if LHS and RHS constants
5359+
// TODO: move this optimization to AIR
5360+
if (
5361+
getExpressionId(leftExpr) == ExpressionId.Const &&
5362+
getExpressionId(rightExpr) == ExpressionId.Const
5363+
) {
5364+
let leftValue = getConstValueF64(leftExpr);
5365+
let rightValue = getConstValueF64(rightExpr);
5366+
this.currentType = type;
5367+
return module.f64(Math.pow(leftValue, rightValue));
5368+
}
5369+
}
52905370
let instance = this.f64PowInstance;
52915371
if (!instance) {
52925372
let namespace = this.program.lookup(CommonNames.Math);

0 commit comments

Comments
 (0)