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