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