From fc7f66a870f6bcf17091d6cb8b7545a8e7593e7c Mon Sep 17 00:00:00 2001 From: Max Graey Date: Mon, 5 Oct 2020 23:37:43 +0300 Subject: fast-math: Fold `fp * -1` to `-fp` (#3189) --- src/passes/OptimizeInstructions.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index de96e418e..3a344ff88 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1447,8 +1447,7 @@ private: } { double value; - if (fastMath && - matches(curr, binary(Abstract::Sub, any(), fval(&value))) && + if (matches(curr, binary(Abstract::Sub, any(), fval(&value))) && value == 0.0) { // x - (-0.0) ==> x + 0.0 if (std::signbit(value)) { @@ -1470,6 +1469,10 @@ private: return curr->left; } } + // x * -1.0 ==> -x + if (fastMath && matches(curr, binary(Abstract::Mul, any(), fval(-1.0)))) { + return builder.makeUnary(Abstract::getUnary(type, Abstract::Neg), left); + } if (matches(curr, binary(Abstract::Mul, any(&left), constant(1))) || matches(curr, binary(Abstract::DivS, any(&left), constant(1))) || matches(curr, binary(Abstract::DivU, any(&left), constant(1)))) { -- cgit v1.2.3