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