TypeCheckHints.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.nodes;
24
25import java.util.Arrays;
26
27import org.graalvm.compiler.core.common.type.TypeReference;
28
29import jdk.vm.ci.meta.Assumptions;
30import jdk.vm.ci.meta.JavaTypeProfile;
31import jdk.vm.ci.meta.JavaTypeProfile.ProfiledType;
32import jdk.vm.ci.meta.ResolvedJavaType;
33
34/**
35 * Utility for deriving hint types for a type check instruction (e.g. checkcast or instanceof) based
36 * on the target type of the check and any profiling information available for the instruction.
37 */
38public class TypeCheckHints {
39
40    /**
41     * A receiver type profiled in a type check instruction.
42     */
43    public static class Hint {
44
45        /**
46         * A type seen while profiling a type check instruction.
47         */
48        public final ResolvedJavaType type;
49
50        /**
51         * Specifies if {@link #type} is a sub-type of the checked type.
52         */
53        public final boolean positive;
54
55        Hint(ResolvedJavaType type, boolean positive) {
56            this.type = type;
57            this.positive = positive;
58        }
59    }
60
61    private static final Hint[] NO_HINTS = {};
62
63    /**
64     * If non-null, then this is the only type that could pass the type check because the target of
65     * the type check is a final class or has been speculated to be a final class and this value is
66     * the only concrete subclass of the target type.
67     */
68    public final ResolvedJavaType exact;
69
70    /**
71     * The most likely types that the type check instruction will see.
72     */
73    public final Hint[] hints;
74
75    /**
76     * The profile from which this information was derived.
77     */
78    public final JavaTypeProfile profile;
79
80    /**
81     * The total probability that the type check will hit one of the types in {@link #hints}.
82     */
83    public final double hintHitProbability;
84
85    /**
86     * Derives hint information for use when generating the code for a type check instruction.
87     *
88     * @param targetType the target type of the type check
89     * @param profile the profiling information available for the instruction (if any)
90     * @param assumptions the object in which speculations are recorded. This is null if
91     *            speculations are not supported.
92     * @param minHintHitProbability if the probability that the type check will hit one of the
93     *            profiled types (up to {@code maxHints}) is below this value, then {@link #hints}
94     *            will be null
95     * @param maxHints the maximum length of {@link #hints}
96     */
97    public TypeCheckHints(TypeReference targetType, JavaTypeProfile profile, Assumptions assumptions, double minHintHitProbability, int maxHints) {
98        this.profile = profile;
99        if (targetType != null && targetType.isExact()) {
100            exact = targetType.getType();
101        } else {
102            exact = null;
103        }
104        Double[] hitProbability = {null};
105        this.hints = makeHints(targetType, profile, minHintHitProbability, maxHints, hitProbability);
106        this.hintHitProbability = hitProbability[0];
107    }
108
109    private static Hint[] makeHints(TypeReference targetType, JavaTypeProfile profile, double minHintHitProbability, int maxHints, Double[] hitProbability) {
110        double hitProb = 0.0d;
111        Hint[] hintsBuf = NO_HINTS;
112        if (profile != null) {
113            double notRecordedTypes = profile.getNotRecordedProbability();
114            ProfiledType[] ptypes = profile.getTypes();
115            if (notRecordedTypes < (1D - minHintHitProbability) && ptypes != null && ptypes.length > 0) {
116                hintsBuf = new Hint[ptypes.length];
117                int hintCount = 0;
118                for (ProfiledType ptype : ptypes) {
119                    if (targetType != null) {
120                        ResolvedJavaType hintType = ptype.getType();
121                        hintsBuf[hintCount++] = new Hint(hintType, targetType.getType().isAssignableFrom(hintType));
122                        hitProb += ptype.getProbability();
123                    }
124                    if (hintCount == maxHints) {
125                        break;
126                    }
127                }
128                if (hitProb >= minHintHitProbability) {
129                    if (hintsBuf.length != hintCount || hintCount > maxHints) {
130                        hintsBuf = Arrays.copyOf(hintsBuf, Math.min(maxHints, hintCount));
131                    }
132                } else {
133                    hintsBuf = NO_HINTS;
134                    hitProb = 0.0d;
135                }
136            }
137        }
138        hitProbability[0] = hitProb;
139        return hintsBuf;
140    }
141}
142