summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai (kripken) <alonzakai@gmail.com>2017-07-10 18:48:02 -0700
committerAlon Zakai (kripken) <alonzakai@gmail.com>2017-07-11 11:07:45 -0700
commit4be7ec00157250ca83b61a2e1885645c67c6369c (patch)
treec0e6b98b252aad7307e1a915cbad7c5641625cab
parent45f8fedce6d432bfe24573ae4edb092202ef7bb9 (diff)
downloadbinaryen-4be7ec00157250ca83b61a2e1885645c67c6369c.tar.gz
binaryen-4be7ec00157250ca83b61a2e1885645c67c6369c.tar.bz2
binaryen-4be7ec00157250ca83b61a2e1885645c67c6369c.zip
handle unary and binary nodes that have implicit traps in vacuum
-rw-r--r--src/passes/Vacuum.cpp13
-rw-r--r--test/passes/vacuum.txt13
-rw-r--r--test/passes/vacuum.wast13
-rw-r--r--test/passes/vacuum_ignore-implicit-traps.txt4
-rw-r--r--test/passes/vacuum_ignore-implicit-traps.wast13
5 files changed, 55 insertions, 1 deletions
diff --git a/src/passes/Vacuum.cpp b/src/passes/Vacuum.cpp
index 70230e502..40f4a6e59 100644
--- a/src/passes/Vacuum.cpp
+++ b/src/passes/Vacuum.cpp
@@ -93,8 +93,14 @@ struct Vacuum : public WalkerPass<PostWalker<Vacuum>> {
if (resultUsed) {
return curr; // used, keep it
}
- // for unary, binary, and select, we need to check their arguments for side effects
+ // for unary, binary, and select, we need to check their arguments for side effects,
+ // as well as the node itself, as some unaries and binaries have implicit traps
if (auto* unary = curr->dynCast<Unary>()) {
+ EffectAnalyzer tester(getPassOptions());
+ tester.visitUnary(unary);
+ if (tester.hasSideEffects()) {
+ return curr;
+ }
if (EffectAnalyzer(getPassOptions(), unary->value).hasSideEffects()) {
curr = unary->value;
continue;
@@ -102,6 +108,11 @@ struct Vacuum : public WalkerPass<PostWalker<Vacuum>> {
return nullptr;
}
} else if (auto* binary = curr->dynCast<Binary>()) {
+ EffectAnalyzer tester(getPassOptions());
+ tester.visitBinary(binary);
+ if (tester.hasSideEffects()) {
+ return curr;
+ }
if (EffectAnalyzer(getPassOptions(), binary->left).hasSideEffects()) {
if (EffectAnalyzer(getPassOptions(), binary->right).hasSideEffects()) {
return curr; // leave them
diff --git a/test/passes/vacuum.txt b/test/passes/vacuum.txt
index f17607734..e07357296 100644
--- a/test/passes/vacuum.txt
+++ b/test/passes/vacuum.txt
@@ -260,4 +260,17 @@
(i64.const 0)
)
)
+ (func $unary-binary-may-trap (type $0)
+ (drop
+ (i64.div_s
+ (i64.const -1)
+ (i64.const 729618461987467893)
+ )
+ )
+ (drop
+ (i64.trunc_u/f32
+ (f32.const 70847791997969805621592064)
+ )
+ )
+ )
)
diff --git a/test/passes/vacuum.wast b/test/passes/vacuum.wast
index 65e528621..77ffabd60 100644
--- a/test/passes/vacuum.wast
+++ b/test/passes/vacuum.wast
@@ -580,4 +580,17 @@
(i64.const 0)
)
)
+ (func $unary-binary-may-trap
+ (drop
+ (i64.div_s
+ (i64.const 70847791997969805621592064)
+ (i64.const 729618461987467893)
+ )
+ )
+ (drop
+ (i64.trunc_u/f32
+ (f32.const 70847791997969805621592064)
+ )
+ )
+ )
)
diff --git a/test/passes/vacuum_ignore-implicit-traps.txt b/test/passes/vacuum_ignore-implicit-traps.txt
index 040a3dc1a..e5ff28583 100644
--- a/test/passes/vacuum_ignore-implicit-traps.txt
+++ b/test/passes/vacuum_ignore-implicit-traps.txt
@@ -1,5 +1,6 @@
(module
(type $0 (func (result i32)))
+ (type $1 (func))
(memory $0 0)
(func $load-would-normally-have-side-effects (type $0) (result i32)
(i64.ge_s
@@ -7,4 +8,7 @@
(i64.const 0)
)
)
+ (func $unary-binary-may-trap (type $1)
+ (nop)
+ )
)
diff --git a/test/passes/vacuum_ignore-implicit-traps.wast b/test/passes/vacuum_ignore-implicit-traps.wast
index 08b41a141..21e87e10c 100644
--- a/test/passes/vacuum_ignore-implicit-traps.wast
+++ b/test/passes/vacuum_ignore-implicit-traps.wast
@@ -15,5 +15,18 @@
(i64.const 0)
)
)
+ (func $unary-binary-may-trap
+ (drop
+ (i64.div_s
+ (i64.const 70847791997969805621592064)
+ (i64.const 729618461987467893)
+ )
+ )
+ (drop
+ (i64.trunc_u/f32
+ (f32.const 70847791997969805621592064)
+ )
+ )
+ )
)