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.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG;
  26 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.KLASS_SUPER_CHECK_OFFSET_LOCATION;
  27 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.METASPACE_ARRAY_LENGTH_LOCATION;
  28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.PRIMARY_SUPERS_LOCATION;
  29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPERS_ELEMENT_LOCATION;
  30 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPERS_LOCATION;
  31 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPER_CACHE_LOCATION;
  32 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.metaspaceArrayBaseOffset;
  33 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.metaspaceArrayLengthOffset;
  34 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.secondarySuperCacheOffset;
  35 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.secondarySupersOffset;
  36 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.superCheckOffsetOffset;
  37 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.wordSize;
  38 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.NOT_LIKELY_PROBABILITY;
  39 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability;
  40 
  41 import java.util.Arrays;
  42 
  43 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp;
  44 import org.graalvm.compiler.hotspot.word.KlassPointer;
  45 import org.graalvm.compiler.nodes.ConstantNode;
  46 import org.graalvm.compiler.nodes.StructuredGraph;
  47 import org.graalvm.compiler.nodes.TypeCheckHints;
  48 import org.graalvm.compiler.replacements.SnippetCounter;
  49 import org.graalvm.compiler.replacements.SnippetCounter.Group;
  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 sNonNull, Counters counters) {
  63         // if (S.cache == T) return true
  64         if (sNonNull.readKlassPointer(secondarySuperCacheOffset(INJECTED_VMCONFIG), SECONDARY_SUPER_CACHE_LOCATION).equal(t)) {
  65             counters.cacheHit.inc();
  66             return true;
  67         }
  68 
  69         return checkSelfAndSupers(t, sNonNull, counters);
  70     }
  71 
  72     static boolean checkUnknownSubType(KlassPointer t, KlassPointer sNonNull, Counters counters) {
  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 (sNonNull.readKlassPointer(superCheckOffset, PRIMARY_SUPERS_LOCATION).equal(t)) {
  79             if (primary) {
  80                 counters.cacheHit.inc();
  81             } else {
  82                 counters.displayHit.inc();
  83             }
  84             return true;
  85         }
  86 
  87         // if (off != &cache) return false
  88         if (primary) {
  89             counters.displayMiss.inc();
  90             return false;
  91         }
  92 
  93         return checkSelfAndSupers(t, sNonNull, counters);
  94     }
  95 
  96     private static boolean checkSelfAndSupers(KlassPointer t, KlassPointer s, Counters counters) {
  97         // if (T == S) return true
  98         if (s.equal(t)) {
  99             counters.equalsSecondary.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                 counters.secondariesHit.inc();
 110                 return true;
 111             }
 112         }
 113         counters.secondariesMiss.inc();
 114         return false;
 115     }
 116 
 117     static class Counters {
 118         final SnippetCounter hintsHit;
 119         final SnippetCounter hintsMiss;
 120         final SnippetCounter exactHit;
 121         final SnippetCounter exactMiss;
 122         final SnippetCounter isNull;
 123         final SnippetCounter cacheHit;
 124         final SnippetCounter secondariesHit;
 125         final SnippetCounter secondariesMiss;
 126         final SnippetCounter displayHit;
 127         final SnippetCounter displayMiss;
 128         final SnippetCounter equalsSecondary;
 129 
 130         Counters(SnippetCounter.Group.Factory factory) {
 131             Group group = factory.createSnippetCounterGroup("TypeCheck");
 132             hintsHit = new SnippetCounter(group, "hintsHit", "hit a hint type");
 133             hintsMiss = new SnippetCounter(group, "hintsMiss", "missed a hint type");
 134             exactHit = new SnippetCounter(group, "exactHit", "exact type test succeeded");
 135             exactMiss = new SnippetCounter(group, "exactMiss", "exact type test failed");
 136             isNull = new SnippetCounter(group, "isNull", "object tested was null");
 137             cacheHit = new SnippetCounter(group, "cacheHit", "secondary type cache hit");
 138             secondariesHit = new SnippetCounter(group, "secondariesHit", "secondaries scan succeeded");
 139             secondariesMiss = new SnippetCounter(group, "secondariesMiss", "secondaries scan failed");
 140             displayHit = new SnippetCounter(group, "displayHit", "primary type test succeeded");
 141             displayMiss = new SnippetCounter(group, "displayMiss", "primary type test failed");
 142             equalsSecondary = new SnippetCounter(group, "T_equals_S", "object type was equal to secondary type");
 143         }
 144     }
 145 
 146     /**
 147      * A set of type check hints ordered by decreasing probabilities.
 148      */
 149     public static class Hints {
 150 
 151         /**
 152          * The hubs of the hint types.
 153          */
 154         public final ConstantNode[] hubs;
 155 
 156         /**
 157          * A predicate over {@link #hubs} specifying whether the corresponding hint type is a
 158          * sub-type of the checked type.
 159          */
 160         public final boolean[] isPositive;
 161 
 162         Hints(ConstantNode[] hints, boolean[] hintIsPositive) {
 163             this.hubs = hints;
 164             this.isPositive = hintIsPositive;
 165         }
 166     }
 167 
 168     static Hints createHints(TypeCheckHints hints, MetaAccessProvider metaAccess, boolean positiveOnly, StructuredGraph graph) {
 169         ConstantNode[] hubs = new ConstantNode[hints.hints.length];
 170         boolean[] isPositive = new boolean[hints.hints.length];
 171         int index = 0;
 172         for (int i = 0; i < hubs.length; i++) {
 173             if (!positiveOnly || hints.hints[i].positive) {
 174                 hubs[index] = ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), ((HotSpotResolvedObjectType) hints.hints[i].type).klass(), metaAccess, graph);
 175                 isPositive[index] = hints.hints[i].positive;
 176                 index++;
 177             }
 178         }
 179         if (positiveOnly && index != hubs.length) {
 180             assert index < hubs.length;
 181             hubs = Arrays.copyOf(hubs, index);
 182             isPositive = Arrays.copyOf(isPositive, index);
 183         }
 184         return new Hints(hubs, isPositive);
 185     }
 186 
 187     static KlassPointer loadSecondarySupersElement(Word metaspaceArray, int index) {
 188         return KlassPointer.fromWord(metaspaceArray.readWord(metaspaceArrayBaseOffset(INJECTED_VMCONFIG) + index * wordSize(), SECONDARY_SUPERS_ELEMENT_LOCATION));
 189     }
 190 }