diff options
author | Max Graey <maxgraey@gmail.com> | 2020-09-30 22:21:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-30 12:21:30 -0700 |
commit | 11de8894505d37b7b970a2103bc5b1cfd094b115 (patch) | |
tree | 6c111c5f53da2516a571484d775be03b3601bbdb | |
parent | 7e99538386e047bddc41ad50b9762b7d4b9611b8 (diff) | |
download | binaryen-11de8894505d37b7b970a2103bc5b1cfd094b115.tar.gz binaryen-11de8894505d37b7b970a2103bc5b1cfd094b115.tar.bz2 binaryen-11de8894505d37b7b970a2103bc5b1cfd094b115.zip |
Fold i32.eqz(wrap_i64(x)) -> i64.eqz(x) where possible (#3181)
Specifically, when `x` has at most 32 bits so that wrapping doesn't change its value.
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 12 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.txt | 36 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.wast | 51 |
3 files changed, 99 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 1f65ae609..36d92e81f 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -330,6 +330,18 @@ struct OptimizeInstructions return un; } } + { + // i32.eqz(i32.wrap_i64(x)) => i64.eqz(x) + // where maxBits(x) <= 32 + Unary* inner; + Expression* x; + if (matches(curr, unary(EqZInt32, unary(&inner, WrapInt64, any(&x)))) && + Bits::getMaxBits(x, this) <= 32) { + inner->op = EqZInt64; + inner->value = x; + return inner; + } + } } if (auto* select = curr->dynCast<Select>()) { diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt index 860783f36..873d550d7 100644 --- a/test/passes/optimize-instructions_all-features.txt +++ b/test/passes/optimize-instructions_all-features.txt @@ -3516,6 +3516,42 @@ ) ) (drop + (i32.eqz + (i32.shr_u + (local.get $x) + (i32.const 31) + ) + ) + ) + (drop + (i64.eqz + (i64.shr_u + (local.get $y) + (i64.const 63) + ) + ) + ) + (drop + (i64.eqz + (i64.shr_u + (local.get $y) + (i64.const 63) + ) + ) + ) + (drop + (i64.eqz + (local.get $y) + ) + ) + (drop + (i32.eqz + (i32.wrap_i64 + (local.get $y) + ) + ) + ) + (drop (i32.wrap_i64 (i64.and (local.get $y) diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast index 1afa3e5b5..59e7e21d8 100644 --- a/test/passes/optimize-instructions_all-features.wast +++ b/test/passes/optimize-instructions_all-features.wast @@ -4063,6 +4063,57 @@ ) (i64.const 0) )) + ;; eqz((i32(bool(expr)) != 0) != 0) + (drop (i32.eqz + (i32.ne + (i32.ne + (i32.shr_u + (local.get $x) + (i32.const 31) + ) + (i32.const 0) + ) + (i32.const 0) + ) + )) + ;; i32.eqz(wrap(i64(x))) + (drop (i32.eqz + (i32.wrap_i64 + (i64.shr_u + (local.get $y) + (i64.const 63) + ) + ) + )) + ;; eqz((i64(bool(expr)) != 0) != 0) + (drop (i32.eqz + (i32.ne + (i64.ne + (i64.shr_u + (local.get $y) + (i64.const 63) + ) + (i64.const 0) + ) + (i32.const 0) + ) + )) + ;; eqz((i64(bool(expr)) != 0) != 0) + (drop (i32.eqz + (i32.ne + (i64.ne + (local.get $y) + (i64.const 0) + ) + (i32.const 0) + ) + )) + ;; i32.eqz(wrap(i64(x))) -> skip + (drop (i32.eqz + (i32.wrap_i64 + (local.get $y) + ) + )) ;; i64(bool(expr)) == 1 -> i32(bool(expr)) (drop (i64.eq (i64.and |