Search in sources :

Example 1 with ProfiledType

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

the class BytecodeParser method adjustProfileForInvocationPlugin.

/**
 * Adjusts the profile for an indirect invocation of a virtual method for which there is an
 * intrinsic. The adjustment made by this method is to remove all types from the profile that do
 * not override {@code targetMethod}.
 *
 * @param profile the profile to adjust
 * @param targetMethod the virtual method for which there is an intrinsic
 * @return the adjusted profile or the original {@code profile} object if no adjustment was made
 */
protected JavaTypeProfile adjustProfileForInvocationPlugin(JavaTypeProfile profile, ResolvedJavaMethod targetMethod) {
    if (profile.getTypes().length > 0) {
        List<ProfiledType> retained = new ArrayList<>();
        double notRecordedProbability = profile.getNotRecordedProbability();
        for (ProfiledType ptype : profile.getTypes()) {
            if (!ptype.getType().resolveMethod(targetMethod, method.getDeclaringClass()).equals(targetMethod)) {
                retained.add(ptype);
            } else {
                notRecordedProbability += ptype.getProbability();
            }
        }
        if (!retained.isEmpty()) {
            if (retained.size() != profile.getTypes().length) {
                return new JavaTypeProfile(profile.getNullSeen(), notRecordedProbability, retained.toArray(new ProfiledType[retained.size()]));
            }
        } else {
            return new JavaTypeProfile(profile.getNullSeen(), notRecordedProbability, new ProfiledType[0]);
        }
    }
    return profile;
}
Also used : ProfiledType(jdk.vm.ci.meta.JavaTypeProfile.ProfiledType) JavaTypeProfile(jdk.vm.ci.meta.JavaTypeProfile) ArrayList(java.util.ArrayList)

Example 2 with ProfiledType

use of jdk.vm.ci.meta.JavaTypeProfile.ProfiledType 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 3 with ProfiledType

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

the class TypeCheckHints method makeHints.

private static Hint[] makeHints(TypeReference targetType, JavaTypeProfile profile, double minHintHitProbability, int maxHints, Double[] hitProbability) {
    double hitProb = 0.0d;
    Hint[] hintsBuf = NO_HINTS;
    if (profile != null) {
        double notRecordedTypes = profile.getNotRecordedProbability();
        ProfiledType[] ptypes = profile.getTypes();
        if (notRecordedTypes < (1D - minHintHitProbability) && ptypes != null && ptypes.length > 0) {
            hintsBuf = new Hint[ptypes.length];
            int hintCount = 0;
            for (ProfiledType ptype : ptypes) {
                if (targetType != null) {
                    ResolvedJavaType hintType = ptype.getType();
                    hintsBuf[hintCount++] = new Hint(hintType, targetType.getType().isAssignableFrom(hintType));
                    hitProb += ptype.getProbability();
                }
                if (hintCount == maxHints) {
                    break;
                }
            }
            if (hitProb >= minHintHitProbability) {
                if (hintsBuf.length != hintCount || hintCount > maxHints) {
                    hintsBuf = Arrays.copyOf(hintsBuf, Math.min(maxHints, hintCount));
                }
            } else {
                hintsBuf = NO_HINTS;
                hitProb = 0.0d;
            }
        }
    }
    hitProbability[0] = hitProb;
    return hintsBuf;
}
Also used : ProfiledType(jdk.vm.ci.meta.JavaTypeProfile.ProfiledType) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType)

Aggregations

ProfiledType (jdk.vm.ci.meta.JavaTypeProfile.ProfiledType)3 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)2 HostedType (com.oracle.svm.hosted.meta.HostedType)1 ArrayList (java.util.ArrayList)1 JavaTypeProfile (jdk.vm.ci.meta.JavaTypeProfile)1 AbstractObjectStamp (org.graalvm.compiler.core.common.type.AbstractObjectStamp)1 ObjectStamp (org.graalvm.compiler.core.common.type.ObjectStamp)1 TypeReference (org.graalvm.compiler.core.common.type.TypeReference)1 FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)1 FixedWithNextNode (org.graalvm.compiler.nodes.FixedWithNextNode)1 InvokeWithExceptionNode (org.graalvm.compiler.nodes.InvokeWithExceptionNode)1 ParameterNode (org.graalvm.compiler.nodes.ParameterNode)1 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)1