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 package org.graalvm.compiler.phases.verify;
  24 
  25 import java.util.List;
  26 
  27 import org.graalvm.compiler.graph.Node;
  28 import org.graalvm.compiler.graph.Node.Input;
  29 import org.graalvm.compiler.graph.Node.OptionalInput;
  30 import org.graalvm.compiler.graph.NodeInputList;
  31 import org.graalvm.compiler.nodes.StructuredGraph;
  32 import org.graalvm.compiler.nodes.java.LoadFieldNode;
  33 import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
  34 import org.graalvm.compiler.nodes.java.StoreFieldNode;
  35 import org.graalvm.compiler.phases.VerifyPhase;
  36 import org.graalvm.compiler.phases.tiers.PhaseContext;
  37 
  38 import jdk.vm.ci.meta.ResolvedJavaField;
  39 import jdk.vm.ci.meta.ResolvedJavaMethod;
  40 import jdk.vm.ci.meta.ResolvedJavaType;
  41 
  42 /**
  43  * Try to ensure that methods which update {@link Input} or {@link OptionalInput} fields also
  44  * include a call to {@link Node#updateUsages} or {@link Node#updateUsagesInterface}.
  45  */
  46 public class VerifyUpdateUsages extends VerifyPhase<PhaseContext> {
  47 
  48     @Override
  49     public boolean checkContract() {
  50         return false;
  51     }
  52 
  53     public VerifyUpdateUsages() {
  54     }
  55 
  56     @Override
  57     protected boolean verify(StructuredGraph graph, PhaseContext context) {
  58         if (graph.method().isConstructor()) {
  59             return true;
  60         }
  61         /*
  62          * There are only two acceptable patterns for methods which update Node inputs, either a
  63          * single StoreField node and invoke of updateUsages or updateUsagesInterface, or 2
  64          * StoreFields that come from LoadFields on the same object. Other patterns can be added as
  65          * needed but it would be best to keep things simple so that verification can be simple.
  66          */
  67         List<StoreFieldNode> stores = graph.getNodes().filter(StoreFieldNode.class).snapshot();
  68         ResolvedJavaType declaringClass = graph.method().getDeclaringClass();
  69         ResolvedJavaType nodeInputList = context.getMetaAccess().lookupJavaType(NodeInputList.class);
  70         StoreFieldNode storeField1 = null;
  71         StoreFieldNode storeField2 = null;
  72         for (StoreFieldNode store : stores) {
  73             if (isNodeInput(store.field(), declaringClass, nodeInputList)) {
  74                 if (storeField1 == null) {
  75                     storeField1 = store;
  76                 } else if (storeField2 == null) {
  77                     storeField2 = store;
  78                 } else {
  79                     return false;
  80                 }
  81             }
  82         }
  83         if (storeField1 == null) {
  84             return true;
  85         }
  86         if (storeField2 == null) {
  87             // Single input field update so just check for updateUsages or updateUsagesInterface
  88             // call
  89             ResolvedJavaType node = context.getMetaAccess().lookupJavaType(Node.class);
  90             for (MethodCallTargetNode call : graph.getNodes().filter(MethodCallTargetNode.class)) {
  91                 ResolvedJavaMethod callee = call.targetMethod();
  92                 if (callee.getDeclaringClass().equals(node) && (callee.getName().equals("updateUsages") || callee.getName().equals("updateUsagesInterface"))) {
  93                     return true;
  94                 }
  95             }
  96         } else {
  97             if (storeField1.value() instanceof LoadFieldNode && storeField2.value() instanceof LoadFieldNode) {
  98                 LoadFieldNode load1 = (LoadFieldNode) storeField1.value();
  99                 LoadFieldNode load2 = (LoadFieldNode) storeField2.value();
 100                 // Check for swapping values within the same object
 101                 if (load1.object() == storeField1.object() && load2.object() == storeField2.object() && storeField1.object() == storeField2.object() &&
 102                                 load1.field().equals(storeField2.field()) && load2.field().equals(storeField1.field())) {
 103                     return true;
 104                 }
 105             }
 106         }
 107         return false;
 108     }
 109 
 110     boolean isNodeInput(ResolvedJavaField field, ResolvedJavaType declaringClass, ResolvedJavaType nodeInputList) {
 111         return declaringClass.isAssignableFrom(field.getDeclaringClass()) && (field.getAnnotation(Input.class) != null || field.getAnnotation(OptionalInput.class) != null) &&
 112                         !field.getType().equals(nodeInputList);
 113     }
 114 }