1 /* 2 * Copyright (c) 2016, 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.meta.HotSpotForeignCallsProviderImpl.IDENTITY_HASHCODE; 27 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.biasedLockMaskInPlace; 28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.identityHashCode; 29 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.identityHashCodeShift; 30 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.loadWordFromObject; 31 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.markOffset; 32 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.uninitializedIdentityHashCodeValue; 33 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.unlockedMask; 34 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.FAST_PATH_PROBABILITY; 35 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.NOT_FREQUENT_PROBABILITY; 36 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability; 37 38 import org.graalvm.compiler.api.replacements.Snippet; 39 import org.graalvm.compiler.debug.DebugHandlersFactory; 40 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 41 import org.graalvm.compiler.nodes.StructuredGraph; 42 import org.graalvm.compiler.nodes.spi.LoweringTool; 43 import org.graalvm.compiler.options.OptionValues; 44 import org.graalvm.compiler.replacements.SnippetTemplate; 45 import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates; 46 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments; 47 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo; 48 import org.graalvm.compiler.replacements.Snippets; 49 import org.graalvm.compiler.word.Word; 50 import org.graalvm.word.WordFactory; 51 52 import jdk.vm.ci.code.TargetDescription; 53 54 public class HashCodeSnippets implements Snippets { 55 56 @Snippet 57 public static int identityHashCodeSnippet(final Object thisObj) { 58 if (probability(NOT_FREQUENT_PROBABILITY, thisObj == null)) { 59 return 0; 60 } 61 return computeHashCode(thisObj); 62 } 63 64 static int computeHashCode(final Object x) { 65 Word mark = loadWordFromObject(x, markOffset(INJECTED_VMCONFIG)); 66 67 // this code is independent from biased locking (although it does not look that way) 68 final Word biasedLock = mark.and(biasedLockMaskInPlace(INJECTED_VMCONFIG)); 69 if (probability(FAST_PATH_PROBABILITY, biasedLock.equal(WordFactory.unsigned(unlockedMask(INJECTED_VMCONFIG))))) { 70 int hash = (int) mark.unsignedShiftRight(identityHashCodeShift(INJECTED_VMCONFIG)).rawValue(); 71 if (probability(FAST_PATH_PROBABILITY, hash != uninitializedIdentityHashCodeValue(INJECTED_VMCONFIG))) { 72 return hash; 73 } 74 } 75 return identityHashCode(IDENTITY_HASHCODE, x); 76 } 77 78 public static class Templates extends AbstractTemplates { 79 80 private final SnippetInfo identityHashCodeSnippet = snippet(HashCodeSnippets.class, "identityHashCodeSnippet", HotSpotReplacementsUtil.MARK_WORD_LOCATION); 81 82 public Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, HotSpotProviders providers, TargetDescription target) { 83 super(options, factories, providers, providers.getSnippetReflection(), target); 84 } 85 86 public void lower(IdentityHashCodeNode node, LoweringTool tool) { 87 StructuredGraph graph = node.graph(); 88 Arguments args = new Arguments(identityHashCodeSnippet, graph.getGuardsStage(), tool.getLoweringStage()); 89 args.add("thisObj", node.object); 90 SnippetTemplate template = template(node.getDebug(), args); 91 template.instantiate(providers.getMetaAccess(), node, SnippetTemplate.DEFAULT_REPLACER, args); 92 } 93 94 } 95 96 }