1 /*
   2  * Copyright (c) 2013, 2014, 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  */
  23 package org.graalvm.compiler.hotspot.replacements;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.SnippetCounters;
  26 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG;
  27 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.KLASS_SUPER_CHECK_OFFSET_LOCATION;
  28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.METASPACE_ARRAY_LENGTH_LOCATION;
  29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.PRIMARY_SUPERS_LOCATION;
  30 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPERS_ELEMENT_LOCATION;
  31 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPERS_LOCATION;
  32 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPER_CACHE_LOCATION;
  33 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.metaspaceArrayBaseOffset;
  34 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.metaspaceArrayLengthOffset;
  35 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.secondarySuperCacheOffset;
  36 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.secondarySupersOffset;
  37 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.superCheckOffsetOffset;
  38 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.wordSize;
  39 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.NOT_LIKELY_PROBABILITY;
  40 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability;
  41 
  42 import java.util.Arrays;
  43 
  44 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp;
  45 import org.graalvm.compiler.hotspot.word.KlassPointer;
  46 import org.graalvm.compiler.nodes.ConstantNode;
  47 import org.graalvm.compiler.nodes.StructuredGraph;
  48 import org.graalvm.compiler.nodes.TypeCheckHints;
  49 import org.graalvm.compiler.replacements.SnippetCounter;
  50 import org.graalvm.compiler.word.Word;
  51 
  52 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  53 import jdk.vm.ci.meta.MetaAccessProvider;
  54 
  55 //JaCoCo Exclude
  56 
  57 /**
  58  * Utilities and common code paths used by the type check snippets.
  59  */
  60 public class TypeCheckSnippetUtils {
  61 
  62     static boolean checkSecondarySubType(KlassPointer t, KlassPointer s) {
  63         // if (S.cache == T) return true
  64         if (s.readKlassPointer(secondarySuperCacheOffset(INJECTED_VMCONFIG), SECONDARY_SUPER_CACHE_LOCATION).equal(t)) {
  65             cacheHit.inc();
  66             return true;
  67         }
  68 
  69         return checkSelfAndSupers(t, s);
  70     }
  71 
  72     static boolean checkUnknownSubType(KlassPointer t, KlassPointer s) {
  73         // int off = T.offset
  74         int superCheckOffset = t.readInt(superCheckOffsetOffset(INJECTED_VMCONFIG), KLASS_SUPER_CHECK_OFFSET_LOCATION);
  75         boolean primary = superCheckOffset != secondarySuperCacheOffset(INJECTED_VMCONFIG);
  76 
  77         // if (T = S[off]) return true
  78         if (s.readKlassPointer(superCheckOffset, PRIMARY_SUPERS_LOCATION).equal(t)) {
  79             if (primary) {
  80                 cacheHit.inc();
  81             } else {
  82                 displayHit.inc();
  83             }
  84             return true;
  85         }
  86 
  87         // if (off != &cache) return false
  88         if (primary) {
  89             displayMiss.inc();
  90             return false;
  91         }
  92 
  93         return checkSelfAndSupers(t, s);
  94     }
  95 
  96     private static boolean checkSelfAndSupers(KlassPointer t, KlassPointer s) {
  97         // if (T == S) return true
  98         if (s.equal(t)) {
  99             T_equals_S.inc();
 100             return true;
 101         }
 102 
 103         // if (S.scan_s_s_array(T)) { S.cache = T; return true; }
 104         Word secondarySupers = s.readWord(secondarySupersOffset(INJECTED_VMCONFIG), SECONDARY_SUPERS_LOCATION);
 105         int length = secondarySupers.readInt(metaspaceArrayLengthOffset(INJECTED_VMCONFIG), METASPACE_ARRAY_LENGTH_LOCATION);
 106         for (int i = 0; i < length; i++) {
 107             if (probability(NOT_LIKELY_PROBABILITY, t.equal(loadSecondarySupersElement(secondarySupers, i)))) {
 108                 s.writeKlassPointer(secondarySuperCacheOffset(INJECTED_VMCONFIG), t, SECONDARY_SUPER_CACHE_LOCATION);
 109                 secondariesHit.inc();
 110                 return true;
 111             }
 112         }
 113         secondariesMiss.inc();
 114         return false;
 115     }
 116 
 117     /**
 118      * A set of type check hints ordered by decreasing probabilities.
 119      */
 120     public static class Hints {
 121 
 122         /**
 123          * The hubs of the hint types.
 124          */
 125         public final ConstantNode[] hubs;
 126 
 127         /**
 128          * A predicate over {@link #hubs} specifying whether the corresponding hint type is a
 129          * sub-type of the checked type.
 130          */
 131         public final boolean[] isPositive;
 132 
 133         Hints(ConstantNode[] hints, boolean[] hintIsPositive) {
 134             this.hubs = hints;
 135             this.isPositive = hintIsPositive;
 136         }
 137     }
 138 
 139     static Hints createHints(TypeCheckHints hints, MetaAccessProvider metaAccess, boolean positiveOnly, StructuredGraph graph) {
 140         ConstantNode[] hubs = new ConstantNode[hints.hints.length];
 141         boolean[] isPositive = new boolean[hints.hints.length];
 142         int index = 0;
 143         for (int i = 0; i < hubs.length; i++) {
 144             if (!positiveOnly || hints.hints[i].positive) {
 145                 hubs[index] = ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), ((HotSpotResolvedObjectType) hints.hints[i].type).klass(), metaAccess, graph);
 146                 isPositive[index] = hints.hints[i].positive;
 147                 index++;
 148             }
 149         }
 150         if (positiveOnly && index != hubs.length) {
 151             assert index < hubs.length;
 152             hubs = Arrays.copyOf(hubs, index);
 153             isPositive = Arrays.copyOf(isPositive, index);
 154         }
 155         return new Hints(hubs, isPositive);
 156     }
 157 
 158     static KlassPointer loadSecondarySupersElement(Word metaspaceArray, int index) {
 159         return KlassPointer.fromWord(metaspaceArray.readWord(metaspaceArrayBaseOffset(INJECTED_VMCONFIG) + index * wordSize(), SECONDARY_SUPERS_ELEMENT_LOCATION));
 160     }
 161 
 162     private static final SnippetCounter.Group counters = SnippetCounters.getValue() ? new SnippetCounter.Group("TypeCheck") : null;
 163     static final SnippetCounter hintsHit = new SnippetCounter(counters, "hintsHit", "hit a hint type");
 164     static final SnippetCounter hintsMiss = new SnippetCounter(counters, "hintsMiss", "missed a hint type");
 165     static final SnippetCounter exactHit = new SnippetCounter(counters, "exactHit", "exact type test succeeded");
 166     static final SnippetCounter exactMiss = new SnippetCounter(counters, "exactMiss", "exact type test failed");
 167     static final SnippetCounter isNull = new SnippetCounter(counters, "isNull", "object tested was null");
 168     static final SnippetCounter cacheHit = new SnippetCounter(counters, "cacheHit", "secondary type cache hit");
 169     static final SnippetCounter secondariesHit = new SnippetCounter(counters, "secondariesHit", "secondaries scan succeeded");
 170     static final SnippetCounter secondariesMiss = new SnippetCounter(counters, "secondariesMiss", "secondaries scan failed");
 171     static final SnippetCounter displayHit = new SnippetCounter(counters, "displayHit", "primary type test succeeded");
 172     static final SnippetCounter displayMiss = new SnippetCounter(counters, "displayMiss", "primary type test failed");
 173     static final SnippetCounter T_equals_S = new SnippetCounter(counters, "T_equals_S", "object type was equal to secondary type");
 174 
 175 }