diff options
-rw-r--r-- | src/passes/Vacuum.cpp | 9 | ||||
-rw-r--r-- | test/passes/vacuum_all-features.txt (renamed from test/passes/vacuum.txt) | 24 | ||||
-rw-r--r-- | test/passes/vacuum_all-features.wast (renamed from test/passes/vacuum.wast) | 30 |
3 files changed, 63 insertions, 0 deletions
diff --git a/src/passes/Vacuum.cpp b/src/passes/Vacuum.cpp index f0bdd7722..54707fddc 100644 --- a/src/passes/Vacuum.cpp +++ b/src/passes/Vacuum.cpp @@ -414,6 +414,15 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum>> { } } + void visitTry(Try* curr) { + // If try's body does not throw, the whole try-catch can be replaced with + // the try's body. + if (!EffectAnalyzer(getPassOptions(), getModule()->features, curr->body) + .throws) { + replaceCurrent(curr->body); + } + } + void visitFunction(Function* curr) { auto* optimized = optimize(curr->body, curr->sig.results != Type::none, true); diff --git a/test/passes/vacuum.txt b/test/passes/vacuum_all-features.txt index bf6596cac..8ca7dacdf 100644 --- a/test/passes/vacuum.txt +++ b/test/passes/vacuum_all-features.txt @@ -440,3 +440,27 @@ ) ) ) +(module + (type $none_=>_none (func)) + (type $i32_=>_none (func (param i32))) + (event $e (attr 0) (param i32)) + (func $try-test (; 0 ;) + (nop) + ) + (func $inner-try-test (; 1 ;) + (local $0 i32) + (try + (throw $e + (i32.const 0) + ) + (catch + (drop + (exnref.pop) + ) + (local.set $0 + (i32.const 1) + ) + ) + ) + ) +) diff --git a/test/passes/vacuum.wast b/test/passes/vacuum_all-features.wast index 0408277d7..98f13dd69 100644 --- a/test/passes/vacuum.wast +++ b/test/passes/vacuum_all-features.wast @@ -795,3 +795,33 @@ ) ) ) + +(module + (event $e (attr 0) (param i32)) + ;; When try body does not throw, try-body can be replaced with the try body + (func $try-test + (try + (drop (i32.const 0)) + (catch + (drop (exnref.pop)) + ) + ) + ) + + ;; The exception thrown in the inner try is caught by the inner catch, so the + ;; outer try body does not throw and the outer try-catch can be removed + (func $inner-try-test (local $0 i32) + (try + (try + (throw $e (i32.const 0)) + (catch + (drop (exnref.pop)) + (local.set $0 (i32.const 1)) + ) + ) + (catch + (drop (exnref.pop)) + ) + ) + ) +) |