Search in sources :

Example 81 with JavaKind

use of jdk.vm.ci.meta.JavaKind in project graal by oracle.

the class CollectingObjectReferenceVisitor method verifyVirtualObject.

private void verifyVirtualObject(CompilationResult compilation, VirtualObject expectedObject, ValueInfo[] actualObject, FrameInfoQueryResult actualFrame, BitSet visitedVirtualObjects) {
    if (visitedVirtualObjects.get(expectedObject.getId())) {
        return;
    }
    visitedVirtualObjects.set(expectedObject.getId());
    ObjectLayout objectLayout = ConfigurationValues.getObjectLayout();
    SharedType expectedType = (SharedType) expectedObject.getType();
    if (expectedType.isArray()) {
        JavaKind kind = expectedType.getComponentType().getJavaKind();
        int expectedLength = 0;
        for (int i = 0; i < expectedObject.getValues().length; i++) {
            JavaValue expectedValue = expectedObject.getValues()[i];
            UnsignedWord expectedOffset = WordFactory.unsigned(objectLayout.getArrayElementOffset(expectedType.getComponentType().getJavaKind(), expectedLength));
            ValueInfo actualValue = findActualArrayElement(actualObject, expectedOffset);
            verifyValue(compilation, expectedValue, actualValue, actualFrame, visitedVirtualObjects);
            JavaKind valueKind = expectedObject.getSlotKind(i);
            if (objectLayout.sizeInBytes(kind) == 4 && objectLayout.sizeInBytes(valueKind) == 8) {
                /*
                     * Truffle uses arrays in a non-standard way: it declares an int[] array and
                     * uses it to also store long and double values. These values span two array
                     * elements - so we have to add 2 to the length.
                     */
                expectedLength += 2;
            } else {
                expectedLength++;
            }
        }
        int actualLength = actualObject[1].value.asInt();
        assert expectedLength == actualLength;
    } else {
        SharedField[] expectedFields = (SharedField[]) expectedType.getInstanceFields(true);
        int fieldIdx = 0;
        int valueIdx = 0;
        while (valueIdx < expectedObject.getValues().length) {
            SharedField expectedField = expectedFields[fieldIdx];
            fieldIdx += 1;
            JavaValue expectedValue = expectedObject.getValues()[valueIdx];
            JavaKind valueKind = expectedObject.getSlotKind(valueIdx);
            valueIdx += 1;
            JavaKind kind = expectedField.getStorageKind();
            if (objectLayout.sizeInBytes(kind) == 4 && objectLayout.sizeInBytes(valueKind) == 8) {
                /*
                     * Truffle uses fields in a non-standard way: it declares a couple of
                     * (consecutive) int fields, and uses them to also store long and double values.
                     * These values span two fields - so we have to ignore a field.
                     */
                fieldIdx++;
            }
            UnsignedWord expectedOffset = WordFactory.unsigned(expectedField.getLocation());
            ValueInfo actualValue = findActualField(actualObject, expectedOffset);
            verifyValue(compilation, expectedValue, actualValue, actualFrame, visitedVirtualObjects);
        }
    }
}
Also used : SharedField(com.oracle.svm.core.meta.SharedField) UnsignedWord(org.graalvm.word.UnsignedWord) JavaValue(jdk.vm.ci.meta.JavaValue) ValueInfo(com.oracle.svm.core.code.FrameInfoQueryResult.ValueInfo) ObjectLayout(com.oracle.svm.core.config.ObjectLayout) SharedType(com.oracle.svm.core.meta.SharedType) Infopoint(jdk.vm.ci.code.site.Infopoint) DeoptEntryInfopoint(com.oracle.svm.core.deopt.DeoptEntryInfopoint) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 82 with JavaKind

use of jdk.vm.ci.meta.JavaKind in project graal by oracle.

the class FrameInfoVerifier method makeVirtualObject.

