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