changeset 16783:c914e5837b4b

More canonicalizations in FloatAdd/SubNode.
author Roland Schatz <roland.schatz@oracle.com>
date Tue, 12 Aug 2014 14:13:50 +0200
parents 9f5e33cf8d52
children 88df5d7b1001
files graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/FloatAddNode.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/FloatSubNode.java
diffstat 2 files changed, 35 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/FloatAddNode.java	Tue Aug 12 14:04:01 2014 +0200
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/FloatAddNode.java	Tue Aug 12 14:13:50 2014 +0200
@@ -76,6 +76,16 @@
                     throw GraalGraphInternalError.shouldNotReachHere();
             }
         }
+        /*
+         * JVM spec, Chapter 6, dsub/fsub bytecode: For double subtraction, it is always the case
+         * that a-b produces the same result as a+(-b).
+         */
+        if (forX instanceof NegateNode) {
+            return new FloatSubNode(forY, ((NegateNode) forX).getValue(), isStrictFP());
+        }
+        if (forY instanceof NegateNode) {
+            return new FloatSubNode(forX, ((NegateNode) forY).getValue(), isStrictFP());
+        }
         return this;
     }
 
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/FloatSubNode.java	Tue Aug 12 14:04:01 2014 +0200
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/FloatSubNode.java	Tue Aug 12 14:13:50 2014 +0200
@@ -56,6 +56,24 @@
         if (forX.isConstant() && forY.isConstant()) {
             return ConstantNode.forPrimitive(evalConst(forX.asConstant(), forY.asConstant()));
         }
+        // Constant -0.0 is an additive identity, so (-0.0) - x == (-0.0) + (-x) == -x.
+        if (forX.isConstant()) {
+            Constant x = forX.asConstant();
+            switch (x.getKind()) {
+                case Float:
+                    if (Float.compare(x.asFloat(), -0.0f) == 0) {
+                        return new NegateNode(forY);
+                    }
+                    break;
+                case Double:
+                    if (Double.compare(x.asDouble(), -0.0) == 0) {
+                        return new NegateNode(forY);
+                    }
+                    break;
+                default:
+                    throw GraalGraphInternalError.shouldNotReachHere();
+            }
+        }
         // Constant -0.0 can't be eliminated since it can affect the sign of the result.
         // Constant 0.0 is a subtractive identity.
         if (forY.isConstant()) {
@@ -77,6 +95,13 @@
                     throw GraalGraphInternalError.shouldNotReachHere();
             }
         }
+        /*
+         * JVM spec, Chapter 6, dsub/fsub bytecode: For double subtraction, it is always the case
+         * that a-b produces the same result as a+(-b).
+         */
+        if (forY instanceof NegateNode) {
+            return new FloatAddNode(forX, ((NegateNode) forY).getValue(), isStrictFP());
+        }
         return this;
     }