diff options
author | Max Graey <maxgraey@gmail.com> | 2020-11-03 18:24:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-03 08:24:17 -0800 |
commit | 081019104a09b5201fa1307a4758c19ca392c2d8 (patch) | |
tree | 692b220a3863e6ac473a39f92d34876eeb8f5117 /src/passes/OptimizeInstructions.cpp | |
parent | cdc2d2c006bc0406c93509ed0d7a2dfbb6f51ea5 (diff) | |
download | binaryen-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
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 15 |
1 files changed, 12 insertions, 3 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))) || |