Skip to content

Commit

Permalink
[GR-52562] Add arithmetic optimizations around integer negation.
Browse files Browse the repository at this point in the history
PullRequest: graal/19141
  • Loading branch information
mur47x111 committed Oct 28, 2024
2 parents f9fb072 + 22c6b08 commit 5f95334
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
*/
package jdk.graal.compiler.core.test;

import org.junit.Test;

import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.graal.compiler.nodes.calc.NegateNode;
import jdk.graal.compiler.nodes.calc.RightShiftNode;
import jdk.graal.compiler.nodes.calc.UnsignedRightShiftNode;
import org.junit.Test;

public class NegateCanonicalizationTest extends GraalCompilerTest {

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

private void checkNodes(String methodName) {
public static int negateNegate(int x) {
int var0 = -x;
int var1 = -(0 ^ var0);
return var1;
}

public static int negateNotDecrement(int x) {
return -~(x - 1);
}

private void checkNodesOnlyUnsignedRightShift(String methodName) {
StructuredGraph graph = parseForCompile(getResolvedJavaMethod(methodName));
createCanonicalizerPhase().apply(graph, getProviders());
assertTrue(graph.getNodes().filter(NegateNode.class).count() == 0);
assertTrue(graph.getNodes().filter(RightShiftNode.class).count() == 0);
assertTrue(graph.getNodes().filter(UnsignedRightShiftNode.class).count() == 1);
}

private void checkNodesNoNegate(String methodName) {
StructuredGraph graph = parseForCompile(getResolvedJavaMethod(methodName));
createCanonicalizerPhase().apply(graph, getProviders());
assertTrue(graph.getNodes().filter(NegateNode.class).count() == 0);
}

@Test
public void testNegate() {
checkNodes("negateInt");
checkNodes("negateLong");
checkNodes("signExtractInt");
checkNodes("signExtractLong");
checkNodesOnlyUnsignedRightShift("negateInt");
checkNodesOnlyUnsignedRightShift("negateLong");
checkNodesOnlyUnsignedRightShift("signExtractInt");
checkNodesOnlyUnsignedRightShift("signExtractLong");
checkNodesNoNegate("negateNegate");
checkNodesNoNegate("negateNotDecrement");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected UnaryOp<Neg> getOp(ArithmeticOpTable table) {

@Override
public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
ValueNode synonym = findSynonym(forValue, getOp(forValue));
ValueNode synonym = findSynonym(forValue, NodeView.DEFAULT);
if (synonym != null) {
return synonym;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import jdk.graal.compiler.core.common.type.ArithmeticOpTable.UnaryOp.Not;
import jdk.graal.compiler.core.common.type.Stamp;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.nodes.NodeView;
import jdk.graal.compiler.nodes.ValueNode;
import jdk.graal.compiler.nodes.spi.ArithmeticLIRLowerable;
import jdk.graal.compiler.nodes.spi.CanonicalizerTool;
Expand Down Expand Up @@ -74,6 +75,10 @@ private static ValueNode canonicalize(NotNode node, ValueNode x) {
if (x instanceof NotNode) {
return ((NotNode) x).getValue();
}
if (x instanceof AddNode addNode && addNode.getY().isJavaConstant() && addNode.getY().asJavaConstant().asLong() == -1) {
// ~(x - 1) -> -x
return NegateNode.create(addNode.getX(), NodeView.DEFAULT);
}
if (node != null) {
return node;
}
Expand Down

0 comments on commit 5f95334

Please sign in to comment.