diff options
author | Max Graey <maxgraey@gmail.com> | 2020-07-09 03:01:56 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-08 17:01:56 -0700 |
commit | ec5a88c12bea053200c4529758cf1f5ce916e455 (patch) | |
tree | 443c09caf8bb6e6cb025d8171c7bdc5b30c2d9bf | |
parent | 18095a6c6030fb157f89889b8094eca0b3f654cb (diff) | |
download | binaryen-ec5a88c12bea053200c4529758cf1f5ce916e455.tar.gz binaryen-ec5a88c12bea053200c4529758cf1f5ce916e455.tar.bz2 binaryen-ec5a88c12bea053200c4529758cf1f5ce916e455.zip |
Optimize booleans when argument is negative integer (#2930)
bool(-x) ==> bool(x)
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 8 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.txt | 11 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.wast | 12 |
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) |