Skip to content

Generalize transforms for #3153 #3193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 80 additions & 5 deletions src/literal.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,57 @@ class Literal {
}
return false;
}
bool isZero() const {
switch (type.getBasic()) {
case Type::i32:
return i32 == 0;
case Type::i64:
return i64 == 0LL;
case Type::f32:
return bit_cast<float>(i32) == 0.0f;
case Type::f64:
return bit_cast<double>(i64) == 0.0;
case Type::v128: {
uint8_t zeros[16] = {0};
return memcmp(&v128, zeros, 16) == 0;
}
default:
WASM_UNREACHABLE("unexpected type");
}
}
bool isSignedMin() const {
switch (type.getBasic()) {
case Type::i32:
return i32 == std::numeric_limits<int32_t>::min();
case Type::i64:
return i64 == std::numeric_limits<int64_t>::min();
default:
WASM_UNREACHABLE("unexpected type");
}
}
bool isSignedMax() const {
switch (type.getBasic()) {
case Type::i32:
return i32 == std::numeric_limits<int32_t>::max();
case Type::i64:
return i64 == std::numeric_limits<int64_t>::max();
default:
WASM_UNREACHABLE("unexpected type");
}
}
bool isUnsignedMax() const {
switch (type.getBasic()) {
case Type::i32:
return uint32_t(i32) == std::numeric_limits<uint32_t>::max();
case Type::i64:
return uint64_t(i64) == std::numeric_limits<uint64_t>::max();
default:
WASM_UNREACHABLE("unexpected type");
}
}

static Literals makeZero(Type type);
static Literal makeSingleZero(Type type);
static Literal makeFromInt32(int32_t x, Type type) {
switch (type.getBasic()) {
case Type::i32:
Expand All @@ -116,7 +166,6 @@ class Literal {
WASM_UNREACHABLE("unexpected type");
}
}

static Literal makeFromUInt64(uint64_t x, Type type) {
switch (type.getBasic()) {
case Type::i32:
Expand All @@ -131,10 +180,36 @@ class Literal {
WASM_UNREACHABLE("unexpected type");
}
}

static Literals makeZero(Type type);
static Literal makeSingleZero(Type type);

static Literal makeSignedMin(Type type) {
switch (type.getBasic()) {
case Type::i32:
return Literal(std::numeric_limits<int32_t>::min());
case Type::i64:
return Literal(std::numeric_limits<int64_t>::min());
default:
WASM_UNREACHABLE("unexpected type");
}
}
static Literal makeSignedMax(Type type) {
switch (type.getBasic()) {
case Type::i32:
return Literal(std::numeric_limits<int32_t>::max());
case Type::i64:
return Literal(std::numeric_limits<int64_t>::max());
default:
WASM_UNREACHABLE("unexpected type");
}
}
static Literal makeUnsignedMax(Type type) {
switch (type.getBasic()) {
case Type::i32:
return Literal(std::numeric_limits<uint32_t>::max());
case Type::i64:
return Literal(std::numeric_limits<uint64_t>::max());
default:
WASM_UNREACHABLE("unexpected type");
}
}
static Literal makeNull(Type type) {
assert(type.isNullable());
return Literal(type);
Expand Down
23 changes: 17 additions & 6 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,16 @@ struct OptimizeInstructions
}
}
{
// eqz((signed)x % C_pot) => eqz(x & (C_pot - 1))
// eqz((signed)x % C_pot) => eqz(x & (abs(C_pot) - 1))
Const* c;
Binary* inner;
if (matches(curr,
unary(Abstract::EqZ,
binary(&inner, Abstract::RemS, any(), ival(&c)))) &&
Bits::isPowerOf2((uint64_t)c->value.getInteger())) {
!c->value.isSignedMin() &&
Bits::isPowerOf2(c->value.abs().getInteger())) {
inner->op = Abstract::getBinary(c->type, Abstract::And);
c->value = c->value.sub(Literal::makeFromInt32(1, c->type));
c->value = c->value.abs().sub(Literal::makeFromInt32(1, c->type));
return curr;
}
}
Expand Down Expand Up @@ -1307,17 +1308,27 @@ struct OptimizeInstructions
right->value = Literal::makeSingleZero(type);
return right;
}
// (signed)x % C_pot != 0 ==> x & (C_pot - 1) != 0
{
// (signed)x % (i32|i64).min_s ==> (x & (i32|i64).max_s)
if (matches(curr, binary(Abstract::RemS, any(&left), ival())) &&
right->value.isSignedMin()) {
curr->op = Abstract::getBinary(type, Abstract::And);
right->value = Literal::makeSignedMax(type);
return curr;
}
}
// (signed)x % C_pot != 0 ==> (x & (abs(C_pot) - 1)) != 0
{
Const* c;
Binary* inner;
if (matches(curr,
binary(Abstract::Ne,
binary(&inner, Abstract::RemS, any(), ival(&c)),
ival(0))) &&
Bits::isPowerOf2((uint64_t)c->value.getInteger())) {
!c->value.isSignedMin() &&
Bits::isPowerOf2(c->value.abs().getInteger())) {
inner->op = Abstract::getBinary(c->type, Abstract::And);
c->value = c->value.sub(Literal::makeFromInt32(1, c->type));
c->value = c->value.abs().sub(Literal::makeFromInt32(1, c->type));
return curr;
}
}
Expand Down
83 changes: 79 additions & 4 deletions test/passes/optimize-instructions_all-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2737,13 +2737,25 @@
)
(unreachable)
)
(func $srem-by-1 (param $x i32) (param $y i64)
(func $srem-by-const (param $x i32) (param $y i64)
(drop
(i32.const 0)
)
(drop
(i64.const 0)
)
(drop
(i32.and
(local.get $x)
(i32.const 2147483647)
)
)
(drop
(i64.and
(local.get $y)
(i64.const 9223372036854775807)
)
)
)
(func $srem-by-pot-eq-ne-zero (param $x i32) (param $y i64)
(drop
Expand All @@ -2770,6 +2782,22 @@
)
)
)
(drop
(i64.eqz
(i64.and
(local.get $y)
(i64.const 3)
)
)
)
(drop
(i32.eqz
(i32.and
(local.get $x)
(i32.const 3)
)
)
)
(drop
(i64.eqz
(i64.and
Expand All @@ -2778,6 +2806,22 @@
)
)
)
(drop
(i32.eqz
(i32.and
(local.get $x)
(i32.const 3)
)
)
)
(drop
(i64.eqz
(i64.and
(local.get $y)
(i64.const 3)
)
)
)
(drop
(i32.ne
(i32.and
Expand All @@ -2797,17 +2841,48 @@
)
(drop
(i32.eqz
(i32.rem_s
(i32.const 0)
)
)
(drop
(i32.eqz
(i32.and
(local.get $x)
(i32.const 3)
(i32.const 2147483647)
)
)
)
(drop
(i32.ne
(i32.and
(local.get $x)
(i32.const 2147483647)
)
(i32.const 0)
)
)
(drop
(i64.eqz
(i64.and
(local.get $y)
(i64.const 9223372036854775807)
)
)
)
(drop
(i64.ne
(i64.and
(local.get $y)
(i64.const 9223372036854775807)
)
(i64.const 0)
)
)
(drop
(i32.eqz
(i32.rem_s
(local.get $x)
(i32.const -4)
(i32.const 3)
)
)
)
Expand Down
Loading