summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp29
-rw-r--r--test/passes/optimize-instructions_all-features.txt8
2 files changed, 18 insertions, 19 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 3a344ff88..c54960079 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -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;
}
}
@@ -1308,15 +1312,6 @@ private:
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;
@@ -1325,10 +1320,14 @@ private:
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;
}
}
diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt
index 4d9c626fa..e12afcca4 100644
--- a/test/passes/optimize-instructions_all-features.txt
+++ b/test/passes/optimize-instructions_all-features.txt
@@ -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)
)
)
)