summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/OptimizeInstructions.cpp13
-rw-r--r--test/passes/optimize-instructions_all-features.txt29
-rw-r--r--test/passes/optimize-instructions_all-features.wast19
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))))