1 /* 2 * Copyright (c) 2013, 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.phases; 24 25 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.CLASS_MIRROR_LOCATION; 26 import static org.graalvm.compiler.nodes.ConstantNode.getConstantNodes; 27 import static org.graalvm.compiler.nodes.NamedLocationIdentity.FINAL_LOCATION; 28 29 import org.graalvm.compiler.core.common.type.AbstractObjectStamp; 30 import org.graalvm.compiler.core.common.type.Stamp; 31 import org.graalvm.compiler.core.common.type.StampFactory; 32 import org.graalvm.compiler.core.common.type.TypeReference; 33 import org.graalvm.compiler.debug.GraalError; 34 import org.graalvm.compiler.hotspot.CompressEncoding; 35 import org.graalvm.compiler.hotspot.nodes.CompressionNode; 36 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp; 37 import org.graalvm.compiler.hotspot.nodes.type.NarrowOopStamp; 38 import org.graalvm.compiler.nodes.ConstantNode; 39 import org.graalvm.compiler.nodes.StructuredGraph; 40 import org.graalvm.compiler.nodes.ValueNode; 41 import org.graalvm.compiler.nodes.memory.FloatingReadNode; 42 import org.graalvm.compiler.nodes.memory.address.AddressNode; 43 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode; 44 import org.graalvm.compiler.phases.BasePhase; 45 import org.graalvm.compiler.phases.common.LoweringPhase; 46 import org.graalvm.compiler.phases.tiers.PhaseContext; 47 48 import jdk.vm.ci.hotspot.HotSpotObjectConstant; 49 import jdk.vm.ci.hotspot.HotSpotResolvedJavaField; 50 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType; 51 import jdk.vm.ci.hotspot.HotSpotResolvedPrimitiveType; 52 import jdk.vm.ci.meta.ConstantReflectionProvider; 53 import jdk.vm.ci.meta.JavaConstant; 54 import jdk.vm.ci.meta.MetaAccessProvider; 55 import jdk.vm.ci.meta.ResolvedJavaType; 56 57 /** 58 * For AOT compilation we aren't allowed to use a {@link Class} reference ({@code javaMirror}) 59 * directly. Instead the {@link Class} reference should be obtained from the {@code Klass} object. 60 * The reason for this is, that in Class Data Sharing (CDS) a {@code Klass} object is mapped to a 61 * fixed address in memory, but the {@code javaMirror} is not (which lives in the Java heap). 62 * 63 * Lowering can introduce new {@link ConstantNode}s containing a {@link Class} reference, thus this 64 * phase must be applied after {@link LoweringPhase}. 65 * 66 * @see AheadOfTimeVerificationPhase 67 */ 68 public class LoadJavaMirrorWithKlassPhase extends BasePhase<PhaseContext> { 69 70 private final int classMirrorOffset; 71 private final CompressEncoding oopEncoding; 72 73 public LoadJavaMirrorWithKlassPhase(int classMirrorOffset, CompressEncoding oopEncoding) { 74 this.classMirrorOffset = classMirrorOffset; 75 this.oopEncoding = oopEncoding; 76 } 77 78 private ValueNode getClassConstantReplacement(StructuredGraph graph, PhaseContext context, JavaConstant constant) { 79 if (constant instanceof HotSpotObjectConstant) { 80 ConstantReflectionProvider constantReflection = context.getConstantReflection(); 81 ResolvedJavaType type = constantReflection.asJavaType(constant); 82 if (type != null) { 83 MetaAccessProvider metaAccess = context.getMetaAccess(); 84 Stamp stamp = StampFactory.objectNonNull(TypeReference.createExactTrusted(metaAccess.lookupJavaType(Class.class))); 85 86 if (type instanceof HotSpotResolvedObjectType) { 87 ConstantNode klass = ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), ((HotSpotResolvedObjectType) type).klass(), metaAccess, graph); 88 AddressNode address = graph.unique(new OffsetAddressNode(klass, ConstantNode.forLong(classMirrorOffset, graph))); 89 ValueNode read = graph.unique(new FloatingReadNode(address, CLASS_MIRROR_LOCATION, null, stamp)); 90 91 if (((HotSpotObjectConstant) constant).isCompressed()) { 92 return CompressionNode.compress(read, oopEncoding); 93 } else { 94 return read; 95 } 96 } else { 97 /* 98 * Primitive classes are more difficult since they don't have a corresponding 99 * Klass* so get them from Class.TYPE for the java box type. 100 */ 101 HotSpotResolvedPrimitiveType primitive = (HotSpotResolvedPrimitiveType) type; 102 ResolvedJavaType boxingClass = metaAccess.lookupJavaType(primitive.getJavaKind().toBoxedJavaClass()); 103 ConstantNode clazz = ConstantNode.forConstant(context.getConstantReflection().asJavaClass(boxingClass), metaAccess, graph); 104 HotSpotResolvedJavaField[] a = (HotSpotResolvedJavaField[]) boxingClass.getStaticFields(); 105 HotSpotResolvedJavaField typeField = null; 106 for (HotSpotResolvedJavaField f : a) { 107 if (f.getName().equals("TYPE")) { 108 typeField = f; 109 break; 110 } 111 } 112 if (typeField == null) { 113 throw new GraalError("Can't find TYPE field in class"); 114 } 115 116 if (oopEncoding != null) { 117 stamp = NarrowOopStamp.compressed((AbstractObjectStamp) stamp, oopEncoding); 118 } 119 AddressNode address = graph.unique(new OffsetAddressNode(clazz, ConstantNode.forLong(typeField.offset(), graph))); 120 ValueNode read = graph.unique(new FloatingReadNode(address, FINAL_LOCATION, null, stamp)); 121 122 if (oopEncoding == null || ((HotSpotObjectConstant) constant).isCompressed()) { 123 return read; 124 } else { 125 return CompressionNode.uncompress(read, oopEncoding); 126 } 127 } 128 } 129 } 130 return null; 131 } 132 133 @Override 134 protected void run(StructuredGraph graph, PhaseContext context) { 135 for (ConstantNode node : getConstantNodes(graph)) { 136 JavaConstant constant = node.asJavaConstant(); 137 ValueNode freadNode = getClassConstantReplacement(graph, context, constant); 138 if (freadNode != null) { 139 node.replace(graph, freadNode); 140 } 141 } 142 } 143 144 @Override 145 public float codeSizeIncrease() { 146 return 2.5f; 147 } 148 }