diff --git a/src/module.ts b/src/module.ts index 064e438866..938c122f91 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1514,6 +1514,7 @@ export class Module { passes.push("remove-unused-brs"); passes.push("remove-unused-names"); passes.push("merge-blocks"); + passes.push("precompute"); } if (optimizeLevel >= 3) { passes.push("flatten"); @@ -1545,7 +1546,6 @@ export class Module { } else { passes.push("precompute"); } - passes.push("vacuum"); if (optimizeLevel >= 2 || shrinkLevel >= 1) { passes.push("pick-load-signs"); passes.push("simplify-globals-optimizing"); @@ -1633,7 +1633,6 @@ export class Module { passes.push("vacuum"); passes.push("precompute-propagate"); - passes.push("vacuum"); // replace indirect with direct calls again and inline passes.push("inlining-optimizing"); diff --git a/tests/compiler/const-folding.json b/tests/compiler/const-folding.json new file mode 100644 index 0000000000..453cb07770 --- /dev/null +++ b/tests/compiler/const-folding.json @@ -0,0 +1,5 @@ +{ + "asc_flags": [ + "--runtime none" + ] +} diff --git a/tests/compiler/const-folding.optimized.wat b/tests/compiler/const-folding.optimized.wat new file mode 100644 index 0000000000..14b4c71317 --- /dev/null +++ b/tests/compiler/const-folding.optimized.wat @@ -0,0 +1,15 @@ +(module + (type $i64_=>_i64 (func (param i64) (result i64))) + (memory $0 0) + (export "memory" (memory $0)) + (export "test" (func $const-folding/test)) + (func $const-folding/test (param $0 i64) (result i64) + local.get $0 + i64.const 1 + i64.shl + local.get $0 + i64.const 63 + i64.shr_u + i64.add + ) +) diff --git a/tests/compiler/const-folding.ts b/tests/compiler/const-folding.ts new file mode 100644 index 0000000000..a29303b891 --- /dev/null +++ b/tests/compiler/const-folding.ts @@ -0,0 +1,11 @@ +export function test(value: u64): u64 { + var mod1: u64 = -1; + var mod2: u64 = -1; + + var rlo = value << 1; + var rhi = rlo & ~mod2; + + rhi |= rhi << 1; + rhi |= (value >> 63) & mod1; + return rlo + rhi; +} diff --git a/tests/compiler/const-folding.untouched.wat b/tests/compiler/const-folding.untouched.wat new file mode 100644 index 0000000000..251ac9f6a8 --- /dev/null +++ b/tests/compiler/const-folding.untouched.wat @@ -0,0 +1,44 @@ +(module + (type $i64_=>_i64 (func (param i64) (result i64))) + (memory $0 0) + (table $0 1 funcref) + (export "memory" (memory $0)) + (export "test" (func $const-folding/test)) + (func $const-folding/test (param $0 i64) (result i64) + (local $1 i64) + (local $2 i64) + (local $3 i64) + (local $4 i64) + i64.const -1 + local.set $1 + i64.const -1 + local.set $2 + local.get $0 + i64.const 1 + i64.shl + local.set $3 + local.get $3 + local.get $2 + i64.const -1 + i64.xor + i64.and + local.set $4 + local.get $4 + local.get $4 + i64.const 1 + i64.shl + i64.or + local.set $4 + local.get $4 + local.get $0 + i64.const 63 + i64.shr_u + local.get $1 + i64.and + i64.or + local.set $4 + local.get $3 + local.get $4 + i64.add + ) +) diff --git a/tests/compiler/std/map.optimized.wat b/tests/compiler/std/map.optimized.wat index 826e9f51d9..ca6ae81d81 100644 --- a/tests/compiler/std/map.optimized.wat +++ b/tests/compiler/std/map.optimized.wat @@ -5627,28 +5627,28 @@ i32.const 11 call $~lib/rt/tlsf/__alloc call $~lib/rt/pure/__retain - local.tee $7 + local.tee $0 i32.const 16 call $~lib/arraybuffer/ArrayBuffer#constructor i32.store - local.get $7 + local.get $0 i32.const 3 i32.store offset=4 - local.get $7 + local.get $0 i32.const 48 call $~lib/arraybuffer/ArrayBuffer#constructor i32.store offset=8 - local.get $7 + local.get $0 i32.const 4 i32.store offset=12 - local.get $7 + local.get $0 i32.const 0 i32.store offset=16 - local.get $7 + local.get $0 i32.const 0 i32.store offset=20 loop $for-loop|1 - local.get $6 + local.get $1 i32.const 16 i32.shl i32.const 16 @@ -5656,8 +5656,8 @@ i32.const 100 i32.lt_s if - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#has if i32.const 0 @@ -5667,9 +5667,9 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 - local.get $6 + local.get $0 + local.get $1 + local.get $1 i32.const 16 i32.shl i32.const 16 @@ -5678,8 +5678,8 @@ i32.add call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#has i32.eqz if @@ -5690,10 +5690,10 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#get - local.get $6 + local.get $1 i32.const 16 i32.shl i32.const 16 @@ -5709,14 +5709,14 @@ call $~lib/builtins/abort unreachable end - local.get $6 + local.get $1 i32.const 1 i32.add - local.set $6 + local.set $1 br $for-loop|1 end end - local.get $7 + local.get $0 i32.load offset=20 i32.const 100 i32.ne @@ -5729,9 +5729,9 @@ unreachable end i32.const 0 - local.set $6 + local.set $1 loop $for-loop|3 - local.get $6 + local.get $1 i32.const 16 i32.shl i32.const 16 @@ -5739,8 +5739,8 @@ i32.const 100 i32.lt_s if - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#has i32.eqz if @@ -5751,10 +5751,10 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#get - local.get $6 + local.get $1 i32.const 16 i32.shl i32.const 16 @@ -5770,9 +5770,9 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 - local.get $6 + local.get $0 + local.get $1 + local.get $1 i32.const 16 i32.shl i32.const 16 @@ -5781,8 +5781,8 @@ i32.add call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#has i32.eqz if @@ -5793,10 +5793,10 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#get - local.get $6 + local.get $1 i32.const 16 i32.shl i32.const 16 @@ -5812,14 +5812,14 @@ call $~lib/builtins/abort unreachable end - local.get $6 + local.get $1 i32.const 1 i32.add - local.set $6 + local.set $1 br $for-loop|3 end end - local.get $7 + local.get $0 i32.load offset=20 i32.const 100 i32.ne @@ -5831,47 +5831,46 @@ call $~lib/builtins/abort unreachable end - local.get $7 + local.get $0 call $~lib/map/Map#keys - local.set $5 - local.get $7 + local.set $4 + local.get $0 call $~lib/map/Map#values - local.set $3 + local.set $6 i32.const 24 i32.const 13 call $~lib/rt/tlsf/__alloc call $~lib/rt/pure/__retain - local.tee $6 + local.tee $3 i32.const 16 call $~lib/arraybuffer/ArrayBuffer#constructor i32.store - local.get $6 + local.get $3 i32.const 3 i32.store offset=4 - local.get $6 + local.get $3 i32.const 32 call $~lib/arraybuffer/ArrayBuffer#constructor i32.store offset=8 - local.get $6 + local.get $3 i32.const 4 i32.store offset=12 - local.get $6 + local.get $3 i32.const 0 i32.store offset=16 - local.get $6 + local.get $3 i32.const 0 i32.store offset=20 call $~lib/map/Map#constructor - local.set $4 + local.set $5 loop $for-loop|4 - local.get $1 - local.get $5 + local.get $2 + local.get $4 i32.load offset=12 i32.lt_s if - local.get $1 - local.tee $0 - local.get $5 + local.get $2 + local.get $4 i32.load offset=12 i32.ge_u if @@ -5882,19 +5881,19 @@ call $~lib/builtins/abort unreachable end - local.get $5 + local.get $4 i32.load offset=4 - local.get $0 + local.get $2 i32.const 1 i32.shl i32.add i32.load16_s local.set $1 - local.get $3 - local.get $0 + local.get $6 + local.get $2 call $~lib/array/Array#__get - local.set $2 - local.get $7 + local.set $7 + local.get $0 local.get $1 call $~lib/map/Map#has i32.eqz @@ -5906,8 +5905,8 @@ call $~lib/builtins/abort unreachable end + local.get $0 local.get $7 - local.get $2 i32.const 20 i32.sub call $~lib/map/Map#has @@ -5920,27 +5919,27 @@ call $~lib/builtins/abort unreachable end - local.get $6 + local.get $3 local.get $1 local.get $1 call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $4 - local.get $2 + local.get $5 + local.get $7 i32.const 20 i32.sub local.tee $1 local.get $1 call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $0 + local.get $2 i32.const 1 i32.add - local.set $1 + local.set $2 br $for-loop|4 end end - local.get $6 + local.get $3 i32.load offset=20 i32.const 100 i32.ne @@ -5952,7 +5951,7 @@ call $~lib/builtins/abort unreachable end - local.get $4 + local.get $5 i32.load offset=20 i32.const 100 i32.ne @@ -5965,9 +5964,9 @@ unreachable end i32.const 0 - local.set $1 + local.set $2 loop $for-loop|6 - local.get $1 + local.get $2 i32.const 16 i32.shl i32.const 16 @@ -5975,8 +5974,8 @@ i32.const 50 i32.lt_s if - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has i32.eqz if @@ -5987,10 +5986,10 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#get - local.get $1 + local.get $2 i32.const 16 i32.shl i32.const 16 @@ -6006,11 +6005,11 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#delete - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has if i32.const 0 @@ -6020,14 +6019,14 @@ call $~lib/builtins/abort unreachable end - local.get $1 + local.get $2 i32.const 1 i32.add - local.set $1 + local.set $2 br $for-loop|6 end end - local.get $7 + local.get $0 i32.load offset=20 i32.const 50 i32.ne @@ -6040,9 +6039,9 @@ unreachable end i32.const 0 - local.set $1 + local.set $2 loop $for-loop|8 - local.get $1 + local.get $2 i32.const 16 i32.shl i32.const 16 @@ -6050,8 +6049,8 @@ i32.const 50 i32.lt_s if - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has if i32.const 0 @@ -6061,9 +6060,9 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $1 - local.get $1 + local.get $0 + local.get $2 + local.get $2 i32.const 16 i32.shl i32.const 16 @@ -6072,8 +6071,8 @@ i32.add call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has i32.eqz if @@ -6084,11 +6083,11 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#delete - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has if i32.const 0 @@ -6098,14 +6097,14 @@ call $~lib/builtins/abort unreachable end - local.get $1 + local.get $2 i32.const 1 i32.add - local.set $1 + local.set $2 br $for-loop|8 end end - local.get $7 + local.get $0 i32.load offset=20 i32.const 50 i32.ne @@ -6117,9 +6116,9 @@ call $~lib/builtins/abort unreachable end - local.get $7 + local.get $0 call $~lib/map/Map#clear - local.get $7 + local.get $0 i32.load offset=20 if i32.const 0 @@ -6129,15 +6128,15 @@ call $~lib/builtins/abort unreachable end - local.get $5 - call $~lib/rt/pure/__release - local.get $3 + local.get $4 call $~lib/rt/pure/__release local.get $6 call $~lib/rt/pure/__release - local.get $4 + local.get $3 call $~lib/rt/pure/__release - local.get $7 + local.get $5 + call $~lib/rt/pure/__release + local.get $0 call $~lib/rt/pure/__release ) (func $~lib/map/Map#has (param $0 i32) (param $1 i32) (result i32) @@ -6862,35 +6861,35 @@ i32.const 14 call $~lib/rt/tlsf/__alloc call $~lib/rt/pure/__retain - local.tee $7 + local.tee $0 i32.const 16 call $~lib/arraybuffer/ArrayBuffer#constructor i32.store - local.get $7 + local.get $0 i32.const 3 i32.store offset=4 - local.get $7 + local.get $0 i32.const 48 call $~lib/arraybuffer/ArrayBuffer#constructor i32.store offset=8 - local.get $7 + local.get $0 i32.const 4 i32.store offset=12 - local.get $7 + local.get $0 i32.const 0 i32.store offset=16 - local.get $7 + local.get $0 i32.const 0 i32.store offset=20 loop $for-loop|1 - local.get $6 + local.get $1 i32.const 65535 i32.and i32.const 100 i32.lt_u if - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#has if i32.const 0 @@ -6900,17 +6899,17 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 - local.get $6 + local.get $0 + local.get $1 + local.get $1 i32.const 65535 i32.and i32.const 10 i32.add call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#has i32.eqz if @@ -6921,10 +6920,10 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#get - local.get $6 + local.get $1 i32.const 65535 i32.and i32.const 10 @@ -6938,14 +6937,14 @@ call $~lib/builtins/abort unreachable end - local.get $6 + local.get $1 i32.const 1 i32.add - local.set $6 + local.set $1 br $for-loop|1 end end - local.get $7 + local.get $0 i32.load offset=20 i32.const 100 i32.ne @@ -6958,16 +6957,16 @@ unreachable end i32.const 0 - local.set $6 + local.set $1 loop $for-loop|3 - local.get $6 + local.get $1 i32.const 65535 i32.and i32.const 100 i32.lt_u if - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#has i32.eqz if @@ -6978,10 +6977,10 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#get - local.get $6 + local.get $1 i32.const 65535 i32.and i32.const 10 @@ -6995,17 +6994,17 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 - local.get $6 + local.get $0 + local.get $1 + local.get $1 i32.const 65535 i32.and i32.const 20 i32.add call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#has i32.eqz if @@ -7016,10 +7015,10 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $6 + local.get $0 + local.get $1 call $~lib/map/Map#get - local.get $6 + local.get $1 i32.const 65535 i32.and i32.const 20 @@ -7033,14 +7032,14 @@ call $~lib/builtins/abort unreachable end - local.get $6 + local.get $1 i32.const 1 i32.add - local.set $6 + local.set $1 br $for-loop|3 end end - local.get $7 + local.get $0 i32.load offset=20 i32.const 100 i32.ne @@ -7052,47 +7051,46 @@ call $~lib/builtins/abort unreachable end - local.get $7 + local.get $0 call $~lib/map/Map#keys - local.set $5 - local.get $7 + local.set $4 + local.get $0 call $~lib/map/Map#values - local.set $3 + local.set $6 i32.const 24 i32.const 16 call $~lib/rt/tlsf/__alloc call $~lib/rt/pure/__retain - local.tee $6 + local.tee $3 i32.const 16 call $~lib/arraybuffer/ArrayBuffer#constructor i32.store - local.get $6 + local.get $3 i32.const 3 i32.store offset=4 - local.get $6 + local.get $3 i32.const 32 call $~lib/arraybuffer/ArrayBuffer#constructor i32.store offset=8 - local.get $6 + local.get $3 i32.const 4 i32.store offset=12 - local.get $6 + local.get $3 i32.const 0 i32.store offset=16 - local.get $6 + local.get $3 i32.const 0 i32.store offset=20 call $~lib/map/Map#constructor - local.set $4 + local.set $5 loop $for-loop|4 - local.get $1 - local.get $5 + local.get $2 + local.get $4 i32.load offset=12 i32.lt_s if - local.get $1 - local.tee $0 - local.get $5 + local.get $2 + local.get $4 i32.load offset=12 i32.ge_u if @@ -7103,19 +7101,19 @@ call $~lib/builtins/abort unreachable end - local.get $5 + local.get $4 i32.load offset=4 - local.get $0 + local.get $2 i32.const 1 i32.shl i32.add i32.load16_u local.set $1 - local.get $3 - local.get $0 + local.get $6 + local.get $2 call $~lib/array/Array#__get - local.set $2 - local.get $7 + local.set $7 + local.get $0 local.get $1 call $~lib/map/Map#has i32.eqz @@ -7127,8 +7125,8 @@ call $~lib/builtins/abort unreachable end + local.get $0 local.get $7 - local.get $2 i32.const 20 i32.sub call $~lib/map/Map#has @@ -7141,27 +7139,27 @@ call $~lib/builtins/abort unreachable end - local.get $6 + local.get $3 local.get $1 local.get $1 call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $4 - local.get $2 + local.get $5 + local.get $7 i32.const 20 i32.sub local.tee $1 local.get $1 call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $0 + local.get $2 i32.const 1 i32.add - local.set $1 + local.set $2 br $for-loop|4 end end - local.get $6 + local.get $3 i32.load offset=20 i32.const 100 i32.ne @@ -7173,7 +7171,7 @@ call $~lib/builtins/abort unreachable end - local.get $4 + local.get $5 i32.load offset=20 i32.const 100 i32.ne @@ -7186,16 +7184,16 @@ unreachable end i32.const 0 - local.set $1 + local.set $2 loop $for-loop|6 - local.get $1 + local.get $2 i32.const 65535 i32.and i32.const 50 i32.lt_u if - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has i32.eqz if @@ -7206,10 +7204,10 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#get - local.get $1 + local.get $2 i32.const 65535 i32.and i32.const 20 @@ -7223,11 +7221,11 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#delete - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has if i32.const 0 @@ -7237,14 +7235,14 @@ call $~lib/builtins/abort unreachable end - local.get $1 + local.get $2 i32.const 1 i32.add - local.set $1 + local.set $2 br $for-loop|6 end end - local.get $7 + local.get $0 i32.load offset=20 i32.const 50 i32.ne @@ -7257,16 +7255,16 @@ unreachable end i32.const 0 - local.set $1 + local.set $2 loop $for-loop|8 - local.get $1 + local.get $2 i32.const 65535 i32.and i32.const 50 i32.lt_u if - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has if i32.const 0 @@ -7276,17 +7274,17 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $1 - local.get $1 + local.get $0 + local.get $2 + local.get $2 i32.const 65535 i32.and i32.const 10 i32.add call $~lib/map/Map#set call $~lib/rt/pure/__release - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has i32.eqz if @@ -7297,11 +7295,11 @@ call $~lib/builtins/abort unreachable end - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#delete - local.get $7 - local.get $1 + local.get $0 + local.get $2 call $~lib/map/Map#has if i32.const 0 @@ -7311,14 +7309,14 @@ call $~lib/builtins/abort unreachable end - local.get $1 + local.get $2 i32.const 1 i32.add - local.set $1 + local.set $2 br $for-loop|8 end end - local.get $7 + local.get $0 i32.load offset=20 i32.const 50 i32.ne @@ -7330,9 +7328,9 @@ call $~lib/builtins/abort unreachable end - local.get $7 + local.get $0 call $~lib/map/Map#clear - local.get $7 + local.get $0 i32.load offset=20 if i32.const 0 @@ -7342,15 +7340,15 @@ call $~lib/builtins/abort unreachable end - local.get $5 - call $~lib/rt/pure/__release - local.get $3 + local.get $4 call $~lib/rt/pure/__release local.get $6 call $~lib/rt/pure/__release - local.get $4 + local.get $3 call $~lib/rt/pure/__release - local.get $7 + local.get $5 + call $~lib/rt/pure/__release + local.get $0 call $~lib/rt/pure/__release ) (func $~lib/map/Map#has (param $0 i32) (param $1 i32) (result i32) diff --git a/tests/compiler/std/math.optimized.wat b/tests/compiler/std/math.optimized.wat index f5b08a4674..2a0b6d8bc7 100644 --- a/tests/compiler/std/math.optimized.wat +++ b/tests/compiler/std/math.optimized.wat @@ -10664,7 +10664,7 @@ local.get $2 local.get $2 f64.mul - local.tee $1 + local.tee $4 f64.mul f64.const -2.5050760253406863e-08 local.get $2 @@ -10682,13 +10682,13 @@ f64.const 0.5 local.get $2 f64.mul - local.tee $4 + local.tee $1 f64.sub local.tee $7 f64.const 1 local.get $7 f64.sub - local.get $4 + local.get $1 f64.sub local.get $2 local.get $2 @@ -10702,8 +10702,8 @@ f64.mul f64.add f64.mul - local.get $1 - local.get $1 + local.get $4 + local.get $4 f64.mul f64.const -2.7557314351390663e-07 local.get $2