Skip to content

Commit 5f95334

Browse files
committed
[GR-52562] Add arithmetic optimizations around integer negation.
PullRequest: graal/19141
2 parents f9fb072 + 22c6b08 commit 5f95334

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/NegateCanonicalizationTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
*/
2626
package jdk.graal.compiler.core.test;
2727

28+
import org.junit.Test;
29+
2830
import jdk.graal.compiler.nodes.StructuredGraph;
2931
import jdk.graal.compiler.nodes.calc.NegateNode;
3032
import jdk.graal.compiler.nodes.calc.RightShiftNode;
3133
import jdk.graal.compiler.nodes.calc.UnsignedRightShiftNode;
32-
import org.junit.Test;
3334

3435
public class NegateCanonicalizationTest extends GraalCompilerTest {
3536

@@ -49,19 +50,37 @@ public static long signExtractLong(long x) {
4950
return (x >> 63) >>> 63;
5051
}
5152

52-
private void checkNodes(String methodName) {
53+
public static int negateNegate(int x) {
54+
int var0 = -x;
55+
int var1 = -(0 ^ var0);
56+
return var1;
57+
}
58+
59+
public static int negateNotDecrement(int x) {
60+
return -~(x - 1);
61+
}
62+
63+
private void checkNodesOnlyUnsignedRightShift(String methodName) {
5364
StructuredGraph graph = parseForCompile(getResolvedJavaMethod(methodName));
5465
createCanonicalizerPhase().apply(graph, getProviders());
5566
assertTrue(graph.getNodes().filter(NegateNode.class).count() == 0);
5667
assertTrue(graph.getNodes().filter(RightShiftNode.class).count() == 0);
5768
assertTrue(graph.getNodes().filter(UnsignedRightShiftNode.class).count() == 1);
5869
}
5970

71+
private void checkNodesNoNegate(String methodName) {
72+
StructuredGraph graph = parseForCompile(getResolvedJavaMethod(methodName));
73+
createCanonicalizerPhase().apply(graph, getProviders());
74+
assertTrue(graph.getNodes().filter(NegateNode.class).count() == 0);
75+
}
76+
6077
@Test
6178
public void testNegate() {
62-
checkNodes("negateInt");
63-
checkNodes("negateLong");
64-
checkNodes("signExtractInt");
65-
checkNodes("signExtractLong");
79+
checkNodesOnlyUnsignedRightShift("negateInt");
80+
checkNodesOnlyUnsignedRightShift("negateLong");
81+
checkNodesOnlyUnsignedRightShift("signExtractInt");
82+
checkNodesOnlyUnsignedRightShift("signExtractLong");
83+
checkNodesNoNegate("negateNegate");
84+
checkNodesNoNegate("negateNotDecrement");
6685
}
6786
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/calc/NegateNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected UnaryOp<Neg> getOp(ArithmeticOpTable table) {
7373

7474
@Override
7575
public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
76-
ValueNode synonym = findSynonym(forValue, getOp(forValue));
76+
ValueNode synonym = findSynonym(forValue, NodeView.DEFAULT);
7777
if (synonym != null) {
7878
return synonym;
7979
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/calc/NotNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import jdk.graal.compiler.core.common.type.ArithmeticOpTable.UnaryOp.Not;
3333
import jdk.graal.compiler.core.common.type.Stamp;
3434
import jdk.graal.compiler.graph.NodeClass;
35+
import jdk.graal.compiler.nodes.NodeView;
3536
import jdk.graal.compiler.nodes.ValueNode;
3637
import jdk.graal.compiler.nodes.spi.ArithmeticLIRLowerable;
3738
import jdk.graal.compiler.nodes.spi.CanonicalizerTool;
@@ -74,6 +75,10 @@ private static ValueNode canonicalize(NotNode node, ValueNode x) {
7475
if (x instanceof NotNode) {
7576
return ((NotNode) x).getValue();
7677
}
78+
if (x instanceof AddNode addNode && addNode.getY().isJavaConstant() && addNode.getY().asJavaConstant().asLong() == -1) {
79+
// ~(x - 1) -> -x
80+
return NegateNode.create(addNode.getX(), NodeView.DEFAULT);
81+
}
7782
if (node != null) {
7883
return node;
7984
}

0 commit comments

Comments
 (0)