private void makeVirtualObject(FrameData data, VirtualObject virtualObject) {
    int id = virtualObject.getId();
    if (data.virtualObjects[id] != null) {
        return;
    }
    /* Install a non-null value to support recursive VirtualObjects. */
    data.virtualObjects[id] = MARKER;
    /* Objects must contain only compressed references when compression is enabled */
    boolean compressedRefs = ReferenceAccess.singleton().haveCompressedReferences();
    List<ValueInfo> valueList = new ArrayList<>(virtualObject.getValues().length + 4);
    SharedType type = (SharedType) virtualObject.getType();
    /* The first element is the hub of the virtual object. */
    valueList.add(makeValueInfo(data, JavaKind.Object, SubstrateObjectConstant.forObject(type.getHub())));
    ObjectLayout objectLayout = ConfigurationValues.getObjectLayout();
    assert type.isArray() == LayoutEncoding.isArray(type.getHub().getLayoutEncoding()) : "deoptimization code uses layout encoding to determine if type is an array";
    if (type.isArray()) {
        /* We do not know the final length yet, so add a placeholder. */
        valueList.add(null);
        int length = 0;
        JavaKind kind = ((SharedType) type.getComponentType()).getStorageKind();
        for (int i = 0; i < virtualObject.getValues().length; i++) {
            JavaValue value = virtualObject.getValues()[i];
            JavaKind valueKind = virtualObject.getSlotKind(i);
            if (objectLayout.sizeInBytes(kind, compressedRefs) == 4 && objectLayout.sizeInBytes(valueKind, compressedRefs) == 8) {
                /*
                     * Truffle uses arrays in a non-standard way: it declares an int[] array and
                     * uses it to also store long and double values. These values span two array
                     * elements - so we have to write this element with the actual value kind and
                     * add 2 to the length.
                     */
                valueList.add(makeValueInfo(data, valueKind, value));
                length += 2;
            } else {
                assert objectLayout.sizeInBytes(valueKind.getStackKind(), compressedRefs) <= objectLayout.sizeInBytes(kind.getStackKind(), compressedRefs);
                valueList.add(makeValueInfo(data, kind, value));
                length++;
            }
            assert objectLayout.getArrayElementOffset(type.getComponentType().getJavaKind(), length) == objectLayout.getArrayBaseOffset(type.getComponentType().getJavaKind()) + computeOffset(valueList.subList(2, valueList.size()), compressedRefs);
        }
        assert valueList.get(1) == null;
        valueList.set(1, makeValueInfo(data, JavaKind.Int, JavaConstant.forInt(length)));
    } else {
        /*
             * We must add filling constants for padding, so that values are contiguous. The
             * deoptimization code does not have access to field information.
             */
        SharedField[] fields = (SharedField[]) type.getInstanceFields(true);
        long curOffset = objectLayout.getFirstFieldOffset();
        int fieldIdx = 0;
        int valueIdx = 0;
        while (valueIdx < virtualObject.getValues().length) {
            SharedField field = fields[fieldIdx];
            fieldIdx += 1;
            JavaValue value = virtualObject.getValues()[valueIdx];
            JavaKind valueKind = virtualObject.getSlotKind(valueIdx);
            valueIdx += 1;
            JavaKind kind = field.getStorageKind();
            if (objectLayout.sizeInBytes(kind, compressedRefs) == 4 && objectLayout.sizeInBytes(valueKind, compressedRefs) == 8) {
                /*
                     * Truffle uses fields in a non-standard way: it declares a couple of
                     * (consecutive) int fields, and uses them to also store long and double values.
                     * These values span two fields - so we have to ignore a field.
                     */
                kind = valueKind;
                assert fields[fieldIdx].getJavaKind() == field.getJavaKind();
                fieldIdx++;
            }
            if (field.getLocation() >= 0) {
                assert curOffset <= field.getLocation();
                while (curOffset + 7 < field.getLocation()) {
                    valueList.add(makeValueInfo(data, JavaKind.Long, JavaConstant.LONG_0));
                    curOffset += 8;
                }
                if (curOffset + 3 < field.getLocation()) {
                    valueList.add(makeValueInfo(data, JavaKind.Int, JavaConstant.INT_0));
                    curOffset += 4;
                }
                if (curOffset + 1 < field.getLocation()) {
                    valueList.add(makeValueInfo(data, JavaKind.Short, JavaConstant.forShort((short) 0)));
                    curOffset += 2;
                }
                if (curOffset < field.getLocation()) {
                    valueList.add(makeValueInfo(data, JavaKind.Byte, JavaConstant.forByte((byte) 0)));
                    curOffset += 1;
                }
                assert curOffset == field.getLocation();
                assert curOffset == computeOffset(valueList, compressedRefs);
                valueList.add(makeValueInfo(data, kind, value));
                curOffset += objectLayout.sizeInBytes(kind, compressedRefs);
            }
        }
    }
    data.virtualObjects[id] = valueList.toArray(new ValueInfo[valueList.size()]);
    ImageSingletons.lookup(Counters.class).virtualObjectsCount.inc();
}
Also used : SharedField(com.oracle.svm.core.meta.SharedField) ValueInfo(com.oracle.svm.core.code.FrameInfoQueryResult.ValueInfo) JavaValue(jdk.vm.ci.meta.JavaValue) ObjectLayout(com.oracle.svm.core.config.ObjectLayout) ArrayList(java.util.ArrayList) SharedType(com.oracle.svm.core.meta.SharedType) Infopoint(jdk.vm.ci.code.site.Infopoint) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 83 with JavaKind

