Skip to content

Commit fc7f66a

Browse files
authored
fast-math: Fold fp * -1 to -fp (#3189)
1 parent 654cfca commit fc7f66a

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

src/passes/OptimizeInstructions.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,8 +1447,7 @@ struct OptimizeInstructions
14471447
}
14481448
{
14491449
double value;
1450-
if (fastMath &&
1451-
matches(curr, binary(Abstract::Sub, any(), fval(&value))) &&
1450+
if (matches(curr, binary(Abstract::Sub, any(), fval(&value))) &&
14521451
value == 0.0) {
14531452
// x - (-0.0) ==> x + 0.0
14541453
if (std::signbit(value)) {
@@ -1470,6 +1469,10 @@ struct OptimizeInstructions
14701469
return curr->left;
14711470
}
14721471
}
1472+
// x * -1.0 ==> -x
1473+
if (fastMath && matches(curr, binary(Abstract::Mul, any(), fval(-1.0)))) {
1474+
return builder.makeUnary(Abstract::getUnary(type, Abstract::Neg), left);
1475+
}
14731476
if (matches(curr, binary(Abstract::Mul, any(&left), constant(1))) ||
14741477
matches(curr, binary(Abstract::DivS, any(&left), constant(1))) ||
14751478
matches(curr, binary(Abstract::DivU, any(&left), constant(1)))) {

test/passes/O_fast-math.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(module
22
(type $none_=>_f32 (func (result f32)))
3+
(type $f32_=>_f32 (func (param f32) (result f32)))
4+
(type $f64_=>_f64 (func (param f64) (result f64)))
35
(export "div" (func $0))
46
(export "mul1" (func $1))
57
(export "mul2" (func $2))
@@ -9,6 +11,8 @@
911
(export "add4" (func $2))
1012
(export "sub1" (func $1))
1113
(export "sub2" (func $2))
14+
(export "mul_neg_one1" (func $9))
15+
(export "mul_neg_one2" (func $10))
1216
(func $0 (; has Stack IR ;) (result f32)
1317
(f32.const -nan:0x23017a)
1418
)
@@ -18,4 +22,14 @@
1822
(func $2 (; has Stack IR ;) (result f32)
1923
(f32.const -nan:0x74546d)
2024
)
25+
(func $9 (; has Stack IR ;) (param $0 f32) (result f32)
26+
(f32.neg
27+
(local.get $0)
28+
)
29+
)
30+
(func $10 (; has Stack IR ;) (param $0 f64) (result f64)
31+
(f64.neg
32+
(local.get $0)
33+
)
34+
)
2135
)

test/passes/O_fast-math.wast

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,16 @@
5454
(f32.const -0)
5555
)
5656
)
57+
(func "mul_neg_one1" (param $x f32) (result f32)
58+
(f32.mul
59+
(local.get $x)
60+
(f32.const -1)
61+
)
62+
)
63+
(func "mul_neg_one2" (param $x f64) (result f64)
64+
(f64.mul
65+
(local.get $x)
66+
(f64.const -1)
67+
)
68+
)
5769
)

test/passes/optimize-instructions_all-features.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,15 +3829,15 @@
38293829
)
38303830
)
38313831
(drop
3832-
(f32.sub
3832+
(f32.add
38333833
(local.get $fx)
3834-
(f32.const -0)
3834+
(f32.const 0)
38353835
)
38363836
)
38373837
(drop
3838-
(f64.sub
3838+
(f64.add
38393839
(local.get $fy)
3840-
(f64.const -0)
3840+
(f64.const 0)
38413841
)
38423842
)
38433843
(drop

0 commit comments

Comments
 (0)