summaryrefslogtreecommitdiff
path: root/src/passes/OptimizeInstructions.cpp
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2020-09-14 04:04:38 +0300
committerGitHub <noreply@github.com>2020-09-13 18:04:38 -0700
commitcd9bd149162ecdd9f2715367617771ea55b7cd7e (patch)
tree197641f370ab9560f2edd64700178344910b7b07 /src/passes/OptimizeInstructions.cpp
parent643facd9bf6af1792db473e73e16983df940106f (diff)
downloadbinaryen-cd9bd149162ecdd9f2715367617771ea55b7cd7e.tar.gz
binaryen-cd9bd149162ecdd9f2715367617771ea55b7cd7e.tar.bz2
binaryen-cd9bd149162ecdd9f2715367617771ea55b7cd7e.zip
Simplify subtracting zero from float expressions (#3125)
`x - 0.0` -> `x` `x + (-0.0)` -> `x` `x - (-0.0)` -> `x + 0.0` where `x` is `f32` or `f64`.
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r--src/passes/OptimizeInstructions.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index b4a5b9217..676e753ce 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -1299,6 +1299,27 @@ private:
}
}
}
+ if (type.isFloat()) {
+ auto value = right->value.getFloat();
+ if (value == 0.0) {
+ if (binary->op == Abstract::getBinary(type, Abstract::Sub)) {
+ if (std::signbit(value)) {
+ // x - (-0.0) ==> x + 0.0
+ binary->op = Abstract::getBinary(type, Abstract::Add);
+ right->value = right->value.neg();
+ return binary;
+ } else {
+ // x - 0.0 ==> x
+ return binary->left;
+ }
+ } else if (binary->op == Abstract::getBinary(type, Abstract::Add)) {
+ if (std::signbit(value)) {
+ // x + (-0.0) ==> x
+ return binary->left;
+ }
+ }
+ }
+ }
if (type.isInteger() || type.isFloat()) {
// note that this is correct even on floats with a NaN on the left,
// as a NaN would skip the computation and just return the NaN,