package org.graalvm.compiler.replacements.aarch64;
import org.graalvm.compiler.api.replacements.Snippet;
import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
import org.graalvm.compiler.core.common.type.IntegerStamp;
import org.graalvm.compiler.debug.DebugHandlersFactory;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.Node.NodeIntrinsic;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.DeoptimizeNode;
import org.graalvm.compiler.nodes.NodeView;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.calc.IntegerDivRemNode;
import org.graalvm.compiler.nodes.calc.SignedDivNode;
import org.graalvm.compiler.nodes.calc.SignedRemNode;
import org.graalvm.compiler.nodes.calc.UnsignedDivNode;
import org.graalvm.compiler.nodes.calc.UnsignedRemNode;
import org.graalvm.compiler.nodes.extended.BranchProbabilityNode;
import org.graalvm.compiler.nodes.spi.LoweringTool;
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
import org.graalvm.compiler.options.OptionValues;
import org.graalvm.compiler.phases.util.Providers;
import org.graalvm.compiler.replacements.SnippetTemplate;
import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
import org.graalvm.compiler.replacements.Snippets;
import jdk.vm.ci.code.TargetDescription;
import jdk.vm.ci.meta.DeoptimizationAction;
import jdk.vm.ci.meta.DeoptimizationReason;
import jdk.vm.ci.meta.JavaKind;
public class AArch64IntegerArithmeticSnippets extends AbstractTemplates implements Snippets {
private final SnippetTemplate.SnippetInfo idiv;
private final SnippetTemplate.SnippetInfo ldiv;
private final SnippetTemplate.SnippetInfo irem;
private final SnippetTemplate.SnippetInfo lrem;
private final SnippetTemplate.SnippetInfo uidiv;
private final SnippetTemplate.SnippetInfo uldiv;
private final SnippetTemplate.SnippetInfo uirem;
private final SnippetTemplate.SnippetInfo ulrem;
public AArch64IntegerArithmeticSnippets(OptionValues options, Iterable<DebugHandlersFactory> factories, Providers providers, SnippetReflectionProvider snippetReflection,
TargetDescription target) {
super(options, factories, providers, snippetReflection, target);
idiv = snippet(AArch64IntegerArithmeticSnippets.class, "idivSnippet");
ldiv = snippet(AArch64IntegerArithmeticSnippets.class, "ldivSnippet");
irem = snippet(AArch64IntegerArithmeticSnippets.class, "iremSnippet");
lrem = snippet(AArch64IntegerArithmeticSnippets.class, "lremSnippet");
uidiv = snippet(AArch64IntegerArithmeticSnippets.class, "uidivSnippet");
uldiv = snippet(AArch64IntegerArithmeticSnippets.class, "uldivSnippet");
uirem = snippet(AArch64IntegerArithmeticSnippets.class, "uiremSnippet");
ulrem = snippet(AArch64IntegerArithmeticSnippets.class, "ulremSnippet");
}
public void lower(IntegerDivRemNode node, LoweringTool tool) {
JavaKind kind = node.stamp(NodeView.DEFAULT).getStackKind();
assert kind == JavaKind.Int || kind == JavaKind.Long;
SnippetTemplate.SnippetInfo snippet;
if (node instanceof SafeNode) {
return;
} else if (node instanceof SignedDivNode) {
snippet = kind == JavaKind.Int ? idiv : ldiv;
} else if (node instanceof SignedRemNode) {
snippet = kind == JavaKind.Int ? irem : lrem;
} else if (node instanceof UnsignedDivNode) {
snippet = kind == JavaKind.Int ? uidiv : uldiv;
} else if (node instanceof UnsignedRemNode) {
snippet = kind == JavaKind.Int ? uirem : ulrem;
} else {
throw GraalError.shouldNotReachHere();
}
StructuredGraph graph = node.graph();
Arguments args = new Arguments(snippet, graph.getGuardsStage(), tool.getLoweringStage());
args.add("x", node.getX());
args.add("y", node.getY());
IntegerStamp yStamp = (IntegerStamp) node.getY().stamp(NodeView.DEFAULT);
args.addConst("needsZeroCheck", node.getZeroCheck() == null && yStamp.contains(0));
template(node, args).instantiate(providers.getMetaAccess(), node, SnippetTemplate.DEFAULT_REPLACER, args);
}
@Snippet
public static int idivSnippet(int x, int y, @ConstantParameter boolean needsZeroCheck) {
if (needsZeroCheck) {
checkForZero(y);
}
return safeDiv(x, y);
}
@Snippet
public static long ldivSnippet(long x, long y, @ConstantParameter boolean needsZeroCheck) {
if (needsZeroCheck) {
checkForZero(y);
}
return safeDiv(x, y);
}
@Snippet
public static int iremSnippet(int x, int y, @ConstantParameter boolean needsZeroCheck) {
if (needsZeroCheck) {
checkForZero(y);
}
return safeRem(x, y);
}
@Snippet
public static long lremSnippet(long x, long y, @ConstantParameter boolean needsZeroCheck) {
if (needsZeroCheck) {
checkForZero(y);
}
return safeRem(x, y);
}
@Snippet
public static int uidivSnippet(int x, int y, @ConstantParameter boolean needsZeroCheck) {
if (needsZeroCheck) {
checkForZero(y);
}
return safeUDiv(x, y);
}
@Snippet
public static long uldivSnippet(long x, long y, @ConstantParameter boolean needsZeroCheck) {
if (needsZeroCheck) {
checkForZero(y);
}
return safeUDiv(x, y);
}
@Snippet
public static int uiremSnippet(int x, int y, @ConstantParameter boolean needsZeroCheck) {
if (needsZeroCheck) {
checkForZero(y);
}
return safeURem(x, y);
}
@Snippet
public static long ulremSnippet(long x, long y, @ConstantParameter boolean needsZeroCheck) {
if (needsZeroCheck) {
checkForZero(y);
}
return safeURem(x, y);
}
private static void checkForZero(int y) {
if (BranchProbabilityNode.probability(BranchProbabilityNode.DEOPT_PROBABILITY, y == 0)) {
DeoptimizeNode.deopt(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.ArithmeticException);
}
}
private static void checkForZero(long y) {
if (BranchProbabilityNode.probability(BranchProbabilityNode.DEOPT_PROBABILITY, y == 0)) {
DeoptimizeNode.deopt(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.ArithmeticException);
}
}
@NodeIntrinsic(SafeSignedDivNode.class)
private static native int safeDiv(int x, int y);
@NodeIntrinsic(SafeSignedDivNode.class)
private static native long safeDiv(long x, long y);
@NodeIntrinsic(SafeSignedRemNode.class)
private static native int safeRem(int x, int y);
@NodeIntrinsic(SafeSignedRemNode.class)
private static native long safeRem(long x, long y);
@NodeIntrinsic(SafeUnsignedDivNode.class)
private static native int safeUDiv(int x, int y);
@NodeIntrinsic(SafeUnsignedDivNode.class)
private static native long safeUDiv(long x, long y);
@NodeIntrinsic(SafeUnsignedRemNode.class)
private static native int safeURem(int x, int y);
@NodeIntrinsic(SafeUnsignedRemNode.class)
private static native long safeURem(long x, long y);
private interface SafeNode {
}
@NodeInfo
static class SafeSignedDivNode extends SignedDivNode implements SafeNode {
public static final NodeClass<SafeSignedDivNode> TYPE = NodeClass.create(SafeSignedDivNode.class);
protected SafeSignedDivNode(ValueNode x, ValueNode y) {
super(TYPE, x, y, null);
}
@Override
public void generate(NodeLIRBuilderTool gen) {
gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitDiv(gen.operand(getX()), gen.operand(getY()), null));
}
}
@NodeInfo
static class SafeSignedRemNode extends SignedRemNode implements SafeNode {
public static final NodeClass<SafeSignedRemNode> TYPE = NodeClass.create(SafeSignedRemNode.class);
protected SafeSignedRemNode(ValueNode x, ValueNode y) {
super(TYPE, x, y, null);
}
@Override
public void generate(NodeLIRBuilderTool gen) {
gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitRem(gen.operand(getX()), gen.operand(getY()), null));
}
}
@NodeInfo
static class SafeUnsignedDivNode extends UnsignedDivNode implements SafeNode {
public static final NodeClass<SafeUnsignedDivNode> TYPE = NodeClass.create(SafeUnsignedDivNode.class);
protected SafeUnsignedDivNode(ValueNode x, ValueNode y) {
super(TYPE, x, y, null);
}
@Override
public void generate(NodeLIRBuilderTool gen) {
gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitUDiv(gen.operand(getX()), gen.operand(getY()), null));
}
}
@NodeInfo
static class SafeUnsignedRemNode extends UnsignedRemNode implements SafeNode {
public static final NodeClass<SafeUnsignedRemNode> TYPE = NodeClass.create(SafeUnsignedRemNode.class);
protected SafeUnsignedRemNode(ValueNode x, ValueNode y) {
super(TYPE, x, y, null);
}
@Override
public void generate(NodeLIRBuilderTool gen) {
gen.setResult(this, gen.getLIRGeneratorTool().getArithmetic().emitURem(gen.operand(getX()), gen.operand(getY()), null));
}
}
}