summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/Vacuum.cpp20
-rw-r--r--test/passes/vacuum.txt11
-rw-r--r--test/passes/vacuum.wast15
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)
+ )
)
)