summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp8
-rw-r--r--test/passes/optimize-instructions_all-features.txt11
-rw-r--r--test/passes/optimize-instructions_all-features.wast12
3 files changed, 30 insertions, 1 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index cbd8c9323..2e59775c0 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -982,6 +982,14 @@ private:
}
}
} else if (auto* binary = boolean->dynCast<Binary>()) {
+ if (binary->op == SubInt32) {
+ if (auto* num = binary->left->dynCast<Const>()) {
+ if (num->value.geti32() == 0) {
+ // bool(0 - x) ==> bool(x)
+ return binary->right;
+ }
+ }
+ }
if (binary->op == OrInt32) {
// an or flowing into a boolean context can consider each input as
// boolean
diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt
index 8da31f107..76c6ffe0a 100644
--- a/test/passes/optimize-instructions_all-features.txt
+++ b/test/passes/optimize-instructions_all-features.txt
@@ -3,9 +3,9 @@
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $none_=>_i32 (func (result i32)))
(type $none_=>_none (func))
+ (type $i32_=>_none (func (param i32)))
(type $i32_i64_=>_none (func (param i32 i64)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
- (type $i32_=>_none (func (param i32)))
(type $none_=>_i64 (func (result i64)))
(type $i64_=>_i64 (func (param i64) (result i64)))
(type $i32_i64_f32_=>_none (func (param i32 i64 f32)))
@@ -3265,6 +3265,15 @@
)
)
)
+ (func $optimize-boolean (param $x i32)
+ (drop
+ (select
+ (i32.const 1)
+ (i32.const 2)
+ (local.get $x)
+ )
+ )
+ )
(func $getFallthrough
(local $x0 i32)
(local $x1 i32)
diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast
index a0fff2936..c62f0bc42 100644
--- a/test/passes/optimize-instructions_all-features.wast
+++ b/test/passes/optimize-instructions_all-features.wast
@@ -3766,6 +3766,18 @@
)
)
)
+ (func $optimize-boolean (param $x i32)
+ (drop
+ (select
+ (i32.const 1)
+ (i32.const 2)
+ (i32.sub ;; bool(-x) -> bool(x)
+ (i32.const 0)
+ (local.get $x)
+ )
+ )
+ )
+ )
(func $getFallthrough ;; unit tests for Properties::getFallthrough
(local $x0 i32)
(local $x1 i32)