TypeCheckSnippetUtils.java revision 12651:6ef01bd40ce2
1187063Srwatson/* 2187063Srwatson * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 3187063Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4187063Srwatson * 5187063Srwatson * This code is free software; you can redistribute it and/or modify it 6187063Srwatson * under the terms of the GNU General Public License version 2 only, as 7187063Srwatson * published by the Free Software Foundation. 8187063Srwatson * 9187063Srwatson * This code is distributed in the hope that it will be useful, but WITHOUT 10187063Srwatson * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11187063Srwatson * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12187063Srwatson * version 2 for more details (a copy is included in the LICENSE file that 13187063Srwatson * accompanied this code). 14187063Srwatson * 15187063Srwatson * You should have received a copy of the GNU General Public License version 16187063Srwatson * 2 along with this work; if not, write to the Free Software Foundation, 17187063Srwatson * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18187063Srwatson * 19187063Srwatson * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20187063Srwatson * or visit www.oracle.com if you need additional information or have any 21187063Srwatson * questions. 22187063Srwatson */ 23187063Srwatsonpackage org.graalvm.compiler.hotspot.replacements; 24187063Srwatson 25187063Srwatsonimport static org.graalvm.compiler.core.common.GraalOptions.SnippetCounters; 26187063Srwatsonimport static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG; 27187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.KLASS_SUPER_CHECK_OFFSET_LOCATION; 28187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.METASPACE_ARRAY_LENGTH_LOCATION; 29187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.PRIMARY_SUPERS_LOCATION; 30187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPERS_ELEMENT_LOCATION; 31187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPERS_LOCATION; 32187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPER_CACHE_LOCATION; 33187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.metaspaceArrayBaseOffset; 34187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.metaspaceArrayLengthOffset; 35187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.secondarySuperCacheOffset; 36187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.secondarySupersOffset; 37187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.superCheckOffsetOffset; 38187063Srwatsonimport static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.wordSize; 39187063Srwatsonimport static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.NOT_LIKELY_PROBABILITY; 40187063Srwatsonimport static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability; 41187063Srwatson 42187063Srwatsonimport java.util.Arrays; 43187063Srwatson 44187063Srwatsonimport org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp; 45187063Srwatsonimport org.graalvm.compiler.hotspot.word.KlassPointer; 46187063Srwatsonimport org.graalvm.compiler.nodes.ConstantNode; 47187063Srwatsonimport org.graalvm.compiler.nodes.StructuredGraph; 48187063Srwatsonimport org.graalvm.compiler.nodes.TypeCheckHints; 49187063Srwatsonimport org.graalvm.compiler.replacements.SnippetCounter; 50187063Srwatsonimport org.graalvm.compiler.word.Word; 51187063Srwatson 52187063Srwatsonimport jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 53187063Srwatsonimport jdk.vm.ci.meta.MetaAccessProvider; 54187063Srwatson 55187063Srwatson//JaCoCo Exclude 56187063Srwatson 57187063Srwatson/** 58187063Srwatson * Utilities and common code paths used by the type check snippets. 59187063Srwatson */ 60187063Srwatsonpublic class TypeCheckSnippetUtils { 61187063Srwatson 62187063Srwatson static boolean checkSecondarySubType(KlassPointer t, KlassPointer s) { 63187063Srwatson // if (S.cache == T) return true 64187063Srwatson if (s.readKlassPointer(secondarySuperCacheOffset(INJECTED_VMCONFIG), SECONDARY_SUPER_CACHE_LOCATION).equal(t)) { 65187063Srwatson cacheHit.inc(); 66187063Srwatson return true; 67187063Srwatson } 68187063Srwatson 69187063Srwatson return checkSelfAndSupers(t, s); 70187063Srwatson } 71187063Srwatson 72187063Srwatson static boolean checkUnknownSubType(KlassPointer t, KlassPointer s) { 73187063Srwatson // int off = T.offset 74187063Srwatson int superCheckOffset = t.readInt(superCheckOffsetOffset(INJECTED_VMCONFIG), KLASS_SUPER_CHECK_OFFSET_LOCATION); 75187063Srwatson boolean primary = superCheckOffset != secondarySuperCacheOffset(INJECTED_VMCONFIG); 76187063Srwatson 77187063Srwatson // if (T = S[off]) return true 78187063Srwatson if (s.readKlassPointer(superCheckOffset, PRIMARY_SUPERS_LOCATION).equal(t)) { 79187063Srwatson if (primary) { 80187063Srwatson cacheHit.inc(); 81187063Srwatson } else { 82187063Srwatson displayHit.inc(); 83187063Srwatson } 84187063Srwatson return true; 85187063Srwatson } 86187063Srwatson 87187063Srwatson // if (off != &cache) return false 88187063Srwatson if (primary) { 89187063Srwatson displayMiss.inc(); 90187063Srwatson return false; 91187063Srwatson } 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} 176