summaryrefslogtreecommitdiff
path: root/src/passes/OptimizeInstructions.cpp
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 /src/passes/OptimizeInstructions.cpp
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).
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r--src/passes/OptimizeInstructions.cpp21
1 files changed, 17 insertions, 4 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);
}
}