1 /*
   2  * Copyright (c) 2014, 2016, 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.LocationIdentity;
  29 import org.graalvm.compiler.core.common.calc.Condition;
  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.nodes.type.KlassPointerStamp;
  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.PiNode;
  39 import org.graalvm.compiler.nodes.ValueNode;
  40 import org.graalvm.compiler.nodes.calc.ConvertNode;
  41 import org.graalvm.compiler.nodes.calc.FloatingNode;
  42 import org.graalvm.compiler.nodes.extended.GetClassNode;
  43 import org.graalvm.compiler.nodes.extended.GuardingNode;
  44 import org.graalvm.compiler.nodes.extended.LoadHubNode;
  45 import org.graalvm.compiler.nodes.memory.ReadNode;
  46 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  47 import org.graalvm.compiler.nodes.spi.Lowerable;
  48 import org.graalvm.compiler.nodes.spi.LoweringTool;
  49 
  50 import jdk.vm.ci.meta.Constant;
  51 import jdk.vm.ci.meta.ConstantReflectionProvider;
  52 import jdk.vm.ci.meta.JavaConstant;
  53 import jdk.vm.ci.meta.MetaAccessProvider;
  54 import jdk.vm.ci.meta.ResolvedJavaType;
  55 
  56 /**
  57  * Read {@code Class::_klass} to get the hub for a {@link java.lang.Class}. This node mostly exists
  58  * to replace {@code _klass._java_mirror._klass} with {@code _klass}. The constant folding could be
  59  * handled by
  60  * {@link ReadNode#canonicalizeRead(ValueNode, AddressNode, LocationIdentity, CanonicalizerTool)}.
  61  */
  62 @NodeInfo(cycles = CYCLES_4, size = SIZE_1)
  63 public final class ClassGetHubNode extends FloatingNode implements Lowerable, Canonicalizable, ConvertNode {
  64     public static final NodeClass<ClassGetHubNode> TYPE = NodeClass.create(ClassGetHubNode.class);
  65     @Input protected ValueNode clazz;
  66 
  67     public ClassGetHubNode(ValueNode clazz) {
  68         super(TYPE, KlassPointerStamp.klass());
  69         this.clazz = clazz;
  70     }
  71 
  72     @Override
  73     public Node canonical(CanonicalizerTool tool) {
  74         if (tool.allUsagesAvailable() && hasNoUsages()) {
  75             return null;
  76         } else {
  77             if (clazz.isConstant()) {
  78                 MetaAccessProvider metaAccess = tool.getMetaAccess();
  79                 if (metaAccess != null) {
  80                     ResolvedJavaType exactType = tool.getConstantReflection().asJavaType(clazz.asJavaConstant());
  81                     if (exactType.isPrimitive()) {
  82                         return ConstantNode.forConstant(stamp(), JavaConstant.NULL_POINTER, metaAccess);
  83                     } else {
  84                         return ConstantNode.forConstant(stamp(), tool.getConstantReflection().asObjectHub(exactType), metaAccess);
  85                     }
  86                 }
  87             }
  88             if (clazz instanceof GetClassNode) {
  89                 GetClassNode getClass = (GetClassNode) clazz;
  90                 return new LoadHubNode(KlassPointerStamp.klassNonNull(), getClass.getObject());
  91             }
  92             if (clazz instanceof HubGetClassNode) {
  93                 // replace _klass._java_mirror._klass -> _klass
  94                 return ((HubGetClassNode) clazz).getHub();
  95             }
  96             return this;
  97         }
  98     }
  99 
 100     @Override
 101     public void lower(LoweringTool tool) {
 102         tool.getLowerer().lower(this, tool);
 103     }
 104 
 105     @NodeIntrinsic
 106     public static native KlassPointer readClass(Class<?> clazzNonNull);
 107 
 108     @NodeIntrinsic(PiNode.class)
 109     public static native KlassPointer piCastNonNull(Object object, GuardingNode anchor);
 110 
 111     @Override
 112     public ValueNode getValue() {
 113         return clazz;
 114     }
 115 
 116     @Override
 117     public Constant convert(Constant c, ConstantReflectionProvider constantReflection) {
 118         ResolvedJavaType exactType = constantReflection.asJavaType(c);
 119         if (exactType.isPrimitive()) {
 120             return JavaConstant.NULL_POINTER;
 121         } else {
 122             return constantReflection.asObjectHub(exactType);
 123         }
 124     }
 125 
 126     @Override
 127     public Constant reverse(Constant c, ConstantReflectionProvider constantReflection) {
 128         assert !c.equals(JavaConstant.NULL_POINTER);
 129         ResolvedJavaType objectType = constantReflection.asJavaType(c);
 130         return constantReflection.asJavaClass(objectType);
 131     }
 132 
 133     @Override
 134     public boolean isLossless() {
 135         return false;
 136     }
 137 
 138     /**
 139      * There is more than one {@link java.lang.Class} value that has a NULL hub.
 140      */
 141     @Override
 142     public boolean mayNullCheckSkipConversion() {
 143         return false;
 144     }
 145 
 146     @Override
 147     public boolean preservesOrder(Condition op, Constant value, ConstantReflectionProvider constantReflection) {
 148         assert op == Condition.EQ || op == Condition.NE;
 149         ResolvedJavaType exactType = constantReflection.asJavaType(value);
 150         return !exactType.isPrimitive();
 151     }
 152 
 153 }