1 /*
   2  * Copyright (c) 2014, 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 package org.graalvm.compiler.hotspot.replacements;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_1;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_1;
  27 
  28 import org.graalvm.compiler.core.common.GraalOptions;
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 import org.graalvm.compiler.core.common.type.TypeReference;
  31 import org.graalvm.compiler.graph.Node;
  32 import org.graalvm.compiler.graph.NodeClass;
  33 import org.graalvm.compiler.graph.spi.Canonicalizable;
  34 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  35 import org.graalvm.compiler.hotspot.word.KlassPointer;
  36 import org.graalvm.compiler.nodeinfo.NodeInfo;
  37 import org.graalvm.compiler.nodes.ConstantNode;
  38 import org.graalvm.compiler.nodes.ValueNode;
  39 import org.graalvm.compiler.nodes.calc.ConvertNode;
  40 import org.graalvm.compiler.nodes.calc.FloatingNode;
  41 import org.graalvm.compiler.nodes.spi.Lowerable;
  42 import org.graalvm.compiler.nodes.spi.LoweringTool;
  43 
  44 import jdk.vm.ci.meta.Constant;
  45 import jdk.vm.ci.meta.ConstantReflectionProvider;
  46 import jdk.vm.ci.meta.JavaConstant;
  47 import jdk.vm.ci.meta.MetaAccessProvider;
  48 import jdk.vm.ci.meta.ResolvedJavaType;
  49 
  50 /**
  51  * Read {@code Klass::_java_mirror} and incorporate non-null type information into stamp. This is
  52  * also used by {@link ClassGetHubNode} to eliminate chains of {@code klass._java_mirror._klass}.
  53  */
  54 @NodeInfo(cycles = CYCLES_1, size = SIZE_1)
  55 public final class HubGetClassNode extends FloatingNode implements Lowerable, Canonicalizable, ConvertNode {
  56     public static final NodeClass<HubGetClassNode> TYPE = NodeClass.create(HubGetClassNode.class);
  57     @Input protected ValueNode hub;
  58 
  59     public HubGetClassNode(@InjectedNodeParameter MetaAccessProvider metaAccess, ValueNode hub) {
  60         super(TYPE, StampFactory.objectNonNull(TypeReference.createWithoutAssumptions(metaAccess.lookupJavaType(Class.class))));
  61         this.hub = hub;
  62     }
  63 
  64     public ValueNode getHub() {
  65         return hub;
  66     }
  67 
  68     @Override
  69     public Node canonical(CanonicalizerTool tool) {
  70         if (tool.allUsagesAvailable() && hasNoUsages()) {
  71             return null;
  72         } else {
  73             MetaAccessProvider metaAccess = tool.getMetaAccess();
  74             if (metaAccess != null && hub.isConstant() && !GraalOptions.ImmutableCode.getValue(tool.getOptions())) {
  75                 ResolvedJavaType exactType = tool.getConstantReflection().asJavaType(hub.asConstant());
  76                 if (exactType != null) {
  77                     return ConstantNode.forConstant(tool.getConstantReflection().asJavaClass(exactType), metaAccess);
  78                 }
  79             }
  80             return this;
  81         }
  82     }
  83 
  84     @Override
  85     public void lower(LoweringTool tool) {
  86         tool.getLowerer().lower(this, tool);
  87     }
  88 
  89     @NodeIntrinsic
  90     public static native Class<?> readClass(KlassPointer hub);
  91 
  92     @Override
  93     public ValueNode getValue() {
  94         return hub;
  95     }
  96 
  97     @Override
  98     public Constant convert(Constant c, ConstantReflectionProvider constantReflection) {
  99         if (JavaConstant.NULL_POINTER.equals(c)) {
 100             return c;
 101         }
 102         return constantReflection.asJavaClass(constantReflection.asJavaType(c));
 103     }
 104 
 105     @Override
 106     public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) {
 107         if (JavaConstant.NULL_POINTER.equals(c)) {
 108             return c;
 109         }
 110         ResolvedJavaType type = constantReflection.asJavaType(c);
 111         if (type.isPrimitive()) {
 112             return JavaConstant.NULL_POINTER;
 113         } else {
 114             return constantReflection.asObjectHub(type);
 115         }
 116     }
 117 
 118     /**
 119      * Any concrete Klass* has a corresponding {@link java.lang.Class}.
 120      */
 121     @Override
 122     public boolean isLossless() {
 123         return true;
 124     }
 125 
 126     @Override
 127     public boolean mayNullCheckSkipConversion() {
 128         return true;
 129     }
 130 }