# HG changeset patch # User Roland Schatz # Date 1364896126 -7200 # Node ID 4d75c3833c54221041d493c3d61421cbed850e90 # Parent c423a5fd8ac78268898ea41d4e6d0297dcbcde56 Canonicalize (a + b) - b and (a - b) + b. diff -r c423a5fd8ac7 -r 4d75c3833c54 graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerAddNode.java --- 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; } diff -r c423a5fd8ac7 -r 4d75c3833c54 graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerSubNode.java --- 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; }