hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/ReplacementsImpl.java
changeset 46640 70bdce04c59b
parent 46459 7d4e637d3f21
--- a/hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/ReplacementsImpl.java	Fri Jul 07 10:37:52 2017 +0200
+++ b/hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/ReplacementsImpl.java	Fri Jul 07 09:40:47 2017 -0700
@@ -23,14 +23,18 @@
 package org.graalvm.compiler.replacements;
 
 import static org.graalvm.compiler.core.common.GraalOptions.UseSnippetGraphCache;
+import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM;
 import static org.graalvm.compiler.java.BytecodeParserOptions.InlineDuringParsing;
 import static org.graalvm.compiler.java.BytecodeParserOptions.InlineIntrinsicsDuringParsing;
 import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.createIntrinsicInlineInfo;
 import static org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext.CompilationContext.INLINE_AFTER_PARSING;
 import static org.graalvm.compiler.phases.common.DeadCodeEliminationPhase.Optionality.Required;
 
+import java.util.Collections;
+import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.graalvm.compiler.api.replacements.Fold;
 import org.graalvm.compiler.api.replacements.MethodSubstitution;
@@ -42,11 +46,12 @@
 import org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode;
 import org.graalvm.compiler.core.common.GraalOptions;
 import org.graalvm.compiler.core.common.spi.ConstantFieldProvider;
-import org.graalvm.compiler.debug.Debug;
-import org.graalvm.compiler.debug.Debug.Scope;
 import org.graalvm.compiler.debug.DebugCloseable;
-import org.graalvm.compiler.debug.DebugTimer;
+import org.graalvm.compiler.debug.DebugHandlersFactory;
+import org.graalvm.compiler.debug.DebugContext;
+import org.graalvm.compiler.debug.DebugContext.Description;
 import org.graalvm.compiler.debug.GraalError;
+import org.graalvm.compiler.debug.TimerKey;
 import org.graalvm.compiler.graph.Node;
 import org.graalvm.compiler.graph.Node.NodeIntrinsic;
 import org.graalvm.compiler.java.GraphBuilderPhase;
@@ -92,6 +97,7 @@
     public final SnippetReflectionProvider snippetReflection;
     public final TargetDescription target;
     private GraphBuilderConfiguration.Plugins graphBuilderPlugins;
