Skip to content

Commit 6cf5cfb

Browse files
committed
don't const fold for -O0
1 parent 6dc89d0 commit 6cf5cfb

File tree

3 files changed

+795
-53
lines changed

3 files changed

+795
-53
lines changed

src/compiler.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5333,6 +5333,10 @@ export class Compiler extends DiagnosticEmitter {
53335333
makePow(leftExpr: ExpressionRef, rightExpr: ExpressionRef, type: Type, reportNode: Node): ExpressionRef {
53345334
// Cares about garbage bits
53355335
let module = this.module;
5336+
let canOptimize = (
5337+
this.options.optimizeLevelHint != 0 ||
5338+
this.options.shrinkLevelHint != 0
5339+
);
53365340
switch (type.kind) {
53375341
case TypeKind.BOOL: {
53385342
return module.select(
@@ -5352,6 +5356,7 @@ export class Compiler extends DiagnosticEmitter {
53525356
case TypeKind.I32:
53535357
case TypeKind.U32: {
53545358
if (
5359+
canOptimize &&
53555360
getExpressionId(leftExpr) == ExpressionId.Const &&
53565361
getExpressionId(rightExpr) == ExpressionId.Const
53575362
) {
@@ -5389,6 +5394,7 @@ export class Compiler extends DiagnosticEmitter {
53895394
case TypeKind.I64:
53905395
case TypeKind.U64: {
53915396
if (
5397+
canOptimize &&
53925398
getExpressionId(leftExpr) == ExpressionId.Const &&
53935399
getExpressionId(rightExpr) == ExpressionId.Const
53945400
) {
@@ -5419,6 +5425,7 @@ export class Compiler extends DiagnosticEmitter {
54195425
case TypeKind.USIZE: {
54205426
let isWasm64 = this.options.isWasm64;
54215427
if (
5428+
canOptimize &&
54225429
getExpressionId(leftExpr) == ExpressionId.Const &&
54235430
getExpressionId(rightExpr) == ExpressionId.Const
54245431
) {
@@ -5461,6 +5468,7 @@ export class Compiler extends DiagnosticEmitter {
54615468
}
54625469
case TypeKind.F32: {
54635470
if (
5471+
canOptimize &&
54645472
getExpressionId(leftExpr) == ExpressionId.Const &&
54655473
getExpressionId(rightExpr) == ExpressionId.Const
54665474
) {
@@ -5498,6 +5506,7 @@ export class Compiler extends DiagnosticEmitter {
54985506
// Math.pow otherwise (result is f64)
54995507
case TypeKind.F64: {
55005508
if (
5509+
canOptimize &&
55015510
getExpressionId(leftExpr) == ExpressionId.Const &&
55025511
getExpressionId(rightExpr) == ExpressionId.Const
55035512
) {

tests/compiler/resolve-binary.untouched.wat

Lines changed: 214 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4533,6 +4533,211 @@
45334533
local.get $0
45344534
call $~lib/util/number/dtoa
45354535
)
4536+
(func $~lib/math/ipow32 (param $0 i32) (param $1 i32) (result i32)
4537+
(local $2 i32)
4538+
(local $3 i32)
4539+
(local $4 i32)
4540+
i32.const 1
4541+
local.set $2
4542+
i32.const 0
4543+
i32.const 1
4544+
i32.lt_s
4545+
drop
4546+
local.get $1
4547+
i32.const 0
4548+
i32.le_s
4549+
if
4550+
local.get $0
4551+
i32.const -1
4552+
i32.eq
4553+
if
4554+
i32.const -1
4555+
i32.const 1
4556+
local.get $1
4557+
i32.const 1
4558+
i32.and
4559+
select
4560+
return
4561+
end
4562+
local.get $1
4563+
i32.const 0
4564+
i32.eq
4565+
local.get $0
4566+
i32.const 1
4567+
i32.eq
4568+
i32.or
4569+
return
4570+
else
4571+
local.get $1
4572+
i32.const 1
4573+
i32.eq
4574+
if
4575+
local.get $0
4576+
return
4577+
else
4578+
local.get $1
4579+
i32.const 2
4580+
i32.eq
4581+
if
4582+
local.get $0
4583+
local.get $0
4584+
i32.mul
4585+
return
4586+
else
4587+
local.get $1
4588+
i32.const 32
4589+
i32.lt_s
4590+
if
4591+
i32.const 32
4592+
local.get $1
4593+
i32.clz
4594+
i32.sub
4595+
local.set $3
4596+
block $break|0
4597+
block $case4|0
4598+
block $case3|0
4599+
block $case2|0
4600+
block $case1|0
4601+
block $case0|0
4602+
local.get $3
4603+
local.set $4
4604+
local.get $4
4605+
i32.const 5
4606+
i32.eq
4607+
br_if $case0|0
4608+
local.get $4
4609+
i32.const 4
4610+
i32.eq
4611+
br_if $case1|0
4612+
local.get $4
4613+
i32.const 3
4614+
i32.eq
4615+
br_if $case2|0
4616+
local.get $4
4617+
i32.const 2
4618+
i32.eq
4619+
br_if $case3|0
4620+
local.get $4
4621+
i32.const 1
4622+
i32.eq
4623+
br_if $case4|0
4624+
br $break|0
4625+
end
4626+
local.get $1
4627+
i32.const 1
4628+
i32.and
4629+
if
4630+
local.get $2
4631+
local.get $0
4632+
i32.mul
4633+
local.set $2
4634+
end
4635+
local.get $1
4636+
i32.const 1
4637+
i32.shr_u
4638+
local.set $1
4639+
local.get $0
4640+
local.get $0
4641+
i32.mul
4642+
local.set $0
4643+
end
4644+
local.get $1
4645+
i32.const 1
4646+
i32.and
4647+
if
4648+
local.get $2
4649+
local.get $0
4650+
i32.mul
4651+
local.set $2
4652+
end
4653+
local.get $1
4654+
i32.const 1
4655+
i32.shr_u
4656+
local.set $1
4657+
local.get $0
4658+
local.get $0
4659+
i32.mul
4660+
local.set $0
4661+
end
4662+
local.get $1
4663+
i32.const 1
4664+
i32.and
4665+
if
4666+
local.get $2
4667+
local.get $0
4668+
i32.mul
4669+
local.set $2
4670+
end
4671+
local.get $1
4672+
i32.const 1
4673+
i32.shr_u
4674+
local.set $1
4675+
local.get $0
4676+
local.get $0
4677+
i32.mul
4678+
local.set $0
4679+
end
4680+
local.get $1
4681+
i32.const 1
4682+
i32.and
4683+
if
4684+
local.get $2
4685+
local.get $0
4686+
i32.mul
4687+
local.set $2
4688+
end
4689+
local.get $1
4690+
i32.const 1
4691+
i32.shr_u
4692+
local.set $1
4693+
local.get $0
4694+
local.get $0
4695+
i32.mul
4696+
local.set $0
4697+
end
4698+
local.get $1
4699+
i32.const 1
4700+
i32.and
4701+
if
4702+
local.get $2
4703+
local.get $0
4704+
i32.mul
4705+
local.set $2
4706+
end
4707+
end
4708+
local.get $2
4709+
return
4710+
end
4711+
end
4712+
end
4713+
end
4714+
loop $while-continue|1
4715+
local.get $1
4716+
local.set $3
4717+
local.get $3
4718+
if
4719+
local.get $1
4720+
i32.const 1
4721+
i32.and
4722+
if
4723+
local.get $2
4724+
local.get $0
4725+
i32.mul
4726+
local.set $2
4727+
end
4728+
local.get $1
4729+
i32.const 1
4730+
i32.shr_u
4731+
local.set $1
4732+
local.get $0
4733+
local.get $0
4734+
i32.mul
4735+
local.set $0
4736+
br $while-continue|1
4737+
end
4738+
end
4739+
local.get $2
4740+
)
45364741
(func $resolve-binary/Foo#constructor (param $0 i32) (result i32)
45374742
local.get $0
45384743
i32.eqz
@@ -5259,7 +5464,9 @@
52595464
call $~lib/builtins/abort
52605465
unreachable
52615466
end
5262-
i32.const 4
5467+
i32.const 2
5468+
i32.const 2
5469+
call $~lib/math/ipow32
52635470
i32.const 10
52645471
call $~lib/number/I32#toString
52655472
local.tee $26
@@ -5274,7 +5481,9 @@
52745481
call $~lib/builtins/abort
52755482
unreachable
52765483
end
5277-
f64.const 4
5484+
f64.const 2
5485+
f64.const 2
5486+
call $~lib/math/NativeMath.pow
52785487
i32.const 0
52795488
call $~lib/number/F64#toString
52805489
local.tee $27
@@ -5289,7 +5498,9 @@
52895498
call $~lib/builtins/abort
52905499
unreachable
52915500
end
5292-
f64.const 4
5501+
f64.const 2
5502+
f64.const 2
5503+
call $~lib/math/NativeMath.pow
52935504
i32.const 0
52945505
call $~lib/number/F64#toString
52955506
local.tee $28

0 commit comments

Comments
 (0)