src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/VerifyGraphAddUsage.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58299 6df94ce3ab2f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
     1
/*
58299
6df94ce3ab2f 8229201: Update Graal
dlong
parents: 55509
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
52910
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.lang.reflect.Constructor;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    28
import java.lang.reflect.Method;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    29
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    30
import jdk.internal.vm.compiler.collections.EconomicSet;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    31
import org.graalvm.compiler.debug.GraalError;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    32
import org.graalvm.compiler.graph.Graph;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    33
import org.graalvm.compiler.graph.Node;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    34
import org.graalvm.compiler.nodes.ConstantNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    35
import org.graalvm.compiler.nodes.Invoke;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    36
import org.graalvm.compiler.nodes.ParameterNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    37
import org.graalvm.compiler.nodes.PiNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    38
import org.graalvm.compiler.nodes.StructuredGraph;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    39
import org.graalvm.compiler.nodes.ValueNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    40
import org.graalvm.compiler.nodes.ValuePhiNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    41
import org.graalvm.compiler.nodes.ValueProxyNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    42
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    43
import org.graalvm.compiler.nodes.java.NewInstanceNode;
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    44
import org.graalvm.compiler.nodes.spi.CoreProviders;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    45
import org.graalvm.compiler.nodes.spi.LoweringProvider;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    46
import org.graalvm.compiler.phases.VerifyPhase;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    47
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    48
import jdk.vm.ci.meta.ResolvedJavaMethod;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    49
import jdk.vm.ci.meta.ResolvedJavaType;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    50
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    51
public class VerifyGraphAddUsage extends VerifyPhase<CoreProviders> {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    52
    private static final Method ADD_OR_UNIQUE;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    53
    private static final Method CONSTRUCTOR_NEW_INSTANCE;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    54
    private static final EconomicSet<Class<?>> ALLOWED_CLASSES = EconomicSet.create();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    55
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    56
    static {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    57
        try {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    58
            ADD_OR_UNIQUE = Graph.class.getDeclaredMethod("addOrUnique", Node.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    59
            CONSTRUCTOR_NEW_INSTANCE = Constructor.class.getDeclaredMethod("newInstance", Object[].class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    60
        } catch (NoSuchMethodException e) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    61
            throw new GraalError(e);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    62
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    63
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    64
        ALLOWED_CLASSES.add(Graph.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    65
        ALLOWED_CLASSES.add(LoweringProvider.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    66
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    67
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    68
    @Override
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    69
    protected void verify(StructuredGraph graph, CoreProviders context) {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    70
        boolean allowed = false;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    71
        for (Class<?> cls : ALLOWED_CLASSES) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    72
            ResolvedJavaType declaringClass = graph.method().getDeclaringClass();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    73
            if (context.getMetaAccess().lookupJavaType(cls).isAssignableFrom(declaringClass)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    74
                allowed = true;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    75
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    76
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    77
        if (!allowed) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    78
            ResolvedJavaMethod addOrUniqueMethod = context.getMetaAccess().lookupJavaMethod(ADD_OR_UNIQUE);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    79
            for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    80
                ResolvedJavaMethod callee = t.targetMethod();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    81
                if (callee.equals(addOrUniqueMethod)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    82
                    ValueNode nodeArgument = t.arguments().get(1);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    83
                    EconomicSet<Node> seen = EconomicSet.create();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    84
                    checkNonFactory(graph, seen, context, nodeArgument);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    85
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    86
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    87
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    88
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    89
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    90
    private void checkNonFactory(StructuredGraph graph, EconomicSet<Node> seen, CoreProviders context, ValueNode node) {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    91
        if (seen.contains(node)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    92
            return;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    93
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    94
        seen.add(node);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    95
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    96
        // Check where the value came from recursively, or if it is allowed.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    97
        if (node instanceof ValuePhiNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    98
            for (ValueNode input : ((ValuePhiNode) node).values()) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    99
                checkNonFactory(graph, seen, context, input);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   100
            }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   101
        } else if (node instanceof PiNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   102
            checkNonFactory(graph, seen, context, ((PiNode) node).object());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   103
        } else if (node instanceof ParameterNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   104
            return;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   105
        } else if (node instanceof ConstantNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   106
            return;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   107
        } else if (node instanceof ValueProxyNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   108
            checkNonFactory(graph, seen, context, ((ValueProxyNode) node).value());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   109
        } else if (node instanceof Invoke && ((Invoke) node).callTarget().targetMethod().equals(context.getMetaAccess().lookupJavaMethod(CONSTRUCTOR_NEW_INSTANCE))) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   110
            return;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   111
        } else if (!(node instanceof NewInstanceNode)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   112
            // In all other cases, the argument must be a new instance.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   113
            throw new VerificationError("Must add node '%s' with inputs in method '%s' of class '%s'.",
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   114
                            node, graph.method().getName(), graph.method().getDeclaringClass().getName());
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   115
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   116
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   117
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   118
}