1 /*
   2  * Copyright (c) 2011, 2019, 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.AbstractPointerStamp;
  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, or null if the object is null.
  55  */
  56 @NodeInfo(cycles = CYCLES_2, size = SIZE_1)
  57 public final class LoadHubOrNullNode extends FloatingNode implements Lowerable, Canonicalizable, Virtualizable {
  58 
  59     public static final NodeClass<LoadHubOrNullNode> TYPE = NodeClass.create(LoadHubOrNullNode.class);
  60     @Input ValueNode value;
  61 
  62     public ValueNode getValue() {
  63         return value;
  64     }
  65 
  66     private static AbstractPointerStamp hubStamp(StampProvider stampProvider, ValueNode value) {
  67         assert value.stamp(NodeView.DEFAULT) instanceof ObjectStamp;
  68         return stampProvider.createHubStamp(((ObjectStamp) value.stamp(NodeView.DEFAULT))).asMaybeNull();
  69     }
  70 
  71     public static ValueNode create(ValueNode value, StampProvider stampProvider, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
  72         AbstractPointerStamp stamp = hubStamp(stampProvider, value);
  73         return create(value, stamp, metaAccess, constantReflection);
  74     }
  75 
  76     public static ValueNode create(ValueNode value, AbstractPointerStamp stamp, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
  77         ValueNode synonym = findSynonym(value, stamp, metaAccess, constantReflection);
  78         if (synonym != null) {
  79             return synonym;
  80         }
  81         return new LoadHubOrNullNode(stamp, value);
  82     }
  83 
  84     public LoadHubOrNullNode(@InjectedNodeParameter StampProvider stampProvider, ValueNode value) {
  85         this(hubStamp(stampProvider, value), value);
  86     }
  87 
  88     public LoadHubOrNullNode(Stamp stamp, ValueNode value) {
  89         super(TYPE, stamp);
  90         this.value = value;
  91     }
  92 
  93     @Override
  94     public void lower(LoweringTool tool) {
  95         tool.getLowerer().lower(this, tool);
  96     }
  97 
  98     @Override
  99     public ValueNode canonical(CanonicalizerTool tool) {
 100         if (!GeneratePIC.getValue(tool.getOptions())) {
 101             NodeView view = NodeView.from(tool);
 102             MetaAccessProvider metaAccess = tool.getMetaAccess();
 103             ValueNode curValue = getValue();
 104             ValueNode newNode = findSynonym(curValue, (AbstractPointerStamp) stamp(view), metaAccess, tool.getConstantReflection());
 105             if (newNode != null) {
 106                 return newNode;
 107             }
 108         }
 109         return this;
 110     }
 111 
 112     public static ValueNode findSynonym(ValueNode curValue, AbstractPointerStamp stamp, MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
 113         if (StampTool.isPointerNonNull(stamp)) {
 114             return LoadHubNode.create(curValue, stamp.asNonNull(), metaAccess, constantReflection);
 115         }
 116         return null;
 117     }
 118 
 119     @Override
 120     public void virtualize(VirtualizerTool tool) {
 121         if (!GeneratePIC.getValue(tool.getOptions())) {
 122             ValueNode alias = tool.getAlias(getValue());
 123             TypeReference type = StampTool.typeReferenceOrNull(alias);
 124             if (type != null && type.isExact()) {
 125                 tool.replaceWithValue(ConstantNode.forConstant(stamp(NodeView.DEFAULT), tool.getConstantReflection().asObjectHub(type.getType()), tool.getMetaAccess(), graph()));
 126             }
 127         }
 128     }
 129 }