use of jdk.vm.ci.meta.JavaKind in project graal by oracle.

the class RecomputedFields method compute.

@Override
public Object compute(ResolvedJavaField original, ResolvedJavaField annotated, Object receiver) {
    try {
        ObjectLayout layout = ImageSingletons.lookup(ObjectLayout.class);
        /*
             * ASHIFT is a hard-coded constant in the original implementation, so there is no need
             * to recompute it. It is a private field, so we need reflection to access it.
             */
        Field ashiftField = java.util.concurrent.Exchanger.class.getDeclaredField("ASHIFT");
        ashiftField.setAccessible(true);
        int ashift = ashiftField.getInt(null);
        /*
             * The original implementation uses Node[].class, but we know that all Object arrays
             * have the same kind and layout. The kind denotes the element type of the array.
             */
        JavaKind ak = JavaKind.Object;
        // ABASE absorbs padding in front of element 0
        int abase = layout.getArrayBaseOffset(ak) + (1 << ashift);
        /* Sanity check. */
        final int s = layout.getArrayIndexScale(ak);
        if ((s & (s - 1)) != 0 || s > (1 << ashift)) {
            throw VMError.shouldNotReachHere("Unsupported array scale");
        }
        return abase;
    } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException ex) {
        throw VMError.shouldNotReachHere(ex);
    }
}
Also used : ResolvedJavaField(jdk.vm.ci.meta.ResolvedJavaField) Field(java.lang.reflect.Field) ObjectLayout(com.oracle.svm.core.config.ObjectLayout) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 84 with JavaKind

use of jdk.vm.ci.meta.JavaKind in project graal by oracle.

the class Deoptimizer method materializeObject.

/**
 * Materializes a virtual object.
 *
 * @param virtualObjectId the id of the virtual object to materialize
 * @return the materialized object
 */
private Object materializeObject(int virtualObjectId, FrameInfoQueryResult sourceFrame) {
    if (materializedObjects == null) {
        materializedObjects = new Object[sourceFrame.getVirtualObjects().length];
    }
    assert materializedObjects.length == sourceFrame.getVirtualObjects().length;
    Object obj = materializedObjects[virtualObjectId];
    if (obj != null) {
        return obj;
    }
    DeoptimizationCounters.counters().virtualObjectsCount.inc();
    ValueInfo[] encodings = sourceFrame.getVirtualObjects()[virtualObjectId];
    DynamicHub hub = KnownIntrinsics.convertUnknownValue(SubstrateObjectConstant.asObject(readValue(encodings[0], sourceFrame)), DynamicHub.class);
    ObjectLayout objectLayout = ConfigurationValues.getObjectLayout();
    int curIdx;
    UnsignedWord curOffset;
    if (LayoutEncoding.isArray(hub.getLayoutEncoding())) {
        /* For arrays, the second encoded value is the array length. */
        int length = readValue(encodings[1], sourceFrame).asInt();
        obj = Array.newInstance(hub.getComponentHub().asClass(), length);
        curOffset = LayoutEncoding.getArrayBaseOffset(hub.getLayoutEncoding());
        curIdx = 2;
    } else {
        try {
            obj = UnsafeAccess.UNSAFE.allocateInstance(hub.asClass());
        } catch (InstantiationException ex) {
            throw VMError.shouldNotReachHere(ex);
        }
        curOffset = WordFactory.unsigned(objectLayout.getFirstFieldOffset());
        curIdx = 1;
    }
    materializedObjects[virtualObjectId] = obj;
    if (testGCinDeoptimizer) {
        Heap.getHeap().getGC().collect("from Deoptimizer.materializeObject because of testGCinDeoptimizer");
    }
    /* Objects must contain only compressed references when compression is enabled */
    boolean useCompressedReferences = ReferenceAccess.singleton().haveCompressedReferences();
    while (curIdx < encodings.length) {
        ValueInfo value = encodings[curIdx];
        JavaKind kind = value.getKind();
        JavaConstant con = readValue(value, sourceFrame);
        writeValueInMaterializedObj(obj, curOffset, con);
        curOffset = curOffset.add(objectLayout.sizeInBytes(kind, useCompressedReferences));
        curIdx++;
    }
    return obj;
}
Also used : UnsignedWord(org.graalvm.word.UnsignedWord) ValueInfo(com.oracle.svm.core.code.FrameInfoQueryResult.ValueInfo) ObjectLayout(com.oracle.svm.core.config.ObjectLayout) DynamicHub(com.oracle.svm.core.hub.DynamicHub) JavaConstant(jdk.vm.ci.meta.JavaConstant) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 85 with JavaKind

