Search in sources :

Example 6 with HostedType

use of com.oracle.svm.hosted.meta.HostedType in project graal by oracle.

the class StrengthenStampsPhase method strengthenStamp.

private Stamp strengthenStamp(ValueNode node, JavaTypeProfile typeProfile) {
    ObjectStamp oldStamp = (ObjectStamp) node.stamp(NodeView.DEFAULT);
    HostedType oldType = toHosted(oldStamp.type());
    if (oldStamp.alwaysNull()) {
        /* We cannot make that more precise. */
        return oldStamp;
    }
    boolean nonNull = oldStamp.nonNull() || typeProfile.getNullSeen() == TriState.FALSE;
    ProfiledType[] exactTypes = typeProfile.getTypes();
    if (exactTypes.length == 1) {
        ResolvedJavaType exactType = exactTypes[0].getType();
        assert oldType == null || oldType.isAssignableFrom(exactType);
        if (!oldStamp.isExactType() || !exactType.equals(oldType) || nonNull != oldStamp.nonNull()) {
            TypeReference typeRef = TypeReference.createExactTrusted(toTarget(exactType));
            return nonNull ? StampFactory.objectNonNull(typeRef) : StampFactory.object(typeRef);
        } else {
            return oldStamp;
        }
    }
    if (exactTypes.length == 0) {
        if (!nonNull) {
            return StampFactory.alwaysNull();
        } else {
            /*
                 * The code after the node is unreachable. We just insert a always-failing guard
                 * after the node and let dead code elimination remove everything after the node.
                 */
            StructuredGraph graph = node.graph();
            FixedWithNextNode insertionPoint;
            if (node instanceof ParameterNode) {
                /* The whole method is unreachable. */
                insertionPoint = graph.start();
            } else if (node instanceof InvokeWithExceptionNode) {
                /* The invoked method never returns normally (but can throw an exception). */
                insertionPoint = ((InvokeWithExceptionNode) node).next();
            } else {
                insertionPoint = (FixedWithNextNode) node;
            }
            graph.addAfterFixed(insertionPoint, graph.add(new FixedGuardNode(LogicConstantNode.forBoolean(true, graph), DeoptimizationReason.UnreachedCode, DeoptimizationAction.None, true)));
            return oldStamp;
        }
    }
    ResolvedJavaType baseType;
    if (oldStamp.isExactType()) {
        /* Base type cannot be more precise. */
        baseType = oldType;
    } else {
        assert exactTypes.length > 1;
        assert oldType == null || oldType.isAssignableFrom(exactTypes[0].getType());
        baseType = exactTypes[0].getType();
        for (int i = 1; i < exactTypes.length; i++) {
            assert oldType == null || oldType.isAssignableFrom(exactTypes[i].getType());
            baseType = baseType.findLeastCommonAncestor(exactTypes[i].getType());
        }
        if (oldType != null && !oldType.isAssignableFrom(baseType)) {
            /*
                 * When the original stamp is an interface type, we do not want to weaken that type
                 * with the common base class of all implementation types (which could even be
                 * java.lang.Object).
                 */
            baseType = oldType;
        }
    }
    if (!baseType.equals(oldType) || nonNull != oldStamp.nonNull()) {
        TypeReference typeRef = TypeReference.createTrustedWithoutAssumptions(toTarget(baseType));
        return nonNull ? StampFactory.objectNonNull(typeRef) : StampFactory.object(typeRef);
    }
    return oldStamp;
}
Also used : FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) ProfiledType(jdk.vm.ci.meta.JavaTypeProfile.ProfiledType) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) AbstractObjectStamp(org.graalvm.compiler.core.common.type.AbstractObjectStamp) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) HostedType(com.oracle.svm.hosted.meta.HostedType) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) ParameterNode(org.graalvm.compiler.nodes.ParameterNode) InvokeWithExceptionNode(org.graalvm.compiler.nodes.InvokeWithExceptionNode) TypeReference(org.graalvm.compiler.core.common.type.TypeReference)

