42650
|
1 |
/*
|
46949
|
2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
|
42650
|
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 |
package jdk.tools.jaotc;
|
|
25 |
|
|
26 |
import java.lang.annotation.Annotation;
|
|
27 |
import java.util.Arrays;
|
|
28 |
import java.util.HashSet;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.Set;
|
|
31 |
import java.util.stream.Collectors;
|
|
32 |
|
48826
|
33 |
import jdk.vm.ci.hotspot.HotSpotConstantPool;
|
|
34 |
import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
|
42650
|
35 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
36 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
37 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
38 |
|
|
39 |
import org.graalvm.compiler.api.directives.GraalDirectives;
|
|
40 |
import org.graalvm.compiler.api.replacements.ClassSubstitution;
|
|
41 |
import org.graalvm.compiler.api.replacements.MethodSubstitution;
|
|
42 |
import org.graalvm.compiler.api.replacements.Snippet;
|
|
43 |
import org.graalvm.compiler.debug.GraalError;
|
|
44 |
import org.graalvm.compiler.graph.Node.NodeIntrinsic;
|
|
45 |
import org.graalvm.compiler.hotspot.replacements.HotSpotClassSubstitutions;
|
|
46 |
import org.graalvm.compiler.hotspot.word.MetaspacePointer;
|
|
47 |
import org.graalvm.compiler.replacements.Snippets;
|
46551
|
48 |
import org.graalvm.word.WordBase;
|
42650
|
49 |
|
46949
|
50 |
final class GraalFilters {
|
42650
|
51 |
private List<ResolvedJavaType> specialClasses;
|
|
52 |
private List<ResolvedJavaType> specialArgumentAndReturnTypes;
|
|
53 |
|
|
54 |
private static Set<Class<?>> skipAnnotations = new HashSet<>();
|
|
55 |
|
|
56 |
static {
|
|
57 |
skipAnnotations.add(NodeIntrinsic.class);
|
|
58 |
skipAnnotations.add(Snippet.class);
|
|
59 |
skipAnnotations.add(MethodSubstitution.class);
|
|
60 |
}
|
|
61 |
|
46949
|
62 |
boolean shouldCompileMethod(ResolvedJavaMethod method) {
|
42650
|
63 |
// NodeIntrinsics cannot be compiled.
|
|
64 |
if (hasExcludedAnnotation(method)) {
|
|
65 |
return false;
|
|
66 |
}
|
|
67 |
|
|
68 |
ResolvedJavaType declaringClass = method.getDeclaringClass();
|
|
69 |
// Check for special magical types in the signature, like Word or MetaspacePointer. Those
|
|
70 |
// are definitely snippets.
|
|
71 |
List<ResolvedJavaType> signatureTypes = Arrays.asList(method.toParameterTypes()).stream().map(p -> p.resolve(declaringClass)).collect(Collectors.toList());
|
|
72 |
signatureTypes.add(method.getSignature().getReturnType(null).resolve(declaringClass));
|
|
73 |
if (signatureTypes.stream().flatMap(t -> specialArgumentAndReturnTypes.stream().filter(s -> s.isAssignableFrom(t))).findAny().isPresent()) {
|
|
74 |
return false;
|
|
75 |
}
|
|
76 |
return true;
|
|
77 |
}
|
|
78 |
|
|
79 |
private static boolean hasExcludedAnnotation(ResolvedJavaMethod method) {
|
|
80 |
for (Annotation annotation : method.getAnnotations()) {
|
|
81 |
if (skipAnnotations.contains(annotation.annotationType())) {
|
|
82 |
return true;
|
|
83 |
}
|
|
84 |
}
|
|
85 |
return false;
|
|
86 |
}
|
|
87 |
|
46949
|
88 |
boolean shouldCompileAnyMethodInClass(ResolvedJavaType klass) {
|
42650
|
89 |
if (specialClasses.stream().filter(s -> s.isAssignableFrom(klass)).findAny().isPresent()) {
|
|
90 |
return false;
|
|
91 |
}
|
48826
|
92 |
// Skip klass with Condy until Graal is fixed.
|
|
93 |
if (((HotSpotConstantPool)((HotSpotResolvedObjectType) klass).getConstantPool()).hasDynamicConstant()) {
|
|
94 |
return false;
|
|
95 |
}
|
42650
|
96 |
return true;
|
|
97 |
}
|
|
98 |
|
|
99 |
// Don't compile methods in classes and their subtypes that are in the list.
|
|
100 |
private static List<ResolvedJavaType> getSpecialClasses(MetaAccessProvider meta) {
|
|
101 |
// @formatter:off
|
|
102 |
return Arrays.asList(meta.lookupJavaType(Snippets.class),
|
|
103 |
meta.lookupJavaType(HotSpotClassSubstitutions.class),
|
|
104 |
meta.lookupJavaType(GraalDirectives.class),
|
|
105 |
meta.lookupJavaType(ClassSubstitution.class));
|
|
106 |
// @formatter:on
|
|
107 |
}
|
|
108 |
|
|
109 |
// Don't compile methods that have have the listed class or their subtypes in their signature.
|
|
110 |
private static List<ResolvedJavaType> getSpecialArgumentAndReturnTypes(MetaAccessProvider meta) {
|
|
111 |
// @formatter:off
|
|
112 |
return Arrays.asList(meta.lookupJavaType(WordBase.class),
|
|
113 |
meta.lookupJavaType(MetaspacePointer.class));
|
|
114 |
// @formatter:on
|
|
115 |
}
|
|
116 |
|
|
117 |
GraalFilters(MetaAccessProvider metaAccess) {
|
|
118 |
specialClasses = getSpecialClasses(metaAccess);
|
|
119 |
specialArgumentAndReturnTypes = getSpecialArgumentAndReturnTypes(metaAccess);
|
|
120 |
}
|
|
121 |
|
46949
|
122 |
static boolean shouldIgnoreException(Throwable e) {
|
42650
|
123 |
if (e instanceof GraalError) {
|
|
124 |
String m = e.getMessage();
|
|
125 |
if (m.contains("ArrayKlass::_component_mirror")) {
|
|
126 |
// When compiling Graal, ignore errors in JDK8 snippets.
|
|
127 |
return true;
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
if (e instanceof org.graalvm.compiler.java.BytecodeParser.BytecodeParserError) {
|
|
132 |
Throwable cause = e.getCause();
|
|
133 |
if (cause instanceof GraalError) {
|
|
134 |
String m = cause.getMessage();
|
|
135 |
// When compiling Graal suppress attempts to compile snippet fragments that bottom
|
|
136 |
// out with node intrinsics. These are unfortunately not explicitly marked, so we
|
|
137 |
// have to try to compile them and bail out if we think it's a snippet.
|
|
138 |
if (m.contains("@NodeIntrinsic method") && m.contains("must only be called from within a replacement")) {
|
|
139 |
return true;
|
|
140 |
}
|
|
141 |
}
|
|
142 |
}
|
|
143 |
return false;
|
|
144 |
}
|
|
145 |
}
|