52910
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
52910
|
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 |
|
|
24 |
|
|
25 |
package org.graalvm.compiler.replacements.arraycopy;
|
|
26 |
|
54084
|
27 |
import static jdk.vm.ci.services.Services.IS_BUILDING_NATIVE_IMAGE;
|
58299
|
28 |
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
|
52910
|
29 |
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.FREQUENT_PROBABILITY;
|
|
30 |
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.LIKELY_PROBABILITY;
|
|
31 |
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.NOT_FREQUENT_PROBABILITY;
|
|
32 |
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.SLOW_PATH_PROBABILITY;
|
58299
|
33 |
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.DEOPT_PROBABILITY;
|
52910
|
34 |
import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability;
|
|
35 |
|
|
36 |
import java.util.EnumMap;
|
|
37 |
|
|
38 |
import jdk.internal.vm.compiler.collections.UnmodifiableEconomicMap;
|
|
39 |
import org.graalvm.compiler.api.directives.GraalDirectives;
|
|
40 |
import org.graalvm.compiler.api.replacements.Fold.InjectedParameter;
|
|
41 |
import org.graalvm.compiler.api.replacements.Snippet;
|
|
42 |
import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter;
|
|
43 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
|
44 |
import org.graalvm.compiler.debug.DebugHandlersFactory;
|
|
45 |
import org.graalvm.compiler.debug.GraalError;
|
|
46 |
import org.graalvm.compiler.graph.Node;
|
|
47 |
import org.graalvm.compiler.nodes.CallTargetNode;
|
|
48 |
import org.graalvm.compiler.nodes.DeoptimizeNode;
|
|
49 |
import org.graalvm.compiler.nodes.InvokeNode;
|
|
50 |
import org.graalvm.compiler.nodes.InvokeWithExceptionNode;
|
|
51 |
import org.graalvm.compiler.nodes.NamedLocationIdentity;
|
|
52 |
import org.graalvm.compiler.nodes.NodeView;
|
|
53 |
import org.graalvm.compiler.nodes.PiNode;
|
54601
|
54 |
import org.graalvm.compiler.nodes.SnippetAnchorNode;
|
52910
|
55 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
54601
|
56 |
import org.graalvm.compiler.nodes.extended.GuardedUnsafeLoadNode;
|
|
57 |
import org.graalvm.compiler.nodes.extended.GuardingNode;
|
52910
|
58 |
import org.graalvm.compiler.nodes.extended.RawStoreNode;
|
|
59 |
import org.graalvm.compiler.nodes.java.ArrayLengthNode;
|
|
60 |
import org.graalvm.compiler.nodes.spi.LoweringTool;
|
|
61 |
import org.graalvm.compiler.nodes.type.StampTool;
|
|
62 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
|
63 |
import org.graalvm.compiler.options.OptionValues;
|
|
64 |
import org.graalvm.compiler.phases.util.Providers;
|
|
65 |
import org.graalvm.compiler.replacements.ReplacementsUtil;
|
|
66 |
import org.graalvm.compiler.replacements.SnippetCounter;
|
|
67 |
import org.graalvm.compiler.replacements.SnippetCounter.Group;
|
|
68 |
import org.graalvm.compiler.replacements.SnippetCounter.Group.Factory;
|
|
69 |
import org.graalvm.compiler.replacements.SnippetIntegerHistogram;
|
|
70 |
import org.graalvm.compiler.replacements.SnippetTemplate;
|
|
71 |
import org.graalvm.compiler.replacements.SnippetTemplate.Arguments;
|
|
72 |
import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo;
|
|
73 |
import org.graalvm.compiler.replacements.Snippets;
|
|
74 |
import org.graalvm.compiler.replacements.nodes.BasicArrayCopyNode;
|
|
75 |
import org.graalvm.compiler.word.Word;
|
|
76 |
import jdk.internal.vm.compiler.word.LocationIdentity;
|
|
77 |
import jdk.internal.vm.compiler.word.Pointer;
|
|
78 |
|
|
79 |
import jdk.vm.ci.code.TargetDescription;
|
|
80 |
import jdk.vm.ci.meta.DeoptimizationAction;
|
|
81 |
import jdk.vm.ci.meta.DeoptimizationReason;
|
|
82 |
import jdk.vm.ci.meta.JavaKind;
|
|
83 |
import jdk.vm.ci.meta.MetaAccessProvider;
|
|
84 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
85 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
86 |
|
|
87 |
public abstract class ArrayCopySnippets implements Snippets {
|
|
88 |
|
|
89 |
private enum ArrayCopyTypeCheck {
|
|
90 |
UNDEFINED_ARRAY_TYPE_CHECK,
|
|
91 |
// either we know that both objects are arrays and have the same type,
|
|
92 |
// or we apply generic array copy snippet, which enforces type check
|
|
93 |
NO_ARRAY_TYPE_CHECK,
|
|
94 |
// can be used when we know that one of the objects is a primitive array
|
|
95 |
HUB_BASED_ARRAY_TYPE_CHECK,
|
|
96 |
// can be used when we know that one of the objects is an object array
|
|
97 |
LAYOUT_HELPER_BASED_ARRAY_TYPE_CHECK
|
|
98 |
}
|
|
99 |
|
|
100 |
/** Marker value for the {@link InjectedParameter} injected parameter. */
|
|
101 |
static final MetaAccessProvider INJECTED_META_ACCESS = null;
|
|
102 |
|
|
103 |
public abstract Pointer loadHub(Object nonNullSrc);
|
|
104 |
|
|
105 |
public abstract Pointer getDestElemClass(Pointer destKlass);
|
|
106 |
|
|
107 |
public abstract Word getSuperCheckOffset(Pointer destElemKlass);
|
|
108 |
|
|
109 |
public abstract int getReadLayoutHelper(Pointer srcHub);
|
|
110 |
|
|
111 |
protected abstract int heapWordSize();
|
|
112 |
|
54601
|
113 |
@SuppressWarnings("unused")
|
52910
|
114 |
@Snippet
|
|
115 |
public void arraycopyExactSnippet(Object src, int srcPos, Object dest, int destPos, int length, @ConstantParameter ArrayCopyTypeCheck arrayTypeCheck,
|
54601
|
116 |
@ConstantParameter SnippetInfo workSnippet, @ConstantParameter JavaKind elementKind, @ConstantParameter LocationIdentity locationIdentity,
|
54084
|
117 |
@ConstantParameter SnippetCounter elementKindCounter, @ConstantParameter SnippetCounter elementKindCopiedCounter, @ConstantParameter Counters counters) {
|
52910
|
118 |
Object nonNullSrc = GraalDirectives.guardingNonNull(src);
|
|
119 |
Object nonNullDest = GraalDirectives.guardingNonNull(dest);
|
|
120 |
checkArrayTypes(nonNullSrc, nonNullDest, arrayTypeCheck);
|
|
121 |
checkLimits(nonNullSrc, srcPos, nonNullDest, destPos, length, counters);
|
|
122 |
incrementLengthCounter(length, counters);
|
|
123 |
|
|
124 |
elementKindCounter.inc();
|
|
125 |
elementKindCopiedCounter.add(length);
|
54601
|
126 |
|
|
127 |
ArrayCopyWithDelayedLoweringNode.arraycopy(nonNullSrc, srcPos, nonNullDest, destPos, length, workSnippet, elementKind);
|
52910
|
128 |
}
|
|
129 |
|
54601
|
130 |
@SuppressWarnings("unused")
|
52910
|
131 |
@Snippet
|
54601
|
132 |
public void arraycopyExactStubCallSnippet(Object src, int srcPos, Object dest, int destPos, int length, @ConstantParameter ArrayCopyTypeCheck arrayTypeCheck,
|
|
133 |
@ConstantParameter SnippetInfo workSnippet, @ConstantParameter JavaKind elementKind, @ConstantParameter LocationIdentity locationIdentity,
|
|
134 |
@ConstantParameter SnippetCounter elementKindCounter, @ConstantParameter SnippetCounter elementKindCopiedCounter, @ConstantParameter Counters counters) {
|
52910
|
135 |
Object nonNullSrc = GraalDirectives.guardingNonNull(src);
|
|
136 |
Object nonNullDest = GraalDirectives.guardingNonNull(dest);
|
|
137 |
checkArrayTypes(nonNullSrc, nonNullDest, arrayTypeCheck);
|
|
138 |
checkLimits(nonNullSrc, srcPos, nonNullDest, destPos, length, counters);
|
|
139 |
incrementLengthCounter(length, counters);
|
|
140 |
|
54601
|
141 |
elementKindCounter.inc();
|
|
142 |
elementKindCopiedCounter.add(length);
|
|
143 |
|
|
144 |
ArrayCopyCallNode.arraycopy(nonNullSrc, srcPos, nonNullDest, destPos, length, elementKind, locationIdentity, heapWordSize());
|
52910
|
145 |
}
|
|
146 |
|
|
147 |
@Snippet
|
54601
|
148 |
public void arraycopyCheckcastSnippet(Object src, int srcPos, Object dest, int destPos, int length, @ConstantParameter ArrayCopyTypeCheck arrayTypeCheck, @ConstantParameter Counters counters,
|
|
149 |
@ConstantParameter SnippetInfo workSnippet, @ConstantParameter JavaKind elementKind) {
|
52910
|
150 |
Object nonNullSrc = GraalDirectives.guardingNonNull(src);
|
|
151 |
Object nonNullDest = GraalDirectives.guardingNonNull(dest);
|
|
152 |
checkArrayTypes(nonNullSrc, nonNullDest, arrayTypeCheck);
|
|
153 |
checkLimits(nonNullSrc, srcPos, nonNullDest, destPos, length, counters);
|
|
154 |
incrementLengthCounter(length, counters);
|
|
155 |
|
54601
|
156 |
ArrayCopyWithDelayedLoweringNode.arraycopy(nonNullSrc, srcPos, nonNullDest, destPos, length, workSnippet, elementKind);
|
52910
|
157 |
}
|
|
158 |
|
|
159 |
@Snippet
|
|
160 |
public void arraycopyGenericSnippet(Object src, int srcPos, Object dest, int destPos, int length, @ConstantParameter ArrayCopyTypeCheck arrayTypeCheck, @ConstantParameter Counters counters,
|
|
161 |
@ConstantParameter SnippetInfo workSnippet, @ConstantParameter JavaKind elementKind) {
|
|
162 |
Object nonNullSrc = GraalDirectives.guardingNonNull(src);
|
|
163 |
Object nonNullDest = GraalDirectives.guardingNonNull(dest);
|
|
164 |
checkArrayTypes(nonNullSrc, nonNullDest, arrayTypeCheck);
|
|
165 |
checkLimits(nonNullSrc, srcPos, nonNullDest, destPos, length, counters);
|
|
166 |
incrementLengthCounter(length, counters);
|
|
167 |
|
54601
|
168 |
ArrayCopyWithDelayedLoweringNode.arraycopy(nonNullSrc, srcPos, nonNullDest, destPos, length, workSnippet, elementKind);
|
52910
|
169 |
}
|
|
170 |
|
|
171 |
@Snippet
|
|
172 |
public static void arraycopyNativeSnippet(Object src, int srcPos, Object dest, int destPos, int length, @ConstantParameter Counters counters) {
|
|
173 |
// all checks are done in the native method, so no need to emit additional checks here
|
|
174 |
incrementLengthCounter(length, counters);
|
|
175 |
counters.systemArraycopyCounter.inc();
|
|
176 |
counters.systemArraycopyCopiedCounter.add(length);
|
|
177 |
|
|
178 |
System.arraycopy(src, srcPos, dest, destPos, length);
|
|
179 |
}
|
|
180 |
|
54601
|
181 |
@SuppressWarnings("unused")
|
|
182 |
@Snippet(allowPartialIntrinsicArgumentMismatch = true)
|
|
183 |
public void exactArraycopyWithSlowPathWork(Object src, int srcPos, Object dest, int destPos, int length, @ConstantParameter JavaKind elementKind, @ConstantParameter LocationIdentity arrayLocation,
|
|
184 |
@ConstantParameter Counters counters) {
|
52910
|
185 |
int scale = ReplacementsUtil.arrayIndexScale(INJECTED_META_ACCESS, elementKind);
|
|
186 |
int arrayBaseOffset = ReplacementsUtil.getArrayBaseOffset(INJECTED_META_ACCESS, elementKind);
|
|
187 |
long sourceOffset = arrayBaseOffset + (long) srcPos * scale;
|
|
188 |
long destOffset = arrayBaseOffset + (long) destPos * scale;
|
54601
|
189 |
|
|
190 |
GuardingNode anchor = SnippetAnchorNode.anchor();
|
|
191 |
if (probability(NOT_FREQUENT_PROBABILITY, src == dest && srcPos < destPos)) {
|
52910
|
192 |
// bad aliased case so we need to copy the array from back to front
|
54601
|
193 |
for (int position = length - 1; position >= 0; position--) {
|
|
194 |
Object value = GuardedUnsafeLoadNode.guardedLoad(src, sourceOffset + ((long) position) * scale, elementKind, arrayLocation, anchor);
|
|
195 |
RawStoreNode.storeObject(dest, destOffset + ((long) position) * scale, value, elementKind, arrayLocation, true);
|
|
196 |
}
|
|
197 |
} else {
|
|
198 |
for (int position = 0; position < length; position++) {
|
|
199 |
Object value = GuardedUnsafeLoadNode.guardedLoad(src, sourceOffset + ((long) position) * scale, elementKind, arrayLocation, anchor);
|
|
200 |
RawStoreNode.storeObject(dest, destOffset + ((long) position) * scale, value, elementKind, arrayLocation, true);
|
|
201 |
}
|
52910
|
202 |
}
|
|
203 |
}
|
|
204 |
|
54601
|
205 |
@SuppressWarnings("unused")
|
52910
|
206 |
@Snippet(allowPartialIntrinsicArgumentMismatch = true)
|
54601
|
207 |
public void checkcastArraycopyWithSlowPathWork(Object src, int srcPos, Object dest, int destPos, int length, @ConstantParameter JavaKind elementKind,
|
|
208 |
@ConstantParameter LocationIdentity arrayLocation,
|
|
209 |
@ConstantParameter Counters counters) {
|
52910
|
210 |
if (probability(FREQUENT_PROBABILITY, length > 0)) {
|
|
211 |
Object nonNullSrc = PiNode.asNonNullObject(src);
|
|
212 |
Object nonNullDest = PiNode.asNonNullObject(dest);
|
|
213 |
Pointer srcKlass = loadHub(nonNullSrc);
|
|
214 |
Pointer destKlass = loadHub(nonNullDest);
|
58299
|
215 |
if (probability(LIKELY_PROBABILITY, srcKlass == destKlass) || probability(LIKELY_PROBABILITY, nonNullDest.getClass() == Object[].class)) {
|
52910
|
216 |
// no storecheck required.
|
|
217 |
counters.objectCheckcastSameTypeCounter.inc();
|
|
218 |
counters.objectCheckcastSameTypeCopiedCounter.add(length);
|
|
219 |
ArrayCopyCallNode.arraycopyObjectKillsAny(nonNullSrc, srcPos, nonNullDest, destPos, length, heapWordSize());
|
|
220 |
} else {
|
|
221 |
Pointer destElemKlass = getDestElemClass(destKlass);
|
|
222 |
Word superCheckOffset = getSuperCheckOffset(destElemKlass);
|
|
223 |
|
|
224 |
counters.objectCheckcastDifferentTypeCounter.inc();
|
|
225 |
counters.objectCheckcastDifferentTypeCopiedCounter.add(length);
|
|
226 |
|
58299
|
227 |
System.arraycopy(nonNullSrc, srcPos, nonNullDest, destPos, length);
|
52910
|
228 |
}
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
54601
|
232 |
@SuppressWarnings("unused")
|
52910
|
233 |
@Snippet(allowPartialIntrinsicArgumentMismatch = true)
|
54601
|
234 |
public void genericArraycopyWithSlowPathWork(Object src, int srcPos, Object dest, int destPos, int length, @ConstantParameter JavaKind elementKind,
|
|
235 |
@ConstantParameter LocationIdentity arrayLocation,
|
|
236 |
@ConstantParameter Counters counters) {
|
52910
|
237 |
// The length > 0 check should not be placed here because generic array copy stub should
|
|
238 |
// enforce type check. This is fine performance-wise because this snippet is rarely used.
|
|
239 |
counters.genericArraycopyDifferentTypeCounter.inc();
|
|
240 |
counters.genericArraycopyDifferentTypeCopiedCounter.add(length);
|
|
241 |
int copiedElements = GenericArrayCopyCallNode.genericArraycopy(src, srcPos, dest, destPos, length);
|
|
242 |
if (probability(SLOW_PATH_PROBABILITY, copiedElements != 0)) {
|
|
243 |
/*
|
|
244 |
* the stub doesn't throw the ArrayStoreException, but returns the number of copied
|
|
245 |
* elements (xor'd with -1).
|
|
246 |
*/
|
|
247 |
copiedElements ^= -1;
|
|
248 |
System.arraycopy(src, srcPos + copiedElements, dest, destPos + copiedElements, length - copiedElements);
|
|
249 |
}
|
|
250 |
}
|
|
251 |
|
|
252 |
private static void incrementLengthCounter(int length, Counters counters) {
|
54084
|
253 |
if (!IS_BUILDING_NATIVE_IMAGE) {
|
|
254 |
counters.lengthHistogram.inc(length);
|
|
255 |
}
|
52910
|
256 |
}
|
|
257 |
|
58299
|
258 |
/**
|
|
259 |
* Writing this as individual if statements to avoid a merge without a frame state.
|
|
260 |
*/
|
52910
|
261 |
private static void checkLimits(Object src, int srcPos, Object dest, int destPos, int length, Counters counters) {
|
58299
|
262 |
if (probability(DEOPT_PROBABILITY, srcPos < 0)) {
|
|
263 |
counters.checkAIOOBECounter.inc();
|
|
264 |
DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.RuntimeConstraint);
|
|
265 |
}
|
|
266 |
if (probability(DEOPT_PROBABILITY, destPos < 0)) {
|
|
267 |
counters.checkAIOOBECounter.inc();
|
|
268 |
DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.RuntimeConstraint);
|
|
269 |
}
|
|
270 |
if (probability(DEOPT_PROBABILITY, length < 0)) {
|
|
271 |
counters.checkAIOOBECounter.inc();
|
|
272 |
DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.RuntimeConstraint);
|
|
273 |
}
|
|
274 |
if (probability(DEOPT_PROBABILITY, srcPos > ArrayLengthNode.arrayLength(src) - length)) {
|
|
275 |
counters.checkAIOOBECounter.inc();
|
|
276 |
DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.RuntimeConstraint);
|
|
277 |
}
|
|
278 |
if (probability(DEOPT_PROBABILITY, destPos > ArrayLengthNode.arrayLength(dest) - length)) {
|
52910
|
279 |
counters.checkAIOOBECounter.inc();
|
|
280 |
DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.RuntimeConstraint);
|
|
281 |
}
|
|
282 |
counters.checkSuccessCounter.inc();
|
|
283 |
}
|
|
284 |
|
|
285 |
private void checkArrayTypes(Object nonNullSrc, Object nonNullDest, ArrayCopyTypeCheck arrayTypeCheck) {
|
|
286 |
if (arrayTypeCheck == ArrayCopyTypeCheck.NO_ARRAY_TYPE_CHECK) {
|
|
287 |
// nothing to do
|
|
288 |
} else if (arrayTypeCheck == ArrayCopyTypeCheck.HUB_BASED_ARRAY_TYPE_CHECK) {
|
|
289 |
Pointer srcHub = loadHub(nonNullSrc);
|
|
290 |
Pointer destHub = loadHub(nonNullDest);
|
58299
|
291 |
if (probability(DEOPT_PROBABILITY, srcHub != destHub)) {
|
52910
|
292 |
DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.RuntimeConstraint);
|
|
293 |
}
|
|
294 |
} else if (arrayTypeCheck == ArrayCopyTypeCheck.LAYOUT_HELPER_BASED_ARRAY_TYPE_CHECK) {
|
|
295 |
Pointer srcHub = loadHub(nonNullSrc);
|
|
296 |
Pointer destHub = loadHub(nonNullDest);
|
58299
|
297 |
if (probability(DEOPT_PROBABILITY, getReadLayoutHelper(srcHub) != getReadLayoutHelper(destHub))) {
|
52910
|
298 |
DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.RuntimeConstraint);
|
|
299 |
}
|
|
300 |
} else {
|
52956
|
301 |
ReplacementsUtil.staticAssert(false, "unknown array type check ", arrayTypeCheck);
|
52910
|
302 |
}
|
|
303 |
}
|
|
304 |
|
|
305 |
static class Counters {
|
|
306 |
final SnippetCounter checkSuccessCounter;
|
|
307 |
final SnippetCounter checkAIOOBECounter;
|
|
308 |
|
|
309 |
final SnippetCounter zeroLengthStaticCounter;
|
|
310 |
final SnippetIntegerHistogram lengthHistogram;
|
|
311 |
|
|
312 |
final SnippetCounter systemArraycopyCounter;
|
|
313 |
final SnippetCounter systemArraycopyCopiedCounter;
|
|
314 |
|
|
315 |
final SnippetCounter genericArraycopyDifferentTypeCopiedCounter;
|
|
316 |
final SnippetCounter genericArraycopyDifferentTypeCounter;
|
|
317 |
|
|
318 |
final SnippetCounter objectCheckcastSameTypeCopiedCounter;
|
|
319 |
final SnippetCounter objectCheckcastSameTypeCounter;
|
|
320 |
final SnippetCounter objectCheckcastDifferentTypeCopiedCounter;
|
|
321 |
final SnippetCounter objectCheckcastDifferentTypeCounter;
|
|
322 |
|
|
323 |
final EnumMap<JavaKind, SnippetCounter> arraycopyCallCounters = new EnumMap<>(JavaKind.class);
|
|
324 |
final EnumMap<JavaKind, SnippetCounter> arraycopyCallCopiedCounters = new EnumMap<>(JavaKind.class);
|
|
325 |
|
|
326 |
Counters(SnippetCounter.Group.Factory factory) {
|
|
327 |
final Group checkCounters = factory.createSnippetCounterGroup("System.arraycopy checkInputs");
|
|
328 |
final Group callCounters = factory.createSnippetCounterGroup("System.arraycopy calls");
|
|
329 |
final Group copiedElementsCounters = factory.createSnippetCounterGroup("System.arraycopy copied elements");
|
|
330 |
final Group lengthCounters = factory.createSnippetCounterGroup("System.arraycopy with 0-length");
|
|
331 |
|
|
332 |
checkSuccessCounter = new SnippetCounter(checkCounters, "checkSuccess", "checkSuccess");
|
|
333 |
checkAIOOBECounter = new SnippetCounter(checkCounters, "checkAIOOBE", "checkAIOOBE");
|
|
334 |
|
|
335 |
zeroLengthStaticCounter = new SnippetCounter(lengthCounters, "0-length copy static", "calls where the length is statically 0");
|
|
336 |
lengthHistogram = new SnippetIntegerHistogram(lengthCounters, 2, "length", "length");
|
|
337 |
|
|
338 |
systemArraycopyCounter = new SnippetCounter(callCounters, "native System.arraycopy", "JNI-based System.arraycopy call");
|
|
339 |
systemArraycopyCopiedCounter = new SnippetCounter(copiedElementsCounters, "native System.arraycopy", "JNI-based System.arraycopy call");
|
|
340 |
|
|
341 |
genericArraycopyDifferentTypeCounter = new SnippetCounter(callCounters, "generic[] stub", "generic arraycopy stub");
|
|
342 |
genericArraycopyDifferentTypeCopiedCounter = new SnippetCounter(copiedElementsCounters, "generic[] stub", "generic arraycopy stub");
|
|
343 |
|
|
344 |
objectCheckcastSameTypeCounter = new SnippetCounter(callCounters, "checkcast object[] (same-type)", "checkcast object[] stub but src.klass == dest.klass Object[] arrays");
|
|
345 |
objectCheckcastSameTypeCopiedCounter = new SnippetCounter(copiedElementsCounters, "checkcast object[] (same-type)", "checkcast object[] stub but src.klass == dest.klass Object[] arrays");
|
|
346 |
objectCheckcastDifferentTypeCounter = new SnippetCounter(callCounters, "checkcast object[] (store-check)", "checkcast object[] stub with store check");
|
|
347 |
objectCheckcastDifferentTypeCopiedCounter = new SnippetCounter(copiedElementsCounters, "checkcast object[] (store-check)", "checkcast object[] stub with store check");
|
|
348 |
|
|
349 |
createArraycopyCounter(JavaKind.Byte, callCounters, copiedElementsCounters);
|
|
350 |
createArraycopyCounter(JavaKind.Boolean, callCounters, copiedElementsCounters);
|
|
351 |
createArraycopyCounter(JavaKind.Char, callCounters, copiedElementsCounters);
|
|
352 |
createArraycopyCounter(JavaKind.Short, callCounters, copiedElementsCounters);
|
|
353 |
createArraycopyCounter(JavaKind.Int, callCounters, copiedElementsCounters);
|
|
354 |
createArraycopyCounter(JavaKind.Long, callCounters, copiedElementsCounters);
|
|
355 |
createArraycopyCounter(JavaKind.Float, callCounters, copiedElementsCounters);
|
|
356 |
createArraycopyCounter(JavaKind.Double, callCounters, copiedElementsCounters);
|
|
357 |
createArraycopyCounter(JavaKind.Object, callCounters, copiedElementsCounters);
|
|
358 |
}
|
|
359 |
|
|
360 |
void createArraycopyCounter(JavaKind kind, Group counters, Group copiedCounters) {
|
|
361 |
arraycopyCallCounters.put(kind, new SnippetCounter(counters, kind + "[] stub", "arraycopy call for " + kind + "[] arrays"));
|
|
362 |
arraycopyCallCopiedCounters.put(kind, new SnippetCounter(copiedCounters, kind + "[] stub", "arraycopy call for " + kind + "[] arrays"));
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
public static class Templates extends SnippetTemplate.AbstractTemplates {
|
|
367 |
private final SnippetInfo arraycopyGenericSnippet;
|
|
368 |
private final SnippetInfo arraycopyExactSnippet;
|
54601
|
369 |
private final SnippetInfo arraycopyExactStubCallSnippet;
|
52910
|
370 |
private final SnippetInfo arraycopyCheckcastSnippet;
|
|
371 |
private final SnippetInfo arraycopyNativeSnippet;
|
|
372 |
private final SnippetInfo checkcastArraycopyWithSlowPathWork;
|
|
373 |
private final SnippetInfo genericArraycopyWithSlowPathWork;
|
54601
|
374 |
private final SnippetInfo exactArraycopyWithSlowPathWork;
|
52910
|
375 |
|
|
376 |
private ResolvedJavaMethod originalArraycopy;
|
|
377 |
private final Counters counters;
|
54601
|
378 |
private boolean expandArraycopyLoop;
|
52910
|
379 |
|
|
380 |
public Templates(ArrayCopySnippets receiver, OptionValues options, Iterable<DebugHandlersFactory> factories, Factory factory, Providers providers,
|
|
381 |
SnippetReflectionProvider snippetReflection, TargetDescription target) {
|
|
382 |
super(options, factories, providers, snippetReflection, target);
|
|
383 |
this.counters = new Counters(factory);
|
|
384 |
|
|
385 |
arraycopyGenericSnippet = snippet(receiver, "arraycopyGenericSnippet");
|
|
386 |
arraycopyExactSnippet = snippet(receiver, "arraycopyExactSnippet");
|
54601
|
387 |
arraycopyExactStubCallSnippet = snippet(receiver, "arraycopyExactStubCallSnippet");
|
52910
|
388 |
arraycopyCheckcastSnippet = snippet(receiver, "arraycopyCheckcastSnippet");
|
|
389 |
arraycopyNativeSnippet = snippet(null, "arraycopyNativeSnippet");
|
|
390 |
checkcastArraycopyWithSlowPathWork = snippet(receiver, "checkcastArraycopyWithSlowPathWork");
|
|
391 |
genericArraycopyWithSlowPathWork = snippet(receiver, "genericArraycopyWithSlowPathWork");
|
54601
|
392 |
exactArraycopyWithSlowPathWork = snippet(receiver, "exactArraycopyWithSlowPathWork");
|
52910
|
393 |
}
|
|
394 |
|
|
395 |
protected SnippetInfo snippet(ArrayCopySnippets receiver, String methodName) {
|
52956
|
396 |
SnippetInfo info = snippet(ArrayCopySnippets.class, methodName, originalArraycopy(), receiver, LocationIdentity.any());
|
52910
|
397 |
return info;
|
|
398 |
}
|
|
399 |
|
|
400 |
public void lower(ArrayCopyNode arraycopy, LoweringTool tool) {
|
|
401 |
JavaKind elementKind = selectComponentKind(arraycopy);
|
|
402 |
SnippetInfo snippetInfo;
|
|
403 |
ArrayCopyTypeCheck arrayTypeCheck;
|
|
404 |
|
|
405 |
ResolvedJavaType srcType = StampTool.typeOrNull(arraycopy.getSource().stamp(NodeView.DEFAULT));
|
|
406 |
ResolvedJavaType destType = StampTool.typeOrNull(arraycopy.getDestination().stamp(NodeView.DEFAULT));
|
|
407 |
if (!canBeArray(srcType) || !canBeArray(destType)) {
|
|
408 |
// at least one of the objects is definitely not an array - use the native call
|
|
409 |
// right away as the copying will fail anyways
|
|
410 |
snippetInfo = arraycopyNativeSnippet;
|
|
411 |
arrayTypeCheck = ArrayCopyTypeCheck.UNDEFINED_ARRAY_TYPE_CHECK;
|
|
412 |
} else {
|
|
413 |
ResolvedJavaType srcComponentType = srcType == null ? null : srcType.getComponentType();
|
|
414 |
ResolvedJavaType destComponentType = destType == null ? null : destType.getComponentType();
|
|
415 |
|
|
416 |
if (arraycopy.isExact()) {
|
|
417 |
// there is a sufficient type match - we don't need any additional type checks
|
54601
|
418 |
snippetInfo = arraycopyExactStubCallSnippet;
|
52910
|
419 |
arrayTypeCheck = ArrayCopyTypeCheck.NO_ARRAY_TYPE_CHECK;
|
|
420 |
} else if (srcComponentType == null && destComponentType == null) {
|
|
421 |
// we don't know anything about the types - use the generic copying
|
|
422 |
snippetInfo = arraycopyGenericSnippet;
|
|
423 |
// no need for additional type check to avoid duplicated work
|
|
424 |
arrayTypeCheck = ArrayCopyTypeCheck.NO_ARRAY_TYPE_CHECK;
|
58299
|
425 |
} else if (GeneratePIC.getValue(options)) {
|
|
426 |
// use generic copying for AOT compilation
|
|
427 |
snippetInfo = arraycopyGenericSnippet;
|
|
428 |
arrayTypeCheck = ArrayCopyTypeCheck.NO_ARRAY_TYPE_CHECK;
|
52910
|
429 |
} else if (srcComponentType != null && destComponentType != null) {
|
|
430 |
if (!srcComponentType.isPrimitive() && !destComponentType.isPrimitive()) {
|
|
431 |
// it depends on the array content if the copy succeeds - we need
|
|
432 |
// a type check for every store
|
|
433 |
snippetInfo = arraycopyCheckcastSnippet;
|
|
434 |
arrayTypeCheck = ArrayCopyTypeCheck.NO_ARRAY_TYPE_CHECK;
|
|
435 |
} else {
|
|
436 |
// one object is an object array, the other one is a primitive array.
|
|
437 |
// this copy will always fail - use the native call right away
|
|
438 |
assert !srcComponentType.equals(destComponentType) : "must be handled by arraycopy.isExact()";
|
|
439 |
snippetInfo = arraycopyNativeSnippet;
|
|
440 |
arrayTypeCheck = ArrayCopyTypeCheck.UNDEFINED_ARRAY_TYPE_CHECK;
|
|
441 |
}
|
|
442 |
} else {
|
|
443 |
ResolvedJavaType nonNullComponentType = srcComponentType != null ? srcComponentType : destComponentType;
|
|
444 |
if (nonNullComponentType.isPrimitive()) {
|
|
445 |
// one involved object is a primitive array - it is sufficient to directly
|
|
446 |
// compare the hub.
|
54601
|
447 |
snippetInfo = arraycopyExactStubCallSnippet;
|
52910
|
448 |
arrayTypeCheck = ArrayCopyTypeCheck.HUB_BASED_ARRAY_TYPE_CHECK;
|
|
449 |
elementKind = nonNullComponentType.getJavaKind();
|
|
450 |
} else {
|
|
451 |
// one involved object is an object array - the other array's element type
|
|
452 |
// may be primitive or object, hence we compare the layout helper.
|
|
453 |
snippetInfo = arraycopyCheckcastSnippet;
|
|
454 |
arrayTypeCheck = ArrayCopyTypeCheck.LAYOUT_HELPER_BASED_ARRAY_TYPE_CHECK;
|
|
455 |
}
|
|
456 |
}
|
|
457 |
}
|
|
458 |
|
54601
|
459 |
if (this.expandArraycopyLoop && snippetInfo == arraycopyExactStubCallSnippet) {
|
|
460 |
snippetInfo = arraycopyExactSnippet;
|
52910
|
461 |
}
|
|
462 |
|
|
463 |
// create the snippet
|
|
464 |
Arguments args = new Arguments(snippetInfo, arraycopy.graph().getGuardsStage(), tool.getLoweringStage());
|
|
465 |
args.add("src", arraycopy.getSource());
|
|
466 |
args.add("srcPos", arraycopy.getSourcePosition());
|
|
467 |
args.add("dest", arraycopy.getDestination());
|
|
468 |
args.add("destPos", arraycopy.getDestinationPosition());
|
|
469 |
args.add("length", arraycopy.getLength());
|
|
470 |
if (snippetInfo != arraycopyNativeSnippet) {
|
|
471 |
assert arrayTypeCheck != ArrayCopyTypeCheck.UNDEFINED_ARRAY_TYPE_CHECK;
|
|
472 |
args.addConst("arrayTypeCheck", arrayTypeCheck);
|
|
473 |
}
|
54084
|
474 |
Object locationIdentity = arraycopy.killsAnyLocation() ? LocationIdentity.any() : NamedLocationIdentity.getArrayLocation(elementKind);
|
54601
|
475 |
if (snippetInfo == arraycopyExactStubCallSnippet || snippetInfo == arraycopyExactSnippet) {
|
52910
|
476 |
assert elementKind != null;
|
54601
|
477 |
args.addConst("workSnippet", exactArraycopyWithSlowPathWork);
|
52910
|
478 |
args.addConst("elementKind", elementKind);
|
54084
|
479 |
args.addConst("locationIdentity", locationIdentity);
|
52910
|
480 |
args.addConst("elementKindCounter", counters.arraycopyCallCounters.get(elementKind));
|
|
481 |
args.addConst("elementKindCopiedCounter", counters.arraycopyCallCopiedCounters.get(elementKind));
|
|
482 |
}
|
|
483 |
args.addConst("counters", counters);
|
|
484 |
if (snippetInfo == arraycopyCheckcastSnippet) {
|
|
485 |
args.addConst("workSnippet", checkcastArraycopyWithSlowPathWork);
|
|
486 |
args.addConst("elementKind", JavaKind.Illegal);
|
|
487 |
}
|
|
488 |
if (snippetInfo == arraycopyGenericSnippet) {
|
|
489 |
args.addConst("workSnippet", genericArraycopyWithSlowPathWork);
|
|
490 |
args.addConst("elementKind", JavaKind.Illegal);
|
|
491 |
}
|
|
492 |
|
|
493 |
instantiate(args, arraycopy);
|
|
494 |
}
|
|
495 |
|
54601
|
496 |
public void lower(ArrayCopyWithDelayedLoweringNode arraycopy, LoweringTool tool) {
|
52910
|
497 |
StructuredGraph graph = arraycopy.graph();
|
54601
|
498 |
|
|
499 |
if (arraycopy.getSnippet() == exactArraycopyWithSlowPathWork && this.expandArraycopyLoop) {
|
|
500 |
if (!graph.getGuardsStage().areDeoptsFixed()) {
|
|
501 |
// Don't lower until floating guards are fixed.
|
|
502 |
return;
|
|
503 |
}
|
|
504 |
} else {
|
|
505 |
if (!graph.getGuardsStage().areFrameStatesAtDeopts()) {
|
|
506 |
// Don't lower until frame states are assigned to deoptimization points.
|
|
507 |
return;
|
|
508 |
}
|
52910
|
509 |
}
|
|
510 |
|
|
511 |
SnippetInfo snippetInfo = arraycopy.getSnippet();
|
|
512 |
Arguments args = new Arguments(snippetInfo, graph.getGuardsStage(), tool.getLoweringStage());
|
|
513 |
args.add("src", arraycopy.getSource());
|
|
514 |
args.add("srcPos", arraycopy.getSourcePosition());
|
|
515 |
args.add("dest", arraycopy.getDestination());
|
|
516 |
args.add("destPos", arraycopy.getDestinationPosition());
|
|
517 |
args.add("length", arraycopy.getLength());
|
54601
|
518 |
|
|
519 |
JavaKind elementKind = arraycopy.getElementKind();
|
|
520 |
args.addConst("elementKind", (elementKind == null) ? JavaKind.Illegal : elementKind);
|
|
521 |
|
|
522 |
Object locationIdentity = (elementKind == null) ? LocationIdentity.any() : NamedLocationIdentity.getArrayLocation(arraycopy.getElementKind());
|
|
523 |
args.addConst("arrayLocation", locationIdentity);
|
52910
|
524 |
args.addConst("counters", counters);
|
|
525 |
instantiate(args, arraycopy);
|
|
526 |
}
|
|
527 |
|
|
528 |
private static boolean canBeArray(ResolvedJavaType type) {
|
|
529 |
return type == null || type.isJavaLangObject() || type.isArray();
|
|
530 |
}
|
|
531 |
|
|
532 |
public static JavaKind selectComponentKind(BasicArrayCopyNode arraycopy) {
|
|
533 |
ResolvedJavaType srcType = StampTool.typeOrNull(arraycopy.getSource().stamp(NodeView.DEFAULT));
|
|
534 |
ResolvedJavaType destType = StampTool.typeOrNull(arraycopy.getDestination().stamp(NodeView.DEFAULT));
|
|
535 |
|
|
536 |
if (srcType == null || !srcType.isArray() || destType == null || !destType.isArray()) {
|
|
537 |
return null;
|
|
538 |
}
|
|
539 |
if (!destType.getComponentType().isAssignableFrom(srcType.getComponentType())) {
|
|
540 |
return null;
|
|
541 |
}
|
|
542 |
if (!arraycopy.isExact()) {
|
|
543 |
return null;
|
|
544 |
}
|
|
545 |
return srcType.getComponentType().getJavaKind();
|
|
546 |
}
|
|
547 |
|
|
548 |
/**
|
|
549 |
* Instantiate the snippet template and fix up the FrameState of any Invokes of
|
|
550 |
* System.arraycopy and propagate the captured bci in the ArrayCopySlowPathNode.
|
|
551 |
*
|
|
552 |
* @param args
|
|
553 |
* @param arraycopy
|
|
554 |
*/
|
|
555 |
private void instantiate(Arguments args, BasicArrayCopyNode arraycopy) {
|
|
556 |
StructuredGraph graph = arraycopy.graph();
|
|
557 |
SnippetTemplate template = template(arraycopy, args);
|
|
558 |
UnmodifiableEconomicMap<Node, Node> replacements = template.instantiate(providers.getMetaAccess(), arraycopy, SnippetTemplate.DEFAULT_REPLACER, args, false);
|
|
559 |
for (Node originalNode : replacements.getKeys()) {
|
|
560 |
if (originalNode instanceof InvokeNode) {
|
|
561 |
InvokeNode invoke = (InvokeNode) replacements.get(originalNode);
|
|
562 |
assert invoke.asNode().graph() == graph;
|
|
563 |
CallTargetNode call = invoke.callTarget();
|
|
564 |
|
|
565 |
if (!call.targetMethod().equals(originalArraycopy)) {
|
|
566 |
throw new GraalError("unexpected invoke %s in snippet", call.targetMethod());
|
|
567 |
}
|
|
568 |
// Here we need to fix the bci of the invoke
|
54601
|
569 |
invoke.replaceBci(arraycopy.getBci());
|
|
570 |
invoke.setStateDuring(null);
|
|
571 |
invoke.setStateAfter(null);
|
52910
|
572 |
if (arraycopy.stateDuring() != null) {
|
54601
|
573 |
invoke.setStateDuring(arraycopy.stateDuring());
|
52910
|
574 |
} else {
|
|
575 |
assert arraycopy.stateAfter() != null : arraycopy;
|
54601
|
576 |
invoke.setStateAfter(arraycopy.stateAfter());
|
52910
|
577 |
}
|
|
578 |
} else if (originalNode instanceof InvokeWithExceptionNode) {
|
|
579 |
throw new GraalError("unexpected invoke with exception %s in snippet", originalNode);
|
54601
|
580 |
} else if (originalNode instanceof ArrayCopyWithDelayedLoweringNode) {
|
|
581 |
ArrayCopyWithDelayedLoweringNode slowPath = (ArrayCopyWithDelayedLoweringNode) replacements.get(originalNode);
|
52910
|
582 |
assert arraycopy.stateAfter() != null : arraycopy;
|
|
583 |
assert slowPath.stateAfter() == arraycopy.stateAfter();
|
|
584 |
slowPath.setBci(arraycopy.getBci());
|
|
585 |
}
|
|
586 |
}
|
|
587 |
GraphUtil.killCFG(arraycopy);
|
|
588 |
}
|
|
589 |
|
|
590 |
private ResolvedJavaMethod originalArraycopy() throws GraalError {
|
|
591 |
if (originalArraycopy == null) {
|
|
592 |
try {
|
52956
|
593 |
originalArraycopy = findMethod(providers.getMetaAccess(), System.class, "arraycopy");
|
|
594 |
} catch (SecurityException e) {
|
52910
|
595 |
throw new GraalError(e);
|
|
596 |
}
|
|
597 |
}
|
|
598 |
return originalArraycopy;
|
|
599 |
}
|
54601
|
600 |
|
|
601 |
public void setExpandArraycopyLoop(boolean b) {
|
|
602 |
this.expandArraycopyLoop = b;
|
|
603 |
}
|
52910
|
604 |
}
|
|
605 |
}
|