src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/VerifyFoldableMethods.java
author dlong
Sat, 08 Dec 2018 00:56:10 -0800
changeset 52910 583fd71c47d6
child 54328 37648a9c4a6a
permissions -rw-r--r--
8214023: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     1
/*
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     4
 *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     7
 * published by the Free Software Foundation.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     8
 *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    13
 * accompanied this code).
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    14
 *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    18
 *
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    21
 * questions.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    22
 */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    23
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    24
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    25
package org.graalvm.compiler.core.test;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    26
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    27
import java.util.Map;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    28
import java.util.concurrent.ConcurrentHashMap;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    29
import java.util.stream.Collectors;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    30
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    31
import org.graalvm.compiler.api.replacements.Fold;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    32
import org.graalvm.compiler.nodes.StructuredGraph;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    33
import org.graalvm.compiler.nodes.graphbuilderconf.GeneratedInvocationPlugin;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    34
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    35
import org.graalvm.compiler.phases.VerifyPhase;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    36
import org.graalvm.compiler.phases.tiers.PhaseContext;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    37
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    38
import jdk.vm.ci.meta.ResolvedJavaMethod;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    39
import jdk.vm.ci.meta.ResolvedJavaType;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    40
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    41
/**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    42
 * Verifies that all {@link Fold} annotated methods have at least one caller.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    43
 */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    44
public class VerifyFoldableMethods extends VerifyPhase<PhaseContext> {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    45
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    46
    @Override
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    47
    public boolean checkContract() {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    48
        return false;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    49
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    50
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    51
    private final Map<ResolvedJavaMethod, Boolean> foldables = new ConcurrentHashMap<>();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    52
    ResolvedJavaType generatedInvocationPluginType;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    53
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    54
    @Override
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    55
    protected boolean verify(StructuredGraph graph, PhaseContext context) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    56
        ResolvedJavaMethod method = graph.method();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    57
        if (method.getAnnotation(Fold.class) != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    58
            foldables.putIfAbsent(method, false);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    59
        } else {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    60
            if (generatedInvocationPluginType == null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    61
                generatedInvocationPluginType = context.getMetaAccess().lookupJavaType(GeneratedInvocationPlugin.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    62
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    63
            if (!generatedInvocationPluginType.isAssignableFrom(method.getDeclaringClass())) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    64
                for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    65
                    ResolvedJavaMethod callee = t.targetMethod();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    66
                    if (callee.getAnnotation(Fold.class) != null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    67
                        foldables.put(callee, true);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    68
                    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    69
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    70
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    71
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    72
        return true;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    73
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    74
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    75
    public void finish() {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    76
        String uncalled = foldables.entrySet().stream().filter(e -> e.getValue() == false).map(e -> e.getKey().format("%H.%n(%p)")).collect(Collectors.joining(System.lineSeparator() + "  "));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    77
        if (uncalled.length() != 0) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    78
            throw new VerificationError(String.format("Methods annotated with @" + Fold.class.getSimpleName() + " appear to have no usages:%n  %s", uncalled));
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    79
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    80
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    81
}