package org.graalvm.compiler.nodes.calc;
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
import org.graalvm.compiler.core.common.calc.Condition;
import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
import org.graalvm.compiler.core.common.type.AbstractPointerStamp;
import org.graalvm.compiler.core.common.type.IntegerStamp;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.graph.NodeClass;
import org.graalvm.compiler.graph.spi.Canonicalizable;
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.BinaryOpLogicNode;
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.StructuredGraph;
import org.graalvm.compiler.nodes.ValueNode;
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.ConstantReflectionProvider;
@NodeInfo(cycles = CYCLES_1)
public abstract class CompareNode extends BinaryOpLogicNode implements Canonicalizable.Binary<ValueNode> {
public static final NodeClass<CompareNode> TYPE = NodeClass.create(CompareNode.class);
protected final Condition condition;
protected final boolean unorderedIsTrue;
protected CompareNode(NodeClass<? extends CompareNode> c, Condition condition, boolean unorderedIsTrue, ValueNode x, ValueNode y) {
super(c, x, y);
this.condition = condition;
this.unorderedIsTrue = unorderedIsTrue;
}
public final Condition condition() {
return condition;
}
public final boolean unorderedIsTrue() {
return this.unorderedIsTrue;
}
private ValueNode optimizeConditional(Constant constant, ConditionalNode conditionalNode, ConstantReflectionProvider constantReflection, Condition cond) {
Constant trueConstant = conditionalNode.trueValue().asConstant();
Constant falseConstant = conditionalNode.falseValue().asConstant();
if (falseConstant != null && trueConstant != null && constantReflection != null) {
boolean trueResult = cond.foldCondition(trueConstant, constant, constantReflection, unorderedIsTrue());
boolean falseResult = cond.foldCondition(falseConstant, constant, constantReflection, unorderedIsTrue());
if (trueResult == falseResult) {
return LogicConstantNode.forBoolean(trueResult);
} else {
if (trueResult) {
assert falseResult == false;
return conditionalNode.condition();
} else {
assert falseResult == true;
return LogicNegationNode.create(conditionalNode.condition());
}
}
}
return this;
}
protected ValueNode optimizeNormalizeCmp(Constant constant, NormalizeCompareNode normalizeNode, boolean mirrored) {
throw new GraalError("NormalizeCompareNode connected to %s (%s %s %s)", this, constant, normalizeNode, mirrored);
}
@Override
public ValueNode canonical(CanonicalizerTool tool, ValueNode forX, ValueNode forY) {
ConstantReflectionProvider constantReflection = tool.getConstantReflection();
LogicNode constantCondition = tryConstantFold(condition(), forX, forY, constantReflection, unorderedIsTrue());
if (constantCondition != null) {
return constantCondition;
}
ValueNode result;
if (forX.isConstant()) {
if ((result = canonicalizeSymmetricConstant(tool, forX.asConstant(), forY, true)) != this) {
return result;
}
} else if (forY.isConstant()) {
if ((result = canonicalizeSymmetricConstant(tool, forY.asConstant(), forX, false)) != this) {
return result;
}
} else if (forX instanceof ConvertNode && forY instanceof ConvertNode) {
ConvertNode convertX = (ConvertNode) forX;
ConvertNode convertY = (ConvertNode) forY;
if (convertX.preservesOrder(condition()) && convertY.preservesOrder(condition()) && convertX.getValue().stamp().isCompatible(convertY.getValue().stamp())) {
boolean supported = true;
if (convertX.getValue().stamp() instanceof IntegerStamp) {
IntegerStamp intStamp = (IntegerStamp) convertX.getValue().stamp();
supported = tool.supportSubwordCompare(intStamp.getBits());
}
if (supported) {
boolean multiUsage = (convertX.asNode().getUsageCount() > 1 || convertY.asNode().getUsageCount() > 1);
if ((forX instanceof ZeroExtendNode || forX instanceof SignExtendNode) && multiUsage) {
return this;
}
return duplicateModified(convertX.getValue(), convertY.getValue());
}
}
}
return this;
}
public static LogicNode tryConstantFold(Condition condition, ValueNode forX, ValueNode forY, ConstantReflectionProvider constantReflection, boolean unorderedIsTrue) {
if (forX.isConstant() && forY.isConstant() && constantReflection != null) {
return LogicConstantNode.forBoolean(condition.foldCondition(forX.asConstant(), forY.asConstant(), constantReflection, unorderedIsTrue));
}
return null;
}
public boolean isIdentityComparison() {
return condition == Condition.EQ;
}
protected abstract LogicNode duplicateModified(ValueNode newX, ValueNode newY);
protected ValueNode canonicalizeSymmetricConstant(CanonicalizerTool tool, Constant constant, ValueNode nonConstant, boolean mirrored) {
if (nonConstant instanceof ConditionalNode) {
return optimizeConditional(constant, (ConditionalNode) nonConstant, tool.getConstantReflection(), mirrored ? condition().mirror() : condition());
} else if (nonConstant instanceof NormalizeCompareNode) {
return optimizeNormalizeCmp(constant, (NormalizeCompareNode) nonConstant, mirrored);
} else if (nonConstant instanceof ConvertNode) {
ConvertNode convert = (ConvertNode) nonConstant;
boolean multiUsage = (convert.asNode().getUsageCount() > 1 && convert.getValue().getUsageCount() == 1);
if ((convert instanceof ZeroExtendNode || convert instanceof SignExtendNode) && multiUsage) {
return this;
}
boolean supported = true;
if (convert.getValue().stamp() instanceof IntegerStamp) {
IntegerStamp intStamp = (IntegerStamp) convert.getValue().stamp();
supported = tool.supportSubwordCompare(intStamp.getBits());
}
if (supported) {
ConstantNode newConstant = canonicalConvertConstant(tool, convert, constant);
if (newConstant != null) {
if (mirrored) {
return duplicateModified(newConstant, convert.getValue());
} else {
return duplicateModified(convert.getValue(), newConstant);
}
}
}
}
return this;
}
private ConstantNode canonicalConvertConstant(CanonicalizerTool tool, ConvertNode convert, Constant constant) {
ConstantReflectionProvider constantReflection = tool.getConstantReflection();
if (convert.preservesOrder(condition(), constant, constantReflection)) {
Constant reverseConverted = convert.reverse(constant, constantReflection);
if (reverseConverted != null && convert.convert(reverseConverted, constantReflection).equals(constant)) {
if (GeneratePIC.getValue()) {
return null;
}
return ConstantNode.forConstant(convert.getValue().stamp(), reverseConverted, tool.getMetaAccess());
}
}
return null;
}
public static LogicNode createCompareNode(StructuredGraph graph, Condition condition, ValueNode x, ValueNode y, ConstantReflectionProvider constantReflection) {
LogicNode result = createCompareNode(condition, x, y, constantReflection);
return (result.graph() == null ? graph.unique(result) : result);
}
public static LogicNode createCompareNode(Condition condition, ValueNode x, ValueNode y, ConstantReflectionProvider constantReflection) {
assert x.getStackKind() == y.getStackKind();
assert condition.isCanonical() : "condition is not canonical: " + condition;
assert !x.getStackKind().isNumericFloat();
LogicNode comparison;
if (condition == Condition.EQ) {
if (x.stamp() instanceof AbstractObjectStamp) {
comparison = ObjectEqualsNode.create(x, y, constantReflection);
} else if (x.stamp() instanceof AbstractPointerStamp) {
comparison = PointerEqualsNode.create(x, y);
} else {
assert x.getStackKind().isNumericInteger();
comparison = IntegerEqualsNode.create(x, y, constantReflection);
}
} else if (condition == Condition.LT) {
assert x.getStackKind().isNumericInteger();
comparison = IntegerLessThanNode.create(x, y, constantReflection);
} else {
assert condition == Condition.BT;
assert x.getStackKind().isNumericInteger();
comparison = IntegerBelowNode.create(x, y, constantReflection);
}
return comparison;
}
}