changeset 8601:4d75c3833c54

Canonicalize (a + b) - b and (a - b) + b.
author Roland Schatz <roland.schatz@oracle.com>
date Tue, 02 Apr 2013 11:48:46 +0200
parents c423a5fd8ac7
children db2b8fbbf8fc
files graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerAddNode.java graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerSubNode.java
diffstat 2 files changed, 52 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerAddNode.java	Tue Apr 02 11:47:56 2013 +0200
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerAddNode.java	Tue Apr 02 11:48:46 2013 +0200
@@ -45,6 +45,19 @@
         if (x().isConstant() && !y().isConstant()) {
             return graph().unique(new IntegerAddNode(kind(), y(), x()));
         }
+        if (x() instanceof IntegerSubNode) {
+            IntegerSubNode sub = (IntegerSubNode) x();
+            if (sub.y() == y()) {
+                // (a - b) + b
+                return sub.x();
+            }
+        } else if (y() instanceof IntegerSubNode) {
+            IntegerSubNode sub = (IntegerSubNode) y();
+            if (sub.y() == x()) {
+                // b + (a - b)
+                return sub.x();
+            }
+        }
         if (x().isConstant()) {
             if (kind() == Kind.Int) {
                 return ConstantNode.forInt(x().asConstant().asInt() + y().asConstant().asInt(), graph());
@@ -73,6 +86,8 @@
         }
         if (x() instanceof NegateNode) {
             return IntegerArithmeticNode.sub(y(), ((NegateNode) x()).x());
+        } else if (y() instanceof NegateNode) {
+            return IntegerArithmeticNode.sub(x(), ((NegateNode) y()).x());
         }
         return this;
     }
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerSubNode.java	Tue Apr 02 11:47:56 2013 +0200
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerSubNode.java	Tue Apr 02 11:48:46 2013 +0200
@@ -45,6 +45,40 @@
         if (x() == y()) {
             return ConstantNode.forIntegerKind(kind(), 0, graph());
         }
+        if (x() instanceof IntegerAddNode) {
+            IntegerAddNode x = (IntegerAddNode) x();
+            if (x.y() == y()) {
+                // (a + b) - b
+                return x.x();
+            }
+            if (x.x() == y()) {
+                // (a + b) - a
+                return x.y();
+            }
+        } else if (x() instanceof IntegerSubNode) {
+            IntegerSubNode x = (IntegerSubNode) x();
+            if (x.x() == y()) {
+                // (a - b) - a
+                return graph().unique(new NegateNode(x.y()));
+            }
+        }
+        if (y() instanceof IntegerAddNode) {
+            IntegerAddNode y = (IntegerAddNode) y();
+            if (y.x() == x()) {
+                // a - (a + b)
+                return graph().unique(new NegateNode(y.y()));
+            }
+            if (y.y() == x()) {
+                // b - (a + b)
+                return graph().unique(new NegateNode(y.x()));
+            }
+        } else if (y() instanceof IntegerSubNode) {
+            IntegerSubNode y = (IntegerSubNode) y();
+            if (y.x() == x()) {
+                // a - (a - b)
+                return y.y();
+            }
+        }
         if (x().isConstant() && y().isConstant()) {
             if (kind() == Kind.Int) {
                 return ConstantNode.forInt(x().asConstant().asInt() - y().asConstant().asInt(), graph());
@@ -76,6 +110,9 @@
             }
             return BinaryNode.reassociate(this, ValueNode.isConstantPredicate());
         }
+        if (y() instanceof NegateNode) {
+            return IntegerArithmeticNode.add(x(), ((NegateNode) y()).x());
+        }
         return this;
     }