diff options
author | Max Graey <maxgraey@gmail.com> | 2020-10-05 23:37:43 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-05 13:37:43 -0700 |
commit | fc7f66a870f6bcf17091d6cb8b7545a8e7593e7c (patch) | |
tree | bd4ecd5bdb6821e406970edfb074163ec74fe43a | |
parent | 654cfca4fabda64dd91d2df9b07422b8bffc9357 (diff) | |
download | binaryen-fc7f66a870f6bcf17091d6cb8b7545a8e7593e7c.tar.gz binaryen-fc7f66a870f6bcf17091d6cb8b7545a8e7593e7c.tar.bz2 binaryen-fc7f66a870f6bcf17091d6cb8b7545a8e7593e7c.zip |
fast-math: Fold `fp * -1` to `-fp` (#3189)
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 7 | ||||
-rw-r--r-- | test/passes/O_fast-math.txt | 14 | ||||
-rw-r--r-- | test/passes/O_fast-math.wast | 12 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.txt | 8 |
4 files changed, 35 insertions, 6 deletions
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)))) { diff --git a/test/passes/O_fast-math.txt b/test/passes/O_fast-math.txt index 1b454c68e..9aadd3d90 100644 --- a/test/passes/O_fast-math.txt +++ b/test/passes/O_fast-math.txt @@ -1,5 +1,7 @@ (module (type $none_=>_f32 (func (result f32))) + (type $f32_=>_f32 (func (param f32) (result f32))) + (type $f64_=>_f64 (func (param f64) (result f64))) (export "div" (func $0)) (export "mul1" (func $1)) (export "mul2" (func $2)) @@ -9,6 +11,8 @@ (export "add4" (func $2)) (export "sub1" (func $1)) (export "sub2" (func $2)) + (export "mul_neg_one1" (func $9)) + (export "mul_neg_one2" (func $10)) (func $0 (; has Stack IR ;) (result f32) (f32.const -nan:0x23017a) ) @@ -18,4 +22,14 @@ (func $2 (; has Stack IR ;) (result f32) (f32.const -nan:0x74546d) ) + (func $9 (; has Stack IR ;) (param $0 f32) (result f32) + (f32.neg + (local.get $0) + ) + ) + (func $10 (; has Stack IR ;) (param $0 f64) (result f64) + (f64.neg + (local.get $0) + ) + ) ) diff --git a/test/passes/O_fast-math.wast b/test/passes/O_fast-math.wast index 2317f782d..ce2cd7b6e 100644 --- a/test/passes/O_fast-math.wast +++ b/test/passes/O_fast-math.wast @@ -54,4 +54,16 @@ (f32.const -0) ) ) + (func "mul_neg_one1" (param $x f32) (result f32) + (f32.mul + (local.get $x) + (f32.const -1) + ) + ) + (func "mul_neg_one2" (param $x f64) (result f64) + (f64.mul + (local.get $x) + (f64.const -1) + ) + ) ) diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt index 272cce240..4d9c626fa 100644 --- a/test/passes/optimize-instructions_all-features.txt +++ b/test/passes/optimize-instructions_all-features.txt @@ -3829,15 +3829,15 @@ ) ) (drop - (f32.sub + (f32.add (local.get $fx) - (f32.const -0) + (f32.const 0) ) ) (drop - (f64.sub + (f64.add (local.get $fy) - (f64.const -0) + (f64.const 0) ) ) (drop |