ART: Fix the simplifier for NEGATE add/sub
Instruction simplifier for negate add/sub should not proceed
with floats because that might cause the incorrect behavior
with signed zero.
Change-Id: I4970694a2b265a3577cde34fee9cd3a437358c0f
diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc
index df6e550..0ac26de 100644
--- a/compiler/optimizing/instruction_simplifier.cc
+++ b/compiler/optimizing/instruction_simplifier.cc
@@ -132,6 +132,12 @@
// with
// ADD tmp, a, b
// NEG dst, tmp
+ // Note that we cannot optimize `(-a) + (-b)` to `-(a + b)` for floating-point.
+ // When `a` is `-0.0` and `b` is `0.0`, the former expression yields `0.0`,
+ // while the later yields `-0.0`.
+ if (!Primitive::IsIntegralType(binop->GetType())) {
+ return false;
+ }
binop->ReplaceInput(left_neg->GetInput(), 0);
binop->ReplaceInput(right_neg->GetInput(), 1);
left_neg->GetBlock()->RemoveInstruction(left_neg);
diff --git a/test/474-fp-sub-neg/expected.txt b/test/474-fp-sub-neg/expected.txt
index 1c15abb..1c7ded3 100644
--- a/test/474-fp-sub-neg/expected.txt
+++ b/test/474-fp-sub-neg/expected.txt
@@ -1,6 +1,13 @@
-0.0
-0.0
-0.0
-0.0
0.0
0.0
+0.0
+0.0
+-0.0
+-0.0
+0.0
+0.0
+0.0
+0.0
+d 0.0
diff --git a/test/474-fp-sub-neg/src/Main.java b/test/474-fp-sub-neg/src/Main.java
index c190e8e..796d56c 100644
--- a/test/474-fp-sub-neg/src/Main.java
+++ b/test/474-fp-sub-neg/src/Main.java
@@ -17,33 +17,58 @@
public class Main {
public static void floatTest() {
float f = 0;
+ float nf = -0;
float fc = 1f;
for (int i = 0; i < 2; i++) {
f -= fc;
f = -f;
+ nf -= fc;
+ nf = -nf;
}
System.out.println(f);
+ System.out.println(nf);
System.out.println(f + 0f);
System.out.println(f - (-0f));
+ System.out.println(-f - (-nf));
+ System.out.println(-f + (-nf));
}
public static void doubleTest() {
double d = 0;
+ double nd = -0;
double dc = 1f;
for (int i = 0; i < 2; i++) {
d -= dc;
d = -d;
+ nd -= dc;
+ nd = -nd;
}
System.out.println(d);
+ System.out.println(nd);
System.out.println(d + 0f);
System.out.println(d - (-0f));
+ System.out.println(-d - (-nd));
+ System.out.println(-d + (-nd));
+ }
+
+ public static void bug_1() {
+ int i4=18, i3=-48959;
+ float d;
+ float f=-0.0f;
+ float a=0.0f;
+
+ d = -f + (-a);
+ f += i4 * i3;
+
+ System.out.println("d " + d);
}
public static void main(String[] args) {
doubleTest();
floatTest();
+ bug_1();
}
}