Example 7 with HostedType

use of com.oracle.svm.hosted.meta.HostedType in project graal by oracle.

the class StrengthenStampsPhase method strengthen.

private Stamp strengthen(Stamp s) {
    if (!(s instanceof AbstractObjectStamp)) {
        return null;
    }
    AbstractObjectStamp stamp = (AbstractObjectStamp) s;
    HostedType originalType = toHosted(stamp.type());
    if (originalType == null) {
        return null;
    }
    HostedType strengthenType = originalType.getStrengthenStampType();
    if (originalType.equals(strengthenType)) {
        /* Nothing to strengthen. */
        return null;
    }
    Stamp newStamp;
    if (strengthenType == null) {
        if (stamp.nonNull()) {
            /* We must be in dead code. */
            newStamp = StampFactory.empty(JavaKind.Object);
        } else {
            /* The type its subtypes are not instantiated, the only possible value is null. */
            newStamp = StampFactory.alwaysNull();
        }
    } else {
        if (stamp.isExactType()) {
            /* We must be in dead code. */
            newStamp = StampFactory.empty(JavaKind.Object);
        } else {
            TypeReference typeRef = TypeReference.createTrustedWithoutAssumptions(toTarget(strengthenType));
            newStamp = StampFactory.object(typeRef, stamp.nonNull());
        }
    }
    return newStamp;
}
Also used : HostedType(com.oracle.svm.hosted.meta.HostedType) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) AbstractObjectStamp(org.graalvm.compiler.core.common.type.AbstractObjectStamp) Stamp(org.graalvm.compiler.core.common.type.Stamp) AbstractObjectStamp(org.graalvm.compiler.core.common.type.AbstractObjectStamp) TypeReference(org.graalvm.compiler.core.common.type.TypeReference)

Example 8 with HostedType

use of com.oracle.svm.hosted.meta.HostedType in project graal by oracle.

the class NativeImageCodeCache method printCompilationResults.

public void printCompilationResults() {
    System.out.println("--- compiled methods");
    for (Entry<HostedMethod, CompilationResult> entry : compilations.entrySet()) {
        HostedMethod method = entry.getKey();
        System.out.format("%8d %5d %s: frame %d\n", method.getCodeAddressOffset(), entry.getValue().getTargetCodeSize(), method.format("%H.%n(%p)"), entry.getValue().getTotalFrameSize());
    }
    System.out.println("--- vtables:");
    for (HostedType type : imageHeap.getUniverse().getTypes()) {
        for (int i = 0; i < type.getVTable().length; i++) {
            HostedMethod method = type.getVTable()[i];
            if (method != null) {
                CompilationResult comp = compilations.get(type.getVTable()[i]);
                if (comp != null) {
                    System.out.format("%d %s @ %d: %s = 0x%x\n", type.getTypeID(), type.toJavaName(false), i, method.format("%r %n(%p)"), method.getCodeAddressOffset());
                }
            }
        }
    }
}
Also used : HostedType(com.oracle.svm.hosted.meta.HostedType) HostedMethod(com.oracle.svm.hosted.meta.HostedMethod) CompilationResult(org.graalvm.compiler.code.CompilationResult) Infopoint(jdk.vm.ci.code.site.Infopoint) DeoptEntryInfopoint(com.oracle.svm.core.deopt.DeoptEntryInfopoint)

Example 9 with HostedType

use of com.oracle.svm.hosted.meta.HostedType in project graal by oracle.

the class NativeImageHeap method choosePartition.

/**
 * Choose a partition of the native image heap for the given object.
 */
