summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2022-08-08 20:25:27 +0300
committerGitHub <noreply@github.com>2022-08-08 17:25:27 +0000
commit708bc424fcf6398c700dd6d209c6c55ff6f9e2e9 (patch)
tree20037137ebd920e0016ba3c96cbf0c2027d93450
parent680e4ecb581dd29947ecbaf3fb11817c187c30c9 (diff)
downloadbinaryen-708bc424fcf6398c700dd6d209c6c55ff6f9e2e9.tar.gz
binaryen-708bc424fcf6398c700dd6d209c6c55ff6f9e2e9.tar.bz2
binaryen-708bc424fcf6398c700dd6d209c6c55ff6f9e2e9.zip
[Optimize Instructions] Fold eqz(eqz(x)) to not-equal of zero (#4855)
eqz(eqz(i32(x))) -> i32(x) != 0 eqz(eqz(i64(x))) -> i64(x) != 0 Only when shrinkLevel == 0 (prefer speed over binary size).
-rw-r--r--src/passes/OptimizeInstructions.cpp21
-rw-r--r--test/lit/passes/optimize-instructions.wast34
2 files changed, 43 insertions, 12 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 828b2fdc9..e3a5774b1 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -875,11 +875,26 @@ struct OptimizeInstructions
if (matches(curr, unary(EqZInt32, unary(&inner, WrapInt64, any(&x)))) &&
Bits::getMaxBits(x, this) <= 32) {
inner->op = EqZInt64;
- inner->value = x;
return replaceCurrent(inner);
}
}
{
+ // i32.eqz(i32.eqz(x)) => i32(x) != 0
+ // i32.eqz(i64.eqz(x)) => i64(x) != 0
+ // iff shinkLevel == 0
+ // (1 instruction instead of 2, but 1 more byte)
+ if (getPassRunner()->options.shrinkLevel == 0) {
+ Expression* x;
+ if (matches(curr, unary(EqZInt32, unary(EqZ, any(&x))))) {
+ Builder builder(*getModule());
+ return replaceCurrent(builder.makeBinary(
+ getBinary(x->type, Ne),
+ x,
+ builder.makeConst(Literal::makeZero(x->type))));
+ }
+ }
+ }
+ {
// i64.extend_i32_s(i32.wrap_i64(x)) => x
// where maxBits(x) <= 31
//
@@ -900,12 +915,10 @@ struct OptimizeInstructions
if (getModule()->features.hasSignExt()) {
// i64.extend_i32_s(i32.wrap_i64(x)) => i64.extend32_s(x)
Unary* inner;
- Expression* x;
if (matches(curr,
- unary(ExtendSInt32, unary(&inner, WrapInt64, any(&x))))) {
+ unary(ExtendSInt32, unary(&inner, WrapInt64, any())))) {
inner->op = ExtendS32Int64;
inner->type = Type::i64;
- inner->value = x;
return replaceCurrent(inner);
}
}
diff --git a/test/lit/passes/optimize-instructions.wast b/test/lit/passes/optimize-instructions.wast
index e6d7c2c1d..1bf3f751c 100644
--- a/test/lit/passes/optimize-instructions.wast
+++ b/test/lit/passes/optimize-instructions.wast
@@ -8872,10 +8872,9 @@
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
- ;; CHECK-NEXT: (i32.eqz
- ;; CHECK-NEXT: (i32.eqz
- ;; CHECK-NEXT: (local.get $x)
- ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.ne
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
@@ -9612,6 +9611,26 @@
(i32.const 0)
))
)
+ ;; CHECK: (func $fold-eqz-eqz (param $x i32) (param $y i64)
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i32.ne
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (i64.ne
+ ;; CHECK-NEXT: (local.get $y)
+ ;; CHECK-NEXT: (i64.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $fold-eqz-eqz (param $x i32) (param $y i64)
+ ;; i32.eqz(i32.eqz(x)) -> x != 0
+ (drop (i32.eqz (i32.eqz (local.get $x))))
+ ;; i32.eqz(i64.eqz(y)) -> y != 0
+ (drop (i32.eqz (i64.eqz (local.get $y))))
+ )
;; CHECK: (func $optimize-bitwise-oprations (param $x i32) (param $y i32) (param $z i64) (param $w i64)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.rotl
@@ -12251,10 +12270,9 @@
;; CHECK-NEXT: (local.get $w)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
- ;; CHECK-NEXT: (i32.eqz
- ;; CHECK-NEXT: (i32.eqz
- ;; CHECK-NEXT: (local.get $x)
- ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (i32.ne
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop