src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.phases/src/org/graalvm/compiler/phases/verify/VerifyGraphAddUsage.java
author iveresov
Fri, 02 Feb 2018 17:28:17 -0800
changeset 48861 47f19ff9903c
parent 47798 9fe9292f5931
child 49873 26ebfe8ce852
permissions -rw-r--r--
8194819: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     1
/*
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     4
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     8
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    13
 * accompanied this code).
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    14
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    18
 *
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    21
 * questions.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    22
 */
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    23
package org.graalvm.compiler.phases.verify;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    24
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47798
diff changeset
    25
import java.lang.reflect.Constructor;
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47798
diff changeset
    26
import java.lang.reflect.Method;
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47798
diff changeset
    27
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47798
diff changeset
    28
import org.graalvm.collections.EconomicSet;
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    29
import org.graalvm.compiler.debug.GraalError;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    30
import org.graalvm.compiler.graph.Graph;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    31
import org.graalvm.compiler.graph.Node;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    32
import org.graalvm.compiler.nodes.ConstantNode;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    33
import org.graalvm.compiler.nodes.Invoke;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    34
import org.graalvm.compiler.nodes.ParameterNode;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    35
import org.graalvm.compiler.nodes.PiNode;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    36
import org.graalvm.compiler.nodes.StructuredGraph;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    37
import org.graalvm.compiler.nodes.ValueNode;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    38
import org.graalvm.compiler.nodes.ValuePhiNode;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    39
import org.graalvm.compiler.nodes.ValueProxyNode;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    40
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    41
import org.graalvm.compiler.nodes.java.NewInstanceNode;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    42
import org.graalvm.compiler.nodes.spi.LoweringProvider;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    43
import org.graalvm.compiler.phases.VerifyPhase;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    44
import org.graalvm.compiler.phases.tiers.PhaseContext;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    45
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47798
diff changeset
    46
import jdk.vm.ci.meta.ResolvedJavaMethod;
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47798
diff changeset
    47
import jdk.vm.ci.meta.ResolvedJavaType;
47798
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    48
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    49
public class VerifyGraphAddUsage extends VerifyPhase<PhaseContext> {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    50
    private static final Method ADD_OR_UNIQUE;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    51
    private static final Method CONSTRUCTOR_NEW_INSTANCE;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    52
    private static final EconomicSet<Class<?>> ALLOWED_CLASSES = EconomicSet.create();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    53
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    54
    static {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    55
        try {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    56
            ADD_OR_UNIQUE = Graph.class.getDeclaredMethod("addOrUnique", Node.class);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    57
            CONSTRUCTOR_NEW_INSTANCE = Constructor.class.getDeclaredMethod("newInstance", Object[].class);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    58
        } catch (NoSuchMethodException e) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    59
            throw new GraalError(e);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    60
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    61
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    62
        ALLOWED_CLASSES.add(Graph.class);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    63
        ALLOWED_CLASSES.add(LoweringProvider.class);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    64
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    65
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    66
    @Override
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    67
    protected boolean verify(StructuredGraph graph, PhaseContext context) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    68
        boolean allowed = false;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    69
        for (Class<?> cls : ALLOWED_CLASSES) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    70
            ResolvedJavaType declaringClass = graph.method().getDeclaringClass();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    71
            if (context.getMetaAccess().lookupJavaType(cls).isAssignableFrom(declaringClass)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    72
                allowed = true;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    73
            }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    74
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    75
        if (!allowed) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    76
            ResolvedJavaMethod addOrUniqueMethod = context.getMetaAccess().lookupJavaMethod(ADD_OR_UNIQUE);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    77
            for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    78
                ResolvedJavaMethod callee = t.targetMethod();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    79
                if (callee.equals(addOrUniqueMethod)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    80
                    ValueNode nodeArgument = t.arguments().get(1);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    81
                    EconomicSet<Node> seen = EconomicSet.create();
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    82
                    checkNonFactory(graph, seen, context, nodeArgument);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    83
                }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    84
            }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    85
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    86
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    87
        return true;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    88
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    89
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    90
    private void checkNonFactory(StructuredGraph graph, EconomicSet<Node> seen, PhaseContext context, ValueNode node) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    91
        if (seen.contains(node)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    92
            return;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    93
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    94
        seen.add(node);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    95
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    96
        // Check where the value came from recursively, or if it is allowed.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    97
        if (node instanceof ValuePhiNode) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    98
            for (ValueNode input : ((ValuePhiNode) node).values()) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
    99
                checkNonFactory(graph, seen, context, input);
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   100
            }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   101
        } else if (node instanceof PiNode) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   102
            checkNonFactory(graph, seen, context, ((PiNode) node).object());
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   103
        } else if (node instanceof ParameterNode) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   104
            return;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   105
        } else if (node instanceof ConstantNode) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   106
            return;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   107
        } else if (node instanceof ValueProxyNode) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   108
            checkNonFactory(graph, seen, context, ((ValueProxyNode) node).value());
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   109
        } else if (node instanceof Invoke && ((Invoke) node).callTarget().targetMethod().equals(context.getMetaAccess().lookupJavaMethod(CONSTRUCTOR_NEW_INSTANCE))) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   110
            return;
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   111
        } else if (!(node instanceof NewInstanceNode)) {
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   112
            // In all other cases, the argument must be a new instance.
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   113
            throw new VerificationError("Must add node '%s' with inputs in method '%s' of class '%s'.",
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   114
                            node, graph.method().getName(), graph.method().getDeclaringClass().getName());
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   115
        }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   116
    }
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   117
9fe9292f5931 8190710: Update Graal
dlong
parents:
diff changeset
   118
}