diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-04-23 21:39:52 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-04-24 09:36:04 -0700 |
commit | 203d502f9c3c5fc1f16822d3432087785653d767 (patch) | |
tree | a69336ce42338687081b3d1fe5b61ce6ba682d42 | |
parent | 9f1849ef99db9fa8a8a44daec348b7f2bb562453 (diff) | |
download | binaryen-203d502f9c3c5fc1f16822d3432087785653d767.tar.gz binaryen-203d502f9c3c5fc1f16822d3432087785653d767.tar.bz2 binaryen-203d502f9c3c5fc1f16822d3432087785653d767.zip |
get rid of nops in ifs
-rw-r--r-- | src/passes/Vacuum.cpp | 20 | ||||
-rw-r--r-- | test/passes/vacuum.txt | 11 | ||||
-rw-r--r-- | test/passes/vacuum.wast | 15 |
3 files changed, 46 insertions, 0 deletions
diff --git a/src/passes/Vacuum.cpp b/src/passes/Vacuum.cpp index 12565f603..dd88fa0eb 100644 --- a/src/passes/Vacuum.cpp +++ b/src/passes/Vacuum.cpp @@ -21,6 +21,7 @@ #include <wasm.h> #include <pass.h> #include <ast_utils.h> +#include <wasm-builder.h> namespace wasm { @@ -62,6 +63,25 @@ struct Vacuum : public WalkerPass<PostWalker<Vacuum, Visitor<Vacuum>>> { } } } + + void visitIf(If* curr) { + if (curr->ifFalse) { + if (curr->ifFalse->is<Nop>()) { + curr->ifFalse = nullptr; + } else if (curr->ifTrue->is<Nop>()) { + curr->ifTrue = curr->ifFalse; + curr->ifFalse = nullptr; + curr->condition = Builder(*getModule()).makeUnary(EqZ, curr->condition, curr->condition->type); + } + } + if (!curr->ifFalse) { + // no else + if (curr->ifTrue->is<Nop>()) { + // no nothing + replaceCurrent(curr->condition); + } + } + } }; static RegisterPass<Vacuum> registerPass("vacuum", "removes obviously unneeded code"); diff --git a/test/passes/vacuum.txt b/test/passes/vacuum.txt index 103dbc6bc..d604cfa08 100644 --- a/test/passes/vacuum.txt +++ b/test/passes/vacuum.txt @@ -16,5 +16,16 @@ (i32.const 57) ) ) + (if + (i32.eqz + (i32.const 100) + ) + (i32.const 101) + ) + (if + (i32.const 102) + (i32.const 103) + ) + (i32.const 104) ) ) diff --git a/test/passes/vacuum.wast b/test/passes/vacuum.wast index a0c638eb8..28125b969 100644 --- a/test/passes/vacuum.wast +++ b/test/passes/vacuum.wast @@ -23,6 +23,21 @@ ) (i32.const 58) ) + (if + (i32.const 100) + (nop) + (i32.const 101) + ) + (if + (i32.const 102) + (i32.const 103) + (nop) + ) + (if + (i32.const 104) + (nop) + (nop) + ) ) ) |