1 /*
   2  * Copyright (c) 2011, 2018, 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 
  24 
  25 package org.graalvm.compiler.nodes.extended;
  26 
  27 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
  28 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_2;
  29 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  30 
  31 import org.graalvm.compiler.core.common.type.ObjectStamp;
  32 import org.graalvm.compiler.core.common.type.Stamp;
  33 import org.graalvm.compiler.core.common.type.TypeReference;
  34 import org.graalvm.compiler.graph.NodeClass;
  35 import org.graalvm.compiler.graph.spi.Canonicalizable;
  36 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  37 import org.graalvm.compiler.nodeinfo.NodeInfo;
  38 import org.graalvm.compiler.nodes.ConstantNode;
  39 import org.graalvm.compiler.nodes.NodeView;
  40 import org.graalvm.compiler.nodes.ValueNode;
  41 import org.graalvm.compiler.nodes.calc.FloatingNode;
  42 import org.graalvm.compiler.nodes.spi.Lowerable;
  43 import org.graalvm.compiler.nodes.spi.LoweringTool;
  44 import org.graalvm.compiler.nodes.spi.StampProvider;
  45 import org.graalvm.compiler.nodes.spi.Virtualizable;
  46 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  47 import org.graalvm.compiler.nodes.type.StampTool;
  48 
  49 import jdk.vm.ci.meta.ConstantReflectionProvider;
  50 import jdk.vm.ci.meta.MetaAccessProvider;
  51 
  52 /**
  53  * Loads an object's hub. The object is not null-checked by this operation.
  54  */
  55 @NodeInfo(cycles = CYCLES_2, size = SIZE_1)
  56 public final class LoadHubNode extends FloatingNode implements Lowerable, Canonicalizable, Virtualizable {
  57 
  58     public static final NodeClass<LoadHubNode> TYPE = NodeClass.create(LoadHubNode.class);
  59     @Input ValueNode value;
  60 
  61     public ValueNode getValue() {
  62         return value;
  63     }
  64 
  65     private static Stamp hubStamp(StampProvider stampProvider, ValueNode value) {
  66         assert value.stamp(NodeView.DEFAULT) instanceof ObjectStamp;
  67         return stampProvider.createHubStamp(((ObjectStamp) value.stamp(NodeView.DEFAULT)));
  68     }
  69 
  70     public static ValueNode create(ValueNode value, StampProvider stampProvider, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
  71         Stamp stamp = hubStamp(stampProvider, value);
  72         ValueNode synonym = findSynonym(value, stamp, metaAccess, constantReflection);
  73         if (synonym != null) {
  74             return synonym;
  75         }
  76         return new LoadHubNode(stamp, value);
  77     }
  78 
  79     public LoadHubNode(@InjectedNodeParameter StampProvider stampProvider, ValueNode value) {
  80         this(hubStamp(stampProvider, value), value);
  81     }
  82 
  83     public LoadHubNode(Stamp stamp, ValueNode value) {
  84         super(TYPE, stamp);
  85         this.value = value;
  86     }
  87 
  88     @Override
  89     public void lower(LoweringTool tool) {
  90         tool.getLowerer().lower(this, tool);
  91     }
  92 
  93     @Override
  94     public ValueNode canonical(CanonicalizerTool tool) {
  95         if (!GeneratePIC.getValue(tool.getOptions())) {
  96             NodeView view = NodeView.from(tool);
  97             MetaAccessProvider metaAccess = tool.getMetaAccess();
  98             ValueNode curValue = getValue();
  99             ValueNode newNode = findSynonym(curValue, stamp(view), metaAccess, tool.getConstantReflection());
 100             if (newNode != null) {
 101                 return newNode;
 102             }
 103         }
 104         return this;
 105     }
 106 
 107     public static ValueNode findSynonym(ValueNode curValue, Stamp stamp, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
 108         if (!GeneratePIC.getValue(curValue.getOptions())) {
 109             TypeReference type = StampTool.typeReferenceOrNull(curValue);
 110             if (type != null && type.isExact()) {
 111                 return ConstantNode.forConstant(stamp, constantReflection.asObjectHub(type.getType()), metaAccess);
 112             }
 113         }
 114         return null;
 115     }
 116 
 117     @Override
 118     public void virtualize(VirtualizerTool tool) {
 119         if (!GeneratePIC.getValue(tool.getOptions())) {
 120             ValueNode alias = tool.getAlias(getValue());
 121             TypeReference type = StampTool.typeReferenceOrNull(alias);
 122             if (type != null && type.isExact()) {
 123                 tool.replaceWithValue(ConstantNode.forConstant(stamp(NodeView.DEFAULT), tool.getConstantReflection().asObjectHub(type.getType()), tool.getMetaAccess(), graph()));
 124             }
 125         }
 126     }
 127 }