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.core.common.GraalOptions.ImmutableCode;
  26 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_0;
  27 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_0;
  28 
  29 import org.graalvm.compiler.core.common.LocationIdentity;
  30 import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
  31 import org.graalvm.compiler.core.common.type.StampFactory;
  32 import org.graalvm.compiler.graph.Node;
  33 import org.graalvm.compiler.graph.NodeClass;
  34 import org.graalvm.compiler.graph.spi.Canonicalizable;
  35 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  36 import org.graalvm.compiler.nodeinfo.NodeInfo;
  37 import org.graalvm.compiler.nodes.ConstantNode;
  38 import org.graalvm.compiler.nodes.FixedWithNextNode;
  39 import org.graalvm.compiler.nodes.ValueNode;
  40 import org.graalvm.compiler.nodes.memory.MemoryCheckpoint;
  41 import org.graalvm.compiler.nodes.spi.Lowerable;
  42 import org.graalvm.compiler.nodes.spi.LoweringTool;
  43 
  44 import jdk.vm.ci.hotspot.HotSpotObjectConstant;
  45 import jdk.vm.ci.meta.JavaConstant;
  46 
  47 @NodeInfo(cycles = CYCLES_0, size = SIZE_0)
  48 public class IdentityHashCodeNode extends FixedWithNextNode implements Canonicalizable, Lowerable, MemoryCheckpoint.Single {
  49 
  50     public static final NodeClass<IdentityHashCodeNode> TYPE = NodeClass.create(IdentityHashCodeNode.class);
  51 
  52     @Input ValueNode object;
  53 
  54     public IdentityHashCodeNode(ValueNode object) {
  55         super(TYPE, StampFactory.forInteger(32));
  56         this.object = object;
  57 
  58     }
  59 
  60     @Override
  61     public LocationIdentity getLocationIdentity() {
  62         return HotSpotReplacementsUtil.MARK_WORD_LOCATION;
  63     }
  64 
  65     @Override
  66     public Node canonical(CanonicalizerTool tool) {
  67         if (object.isConstant()) {
  68             assert object.stamp() instanceof AbstractObjectStamp;
  69             JavaConstant c = (JavaConstant) object.asConstant();
  70             if (ImmutableCode.getValue()) {
  71                 return this;
  72             }
  73             JavaConstant identityHashCode = null;
  74             if (c.isNull()) {
  75                 identityHashCode = JavaConstant.forInt(0);
  76             } else {
  77                 identityHashCode = JavaConstant.forInt(((HotSpotObjectConstant) c).getIdentityHashCode());
  78             }
  79 
  80             return new ConstantNode(identityHashCode, StampFactory.forConstant(identityHashCode));
  81         }
  82         return this;
  83     }
  84 
  85     @Override
  86     public void lower(LoweringTool tool) {
  87         tool.getLowerer().lower(this, tool);
  88     }
  89 
  90     @NodeIntrinsic
  91     public static native int identityHashCode(Object object);
  92 
  93 }