001/*
002 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.replacements;
024
025import static com.oracle.graal.compiler.common.GraalOptions.*;
026import static com.oracle.graal.hotspot.replacements.HotSpotReplacementsUtil.*;
027import static com.oracle.graal.nodes.extended.BranchProbabilityNode.*;
028
029import java.util.*;
030
031import jdk.internal.jvmci.hotspot.*;
032import jdk.internal.jvmci.meta.*;
033
034import com.oracle.graal.hotspot.word.*;
035import com.oracle.graal.nodes.*;
036import com.oracle.graal.nodes.spi.*;
037import com.oracle.graal.replacements.*;
038import com.oracle.graal.word.*;
039
040//JaCoCo Exclude
041
042/**
043 * Utilities and common code paths used by the type check snippets.
044 */
045public class TypeCheckSnippetUtils {
046
047    static boolean checkSecondarySubType(KlassPointer t, KlassPointer s) {
048        // if (S.cache == T) return true
049        if (s.readKlassPointer(secondarySuperCacheOffset(), SECONDARY_SUPER_CACHE_LOCATION).equal(t)) {
050            cacheHit.inc();
051            return true;
052        }
053
054        return checkSelfAndSupers(t, s);
055    }
056
057    static boolean checkUnknownSubType(KlassPointer t, KlassPointer s) {
058        // int off = T.offset
059        int superCheckOffset = t.readInt(superCheckOffsetOffset(), KLASS_SUPER_CHECK_OFFSET_LOCATION);
060        boolean primary = superCheckOffset != secondarySuperCacheOffset();
061
062        // if (T = S[off]) return true
063        if (s.readKlassPointer(superCheckOffset, PRIMARY_SUPERS_LOCATION).equal(t)) {
064            if (primary) {
065                cacheHit.inc();
066            } else {
067                displayHit.inc();
068            }
069            return true;
070        }
071
072        // if (off != &cache) return false
073        if (primary) {
074            displayMiss.inc();
075            return false;
076        }
077
078        return checkSelfAndSupers(t, s);
079    }
080
081    private static boolean checkSelfAndSupers(KlassPointer t, KlassPointer s) {
082        // if (T == S) return true
083        if (s.equal(t)) {
084            T_equals_S.inc();
085            return true;
086        }
087
088        // if (S.scan_s_s_array(T)) { S.cache = T; return true; }
089        Word secondarySupers = s.readWord(secondarySupersOffset(), SECONDARY_SUPERS_LOCATION);
090        int length = secondarySupers.readInt(metaspaceArrayLengthOffset(), METASPACE_ARRAY_LENGTH_LOCATION);
091        for (int i = 0; i < length; i++) {
092            if (probability(NOT_LIKELY_PROBABILITY, t.equal(loadSecondarySupersElement(secondarySupers, i)))) {
093                s.writeKlassPointer(secondarySuperCacheOffset(), t, SECONDARY_SUPER_CACHE_LOCATION);
094                secondariesHit.inc();
095                return true;
096            }
097        }
098        secondariesMiss.inc();
099        return false;
100    }
101
102    /**
103     * A set of type check hints ordered by decreasing probabilities.
104     */
105    public static class Hints {
106
107        /**
108         * The hubs of the hint types.
109         */
110        public final ConstantNode[] hubs;
111
112        /**
113         * A predicate over {@link #hubs} specifying whether the corresponding hint type is a
114         * sub-type of the checked type.
115         */
116        public final boolean[] isPositive;
117
118        Hints(ConstantNode[] hints, boolean[] hintIsPositive) {
119            this.hubs = hints;
120            this.isPositive = hintIsPositive;
121        }
122    }
123
124    static Hints createHints(TypeCheckHints hints, MetaAccessProvider metaAccess, boolean positiveOnly, StructuredGraph graph, LoweringTool tool) {
125        ConstantNode[] hubs = new ConstantNode[hints.hints.length];
126        boolean[] isPositive = new boolean[hints.hints.length];
127        int index = 0;
128        for (int i = 0; i < hubs.length; i++) {
129            if (!positiveOnly || hints.hints[i].positive) {
130                hubs[index] = ConstantNode.forConstant(tool.getStampProvider().createHubStamp(true), ((HotSpotResolvedObjectType) hints.hints[i].type).klass(), metaAccess, graph);
131                isPositive[index] = hints.hints[i].positive;
132                index++;
133            }
134        }
135        if (positiveOnly && index != hubs.length) {
136            assert index < hubs.length;
137            hubs = Arrays.copyOf(hubs, index);
138            isPositive = Arrays.copyOf(isPositive, index);
139        }
140        return new Hints(hubs, isPositive);
141    }
142
143    static KlassPointer loadSecondarySupersElement(Word metaspaceArray, int index) {
144        return KlassPointer.fromWord(metaspaceArray.readWord(metaspaceArrayBaseOffset() + index * wordSize(), SECONDARY_SUPERS_ELEMENT_LOCATION));
145    }
146
147    private static final SnippetCounter.Group counters = SnippetCounters.getValue() ? new SnippetCounter.Group("TypeCheck") : null;
148    static final SnippetCounter hintsHit = new SnippetCounter(counters, "hintsHit", "hit a hint type");
149    static final SnippetCounter hintsMiss = new SnippetCounter(counters, "hintsMiss", "missed a hint type");
150    static final SnippetCounter exactHit = new SnippetCounter(counters, "exactHit", "exact type test succeeded");
151    static final SnippetCounter exactMiss = new SnippetCounter(counters, "exactMiss", "exact type test failed");
152    static final SnippetCounter isNull = new SnippetCounter(counters, "isNull", "object tested was null");
153    static final SnippetCounter cacheHit = new SnippetCounter(counters, "cacheHit", "secondary type cache hit");
154    static final SnippetCounter secondariesHit = new SnippetCounter(counters, "secondariesHit", "secondaries scan succeeded");
155    static final SnippetCounter secondariesMiss = new SnippetCounter(counters, "secondariesMiss", "secondaries scan failed");
156    static final SnippetCounter displayHit = new SnippetCounter(counters, "displayHit", "primary type test succeeded");
157    static final SnippetCounter displayMiss = new SnippetCounter(counters, "displayMiss", "primary type test failed");
158    static final SnippetCounter T_equals_S = new SnippetCounter(counters, "T_equals_S", "object type was equal to secondary type");
159
160}