From f3125579cca998300c230232ed4ded4fe0aaa34c Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 26 Oct 2020 18:43:29 -0700 Subject: Rewrite DCE pass (#3274) The DCE pass is one of the oldest in binaryen, and had quite a lot of cruft from the changes in unreachability and other stuff in wasm and binaryen's history. This PR rewrites it from scratch, making it about 1/3 the size. I noticed this when looking for places to use code autogeneration. The old version had annoying boilerplate, while the new one avoids any need for it. There may be noticeable differences, as the old pass did more than it needed to. It overlapped with remove-unused-names for some reason I don't remember. The new pass leaves that to the other pass to do. I added another run of remove-unused-names to avoid noticeable differences in optimized builds, but you can see differences in the testcases that only run DCE by itself. (The test differences in this PR are mostly whitespace.) (The overlap is that if a block ended up not needed, that is, all branches to it were removed, the old DCE would remove the block.) This pass is about 15% faster than the old version. However, when adding another run of remove-unused-names the difference basically vanishes, so this isn't a speedup. --- test/passes/asyncify_enable-multivalue.txt | 408 +++++++++++---------- ...syncify_mod-asyncify-always-and-only-unwind.txt | 74 ++-- test/passes/asyncify_mod-asyncify-never-unwind.txt | 76 ++-- ...arg=asyncify-imports@env.import,env.import2.txt | 252 +++++++------ test/passes/dce_all-features.txt | 243 ++++++++---- test/passes/dce_all-features.wast | 57 +++ test/passes/dce_vacuum.bin.txt | 98 ----- test/passes/dce_vacuum.txt | 25 -- test/passes/dce_vacuum.wasm | Bin 260 -> 0 bytes test/passes/dce_vacuum.wast | 51 --- test/passes/dce_vacuum_remove-unused-names.bin.txt | 94 +++++ test/passes/dce_vacuum_remove-unused-names.txt | 25 ++ test/passes/dce_vacuum_remove-unused-names.wasm | Bin 0 -> 260 bytes test/passes/dce_vacuum_remove-unused-names.wast | 51 +++ test/passes/interesting-pass-mix.txt | 19 +- test/wasm2js/br.2asm.js | 22 +- test/wasm2js/br_table.2asm.js | 34 +- test/wasm2js/br_table_temp.2asm.js | 34 +- test/wasm2js/excess_fallthrough.2asm.js | 28 +- 19 files changed, 893 insertions(+), 698 deletions(-) delete mode 100644 test/passes/dce_vacuum.bin.txt delete mode 100644 test/passes/dce_vacuum.txt delete mode 100644 test/passes/dce_vacuum.wasm delete mode 100644 test/passes/dce_vacuum.wast create mode 100644 test/passes/dce_vacuum_remove-unused-names.bin.txt create mode 100644 test/passes/dce_vacuum_remove-unused-names.txt create mode 100644 test/passes/dce_vacuum_remove-unused-names.wasm create mode 100644 test/passes/dce_vacuum_remove-unused-names.wast (limited to 'test') diff --git a/test/passes/asyncify_enable-multivalue.txt b/test/passes/asyncify_enable-multivalue.txt index 08d0d7bf9..7d92b0c97 100644 --- a/test/passes/asyncify_enable-multivalue.txt +++ b/test/passes/asyncify_enable-multivalue.txt @@ -491,55 +491,57 @@ ) ) (block - (if - (if (result i32) - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (i32.const 1) - (i32.eq - (local.get $6) - (i32.const 0) - ) - ) - (block - (local.set $7 - (call $import2) - ) - (if + (block + (if + (if (result i32) (i32.eq (global.get $__asyncify_state) - (i32.const 1) + (i32.const 0) ) - (br $__asyncify_unwind + (i32.const 1) + (i32.eq + (local.get $6) (i32.const 0) ) - (local.set $1 - (local.get $7) + ) + (block + (local.set $7 + (call $import2) + ) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 1) + ) + (br $__asyncify_unwind + (i32.const 0) + ) + (local.set $1 + (local.get $7) + ) ) ) ) - ) - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (block - (local.set $temp - (local.get $1) - ) - (local.set $2 - (local.get $temp) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) ) - (return - (local.get $2) + (block + (local.set $temp + (local.get $1) + ) + (local.set $2 + (local.get $temp) + ) + (return + (local.get $2) + ) ) ) + (nop) + (nop) ) - (nop) - (nop) ) (unreachable) ) @@ -814,84 +816,86 @@ ) ) (block - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (loop $l - (local.set $3 - (local.get $y) - ) - (local.set $4 - (i32.add - (local.get $3) - (i32.const 1) - ) - ) - (local.set $x - (local.get $4) - ) - (local.set $5 - (local.get $x) - ) - (local.set $6 - (i32.div_s - (local.get $5) - (i32.const 3) - ) - ) - (local.set $y - (local.get $6) - ) - (local.set $7 - (local.get $y) - ) - (br_if $l - (local.get $7) - ) - ) - ) - (if - (if (result i32) + (block + (if (i32.eq (global.get $__asyncify_state) (i32.const 0) ) - (i32.const 1) - (i32.eq - (local.get $12) - (i32.const 0) + (loop $l + (local.set $3 + (local.get $y) + ) + (local.set $4 + (i32.add + (local.get $3) + (i32.const 1) + ) + ) + (local.set $x + (local.get $4) + ) + (local.set $5 + (local.get $x) + ) + (local.set $6 + (i32.div_s + (local.get $5) + (i32.const 3) + ) + ) + (local.set $y + (local.get $6) + ) + (local.set $7 + (local.get $y) + ) + (br_if $l + (local.get $7) + ) ) ) - (block - (call $import) - (if + (if + (if (result i32) (i32.eq (global.get $__asyncify_state) - (i32.const 1) + (i32.const 0) ) - (br $__asyncify_unwind + (i32.const 1) + (i32.eq + (local.get $12) (i32.const 0) ) ) + (block + (call $import) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 1) + ) + (br $__asyncify_unwind + (i32.const 0) + ) + ) + ) ) - ) - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (block - (local.set $8 - (local.get $y) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) ) - (return - (local.get $8) + (block + (local.set $8 + (local.get $y) + ) + (return + (local.get $8) + ) ) ) + (nop) ) - (nop) ) (unreachable) ) @@ -1291,90 +1295,92 @@ ) (block (block - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (local.set $1 - (local.get $x) - ) - ) (block (if (i32.eq (global.get $__asyncify_state) (i32.const 0) ) - (local.set $4 - (local.get $1) + (local.set $1 + (local.get $x) ) ) - (if - (i32.or - (local.get $4) - (i32.eq - (global.get $__asyncify_state) - (i32.const 2) - ) - ) + (block (if (i32.eq (global.get $__asyncify_state) (i32.const 0) ) - (return - (i32.const 1) - ) - ) - ) - (if - (i32.or - (i32.eqz - (local.get $4) - ) - (i32.eq - (global.get $__asyncify_state) - (i32.const 2) + (local.set $4 + (local.get $1) ) ) (if - (if (result i32) + (i32.or + (local.get $4) (i32.eq (global.get $__asyncify_state) - (i32.const 0) + (i32.const 2) ) - (i32.const 1) + ) + (if (i32.eq - (local.get $6) + (global.get $__asyncify_state) (i32.const 0) ) + (return + (i32.const 1) + ) ) - (block - (call $import3 + ) + (if + (i32.or + (i32.eqz + (local.get $4) + ) + (i32.eq + (global.get $__asyncify_state) (i32.const 2) ) - (if + ) + (if + (if (result i32) (i32.eq (global.get $__asyncify_state) - (i32.const 1) + (i32.const 0) ) - (br $__asyncify_unwind + (i32.const 1) + (i32.eq + (local.get $6) (i32.const 0) ) ) + (block + (call $import3 + (i32.const 2) + ) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 1) + ) + (br $__asyncify_unwind + (i32.const 0) + ) + ) + ) ) ) ) ) - ) - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (return - (i32.const 3) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) + ) + (return + (i32.const 3) + ) ) ) ) @@ -1470,90 +1476,92 @@ ) (block (block - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (local.set $1 - (local.get $x) - ) - ) (block (if (i32.eq (global.get $__asyncify_state) (i32.const 0) ) - (local.set $4 - (local.get $1) + (local.set $1 + (local.get $x) ) ) - (if - (i32.or - (local.get $4) + (block + (if (i32.eq (global.get $__asyncify_state) - (i32.const 2) + (i32.const 0) + ) + (local.set $4 + (local.get $1) ) ) (if - (if (result i32) + (i32.or + (local.get $4) (i32.eq (global.get $__asyncify_state) - (i32.const 0) - ) - (i32.const 1) - (i32.eq - (local.get $6) - (i32.const 0) + (i32.const 2) ) ) - (block - (call $import3 - (i32.const 1) - ) - (if + (if + (if (result i32) (i32.eq (global.get $__asyncify_state) - (i32.const 1) + (i32.const 0) ) - (br $__asyncify_unwind + (i32.const 1) + (i32.eq + (local.get $6) (i32.const 0) ) ) - ) - ) - ) - (if - (i32.or - (i32.eqz - (local.get $4) - ) - (i32.eq - (global.get $__asyncify_state) - (i32.const 2) + (block + (call $import3 + (i32.const 1) + ) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 1) + ) + (br $__asyncify_unwind + (i32.const 0) + ) + ) + ) ) ) (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) + (i32.or + (i32.eqz + (local.get $4) + ) + (i32.eq + (global.get $__asyncify_state) + (i32.const 2) + ) ) - (return - (i32.const 2) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) + ) + (return + (i32.const 2) + ) ) ) ) ) - ) - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (return - (i32.const 3) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) + ) + (return + (i32.const 3) + ) ) ) ) diff --git a/test/passes/asyncify_mod-asyncify-always-and-only-unwind.txt b/test/passes/asyncify_mod-asyncify-always-and-only-unwind.txt index 62c3ba33f..ecf0a41c8 100644 --- a/test/passes/asyncify_mod-asyncify-always-and-only-unwind.txt +++ b/test/passes/asyncify_mod-asyncify-always-and-only-unwind.txt @@ -152,52 +152,54 @@ ) ) (block - (if - (if (result i32) - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (i32.const 1) - (i32.eq - (local.get $6) - (i32.const 0) - ) - ) - (block - (local.set $7 - (call $import2) - ) - (if + (block + (if + (if (result i32) + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) + ) (i32.const 1) - (br $__asyncify_unwind + (i32.eq + (local.get $6) (i32.const 0) ) - (local.set $1 - (local.get $7) + ) + (block + (local.set $7 + (call $import2) + ) + (if + (i32.const 1) + (br $__asyncify_unwind + (i32.const 0) + ) + (local.set $1 + (local.get $7) + ) ) ) ) - ) - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (block - (local.set $temp - (local.get $1) - ) - (local.set $2 - (local.get $temp) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) ) - (return - (local.get $2) + (block + (local.set $temp + (local.get $1) + ) + (local.set $2 + (local.get $temp) + ) + (return + (local.get $2) + ) ) ) + (nop) + (nop) ) - (nop) - (nop) ) (unreachable) ) diff --git a/test/passes/asyncify_mod-asyncify-never-unwind.txt b/test/passes/asyncify_mod-asyncify-never-unwind.txt index eaf2794cf..2177c5220 100644 --- a/test/passes/asyncify_mod-asyncify-never-unwind.txt +++ b/test/passes/asyncify_mod-asyncify-never-unwind.txt @@ -164,52 +164,54 @@ ) ) (block - (if - (if (result i32) - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (i32.const 1) - (i32.eq - (local.get $6) - (i32.const 0) - ) - ) - (block - (local.set $7 - (call $import2) - ) - (if - (i32.const 0) - (br $__asyncify_unwind + (block + (if + (if (result i32) + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) + ) + (i32.const 1) + (i32.eq + (local.get $6) (i32.const 0) ) - (local.set $1 - (local.get $7) + ) + (block + (local.set $7 + (call $import2) + ) + (if + (i32.const 0) + (br $__asyncify_unwind + (i32.const 0) + ) + (local.set $1 + (local.get $7) + ) ) ) ) - ) - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (block - (local.set $temp - (local.get $1) - ) - (local.set $2 - (local.get $temp) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) ) - (return - (local.get $2) + (block + (local.set $temp + (local.get $1) + ) + (local.set $2 + (local.get $temp) + ) + (return + (local.get $2) + ) ) ) + (nop) + (nop) ) - (nop) - (nop) ) (unreachable) ) diff --git a/test/passes/asyncify_pass-arg=asyncify-imports@env.import,env.import2.txt b/test/passes/asyncify_pass-arg=asyncify-imports@env.import,env.import2.txt index defef94ed..2a8c0061a 100644 --- a/test/passes/asyncify_pass-arg=asyncify-imports@env.import,env.import2.txt +++ b/test/passes/asyncify_pass-arg=asyncify-imports@env.import,env.import2.txt @@ -488,55 +488,57 @@ ) ) (block - (if - (if (result i32) - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (i32.const 1) - (i32.eq - (local.get $6) - (i32.const 0) - ) - ) - (block - (local.set $7 - (call $import2) - ) - (if + (block + (if + (if (result i32) (i32.eq (global.get $__asyncify_state) - (i32.const 1) + (i32.const 0) ) - (br $__asyncify_unwind + (i32.const 1) + (i32.eq + (local.get $6) (i32.const 0) ) - (local.set $1 - (local.get $7) + ) + (block + (local.set $7 + (call $import2) + ) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 1) + ) + (br $__asyncify_unwind + (i32.const 0) + ) + (local.set $1 + (local.get $7) + ) ) ) ) - ) - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (block - (local.set $temp - (local.get $1) - ) - (local.set $2 - (local.get $temp) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) ) - (return - (local.get $2) + (block + (local.set $temp + (local.get $1) + ) + (local.set $2 + (local.get $temp) + ) + (return + (local.get $2) + ) ) ) + (nop) + (nop) ) - (nop) - (nop) ) (unreachable) ) @@ -810,84 +812,86 @@ ) ) (block - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (loop $l - (local.set $2 - (local.get $y) - ) - (local.set $3 - (i32.add - (local.get $2) - (i32.const 1) - ) - ) - (local.set $x - (local.get $3) - ) - (local.set $4 - (local.get $x) - ) - (local.set $5 - (i32.div_s - (local.get $4) - (i32.const 3) - ) - ) - (local.set $y - (local.get $5) - ) - (local.set $6 - (local.get $y) - ) - (br_if $l - (local.get $6) - ) - ) - ) - (if - (if (result i32) + (block + (if (i32.eq (global.get $__asyncify_state) (i32.const 0) ) - (i32.const 1) - (i32.eq - (local.get $11) - (i32.const 0) + (loop $l + (local.set $2 + (local.get $y) + ) + (local.set $3 + (i32.add + (local.get $2) + (i32.const 1) + ) + ) + (local.set $x + (local.get $3) + ) + (local.set $4 + (local.get $x) + ) + (local.set $5 + (i32.div_s + (local.get $4) + (i32.const 3) + ) + ) + (local.set $y + (local.get $5) + ) + (local.set $6 + (local.get $y) + ) + (br_if $l + (local.get $6) + ) ) ) - (block - (call $import) - (if + (if + (if (result i32) (i32.eq (global.get $__asyncify_state) - (i32.const 1) + (i32.const 0) ) - (br $__asyncify_unwind + (i32.const 1) + (i32.eq + (local.get $11) (i32.const 0) ) ) + (block + (call $import) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 1) + ) + (br $__asyncify_unwind + (i32.const 0) + ) + ) + ) ) - ) - (if - (i32.eq - (global.get $__asyncify_state) - (i32.const 0) - ) - (block - (local.set $7 - (local.get $y) + (if + (i32.eq + (global.get $__asyncify_state) + (i32.const 0) ) - (return - (local.get $7) + (block + (local.set $7 + (local.get $y) + ) + (return + (local.get $7) + ) ) ) + (nop) ) - (nop) ) (unreachable) ) @@ -1062,21 +1066,23 @@ (local $2 i32) (local $3 i32) (block - (local.set $1 - (local.get $x) - ) - (if - (local.get $1) - (return - (i32.const 1) + (block + (local.set $1 + (local.get $x) ) - (call $import3 - (i32.const 2) + (if + (local.get $1) + (return + (i32.const 1) + ) + (call $import3 + (i32.const 2) + ) ) ) - ) - (return - (i32.const 3) + (return + (i32.const 3) + ) ) ) (func $calls-import2-if-else-oneside2 (param $x i32) (result i32) @@ -1084,21 +1090,23 @@ (local $2 i32) (local $3 i32) (block - (local.set $1 - (local.get $x) - ) - (if - (local.get $1) - (call $import3 - (i32.const 1) + (block + (local.set $1 + (local.get $x) ) - (return - (i32.const 2) + (if + (local.get $1) + (call $import3 + (i32.const 1) + ) + (return + (i32.const 2) + ) ) ) - ) - (return - (i32.const 3) + (return + (i32.const 3) + ) ) ) (func $calls-loop (param $x i32) diff --git a/test/passes/dce_all-features.txt b/test/passes/dce_all-features.txt index 4b76b2f12..4088d5a34 100644 --- a/test/passes/dce_all-features.txt +++ b/test/passes/dce_all-features.txt @@ -24,7 +24,9 @@ ) (if (i32.const 0) - (return) + (block $out3 + (return) + ) ) (block $out4 (br_table $out4 $out4 $out4 $out4 @@ -41,10 +43,12 @@ ) (if (i32.const 0) - (if - (i32.const 0) - (unreachable) - (unreachable) + (block $block4 + (if + (i32.const 0) + (unreachable) + (unreachable) + ) ) ) (if @@ -69,13 +73,15 @@ ) (if (i32.const 0) - (block $out18 - (block $in19 - (br_if $in19 - (i32.const 1) + (block $block11 + (block $out18 + (block $in19 + (br_if $in19 + (i32.const 1) + ) ) + (unreachable) ) - (unreachable) ) ) (block $out20 @@ -96,13 +102,15 @@ ) (if (i32.const 0) - (block $out25 - (block $in26 - (br_table $in26 $in26 - (i32.const 1) + (block $block13 + (block $out25 + (block $in26 + (br_table $in26 $in26 + (i32.const 1) + ) ) + (unreachable) ) - (unreachable) ) ) (if @@ -122,7 +130,7 @@ (unreachable) ) (block $out29 - (block + (loop $in30 (br_if $out29 (i32.const 1) ) @@ -131,11 +139,13 @@ ) (if (i32.const 0) - (loop $in32 - (br_if $in32 - (i32.const 1) + (block $block20 + (loop $in32 + (br_if $in32 + (i32.const 1) + ) + (unreachable) ) - (unreachable) ) ) (if @@ -273,8 +283,12 @@ ) ) (func $typed-block-none-then-unreachable (result i32) - (return - (i32.const 0) + (block $top-typed + (block $switch$0 + (return + (i32.const 0) + ) + ) ) ) (func $typed-block-remove-br-changes-type (param $$$0 i32) (result i32) @@ -317,15 +331,19 @@ ) ) (func $unreachable-block-ends-switch (result i32) - (block $label$3 - (nop) - (unreachable) + (block $label$0 + (block $label$3 + (nop) + (unreachable) + ) ) ) (func $unreachable-block-ends-br_if (result i32) - (block $label$2 - (nop) - (unreachable) + (block $label$0 + (block $label$2 + (nop) + (unreachable) + ) ) ) (func $unreachable-brs-3 (result i32) @@ -339,11 +357,15 @@ (drop (i32.const 1) ) - (block - (drop - (i32.const 4104) + (block $label$0 + (block $label$1 + (block + (drop + (i32.const 4104) + ) + (unreachable) + ) ) - (unreachable) ) ) (func $call-unreach (param $var$0 i64) (param $var$1 i64) (result i64) @@ -355,32 +377,36 @@ (block $label$0 (result i64) (local.get $var$1) ) - (block - (drop - (i64.sub - (local.get $var$0) - (i64.const 1) - ) - ) + (block $label$1 (block (drop - (block $block (result i64) - (local.set $2 - (local.get $var$0) + (i64.sub + (local.get $var$0) + (i64.const 1) + ) + ) + (block + (drop + (block $block (result i64) + (local.set $2 + (local.get $var$0) + ) + (nop) + (local.get $2) ) - (nop) - (local.get $2) ) + (unreachable) ) - (unreachable) ) ) ) ) (func $br-gone-means-block-type-changes-then-refinalize-at-end-is-too-late (param $var$0 i32) (result i32) - (block $block - (nop) - (unreachable) + (block $label$0 + (block $block + (nop) + (unreachable) + ) ) ) (func $br-with-unreachable-value-should-not-give-a-block-a-value (param $var$0 i32) (result i32) @@ -397,25 +423,31 @@ ) ) (func $replace-br-value-of-i32-with-unreachable (result i32) - (block $label$1 - (nop) - (unreachable) + (block $label$0 + (block $label$1 + (nop) + (unreachable) + ) ) ) (func $shorten-block-requires-sync-refinalize (param $var$0 i32) (param $var$1 i32) (unreachable) ) (func $block-with-type-but-is-unreachable (param $var$0 i32) (result i32) - (block $block - (nop) - (unreachable) + (block $label$0 + (block $block + (nop) + (unreachable) + ) ) ) (func $if-with-type-but-is-unreachable (param $var$0 i32) (result i32) - (if - (local.get $var$0) - (unreachable) - (unreachable) + (block $label$0 + (if + (local.get $var$0) + (unreachable) + (unreachable) + ) ) ) (func $unreachable-loop @@ -423,8 +455,10 @@ ) (func $br-block-from-unary (result i32) (block $label$6 (result i32) - (br $label$6 - (i32.const 8) + (block $label$7 + (br $label$6 + (i32.const 8) + ) ) ) ) @@ -444,19 +478,23 @@ ) ) (func $replace-with-unreachable-affects-parent (param $var$0 f32) (param $var$1 i64) - (drop - (i64.const 0) - ) - (if - (block $block (result i32) - (call $replace-with-unreachable-affects-parent - (f32.const 1) - (i64.const -15917430362925035) + (block $top + (block + (drop + (i64.const 0) + ) + (if + (block $block (result i32) + (call $replace-with-unreachable-affects-parent + (f32.const 1) + (i64.const -15917430362925035) + ) + (i32.const 1) + ) + (unreachable) + (unreachable) ) - (i32.const 1) ) - (unreachable) - (unreachable) ) ) (func $replace-block-changes-later-when-if-goes @@ -503,6 +541,7 @@ ) (module (type $none_=>_none (func)) + (type $none_=>_i32 (func (result i32))) (event $e (attr 0) (param)) (func $foo (nop) @@ -542,12 +581,68 @@ ) ) (func $throw - (throw $e + (block $label$0 + (block $label$1 + (throw $e + ) + ) ) ) (func $rethrow - (rethrow - (ref.null exn) + (block $label$0 + (block $label$1 + (rethrow + (ref.null exn) + ) + ) + ) + ) + (func $unnecessary-concrete-block (result i32) + (block $foo + (nop) + (unreachable) + ) + ) + (func $necessary-concrete-block (result i32) + (block $foo (result i32) + (br $foo + (i32.const 1) + ) + ) + ) + (func $unnecessary-concrete-if (result i32) + (if + (i32.const 0) + (return + (i32.const 1) + ) + (unreachable) + ) + ) + (func $unnecessary-concrete-try (result i32) + (try + (do + (unreachable) + ) + (catch + (unreachable) + ) + ) + ) + (func $note-loss-of-if-children + (block $label$1 + (block $label$2 + (nop) + (unreachable) + ) + ) + ) + (func $note-loss-of-non-control-flow-children + (block $out + (block $block + (nop) + (unreachable) + ) ) ) ) diff --git a/test/passes/dce_all-features.wast b/test/passes/dce_all-features.wast index bf0c188d0..9ebcccb60 100644 --- a/test/passes/dce_all-features.wast +++ b/test/passes/dce_all-features.wast @@ -810,4 +810,61 @@ ) ) ) + + (func $unnecessary-concrete-block (result i32) + (block $foo (result i32) ;; unnecessary type + (nop) + (unreachable) + ) + ) + (func $necessary-concrete-block (result i32) + (block $foo (result i32) + (br $foo (i32.const 1)) + (unreachable) + ) + ) + (func $unnecessary-concrete-if (result i32) + (if (result i32) ;; unnecessary type + (i32.const 0) + (return (i32.const 1)) + (unreachable) + ) + ) + (func $unnecessary-concrete-try (result i32) + (try (result i32) + (do + (unreachable) + ) + (catch + (unreachable) + ) + ) + ) + (func $note-loss-of-if-children + (block $label$1 + (if ;; begins unreachable - type never changes - but after the condition + ;; becomes unreachable, it will lose the children, which means no more + ;; br to the outer block, changing that type. + (block $label$2 (result i32) + (nop) + (unreachable) + ) + (unreachable) + (br $label$1) + ) + ) + ) + (func $note-loss-of-non-control-flow-children + (block $out + (drop + (i32.add + (block (result i32) + (nop) + (unreachable) + ) + (br $out) ;; when this is removed as dead, the block becomes unreachable + ) + ) + ) + ) ) diff --git a/test/passes/dce_vacuum.bin.txt b/test/passes/dce_vacuum.bin.txt deleted file mode 100644 index cee1143d3..000000000 --- a/test/passes/dce_vacuum.bin.txt +++ /dev/null @@ -1,98 +0,0 @@ -(module - (type $f32_f32_=>_f32 (func (param f32 f32) (result f32))) - (type $f64_f64_=>_f64 (func (param f64 f64) (result f64))) - (export "f32.compute_radix" (func $0)) - (export "f64.compute_radix" (func $1)) - (func $0 (param $0 f32) (param $1 f32) (result f32) - (block $label$1 - (loop $label$2 - (br_if $label$2 - (f32.eq - (f32.add - (f32.sub - (f32.add - (local.tee $0 - (f32.add - (local.get $0) - (local.get $0) - ) - ) - (f32.const 1) - ) - (local.get $0) - ) - (f32.const -1) - ) - (f32.const 0) - ) - ) - ) - (block - (drop - (call $0 - (f32.add - (local.get $0) - (local.tee $1 - (f32.add - (local.get $1) - (f32.const 1) - ) - ) - ) - (local.get $0) - ) - ) - (unreachable) - ) - ) - ) - (func $1 (param $0 f64) (param $1 f64) (result f64) - (block $label$1 (result f64) - (loop $label$2 - (br_if $label$2 - (f64.eq - (f64.add - (f64.sub - (f64.add - (local.tee $0 - (f64.add - (local.get $0) - (local.get $0) - ) - ) - (f64.const 1) - ) - (local.get $0) - ) - (f64.const -1) - ) - (f64.const 0) - ) - ) - ) - (loop $label$3 - (br_if $label$3 - (f64.ne - (f64.sub - (f64.sub - (f64.add - (local.get $0) - (local.tee $1 - (f64.add - (local.get $1) - (f64.const 1) - ) - ) - ) - (local.get $0) - ) - (local.get $1) - ) - (f64.const 0) - ) - ) - ) - (local.get $1) - ) - ) -) diff --git a/test/passes/dce_vacuum.txt b/test/passes/dce_vacuum.txt deleted file mode 100644 index 2c4466058..000000000 --- a/test/passes/dce_vacuum.txt +++ /dev/null @@ -1,25 +0,0 @@ -(module - (type $none_=>_i32 (func (result i32))) - (type $i64_=>_i64 (func (param i64) (result i64))) - (type $f32_f32_=>_f32 (func (param f32 f32) (result f32))) - (func $__Z12serveroptionPc (result i32) - (return - (i32.const 0) - ) - ) - (func $drop-unreachable (param $var$0 f32) (param $var$1 f32) (result f32) - (unreachable) - ) - (func $set-unreachable (param $var$0 i64) (result i64) - (local $var$1 i64) - (local $var$2 i64) - (if - (i64.eq - (local.get $var$1) - (i64.const 0) - ) - (unreachable) - (unreachable) - ) - ) -) diff --git a/test/passes/dce_vacuum.wasm b/test/passes/dce_vacuum.wasm deleted file mode 100644 index 5fa1892bd..000000000 Binary files a/test/passes/dce_vacuum.wasm and /dev/null differ diff --git a/test/passes/dce_vacuum.wast b/test/passes/dce_vacuum.wast deleted file mode 100644 index 47f4affbd..000000000 --- a/test/passes/dce_vacuum.wast +++ /dev/null @@ -1,51 +0,0 @@ -(module - (func $__Z12serveroptionPc (result i32) - (block $switch$0 - (return - (i32.const 0) - ) - (br $switch$0) - ) - (return - (i32.const 0) - ) - ) - (func $drop-unreachable (param $var$0 f32) (param $var$1 f32) (result f32) - (block $label$0 (result f32) - (loop $label$2 - (drop - (unreachable) - ) - (unreachable) - ) - (local.get $var$1) - ) - ) - - (func $set-unreachable (param $var$0 i64) (result i64) - (local $var$1 i64) - (local $var$2 i64) - (block $label$0 (result i64) - (block $label$1 - (loop $label$2 - (if - (i64.eq - (local.get $var$1) - (i64.const 0) - ) - (unreachable) - (local.set $var$2 - (i64.mul - (unreachable) - (local.get $var$2) - ) - ) - ) - (br $label$2) - ) - ) - (local.get $var$2) - ) - ) -) - diff --git a/test/passes/dce_vacuum_remove-unused-names.bin.txt b/test/passes/dce_vacuum_remove-unused-names.bin.txt new file mode 100644 index 000000000..26e2c565a --- /dev/null +++ b/test/passes/dce_vacuum_remove-unused-names.bin.txt @@ -0,0 +1,94 @@ +(module + (type $f32_f32_=>_f32 (func (param f32 f32) (result f32))) + (type $f64_f64_=>_f64 (func (param f64 f64) (result f64))) + (export "f32.compute_radix" (func $0)) + (export "f64.compute_radix" (func $1)) + (func $0 (param $0 f32) (param $1 f32) (result f32) + (loop $label$2 + (br_if $label$2 + (f32.eq + (f32.add + (f32.sub + (f32.add + (local.tee $0 + (f32.add + (local.get $0) + (local.get $0) + ) + ) + (f32.const 1) + ) + (local.get $0) + ) + (f32.const -1) + ) + (f32.const 0) + ) + ) + ) + (block + (drop + (call $0 + (f32.add + (local.get $0) + (local.tee $1 + (f32.add + (local.get $1) + (f32.const 1) + ) + ) + ) + (local.get $0) + ) + ) + (unreachable) + ) + ) + (func $1 (param $0 f64) (param $1 f64) (result f64) + (loop $label$2 + (br_if $label$2 + (f64.eq + (f64.add + (f64.sub + (f64.add + (local.tee $0 + (f64.add + (local.get $0) + (local.get $0) + ) + ) + (f64.const 1) + ) + (local.get $0) + ) + (f64.const -1) + ) + (f64.const 0) + ) + ) + ) + (loop $label$3 + (br_if $label$3 + (f64.ne + (f64.sub + (f64.sub + (f64.add + (local.get $0) + (local.tee $1 + (f64.add + (local.get $1) + (f64.const 1) + ) + ) + ) + (local.get $0) + ) + (local.get $1) + ) + (f64.const 0) + ) + ) + ) + (local.get $1) + ) +) diff --git a/test/passes/dce_vacuum_remove-unused-names.txt b/test/passes/dce_vacuum_remove-unused-names.txt new file mode 100644 index 000000000..2c4466058 --- /dev/null +++ b/test/passes/dce_vacuum_remove-unused-names.txt @@ -0,0 +1,25 @@ +(module + (type $none_=>_i32 (func (result i32))) + (type $i64_=>_i64 (func (param i64) (result i64))) + (type $f32_f32_=>_f32 (func (param f32 f32) (result f32))) + (func $__Z12serveroptionPc (result i32) + (return + (i32.const 0) + ) + ) + (func $drop-unreachable (param $var$0 f32) (param $var$1 f32) (result f32) + (unreachable) + ) + (func $set-unreachable (param $var$0 i64) (result i64) + (local $var$1 i64) + (local $var$2 i64) + (if + (i64.eq + (local.get $var$1) + (i64.const 0) + ) + (unreachable) + (unreachable) + ) + ) +) diff --git a/test/passes/dce_vacuum_remove-unused-names.wasm b/test/passes/dce_vacuum_remove-unused-names.wasm new file mode 100644 index 000000000..5fa1892bd Binary files /dev/null and b/test/passes/dce_vacuum_remove-unused-names.wasm differ diff --git a/test/passes/dce_vacuum_remove-unused-names.wast b/test/passes/dce_vacuum_remove-unused-names.wast new file mode 100644 index 000000000..47f4affbd --- /dev/null +++ b/test/passes/dce_vacuum_remove-unused-names.wast @@ -0,0 +1,51 @@ +(module + (func $__Z12serveroptionPc (result i32) + (block $switch$0 + (return + (i32.const 0) + ) + (br $switch$0) + ) + (return + (i32.const 0) + ) + ) + (func $drop-unreachable (param $var$0 f32) (param $var$1 f32) (result f32) + (block $label$0 (result f32) + (loop $label$2 + (drop + (unreachable) + ) + (unreachable) + ) + (local.get $var$1) + ) + ) + + (func $set-unreachable (param $var$0 i64) (result i64) + (local $var$1 i64) + (local $var$2 i64) + (block $label$0 (result i64) + (block $label$1 + (loop $label$2 + (if + (i64.eq + (local.get $var$1) + (i64.const 0) + ) + (unreachable) + (local.set $var$2 + (i64.mul + (unreachable) + (local.get $var$2) + ) + ) + ) + (br $label$2) + ) + ) + (local.get $var$2) + ) + ) +) + diff --git a/test/passes/interesting-pass-mix.txt b/test/passes/interesting-pass-mix.txt index 0fbbc809c..c37414599 100644 --- a/test/passes/interesting-pass-mix.txt +++ b/test/passes/interesting-pass-mix.txt @@ -41,14 +41,16 @@ ) (loop $shape$4$continue (call $trivial) - (br_if $shape$4$continue + (if (local.get $0) + (br $shape$4$continue) ) ) (loop $shape$6$continue (call $trivial) - (br_if $shape$6$continue + (if (local.get $0) + (br $shape$6$continue) ) ) ) @@ -119,8 +121,9 @@ (call $before-and-after (i32.const 9) ) - (br_if $shape$4$continue + (if (local.get $0) + (br $shape$4$continue) ) ) (call $before-and-after @@ -187,9 +190,12 @@ ) (block $block$7$break (block $switch$3$default - (br_table $block$7$break $block$7$break $block$7$break $switch$3$default - (local.get $0) + (block $switch$3$case$7 + (br_table $switch$3$case$7 $switch$3$case$7 $switch$3$case$7 $switch$3$default + (local.get $0) + ) ) + (br $block$7$break) ) (call $switch (i32.const 2) @@ -212,8 +218,9 @@ (call $if-br-wat (i32.const 1) ) - (br_if $block$2$break + (if (local.get $0) + (br $block$2$break) ) ) (call $if-br-wat diff --git a/test/wasm2js/br.2asm.js b/test/wasm2js/br.2asm.js index 73484e457..c60de9fc2 100644 --- a/test/wasm2js/br.2asm.js +++ b/test/wasm2js/br.2asm.js @@ -106,8 +106,10 @@ function asmFunc(global, env) { function $13() { var $0 = 0, $1_1 = 0, $3_1 = 0; block : { - $0 = 3; - break block; + loop_in : while (1) { + $0 = 3; + break block; + }; } return $0 | 0; } @@ -115,9 +117,11 @@ function asmFunc(global, env) { function $14() { var $0 = 0, $1_1 = 0, $3_1 = 0; block : { - dummy(); - $0 = 4; - break block; + loop_in : while (1) { + dummy(); + $0 = 4; + break block; + }; } return $0 | 0; } @@ -125,9 +129,11 @@ function asmFunc(global, env) { function $15() { var $0 = 0; block : { - dummy(); - $0 = 5; - break block; + loop_in : while (1) { + dummy(); + $0 = 5; + break block; + }; } return $0 | 0; } diff --git a/test/wasm2js/br_table.2asm.js b/test/wasm2js/br_table.2asm.js index 4171374b9..5e1c2c05c 100644 --- a/test/wasm2js/br_table.2asm.js +++ b/test/wasm2js/br_table.2asm.js @@ -12560,10 +12560,12 @@ function asmFunc(global, env) { function $20() { var $1_1 = 0, $2_1 = 0, $4_1 = 0; fake_return_waka123 : { - $1_1 = 3; - switch (0 | 0) { - default: - break fake_return_waka123; + loop_in : while (1) { + $1_1 = 3; + switch (0 | 0) { + default: + break fake_return_waka123; + }; }; } return $1_1 | 0; @@ -12572,11 +12574,13 @@ function asmFunc(global, env) { function $21() { var $1_1 = 0, $2_1 = 0, $4_1 = 0; fake_return_waka123 : { - dummy(); - $1_1 = 4; - switch (-1 | 0) { - default: - break fake_return_waka123; + loop_in : while (1) { + dummy(); + $1_1 = 4; + switch (-1 | 0) { + default: + break fake_return_waka123; + }; }; } return $1_1 | 0; @@ -12585,11 +12589,13 @@ function asmFunc(global, env) { function $22() { var $1_1 = 0; fake_return_waka123 : { - dummy(); - $1_1 = 5; - switch (1 | 0) { - default: - break fake_return_waka123; + loop_in : while (1) { + dummy(); + $1_1 = 5; + switch (1 | 0) { + default: + break fake_return_waka123; + }; }; } return $1_1 | 0; diff --git a/test/wasm2js/br_table_temp.2asm.js b/test/wasm2js/br_table_temp.2asm.js index 3a56dda00..99d7ceeef 100644 --- a/test/wasm2js/br_table_temp.2asm.js +++ b/test/wasm2js/br_table_temp.2asm.js @@ -12556,10 +12556,12 @@ function asmFunc(global, env) { function $20() { var $1_1 = 0, $2_1 = 0, $4_1 = 0; fake_return_waka123 : { - $1_1 = 3; - switch (0 | 0) { - default: - break fake_return_waka123; + loop_in : while (1) { + $1_1 = 3; + switch (0 | 0) { + default: + break fake_return_waka123; + }; }; } return $1_1 | 0; @@ -12568,11 +12570,13 @@ function asmFunc(global, env) { function $21() { var $1_1 = 0, $2_1 = 0, $4_1 = 0; fake_return_waka123 : { - dummy(); - $1_1 = 4; - switch (-1 | 0) { - default: - break fake_return_waka123; + loop_in : while (1) { + dummy(); + $1_1 = 4; + switch (-1 | 0) { + default: + break fake_return_waka123; + }; }; } return $1_1 | 0; @@ -12581,11 +12585,13 @@ function asmFunc(global, env) { function $22() { var $1_1 = 0; fake_return_waka123 : { - dummy(); - $1_1 = 5; - switch (1 | 0) { - default: - break fake_return_waka123; + loop_in : while (1) { + dummy(); + $1_1 = 5; + switch (1 | 0) { + default: + break fake_return_waka123; + }; }; } return $1_1 | 0; diff --git a/test/wasm2js/excess_fallthrough.2asm.js b/test/wasm2js/excess_fallthrough.2asm.js index d1290aa50..3c8d09bf4 100644 --- a/test/wasm2js/excess_fallthrough.2asm.js +++ b/test/wasm2js/excess_fallthrough.2asm.js @@ -18,20 +18,22 @@ function asmFunc(global, env) { function foo($0) { $0 = $0 | 0; - label$5 : { - bar(); - block : { - switch (123 | 0) { - case 0: - bar(); - break; - default: - break label$5; - }; + label$4 : while (1) { + label$5 : { + bar(); + block : { + switch (123 | 0) { + case 0: + bar(); + break; + default: + break label$5; + }; + } + return; } - return; - } - abort(); + abort(); + }; } return { -- cgit v1.2.3