private HeapPartition choosePartition(final Object candidate, final boolean immutableArg) {
    final HostedType type = getMetaAccess().lookupJavaType(candidate.getClass());
    assert type.getWrapped().isInstantiated() : type;
    boolean written = false;
    boolean references = false;
    boolean immutable = immutableArg;
    if (type.isInstanceClass()) {
        final HostedInstanceClass clazz = (HostedInstanceClass) type;
        if (HybridLayout.isHybrid(clazz)) {
            final HybridLayout<?> hybridLayout = new HybridLayout<>(clazz, layout);
            final HostedField arrayField = hybridLayout.getArrayField();
            written |= arrayField.isWritten();
            final JavaKind arrayKind = hybridLayout.getArrayElementKind();
            references |= arrayKind.isObject();
        }
        // Aggregate over all the fields of the instance.
        for (HostedField field : clazz.getInstanceFields(true)) {
            /*
                 * Any field that is written says the instance is written. Except that if the field
                 * is final, it will only be written during initialization during native image
                 * construction, but will not be written in the running image.
                 */
            written |= field.isWritten() && !field.isFinal();
            references |= field.getType().getStorageKind().isObject();
        }
        // If the type has a monitor field, it has a reference field that is written.
        if (clazz.getMonitorFieldOffset() != 0) {
            written = true;
            references = true;
            immutable = false;
        }
    } else if (type.isArray()) {
        HostedArrayClass clazz = (HostedArrayClass) type;
        // TODO: How to know if any of the array elements are written?
        written = true;
        JavaKind kind = clazz.getComponentType().getJavaKind();
        references = kind.isObject();
    } else {
        throw shouldNotReachHere();
    }
    if (SubstrateOptions.UseOnlyWritableBootImageHeap.getValue()) {
        assert !spawnIsolates();
        // Emergency use only! Alarms will sound!
        return writableReference;
    }
    if (!written || immutable) {
        return references ? readOnlyReference : readOnlyPrimitive;
    } else {
        return references ? writableReference : writablePrimitive;
    }
}
Also used : HostedType(com.oracle.svm.hosted.meta.HostedType) HostedArrayClass(com.oracle.svm.hosted.meta.HostedArrayClass) HostedField(com.oracle.svm.hosted.meta.HostedField) HybridLayout(com.oracle.svm.hosted.config.HybridLayout) HostedInstanceClass(com.oracle.svm.hosted.meta.HostedInstanceClass) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 10 with HostedType

use of com.oracle.svm.hosted.meta.HostedType in project graal by oracle.

the class NativeImageGenerator method printTypes.

