From 4d22e2eac0eb4dfb2793ce0399f3c1a828d6544a Mon Sep 17 00:00:00 2001 From: Max Graey Date: Tue, 20 Sep 2022 22:32:34 +0300 Subject: [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 ``` --- src/passes/OptimizeInstructions.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src') 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 @@ -413,6 +413,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 -- cgit v1.2.3