summaryrefslogtreecommitdiff
path: root/src/passes/OptimizeInstructions.cpp
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2020-10-18 20:56:07 +0300
committerGitHub <noreply@github.com>2020-10-18 10:56:07 -0700
commit8d8e1c53aa1b03ad4285437a6c41bc86f86731ca (patch)
tree8e3cc5c403f5d744d8be2d80d2913967ad1d3179 /src/passes/OptimizeInstructions.cpp
parentbde6878ee30baeda4a1ee82fb9c933768a17b6b2 (diff)
downloadbinaryen-8d8e1c53aa1b03ad4285437a6c41bc86f86731ca.tar.gz
binaryen-8d8e1c53aa1b03ad4285437a6c41bc86f86731ca.tar.bz2
binaryen-8d8e1c53aa1b03ad4285437a6c41bc86f86731ca.zip
Optimize comparisons with 0/1 in boolean context (#3240)
i32(bool(x)) != 0 ==> i32(bool(x)) i64(bool(x)) & 1 ==> i64(bool(x)) Also: * clean up related matching rules in optimizeWithConstantOnRight * add more explanations about isPowerOf2Float & rename to isPowerOfTwoInvertibleFloat
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r--src/passes/OptimizeInstructions.cpp30
1 files changed, 15 insertions, 15 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 1790623eb..2736c8984 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -617,13 +617,13 @@ struct OptimizeInstructions
}
if (binary->op == DivFloat32) {
float c = right->value.getf32();
- if (Bits::isPowerOf2Float(c)) {
+ if (Bits::isPowerOf2InvertibleFloat(c)) {
return optimizePowerOf2FDiv(binary, c);
}
}
if (binary->op == DivFloat64) {
double c = right->value.getf64();
- if (Bits::isPowerOf2Float(c)) {
+ if (Bits::isPowerOf2InvertibleFloat(c)) {
return optimizePowerOf2FDiv(binary, c);
}
}
@@ -1412,18 +1412,13 @@ private:
return curr;
}
}
- // bool(x) | 1 ==> 1
- if (matches(curr, binary(Abstract::Or, pure(&left), ival(1))) &&
- Bits::getMaxBits(left, this) == 1) {
- return right;
- }
- // bool(x) & 1 ==> bool(x)
- if (matches(curr, binary(Abstract::And, any(&left), ival(1))) &&
- Bits::getMaxBits(left, this) == 1) {
- return left;
- }
- // bool(x) == 1 ==> bool(x)
- if (matches(curr, binary(EqInt32, any(&left), i32(1))) &&
+ // i32(bool(x)) == 1 ==> i32(bool(x))
+ // i32(bool(x)) != 0 ==> i32(bool(x))
+ // i32(bool(x)) & 1 ==> i32(bool(x))
+ // i64(bool(x)) & 1 ==> i64(bool(x))
+ if ((matches(curr, binary(EqInt32, any(&left), i32(1))) ||
+ matches(curr, binary(NeInt32, any(&left), i32(0))) ||
+ matches(curr, binary(Abstract::And, any(&left), ival(1)))) &&
Bits::getMaxBits(left, this) == 1) {
return left;
}
@@ -1436,9 +1431,14 @@ private:
}
// bool(x) != 1 ==> !bool(x)
if (matches(curr, binary(Abstract::Ne, any(&left), ival(1))) &&
- Bits::getMaxBits(curr->left, this) == 1) {
+ Bits::getMaxBits(left, this) == 1) {
return builder.makeUnary(Abstract::getUnary(type, Abstract::EqZ), left);
}
+ // bool(x) | 1 ==> 1
+ if (matches(curr, binary(Abstract::Or, pure(&left), ival(1))) &&
+ Bits::getMaxBits(left, this) == 1) {
+ return right;
+ }
// Operations on all 1s
// x & -1 ==> x