# HG changeset patch # User Lukas Stadler # Date 1336552923 -7200 # Node ID 88cd108e01644cc8142fa585e1fba11dbdf6b7d6 # Parent 8b5ef24da264e68da6a09cc9226eff5d60228f61 canonicalize more integer operations: (a+1)+2 to a+3 and (a*2)*2 to a*4 diff -r 8b5ef24da264 -r 88cd108e0164 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 Fri May 04 16:54:27 2012 +0200 +++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerAddNode.java Wed May 09 10:42:03 2012 +0200 @@ -60,6 +60,20 @@ return x(); } } + // canonicalize expressions like "(a + 1) + 2" + if (x() instanceof IntegerAddNode) { + IntegerAddNode other = (IntegerAddNode) x(); + if (other.y().isConstant()) { + ConstantNode sum; + if (kind() == CiKind.Int) { + sum = ConstantNode.forInt(y().asConstant().asInt() + other.y().asConstant().asInt(), graph()); + } else { + assert kind() == CiKind.Long; + sum = ConstantNode.forLong(y().asConstant().asLong() + other.y().asConstant().asLong(), graph()); + } + return graph().unique(new IntegerAddNode(kind(), other.x(), sum)); + } + } } return this; } diff -r 8b5ef24da264 -r 88cd108e0164 graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerMulNode.java --- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerMulNode.java Fri May 04 16:54:27 2012 +0200 +++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/calc/IntegerMulNode.java Wed May 09 10:42:03 2012 +0200 @@ -57,6 +57,20 @@ if (c > 0 && CiUtil.isPowerOf2(c)) { return graph().unique(new LeftShiftNode(kind(), x(), ConstantNode.forInt(CiUtil.log2(c), graph()))); } + // canonicalize expressions like "(a * 1) * 2" + if (x() instanceof IntegerMulNode) { + IntegerMulNode other = (IntegerMulNode) x(); + if (other.y().isConstant()) { + ConstantNode sum; + if (kind() == CiKind.Int) { + sum = ConstantNode.forInt(y().asConstant().asInt() * other.y().asConstant().asInt(), graph()); + } else { + assert kind() == CiKind.Long; + sum = ConstantNode.forLong(y().asConstant().asLong() * other.y().asConstant().asLong(), graph()); + } + return graph().unique(new IntegerMulNode(kind(), other.x(), sum)); + } + } } return this; }