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