|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 2022, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.hosted.code; |
| 26 | + |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.HashSet; |
| 29 | +import java.util.Set; |
| 30 | + |
| 31 | +import org.graalvm.compiler.graph.NodeSourcePosition; |
| 32 | +import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind; |
| 33 | +import org.graalvm.compiler.nodes.EncodedGraph; |
| 34 | +import org.graalvm.compiler.nodes.GraphEncoder; |
| 35 | +import org.graalvm.compiler.nodes.StructuredGraph; |
| 36 | +import org.graalvm.compiler.nodes.java.MethodCallTargetNode; |
| 37 | + |
| 38 | +import com.oracle.graal.pointsto.flow.AnalysisParsedGraph; |
| 39 | +import com.oracle.svm.hosted.meta.HostedMethod; |
| 40 | + |
| 41 | +public final class CompilationGraph { |
| 42 | + |
| 43 | + public static class InvokeInfo { |
| 44 | + public final InvokeKind invokeKind; |
| 45 | + public final HostedMethod targetMethod; |
| 46 | + public final HostedMethod directCaller; |
| 47 | + public final NodeSourcePosition nodeSourcePosition; |
| 48 | + |
| 49 | + InvokeInfo(InvokeKind invokeKind, HostedMethod targetMethod, HostedMethod directCaller, NodeSourcePosition nodeSourcePosition) { |
| 50 | + this.invokeKind = invokeKind; |
| 51 | + this.targetMethod = targetMethod; |
| 52 | + this.directCaller = directCaller; |
| 53 | + this.nodeSourcePosition = nodeSourcePosition; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static class AllocationInfo { |
| 58 | + public final NodeSourcePosition nodeSourcePosition; |
| 59 | + |
| 60 | + AllocationInfo(NodeSourcePosition nodeSourcePosition) { |
| 61 | + this.nodeSourcePosition = nodeSourcePosition; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private final EncodedGraph encodedGraph; |
| 66 | + private final int nodeCount; |
| 67 | + private final Set<InvokeInfo> invokeInfos; |
| 68 | + private final Set<AllocationInfo> allocationInfos; |
| 69 | + |
| 70 | + private CompilationGraph(EncodedGraph encodedGraph, int nodeCount, Set<InvokeInfo> invokeInfos, Set<AllocationInfo> allocationInfos) { |
| 71 | + this.encodedGraph = encodedGraph; |
| 72 | + this.nodeCount = nodeCount; |
| 73 | + this.invokeInfos = invokeInfos; |
| 74 | + this.allocationInfos = allocationInfos; |
| 75 | + } |
| 76 | + |
| 77 | + static CompilationGraph encode(StructuredGraph graph) { |
| 78 | + Set<InvokeInfo> invokeInfos = new HashSet<>(); |
| 79 | + Set<AllocationInfo> allocationInfos = new HashSet<>(); |
| 80 | + for (var n : graph.getNodes()) { |
| 81 | + if (n instanceof MethodCallTargetNode) { |
| 82 | + MethodCallTargetNode node = (MethodCallTargetNode) n; |
| 83 | + invokeInfos.add(new InvokeInfo( |
| 84 | + node.invokeKind(), |
| 85 | + (HostedMethod) node.targetMethod(), |
| 86 | + (HostedMethod) node.invoke().stateAfter().getMethod(), |
| 87 | + node.getNodeSourcePosition())); |
| 88 | + } |
| 89 | + if (UninterruptibleAnnotationChecker.isAllocationNode(n)) { |
| 90 | + allocationInfos.add(new AllocationInfo(n.getNodeSourcePosition())); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return new CompilationGraph( |
| 95 | + GraphEncoder.encodeSingleGraph(graph, AnalysisParsedGraph.HOST_ARCHITECTURE), |
| 96 | + graph.getNodeCount(), |
| 97 | + invokeInfos.isEmpty() ? Collections.emptySet() : invokeInfos, |
| 98 | + allocationInfos.isEmpty() ? Collections.emptySet() : allocationInfos); |
| 99 | + } |
| 100 | + |
| 101 | + public EncodedGraph getEncodedGraph() { |
| 102 | + return encodedGraph; |
| 103 | + } |
| 104 | + |
| 105 | + public int getNodeCount() { |
| 106 | + return nodeCount; |
| 107 | + } |
| 108 | + |
| 109 | + public Set<InvokeInfo> getInvokeInfos() { |
| 110 | + return invokeInfos; |
| 111 | + } |
| 112 | + |
| 113 | + public Set<AllocationInfo> getAllocationInfos() { |
| 114 | + return allocationInfos; |
| 115 | + } |
| 116 | +} |
0 commit comments