summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp8
-rw-r--r--test/emcc_hello_world.fromasm17
-rw-r--r--test/emcc_hello_world.fromasm.imprecise17
-rw-r--r--test/passes/optimize-instructions.txt20
-rw-r--r--test/passes/optimize-instructions.wast16
5 files changed, 52 insertions, 26 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 6ba34990f..1970cf08f 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -363,8 +363,12 @@ private:
}
}
} else if (auto* binary = boolean->dynCast<Binary>()) {
- // x != 0 is just x if it's used as a bool
- if (binary->op == NeInt32) {
+ if (binary->op == OrInt32) {
+ // an or flowing into a boolean context can consider each input as boolean
+ binary->left = optimizeBoolean(binary->left);
+ binary->right = optimizeBoolean(binary->right);
+ } else if (binary->op == NeInt32) {
+ // x != 0 is just x if it's used as a bool
if (auto* num = binary->right->dynCast<Const>()) {
if (num->value.geti32() == 0) {
return binary->left;
diff --git a/test/emcc_hello_world.fromasm b/test/emcc_hello_world.fromasm
index d1c3304ff..239c2a185 100644
--- a/test/emcc_hello_world.fromasm
+++ b/test/emcc_hello_world.fromasm
@@ -6339,15 +6339,11 @@
(i32.const 1)
)
)
- (i32.eqz
- (i32.eqz
- (i32.and
- (i32.load
- (get_local $0)
- )
- (i32.const 32)
- )
+ (i32.and
+ (i32.load
+ (get_local $0)
)
+ (i32.const 32)
)
)
)
@@ -7043,10 +7039,7 @@
(set_local $6
(if i32
(i32.or
- (i32.ne
- (get_local $7)
- (i32.const 0)
- )
+ (get_local $7)
(tee_local $9
(i32.or
(i32.ne
diff --git a/test/emcc_hello_world.fromasm.imprecise b/test/emcc_hello_world.fromasm.imprecise
index d95790044..8417befb4 100644
--- a/test/emcc_hello_world.fromasm.imprecise
+++ b/test/emcc_hello_world.fromasm.imprecise
@@ -6325,15 +6325,11 @@
(i32.const 1)
)
)
- (i32.eqz
- (i32.eqz
- (i32.and
- (i32.load
- (get_local $0)
- )
- (i32.const 32)
- )
+ (i32.and
+ (i32.load
+ (get_local $0)
)
+ (i32.const 32)
)
)
)
@@ -7029,10 +7025,7 @@
(set_local $6
(if i32
(i32.or
- (i32.ne
- (get_local $7)
- (i32.const 0)
- )
+ (get_local $7)
(tee_local $9
(i32.or
(i32.ne
diff --git a/test/passes/optimize-instructions.txt b/test/passes/optimize-instructions.txt
index 226f1766e..cb043f155 100644
--- a/test/passes/optimize-instructions.txt
+++ b/test/passes/optimize-instructions.txt
@@ -373,6 +373,26 @@
(call $ne0)
(nop)
)
+ (if
+ (i32.or
+ (call $ne0)
+ (call $ne0)
+ )
+ (nop)
+ )
+ (if
+ (i32.and
+ (i32.ne
+ (call $ne0)
+ (i32.const 0)
+ )
+ (i32.ne
+ (call $ne0)
+ (i32.const 0)
+ )
+ )
+ (nop)
+ )
(i32.const 1)
)
(func $recurse-bool (type $1)
diff --git a/test/passes/optimize-instructions.wast b/test/passes/optimize-instructions.wast
index dfa6365f9..3cf756548 100644
--- a/test/passes/optimize-instructions.wast
+++ b/test/passes/optimize-instructions.wast
@@ -294,6 +294,22 @@
(if (i32.ne (i32.const 0) (call $ne0))
(nop)
)
+ ;; through an or
+ (if
+ (i32.or
+ (i32.ne (i32.const 0) (call $ne0))
+ (i32.ne (i32.const 0) (call $ne0))
+ )
+ (nop)
+ )
+ ;; but not an and
+ (if
+ (i32.and
+ (i32.ne (i32.const 0) (call $ne0))
+ (i32.ne (i32.const 0) (call $ne0))
+ )
+ (nop)
+ )
(i32.const 1)
)
(func $recurse-bool