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