43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.replacements;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
|
|
28 |
import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
|
|
29 |
import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER;
|
|
30 |
|
|
31 |
import java.util.Arrays;
|
|
32 |
|
|
33 |
import org.graalvm.compiler.api.replacements.Fold;
|
|
34 |
import org.graalvm.compiler.api.replacements.Snippet;
|
|
35 |
import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
|
|
36 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
|
37 |
import org.graalvm.compiler.core.common.type.StampFactory;
|
46640
|
38 |
import org.graalvm.compiler.debug.DebugHandlersFactory;
|
43972
|
39 |
import org.graalvm.compiler.debug.GraalError;
|
|
40 |
import org.graalvm.compiler.graph.NodeClass;
|
|
41 |
import org.graalvm.compiler.nodeinfo.NodeInfo;
|
|
42 |
import org.graalvm.compiler.nodes.FixedWithNextNode;
|
|
43 |
import org.graalvm.compiler.nodes.NamedLocationIdentity;
|
|
44 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
45 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
46 |
import org.graalvm.compiler.nodes.spi.Lowerable;
|
|
47 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
46344
|
48 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
49 |
import org.graalvm.compiler.phases.util.Providers;
|
|
50 |
import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates;
|
|
51 |
import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
|
|
52 |
import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
|
54328
|
53 |
import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess;
|
43972
|
54 |
import org.graalvm.compiler.word.ObjectAccess;
|
49873
|
55 |
import jdk.internal.vm.compiler.word.LocationIdentity;
|
43972
|
56 |
|
|
57 |
import jdk.vm.ci.code.TargetDescription;
|
|
58 |
import sun.misc.Unsafe;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* This node can be used to add a counter to the code that will estimate the dynamic number of calls
|
|
62 |
* by adding an increment to the compiled code. This should of course only be used for
|
|
63 |
* debugging/testing purposes.
|
|
64 |
*
|
|
65 |
* A unique counter will be created for each unique name passed to the constructor. Depending on the
|
|
66 |
* value of withContext, the name of the root method is added to the counter's name.
|
|
67 |
*/
|
|
68 |
@NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
|
|
69 |
public class SnippetCounterNode extends FixedWithNextNode implements Lowerable {
|
|
70 |
|
|
71 |
public static final NodeClass<SnippetCounterNode> TYPE = NodeClass.create(SnippetCounterNode.class);
|
|
72 |
|
|
73 |
@Input protected ValueNode increment;
|
|
74 |
|
|
75 |
protected final SnippetCounter counter;
|
|
76 |
|
|
77 |
public SnippetCounterNode(SnippetCounter counter, ValueNode increment) {
|
|
78 |
super(TYPE, StampFactory.forVoid());
|
|
79 |
this.counter = counter;
|
|
80 |
this.increment = increment;
|
|
81 |
}
|
|
82 |
|
|
83 |
public SnippetCounter getCounter() {
|
|
84 |
return counter;
|
|
85 |
}
|
|
86 |
|
|
87 |
public ValueNode getIncrement() {
|
|
88 |
return increment;
|
|
89 |
}
|
|
90 |
|
|
91 |
@NodeIntrinsic
|
|
92 |
public static native void add(@ConstantNodeParameter SnippetCounter counter, int increment);
|
|
93 |
|
|
94 |
public static void increment(@ConstantNodeParameter SnippetCounter counter) {
|
|
95 |
add(counter, 1);
|
|
96 |
}
|
|
97 |
|
|
98 |
@Override
|
|
99 |
public void lower(LoweringTool tool) {
|
|
100 |
if (graph().getGuardsStage().areFrameStatesAtDeopts()) {
|
|
101 |
SnippetCounterSnippets.Templates templates = tool.getReplacements().getSnippetTemplateCache(SnippetCounterSnippets.Templates.class);
|
|
102 |
templates.lower(this, tool);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
46344
|
107 |
* Add {@link #SNIPPET_COUNTER_LOCATION} to {@code privateLocations} if it isn't already there.
|
43972
|
108 |
*
|
|
109 |
* @param privateLocations
|
|
110 |
* @return a copy of privateLocations with any needed locations added
|
|
111 |
*/
|
|
112 |
public static LocationIdentity[] addSnippetCounters(LocationIdentity[] privateLocations) {
|
46344
|
113 |
for (LocationIdentity location : privateLocations) {
|
|
114 |
if (location.equals(SNIPPET_COUNTER_LOCATION)) {
|
|
115 |
return privateLocations;
|
43972
|
116 |
}
|
|
117 |
}
|
46344
|
118 |
LocationIdentity[] result = Arrays.copyOf(privateLocations, privateLocations.length + 1);
|
|
119 |
result[result.length - 1] = SnippetCounterNode.SNIPPET_COUNTER_LOCATION;
|
|
120 |
return result;
|
43972
|
121 |
}
|
|
122 |
|
|
123 |
/**
|
46344
|
124 |
* We do not want to use the {@link LocationIdentity} of the {@link SnippetCounter#value()}
|
|
125 |
* field, so that the usage in snippets is always possible. If a method accesses the counter via
|
|
126 |
* the field and the snippet, the result might not be correct though.
|
43972
|
127 |
*/
|
|
128 |
public static final LocationIdentity SNIPPET_COUNTER_LOCATION = NamedLocationIdentity.mutable("SnippetCounter");
|
|
129 |
|
|
130 |
static class SnippetCounterSnippets implements Snippets {
|
|
131 |
|
54328
|
132 |
private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe();
|
|
133 |
|
43972
|
134 |
@Fold
|
|
135 |
static int countOffset() {
|
|
136 |
try {
|
|
137 |
return (int) UNSAFE.objectFieldOffset(SnippetCounter.class.getDeclaredField("value"));
|
|
138 |
} catch (Exception e) {
|
|
139 |
throw new GraalError(e);
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
@Snippet
|
|
144 |
public static void add(@ConstantParameter SnippetCounter counter, int increment) {
|
|
145 |
long loadedValue = ObjectAccess.readLong(counter, countOffset(), SNIPPET_COUNTER_LOCATION);
|
|
146 |
ObjectAccess.writeLong(counter, countOffset(), loadedValue + increment, SNIPPET_COUNTER_LOCATION);
|
|
147 |
}
|
|
148 |
|
|
149 |
public static class Templates extends AbstractTemplates {
|
|
150 |
|
|
151 |
private final SnippetInfo add = snippet(SnippetCounterSnippets.class, "add", SNIPPET_COUNTER_LOCATION);
|
|
152 |
|
46640
|
153 |
Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, Providers providers, SnippetReflectionProvider snippetReflection, TargetDescription target) {
|
|
154 |
super(options, factories, providers, snippetReflection, target);
|
43972
|
155 |
}
|
|
156 |
|
|
157 |
public void lower(SnippetCounterNode counter, LoweringTool tool) {
|
|
158 |
StructuredGraph graph = counter.graph();
|
|
159 |
Arguments args = new Arguments(add, graph.getGuardsStage(), tool.getLoweringStage());
|
|
160 |
args.addConst("counter", counter.getCounter());
|
|
161 |
args.add("increment", counter.getIncrement());
|
|
162 |
|
49451
|
163 |
template(counter, args).instantiate(providers.getMetaAccess(), counter, DEFAULT_REPLACER, args);
|
43972
|
164 |
}
|
|
165 |
}
|
|
166 |
}
|
|
167 |
}
|