diff options
author | Max Graey <maxgraey@gmail.com> | 2020-10-28 00:42:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-27 15:42:05 -0700 |
commit | 5c31f461afe87088e1e0972dbcbf0507c15b2880 (patch) | |
tree | e7ca96cd38a3ae24fa1f3ad36d206d1589bd62fb | |
parent | c015eebaee6161112eb82b1faa892c864ddffa27 (diff) | |
download | binaryen-5c31f461afe87088e1e0972dbcbf0507c15b2880.tar.gz binaryen-5c31f461afe87088e1e0972dbcbf0507c15b2880.tar.bz2 binaryen-5c31f461afe87088e1e0972dbcbf0507c15b2880.zip |
Replace x * 2 with x + x for floats (#3016)
But only when doing so doesn't require adding a new local.
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 13 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.txt | 29 | ||||
-rw-r--r-- | test/passes/optimize-instructions_all-features.wast | 19 |
3 files changed, 61 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index b11df3276..89315ab60 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1664,6 +1664,19 @@ private: } } { + // x * 2.0 ==> x + x + // but we apply this only for simple expressions like + // local.get and global.get for avoid using extra local + // variable. + Expression* x; + if (matches(curr, binary(Mul, any(&x), fval(2.0))) && + (x->is<LocalGet>() || x->is<GlobalGet>())) { + curr->op = Abstract::getBinary(type, Abstract::Add); + curr->right = ExpressionManipulator::copy(x, *getModule()); + return curr; + } + } + { // x + (-0.0) ==> x double value; if (fastMath && matches(curr, binary(Add, any(), fval(&value))) && diff --git a/test/passes/optimize-instructions_all-features.txt b/test/passes/optimize-instructions_all-features.txt index 80bd7a89d..15a38f267 100644 --- a/test/passes/optimize-instructions_all-features.txt +++ b/test/passes/optimize-instructions_all-features.txt @@ -19,6 +19,7 @@ (type $f32_=>_none (func (param f32))) (type $f32_f64_=>_none (func (param f32 f64))) (type $f64_=>_none (func (param f64))) + (type $f64_f32_=>_none (func (param f64 f32))) (type $f64_f64_f32_f32_=>_none (func (param f64 f64 f32 f32))) (type $none_=>_f64 (func (result f64))) (memory $0 0) @@ -4597,6 +4598,34 @@ ) ) ) + (func $optimize-float-mul-by-two (param $0 f64) (param $1 f32) + (drop + (f64.add + (local.get $0) + (local.get $0) + ) + ) + (drop + (f32.add + (local.get $1) + (local.get $1) + ) + ) + (drop + (f64.mul + (call $tee-with-unreachable-value) + (f64.const 2) + ) + ) + (drop + (f64.mul + (f64.neg + (local.get $0) + ) + (f64.const 2) + ) + ) + ) (func $duplicate-elimination (param $x i32) (param $y i32) (param $z i32) (param $w f64) (drop (f64.abs diff --git a/test/passes/optimize-instructions_all-features.wast b/test/passes/optimize-instructions_all-features.wast index c3a6aaf59..c9f071727 100644 --- a/test/passes/optimize-instructions_all-features.wast +++ b/test/passes/optimize-instructions_all-features.wast @@ -5069,6 +5069,25 @@ (i32.const -7) ;; skip )) ) + (func $optimize-float-mul-by-two (param $0 f64) (param $1 f32) + (drop (f64.mul + (local.get $0) + (f64.const 2) + )) + (drop (f32.mul + (local.get $1) + (f32.const 2) + )) + + (drop (f64.mul + (call $tee-with-unreachable-value) ;; side effect + (f64.const 2) + )) + (drop (f64.mul + (f64.neg (local.get $0)) ;; complex expression + (f64.const 2) + )) + ) (func $duplicate-elimination (param $x i32) (param $y i32) (param $z i32) (param $w f64) ;; unary (drop (f64.abs (f64.abs (local.get $w)))) |