package org.graalvm.compiler.nodes.calc;
import static org.graalvm.compiler.core.common.calc.CanonicalCondition.LT;
import org.graalvm.compiler.core.common.NumUtil;
import org.graalvm.compiler.core.common.calc.CanonicalCondition;
import org.graalvm.compiler.core.common.type.FloatStamp;
import org.graalvm.compiler.core.common.type.IntegerStamp;
import org.graalvm.compiler.core.common.type.StampFactory;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.LogicConstantNode;
import org.graalvm.compiler.nodes.LogicNegationNode;
import org.graalvm.compiler.nodes.LogicNode;
import org.graalvm.compiler.nodes.NodeView;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.options.OptionValues;
import jdk.vm.ci.code.CodeUtil;
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.ConstantReflectionProvider;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.PrimitiveConstant;
@NodeInfo(shortName = "<")
public final class IntegerLessThanNode extends IntegerLowerThanNode {
public static final NodeClass<IntegerLessThanNode> TYPE = NodeClass.create(IntegerLessThanNode.class);
private static final LessThanOp OP = new LessThanOp();
public IntegerLessThanNode(ValueNode x, ValueNode y) {
super(TYPE, x, y, OP);
assert !x.getStackKind().isNumericFloat() && x.getStackKind() != JavaKind.Object;
assert !y.getStackKind().isNumericFloat() && y.getStackKind() != JavaKind.Object;
}
public static LogicNode create(ValueNode x, ValueNode y, NodeView view) {
return OP.create(x, y, view);
}
public static LogicNode create(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
ValueNode x, ValueNode y, NodeView view) {
LogicNode value = OP.canonical(constantReflection, metaAccess, options, smallestCompareWidth, OP.getCondition(), false, x, y, view);
if (value != null) {
return value;
}
return create(x, y, view);
}
@Override
public Node canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
NodeView view = NodeView.from(tool);
ValueNode value = OP.canonical(tool.getConstantReflection(), tool.getMetaAccess(), tool.getOptions(), tool.smallestCompareWidth(), OP.getCondition(), false, forX, forY, view);
if (value != null) {
return value;
}
return this;
}
public static boolean subtractMayUnderflow(long x, long y, long minValue) {
long r = x - y;
return (((x ^ y) & (x ^ r)) < 0) || r <= minValue;
}
public static boolean subtractMayOverflow(long x, long y, long maxValue) {
long r = x - y;
return (((x ^ y) & (x ^ r)) < 0) || r > maxValue;
}
public static class LessThanOp extends LowerOp {
@Override
protected CompareNode duplicateModified(ValueNode newX, ValueNode newY, boolean unorderedIsTrue, NodeView view) {
if (newX.stamp(view) instanceof FloatStamp && newY.stamp(view) instanceof FloatStamp) {
return new FloatLessThanNode(newX, newY, unorderedIsTrue);
} else if (newX.stamp(view) instanceof IntegerStamp && newY.stamp(view) instanceof IntegerStamp) {
return new IntegerLessThanNode(newX, newY);
}
throw GraalError.shouldNotReachHere();
}
@Override
protected LogicNode optimizeNormalizeCompare(ConstantReflectionProvider constantReflection, MetaAccessProvider metaAccess, OptionValues options, Integer smallestCompareWidth,
Constant constant, NormalizeCompareNode normalizeNode, boolean mirrored, NodeView view) {
PrimitiveConstant primitive = (PrimitiveConstant) constant;
ValueNode a = mirrored ? normalizeNode.getY() : normalizeNode.getX();
ValueNode b = mirrored ? normalizeNode.getX() : normalizeNode.getY();
long cst = mirrored ? -primitive.asLong() : primitive.asLong();
if (cst == 0) {
if (normalizeNode.getX().getStackKind() == JavaKind.Double || normalizeNode.getX().getStackKind() == JavaKind.Float) {
return FloatLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, a, b, mirrored ^ normalizeNode.isUnorderedLess, view);
} else {
return IntegerLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, a, b, view);
}
} else if (cst == 1) {
LogicNode compare;
if (normalizeNode.getX().getStackKind() == JavaKind.Double || normalizeNode.getX().getStackKind() == JavaKind.Float) {
compare = FloatLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, b, a, mirrored == normalizeNode.isUnorderedLess, view);
} else {
compare = IntegerLessThanNode.create(constantReflection, metaAccess, options, smallestCompareWidth, b, a, view);
}
return LogicNegationNode.create(compare);
} else if (cst <= -1) {
return LogicConstantNode.contradiction();
} else {
assert cst >= 2;
return LogicConstantNode.tautology();
}
}
@Override
protected LogicNode findSynonym(ValueNode forX, ValueNode forY, NodeView view) {
LogicNode result = super.findSynonym(forX, forY, view);
if (result != null) {
return result;
}
if (forX.stamp(view) instanceof IntegerStamp && forY.stamp(view) instanceof IntegerStamp) {
if (IntegerStamp.sameSign((IntegerStamp) forX.stamp(view), (IntegerStamp) forY.stamp(view))) {
return new IntegerBelowNode(forX, forY);
}
}
if (forY.isConstant()) {
if (forX instanceof SubNode) {
SubNode sub = (SubNode) forX;
ValueNode xx = null;
ValueNode yy = null;
boolean negate = false;
if (forY.asConstant().isDefaultForKind()) {
xx = sub.getX();
yy = sub.getY();
} else if (forY.isJavaConstant() && forY.asJavaConstant().asLong() == 1) {
xx = sub.getY();
yy = sub.getX();
negate = true;
}
if (xx != null) {
assert yy != null;
IntegerStamp xStamp = (IntegerStamp) sub.getX().stamp(view);
IntegerStamp yStamp = (IntegerStamp) sub.getY().stamp(view);
long minValue = CodeUtil.minValue(xStamp.getBits());
long maxValue = CodeUtil.maxValue(xStamp.getBits());
if (!subtractMayUnderflow(xStamp.lowerBound(), yStamp.upperBound(), minValue) && !subtractMayOverflow(xStamp.upperBound(), yStamp.lowerBound(), maxValue)) {
LogicNode logic = new IntegerLessThanNode(xx, yy);
if (negate) {
logic = LogicNegationNode.create(logic);
}
return logic;
}
}
} else if (forX instanceof AddNode) {
AddNode addNode = (AddNode) forX;
if (addNode.getY().isJavaConstant()) {
IntegerStamp xStamp = (IntegerStamp) addNode.getX().stamp(view);
if (!IntegerStamp.addCanOverflow(xStamp, (IntegerStamp) addNode.getY().stamp(view))) {
long minValue = CodeUtil.minValue(xStamp.getBits());
long maxValue = CodeUtil.maxValue(xStamp.getBits());
long yConstant = forY.asJavaConstant().asLong();
long xConstant = addNode.getY().asJavaConstant().asLong();
if (!subtractMayUnderflow(yConstant, xConstant, minValue) && !subtractMayOverflow(yConstant, xConstant, maxValue)) {
long newConstant = yConstant - xConstant;
return IntegerLessThanNode.create(addNode.getX(), ConstantNode.forIntegerStamp(xStamp, newConstant), view);
}
}
}
}
}
if (forX.stamp(view) instanceof IntegerStamp) {
assert forY.stamp(view) instanceof IntegerStamp;
int bits = ((IntegerStamp) forX.stamp(view)).getBits();
assert ((IntegerStamp) forY.stamp(view)).getBits() == bits;
LogicNode logic = canonicalizeRangeFlip(forX, forY, bits, true, view);
if (logic != null) {
return logic;
}
}
return null;
}
@Override
protected CanonicalCondition getCondition() {
return LT;
}
@Override
protected IntegerLowerThanNode createNode(ValueNode x, ValueNode y) {
return new IntegerLessThanNode(x, y);
}
@Override
protected long upperBound(IntegerStamp stamp) {
return stamp.upperBound();
}
@Override
protected long lowerBound(IntegerStamp stamp) {
return stamp.lowerBound();
}
@Override
protected int compare(long a, long b) {
return Long.compare(a, b);
}
@Override
protected long min(long a, long b) {
return Math.min(a, b);
}
@Override
protected long max(long a, long b) {
return Math.max(a, b);
}
@Override
protected long cast(long a, int bits) {
return CodeUtil.signExtend(a, bits);
}
@Override
protected long minValue(int bits) {
return NumUtil.minValue(bits);
}
@Override
protected long maxValue(int bits) {
return NumUtil.maxValue(bits);
}
@Override
protected IntegerStamp forInteger(int bits, long min, long max) {
return StampFactory.forInteger(bits, cast(min, bits), cast(max, bits));
}
}
}