changeset 19746:17229e8fad57

Refactor integer add exact node.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Mon, 09 Mar 2015 23:04:03 +0100
parents 2bad5984e4fe
children 0493ae8a552d
files graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/arithmetic/IntegerAddExactNode.java
diffstat 1 files changed, 27 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/arithmetic/IntegerAddExactNode.java	Mon Mar 09 21:07:38 2015 +0100
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/arithmetic/IntegerAddExactNode.java	Mon Mar 09 23:04:03 2015 +0100
@@ -53,35 +53,49 @@
 
     @Override
     public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
+        ValueNode result = findSynonym(forX, forY);
+        if (result == null) {
+            return this;
+        } else {
+            return result;
+        }
+    }
+
+    private static ValueNode findSynonym(ValueNode forX, ValueNode forY) {
         if (forX.isConstant() && !forY.isConstant()) {
             return new IntegerAddExactNode(forY, forX);
         }
         if (forX.isConstant()) {
-            return canonicalXconstant(forX, forY);
+            ConstantNode constantNode = canonicalXconstant(forX, forY);
+            if (constantNode != null) {
+                return constantNode;
+            }
         } else if (forY.isConstant()) {
             long c = forY.asJavaConstant().asLong();
             if (c == 0) {
                 return forX;
             }
         }
-        return this;
+        return null;
     }
 
-    private ValueNode canonicalXconstant(ValueNode forX, ValueNode forY) {
+    private static ConstantNode canonicalXconstant(ValueNode forX, ValueNode forY) {
         JavaConstant xConst = forX.asJavaConstant();
         JavaConstant yConst = forY.asJavaConstant();
-        assert xConst.getKind() == yConst.getKind();
-        try {
-            if (xConst.getKind() == Kind.Int) {
-                return ConstantNode.forInt(ExactMath.addExact(xConst.asInt(), yConst.asInt()));
-            } else {
-                assert xConst.getKind() == Kind.Long;
-                return ConstantNode.forLong(ExactMath.addExact(xConst.asLong(), yConst.asLong()));
+        if (xConst != null && yConst != null) {
+            assert xConst.getKind() == yConst.getKind();
+            try {
+                if (xConst.getKind() == Kind.Int) {
+                    return ConstantNode.forInt(ExactMath.addExact(xConst.asInt(), yConst.asInt()));
+                } else {
+                    assert xConst.getKind() == Kind.Long;
+                    return ConstantNode.forLong(ExactMath.addExact(xConst.asLong(), yConst.asLong()));
+                }
+            } catch (ArithmeticException ex) {
+                // The operation will result in an overflow exception, so do not canonicalize.
             }
-        } catch (ArithmeticException ex) {
-            // The operation will result in an overflow exception, so do not canonicalize.
         }
-        return this;
+        return null;
     }
 
     @Override