summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2020-11-03 18:24:17 +0200
committerGitHub <noreply@github.com>2020-11-03 08:24:17 -0800
commit081019104a09b5201fa1307a4758c19ca392c2d8 (patch)
tree692b220a3863e6ac473a39f92d34876eeb8f5117
parentcdc2d2c006bc0406c93509ed0d7a2dfbb6f51ea5 (diff)
downloadbinaryen-081019104a09b5201fa1307a4758c19ca392c2d8.tar.gz
binaryen-081019104a09b5201fa1307a4758c19ca392c2d8.tar.bz2
binaryen-081019104a09b5201fa1307a4758c19ca392c2d8.zip
Optimize x * -1.0 in non-fastMath case (#3315)
We can still make x * -1.0 cheaper for non-fastMath mode as: x * -1.0 -> -0.0 - x Should at least help baseline compilers. Also it could enable further optimizations, e.g.: a + b * -1 a + (-0.0 - b) (a - 0.0) - b a - b
-rw-r--r--src/passes/OptimizeInstructions.cpp15
-rw-r--r--test/passes/optimize-instructions_all-features.txt20
2 files changed, 22 insertions, 13 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 003f204a9..9b47f6a0f 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -1697,9 +1697,18 @@ private:
curr->left = left;
return curr;
}
- // x * -1.0 ==> -x
- if (fastMath && matches(curr, binary(Mul, any(), fval(-1.0)))) {
- return builder.makeUnary(Abstract::getUnary(type, Neg), left);
+ // x * -1.0 ==>
+ // -x, if fastMath == true
+ // -0.0 - x, if fastMath == false
+ if (matches(curr, binary(Mul, any(), fval(-1.0)))) {
+ if (fastMath) {
+ return builder.makeUnary(Abstract::getUnary(type, Neg), left);
+ }
+ // x * -1.0 ==> -0.0 - x
+ curr->op = Abstract::getBinary(type, Sub);
+ right->value = Literal::makeZero(type).neg();
+ std::swap(curr->left, curr->right);
+ return curr;
}
if (matches(curr, binary(Mul, any(&left), constant(1))) ||
matches(curr, binary(DivS, any(&left), constant(1))) ||
diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt
index 1873b8d3a..b80330ad6 100644
--- a/test/passes/optimize-instructions_all-features.txt
+++ b/test/passes/optimize-instructions_all-features.txt
@@ -3354,15 +3354,15 @@
)
)
(drop
- (f32.mul
+ (f32.sub
+ (f32.const -0)
(local.get $y32)
- (f32.const -1)
)
)
(drop
- (f64.mul
+ (f64.sub
+ (f64.const -0)
(local.get $y64)
- (f64.const -1)
)
)
(drop
@@ -4276,15 +4276,15 @@
)
)
(drop
- (f32.mul
+ (f32.sub
+ (f32.const -0)
(local.get $fx)
- (f32.const -1)
)
)
(drop
- (f64.mul
+ (f64.sub
+ (f64.const -0)
(local.get $fy)
- (f64.const -1)
)
)
(drop
@@ -4362,9 +4362,9 @@
)
)
(drop
- (f32.mul
+ (f32.sub
+ (f32.const -0)
(local.get $fx)
- (f32.const -1)
)
)
(drop