diff options
author | Max Graey <maxgraey@gmail.com> | 2022-09-20 22:32:34 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-20 12:32:34 -0700 |
commit | 4d22e2eac0eb4dfb2793ce0399f3c1a828d6544a (patch) | |
tree | 747664a159d64056c235e27b4cd5bee34e323d87 /src | |
parent | 32e0fed9e6619d52dee8c3b1f256d812cf115cb0 (diff) | |
download | binaryen-4d22e2eac0eb4dfb2793ce0399f3c1a828d6544a.tar.gz binaryen-4d22e2eac0eb4dfb2793ce0399f3c1a828d6544a.tar.bz2 binaryen-4d22e2eac0eb4dfb2793ce0399f3c1a828d6544a.zip |
[OptimizeInstructions] Simplify add / sub with negative on LHS or RHS for floating points (#5034)
```
(-x) + y -> y - x
x + (-y) -> x - y
x - (-y) -> x + y
```
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index f16b0c1be..2f1b6b6a4 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -414,6 +414,31 @@ struct OptimizeInstructions } } { + // -x + y ==> y - x + // where x, y are floating points + Expression* x; + if (matches(curr, binary(Add, unary(Neg, any(&x)), any()))) { + curr->op = Abstract::getBinary(curr->type, Sub); + curr->left = x; + std::swap(curr->left, curr->right); + return replaceCurrent(curr); + } + } + { + // x + (-y) ==> x - y + // x - (-y) ==> x + y + // where x, y are floating points + Expression* y; + if (matches(curr, binary(Add, any(), unary(Neg, any(&y)))) || + matches(curr, binary(Sub, any(), unary(Neg, any(&y))))) { + curr->op = Abstract::getBinary( + curr->type, + curr->op == Abstract::getBinary(curr->type, Add) ? Sub : Add); + curr->right = y; + return replaceCurrent(curr); + } + } + { // -x * -y ==> x * y // where x, y are integers Binary* bin; |