1 /*
2  * Copyright (c) 2015, 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.meta;
26 
27 import java.lang.invoke.MethodHandle;
28 import java.lang.invoke.MethodHandles;
29 import java.lang.invoke.MethodType;
30 import java.util.function.Supplier;
31 
32 import org.graalvm.compiler.core.common.type.ObjectStamp;
33 import org.graalvm.compiler.core.common.type.Stamp;
34 import org.graalvm.compiler.core.common.type.StampFactory;
35 import org.graalvm.compiler.debug.GraalError;
36 import org.graalvm.compiler.hotspot.nodes.aot.InitializeKlassNode;
37 import org.graalvm.compiler.hotspot.nodes.aot.ResolveConstantNode;
38 import org.graalvm.compiler.nodes.ConstantNode;
39 import org.graalvm.compiler.nodes.DeoptimizingFixedWithNextNode;
40 import org.graalvm.compiler.nodes.FrameState;
41 import org.graalvm.compiler.nodes.ValueNode;
42 import org.graalvm.compiler.nodes.graphbuilderconf.ClassInitializationPlugin;
43 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext;
44 
45 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
46 import jdk.vm.ci.meta.ConstantPool;
47 import jdk.vm.ci.meta.ResolvedJavaMethod;
48 import jdk.vm.ci.meta.ResolvedJavaType;
49 
50 public final class HotSpotClassInitializationPlugin implements ClassInitializationPlugin {
shouldApply(GraphBuilderContext builder, ResolvedJavaType type)51     private static boolean shouldApply(GraphBuilderContext builder, ResolvedJavaType type) {
52         if (!builder.parsingIntrinsic()) {
53             if (!type.isArray()) {
54                 ResolvedJavaMethod method = builder.getGraph().method();
55                 ResolvedJavaType methodHolder = method.getDeclaringClass();
56                 // We can elide initialization nodes if type >=: methodHolder.
57                 // The type is already initialized by either "new" or "invokestatic".
58 
59                 // Emit initialization node if type is an interface since:
60                 // JLS 12.4: Before a class is initialized, its direct superclass must be
61                 // initialized, but interfaces implemented by the class are not
62                 // initialized and a class or interface type T will be initialized
63                 // immediately before the first occurrence of accesses listed
64                 // in JLS 12.4.1.
65 
66                 return !type.isAssignableFrom(methodHolder) || type.isInterface();
67             } else if (!type.getComponentType().isPrimitive()) {
68                 // Always apply to object array types
69                 return true;
70             }
71         }
72         return false;
73     }
74 
75     @Override
apply(GraphBuilderContext builder, ResolvedJavaType type, Supplier<FrameState> frameState, ValueNode[] classInit)76     public boolean apply(GraphBuilderContext builder, ResolvedJavaType type, Supplier<FrameState> frameState, ValueNode[] classInit) {
77         if (shouldApply(builder, type)) {
78             Stamp hubStamp = builder.getStampProvider().createHubStamp((ObjectStamp) StampFactory.objectNonNull());
79             ConstantNode hub = builder.append(ConstantNode.forConstant(hubStamp, ((HotSpotResolvedObjectType) type).klass(), builder.getMetaAccess(), builder.getGraph()));
80             DeoptimizingFixedWithNextNode result = builder.append(type.isArray() ? new ResolveConstantNode(hub) : new InitializeKlassNode(hub));
81             result.setStateBefore(frameState.get());
82             if (classInit != null) {
83                 classInit[0] = result;
84             }
85             return true;
86         }
87         return false;
88     }
89 
90     private static final Class<? extends ConstantPool> hscp;
91     private static final MethodHandle loadReferencedTypeIIZMH;
92 
93     static {
94         MethodHandle m = null;
95         Class<? extends ConstantPool> c = null;
96         try {
97             c = Class.forName("jdk.vm.ci.hotspot.HotSpotConstantPool").asSubclass(ConstantPool.class);
98             m = MethodHandles.lookup().findVirtual(c, "loadReferencedType", MethodType.methodType(void.class, int.class, int.class, boolean.class));
99         } catch (Exception e) {
100         }
101         loadReferencedTypeIIZMH = m;
102         hscp = c;
103     }
104 
isHotSpotConstantPool(ConstantPool cp)105     private static boolean isHotSpotConstantPool(ConstantPool cp) {
106         // jdk.vm.ci.hotspot.HotSpotConstantPool is final, so we can
107         // directly compare Classes.
108         return cp.getClass() == hscp;
109     }
110 
111     @Override
supportsLazyInitialization(ConstantPool cp)112     public boolean supportsLazyInitialization(ConstantPool cp) {
113         if (loadReferencedTypeIIZMH != null && isHotSpotConstantPool(cp)) {
114             return true;
115         }
116         return false;
117     }
118 
119     @Override
loadReferencedType(GraphBuilderContext builder, ConstantPool cp, int cpi, int opcode)120     public void loadReferencedType(GraphBuilderContext builder, ConstantPool cp, int cpi, int opcode) {
121         if (loadReferencedTypeIIZMH != null && isHotSpotConstantPool(cp)) {
122             try {
123                 loadReferencedTypeIIZMH.invoke(cp, cpi, opcode, false);
124             } catch (Throwable t) {
125                 throw GraalError.shouldNotReachHere(t);
126             }
127         } else {
128             cp.loadReferencedType(cpi, opcode);
129         }
130     }
131 
132 }
133