src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/VerifyUpdateUsages.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) 2013, 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.util.List;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    28
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    29
import org.graalvm.compiler.graph.Node;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    30
import org.graalvm.compiler.graph.Node.Input;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    31
import org.graalvm.compiler.graph.Node.OptionalInput;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    32
import org.graalvm.compiler.graph.NodeInputList;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    33
import org.graalvm.compiler.nodes.StructuredGraph;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    34
import org.graalvm.compiler.nodes.java.LoadFieldNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    35
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    36
import org.graalvm.compiler.nodes.java.StoreFieldNode;
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    37
import org.graalvm.compiler.nodes.spi.CoreProviders;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    38
import org.graalvm.compiler.phases.VerifyPhase;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    39
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    40
import jdk.vm.ci.meta.ResolvedJavaField;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    41
import jdk.vm.ci.meta.ResolvedJavaMethod;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    42
import jdk.vm.ci.meta.ResolvedJavaType;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    43
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    44
/**
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    45
 * Try to ensure that methods which update {@link Input} or {@link OptionalInput} fields also
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    46
 * include a call to {@link Node#updateUsages} or {@link Node#updateUsagesInterface}.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    47
 */
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    48
public class VerifyUpdateUsages extends VerifyPhase<CoreProviders> {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    49
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    50
    @Override
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    51
    public boolean checkContract() {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    52
        return false;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    53
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    54
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    55
    public VerifyUpdateUsages() {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    56
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    57
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    58
    @Override
55509
d58442b8abc1 8225497: Update Graal
jwilhelm
parents: 54328
diff changeset
    59
    protected void verify(StructuredGraph graph, CoreProviders context) {
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    60
        if (graph.method().isConstructor()) {
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    61
            return;
52910
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
         * There are only two acceptable patterns for methods which update Node inputs, either a
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    65
         * single StoreField node and invoke of updateUsages or updateUsagesInterface, or 2
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    66
         * StoreFields that come from LoadFields on the same object. Other patterns can be added as
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    67
         * needed but it would be best to keep things simple so that verification can be simple.
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    68
         */
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    69
        List<StoreFieldNode> stores = graph.getNodes().filter(StoreFieldNode.class).snapshot();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    70
        ResolvedJavaType declaringClass = graph.method().getDeclaringClass();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    71
        ResolvedJavaType nodeInputList = context.getMetaAccess().lookupJavaType(NodeInputList.class);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    72
        StoreFieldNode storeField1 = null;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    73
        StoreFieldNode storeField2 = null;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    74
        for (StoreFieldNode store : stores) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    75
            if (isNodeInput(store.field(), declaringClass, nodeInputList)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    76
                if (storeField1 == null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    77
                    storeField1 = store;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    78
                } else if (storeField2 == null) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    79
                    storeField2 = store;
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    80
                } else {
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    81
                    throw new VerificationError("More than 2 stores to %s or %s fields found in %s",
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    82
                                    Input.class.getSimpleName(),
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    83
                                    OptionalInput.class.getSimpleName(),
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    84
                                    graph.method().format("%H.%n(%p)"));
52910
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
        if (storeField1 == null) {
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    89
            return;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    90
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    91
        if (storeField2 == null) {
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    92
            // Single input field update so just check for updateUsages
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    93
            // or updateUsagesInterface call
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    94
            ResolvedJavaType nodeType = context.getMetaAccess().lookupJavaType(Node.class);
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    95
            for (MethodCallTargetNode call : graph.getNodes().filter(MethodCallTargetNode.class)) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    96
                ResolvedJavaMethod callee = call.targetMethod();
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    97
                if (callee.getDeclaringClass().equals(nodeType) && (callee.getName().equals("updateUsages") || callee.getName().equals("updateUsagesInterface"))) {
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
    98
                    return;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
    99
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   100
            }
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   101
            throw new VerificationError("%s updates field '%s' without calling %s.updateUsages() or %s.updateUsagesInterface()",
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   102
                            graph.method().format("%H.%n(%p)"),
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   103
                            storeField1.field().getName(),
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   104
                            Node.class.getName(),
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   105
                            Node.class.getName());
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   106
        } else {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   107
            if (storeField1.value() instanceof LoadFieldNode && storeField2.value() instanceof LoadFieldNode) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   108
                LoadFieldNode load1 = (LoadFieldNode) storeField1.value();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   109
                LoadFieldNode load2 = (LoadFieldNode) storeField2.value();
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   110
                // Check for swapping values within the same object
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   111
                if (load1.object() == storeField1.object() &&
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   112
                                load2.object() == storeField2.object() &&
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   113
                                storeField1.object() == storeField2.object() &&
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   114
                                load1.field().equals(storeField2.field()) &&
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   115
                                load2.field().equals(storeField1.field())) {
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   116
                    return;
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   117
                }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   118
            }
54328
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   119
            throw new VerificationError("%s performs non-swap update to fields '%s' and '%s' without calling %s.updateUsages() or %s.updateUsagesInterface()",
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   120
                            graph.method().format("%H.%n(%p)"),
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   121
                            storeField1.field().getName(),
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   122
                            storeField2.field().getName(),
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   123
                            Node.class.getName(),
37648a9c4a6a 8221341: Update Graal
jwilhelm
parents: 52910
diff changeset
   124
                            Node.class.getName());
52910
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   125
        }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   126
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   127
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   128
    boolean isNodeInput(ResolvedJavaField field, ResolvedJavaType declaringClass, ResolvedJavaType nodeInputList) {
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   129
        return declaringClass.isAssignableFrom(field.getDeclaringClass()) && (field.getAnnotation(Input.class) != null || field.getAnnotation(OptionalInput.class) != null) &&
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   130
                        !field.getType().equals(nodeInputList);
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   131
    }
583fd71c47d6 8214023: Update Graal
dlong
parents:
diff changeset
   132
}