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.Debug;
  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         NodeFlood flood = graph.createNodeFlood();
  55         EconomicMap<Node, Node> path = EconomicMap.create(Equivalence.IDENTITY);
  56         flood.add(graph.start());
  57         for (Node current : flood) {
  58             if (current instanceof AbstractEndNode) {
  59                 AbstractEndNode end = (AbstractEndNode) current;
  60                 flood.add(end.merge());
  61                 if (!path.containsKey(end.merge())) {
  62                     path.put(end.merge(), end);
  63                 }
  64             } else {
  65                 for (Node successor : current.successors()) {
  66                     flood.add(successor);
  67                     if (!path.containsKey(successor)) {
  68                         path.put(successor, current);
  69                     }
  70                 }
  71             }
  72         }
  73 
  74         for (Node node : obsoleteNodes) {
  75             if (node instanceof FixedNode && !node.isDeleted()) {
  76                 assert !flood.isMarked(node) : node;
  77             }
  78         }
  79 
  80         for (Node node : graph.getNodes()) {
  81             if (flood.isMarked(node)) {
  82                 for (Node input : node.inputs()) {
  83                     flood.add(input);
  84                     if (!path.containsKey(input)) {
  85                         path.put(input, node);
  86                     }
  87                 }
  88             }
  89         }
  90         for (Node current : flood) {
  91             for (Node input : current.inputs()) {
  92                 flood.add(input);
  93                 if (!path.containsKey(input)) {
  94                     path.put(input, current);
  95                 }
  96             }
  97         }
  98         boolean success = true;
  99         for (Node node : obsoleteNodes) {
 100             if (!node.isDeleted() && flood.isMarked(node) && !node.getNodeClass().valueNumberable()) {
 101                 TTY.println("offending node path:");
 102                 Node current = node;
 103                 TTY.print(current.toString());
 104                 while (true) {
 105                     current = path.get(current);
 106                     if (current != null) {
 107                         TTY.print(" -> " + current.toString());
 108                         if (current instanceof FixedNode && !obsoleteNodes.contains(current)) {
 109                             break;
 110                         }
 111                     }
 112                 }
 113                 success = false;
 114             }
 115         }
 116         if (!success) {
 117             TTY.println();
 118             Debug.forceDump(graph, "assertNonReachable");
 119         }
 120         return success;
 121     }
 122 
 123     public static void trace(OptionValues options, String msg) {
 124         if (Debug.isEnabled() && TraceEscapeAnalysis.getValue(options) && Debug.isLogEnabled()) {
 125             Debug.log(msg);
 126         }
 127     }
 128 
 129     public static void trace(OptionValues options, String format, Object obj) {
 130         if (Debug.isEnabled() && TraceEscapeAnalysis.getValue(options) && Debug.isLogEnabled()) {
 131             Debug.logv(format, obj);
 132         }
 133     }
 134 
 135     public static void trace(OptionValues options, String format, Object obj, Object obj2) {
 136         if (Debug.isEnabled() && TraceEscapeAnalysis.getValue(options) && Debug.isLogEnabled()) {
 137             Debug.logv(format, obj, obj2);
 138         }
 139     }
 140 
 141     public static void trace(OptionValues options, String format, Object obj, Object obj2, Object obj3) {
 142         if (Debug.isEnabled() && TraceEscapeAnalysis.getValue(options) && Debug.isLogEnabled()) {
 143             Debug.logv(format, obj, obj2, obj3);
 144         }
 145     }
 146 
 147     public static void trace(OptionValues options, String format, Object obj, Object obj2, Object obj3, Object obj4) {
 148         if (Debug.isEnabled() && TraceEscapeAnalysis.getValue(options) && Debug.isLogEnabled()) {
 149             Debug.logv(format, obj, obj2, obj3, obj4);
 150         }
 151     }
 152 
 153     public static boolean matches(StructuredGraph graph, String filter) {
 154         if (filter != null) {
 155             return matchesHelper(graph, filter);
 156         }
 157         return true;
 158     }
 159 
 160     private static boolean matchesHelper(StructuredGraph graph, String filter) {
 161         if (filter.startsWith("~")) {
 162             ResolvedJavaMethod method = graph.method();
 163             return method == null || !method.format("%H.%n").contains(filter.substring(1));
 164         } else {
 165             ResolvedJavaMethod method = graph.method();
 166             return method != null && method.format("%H.%n").contains(filter);
 167         }
 168     }
 169 }