1 /*
2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 
25 package org.graalvm.compiler.phases.verify;
26 
27 import java.util.List;
28 
29 import org.graalvm.compiler.graph.Node;
30 import org.graalvm.compiler.graph.Node.Input;
31 import org.graalvm.compiler.graph.Node.OptionalInput;
32 import org.graalvm.compiler.graph.NodeInputList;
33 import org.graalvm.compiler.nodes.StructuredGraph;
34 import org.graalvm.compiler.nodes.java.LoadFieldNode;
35 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
36 import org.graalvm.compiler.nodes.java.StoreFieldNode;
37 import org.graalvm.compiler.phases.VerifyPhase;
38 import org.graalvm.compiler.phases.tiers.PhaseContext;
39 
40 import jdk.vm.ci.meta.ResolvedJavaField;
41 import jdk.vm.ci.meta.ResolvedJavaMethod;
42 import jdk.vm.ci.meta.ResolvedJavaType;
43 
44 /**
45  * Try to ensure that methods which update {@link Input} or {@link OptionalInput} fields also
46  * include a call to {@link Node#updateUsages} or {@link Node#updateUsagesInterface}.
47  */
48 public class VerifyUpdateUsages extends VerifyPhase<PhaseContext> {
49 
50     @Override
checkContract()51     public boolean checkContract() {
52         return false;
53     }
54 
VerifyUpdateUsages()55     public VerifyUpdateUsages() {
56     }
57 
58     @Override
verify(StructuredGraph graph, PhaseContext context)59     protected boolean verify(StructuredGraph graph, PhaseContext context) {
60         if (graph.method().isConstructor()) {
61             return true;
62         }
63         /*
64          * There are only two acceptable patterns for methods which update Node inputs, either a
65          * single StoreField node and invoke of updateUsages or updateUsagesInterface, or 2
66          * StoreFields that come from LoadFields on the same object. Other patterns can be added as
67          * needed but it would be best to keep things simple so that verification can be simple.
68          */
69         List<StoreFieldNode> stores = graph.getNodes().filter(StoreFieldNode.class).snapshot();
70         ResolvedJavaType declaringClass = graph.method().getDeclaringClass();
71         ResolvedJavaType nodeInputList = context.getMetaAccess().lookupJavaType(NodeInputList.class);
72         StoreFieldNode storeField1 = null;
73         StoreFieldNode storeField2 = null;
74         for (StoreFieldNode store : stores) {
75             if (isNodeInput(store.field(), declaringClass, nodeInputList)) {
76                 if (storeField1 == null) {
77                     storeField1 = store;
78                 } else if (storeField2 == null) {
79                     storeField2 = store;
80                 } else {
81                     return false;
82                 }
83             }
84         }
85         if (storeField1 == null) {
86             return true;
87         }
88         if (storeField2 == null) {
89             // Single input field update so just check for updateUsages or updateUsagesInterface
90             // call
91             ResolvedJavaType node = context.getMetaAccess().lookupJavaType(Node.class);
92             for (MethodCallTargetNode call : graph.getNodes().filter(MethodCallTargetNode.class)) {
93                 ResolvedJavaMethod callee = call.targetMethod();
94                 if (callee.getDeclaringClass().equals(node) && (callee.getName().equals("updateUsages") || callee.getName().equals("updateUsagesInterface"))) {
95                     return true;
96                 }
97             }
98         } else {
99             if (storeField1.value() instanceof LoadFieldNode && storeField2.value() instanceof LoadFieldNode) {
100                 LoadFieldNode load1 = (LoadFieldNode) storeField1.value();
101                 LoadFieldNode load2 = (LoadFieldNode) storeField2.value();
102                 // Check for swapping values within the same object
103                 if (load1.object() == storeField1.object() && load2.object() == storeField2.object() && storeField1.object() == storeField2.object() &&
104                                 load1.field().equals(storeField2.field()) && load2.field().equals(storeField1.field())) {
105                     return true;
106                 }
107             }
108         }
109         return false;
110     }
111 
isNodeInput(ResolvedJavaField field, ResolvedJavaType declaringClass, ResolvedJavaType nodeInputList)112     boolean isNodeInput(ResolvedJavaField field, ResolvedJavaType declaringClass, ResolvedJavaType nodeInputList) {
113         return declaringClass.isAssignableFrom(field.getDeclaringClass()) && (field.getAnnotation(Input.class) != null || field.getAnnotation(OptionalInput.class) != null) &&
114                         !field.getType().equals(nodeInputList);
115     }
116 }
117