1 /*
   2  * Copyright (c) 2013, 2018, 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 
  24 
  25 package org.graalvm.compiler.hotspot.phases;
  26 
  27 import static org.graalvm.compiler.nodes.ConstantNode.getConstantNodes;
  28 import static org.graalvm.compiler.nodes.NamedLocationIdentity.FINAL_LOCATION;
  29 
  30 import org.graalvm.compiler.core.common.CompressEncoding;
  31 import org.graalvm.compiler.core.common.type.AbstractObjectStamp;
  32 import org.graalvm.compiler.core.common.type.Stamp;
  33 import org.graalvm.compiler.core.common.type.StampFactory;
  34 import org.graalvm.compiler.core.common.type.TypeReference;
  35 import org.graalvm.compiler.debug.GraalError;
  36 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  37 import org.graalvm.compiler.hotspot.nodes.HotSpotCompressionNode;
  38 import org.graalvm.compiler.hotspot.nodes.type.HotSpotNarrowOopStamp;
  39 import org.graalvm.compiler.hotspot.nodes.type.KlassPointerStamp;
  40 import org.graalvm.compiler.hotspot.replacements.HubGetClassNode;
  41 import org.graalvm.compiler.nodes.ConstantNode;
  42 import org.graalvm.compiler.nodes.StructuredGraph;
  43 import org.graalvm.compiler.nodes.ValueNode;
  44 import org.graalvm.compiler.nodes.memory.FloatingReadNode;
  45 import org.graalvm.compiler.nodes.memory.address.AddressNode;
  46 import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
  47 import org.graalvm.compiler.nodes.spi.CoreProviders;
  48 import org.graalvm.compiler.phases.BasePhase;
  49 import org.graalvm.compiler.phases.common.LoweringPhase;
  50 
  51 import jdk.vm.ci.hotspot.HotSpotObjectConstant;
  52 import jdk.vm.ci.hotspot.HotSpotResolvedJavaField;
  53 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  54 import jdk.vm.ci.hotspot.HotSpotResolvedPrimitiveType;
  55 import jdk.vm.ci.meta.ConstantReflectionProvider;
  56 import jdk.vm.ci.meta.JavaConstant;
  57 import jdk.vm.ci.meta.MetaAccessProvider;
  58 import jdk.vm.ci.meta.ResolvedJavaType;
  59 
  60 /**
  61  * For AOT compilation we aren't allowed to use a {@link Class} reference ({@code javaMirror})
  62  * directly. Instead the {@link Class} reference should be obtained from the {@code Klass} object.
  63  * The reason for this is, that in Class Data Sharing (CDS) a {@code Klass} object is mapped to a
  64  * fixed address in memory, but the {@code javaMirror} is not (which lives in the Java heap).
  65  *
  66  * Lowering can introduce new {@link ConstantNode}s containing a {@link Class} reference, thus this
  67  * phase must be applied after {@link LoweringPhase}.
  68  *
  69  * @see AheadOfTimeVerificationPhase
  70  */
  71 public class LoadJavaMirrorWithKlassPhase extends BasePhase<CoreProviders> {
  72 
  73     private final CompressEncoding oopEncoding;
  74 
  75     public LoadJavaMirrorWithKlassPhase(GraalHotSpotVMConfig config) {
  76         this.oopEncoding = config.useCompressedOops ? config.getOopEncoding() : null;
  77     }
  78 
  79     private ValueNode getClassConstantReplacement(StructuredGraph graph, CoreProviders context, JavaConstant constant) {
  80         if (constant instanceof HotSpotObjectConstant) {
  81             ConstantReflectionProvider constantReflection = context.getConstantReflection();
  82             ResolvedJavaType type = constantReflection.asJavaType(constant);
  83             if (type != null) {
  84                 MetaAccessProvider metaAccess = context.getMetaAccess();
  85                 Stamp stamp = StampFactory.objectNonNull(TypeReference.createExactTrusted(metaAccess.lookupJavaType(Class.class)));
  86 
  87                 if (type instanceof HotSpotResolvedObjectType) {
  88                     ConstantNode klass = ConstantNode.forConstant(KlassPointerStamp.klassNonNull(), ((HotSpotResolvedObjectType) type).klass(), metaAccess, graph);
  89                     ValueNode getClass = graph.unique(new HubGetClassNode(metaAccess, klass));
  90 
  91                     if (((HotSpotObjectConstant) constant).isCompressed()) {
  92                         return HotSpotCompressionNode.compress(getClass, oopEncoding);
  93                     } else {
  94                         return getClass;
  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 = HotSpotNarrowOopStamp.compressed((AbstractObjectStamp) stamp, oopEncoding);
 118                     }
 119                     AddressNode address = graph.unique(new OffsetAddressNode(clazz, ConstantNode.forLong(typeField.getOffset(), 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 HotSpotCompressionNode.uncompress(read, oopEncoding);
 126                     }
 127                 }
 128             }
 129         }
 130         return null;
 131     }
 132 
 133     @Override
 134     protected void run(StructuredGraph graph, CoreProviders 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 }