use of jdk.vm.ci.meta.JavaKind in project graal by oracle.

the class NodeLIRBuilder method emitSwitch.

/**
 * This method tries to create a switch implementation that is optimal for the given switch. It
 * will either generate a sequential if/then/else cascade, a set of range tests or a table
 * switch.
 *
 * If the given switch does not contain int keys, it will always create a sequential
 * implementation.
 */
@Override
public void emitSwitch(SwitchNode x) {
    assert x.defaultSuccessor() != null;
    LabelRef defaultTarget = getLIRBlock(x.defaultSuccessor());
    int keyCount = x.keyCount();
    if (keyCount == 0) {
        gen.emitJump(defaultTarget);
    } else {
        Variable value = gen.load(operand(x.value()));
        if (keyCount == 1) {
            assert defaultTarget != null;
            double probability = x.probability(x.keySuccessor(0));
            LIRKind kind = gen.getLIRKind(x.value().stamp(NodeView.DEFAULT));
            Value key = gen.emitConstant(kind, x.keyAt(0));
            gen.emitCompareBranch(kind.getPlatformKind(), gen.load(operand(x.value())), key, Condition.EQ, false, getLIRBlock(x.keySuccessor(0)), defaultTarget, probability);
        } else if (x instanceof IntegerSwitchNode && x.isSorted()) {
            IntegerSwitchNode intSwitch = (IntegerSwitchNode) x;
            LabelRef[] keyTargets = new LabelRef[keyCount];
            JavaConstant[] keyConstants = new JavaConstant[keyCount];
            double[] keyProbabilities = new double[keyCount];
            JavaKind keyKind = intSwitch.keyAt(0).getJavaKind();
            for (int i = 0; i < keyCount; i++) {
                keyTargets[i] = getLIRBlock(intSwitch.keySuccessor(i));
                keyConstants[i] = intSwitch.keyAt(i);
                keyProbabilities[i] = intSwitch.keyProbability(i);
                assert keyConstants[i].getJavaKind() == keyKind;
            }
            gen.emitStrategySwitch(keyConstants, keyProbabilities, keyTargets, defaultTarget, value);
        } else {
            // keyKind != JavaKind.Int || !x.isSorted()
            LabelRef[] keyTargets = new LabelRef[keyCount];
            Constant[] keyConstants = new Constant[keyCount];
            double[] keyProbabilities = new double[keyCount];
            for (int i = 0; i < keyCount; i++) {
                keyTargets[i] = getLIRBlock(x.keySuccessor(i));
                keyConstants[i] = x.keyAt(i);
                keyProbabilities[i] = x.keyProbability(i);
            }
            // hopefully only a few entries
            gen.emitStrategySwitch(new SwitchStrategy.SequentialStrategy(keyProbabilities, keyConstants), value, keyTargets, defaultTarget);
        }
    }
}
Also used : Variable(org.graalvm.compiler.lir.Variable) ComplexMatchValue(org.graalvm.compiler.core.match.ComplexMatchValue) Value(jdk.vm.ci.meta.Value) AllocatableValue(jdk.vm.ci.meta.AllocatableValue) LIRKind(org.graalvm.compiler.core.common.LIRKind) LabelRef(org.graalvm.compiler.lir.LabelRef) IntegerSwitchNode(org.graalvm.compiler.nodes.extended.IntegerSwitchNode) JavaKind(jdk.vm.ci.meta.JavaKind)

Aggregations

JavaKind (jdk.vm.ci.meta.JavaKind)90 ValueNode (org.graalvm.compiler.nodes.ValueNode)44 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)24 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)17 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)16 AddressNode (org.graalvm.compiler.nodes.memory.address.AddressNode)13 OffsetAddressNode (org.graalvm.compiler.nodes.memory.address.OffsetAddressNode)12 JavaType (jdk.vm.ci.meta.JavaType)11 Stamp (org.graalvm.compiler.core.common.type.Stamp)10 LocationIdentity (org.graalvm.word.LocationIdentity)10 ArrayList (java.util.ArrayList)9 JavaConstant (jdk.vm.ci.meta.JavaConstant)9 AbstractMergeNode (org.graalvm.compiler.nodes.AbstractMergeNode)8 FrameState (org.graalvm.compiler.nodes.FrameState)8 GraphBuilderContext (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext)8 Signature (jdk.vm.ci.meta.Signature)7 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)7 ObjectLayout (com.oracle.svm.core.config.ObjectLayout)6 ObjectStamp (org.graalvm.compiler.core.common.type.ObjectStamp)6 InvokeWithExceptionNode (org.graalvm.compiler.nodes.InvokeWithExceptionNode)6