1 /* 2 * Copyright (c) 2012, 2015, 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 jdk.vm.ci.meta.DeoptimizationAction.InvalidateReprofile; 26 import static jdk.vm.ci.meta.DeoptimizationReason.OptimizedTypeCheckViolated; 27 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; 28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.PRIMARY_SUPERS_LOCATION; 29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.SECONDARY_SUPER_CACHE_LOCATION; 30 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.loadHubIntrinsic; 31 import static org.graalvm.compiler.hotspot.replacements.HotspotSnippetsOptions.TypeCheckMaxHints; 32 import static org.graalvm.compiler.hotspot.replacements.HotspotSnippetsOptions.TypeCheckMinProfileHitProbability; 33 import static org.graalvm.compiler.hotspot.replacements.TypeCheckSnippetUtils.checkSecondarySubType; 34 import static org.graalvm.compiler.hotspot.replacements.TypeCheckSnippetUtils.checkUnknownSubType; 35 import static org.graalvm.compiler.hotspot.replacements.TypeCheckSnippetUtils.createHints; 36 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.LIKELY_PROBABILITY; 37 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.NOT_FREQUENT_PROBABILITY; 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 org.graalvm.compiler.api.replacements.Snippet; 42 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter; 43 import org.graalvm.compiler.api.replacements.Snippet.NonNullParameter; 44 import org.graalvm.compiler.api.replacements.Snippet.VarargsParameter; 45 import org.graalvm.compiler.core.common.type.ObjectStamp; 46 import org.graalvm.compiler.core.common.type.StampFactory; 47 import org.graalvm.compiler.debug.GraalError; 48 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 49 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp; 50 import org.graalvm.compiler.hotspot.replacements.TypeCheckSnippetUtils.Counters; 51 import org.graalvm.compiler.hotspot.replacements.TypeCheckSnippetUtils.Hints; 52 import org.graalvm.compiler.hotspot.word.KlassPointer; 53 import org.graalvm.compiler.nodes.ConstantNode; 54 import org.graalvm.compiler.nodes.DeoptimizeNode; 55 import org.graalvm.compiler.nodes.PiNode; 56 import org.graalvm.compiler.nodes.SnippetAnchorNode; 57 import org.graalvm.compiler.nodes.StructuredGraph; 58 import org.graalvm.compiler.nodes.TypeCheckHints; 59 import org.graalvm.compiler.nodes.ValueNode; 60 import org.graalvm.compiler.nodes.extended.BranchProbabilityNode; 61 import org.graalvm.compiler.nodes.extended.GuardingNode; 62 import org.graalvm.compiler.nodes.java.ClassIsAssignableFromNode; 63 import org.graalvm.compiler.nodes.java.InstanceOfDynamicNode; 64 import org.graalvm.compiler.nodes.java.InstanceOfNode; 65 import org.graalvm.compiler.nodes.spi.LoweringTool; 66 import org.graalvm.compiler.options.OptionValues; 67 import org.graalvm.compiler.replacements.InstanceOfSnippetsTemplates; 68 import org.graalvm.compiler.replacements.SnippetCounter; 69 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments; 70 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo; 71 import org.graalvm.compiler.replacements.Snippets; 72 import org.graalvm.compiler.replacements.nodes.ExplodeLoopNode; 73 74 import jdk.vm.ci.code.TargetDescription; 75 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 76 import jdk.vm.ci.meta.Assumptions; 77 import jdk.vm.ci.meta.DeoptimizationAction; 78 import jdk.vm.ci.meta.DeoptimizationReason; 79 import jdk.vm.ci.meta.JavaKind; 80 import jdk.vm.ci.meta.JavaTypeProfile; 81 import jdk.vm.ci.meta.TriState; 82 83 /** 84 * Snippets used for implementing the type test of an instanceof instruction. Since instanceof is a 85 * floating node, it is lowered separately for each of its usages. 86 * 87 * The type tests implemented are described in the paper 88 * <a href="http://dl.acm.org/citation.cfm?id=583821"> Fast subtype checking in the HotSpot JVM</a> 89 * by Cliff Click and John Rose. 90 */ 91 public class InstanceOfSnippets implements Snippets { 92 93 /** 94 * A test against a set of hints derived from a profile with 100% precise coverage of seen 95 * types. This snippet deoptimizes on hint miss paths. 96 */ 97 @Snippet 98 public static Object instanceofWithProfile(Object object, @VarargsParameter KlassPointer[] hints, @VarargsParameter boolean[] hintIsPositive, Object trueValue, Object falseValue, 99 @ConstantParameter boolean nullSeen, @ConstantParameter Counters counters) { 100 if (probability(NOT_FREQUENT_PROBABILITY, object == null)) { 101 counters.isNull.inc(); 102 if (!nullSeen) { 103 // See comment below for other deoptimization path; the 104 // same reasoning applies here. 105 DeoptimizeNode.deopt(InvalidateReprofile, OptimizedTypeCheckViolated); 106 } 107 return falseValue; 108 } 109 GuardingNode anchorNode = SnippetAnchorNode.anchor(); 110 KlassPointer objectHub = loadHubIntrinsic(PiNode.piCastNonNull(object, anchorNode)); 111 // if we get an exact match: succeed immediately 112 ExplodeLoopNode.explodeLoop(); 113 for (int i = 0; i < hints.length; i++) { 114 KlassPointer hintHub = hints[i]; 115 boolean positive = hintIsPositive[i]; 116 if (probability(LIKELY_PROBABILITY, hintHub.equal(objectHub))) { 117 counters.hintsHit.inc(); 118 return positive ? trueValue : falseValue; 119 } 120 counters.hintsMiss.inc(); 121 } 122 // This maybe just be a rare event but it might also indicate a phase change 123 // in the application. Ideally we want to use DeoptimizationAction.None for 124 // the former but the cost is too high if indeed it is the latter. As such, 125 // we defensively opt for InvalidateReprofile. 126 DeoptimizeNode.deopt(DeoptimizationAction.InvalidateReprofile, OptimizedTypeCheckViolated); 127 return falseValue; 128 } 129 130 /** 131 * A test against a final type. 132 */ 133 @Snippet 134 public static Object instanceofExact(Object object, KlassPointer exactHub, Object trueValue, Object falseValue, @ConstantParameter Counters counters) { 135 if (probability(NOT_FREQUENT_PROBABILITY, object == null)) { 136 counters.isNull.inc(); 137 return falseValue; 138 } 139 GuardingNode anchorNode = SnippetAnchorNode.anchor(); 140 KlassPointer objectHub = loadHubIntrinsic(PiNode.piCastNonNull(object, anchorNode)); 141 if (probability(LIKELY_PROBABILITY, objectHub.notEqual(exactHub))) { 142 counters.exactMiss.inc(); 143 return falseValue; 144 } 145 counters.exactHit.inc(); 146 return trueValue; 147 } 148 149 /** 150 * A test against a primary type. 151 */ 152 @Snippet 153 public static Object instanceofPrimary(KlassPointer hub, Object object, @ConstantParameter int superCheckOffset, Object trueValue, Object falseValue, @ConstantParameter Counters counters) { 154 if (probability(NOT_FREQUENT_PROBABILITY, object == null)) { 155 counters.isNull.inc(); 156 return falseValue; 157 } 158 GuardingNode anchorNode = SnippetAnchorNode.anchor(); 159 KlassPointer objectHub = loadHubIntrinsic(PiNode.piCastNonNull(object, anchorNode)); 160 if (probability(NOT_LIKELY_PROBABILITY, objectHub.readKlassPointer(superCheckOffset, PRIMARY_SUPERS_LOCATION).notEqual(hub))) { 161 counters.displayMiss.inc(); 162 return falseValue; 163 } 164 counters.displayHit.inc(); 165 return trueValue; 166 } 167 168 /** 169 * A test against a restricted secondary type type. 170 */ 171 @Snippet 172 public static Object instanceofSecondary(KlassPointer hub, Object object, @VarargsParameter KlassPointer[] hints, @VarargsParameter boolean[] hintIsPositive, Object trueValue, Object falseValue, 173 @ConstantParameter Counters counters) { 174 if (probability(NOT_FREQUENT_PROBABILITY, object == null)) { 175 counters.isNull.inc(); 176 return falseValue; 177 } 178 GuardingNode anchorNode = SnippetAnchorNode.anchor(); 179 KlassPointer objectHub = loadHubIntrinsic(PiNode.piCastNonNull(object, anchorNode)); 180 // if we get an exact match: succeed immediately 181 ExplodeLoopNode.explodeLoop(); 182 for (int i = 0; i < hints.length; i++) { 183 KlassPointer hintHub = hints[i]; 184 boolean positive = hintIsPositive[i]; 185 if (probability(NOT_FREQUENT_PROBABILITY, hintHub.equal(objectHub))) { 186 counters.hintsHit.inc(); 187 return positive ? trueValue : falseValue; 188 } 189 } 190 counters.hintsMiss.inc(); 191 if (!checkSecondarySubType(hub, objectHub, counters)) { 192 return falseValue; 193 } 194 return trueValue; 195 } 196 197 /** 198 * Type test used when the type being tested against is not known at compile time. 199 */ 200 @Snippet 201 public static Object instanceofDynamic(KlassPointer hub, Object object, Object trueValue, Object falseValue, @ConstantParameter boolean allowNull, @ConstantParameter Counters counters) { 202 if (probability(NOT_FREQUENT_PROBABILITY, object == null)) { 203 counters.isNull.inc(); 204 if (allowNull) { 205 return trueValue; 206 } else { 207 return falseValue; 208 } 209 } 210 GuardingNode anchorNode = SnippetAnchorNode.anchor(); 211 KlassPointer nonNullObjectHub = loadHubIntrinsic(PiNode.piCastNonNull(object, anchorNode)); 212 // The hub of a primitive type can be null => always return false in this case. 213 if (BranchProbabilityNode.probability(BranchProbabilityNode.FAST_PATH_PROBABILITY, !hub.isNull())) { 214 if (checkUnknownSubType(hub, nonNullObjectHub, counters)) { 215 return trueValue; 216 } 217 } 218 return falseValue; 219 } 220 221 @Snippet 222 public static Object isAssignableFrom(@NonNullParameter Class<?> thisClassNonNull, Class<?> otherClass, Object trueValue, Object falseValue, @ConstantParameter Counters counters) { 223 if (otherClass == null) { 224 DeoptimizeNode.deopt(DeoptimizationAction.InvalidateReprofile, DeoptimizationReason.NullCheckException); 225 return false; 226 } 227 GuardingNode anchorNode = SnippetAnchorNode.anchor(); 228 Class<?> otherClassNonNull = PiNode.piCastNonNullClass(otherClass, anchorNode); 229 230 if (BranchProbabilityNode.probability(BranchProbabilityNode.NOT_LIKELY_PROBABILITY, thisClassNonNull == otherClassNonNull)) { 231 return trueValue; 232 } 233 234 KlassPointer thisHub = ClassGetHubNode.readClass(thisClassNonNull); 235 KlassPointer otherHub = ClassGetHubNode.readClass(otherClassNonNull); 236 if (BranchProbabilityNode.probability(BranchProbabilityNode.FAST_PATH_PROBABILITY, !thisHub.isNull())) { 237 if (BranchProbabilityNode.probability(BranchProbabilityNode.FAST_PATH_PROBABILITY, !otherHub.isNull())) { 238 GuardingNode guardNonNull = SnippetAnchorNode.anchor(); 239 KlassPointer nonNullOtherHub = ClassGetHubNode.piCastNonNull(otherHub, guardNonNull); 240 if (TypeCheckSnippetUtils.checkUnknownSubType(thisHub, nonNullOtherHub, counters)) { 241 return trueValue; 242 } 243 } 244 } 245 246 // If either hub is null, one of them is a primitive type and given that the class is not 247 // equal, return false. 248 return falseValue; 249 } 250 251 public static class Templates extends InstanceOfSnippetsTemplates { 252 253 private final SnippetInfo instanceofWithProfile = snippet(InstanceOfSnippets.class, "instanceofWithProfile"); 254 private final SnippetInfo instanceofExact = snippet(InstanceOfSnippets.class, "instanceofExact"); 255 private final SnippetInfo instanceofPrimary = snippet(InstanceOfSnippets.class, "instanceofPrimary"); 256 private final SnippetInfo instanceofSecondary = snippet(InstanceOfSnippets.class, "instanceofSecondary", SECONDARY_SUPER_CACHE_LOCATION); 257 private final SnippetInfo instanceofDynamic = snippet(InstanceOfSnippets.class, "instanceofDynamic", SECONDARY_SUPER_CACHE_LOCATION); 258 private final SnippetInfo isAssignableFrom = snippet(InstanceOfSnippets.class, "isAssignableFrom", SECONDARY_SUPER_CACHE_LOCATION); 259 260 private final Counters counters; 261 262 public Templates(OptionValues options, SnippetCounter.Group.Factory factory, HotSpotProviders providers, TargetDescription target) { 263 super(options, providers, providers.getSnippetReflection(), target); 264 this.counters = new Counters(factory); 265 } 266 267 @Override 268 protected Arguments makeArguments(InstanceOfUsageReplacer replacer, LoweringTool tool) { 269 if (replacer.instanceOf instanceof InstanceOfNode) { 270 InstanceOfNode instanceOf = (InstanceOfNode) replacer.instanceOf; 271 ValueNode object = instanceOf.getValue(); 272 Assumptions assumptions = instanceOf.graph().getAssumptions(); 273 274 OptionValues localOptions = instanceOf.getOptions(); 275 JavaTypeProfile profile = instanceOf.profile(); 276 if (GeneratePIC.getValue(localOptions)) { 277 // FIXME: We can't embed constants in hints. We can't really load them from GOT 278 // either. Hard problem. 279 profile = null; 280 } 281 TypeCheckHints hintInfo = new TypeCheckHints(instanceOf.type(), profile, assumptions, TypeCheckMinProfileHitProbability.getValue(localOptions), 282 TypeCheckMaxHints.getValue(localOptions)); 283 final HotSpotResolvedObjectType type = (HotSpotResolvedObjectType) instanceOf.type().getType(); 284 ConstantNode hub = ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), type.klass(), providers.getMetaAccess(), instanceOf.graph()); 285 286 Arguments args; 287 288 StructuredGraph graph = instanceOf.graph(); 289 if (hintInfo.hintHitProbability >= 1.0 && hintInfo.exact == null) { 290 Hints hints = createHints(hintInfo, providers.getMetaAccess(), false, graph); 291 args = new Arguments(instanceofWithProfile, graph.getGuardsStage(), tool.getLoweringStage()); 292 args.add("object", object); 293 args.addVarargs("hints", KlassPointer.class, KlassPointerStamp.klassNonNull(), hints.hubs); 294 args.addVarargs("hintIsPositive", boolean.class, StampFactory.forKind(JavaKind.Boolean), hints.isPositive); 295 } else if (hintInfo.exact != null) { 296 args = new Arguments(instanceofExact, graph.getGuardsStage(), tool.getLoweringStage()); 297 args.add("object", object); 298 args.add("exactHub", ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), ((HotSpotResolvedObjectType) hintInfo.exact).klass(), providers.getMetaAccess(), graph)); 299 } else if (type.isPrimaryType()) { 300 args = new Arguments(instanceofPrimary, graph.getGuardsStage(), tool.getLoweringStage()); 301 args.add("hub", hub); 302 args.add("object", object); 303 args.addConst("superCheckOffset", type.superCheckOffset()); 304 } else { 305 Hints hints = createHints(hintInfo, providers.getMetaAccess(), false, graph); 306 args = new Arguments(instanceofSecondary, graph.getGuardsStage(), tool.getLoweringStage()); 307 args.add("hub", hub); 308 args.add("object", object); 309 args.addVarargs("hints", KlassPointer.class, KlassPointerStamp.klassNonNull(), hints.hubs); 310 args.addVarargs("hintIsPositive", boolean.class, StampFactory.forKind(JavaKind.Boolean), hints.isPositive); 311 } 312 args.add("trueValue", replacer.trueValue); 313 args.add("falseValue", replacer.falseValue); 314 if (hintInfo.hintHitProbability >= 1.0 && hintInfo.exact == null) { 315 args.addConst("nullSeen", hintInfo.profile.getNullSeen() != TriState.FALSE); 316 } 317 args.addConst("counters", counters); 318 return args; 319 } else if (replacer.instanceOf instanceof InstanceOfDynamicNode) { 320 InstanceOfDynamicNode instanceOf = (InstanceOfDynamicNode) replacer.instanceOf; 321 ValueNode object = instanceOf.getObject(); 322 323 Arguments args = new Arguments(instanceofDynamic, instanceOf.graph().getGuardsStage(), tool.getLoweringStage()); 324 args.add("hub", instanceOf.getMirrorOrHub()); 325 args.add("object", object); 326 args.add("trueValue", replacer.trueValue); 327 args.add("falseValue", replacer.falseValue); 328 args.addConst("allowNull", instanceOf.allowsNull()); 329 args.addConst("counters", counters); 330 return args; 331 } else if (replacer.instanceOf instanceof ClassIsAssignableFromNode) { 332 ClassIsAssignableFromNode isAssignable = (ClassIsAssignableFromNode) replacer.instanceOf; 333 Arguments args = new Arguments(isAssignableFrom, isAssignable.graph().getGuardsStage(), tool.getLoweringStage()); 334 assert ((ObjectStamp) isAssignable.getThisClass().stamp()).nonNull(); 335 args.add("thisClassNonNull", isAssignable.getThisClass()); 336 args.add("otherClass", isAssignable.getOtherClass()); 337 args.add("trueValue", replacer.trueValue); 338 args.add("falseValue", replacer.falseValue); 339 args.addConst("counters", counters); 340 return args; 341 } else { 342 throw GraalError.shouldNotReachHere(); 343 } 344 } 345 } 346 }