summaryrefslogtreecommitdiff
path: root/src/passes/OptimizeInstructions.cpp
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2020-09-30 05:36:17 +0300
committerGitHub <noreply@github.com>2020-09-29 19:36:17 -0700
commit7e99538386e047bddc41ad50b9762b7d4b9611b8 (patch)
treebd0d875a773e255c6e35be3806de71ec47250180 /src/passes/OptimizeInstructions.cpp
parent67a96ae60085366b59d64bc997c9e6525c247e27 (diff)
downloadbinaryen-7e99538386e047bddc41ad50b9762b7d4b9611b8.tar.gz
binaryen-7e99538386e047bddc41ad50b9762b7d4b9611b8.tar.bz2
binaryen-7e99538386e047bddc41ad50b9762b7d4b9611b8.zip
Simplify signed remainders compared with zero (#3153)
Specifically when the divisor is a power of two. `eqz((signed)x % C_pot)` -> `eqz(x & (C_pot - 1))` `(signed)x % C_pot != 0` -> `x & (C_pot - 1) != 0`
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r--src/passes/OptimizeInstructions.cpp38
1 files changed, 29 insertions, 9 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index b0d12eb95..1f65ae609 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -243,13 +243,6 @@ struct OptimizeInstructions
using namespace Match;
Builder builder(*getModule());
{
- // X == 0 => eqz X
- Expression* x;
- if (matches(curr, binary(EqInt32, any(&x), i32(0)))) {
- return Builder(*getModule()).makeUnary(EqZInt32, x);
- }
- }
- {
// try to get rid of (0 - ..), that is, a zero only used to negate an
// int. an add of a subtract can be flipped in order to remove it:
// (i32.add
@@ -302,6 +295,19 @@ struct OptimizeInstructions
}
}
{
+ // eqz((signed)x % C_pot) => eqz(x & (C_pot - 1))
+ Const* c;
+ Binary* inner;
+ if (matches(curr,
+ unary(Abstract::EqZ,
+ binary(&inner, Abstract::RemS, any(), ival(&c)))) &&
+ IsPowerOf2((uint64_t)c->value.getInteger())) {
+ inner->op = Abstract::getBinary(c->type, Abstract::And);
+ c->value = c->value.sub(Literal::makeFromInt32(1, c->type));
+ return curr;
+ }
+ }
+ {
// try de-morgan's AND law,
// (eqz X) and (eqz Y) === eqz (X or Y)
// Note that the OR and XOR laws do not work here, as these
@@ -1272,8 +1278,8 @@ private:
return right;
}
// x == 0 ==> eqz x
- if ((matches(curr, binary(Abstract::Eq, any(&left), ival(0))))) {
- return builder.makeUnary(EqZInt64, left);
+ if (matches(curr, binary(Abstract::Eq, any(&left), ival(0)))) {
+ return builder.makeUnary(Abstract::getUnary(type, Abstract::EqZ), left);
}
// Operations on one
// (signed)x % 1 ==> 0
@@ -1281,6 +1287,20 @@ private:
right->value = Literal::makeSingleZero(type);
return right;
}
+ // (signed)x % C_pot != 0 ==> x & (C_pot - 1) != 0
+ {
+ Const* c;
+ Binary* inner;
+ if (matches(curr,
+ binary(Abstract::Ne,
+ binary(&inner, Abstract::RemS, any(), ival(&c)),
+ ival(0))) &&
+ IsPowerOf2((uint64_t)c->value.getInteger())) {
+ inner->op = Abstract::getBinary(c->type, Abstract::And);
+ c->value = c->value.sub(Literal::makeFromInt32(1, c->type));
+ return curr;
+ }
+ }
// bool(x) | 1 ==> 1
if (matches(curr, binary(Abstract::Or, pure(&left), ival(1))) &&
Bits::getMaxBits(left, this) == 1) {