diff options
-rw-r--r-- | src/passes/Precompute.cpp | 18 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.txt | 40 | ||||
-rw-r--r-- | test/example/c-api-kitchen-sink.txt.txt | 20 | ||||
-rw-r--r-- | test/passes/precompute.txt | 33 | ||||
-rw-r--r-- | test/passes/precompute.wast | 12 | ||||
m--------- | test/spec | 0 | ||||
-rw-r--r-- | test/unit.asm.js | 4 | ||||
-rw-r--r-- | test/unit.fromasm | 12 | ||||
-rw-r--r-- | test/unit.fromasm.imprecise | 12 | ||||
-rw-r--r-- | test/unit.fromasm.imprecise.no-opts | 4 | ||||
-rw-r--r-- | test/unit.fromasm.no-opts | 4 |
11 files changed, 51 insertions, 108 deletions
diff --git a/src/passes/Precompute.cpp b/src/passes/Precompute.cpp index a341ba938..008058d58 100644 --- a/src/passes/Precompute.cpp +++ b/src/passes/Precompute.cpp @@ -15,23 +15,14 @@ */ // -// Removes dead, i.e. unreachable, code. -// -// We keep a record of when control flow is reachable. When it isn't, we -// kill (turn into unreachable). We then fold away entire unreachable -// expressions. -// -// When dead code causes an operation to not happen, like a store, a call -// or an add, we replace with a block with a list of what does happen. -// That isn't necessarily smaller, but blocks are friendlier to other -// optimizations: blocks can be merged and eliminated, and they clearly -// have no side effects. +// Computes code at compile time where possible. // #include <wasm.h> #include <pass.h> #include <wasm-builder.h> #include <wasm-interpreter.h> +#include <ast_utils.h> namespace wasm { @@ -90,7 +81,7 @@ struct Precompute : public WalkerPass<PostWalker<Precompute, UnifiedExpressionVi Pass* create() override { return new Precompute; } void visitExpression(Expression* curr) { - if (curr->is<Const>()) return; + if (curr->is<Const>() || curr->is<Nop>()) return; // try to evaluate this into a const Flow flow; try { @@ -99,8 +90,11 @@ struct Precompute : public WalkerPass<PostWalker<Precompute, UnifiedExpressionVi return; } if (flow.breaking()) return; // TODO: can create a break as a replacement in some cases (not NONSTANDALONE) + // this was precomputed if (isConcreteWasmType(flow.value.type)) { replaceCurrent(Builder(*getModule()).makeConst(flow.value)); + } else { + ExpressionManipulator::nop(curr); } } }; diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt index b7b4821ac..8cc6a0a1c 100644 --- a/test/example/c-api-kitchen-sink.txt +++ b/test/example/c-api-kitchen-sink.txt @@ -1128,9 +1128,6 @@ optimized: (call $check (i32.const 1) ) - (br_if $block$7$break - (i32.const 0) - ) (call $check (i32.const 2) ) @@ -1162,23 +1159,6 @@ optimized: (i32.const 0) ) (block $switch$1$leave - (block $switch$1$default - (block $switch$1$case$3 - (block $switch$1$case$2 - (br_table $switch$1$default $switch$1$default $switch$1$case$2 $switch$1$default $switch$1$case$3 $switch$1$case$2 $switch$1$default - (i32.const -99) - ) - ) - (call $check - (i32.const 1) - ) - (br $switch$1$leave) - ) - (call $check - (i32.const 2) - ) - (br $switch$1$leave) - ) (call $check (i32.const 3) ) @@ -3187,9 +3167,6 @@ optimized: (call $check (i32.const 1) ) - (br_if $block$7$break - (i32.const 0) - ) (call $check (i32.const 2) ) @@ -3221,23 +3198,6 @@ optimized: (i32.const 0) ) (block $switch$1$leave - (block $switch$1$default - (block $switch$1$case$3 - (block $switch$1$case$2 - (br_table $switch$1$default $switch$1$default $switch$1$case$2 $switch$1$default $switch$1$case$3 $switch$1$case$2 $switch$1$default - (i32.const -99) - ) - ) - (call $check - (i32.const 1) - ) - (br $switch$1$leave) - ) - (call $check - (i32.const 2) - ) - (br $switch$1$leave) - ) (call $check (i32.const 3) ) diff --git a/test/example/c-api-kitchen-sink.txt.txt b/test/example/c-api-kitchen-sink.txt.txt index a6f6e0853..1ce2c903a 100644 --- a/test/example/c-api-kitchen-sink.txt.txt +++ b/test/example/c-api-kitchen-sink.txt.txt @@ -1121,9 +1121,6 @@ (call $check (i32.const 1) ) - (br_if $block$7$break - (i32.const 0) - ) (call $check (i32.const 2) ) @@ -1155,23 +1152,6 @@ (i32.const 0) ) (block $switch$1$leave - (block $switch$1$default - (block $switch$1$case$3 - (block $switch$1$case$2 - (br_table $switch$1$default $switch$1$default $switch$1$case$2 $switch$1$default $switch$1$case$3 $switch$1$case$2 $switch$1$default - (i32.const -99) - ) - ) - (call $check - (i32.const 1) - ) - (br $switch$1$leave) - ) - (call $check - (i32.const 2) - ) - (br $switch$1$leave) - ) (call $check (i32.const 3) ) diff --git a/test/passes/precompute.txt b/test/passes/precompute.txt index 9825594b6..9c3640757 100644 --- a/test/passes/precompute.txt +++ b/test/passes/precompute.txt @@ -2,26 +2,35 @@ (memory 0) (type $0 (func (param i32))) (func $x (type $0) (param $x i32) - (drop - (i32.const 3) - ) + (nop) (drop (i32.add (i32.const 1) (get_local $x) ) ) - (drop - (i32.const 6) - ) - (drop - (i32.const -1) - ) - (drop - (i32.const 3) - ) + (nop) + (nop) + (nop) (loop $in (br $in) ) + (nop) + (if + (i32.const 2) + (call $x + (i32.const 3) + ) + ) + (nop) + (block $c + (nop) + (call $x + (i32.const 4) + ) + (br_if $c + (i32.const 1) + ) + ) ) ) diff --git a/test/passes/precompute.wast b/test/passes/precompute.wast index 808485a34..ab7f0e3ad 100644 --- a/test/passes/precompute.wast +++ b/test/passes/precompute.wast @@ -41,5 +41,17 @@ (loop $in (br $in) ) + (if (i32.const 0) (call $x (i32.const 1))) + (if (i32.const 2) (call $x (i32.const 3))) + (block $b + (br_if $b (i32.const 0)) + (br_if $b (i32.const 1)) + (call $x (i32.const 4)) + ) + (block $c + (br_if $c (i32.const 0)) + (call $x (i32.const 4)) + (br_if $c (i32.const 1)) + ) ) ) diff --git a/test/spec b/test/spec -Subproject 237f9ee630e951149e73d32633aa78616559519 +Subproject ef185d9cc6466f6d60c0117218f6d2144389676 diff --git a/test/unit.asm.js b/test/unit.asm.js index 7b4b2a96f..86be4cf2f 100644 --- a/test/unit.asm.js +++ b/test/unit.asm.js @@ -205,7 +205,7 @@ function asm(global, env, buffer) { print(1); do { print(5); - if (0) continue; + if (return_int()) continue; } while (0); print(2); } @@ -275,7 +275,7 @@ function asm(global, env, buffer) { } function dropCall() { - if (0) { + if (return_int() | 0) { phi(); // drop this setTempRet0(10); // this too zeroInit(setTempRet0(10) | 0); diff --git a/test/unit.fromasm b/test/unit.fromasm index 123e087e6..6d0952bd4 100644 --- a/test/unit.fromasm +++ b/test/unit.fromasm @@ -358,7 +358,7 @@ (i32.const 5) ) (br_if $unlikely-continue$3 - (i32.const 0) + (call $return_int) ) ) (call $print @@ -519,7 +519,7 @@ ) (func $dropCall (result i32) (if - (i32.const 0) + (call $return_int) (block (drop (call $phi) @@ -1013,13 +1013,7 @@ ) ) (func $jumpThreadDrop (result i32) - (local $0 i32) - (set_local $0 - (call $return_int) - ) - (block $jumpthreading$outer$2 - ) - (get_local $0) + (call $return_int) ) (func $dropIgnoredImportInIf (param $0 i32) (param $1 i32) (param $2 i32) (if diff --git a/test/unit.fromasm.imprecise b/test/unit.fromasm.imprecise index b170c8b82..1e50ad731 100644 --- a/test/unit.fromasm.imprecise +++ b/test/unit.fromasm.imprecise @@ -339,7 +339,7 @@ (i32.const 5) ) (br_if $unlikely-continue$3 - (i32.const 0) + (call $return_int) ) ) (call $print @@ -500,7 +500,7 @@ ) (func $dropCall (result i32) (if - (i32.const 0) + (call $return_int) (block (drop (call $phi) @@ -994,13 +994,7 @@ ) ) (func $jumpThreadDrop (result i32) - (local $0 i32) - (set_local $0 - (call $return_int) - ) - (block $jumpthreading$outer$2 - ) - (get_local $0) + (call $return_int) ) (func $dropIgnoredImportInIf (param $0 i32) (param $1 i32) (param $2 i32) (if diff --git a/test/unit.fromasm.imprecise.no-opts b/test/unit.fromasm.imprecise.no-opts index 913920f8b..f851b86c9 100644 --- a/test/unit.fromasm.imprecise.no-opts +++ b/test/unit.fromasm.imprecise.no-opts @@ -591,7 +591,7 @@ (i32.const 5) ) (if - (i32.const 0) + (call $return_int) (br $unlikely-continue$3) ) ) @@ -870,7 +870,7 @@ ) (func $dropCall (result i32) (if - (i32.const 0) + (call $return_int) (block (drop (call $phi) diff --git a/test/unit.fromasm.no-opts b/test/unit.fromasm.no-opts index 21e7ea79e..a7580f991 100644 --- a/test/unit.fromasm.no-opts +++ b/test/unit.fromasm.no-opts @@ -597,7 +597,7 @@ (i32.const 5) ) (if - (i32.const 0) + (call $return_int) (br $unlikely-continue$3) ) ) @@ -876,7 +876,7 @@ ) (func $dropCall (result i32) (if - (i32.const 0) + (call $return_int) (block (drop (call $phi) |