private void printTypes() {
    for (HostedType type : hUniverse.getTypes()) {
        System.out.format("%8d %s  ", type.getTypeID(), type.toJavaName(true));
        if (type.getSuperclass() != null) {
            System.out.format("extends %d %s  ", type.getSuperclass().getTypeID(), type.getSuperclass().toJavaName(false));
        }
        if (type.getInterfaces().length > 0) {
            System.out.print("implements ");
            String sep = "";
            for (HostedInterface interf : type.getInterfaces()) {
                System.out.format("%s%d %s", sep, interf.getTypeID(), interf.toJavaName(false));
                sep = ", ";
            }
            System.out.print("  ");
        }
        if (type.getWrapped().isInstantiated()) {
            System.out.print("instantiated  ");
        }
        if (type.getWrapped().isInTypeCheck()) {
            System.out.print("inTypeCheck  ");
        }
        System.out.format("assignableFrom %s  ", matchesToString(type.getAssignableFromMatches()));
        System.out.format("instanceOf typeID %d, # %d  ", type.getInstanceOfFromTypeID(), type.getInstanceOfNumTypeIDs());
        // if (type.findLeafConcreteSubtype() != null) {
        // System.out.format("unique %d %s ", type.findLeafConcreteSubtype().getTypeID(),
        // type.findLeafConcreteSubtype().toJavaName(false));
        // }
        int le = type.getHub().getLayoutEncoding();
        if (LayoutEncoding.isPrimitive(le)) {
            System.out.print("primitive  ");
        } else if (LayoutEncoding.isInterface(le)) {
            System.out.print("interface  ");
        } else if (LayoutEncoding.isAbstract(le)) {
            System.out.print("abstract  ");
        } else if (LayoutEncoding.isInstance(le)) {
            System.out.format("instance size %d  ", LayoutEncoding.getInstanceSize(le).rawValue());
        } else if (LayoutEncoding.isObjectArray(le)) {
            System.out.format("object array base %d shift %d scale %d  ", LayoutEncoding.getArrayBaseOffset(le).rawValue(), LayoutEncoding.getArrayIndexShift(le), LayoutEncoding.getArrayIndexScale(le));
        } else if (LayoutEncoding.isPrimitiveArray(le)) {
            System.out.format("primitive array base %d shift %d scale %d  ", LayoutEncoding.getArrayBaseOffset(le).rawValue(), LayoutEncoding.getArrayIndexShift(le), LayoutEncoding.getArrayIndexScale(le));
        } else {
            throw VMError.shouldNotReachHere();
        }
        System.out.println();
        for (HostedType sub : type.getSubTypes()) {
            System.out.format("               s %d %s\n", sub.getTypeID(), sub.toJavaName(false));
        }
        if (type.isInterface()) {
            for (HostedMethod method : hUniverse.getMethods()) {
                if (method.getDeclaringClass() == type) {
                    printMethod(method, -1);
                }
            }
        } else if (type.isInstanceClass()) {
            HostedField[] fields = type.getInstanceFields(false);
            fields = Arrays.copyOf(fields, fields.length);
            Arrays.sort(fields, Comparator.comparing(HostedField::toString));
            for (HostedField field : fields) {
                System.out.println("               f " + field.getLocation() + ": " + field.format("%T %n"));
            }
            HostedMethod[] vtable = type.getVTable();
            for (int i = 0; i < vtable.length; i++) {
                if (vtable[i] != null) {
                    printMethod(vtable[i], i);
                }
            }
            for (HostedMethod method : hUniverse.getMethods()) {
                if (method.getDeclaringClass() == type && !method.hasVTableIndex()) {
                    printMethod(method, -1);
                }
            }
        }
    }
}
Also used : HostedType(com.oracle.svm.hosted.meta.HostedType) HostedField(com.oracle.svm.hosted.meta.HostedField) HostedMethod(com.oracle.svm.hosted.meta.HostedMethod) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) HostedInterface(com.oracle.svm.hosted.meta.HostedInterface)

Aggregations

HostedType (com.oracle.svm.hosted.meta.HostedType)12 HostedField (com.oracle.svm.hosted.meta.HostedField)4 HostedMethod (com.oracle.svm.hosted.meta.HostedMethod)4 JavaKind (jdk.vm.ci.meta.JavaKind)4 AnalysisType (com.oracle.graal.pointsto.meta.AnalysisType)3 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)3 AnalysisField (com.oracle.graal.pointsto.meta.AnalysisField)2 AnalysisMethod (com.oracle.graal.pointsto.meta.AnalysisMethod)2 DynamicHub (com.oracle.svm.core.hub.DynamicHub)2 SubstrateType (com.oracle.svm.graal.meta.SubstrateType)2 StructFieldInfo (com.oracle.svm.hosted.c.info.StructFieldInfo)2 HostedArrayClass (com.oracle.svm.hosted.meta.HostedArrayClass)2 HostedInstanceClass (com.oracle.svm.hosted.meta.HostedInstanceClass)2 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)2 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)2 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)2 CFunctionPointer (org.graalvm.nativeimage.c.function.CFunctionPointer)2 AnalysisUniverse (com.oracle.graal.pointsto.meta.AnalysisUniverse)1 HostedProviders (com.oracle.graal.pointsto.meta.HostedProviders)1 Alias (com.oracle.svm.core.annotate.Alias)1