1 /*
   2  * Copyright (c) 2013, 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.virtual.phases.ea;
  24 
  25 import static org.graalvm.compiler.core.common.GraalOptions.TraceEscapeAnalysis;
  26 
  27 import java.util.List;
  28 
  29 import org.graalvm.compiler.debug.DebugContext;
  30 import org.graalvm.compiler.debug.GraalError;
  31 import org.graalvm.compiler.debug.TTY;
  32 import org.graalvm.compiler.graph.Node;
  33 import org.graalvm.compiler.graph.NodeFlood;
  34 import org.graalvm.compiler.nodes.AbstractEndNode;
  35 import org.graalvm.compiler.nodes.FixedNode;
  36 import org.graalvm.compiler.nodes.StructuredGraph;
  37 import org.graalvm.compiler.options.OptionValues;
  38 import org.graalvm.util.EconomicMap;
  39 import org.graalvm.util.Equivalence;
  40 
  41 import jdk.vm.ci.meta.ResolvedJavaMethod;
  42 
  43 public final class VirtualUtil {
  44 
  45     private VirtualUtil() {
  46         GraalError.shouldNotReachHere();
  47     }
  48 
  49     public static boolean assertNonReachable(StructuredGraph graph, List<Node> obsoleteNodes) {
  50         // Helper code that determines the paths that keep obsolete nodes alive.
  51         // Nodes with support for GVN can be kept alive by GVN and are therefore not part of the
  52         // assertion.
  53 
  54         DebugContext debug = graph.getDebug();
  55         NodeFlood flood = graph.createNodeFlood();
  56         EconomicMap<Node, Node> path = EconomicMap.create(Equivalence.IDENTITY);
  57         flood.add(graph.start());
  58         for (Node current : flood) {
  59             if (current instanceof AbstractEndNode) {
  60                 AbstractEndNode end = (AbstractEndNode) current;
  61                 flood.add(end.merge());
  62                 if (!path.containsKey(end.merge())) {
  63                     path.put(end.merge(), end);
  64                 }
  65             } else {
  66                 for (Node successor : current.successors()) {
  67                     flood.add(successor);
  68                     if (!path.containsKey(successor)) {
  69                         path.put(successor, current);
  70                     }
  71                 }
  72             }
  73         }
  74 
  75         for (Node node : obsoleteNodes) {
  76             if (node instanceof FixedNode && !node.isDeleted()) {
  77                 assert !flood.isMarked(node) : node;
  78             }
  79         }
  80 
  81         for (Node node : graph.getNodes()) {
  82             if (flood.isMarked(node)) {
  83                 for (Node input : node.inputs()) {
  84                     flood.add(input);
  85                     if (!path.containsKey(input)) {
  86                         path.put(input, node);
  87                     }
  88                 }
  89             }
  90         }
  91         for (Node current : flood) {
  92             for (Node input : current.inputs()) {
  93                 flood.add(input);
  94                 if (!path.containsKey(input)) {
  95                     path.put(input, current);
  96                 }
  97             }
  98         }
  99         boolean success = true;
 100         for (Node node : obsoleteNodes) {
 101             if (!node.isDeleted() && flood.isMarked(node) && !node.getNodeClass().valueNumberable()) {
 102                 TTY.println("offending node path:");
 103                 Node current = node;
 104                 TTY.print(current.toString());
 105                 while (true) {
 106                     current = path.get(current);
 107                     if (current != null) {
 108                         TTY.print(" -> " + current.toString());
 109                         if (current instanceof FixedNode && !obsoleteNodes.contains(current)) {
 110                             break;
 111                         }
 112                     }
 113                 }
 114                 success = false;
 115             }
 116         }
 117         if (!success) {
 118             TTY.println();
 119             debug.forceDump(graph, "assertNonReachable");
 120         }
 121         return success;
 122     }
 123 
 124     public static void trace(OptionValues options, DebugContext debug, String msg) {
 125         if (debug.areScopesEnabled() && TraceEscapeAnalysis.getValue(options) && debug.isLogEnabled()) {
 126             debug.log(msg);
 127         }
 128     }
 129 
 130     public static void trace(OptionValues options, DebugContext debug, String format, Object obj) {
 131         if (debug.areScopesEnabled() && TraceEscapeAnalysis.getValue(options) && debug.isLogEnabled()) {
 132             debug.logv(format, obj);
 133         }
 134     }
 135 
 136     public static void trace(OptionValues options, DebugContext debug, String format, Object obj, Object obj2) {
 137         if (debug.areScopesEnabled() && TraceEscapeAnalysis.getValue(options) && debug.isLogEnabled()) {
 138             debug.logv(format, obj, obj2);
 139         }
 140     }
 141 
 142     public static void trace(OptionValues options, DebugContext debug, String format, Object obj, Object obj2, Object obj3) {
 143         if (debug.areScopesEnabled() && TraceEscapeAnalysis.getValue(options) && debug.isLogEnabled()) {
 144             debug.logv(format, obj, obj2, obj3);
 145         }
 146     }
 147 
 148     public static void trace(OptionValues options, DebugContext debug, String format, Object obj, Object obj2, Object obj3, Object obj4) {
 149         if (debug.areScopesEnabled() && TraceEscapeAnalysis.getValue(options) && debug.isLogEnabled()) {
 150             debug.logv(format, obj, obj2, obj3, obj4);
 151         }
 152     }
 153 
 154     public static boolean matches(StructuredGraph graph, String filter) {
 155         if (filter != null) {
 156             return matchesHelper(graph, filter);
 157         }
 158         return true;
 159     }
 160 
 161     private static boolean matchesHelper(StructuredGraph graph, String filter) {
 162         if (filter.startsWith("~")) {
 163             ResolvedJavaMethod method = graph.method();
 164             return method == null || !method.format("%H.%n").contains(filter.substring(1));
 165         } else {
 166             ResolvedJavaMethod method = graph.method();
 167             return method != null && method.format("%H.%n").contains(filter);
 168         }
 169     }
 170 }