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