src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/GraalFilters.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46949 hotspot/src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/GraalFilters.java@44ccdba2d72e
child 48826 c4d9d1b08e2e
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     1
/*
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46551
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     4
 *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     8
 *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    13
 * accompanied this code).
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    14
 *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    18
 *
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    21
 * questions.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    22
 */
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    23
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    24
package jdk.tools.jaotc;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    25
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    26
import java.lang.annotation.Annotation;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    27
import java.util.Arrays;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    28
import java.util.HashSet;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    29
import java.util.List;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    30
import java.util.Set;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    31
import java.util.stream.Collectors;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    32
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    33
import jdk.vm.ci.meta.MetaAccessProvider;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    34
import jdk.vm.ci.meta.ResolvedJavaMethod;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    35
import jdk.vm.ci.meta.ResolvedJavaType;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    36
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    37
import org.graalvm.compiler.api.directives.GraalDirectives;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    38
import org.graalvm.compiler.api.replacements.ClassSubstitution;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    39
import org.graalvm.compiler.api.replacements.MethodSubstitution;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    40
import org.graalvm.compiler.api.replacements.Snippet;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    41
import org.graalvm.compiler.debug.GraalError;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    42
import org.graalvm.compiler.graph.Node.NodeIntrinsic;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    43
import org.graalvm.compiler.hotspot.replacements.HotSpotClassSubstitutions;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    44
import org.graalvm.compiler.hotspot.word.MetaspacePointer;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    45
import org.graalvm.compiler.replacements.Snippets;
46551
d01034a83ab2 8182557: Update Graal
iveresov
parents: 46459
diff changeset
    46
import org.graalvm.word.WordBase;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    47
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46551
diff changeset
    48
final class GraalFilters {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    49
    private List<ResolvedJavaType> specialClasses;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    50
    private List<ResolvedJavaType> specialArgumentAndReturnTypes;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    51
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    52
    private static Set<Class<?>> skipAnnotations = new HashSet<>();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    53
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    54
    static {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    55
        skipAnnotations.add(NodeIntrinsic.class);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    56
        skipAnnotations.add(Snippet.class);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    57
        skipAnnotations.add(MethodSubstitution.class);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    58
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    59
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46551
diff changeset
    60
    boolean shouldCompileMethod(ResolvedJavaMethod method) {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    61
        // NodeIntrinsics cannot be compiled.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    62
        if (hasExcludedAnnotation(method)) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    63
            return false;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    64
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    65
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    66
        ResolvedJavaType declaringClass = method.getDeclaringClass();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    67
        // Check for special magical types in the signature, like Word or MetaspacePointer. Those
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    68
        // are definitely snippets.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    69
        List<ResolvedJavaType> signatureTypes = Arrays.asList(method.toParameterTypes()).stream().map(p -> p.resolve(declaringClass)).collect(Collectors.toList());
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    70
        signatureTypes.add(method.getSignature().getReturnType(null).resolve(declaringClass));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    71
        if (signatureTypes.stream().flatMap(t -> specialArgumentAndReturnTypes.stream().filter(s -> s.isAssignableFrom(t))).findAny().isPresent()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    72
            return false;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    73
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    74
        return true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    75
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    76
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    77
    private static boolean hasExcludedAnnotation(ResolvedJavaMethod method) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    78
        for (Annotation annotation : method.getAnnotations()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    79
            if (skipAnnotations.contains(annotation.annotationType())) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    80
                return true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    81
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    82
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    83
        return false;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    84
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    85
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46551
diff changeset
    86
    boolean shouldCompileAnyMethodInClass(ResolvedJavaType klass) {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    87
        if (specialClasses.stream().filter(s -> s.isAssignableFrom(klass)).findAny().isPresent()) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    88
            return false;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    89
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    90
        return true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    91
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    92
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    93
    // Don't compile methods in classes and their subtypes that are in the list.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    94
    private static List<ResolvedJavaType> getSpecialClasses(MetaAccessProvider meta) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    95
        // @formatter:off
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    96
        return Arrays.asList(meta.lookupJavaType(Snippets.class),
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    97
            meta.lookupJavaType(HotSpotClassSubstitutions.class),
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    98
            meta.lookupJavaType(GraalDirectives.class),
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
    99
            meta.lookupJavaType(ClassSubstitution.class));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   100
        // @formatter:on
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   101
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   102
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   103
    // Don't compile methods that have have the listed class or their subtypes in their signature.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   104
    private static List<ResolvedJavaType> getSpecialArgumentAndReturnTypes(MetaAccessProvider meta) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   105
        // @formatter:off
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   106
        return Arrays.asList(meta.lookupJavaType(WordBase.class),
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   107
            meta.lookupJavaType(MetaspacePointer.class));
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   108
        // @formatter:on
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   109
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   110
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   111
    GraalFilters(MetaAccessProvider metaAccess) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   112
        specialClasses = getSpecialClasses(metaAccess);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   113
        specialArgumentAndReturnTypes = getSpecialArgumentAndReturnTypes(metaAccess);
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   114
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   115
46949
44ccdba2d72e 8186453: [AOT] refactor AOT tool code
kvn
parents: 46551
diff changeset
   116
    static boolean shouldIgnoreException(Throwable e) {
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   117
        if (e instanceof GraalError) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   118
            String m = e.getMessage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   119
            if (m.contains("ArrayKlass::_component_mirror")) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   120
                // When compiling Graal, ignore errors in JDK8 snippets.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   121
                return true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   122
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   123
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   124
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   125
        if (e instanceof org.graalvm.compiler.java.BytecodeParser.BytecodeParserError) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   126
            Throwable cause = e.getCause();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   127
            if (cause instanceof GraalError) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   128
                String m = cause.getMessage();
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   129
                // When compiling Graal suppress attempts to compile snippet fragments that bottom
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   130
                // out with node intrinsics. These are unfortunately not explicitly marked, so we
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   131
                // have to try to compile them and bail out if we think it's a snippet.
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   132
                if (m.contains("@NodeIntrinsic method") && m.contains("must only be called from within a replacement")) {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   133
                    return true;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   134
                }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   135
            }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   136
        }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   137
        return false;
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   138
    }
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents:
diff changeset
   139
}