+    private final DebugHandlersFactory debugHandlersFactory;
 
     @Override
     public OptionValues getOptions() {
@@ -180,7 +186,8 @@
     // it is stable across VM executions (in support of replay compilation).
     private final EconomicMap<String, SnippetTemplateCache> snippetTemplateCache;
 
-    public ReplacementsImpl(OptionValues options, Providers providers, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider, TargetDescription target) {
+    public ReplacementsImpl(OptionValues options, DebugHandlersFactory debugHandlersFactory, Providers providers, SnippetReflectionProvider snippetReflection, BytecodeProvider bytecodeProvider,
+                    TargetDescription target) {
         this.options = options;
         this.providers = providers.copyWith(this);
         this.snippetReflection = snippetReflection;
@@ -188,15 +195,26 @@
         this.graphs = new ConcurrentHashMap<>();
         this.snippetTemplateCache = EconomicMap.create(Equivalence.DEFAULT);
         this.defaultBytecodeProvider = bytecodeProvider;
+        this.debugHandlersFactory = debugHandlersFactory;
+
     }
 
-    private static final DebugTimer SnippetPreparationTime = Debug.timer("SnippetPreparationTime");
+    private static final TimerKey SnippetPreparationTime = DebugContext.timer("SnippetPreparationTime");
 
     @Override
     public StructuredGraph getSnippet(ResolvedJavaMethod method, Object[] args) {
         return getSnippet(method, null, args);
     }
 
+    private static final AtomicInteger nextDebugContextId = new AtomicInteger();
+
+    protected DebugContext openDebugContext(String idPrefix, ResolvedJavaMethod method) {
+        DebugContext outer = DebugContext.forCurrentThread();
+        Description description = new Description(method, idPrefix + nextDebugContextId.incrementAndGet());
+        List<DebugHandlersFactory> factories = debugHandlersFactory == null ? Collections.emptyList() : Collections.singletonList(debugHandlersFactory);
+        return DebugContext.create(options, description, outer.getGlobalMetrics(), DEFAULT_LOG_STREAM, factories);
+    }
+
     @Override
     @SuppressWarnings("try")
     public StructuredGraph getSnippet(ResolvedJavaMethod method, ResolvedJavaMethod recursiveEntry, Object[] args) {
@@ -205,12 +223,14 @@
 
         StructuredGraph graph = UseSnippetGraphCache.getValue(options) ? graphs.get(method) : null;
         if (graph == null) {
-            try (DebugCloseable a = SnippetPreparationTime.start()) {
-                StructuredGraph newGraph = makeGraph(defaultBytecodeProvider, method, args, recursiveEntry);
-                Debug.counter("SnippetNodeCount[%#s]", method).add(newGraph.getNodeCount());
+            try (DebugContext debug = openDebugContext("Snippet_", method);
+                            DebugCloseable a = SnippetPreparationTime.start(debug)) {
+                StructuredGraph newGraph = makeGraph(debug, defaultBytecodeProvider, method, args, recursiveEntry);
+                DebugContext.counter("SnippetNodeCount[%#s]", method).add(newGraph.getDebug(), newGraph.getNodeCount());
                 if (!UseSnippetGraphCache.getValue(options) || args != null) {
                     return newGraph;
                 }
+                newGraph.freeze();
                 graphs.putIfAbsent(method, newGraph);
                 graph = graphs.get(method);
             }
@@ -256,10 +276,12 @@
                 ResolvedJavaMethod substitute = msPlugin.getSubstitute(metaAccess);
                 StructuredGraph graph = graphs.get(substitute);
                 if (graph == null) {
-                    graph = makeGraph(msPlugin.getBytecodeProvider(), substitute, null, method);
-                    graph.freeze();
-                    graphs.putIfAbsent(substitute, graph);
-                    graph = graphs.get(substitute);
+                    try (DebugContext debug = openDebugContext("Substitution_", method)) {
+                        graph = makeGraph(debug, msPlugin.getBytecodeProvider(), substitute, null, method);
+                        graph.freeze();
+                        graphs.putIfAbsent(substitute, graph);
+                        graph = graphs.get(substitute);
+                    }
                 }
                 assert graph.isFrozen();
                 result = graph;
@@ -268,7 +290,9 @@
                 ConstantReflectionProvider constantReflection = providers.getConstantReflection();
                 ConstantFieldProvider constantFieldProvider = providers.getConstantFieldProvider();
                 StampProvider stampProvider = providers.getStampProvider();
-                result = new IntrinsicGraphBuilder(options, metaAccess, constantReflection, constantFieldProvider, stampProvider, code, invokeBci).buildGraph(plugin);
+                try (DebugContext debug = openDebugContext("Substitution_", method)) {
+                    result = new IntrinsicGraphBuilder(options, debug, metaAccess, constantReflection, constantFieldProvider, stampProvider, code, invokeBci).buildGraph(plugin);
+                }
             }
         } else {
             result = null;
@@ -285,8 +309,8 @@
      * @param original the original method if {@code method} is a {@linkplain MethodSubstitution
      *            substitution} otherwise null
      */
-    public StructuredGraph makeGraph(BytecodeProvider bytecodeProvider, ResolvedJavaMethod method, Object[] args, ResolvedJavaMethod original) {
-        return createGraphMaker(method, original).makeGraph(bytecodeProvider, args);
+    public StructuredGraph makeGraph(DebugContext debug, BytecodeProvider bytecodeProvider, ResolvedJavaMethod method, Object[] args, ResolvedJavaMethod original) {
+        return createGraphMaker(method, original).makeGraph(debug, bytecodeProvider, args);
     }
 
     /**
@@ -323,18 +347,18 @@
         }
 
         @SuppressWarnings("try")
-        public StructuredGraph makeGraph(BytecodeProvider bytecodeProvider, Object[] args) {
-            try (Scope s = Debug.scope("BuildSnippetGraph", method)) {
+        public StructuredGraph makeGraph(DebugContext debug, BytecodeProvider bytecodeProvider, Object[] args) {
+            try (DebugContext.Scope s = debug.scope("BuildSnippetGraph", method)) {
                 assert method.hasBytecodes() : method;
-                StructuredGraph graph = buildInitialGraph(bytecodeProvider, method, args);
+                StructuredGraph graph = buildInitialGraph(debug, bytecodeProvider, method, args);
 
                 finalizeGraph(graph);
 
-                Debug.dump(Debug.INFO_LEVEL, graph, "%s: Final", method.getName());
+                debug.dump(DebugContext.INFO_LEVEL, graph, "%s: Final", method.getName());
 
                 return graph;
             } catch (Throwable e) {
-                throw Debug.handle(e);
+                throw debug.handle(e);
             }
         }
 
@@ -390,16 +414,16 @@
          * Builds the initial graph for a replacement.
          */
         @SuppressWarnings("try")
-        protected StructuredGraph buildInitialGraph(BytecodeProvider bytecodeProvider, final ResolvedJavaMethod methodToParse, Object[] args) {
+        protected StructuredGraph buildInitialGraph(DebugContext debug, BytecodeProvider bytecodeProvider, final ResolvedJavaMethod methodToParse, Object[] args) {
             // Replacements cannot have optimistic assumptions since they have
             // to be valid for the entire run of the VM.
+            final StructuredGraph graph = new StructuredGraph.Builder(replacements.options, debug).method(methodToParse).build();
 
-            final StructuredGraph graph = new StructuredGraph.Builder(replacements.options).method(methodToParse).build();
-
-            // They are not user code so they do not participate in unsafe access tracking
+            // Replacements are not user code so they do not participate in unsafe access
+            // tracking
             graph.disableUnsafeAccessTracking();
 
-            try (Scope s = Debug.scope("buildInitialGraph", graph)) {
+            try (DebugContext.Scope s = debug.scope("buildInitialGraph", graph)) {
                 MetaAccessProvider metaAccess = replacements.providers.getMetaAccess();
 
                 Plugins plugins = new Plugins(replacements.graphBuilderPlugins);
@@ -423,7 +447,7 @@
 
                 new CanonicalizerPhase().apply(graph, new PhaseContext(replacements.providers));
             } catch (Throwable e) {
-                throw Debug.handle(e);
+                throw debug.handle(e);
             }
             return graph;
         }