package org.graalvm.compiler.hotspot.nodes;
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
import org.graalvm.compiler.core.common.type.Stamp;
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.Canonicalizable;
import org.graalvm.compiler.graph.spi.CanonicalizerTool;
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
import org.graalvm.compiler.hotspot.HotSpotLIRGenerator;
import org.graalvm.compiler.hotspot.HotSpotMarkId;
import org.graalvm.compiler.hotspot.HotSpotReplacementsImpl;
import org.graalvm.compiler.nodeinfo.NodeInfo;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.calc.FloatingNode;
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
import org.graalvm.compiler.nodes.spi.LIRLowerable;
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.Value;
@NodeInfo(cycles = CYCLES_1, size = SIZE_1)
public class GraalHotSpotVMConfigNode extends FloatingNode implements LIRLowerable, Canonicalizable {
public static final NodeClass<GraalHotSpotVMConfigNode> TYPE = NodeClass.create(GraalHotSpotVMConfigNode.class);
private final GraalHotSpotVMConfig config;
protected final HotSpotMarkId markId;
public GraalHotSpotVMConfigNode(@InjectedNodeParameter Stamp stamp, @InjectedNodeParameter GraalHotSpotVMConfig config, HotSpotMarkId markId) {
super(TYPE, stamp);
this.config = config;
this.markId = markId;
}
public GraalHotSpotVMConfigNode(GraalHotSpotVMConfig config, HotSpotMarkId markId, JavaKind kind) {
super(TYPE, StampFactory.forKind(kind));
this.config = config;
this.markId = markId;
}
@Override
public void generate(NodeLIRBuilderTool generator) {
Value res = ((HotSpotLIRGenerator) generator.getLIRGeneratorTool()).emitLoadConfigValue(markId, generator.getLIRGeneratorTool().getLIRKind(stamp));
generator.setResult(this, res);
}
@NodeIntrinsic
private static native long loadLongConfigValue(@ConstantNodeParameter HotSpotMarkId markId);
@NodeIntrinsic
private static native int loadIntConfigValue(@ConstantNodeParameter HotSpotMarkId markId);
public static long cardTableAddress() {
return loadLongConfigValue(HotSpotMarkId.CARD_TABLE_ADDRESS);
}
public static long crcTableAddress() {
return loadLongConfigValue(HotSpotMarkId.CRC_TABLE_ADDRESS);
}
public static int logOfHeapRegionGrainBytes() {
return loadIntConfigValue(HotSpotMarkId.LOG_OF_HEAP_REGION_GRAIN_BYTES);
}
@SuppressWarnings("unused")
public static boolean intrinsify(GraphBuilderContext b, ResolvedJavaMethod method, @InjectedNodeParameter Stamp returnStamp, @InjectedNodeParameter GraalHotSpotVMConfig config,
HotSpotMarkId mark) {
HotSpotReplacementsImpl replacements = (HotSpotReplacementsImpl) b.getReplacements();
if (replacements.isEncodingSnippets()) {
return false;
}
b.addPush(returnStamp.getStackKind(), new GraalHotSpotVMConfigNode(returnStamp, config, mark));
return true;
}
@Override
public Node canonical(CanonicalizerTool tool) {
Boolean generatePIC = GeneratePIC.getValue(tool.getOptions());
if (markId == null) {
return ConstantNode.forBoolean(!generatePIC);
} else if (!generatePIC) {
if (markId == HotSpotMarkId.CARD_TABLE_ADDRESS) {
return ConstantNode.forLong(config.cardtableStartAddress);
} else if (markId == HotSpotMarkId.CRC_TABLE_ADDRESS) {
return ConstantNode.forLong(config.crcTableAddress);
} else if (markId == HotSpotMarkId.LOG_OF_HEAP_REGION_GRAIN_BYTES) {
return ConstantNode.forInt(config.logOfHRGrainBytes);
} else {
throw GraalError.shouldNotReachHere(markId.toString());
}
}
return this;
}
}