Skip to content
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
29 changes: 14 additions & 15 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,14 @@ struct OptimizeInstructions
if (matches(curr,
unary(Abstract::EqZ,
binary(&inner, Abstract::RemS, any(), ival(&c)))) &&
!c->value.isSignedMin() &&
Bits::isPowerOf2(c->value.abs().getInteger())) {
(c->value.isSignedMin() ||
Bits::isPowerOf2(c->value.abs().getInteger()))) {
inner->op = Abstract::getBinary(c->type, Abstract::And);
c->value = c->value.abs().sub(Literal::makeFromInt32(1, c->type));
if (c->value.isSignedMin()) {
c->value = Literal::makeSignedMax(c->type);
} else {
c->value = c->value.abs().sub(Literal::makeFromInt32(1, c->type));
}
return curr;
}
}
Expand Down Expand Up @@ -1308,15 +1312,6 @@ struct OptimizeInstructions
right->value = Literal::makeSingleZero(type);
return right;
}
{
// (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;
Expand All @@ -1325,10 +1320,14 @@ struct OptimizeInstructions
binary(Abstract::Ne,
binary(&inner, Abstract::RemS, any(), ival(&c)),
ival(0))) &&
!c->value.isSignedMin() &&
Bits::isPowerOf2(c->value.abs().getInteger())) {
(c->value.isSignedMin() ||
Bits::isPowerOf2(c->value.abs().getInteger()))) {
inner->op = Abstract::getBinary(c->type, Abstract::And);
c->value = c->value.abs().sub(Literal::makeFromInt32(1, c->type));
if (c->value.isSignedMin()) {
c->value = Literal::makeSignedMax(c->type);
} else {
c->value = c->value.abs().sub(Literal::makeFromInt32(1, c->type));
}
return curr;
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/passes/optimize-instructions_all-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2745,15 +2745,15 @@
(i64.const 0)
)
(drop
(i32.and
(i32.rem_s
(local.get $x)
(i32.const 2147483647)
(i32.const -2147483648)
)
)
(drop
(i64.and
(i64.rem_s
(local.get $y)
(i64.const 9223372036854775807)
(i64.const -9223372036854775808)
)
)
)
Expand Down