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 #include "asm/macroAssembler.hpp"
26 #include "ci/ciSymbols.hpp"
27 #include "ci/ciUtilities.inline.hpp"
28 #include "classfile/vmIntrinsics.hpp"
29 #include "compiler/compileBroker.hpp"
30 #include "compiler/compileLog.hpp"
31 #include "gc/shared/barrierSet.hpp"
32 #include "jfr/support/jfrIntrinsics.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "oops/klass.inline.hpp"
35 #include "oops/objArrayKlass.hpp"
36 #include "opto/addnode.hpp"
37 #include "opto/arraycopynode.hpp"
38 #include "opto/c2compiler.hpp"
39 #include "opto/castnode.hpp"
40 #include "opto/cfgnode.hpp"
41 #include "opto/convertnode.hpp"
42 #include "opto/countbitsnode.hpp"
43 #include "opto/idealKit.hpp"
44 #include "opto/library_call.hpp"
45 #include "opto/mathexactnode.hpp"
46 #include "opto/mulnode.hpp"
47 #include "opto/narrowptrnode.hpp"
48 #include "opto/opaquenode.hpp"
49 #include "opto/parse.hpp"
50 #include "opto/rootnode.hpp"
51 #include "opto/runtime.hpp"
52 #include "opto/subnode.hpp"
53 #include "opto/vectornode.hpp"
54 #include "prims/jvmtiExport.hpp"
55 #include "prims/jvmtiThreadState.hpp"
56 #include "prims/unsafe.hpp"
57 #include "runtime/jniHandles.inline.hpp"
58 #include "runtime/objectMonitor.hpp"
59 #include "runtime/sharedRuntime.hpp"
60 #include "runtime/stubRoutines.hpp"
61 #include "utilities/macros.hpp"
62 #include "utilities/powerOfTwo.hpp"
63
64 //---------------------------make_vm_intrinsic----------------------------
65 CallGenerator* Compile::make_vm_intrinsic(ciMethod* m, bool is_virtual) {
66 vmIntrinsicID id = m->intrinsic_id();
67 assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
68
69 if (!m->is_loaded()) {
70 // Do not attempt to inline unloaded methods.
71 return nullptr;
72 }
73
74 C2Compiler* compiler = (C2Compiler*)CompileBroker::compiler(CompLevel_full_optimization);
75 bool is_available = false;
76
77 {
78 // For calling is_intrinsic_supported and is_intrinsic_disabled_by_flag
79 // the compiler must transition to '_thread_in_vm' state because both
80 // methods access VM-internal data.
299 case vmIntrinsics::_indexOfIL: return inline_string_indexOfI(StrIntrinsicNode::LL);
300 case vmIntrinsics::_indexOfIU: return inline_string_indexOfI(StrIntrinsicNode::UU);
301 case vmIntrinsics::_indexOfIUL: return inline_string_indexOfI(StrIntrinsicNode::UL);
302 case vmIntrinsics::_indexOfU_char: return inline_string_indexOfChar(StrIntrinsicNode::U);
303 case vmIntrinsics::_indexOfL_char: return inline_string_indexOfChar(StrIntrinsicNode::L);
304
305 case vmIntrinsics::_equalsL: return inline_string_equals(StrIntrinsicNode::LL);
306
307 case vmIntrinsics::_vectorizedHashCode: return inline_vectorizedHashCode();
308
309 case vmIntrinsics::_toBytesStringU: return inline_string_toBytesU();
310 case vmIntrinsics::_getCharsStringU: return inline_string_getCharsU();
311 case vmIntrinsics::_getCharStringU: return inline_string_char_access(!is_store);
312 case vmIntrinsics::_putCharStringU: return inline_string_char_access( is_store);
313
314 case vmIntrinsics::_compressStringC:
315 case vmIntrinsics::_compressStringB: return inline_string_copy( is_compress);
316 case vmIntrinsics::_inflateStringC:
317 case vmIntrinsics::_inflateStringB: return inline_string_copy(!is_compress);
318
319 case vmIntrinsics::_getReference: return inline_unsafe_access(!is_store, T_OBJECT, Relaxed, false);
320 case vmIntrinsics::_getBoolean: return inline_unsafe_access(!is_store, T_BOOLEAN, Relaxed, false);
321 case vmIntrinsics::_getByte: return inline_unsafe_access(!is_store, T_BYTE, Relaxed, false);
322 case vmIntrinsics::_getShort: return inline_unsafe_access(!is_store, T_SHORT, Relaxed, false);
323 case vmIntrinsics::_getChar: return inline_unsafe_access(!is_store, T_CHAR, Relaxed, false);
324 case vmIntrinsics::_getInt: return inline_unsafe_access(!is_store, T_INT, Relaxed, false);
325 case vmIntrinsics::_getLong: return inline_unsafe_access(!is_store, T_LONG, Relaxed, false);
326 case vmIntrinsics::_getFloat: return inline_unsafe_access(!is_store, T_FLOAT, Relaxed, false);
327 case vmIntrinsics::_getDouble: return inline_unsafe_access(!is_store, T_DOUBLE, Relaxed, false);
328
329 case vmIntrinsics::_putReference: return inline_unsafe_access( is_store, T_OBJECT, Relaxed, false);
330 case vmIntrinsics::_putBoolean: return inline_unsafe_access( is_store, T_BOOLEAN, Relaxed, false);
331 case vmIntrinsics::_putByte: return inline_unsafe_access( is_store, T_BYTE, Relaxed, false);
332 case vmIntrinsics::_putShort: return inline_unsafe_access( is_store, T_SHORT, Relaxed, false);
333 case vmIntrinsics::_putChar: return inline_unsafe_access( is_store, T_CHAR, Relaxed, false);
334 case vmIntrinsics::_putInt: return inline_unsafe_access( is_store, T_INT, Relaxed, false);
335 case vmIntrinsics::_putLong: return inline_unsafe_access( is_store, T_LONG, Relaxed, false);
336 case vmIntrinsics::_putFloat: return inline_unsafe_access( is_store, T_FLOAT, Relaxed, false);
337 case vmIntrinsics::_putDouble: return inline_unsafe_access( is_store, T_DOUBLE, Relaxed, false);
338
339 case vmIntrinsics::_getReferenceVolatile: return inline_unsafe_access(!is_store, T_OBJECT, Volatile, false);
340 case vmIntrinsics::_getBooleanVolatile: return inline_unsafe_access(!is_store, T_BOOLEAN, Volatile, false);
341 case vmIntrinsics::_getByteVolatile: return inline_unsafe_access(!is_store, T_BYTE, Volatile, false);
342 case vmIntrinsics::_getShortVolatile: return inline_unsafe_access(!is_store, T_SHORT, Volatile, false);
343 case vmIntrinsics::_getCharVolatile: return inline_unsafe_access(!is_store, T_CHAR, Volatile, false);
344 case vmIntrinsics::_getIntVolatile: return inline_unsafe_access(!is_store, T_INT, Volatile, false);
345 case vmIntrinsics::_getLongVolatile: return inline_unsafe_access(!is_store, T_LONG, Volatile, false);
346 case vmIntrinsics::_getFloatVolatile: return inline_unsafe_access(!is_store, T_FLOAT, Volatile, false);
347 case vmIntrinsics::_getDoubleVolatile: return inline_unsafe_access(!is_store, T_DOUBLE, Volatile, false);
348
349 case vmIntrinsics::_putReferenceVolatile: return inline_unsafe_access( is_store, T_OBJECT, Volatile, false);
350 case vmIntrinsics::_putBooleanVolatile: return inline_unsafe_access( is_store, T_BOOLEAN, Volatile, false);
351 case vmIntrinsics::_putByteVolatile: return inline_unsafe_access( is_store, T_BYTE, Volatile, false);
352 case vmIntrinsics::_putShortVolatile: return inline_unsafe_access( is_store, T_SHORT, Volatile, false);
353 case vmIntrinsics::_putCharVolatile: return inline_unsafe_access( is_store, T_CHAR, Volatile, false);
354 case vmIntrinsics::_putIntVolatile: return inline_unsafe_access( is_store, T_INT, Volatile, false);
355 case vmIntrinsics::_putLongVolatile: return inline_unsafe_access( is_store, T_LONG, Volatile, false);
356 case vmIntrinsics::_putFloatVolatile: return inline_unsafe_access( is_store, T_FLOAT, Volatile, false);
357 case vmIntrinsics::_putDoubleVolatile: return inline_unsafe_access( is_store, T_DOUBLE, Volatile, false);
389 case vmIntrinsics::_getReferenceOpaque: return inline_unsafe_access(!is_store, T_OBJECT, Opaque, false);
390 case vmIntrinsics::_getBooleanOpaque: return inline_unsafe_access(!is_store, T_BOOLEAN, Opaque, false);
391 case vmIntrinsics::_getByteOpaque: return inline_unsafe_access(!is_store, T_BYTE, Opaque, false);
392 case vmIntrinsics::_getShortOpaque: return inline_unsafe_access(!is_store, T_SHORT, Opaque, false);
393 case vmIntrinsics::_getCharOpaque: return inline_unsafe_access(!is_store, T_CHAR, Opaque, false);
394 case vmIntrinsics::_getIntOpaque: return inline_unsafe_access(!is_store, T_INT, Opaque, false);
395 case vmIntrinsics::_getLongOpaque: return inline_unsafe_access(!is_store, T_LONG, Opaque, false);
396 case vmIntrinsics::_getFloatOpaque: return inline_unsafe_access(!is_store, T_FLOAT, Opaque, false);
397 case vmIntrinsics::_getDoubleOpaque: return inline_unsafe_access(!is_store, T_DOUBLE, Opaque, false);
398
399 case vmIntrinsics::_putReferenceOpaque: return inline_unsafe_access( is_store, T_OBJECT, Opaque, false);
400 case vmIntrinsics::_putBooleanOpaque: return inline_unsafe_access( is_store, T_BOOLEAN, Opaque, false);
401 case vmIntrinsics::_putByteOpaque: return inline_unsafe_access( is_store, T_BYTE, Opaque, false);
402 case vmIntrinsics::_putShortOpaque: return inline_unsafe_access( is_store, T_SHORT, Opaque, false);
403 case vmIntrinsics::_putCharOpaque: return inline_unsafe_access( is_store, T_CHAR, Opaque, false);
404 case vmIntrinsics::_putIntOpaque: return inline_unsafe_access( is_store, T_INT, Opaque, false);
405 case vmIntrinsics::_putLongOpaque: return inline_unsafe_access( is_store, T_LONG, Opaque, false);
406 case vmIntrinsics::_putFloatOpaque: return inline_unsafe_access( is_store, T_FLOAT, Opaque, false);
407 case vmIntrinsics::_putDoubleOpaque: return inline_unsafe_access( is_store, T_DOUBLE, Opaque, false);
408
409 case vmIntrinsics::_compareAndSetReference: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap, Volatile);
410 case vmIntrinsics::_compareAndSetByte: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap, Volatile);
411 case vmIntrinsics::_compareAndSetShort: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap, Volatile);
412 case vmIntrinsics::_compareAndSetInt: return inline_unsafe_load_store(T_INT, LS_cmp_swap, Volatile);
413 case vmIntrinsics::_compareAndSetLong: return inline_unsafe_load_store(T_LONG, LS_cmp_swap, Volatile);
414
415 case vmIntrinsics::_weakCompareAndSetReferencePlain: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Relaxed);
416 case vmIntrinsics::_weakCompareAndSetReferenceAcquire: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Acquire);
417 case vmIntrinsics::_weakCompareAndSetReferenceRelease: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Release);
418 case vmIntrinsics::_weakCompareAndSetReference: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Volatile);
419 case vmIntrinsics::_weakCompareAndSetBytePlain: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Relaxed);
420 case vmIntrinsics::_weakCompareAndSetByteAcquire: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Acquire);
421 case vmIntrinsics::_weakCompareAndSetByteRelease: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Release);
422 case vmIntrinsics::_weakCompareAndSetByte: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Volatile);
423 case vmIntrinsics::_weakCompareAndSetShortPlain: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Relaxed);
424 case vmIntrinsics::_weakCompareAndSetShortAcquire: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Acquire);
425 case vmIntrinsics::_weakCompareAndSetShortRelease: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Release);
426 case vmIntrinsics::_weakCompareAndSetShort: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Volatile);
427 case vmIntrinsics::_weakCompareAndSetIntPlain: return inline_unsafe_load_store(T_INT, LS_cmp_swap_weak, Relaxed);
428 case vmIntrinsics::_weakCompareAndSetIntAcquire: return inline_unsafe_load_store(T_INT, LS_cmp_swap_weak, Acquire);
496 #endif
497 case vmIntrinsics::_currentTimeMillis: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeMillis), "currentTimeMillis");
498 case vmIntrinsics::_nanoTime: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeNanos), "nanoTime");
499 case vmIntrinsics::_writeback0: return inline_unsafe_writeback0();
500 case vmIntrinsics::_writebackPreSync0: return inline_unsafe_writebackSync0(true);
501 case vmIntrinsics::_writebackPostSync0: return inline_unsafe_writebackSync0(false);
502 case vmIntrinsics::_allocateInstance: return inline_unsafe_allocate();
503 case vmIntrinsics::_copyMemory: return inline_unsafe_copyMemory();
504 case vmIntrinsics::_setMemory: return inline_unsafe_setMemory();
505 case vmIntrinsics::_getLength: return inline_native_getLength();
506 case vmIntrinsics::_copyOf: return inline_array_copyOf(false);
507 case vmIntrinsics::_copyOfRange: return inline_array_copyOf(true);
508 case vmIntrinsics::_equalsB: return inline_array_equals(StrIntrinsicNode::LL);
509 case vmIntrinsics::_equalsC: return inline_array_equals(StrIntrinsicNode::UU);
510 case vmIntrinsics::_Preconditions_checkIndex: return inline_preconditions_checkIndex(T_INT);
511 case vmIntrinsics::_Preconditions_checkLongIndex: return inline_preconditions_checkIndex(T_LONG);
512 case vmIntrinsics::_clone: return inline_native_clone(intrinsic()->is_virtual());
513
514 case vmIntrinsics::_allocateUninitializedArray: return inline_unsafe_newArray(true);
515 case vmIntrinsics::_newArray: return inline_unsafe_newArray(false);
516
517 case vmIntrinsics::_isAssignableFrom: return inline_native_subtype_check();
518
519 case vmIntrinsics::_isInstance:
520 case vmIntrinsics::_isHidden:
521 case vmIntrinsics::_getSuperclass:
522 case vmIntrinsics::_getClassAccessFlags: return inline_native_Class_query(intrinsic_id());
523
524 case vmIntrinsics::_floatToRawIntBits:
525 case vmIntrinsics::_floatToIntBits:
526 case vmIntrinsics::_intBitsToFloat:
527 case vmIntrinsics::_doubleToRawLongBits:
528 case vmIntrinsics::_doubleToLongBits:
529 case vmIntrinsics::_longBitsToDouble:
530 case vmIntrinsics::_floatToFloat16:
531 case vmIntrinsics::_float16ToFloat: return inline_fp_conversions(intrinsic_id());
532 case vmIntrinsics::_sqrt_float16: return inline_fp16_operations(intrinsic_id(), 1);
533 case vmIntrinsics::_fma_float16: return inline_fp16_operations(intrinsic_id(), 3);
534 case vmIntrinsics::_floatIsFinite:
535 case vmIntrinsics::_floatIsInfinite:
2301 case vmIntrinsics::_remainderUnsigned_l: {
2302 zero_check_long(argument(2));
2303 // Compile-time detect of null-exception
2304 if (stopped()) {
2305 return true; // keep the graph constructed so far
2306 }
2307 n = new UModLNode(control(), argument(0), argument(2));
2308 break;
2309 }
2310 default: fatal_unexpected_iid(id); break;
2311 }
2312 set_result(_gvn.transform(n));
2313 return true;
2314 }
2315
2316 //----------------------------inline_unsafe_access----------------------------
2317
2318 const TypeOopPtr* LibraryCallKit::sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type) {
2319 // Attempt to infer a sharper value type from the offset and base type.
2320 ciKlass* sharpened_klass = nullptr;
2321
2322 // See if it is an instance field, with an object type.
2323 if (alias_type->field() != nullptr) {
2324 if (alias_type->field()->type()->is_klass()) {
2325 sharpened_klass = alias_type->field()->type()->as_klass();
2326 }
2327 }
2328
2329 const TypeOopPtr* result = nullptr;
2330 // See if it is a narrow oop array.
2331 if (adr_type->isa_aryptr()) {
2332 if (adr_type->offset() >= objArrayOopDesc::base_offset_in_bytes()) {
2333 const TypeOopPtr* elem_type = adr_type->is_aryptr()->elem()->make_oopptr();
2334 if (elem_type != nullptr && elem_type->is_loaded()) {
2335 // Sharpen the value type.
2336 result = elem_type;
2337 }
2338 }
2339 }
2340
2341 // The sharpened class might be unloaded if there is no class loader
2342 // contraint in place.
2343 if (result == nullptr && sharpened_klass != nullptr && sharpened_klass->is_loaded()) {
2344 // Sharpen the value type.
2345 result = TypeOopPtr::make_from_klass(sharpened_klass);
2346 }
2347 if (result != nullptr) {
2348 #ifndef PRODUCT
2349 if (C->print_intrinsics() || C->print_inlining()) {
2350 tty->print(" from base type: "); adr_type->dump(); tty->cr();
2351 tty->print(" sharpened value: "); result->dump(); tty->cr();
2352 }
2353 #endif
2354 }
2355 return result;
2356 }
2357
2358 DecoratorSet LibraryCallKit::mo_decorator_for_access_kind(AccessKind kind) {
2359 switch (kind) {
2360 case Relaxed:
2361 return MO_UNORDERED;
2362 case Opaque:
2363 return MO_RELAXED;
2364 case Acquire:
2365 return MO_ACQUIRE;
2366 case Release:
2367 return MO_RELEASE;
2368 case Volatile:
2369 return MO_SEQ_CST;
2370 default:
2371 ShouldNotReachHere();
2372 return 0;
2373 }
2374 }
2375
2376 bool LibraryCallKit::inline_unsafe_access(bool is_store, const BasicType type, const AccessKind kind, const bool unaligned) {
2377 if (callee()->is_static()) return false; // caller must have the capability!
2378 DecoratorSet decorators = C2_UNSAFE_ACCESS;
2379 guarantee(!is_store || kind != Acquire, "Acquire accesses can be produced only for loads");
2380 guarantee( is_store || kind != Release, "Release accesses can be produced only for stores");
2381 assert(type != T_OBJECT || !unaligned, "unaligned access not supported with object type");
2382
2383 if (is_reference_type(type)) {
2384 decorators |= ON_UNKNOWN_OOP_REF;
2385 }
2386
2387 if (unaligned) {
2388 decorators |= C2_UNALIGNED;
2389 }
2390
2391 #ifndef PRODUCT
2392 {
2393 ResourceMark rm;
2394 // Check the signatures.
2395 ciSignature* sig = callee()->signature();
2396 #ifdef ASSERT
2397 if (!is_store) {
2398 // Object getReference(Object base, int/long offset), etc.
2399 BasicType rtype = sig->return_type()->basic_type();
2400 assert(rtype == type, "getter must return the expected value");
2401 assert(sig->count() == 2, "oop getter has 2 arguments");
2402 assert(sig->type_at(0)->basic_type() == T_OBJECT, "getter base is object");
2403 assert(sig->type_at(1)->basic_type() == T_LONG, "getter offset is correct");
2404 } else {
2405 // void putReference(Object base, int/long offset, Object x), etc.
2406 assert(sig->return_type()->basic_type() == T_VOID, "putter must not return a value");
2407 assert(sig->count() == 3, "oop putter has 3 arguments");
2408 assert(sig->type_at(0)->basic_type() == T_OBJECT, "putter base is object");
2409 assert(sig->type_at(1)->basic_type() == T_LONG, "putter offset is correct");
2410 BasicType vtype = sig->type_at(sig->count()-1)->basic_type();
2411 assert(vtype == type, "putter must accept the expected value");
2412 }
2413 #endif // ASSERT
2414 }
2415 #endif //PRODUCT
2416
2417 C->set_has_unsafe_access(true); // Mark eventual nmethod as "unsafe".
2418
2419 Node* receiver = argument(0); // type: oop
2420
2421 // Build address expression.
2422 Node* heap_base_oop = top();
2423
2424 // The base is either a Java object or a value produced by Unsafe.staticFieldBase
2425 Node* base = argument(1); // type: oop
2426 // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
2427 Node* offset = argument(2); // type: long
2428 // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2429 // to be plain byte offsets, which are also the same as those accepted
2430 // by oopDesc::field_addr.
2431 assert(Unsafe_field_offset_to_byte_offset(11) == 11,
2432 "fieldOffset must be byte-scaled");
2433 // 32-bit machines ignore the high half!
2434 offset = ConvL2X(offset);
2435
2436 // Save state and restore on bailout
2437 uint old_sp = sp();
2438 SafePointNode* old_map = clone_map();
2439
2440 Node* adr = make_unsafe_address(base, offset, type, kind == Relaxed);
2441 assert(!stopped(), "Inlining of unsafe access failed: address construction stopped unexpectedly");
2442
2443 if (_gvn.type(base->uncast())->isa_ptr() == TypePtr::NULL_PTR) {
2444 if (type != T_OBJECT) {
2445 decorators |= IN_NATIVE; // off-heap primitive access
2446 } else {
2447 set_map(old_map);
2448 set_sp(old_sp);
2449 return false; // off-heap oop accesses are not supported
2450 }
2451 } else {
2452 heap_base_oop = base; // on-heap or mixed access
2453 }
2454
2455 // Can base be null? Otherwise, always on-heap access.
2456 bool can_access_non_heap = TypePtr::NULL_PTR->higher_equal(_gvn.type(base));
2457
2458 if (!can_access_non_heap) {
2459 decorators |= IN_HEAP;
2460 }
2461
2462 Node* val = is_store ? argument(4) : nullptr;
2463
2464 const TypePtr* adr_type = _gvn.type(adr)->isa_ptr();
2465 if (adr_type == TypePtr::NULL_PTR) {
2466 set_map(old_map);
2467 set_sp(old_sp);
2468 return false; // off-heap access with zero address
2469 }
2470
2471 // Try to categorize the address.
2472 Compile::AliasType* alias_type = C->alias_type(adr_type);
2473 assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2474
2475 if (alias_type->adr_type() == TypeInstPtr::KLASS ||
2476 alias_type->adr_type() == TypeAryPtr::RANGE) {
2477 set_map(old_map);
2478 set_sp(old_sp);
2479 return false; // not supported
2480 }
2481
2482 bool mismatched = false;
2483 BasicType bt = alias_type->basic_type();
2484 if (bt != T_ILLEGAL) {
2485 assert(alias_type->adr_type()->is_oopptr(), "should be on-heap access");
2486 if (bt == T_BYTE && adr_type->isa_aryptr()) {
2487 // Alias type doesn't differentiate between byte[] and boolean[]).
2488 // Use address type to get the element type.
2489 bt = adr_type->is_aryptr()->elem()->array_element_basic_type();
2490 }
2491 if (is_reference_type(bt, true)) {
2492 // accessing an array field with getReference is not a mismatch
2493 bt = T_OBJECT;
2494 }
2495 if ((bt == T_OBJECT) != (type == T_OBJECT)) {
2496 // Don't intrinsify mismatched object accesses
2497 set_map(old_map);
2498 set_sp(old_sp);
2499 return false;
2500 }
2501 mismatched = (bt != type);
2502 } else if (alias_type->adr_type()->isa_oopptr()) {
2503 mismatched = true; // conservatively mark all "wide" on-heap accesses as mismatched
2504 }
2505
2506 destruct_map_clone(old_map);
2507 assert(!mismatched || alias_type->adr_type()->is_oopptr(), "off-heap access can't be mismatched");
2508
2509 if (mismatched) {
2510 decorators |= C2_MISMATCHED;
2511 }
2512
2513 // First guess at the value type.
2514 const Type *value_type = Type::get_const_basic_type(type);
2515
2516 // Figure out the memory ordering.
2517 decorators |= mo_decorator_for_access_kind(kind);
2518
2519 if (!is_store && type == T_OBJECT) {
2520 const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2521 if (tjp != nullptr) {
2522 value_type = tjp;
2523 }
2524 }
2525
2526 receiver = null_check(receiver);
2527 if (stopped()) {
2528 return true;
2529 }
2530 // Heap pointers get a null-check from the interpreter,
2531 // as a courtesy. However, this is not guaranteed by Unsafe,
2532 // and it is not possible to fully distinguish unintended nulls
2533 // from intended ones in this API.
2534
2535 if (!is_store) {
2536 Node* p = nullptr;
2537 // Try to constant fold a load from a constant field
2538 ciField* field = alias_type->field();
2539 if (heap_base_oop != top() && field != nullptr && field->is_constant() && !mismatched) {
2540 // final or stable field
2541 p = make_constant_from_field(field, heap_base_oop);
2542 }
2543
2544 if (p == nullptr) { // Could not constant fold the load
2545 p = access_load_at(heap_base_oop, adr, adr_type, value_type, type, decorators);
2546 // Normalize the value returned by getBoolean in the following cases
2547 if (type == T_BOOLEAN &&
2548 (mismatched ||
2549 heap_base_oop == top() || // - heap_base_oop is null or
2550 (can_access_non_heap && field == nullptr)) // - heap_base_oop is potentially null
2551 // and the unsafe access is made to large offset
2552 // (i.e., larger than the maximum offset necessary for any
2553 // field access)
2554 ) {
2555 IdealKit ideal = IdealKit(this);
2556 #define __ ideal.
2557 IdealVariable normalized_result(ideal);
2558 __ declarations_done();
2559 __ set(normalized_result, p);
2560 __ if_then(p, BoolTest::ne, ideal.ConI(0));
2561 __ set(normalized_result, ideal.ConI(1));
2562 ideal.end_if();
2563 final_sync(ideal);
2564 p = __ value(normalized_result);
2565 #undef __
2566 }
2567 }
2568 if (type == T_ADDRESS) {
2569 p = gvn().transform(new CastP2XNode(nullptr, p));
2570 p = ConvX2UL(p);
2571 }
2572 // The load node has the control of the preceding MemBarCPUOrder. All
2573 // following nodes will have the control of the MemBarCPUOrder inserted at
2574 // the end of this method. So, pushing the load onto the stack at a later
2575 // point is fine.
2576 set_result(p);
2577 } else {
2578 if (bt == T_ADDRESS) {
2579 // Repackage the long as a pointer.
2580 val = ConvL2X(val);
2581 val = gvn().transform(new CastX2PNode(val));
2582 }
2583 access_store_at(heap_base_oop, adr, adr_type, val, value_type, type, decorators);
2584 }
2585
2586 return true;
2587 }
2588
2589 //----------------------------inline_unsafe_load_store----------------------------
2590 // This method serves a couple of different customers (depending on LoadStoreKind):
2591 //
2592 // LS_cmp_swap:
2593 //
2594 // boolean compareAndSetReference(Object o, long offset, Object expected, Object x);
2595 // boolean compareAndSetInt( Object o, long offset, int expected, int x);
2596 // boolean compareAndSetLong( Object o, long offset, long expected, long x);
2597 //
2598 // LS_cmp_swap_weak:
2599 //
2600 // boolean weakCompareAndSetReference( Object o, long offset, Object expected, Object x);
2601 // boolean weakCompareAndSetReferencePlain( Object o, long offset, Object expected, Object x);
2602 // boolean weakCompareAndSetReferenceAcquire(Object o, long offset, Object expected, Object x);
2603 // boolean weakCompareAndSetReferenceRelease(Object o, long offset, Object expected, Object x);
2604 //
2605 // boolean weakCompareAndSetInt( Object o, long offset, int expected, int x);
2606 // boolean weakCompareAndSetIntPlain( Object o, long offset, int expected, int x);
2607 // boolean weakCompareAndSetIntAcquire( Object o, long offset, int expected, int x);
2608 // boolean weakCompareAndSetIntRelease( Object o, long offset, int expected, int x);
2774 }
2775 case LS_cmp_swap:
2776 case LS_cmp_swap_weak:
2777 case LS_get_add:
2778 break;
2779 default:
2780 ShouldNotReachHere();
2781 }
2782
2783 // Null check receiver.
2784 receiver = null_check(receiver);
2785 if (stopped()) {
2786 return true;
2787 }
2788
2789 int alias_idx = C->get_alias_index(adr_type);
2790
2791 if (is_reference_type(type)) {
2792 decorators |= IN_HEAP | ON_UNKNOWN_OOP_REF;
2793
2794 // Transformation of a value which could be null pointer (CastPP #null)
2795 // could be delayed during Parse (for example, in adjust_map_after_if()).
2796 // Execute transformation here to avoid barrier generation in such case.
2797 if (_gvn.type(newval) == TypePtr::NULL_PTR)
2798 newval = _gvn.makecon(TypePtr::NULL_PTR);
2799
2800 if (oldval != nullptr && _gvn.type(oldval) == TypePtr::NULL_PTR) {
2801 // Refine the value to a null constant, when it is known to be null
2802 oldval = _gvn.makecon(TypePtr::NULL_PTR);
2803 }
2804 }
2805
2806 Node* result = nullptr;
2807 switch (kind) {
2808 case LS_cmp_exchange: {
2809 result = access_atomic_cmpxchg_val_at(base, adr, adr_type, alias_idx,
2810 oldval, newval, value_type, type, decorators);
2811 break;
2812 }
2813 case LS_cmp_swap_weak:
2960 Deoptimization::Action_make_not_entrant);
2961 }
2962 if (stopped()) {
2963 return true;
2964 }
2965 #endif //INCLUDE_JVMTI
2966
2967 Node* test = nullptr;
2968 if (LibraryCallKit::klass_needs_init_guard(kls)) {
2969 // Note: The argument might still be an illegal value like
2970 // Serializable.class or Object[].class. The runtime will handle it.
2971 // But we must make an explicit check for initialization.
2972 Node* insp = basic_plus_adr(kls, in_bytes(InstanceKlass::init_state_offset()));
2973 // Use T_BOOLEAN for InstanceKlass::_init_state so the compiler
2974 // can generate code to load it as unsigned byte.
2975 Node* inst = make_load(nullptr, insp, TypeInt::UBYTE, T_BOOLEAN, MemNode::acquire);
2976 Node* bits = intcon(InstanceKlass::fully_initialized);
2977 test = _gvn.transform(new SubINode(inst, bits));
2978 // The 'test' is non-zero if we need to take a slow path.
2979 }
2980
2981 Node* obj = new_instance(kls, test);
2982 set_result(obj);
2983 return true;
2984 }
2985
2986 //------------------------inline_native_time_funcs--------------
2987 // inline code for System.currentTimeMillis() and System.nanoTime()
2988 // these have the same type and signature
2989 bool LibraryCallKit::inline_native_time_funcs(address funcAddr, const char* funcName) {
2990 const TypeFunc* tf = OptoRuntime::void_long_Type();
2991 const TypePtr* no_memory_effects = nullptr;
2992 Node* time = make_runtime_call(RC_LEAF, tf, funcAddr, funcName, no_memory_effects);
2993 Node* value = _gvn.transform(new ProjNode(time, TypeFunc::Parms+0));
2994 #ifdef ASSERT
2995 Node* value_top = _gvn.transform(new ProjNode(time, TypeFunc::Parms+1));
2996 assert(value_top == top(), "second value must be top");
2997 #endif
2998 set_result(value);
2999 return true;
3000 }
3001
3742 Node* thread = _gvn.transform(new ThreadLocalNode());
3743 Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::vthread_offset()));
3744 Node* thread_obj_handle
3745 = make_load(nullptr, p, p->bottom_type()->is_ptr(), T_OBJECT, MemNode::unordered);
3746 thread_obj_handle = _gvn.transform(thread_obj_handle);
3747 const TypePtr *adr_type = _gvn.type(thread_obj_handle)->isa_ptr();
3748 access_store_at(nullptr, thread_obj_handle, adr_type, arr, _gvn.type(arr), T_OBJECT, IN_NATIVE | MO_UNORDERED);
3749
3750 // Change the _monitor_owner_id of the JavaThread
3751 Node* tid = load_field_from_object(arr, "tid", "J");
3752 Node* monitor_owner_id_offset = basic_plus_adr(thread, in_bytes(JavaThread::monitor_owner_id_offset()));
3753 store_to_memory(control(), monitor_owner_id_offset, tid, T_LONG, MemNode::unordered, true);
3754
3755 JFR_ONLY(extend_setCurrentThread(thread, arr);)
3756 return true;
3757 }
3758
3759 const Type* LibraryCallKit::scopedValueCache_type() {
3760 ciKlass* objects_klass = ciObjArrayKlass::make(env()->Object_klass());
3761 const TypeOopPtr* etype = TypeOopPtr::make_from_klass(env()->Object_klass());
3762 const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
3763
3764 // Because we create the scopedValue cache lazily we have to make the
3765 // type of the result BotPTR.
3766 bool xk = etype->klass_is_exact();
3767 const Type* objects_type = TypeAryPtr::make(TypePtr::BotPTR, arr0, objects_klass, xk, 0);
3768 return objects_type;
3769 }
3770
3771 Node* LibraryCallKit::scopedValueCache_helper() {
3772 Node* thread = _gvn.transform(new ThreadLocalNode());
3773 Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::scopedValueCache_offset()));
3774 // We cannot use immutable_memory() because we might flip onto a
3775 // different carrier thread, at which point we'll need to use that
3776 // carrier thread's cache.
3777 // return _gvn.transform(LoadNode::make(_gvn, nullptr, immutable_memory(), p, p->bottom_type()->is_ptr(),
3778 // TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered));
3779 return make_load(nullptr, p, p->bottom_type()->is_ptr(), T_ADDRESS, MemNode::unordered);
3780 }
3781
3782 //------------------------inline_native_scopedValueCache------------------
3783 bool LibraryCallKit::inline_native_scopedValueCache() {
3784 Node* cache_obj_handle = scopedValueCache_helper();
3785 const Type* objects_type = scopedValueCache_type();
3786 set_result(access_load(cache_obj_handle, objects_type, T_OBJECT, IN_NATIVE));
3787
3871 store_to_memory(control(), pin_count_offset, next_pin_count, T_INT, MemNode::unordered);
3872
3873 // Result of top level CFG and Memory.
3874 RegionNode* result_rgn = new RegionNode(PATH_LIMIT);
3875 record_for_igvn(result_rgn);
3876 PhiNode* result_mem = new PhiNode(result_rgn, Type::MEMORY, TypePtr::BOTTOM);
3877 record_for_igvn(result_mem);
3878
3879 result_rgn->init_req(_true_path, _gvn.transform(valid_pin_count));
3880 result_rgn->init_req(_false_path, _gvn.transform(continuation_is_null));
3881 result_mem->init_req(_true_path, _gvn.transform(reset_memory()));
3882 result_mem->init_req(_false_path, _gvn.transform(input_memory_state));
3883
3884 // Set output state.
3885 set_control(_gvn.transform(result_rgn));
3886 set_all_memory(_gvn.transform(result_mem));
3887
3888 return true;
3889 }
3890
3891 //---------------------------load_mirror_from_klass----------------------------
3892 // Given a klass oop, load its java mirror (a java.lang.Class oop).
3893 Node* LibraryCallKit::load_mirror_from_klass(Node* klass) {
3894 Node* p = basic_plus_adr(klass, in_bytes(Klass::java_mirror_offset()));
3895 Node* load = make_load(nullptr, p, TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered);
3896 // mirror = ((OopHandle)mirror)->resolve();
3897 return access_load(load, TypeInstPtr::MIRROR, T_OBJECT, IN_NATIVE);
3898 }
3899
3900 //-----------------------load_klass_from_mirror_common-------------------------
3901 // Given a java mirror (a java.lang.Class oop), load its corresponding klass oop.
3902 // Test the klass oop for null (signifying a primitive Class like Integer.TYPE),
3903 // and branch to the given path on the region.
3904 // If never_see_null, take an uncommon trap on null, so we can optimistically
3905 // compile for the non-null case.
3906 // If the region is null, force never_see_null = true.
3907 Node* LibraryCallKit::load_klass_from_mirror_common(Node* mirror,
3908 bool never_see_null,
3909 RegionNode* region,
3910 int null_path,
3911 int offset) {
3912 if (region == nullptr) never_see_null = true;
3913 Node* p = basic_plus_adr(mirror, offset);
3914 const TypeKlassPtr* kls_type = TypeInstKlassPtr::OBJECT_OR_NULL;
3915 Node* kls = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), p, TypeRawPtr::BOTTOM, kls_type));
3916 Node* null_ctl = top();
3917 kls = null_check_oop(kls, &null_ctl, never_see_null);
3918 if (region != nullptr) {
3919 // Set region->in(null_path) if the mirror is a primitive (e.g, int.class).
3923 }
3924 return kls;
3925 }
3926
3927 //--------------------(inline_native_Class_query helpers)---------------------
3928 // Use this for JVM_ACC_INTERFACE.
3929 // Fall through if (mods & mask) == bits, take the guard otherwise.
3930 Node* LibraryCallKit::generate_klass_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region,
3931 ByteSize offset, const Type* type, BasicType bt) {
3932 // Branch around if the given klass has the given modifier bit set.
3933 // Like generate_guard, adds a new path onto the region.
3934 Node* modp = basic_plus_adr(kls, in_bytes(offset));
3935 Node* mods = make_load(nullptr, modp, type, bt, MemNode::unordered);
3936 Node* mask = intcon(modifier_mask);
3937 Node* bits = intcon(modifier_bits);
3938 Node* mbit = _gvn.transform(new AndINode(mods, mask));
3939 Node* cmp = _gvn.transform(new CmpINode(mbit, bits));
3940 Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
3941 return generate_fair_guard(bol, region);
3942 }
3943 Node* LibraryCallKit::generate_interface_guard(Node* kls, RegionNode* region) {
3944 return generate_klass_flags_guard(kls, JVM_ACC_INTERFACE, 0, region,
3945 Klass::access_flags_offset(), TypeInt::CHAR, T_CHAR);
3946 }
3947
3948 // Use this for testing if Klass is_hidden, has_finalizer, and is_cloneable_fast.
3949 Node* LibraryCallKit::generate_misc_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region) {
3950 return generate_klass_flags_guard(kls, modifier_mask, modifier_bits, region,
3951 Klass::misc_flags_offset(), TypeInt::UBYTE, T_BOOLEAN);
3952 }
3953
3954 Node* LibraryCallKit::generate_hidden_class_guard(Node* kls, RegionNode* region) {
3955 return generate_misc_flags_guard(kls, KlassFlags::_misc_is_hidden_class, 0, region);
3956 }
3957
3958 //-------------------------inline_native_Class_query-------------------
3959 bool LibraryCallKit::inline_native_Class_query(vmIntrinsics::ID id) {
3960 const Type* return_type = TypeInt::BOOL;
3961 Node* prim_return_value = top(); // what happens if it's a primitive class?
3962 bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
4081
4082 case vmIntrinsics::_getClassAccessFlags:
4083 p = basic_plus_adr(kls, in_bytes(Klass::access_flags_offset()));
4084 query_value = make_load(nullptr, p, TypeInt::CHAR, T_CHAR, MemNode::unordered);
4085 break;
4086
4087 default:
4088 fatal_unexpected_iid(id);
4089 break;
4090 }
4091
4092 // Fall-through is the normal case of a query to a real class.
4093 phi->init_req(1, query_value);
4094 region->init_req(1, control());
4095
4096 C->set_has_split_ifs(true); // Has chance for split-if optimization
4097 set_result(region, phi);
4098 return true;
4099 }
4100
4101 //-------------------------inline_Class_cast-------------------
4102 bool LibraryCallKit::inline_Class_cast() {
4103 Node* mirror = argument(0); // Class
4104 Node* obj = argument(1);
4105 const TypeInstPtr* mirror_con = _gvn.type(mirror)->isa_instptr();
4106 if (mirror_con == nullptr) {
4107 return false; // dead path (mirror->is_top()).
4108 }
4109 if (obj == nullptr || obj->is_top()) {
4110 return false; // dead path
4111 }
4112 const TypeOopPtr* tp = _gvn.type(obj)->isa_oopptr();
4113
4114 // First, see if Class.cast() can be folded statically.
4115 // java_mirror_type() returns non-null for compile-time Class constants.
4116 ciType* tm = mirror_con->java_mirror_type();
4117 if (tm != nullptr && tm->is_klass() &&
4118 tp != nullptr) {
4119 if (!tp->is_loaded()) {
4120 // Don't use intrinsic when class is not loaded.
4121 return false;
4122 } else {
4123 int static_res = C->static_subtype_check(TypeKlassPtr::make(tm->as_klass(), Type::trust_interfaces), tp->as_klass_type());
4124 if (static_res == Compile::SSC_always_true) {
4125 // isInstance() is true - fold the code.
4126 set_result(obj);
4127 return true;
4128 } else if (static_res == Compile::SSC_always_false) {
4129 // Don't use intrinsic, have to throw ClassCastException.
4130 // If the reference is null, the non-intrinsic bytecode will
4131 // be optimized appropriately.
4132 return false;
4133 }
4134 }
4135 }
4136
4137 // Bailout intrinsic and do normal inlining if exception path is frequent.
4138 if (too_many_traps(Deoptimization::Reason_intrinsic)) {
4139 return false;
4140 }
4141
4142 // Generate dynamic checks.
4143 // Class.cast() is java implementation of _checkcast bytecode.
4144 // Do checkcast (Parse::do_checkcast()) optimizations here.
4145
4146 mirror = null_check(mirror);
4147 // If mirror is dead, only null-path is taken.
4148 if (stopped()) {
4149 return true;
4150 }
4151
4152 // Not-subtype or the mirror's klass ptr is null (in case it is a primitive).
4153 enum { _bad_type_path = 1, _prim_path = 2, PATH_LIMIT };
4154 RegionNode* region = new RegionNode(PATH_LIMIT);
4155 record_for_igvn(region);
4156
4157 // Now load the mirror's klass metaobject, and null-check it.
4158 // If kls is null, we have a primitive mirror and
4159 // nothing is an instance of a primitive type.
4160 Node* kls = load_klass_from_mirror(mirror, false, region, _prim_path);
4161
4162 Node* res = top();
4163 if (!stopped()) {
4164 Node* bad_type_ctrl = top();
4165 // Do checkcast optimizations.
4166 res = gen_checkcast(obj, kls, &bad_type_ctrl);
4167 region->init_req(_bad_type_path, bad_type_ctrl);
4168 }
4169 if (region->in(_prim_path) != top() ||
4170 region->in(_bad_type_path) != top()) {
4171 // Let Interpreter throw ClassCastException.
4172 PreserveJVMState pjvms(this);
4173 set_control(_gvn.transform(region));
4174 uncommon_trap(Deoptimization::Reason_intrinsic,
4175 Deoptimization::Action_maybe_recompile);
4176 }
4177 if (!stopped()) {
4178 set_result(res);
4179 }
4180 return true;
4181 }
4182
4183
4184 //--------------------------inline_native_subtype_check------------------------
4185 // This intrinsic takes the JNI calls out of the heart of
4186 // UnsafeFieldAccessorImpl.set, which improves Field.set, readObject, etc.
4187 bool LibraryCallKit::inline_native_subtype_check() {
4188 // Pull both arguments off the stack.
4189 Node* args[2]; // two java.lang.Class mirrors: superc, subc
4190 args[0] = argument(0);
4191 args[1] = argument(1);
4192 Node* klasses[2]; // corresponding Klasses: superk, subk
4193 klasses[0] = klasses[1] = top();
4194
4195 enum {
4196 // A full decision tree on {superc is prim, subc is prim}:
4197 _prim_0_path = 1, // {P,N} => false
4198 // {P,P} & superc!=subc => false
4199 _prim_same_path, // {P,P} & superc==subc => true
4200 _prim_1_path, // {N,P} => false
4201 _ref_subtype_path, // {N,N} & subtype check wins => true
4202 _both_ref_path, // {N,N} & subtype check loses => false
4203 PATH_LIMIT
4204 };
4205
4206 RegionNode* region = new RegionNode(PATH_LIMIT);
4207 Node* phi = new PhiNode(region, TypeInt::BOOL);
4208 record_for_igvn(region);
4209
4210 const TypePtr* adr_type = TypeRawPtr::BOTTOM; // memory type of loads
4211 const TypeKlassPtr* kls_type = TypeInstKlassPtr::OBJECT_OR_NULL;
4212 int class_klass_offset = java_lang_Class::klass_offset();
4213
4214 // First null-check both mirrors and load each mirror's klass metaobject.
4215 int which_arg;
4216 for (which_arg = 0; which_arg <= 1; which_arg++) {
4217 Node* arg = args[which_arg];
4218 arg = null_check(arg);
4219 if (stopped()) break;
4220 args[which_arg] = arg;
4221
4222 Node* p = basic_plus_adr(arg, class_klass_offset);
4223 Node* kls = LoadKlassNode::make(_gvn, immutable_memory(), p, adr_type, kls_type);
4224 klasses[which_arg] = _gvn.transform(kls);
4225 }
4226
4227 // Having loaded both klasses, test each for null.
4228 bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
4229 for (which_arg = 0; which_arg <= 1; which_arg++) {
4230 Node* kls = klasses[which_arg];
4231 Node* null_ctl = top();
4232 kls = null_check_oop(kls, &null_ctl, never_see_null);
4233 int prim_path = (which_arg == 0 ? _prim_0_path : _prim_1_path);
4234 region->init_req(prim_path, null_ctl);
4235 if (stopped()) break;
4236 klasses[which_arg] = kls;
4237 }
4238
4239 if (!stopped()) {
4240 // now we have two reference types, in klasses[0..1]
4241 Node* subk = klasses[1]; // the argument to isAssignableFrom
4242 Node* superk = klasses[0]; // the receiver
4243 region->set_req(_both_ref_path, gen_subtype_check(subk, superk));
4244 // now we have a successful reference subtype check
4245 region->set_req(_ref_subtype_path, control());
4246 }
4247
4248 // If both operands are primitive (both klasses null), then
4249 // we must return true when they are identical primitives.
4250 // It is convenient to test this after the first null klass check.
4251 set_control(region->in(_prim_0_path)); // go back to first null check
4252 if (!stopped()) {
4253 // Since superc is primitive, make a guard for the superc==subc case.
4254 Node* cmp_eq = _gvn.transform(new CmpPNode(args[0], args[1]));
4255 Node* bol_eq = _gvn.transform(new BoolNode(cmp_eq, BoolTest::eq));
4256 generate_guard(bol_eq, region, PROB_FAIR);
4257 if (region->req() == PATH_LIMIT+1) {
4258 // A guard was added. If the added guard is taken, superc==subc.
4259 region->swap_edges(PATH_LIMIT, _prim_same_path);
4260 region->del_req(PATH_LIMIT);
4261 }
4262 region->set_req(_prim_0_path, control()); // Not equal after all.
4263 }
4264
4265 // these are the only paths that produce 'true':
4266 phi->set_req(_prim_same_path, intcon(1));
4267 phi->set_req(_ref_subtype_path, intcon(1));
4268
4269 // pull together the cases:
4270 assert(region->req() == PATH_LIMIT, "sane region");
4271 for (uint i = 1; i < region->req(); i++) {
4272 Node* ctl = region->in(i);
4273 if (ctl == nullptr || ctl == top()) {
4274 region->set_req(i, top());
4275 phi ->set_req(i, top());
4276 } else if (phi->in(i) == nullptr) {
4277 phi->set_req(i, intcon(0)); // all other paths produce 'false'
4278 }
4279 }
4280
4281 set_control(_gvn.transform(region));
4282 set_result(_gvn.transform(phi));
4283 return true;
4284 }
4285
4286 //---------------------generate_array_guard_common------------------------
4287 Node* LibraryCallKit::generate_array_guard_common(Node* kls, RegionNode* region,
4288 bool obj_array, bool not_array, Node** obj) {
4289
4290 if (stopped()) {
4291 return nullptr;
4292 }
4293
4294 // If obj_array/non_array==false/false:
4295 // Branch around if the given klass is in fact an array (either obj or prim).
4296 // If obj_array/non_array==false/true:
4297 // Branch around if the given klass is not an array klass of any kind.
4298 // If obj_array/non_array==true/true:
4299 // Branch around if the kls is not an oop array (kls is int[], String, etc.)
4300 // If obj_array/non_array==true/false:
4301 // Branch around if the kls is an oop array (Object[] or subtype)
4302 //
4303 // Like generate_guard, adds a new path onto the region.
4304 jint layout_con = 0;
4305 Node* layout_val = get_layout_helper(kls, layout_con);
4306 if (layout_val == nullptr) {
4307 bool query = (obj_array
4308 ? Klass::layout_helper_is_objArray(layout_con)
4309 : Klass::layout_helper_is_array(layout_con));
4310 if (query == not_array) {
4311 return nullptr; // never a branch
4312 } else { // always a branch
4313 Node* always_branch = control();
4314 if (region != nullptr)
4315 region->add_req(always_branch);
4316 set_control(top());
4317 return always_branch;
4318 }
4319 }
4320 // Now test the correct condition.
4321 jint nval = (obj_array
4322 ? (jint)(Klass::_lh_array_tag_type_value
4323 << Klass::_lh_array_tag_shift)
4324 : Klass::_lh_neutral_value);
4325 Node* cmp = _gvn.transform(new CmpINode(layout_val, intcon(nval)));
4326 BoolTest::mask btest = BoolTest::lt; // correct for testing is_[obj]array
4327 // invert the test if we are looking for a non-array
4328 if (not_array) btest = BoolTest(btest).negate();
4329 Node* bol = _gvn.transform(new BoolNode(cmp, btest));
4330 Node* ctrl = generate_fair_guard(bol, region);
4331 Node* is_array_ctrl = not_array ? control() : ctrl;
4332 if (obj != nullptr && is_array_ctrl != nullptr && is_array_ctrl != top()) {
4333 // Keep track of the fact that 'obj' is an array to prevent
4334 // array specific accesses from floating above the guard.
4335 *obj = _gvn.transform(new CastPPNode(is_array_ctrl, *obj, TypeAryPtr::BOTTOM));
4336 }
4337 return ctrl;
4338 }
4339
4340
4341 //-----------------------inline_native_newArray--------------------------
4342 // private static native Object java.lang.reflect.newArray(Class<?> componentType, int length);
4343 // private native Object Unsafe.allocateUninitializedArray0(Class<?> cls, int size);
4344 bool LibraryCallKit::inline_unsafe_newArray(bool uninitialized) {
4345 Node* mirror;
4346 Node* count_val;
4347 if (uninitialized) {
4348 null_check_receiver();
4349 mirror = argument(1);
4350 count_val = argument(2);
4351 } else {
4352 mirror = argument(0);
4353 count_val = argument(1);
4354 }
4355
4356 mirror = null_check(mirror);
4357 // If mirror or obj is dead, only null-path is taken.
4358 if (stopped()) return true;
4359
4360 enum { _normal_path = 1, _slow_path = 2, PATH_LIMIT };
4361 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
4362 PhiNode* result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
4380 CallJavaNode* slow_call = nullptr;
4381 if (uninitialized) {
4382 // Generate optimized virtual call (holder class 'Unsafe' is final)
4383 slow_call = generate_method_call(vmIntrinsics::_allocateUninitializedArray, false, false, true);
4384 } else {
4385 slow_call = generate_method_call_static(vmIntrinsics::_newArray, true);
4386 }
4387 Node* slow_result = set_results_for_java_call(slow_call);
4388 // this->control() comes from set_results_for_java_call
4389 result_reg->set_req(_slow_path, control());
4390 result_val->set_req(_slow_path, slow_result);
4391 result_io ->set_req(_slow_path, i_o());
4392 result_mem->set_req(_slow_path, reset_memory());
4393 }
4394
4395 set_control(normal_ctl);
4396 if (!stopped()) {
4397 // Normal case: The array type has been cached in the java.lang.Class.
4398 // The following call works fine even if the array type is polymorphic.
4399 // It could be a dynamic mix of int[], boolean[], Object[], etc.
4400 Node* obj = new_array(klass_node, count_val, 0); // no arguments to push
4401 result_reg->init_req(_normal_path, control());
4402 result_val->init_req(_normal_path, obj);
4403 result_io ->init_req(_normal_path, i_o());
4404 result_mem->init_req(_normal_path, reset_memory());
4405
4406 if (uninitialized) {
4407 // Mark the allocation so that zeroing is skipped
4408 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(obj);
4409 alloc->maybe_set_complete(&_gvn);
4410 }
4411 }
4412
4413 // Return the combined state.
4414 set_i_o( _gvn.transform(result_io) );
4415 set_all_memory( _gvn.transform(result_mem));
4416
4417 C->set_has_split_ifs(true); // Has chance for split-if optimization
4418 set_result(result_reg, result_val);
4419 return true;
4468 // the bytecode that invokes Arrays.copyOf if deoptimization happens.
4469 { PreserveReexecuteState preexecs(this);
4470 jvms()->set_should_reexecute(true);
4471
4472 array_type_mirror = null_check(array_type_mirror);
4473 original = null_check(original);
4474
4475 // Check if a null path was taken unconditionally.
4476 if (stopped()) return true;
4477
4478 Node* orig_length = load_array_length(original);
4479
4480 Node* klass_node = load_klass_from_mirror(array_type_mirror, false, nullptr, 0);
4481 klass_node = null_check(klass_node);
4482
4483 RegionNode* bailout = new RegionNode(1);
4484 record_for_igvn(bailout);
4485
4486 // Despite the generic type of Arrays.copyOf, the mirror might be int, int[], etc.
4487 // Bail out if that is so.
4488 Node* not_objArray = generate_non_objArray_guard(klass_node, bailout);
4489 if (not_objArray != nullptr) {
4490 // Improve the klass node's type from the new optimistic assumption:
4491 ciKlass* ak = ciArrayKlass::make(env()->Object_klass());
4492 const Type* akls = TypeKlassPtr::make(TypePtr::NotNull, ak, 0/*offset*/);
4493 Node* cast = new CastPPNode(control(), klass_node, akls);
4494 klass_node = _gvn.transform(cast);
4495 }
4496
4497 // Bail out if either start or end is negative.
4498 generate_negative_guard(start, bailout, &start);
4499 generate_negative_guard(end, bailout, &end);
4500
4501 Node* length = end;
4502 if (_gvn.type(start) != TypeInt::ZERO) {
4503 length = _gvn.transform(new SubINode(end, start));
4504 }
4505
4506 // Bail out if length is negative (i.e., if start > end).
4507 // Without this the new_array would throw
4508 // NegativeArraySizeException but IllegalArgumentException is what
4509 // should be thrown
4510 generate_negative_guard(length, bailout, &length);
4511
4512 // Bail out if start is larger than the original length
4513 Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
4514 generate_negative_guard(orig_tail, bailout, &orig_tail);
4515
4516 if (bailout->req() > 1) {
4517 PreserveJVMState pjvms(this);
4518 set_control(_gvn.transform(bailout));
4519 uncommon_trap(Deoptimization::Reason_intrinsic,
4520 Deoptimization::Action_maybe_recompile);
4521 }
4522
4523 if (!stopped()) {
4524 // How many elements will we copy from the original?
4525 // The answer is MinI(orig_tail, length).
4526 Node* moved = _gvn.transform(new MinINode(orig_tail, length));
4527
4528 // Generate a direct call to the right arraycopy function(s).
4529 // We know the copy is disjoint but we might not know if the
4530 // oop stores need checking.
4531 // Extreme case: Arrays.copyOf((Integer[])x, 10, String[].class).
4537 // to the copyOf to be validated, including that the copy to the
4538 // new array won't trigger an ArrayStoreException. That subtype
4539 // check can be optimized if we know something on the type of
4540 // the input array from type speculation.
4541 if (_gvn.type(klass_node)->singleton()) {
4542 const TypeKlassPtr* subk = _gvn.type(load_object_klass(original))->is_klassptr();
4543 const TypeKlassPtr* superk = _gvn.type(klass_node)->is_klassptr();
4544
4545 int test = C->static_subtype_check(superk, subk);
4546 if (test != Compile::SSC_always_true && test != Compile::SSC_always_false) {
4547 const TypeOopPtr* t_original = _gvn.type(original)->is_oopptr();
4548 if (t_original->speculative_type() != nullptr) {
4549 original = maybe_cast_profiled_obj(original, t_original->speculative_type(), true);
4550 }
4551 }
4552 }
4553
4554 bool validated = false;
4555 // Reason_class_check rather than Reason_intrinsic because we
4556 // want to intrinsify even if this traps.
4557 if (!too_many_traps(Deoptimization::Reason_class_check)) {
4558 Node* not_subtype_ctrl = gen_subtype_check(original, klass_node);
4559
4560 if (not_subtype_ctrl != top()) {
4561 PreserveJVMState pjvms(this);
4562 set_control(not_subtype_ctrl);
4563 uncommon_trap(Deoptimization::Reason_class_check,
4564 Deoptimization::Action_make_not_entrant);
4565 assert(stopped(), "Should be stopped");
4566 }
4567 validated = true;
4568 }
4569
4570 if (!stopped()) {
4571 newcopy = new_array(klass_node, length, 0); // no arguments to push
4572
4573 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, true, true,
4574 load_object_klass(original), klass_node);
4575 if (!is_copyOfRange) {
4576 ac->set_copyof(validated);
4577 } else {
4623
4624 //-----------------------generate_method_call----------------------------
4625 // Use generate_method_call to make a slow-call to the real
4626 // method if the fast path fails. An alternative would be to
4627 // use a stub like OptoRuntime::slow_arraycopy_Java.
4628 // This only works for expanding the current library call,
4629 // not another intrinsic. (E.g., don't use this for making an
4630 // arraycopy call inside of the copyOf intrinsic.)
4631 CallJavaNode*
4632 LibraryCallKit::generate_method_call(vmIntrinsicID method_id, bool is_virtual, bool is_static, bool res_not_null) {
4633 // When compiling the intrinsic method itself, do not use this technique.
4634 guarantee(callee() != C->method(), "cannot make slow-call to self");
4635
4636 ciMethod* method = callee();
4637 // ensure the JVMS we have will be correct for this call
4638 guarantee(method_id == method->intrinsic_id(), "must match");
4639
4640 const TypeFunc* tf = TypeFunc::make(method);
4641 if (res_not_null) {
4642 assert(tf->return_type() == T_OBJECT, "");
4643 const TypeTuple* range = tf->range();
4644 const Type** fields = TypeTuple::fields(range->cnt());
4645 fields[TypeFunc::Parms] = range->field_at(TypeFunc::Parms)->filter_speculative(TypePtr::NOTNULL);
4646 const TypeTuple* new_range = TypeTuple::make(range->cnt(), fields);
4647 tf = TypeFunc::make(tf->domain(), new_range);
4648 }
4649 CallJavaNode* slow_call;
4650 if (is_static) {
4651 assert(!is_virtual, "");
4652 slow_call = new CallStaticJavaNode(C, tf,
4653 SharedRuntime::get_resolve_static_call_stub(), method);
4654 } else if (is_virtual) {
4655 assert(!gvn().type(argument(0))->maybe_null(), "should not be null");
4656 int vtable_index = Method::invalid_vtable_index;
4657 if (UseInlineCaches) {
4658 // Suppress the vtable call
4659 } else {
4660 // hashCode and clone are not a miranda methods,
4661 // so the vtable index is fixed.
4662 // No need to use the linkResolver to get it.
4663 vtable_index = method->vtable_index();
4664 assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
4665 "bad index %d", vtable_index);
4666 }
4667 slow_call = new CallDynamicJavaNode(tf,
4684 set_edges_for_java_call(slow_call);
4685 return slow_call;
4686 }
4687
4688
4689 /**
4690 * Build special case code for calls to hashCode on an object. This call may
4691 * be virtual (invokevirtual) or bound (invokespecial). For each case we generate
4692 * slightly different code.
4693 */
4694 bool LibraryCallKit::inline_native_hashcode(bool is_virtual, bool is_static) {
4695 assert(is_static == callee()->is_static(), "correct intrinsic selection");
4696 assert(!(is_virtual && is_static), "either virtual, special, or static");
4697
4698 enum { _slow_path = 1, _fast_path, _null_path, PATH_LIMIT };
4699
4700 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
4701 PhiNode* result_val = new PhiNode(result_reg, TypeInt::INT);
4702 PhiNode* result_io = new PhiNode(result_reg, Type::ABIO);
4703 PhiNode* result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
4704 Node* obj = nullptr;
4705 if (!is_static) {
4706 // Check for hashing null object
4707 obj = null_check_receiver();
4708 if (stopped()) return true; // unconditionally null
4709 result_reg->init_req(_null_path, top());
4710 result_val->init_req(_null_path, top());
4711 } else {
4712 // Do a null check, and return zero if null.
4713 // System.identityHashCode(null) == 0
4714 obj = argument(0);
4715 Node* null_ctl = top();
4716 obj = null_check_oop(obj, &null_ctl);
4717 result_reg->init_req(_null_path, null_ctl);
4718 result_val->init_req(_null_path, _gvn.intcon(0));
4719 }
4720
4721 // Unconditionally null? Then return right away.
4722 if (stopped()) {
4723 set_control( result_reg->in(_null_path));
4724 if (!stopped())
4725 set_result(result_val->in(_null_path));
4726 return true;
4727 }
4728
4729 // We only go to the fast case code if we pass a number of guards. The
4730 // paths which do not pass are accumulated in the slow_region.
4731 RegionNode* slow_region = new RegionNode(1);
4732 record_for_igvn(slow_region);
4733
4734 // If this is a virtual call, we generate a funny guard. We pull out
4735 // the vtable entry corresponding to hashCode() from the target object.
4736 // If the target method which we are calling happens to be the native
4737 // Object hashCode() method, we pass the guard. We do not need this
4738 // guard for non-virtual calls -- the caller is known to be the native
4739 // Object hashCode().
4740 if (is_virtual) {
4741 // After null check, get the object's klass.
4742 Node* obj_klass = load_object_klass(obj);
4743 generate_virtual_guard(obj_klass, slow_region);
4744 }
4745
4746 // Get the header out of the object, use LoadMarkNode when available
4747 Node* header_addr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes());
4748 // The control of the load must be null. Otherwise, the load can move before
4749 // the null check after castPP removal.
4750 Node* no_ctrl = nullptr;
4751 Node* header = make_load(no_ctrl, header_addr, TypeX_X, TypeX_X->basic_type(), MemNode::unordered);
4752
4753 if (!UseObjectMonitorTable) {
4754 // Test the header to see if it is safe to read w.r.t. locking.
4755 Node *lock_mask = _gvn.MakeConX(markWord::lock_mask_in_place);
4756 Node *lmasked_header = _gvn.transform(new AndXNode(header, lock_mask));
4757 if (LockingMode == LM_LIGHTWEIGHT) {
4758 Node *monitor_val = _gvn.MakeConX(markWord::monitor_value);
4759 Node *chk_monitor = _gvn.transform(new CmpXNode(lmasked_header, monitor_val));
4760 Node *test_monitor = _gvn.transform(new BoolNode(chk_monitor, BoolTest::eq));
4761
4762 generate_slow_guard(test_monitor, slow_region);
4763 } else {
4764 Node *unlocked_val = _gvn.MakeConX(markWord::unlocked_value);
4765 Node *chk_unlocked = _gvn.transform(new CmpXNode(lmasked_header, unlocked_val));
4766 Node *test_not_unlocked = _gvn.transform(new BoolNode(chk_unlocked, BoolTest::ne));
4767
4768 generate_slow_guard(test_not_unlocked, slow_region);
4769 }
4770 }
4771
4772 // Get the hash value and check to see that it has been properly assigned.
4773 // We depend on hash_mask being at most 32 bits and avoid the use of
4774 // hash_mask_in_place because it could be larger than 32 bits in a 64-bit
4775 // vm: see markWord.hpp.
4810 // this->control() comes from set_results_for_java_call
4811 result_reg->init_req(_slow_path, control());
4812 result_val->init_req(_slow_path, slow_result);
4813 result_io ->set_req(_slow_path, i_o());
4814 result_mem ->set_req(_slow_path, reset_memory());
4815 }
4816
4817 // Return the combined state.
4818 set_i_o( _gvn.transform(result_io) );
4819 set_all_memory( _gvn.transform(result_mem));
4820
4821 set_result(result_reg, result_val);
4822 return true;
4823 }
4824
4825 //---------------------------inline_native_getClass----------------------------
4826 // public final native Class<?> java.lang.Object.getClass();
4827 //
4828 // Build special case code for calls to getClass on an object.
4829 bool LibraryCallKit::inline_native_getClass() {
4830 Node* obj = null_check_receiver();
4831 if (stopped()) return true;
4832 set_result(load_mirror_from_klass(load_object_klass(obj)));
4833 return true;
4834 }
4835
4836 //-----------------inline_native_Reflection_getCallerClass---------------------
4837 // public static native Class<?> sun.reflect.Reflection.getCallerClass();
4838 //
4839 // In the presence of deep enough inlining, getCallerClass() becomes a no-op.
4840 //
4841 // NOTE: This code must perform the same logic as JVM_GetCallerClass
4842 // in that it must skip particular security frames and checks for
4843 // caller sensitive methods.
4844 bool LibraryCallKit::inline_native_Reflection_getCallerClass() {
4845 #ifndef PRODUCT
4846 if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
4847 tty->print_cr("Attempting to inline sun.reflect.Reflection.getCallerClass");
4848 }
4849 #endif
4850
5232 // not cloneable or finalizer => slow path to out-of-line Object.clone
5233 //
5234 // The general case has two steps, allocation and copying.
5235 // Allocation has two cases, and uses GraphKit::new_instance or new_array.
5236 //
5237 // Copying also has two cases, oop arrays and everything else.
5238 // Oop arrays use arrayof_oop_arraycopy (same as System.arraycopy).
5239 // Everything else uses the tight inline loop supplied by CopyArrayNode.
5240 //
5241 // These steps fold up nicely if and when the cloned object's klass
5242 // can be sharply typed as an object array, a type array, or an instance.
5243 //
5244 bool LibraryCallKit::inline_native_clone(bool is_virtual) {
5245 PhiNode* result_val;
5246
5247 // Set the reexecute bit for the interpreter to reexecute
5248 // the bytecode that invokes Object.clone if deoptimization happens.
5249 { PreserveReexecuteState preexecs(this);
5250 jvms()->set_should_reexecute(true);
5251
5252 Node* obj = null_check_receiver();
5253 if (stopped()) return true;
5254
5255 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
5256
5257 // If we are going to clone an instance, we need its exact type to
5258 // know the number and types of fields to convert the clone to
5259 // loads/stores. Maybe a speculative type can help us.
5260 if (!obj_type->klass_is_exact() &&
5261 obj_type->speculative_type() != nullptr &&
5262 obj_type->speculative_type()->is_instance_klass()) {
5263 ciInstanceKlass* spec_ik = obj_type->speculative_type()->as_instance_klass();
5264 if (spec_ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem &&
5265 !spec_ik->has_injected_fields()) {
5266 if (!obj_type->isa_instptr() ||
5267 obj_type->is_instptr()->instance_klass()->has_subklass()) {
5268 obj = maybe_cast_profiled_obj(obj, obj_type->speculative_type(), false);
5269 }
5270 }
5271 }
5272
5273 // Conservatively insert a memory barrier on all memory slices.
5274 // Do not let writes into the original float below the clone.
5275 insert_mem_bar(Op_MemBarCPUOrder);
5276
5277 // paths into result_reg:
5278 enum {
5279 _slow_path = 1, // out-of-line call to clone method (virtual or not)
5280 _objArray_path, // plain array allocation, plus arrayof_oop_arraycopy
5281 _array_path, // plain array allocation, plus arrayof_long_arraycopy
5282 _instance_path, // plain instance allocation, plus arrayof_long_arraycopy
5283 PATH_LIMIT
5284 };
5285 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
5286 result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
5287 PhiNode* result_i_o = new PhiNode(result_reg, Type::ABIO);
5288 PhiNode* result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
5289 record_for_igvn(result_reg);
5290
5291 Node* obj_klass = load_object_klass(obj);
5292 Node* array_obj = obj;
5293 Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)nullptr, &array_obj);
5294 if (array_ctl != nullptr) {
5295 // It's an array.
5296 PreserveJVMState pjvms(this);
5297 set_control(array_ctl);
5298 Node* obj_length = load_array_length(array_obj);
5299 Node* array_size = nullptr; // Size of the array without object alignment padding.
5300 Node* alloc_obj = new_array(obj_klass, obj_length, 0, &array_size, /*deoptimize_on_exception=*/true);
5301
5302 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
5303 if (bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, false, BarrierSetC2::Parsing)) {
5304 // If it is an oop array, it requires very special treatment,
5305 // because gc barriers are required when accessing the array.
5306 Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)nullptr);
5307 if (is_obja != nullptr) {
5308 PreserveJVMState pjvms2(this);
5309 set_control(is_obja);
5310 // Generate a direct call to the right arraycopy function(s).
5311 // Clones are always tightly coupled.
5312 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, array_obj, intcon(0), alloc_obj, intcon(0), obj_length, true, false);
5313 ac->set_clone_oop_array();
5314 Node* n = _gvn.transform(ac);
5315 assert(n == ac, "cannot disappear");
5316 ac->connect_outputs(this, /*deoptimize_on_exception=*/true);
5317
5318 result_reg->init_req(_objArray_path, control());
5319 result_val->init_req(_objArray_path, alloc_obj);
5320 result_i_o ->set_req(_objArray_path, i_o());
5321 result_mem ->set_req(_objArray_path, reset_memory());
5322 }
5323 }
5324 // Otherwise, there are no barriers to worry about.
5325 // (We can dispense with card marks if we know the allocation
5326 // comes out of eden (TLAB)... In fact, ReduceInitialCardMarks
5327 // causes the non-eden paths to take compensating steps to
5328 // simulate a fresh allocation, so that no further
5329 // card marks are required in compiled code to initialize
5330 // the object.)
5331
5332 if (!stopped()) {
5333 copy_to_clone(array_obj, alloc_obj, array_size, true);
5334
5335 // Present the results of the copy.
5336 result_reg->init_req(_array_path, control());
5337 result_val->init_req(_array_path, alloc_obj);
5338 result_i_o ->set_req(_array_path, i_o());
5339 result_mem ->set_req(_array_path, reset_memory());
5340 }
5341 }
5342
5343 // We only go to the instance fast case code if we pass a number of guards.
5344 // The paths which do not pass are accumulated in the slow_region.
5345 RegionNode* slow_region = new RegionNode(1);
5346 record_for_igvn(slow_region);
5347 if (!stopped()) {
5348 // It's an instance (we did array above). Make the slow-path tests.
5349 // If this is a virtual call, we generate a funny guard. We grab
5350 // the vtable entry corresponding to clone() from the target object.
5351 // If the target method which we are calling happens to be the
5352 // Object clone() method, we pass the guard. We do not need this
5353 // guard for non-virtual calls; the caller is known to be the native
5354 // Object clone().
5355 if (is_virtual) {
5356 generate_virtual_guard(obj_klass, slow_region);
5357 }
5358
5359 // The object must be easily cloneable and must not have a finalizer.
5360 // Both of these conditions may be checked in a single test.
5361 // We could optimize the test further, but we don't care.
5362 generate_misc_flags_guard(obj_klass,
5363 // Test both conditions:
5364 KlassFlags::_misc_is_cloneable_fast | KlassFlags::_misc_has_finalizer,
5365 // Must be cloneable but not finalizer:
5366 KlassFlags::_misc_is_cloneable_fast,
5458 set_jvms(sfpt->jvms());
5459 _reexecute_sp = jvms()->sp();
5460
5461 return saved_jvms;
5462 }
5463 }
5464 }
5465 return nullptr;
5466 }
5467
5468 // Clone the JVMState of the array allocation and create a new safepoint with it. Re-push the array length to the stack
5469 // such that uncommon traps can be emitted to re-execute the array allocation in the interpreter.
5470 SafePointNode* LibraryCallKit::create_safepoint_with_state_before_array_allocation(const AllocateArrayNode* alloc) const {
5471 JVMState* old_jvms = alloc->jvms()->clone_shallow(C);
5472 uint size = alloc->req();
5473 SafePointNode* sfpt = new SafePointNode(size, old_jvms);
5474 old_jvms->set_map(sfpt);
5475 for (uint i = 0; i < size; i++) {
5476 sfpt->init_req(i, alloc->in(i));
5477 }
5478 // re-push array length for deoptimization
5479 sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp(), alloc->in(AllocateNode::ALength));
5480 old_jvms->set_sp(old_jvms->sp()+1);
5481 old_jvms->set_monoff(old_jvms->monoff()+1);
5482 old_jvms->set_scloff(old_jvms->scloff()+1);
5483 old_jvms->set_endoff(old_jvms->endoff()+1);
5484 old_jvms->set_should_reexecute(true);
5485
5486 sfpt->set_i_o(map()->i_o());
5487 sfpt->set_memory(map()->memory());
5488 sfpt->set_control(map()->control());
5489 return sfpt;
5490 }
5491
5492 // In case of a deoptimization, we restart execution at the
5493 // allocation, allocating a new array. We would leave an uninitialized
5494 // array in the heap that GCs wouldn't expect. Move the allocation
5495 // after the traps so we don't allocate the array if we
5496 // deoptimize. This is possible because tightly_coupled_allocation()
5497 // guarantees there's no observer of the allocated array at this point
5498 // and the control flow is simple enough.
5499 void LibraryCallKit::arraycopy_move_allocation_here(AllocateArrayNode* alloc, Node* dest, JVMState* saved_jvms_before_guards,
5500 int saved_reexecute_sp, uint new_idx) {
5501 if (saved_jvms_before_guards != nullptr && !stopped()) {
5502 replace_unrelated_uncommon_traps_with_alloc_state(alloc, saved_jvms_before_guards);
5503
5504 assert(alloc != nullptr, "only with a tightly coupled allocation");
5505 // restore JVM state to the state at the arraycopy
5506 saved_jvms_before_guards->map()->set_control(map()->control());
5507 assert(saved_jvms_before_guards->map()->memory() == map()->memory(), "memory state changed?");
5508 assert(saved_jvms_before_guards->map()->i_o() == map()->i_o(), "IO state changed?");
5509 // If we've improved the types of some nodes (null check) while
5510 // emitting the guards, propagate them to the current state
5511 map()->replaced_nodes().apply(saved_jvms_before_guards->map(), new_idx);
5512 set_jvms(saved_jvms_before_guards);
5513 _reexecute_sp = saved_reexecute_sp;
5514
5515 // Remove the allocation from above the guards
5516 CallProjections callprojs;
5517 alloc->extract_projections(&callprojs, true);
5518 InitializeNode* init = alloc->initialization();
5519 Node* alloc_mem = alloc->in(TypeFunc::Memory);
5520 C->gvn_replace_by(callprojs.fallthrough_ioproj, alloc->in(TypeFunc::I_O));
5521 C->gvn_replace_by(init->proj_out(TypeFunc::Memory), alloc_mem);
5522
5523 // The CastIINode created in GraphKit::new_array (in AllocateArrayNode::make_ideal_length) must stay below
5524 // the allocation (i.e. is only valid if the allocation succeeds):
5525 // 1) replace CastIINode with AllocateArrayNode's length here
5526 // 2) Create CastIINode again once allocation has moved (see below) at the end of this method
5527 //
5528 // Multiple identical CastIINodes might exist here. Each GraphKit::load_array_length() call will generate
5529 // new separate CastIINode (arraycopy guard checks or any array length use between array allocation and ararycopy)
5530 Node* init_control = init->proj_out(TypeFunc::Control);
5531 Node* alloc_length = alloc->Ideal_length();
5532 #ifdef ASSERT
5533 Node* prev_cast = nullptr;
5534 #endif
5535 for (uint i = 0; i < init_control->outcnt(); i++) {
5536 Node* init_out = init_control->raw_out(i);
5537 if (init_out->is_CastII() && init_out->in(TypeFunc::Control) == init_control && init_out->in(1) == alloc_length) {
5538 #ifdef ASSERT
5539 if (prev_cast == nullptr) {
5540 prev_cast = init_out;
5542 if (prev_cast->cmp(*init_out) == false) {
5543 prev_cast->dump();
5544 init_out->dump();
5545 assert(false, "not equal CastIINode");
5546 }
5547 }
5548 #endif
5549 C->gvn_replace_by(init_out, alloc_length);
5550 }
5551 }
5552 C->gvn_replace_by(init->proj_out(TypeFunc::Control), alloc->in(0));
5553
5554 // move the allocation here (after the guards)
5555 _gvn.hash_delete(alloc);
5556 alloc->set_req(TypeFunc::Control, control());
5557 alloc->set_req(TypeFunc::I_O, i_o());
5558 Node *mem = reset_memory();
5559 set_all_memory(mem);
5560 alloc->set_req(TypeFunc::Memory, mem);
5561 set_control(init->proj_out_or_null(TypeFunc::Control));
5562 set_i_o(callprojs.fallthrough_ioproj);
5563
5564 // Update memory as done in GraphKit::set_output_for_allocation()
5565 const TypeInt* length_type = _gvn.find_int_type(alloc->in(AllocateNode::ALength));
5566 const TypeOopPtr* ary_type = _gvn.type(alloc->in(AllocateNode::KlassNode))->is_klassptr()->as_instance_type();
5567 if (ary_type->isa_aryptr() && length_type != nullptr) {
5568 ary_type = ary_type->is_aryptr()->cast_to_size(length_type);
5569 }
5570 const TypePtr* telemref = ary_type->add_offset(Type::OffsetBot);
5571 int elemidx = C->get_alias_index(telemref);
5572 set_memory(init->proj_out_or_null(TypeFunc::Memory), Compile::AliasIdxRaw);
5573 set_memory(init->proj_out_or_null(TypeFunc::Memory), elemidx);
5574
5575 Node* allocx = _gvn.transform(alloc);
5576 assert(allocx == alloc, "where has the allocation gone?");
5577 assert(dest->is_CheckCastPP(), "not an allocation result?");
5578
5579 _gvn.hash_delete(dest);
5580 dest->set_req(0, control());
5581 Node* destx = _gvn.transform(dest);
5582 assert(destx == dest, "where has the allocation result gone?");
5880 top_src = src_type->isa_aryptr();
5881 has_src = (top_src != nullptr && top_src->elem() != Type::BOTTOM);
5882 src_spec = true;
5883 }
5884 if (!has_dest) {
5885 dest = maybe_cast_profiled_obj(dest, dest_k, true);
5886 dest_type = _gvn.type(dest);
5887 top_dest = dest_type->isa_aryptr();
5888 has_dest = (top_dest != nullptr && top_dest->elem() != Type::BOTTOM);
5889 dest_spec = true;
5890 }
5891 }
5892 }
5893
5894 if (has_src && has_dest && can_emit_guards) {
5895 BasicType src_elem = top_src->isa_aryptr()->elem()->array_element_basic_type();
5896 BasicType dest_elem = top_dest->isa_aryptr()->elem()->array_element_basic_type();
5897 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
5898 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
5899
5900 if (src_elem == dest_elem && src_elem == T_OBJECT) {
5901 // If both arrays are object arrays then having the exact types
5902 // for both will remove the need for a subtype check at runtime
5903 // before the call and may make it possible to pick a faster copy
5904 // routine (without a subtype check on every element)
5905 // Do we have the exact type of src?
5906 bool could_have_src = src_spec;
5907 // Do we have the exact type of dest?
5908 bool could_have_dest = dest_spec;
5909 ciKlass* src_k = nullptr;
5910 ciKlass* dest_k = nullptr;
5911 if (!src_spec) {
5912 src_k = src_type->speculative_type_not_null();
5913 if (src_k != nullptr && src_k->is_array_klass()) {
5914 could_have_src = true;
5915 }
5916 }
5917 if (!dest_spec) {
5918 dest_k = dest_type->speculative_type_not_null();
5919 if (dest_k != nullptr && dest_k->is_array_klass()) {
5920 could_have_dest = true;
5921 }
5922 }
5923 if (could_have_src && could_have_dest) {
5924 // If we can have both exact types, emit the missing guards
5925 if (could_have_src && !src_spec) {
5926 src = maybe_cast_profiled_obj(src, src_k, true);
5927 }
5928 if (could_have_dest && !dest_spec) {
5929 dest = maybe_cast_profiled_obj(dest, dest_k, true);
5930 }
5931 }
5932 }
5933 }
5934
5935 ciMethod* trap_method = method();
5936 int trap_bci = bci();
5937 if (saved_jvms_before_guards != nullptr) {
5938 trap_method = alloc->jvms()->method();
5939 trap_bci = alloc->jvms()->bci();
5940 }
5941
5942 bool negative_length_guard_generated = false;
5943
5944 if (!C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_intrinsic) &&
5945 can_emit_guards &&
5946 !src->is_top() && !dest->is_top()) {
5947 // validate arguments: enables transformation the ArrayCopyNode
5948 validated = true;
5949
5950 RegionNode* slow_region = new RegionNode(1);
5951 record_for_igvn(slow_region);
5952
5953 // (1) src and dest are arrays.
5954 generate_non_array_guard(load_object_klass(src), slow_region, &src);
5955 generate_non_array_guard(load_object_klass(dest), slow_region, &dest);
5956
5957 // (2) src and dest arrays must have elements of the same BasicType
5958 // done at macro expansion or at Ideal transformation time
5959
5960 // (4) src_offset must not be negative.
5961 generate_negative_guard(src_offset, slow_region);
5962
5963 // (5) dest_offset must not be negative.
5964 generate_negative_guard(dest_offset, slow_region);
5965
5966 // (7) src_offset + length must not exceed length of src.
5969 slow_region);
5970
5971 // (8) dest_offset + length must not exceed length of dest.
5972 generate_limit_guard(dest_offset, length,
5973 load_array_length(dest),
5974 slow_region);
5975
5976 // (6) length must not be negative.
5977 // This is also checked in generate_arraycopy() during macro expansion, but
5978 // we also have to check it here for the case where the ArrayCopyNode will
5979 // be eliminated by Escape Analysis.
5980 if (EliminateAllocations) {
5981 generate_negative_guard(length, slow_region);
5982 negative_length_guard_generated = true;
5983 }
5984
5985 // (9) each element of an oop array must be assignable
5986 Node* dest_klass = load_object_klass(dest);
5987 if (src != dest) {
5988 Node* not_subtype_ctrl = gen_subtype_check(src, dest_klass);
5989
5990 if (not_subtype_ctrl != top()) {
5991 PreserveJVMState pjvms(this);
5992 set_control(not_subtype_ctrl);
5993 uncommon_trap(Deoptimization::Reason_intrinsic,
5994 Deoptimization::Action_make_not_entrant);
5995 assert(stopped(), "Should be stopped");
5996 }
5997 }
5998 {
5999 PreserveJVMState pjvms(this);
6000 set_control(_gvn.transform(slow_region));
6001 uncommon_trap(Deoptimization::Reason_intrinsic,
6002 Deoptimization::Action_make_not_entrant);
6003 assert(stopped(), "Should be stopped");
6004 }
6005
6006 const TypeKlassPtr* dest_klass_t = _gvn.type(dest_klass)->is_klassptr();
6007 const Type *toop = dest_klass_t->cast_to_exactness(false)->as_instance_type();
6008 src = _gvn.transform(new CheckCastPPNode(control(), src, toop));
6009 arraycopy_move_allocation_here(alloc, dest, saved_jvms_before_guards, saved_reexecute_sp, new_idx);
6010 }
6011
6012 if (stopped()) {
6013 return true;
6014 }
6015
6016 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, src, src_offset, dest, dest_offset, length, alloc != nullptr, negative_length_guard_generated,
6017 // Create LoadRange and LoadKlass nodes for use during macro expansion here
6018 // so the compiler has a chance to eliminate them: during macro expansion,
6019 // we have to set their control (CastPP nodes are eliminated).
6020 load_object_klass(src), load_object_klass(dest),
6021 load_array_length(src), load_array_length(dest));
6022
6023 ac->set_arraycopy(validated);
6024
6025 Node* n = _gvn.transform(ac);
6026 if (n == ac) {
6027 ac->connect_outputs(this);
6028 } else {
|
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 #include "asm/macroAssembler.hpp"
26 #include "ci/ciArrayKlass.hpp"
27 #include "ci/ciFlatArrayKlass.hpp"
28 #include "ci/ciInstanceKlass.hpp"
29 #include "ci/ciSymbols.hpp"
30 #include "ci/ciUtilities.inline.hpp"
31 #include "classfile/vmIntrinsics.hpp"
32 #include "compiler/compileBroker.hpp"
33 #include "compiler/compileLog.hpp"
34 #include "gc/shared/barrierSet.hpp"
35 #include "gc/shared/c2/barrierSetC2.hpp"
36 #include "jfr/support/jfrIntrinsics.hpp"
37 #include "memory/resourceArea.hpp"
38 #include "oops/accessDecorators.hpp"
39 #include "oops/klass.inline.hpp"
40 #include "oops/layoutKind.hpp"
41 #include "oops/objArrayKlass.hpp"
42 #include "opto/addnode.hpp"
43 #include "opto/arraycopynode.hpp"
44 #include "opto/c2compiler.hpp"
45 #include "opto/castnode.hpp"
46 #include "opto/cfgnode.hpp"
47 #include "opto/convertnode.hpp"
48 #include "opto/countbitsnode.hpp"
49 #include "opto/graphKit.hpp"
50 #include "opto/idealKit.hpp"
51 #include "opto/inlinetypenode.hpp"
52 #include "opto/library_call.hpp"
53 #include "opto/mathexactnode.hpp"
54 #include "opto/mulnode.hpp"
55 #include "opto/narrowptrnode.hpp"
56 #include "opto/opaquenode.hpp"
57 #include "opto/opcodes.hpp"
58 #include "opto/parse.hpp"
59 #include "opto/rootnode.hpp"
60 #include "opto/runtime.hpp"
61 #include "opto/subnode.hpp"
62 #include "opto/type.hpp"
63 #include "opto/vectornode.hpp"
64 #include "prims/jvmtiExport.hpp"
65 #include "prims/jvmtiThreadState.hpp"
66 #include "prims/unsafe.hpp"
67 #include "runtime/jniHandles.inline.hpp"
68 #include "runtime/objectMonitor.hpp"
69 #include "runtime/sharedRuntime.hpp"
70 #include "runtime/stubRoutines.hpp"
71 #include "utilities/globalDefinitions.hpp"
72 #include "utilities/macros.hpp"
73 #include "utilities/powerOfTwo.hpp"
74
75 //---------------------------make_vm_intrinsic----------------------------
76 CallGenerator* Compile::make_vm_intrinsic(ciMethod* m, bool is_virtual) {
77 vmIntrinsicID id = m->intrinsic_id();
78 assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
79
80 if (!m->is_loaded()) {
81 // Do not attempt to inline unloaded methods.
82 return nullptr;
83 }
84
85 C2Compiler* compiler = (C2Compiler*)CompileBroker::compiler(CompLevel_full_optimization);
86 bool is_available = false;
87
88 {
89 // For calling is_intrinsic_supported and is_intrinsic_disabled_by_flag
90 // the compiler must transition to '_thread_in_vm' state because both
91 // methods access VM-internal data.
310 case vmIntrinsics::_indexOfIL: return inline_string_indexOfI(StrIntrinsicNode::LL);
311 case vmIntrinsics::_indexOfIU: return inline_string_indexOfI(StrIntrinsicNode::UU);
312 case vmIntrinsics::_indexOfIUL: return inline_string_indexOfI(StrIntrinsicNode::UL);
313 case vmIntrinsics::_indexOfU_char: return inline_string_indexOfChar(StrIntrinsicNode::U);
314 case vmIntrinsics::_indexOfL_char: return inline_string_indexOfChar(StrIntrinsicNode::L);
315
316 case vmIntrinsics::_equalsL: return inline_string_equals(StrIntrinsicNode::LL);
317
318 case vmIntrinsics::_vectorizedHashCode: return inline_vectorizedHashCode();
319
320 case vmIntrinsics::_toBytesStringU: return inline_string_toBytesU();
321 case vmIntrinsics::_getCharsStringU: return inline_string_getCharsU();
322 case vmIntrinsics::_getCharStringU: return inline_string_char_access(!is_store);
323 case vmIntrinsics::_putCharStringU: return inline_string_char_access( is_store);
324
325 case vmIntrinsics::_compressStringC:
326 case vmIntrinsics::_compressStringB: return inline_string_copy( is_compress);
327 case vmIntrinsics::_inflateStringC:
328 case vmIntrinsics::_inflateStringB: return inline_string_copy(!is_compress);
329
330 case vmIntrinsics::_makePrivateBuffer: return inline_unsafe_make_private_buffer();
331 case vmIntrinsics::_finishPrivateBuffer: return inline_unsafe_finish_private_buffer();
332 case vmIntrinsics::_getReference: return inline_unsafe_access(!is_store, T_OBJECT, Relaxed, false);
333 case vmIntrinsics::_getBoolean: return inline_unsafe_access(!is_store, T_BOOLEAN, Relaxed, false);
334 case vmIntrinsics::_getByte: return inline_unsafe_access(!is_store, T_BYTE, Relaxed, false);
335 case vmIntrinsics::_getShort: return inline_unsafe_access(!is_store, T_SHORT, Relaxed, false);
336 case vmIntrinsics::_getChar: return inline_unsafe_access(!is_store, T_CHAR, Relaxed, false);
337 case vmIntrinsics::_getInt: return inline_unsafe_access(!is_store, T_INT, Relaxed, false);
338 case vmIntrinsics::_getLong: return inline_unsafe_access(!is_store, T_LONG, Relaxed, false);
339 case vmIntrinsics::_getFloat: return inline_unsafe_access(!is_store, T_FLOAT, Relaxed, false);
340 case vmIntrinsics::_getDouble: return inline_unsafe_access(!is_store, T_DOUBLE, Relaxed, false);
341 case vmIntrinsics::_getValue: return inline_unsafe_access(!is_store, T_OBJECT, Relaxed, false, true);
342
343 case vmIntrinsics::_putReference: return inline_unsafe_access( is_store, T_OBJECT, Relaxed, false);
344 case vmIntrinsics::_putBoolean: return inline_unsafe_access( is_store, T_BOOLEAN, Relaxed, false);
345 case vmIntrinsics::_putByte: return inline_unsafe_access( is_store, T_BYTE, Relaxed, false);
346 case vmIntrinsics::_putShort: return inline_unsafe_access( is_store, T_SHORT, Relaxed, false);
347 case vmIntrinsics::_putChar: return inline_unsafe_access( is_store, T_CHAR, Relaxed, false);
348 case vmIntrinsics::_putInt: return inline_unsafe_access( is_store, T_INT, Relaxed, false);
349 case vmIntrinsics::_putLong: return inline_unsafe_access( is_store, T_LONG, Relaxed, false);
350 case vmIntrinsics::_putFloat: return inline_unsafe_access( is_store, T_FLOAT, Relaxed, false);
351 case vmIntrinsics::_putDouble: return inline_unsafe_access( is_store, T_DOUBLE, Relaxed, false);
352 case vmIntrinsics::_putValue: return inline_unsafe_access( is_store, T_OBJECT, Relaxed, false, true);
353
354 case vmIntrinsics::_getReferenceVolatile: return inline_unsafe_access(!is_store, T_OBJECT, Volatile, false);
355 case vmIntrinsics::_getBooleanVolatile: return inline_unsafe_access(!is_store, T_BOOLEAN, Volatile, false);
356 case vmIntrinsics::_getByteVolatile: return inline_unsafe_access(!is_store, T_BYTE, Volatile, false);
357 case vmIntrinsics::_getShortVolatile: return inline_unsafe_access(!is_store, T_SHORT, Volatile, false);
358 case vmIntrinsics::_getCharVolatile: return inline_unsafe_access(!is_store, T_CHAR, Volatile, false);
359 case vmIntrinsics::_getIntVolatile: return inline_unsafe_access(!is_store, T_INT, Volatile, false);
360 case vmIntrinsics::_getLongVolatile: return inline_unsafe_access(!is_store, T_LONG, Volatile, false);
361 case vmIntrinsics::_getFloatVolatile: return inline_unsafe_access(!is_store, T_FLOAT, Volatile, false);
362 case vmIntrinsics::_getDoubleVolatile: return inline_unsafe_access(!is_store, T_DOUBLE, Volatile, false);
363
364 case vmIntrinsics::_putReferenceVolatile: return inline_unsafe_access( is_store, T_OBJECT, Volatile, false);
365 case vmIntrinsics::_putBooleanVolatile: return inline_unsafe_access( is_store, T_BOOLEAN, Volatile, false);
366 case vmIntrinsics::_putByteVolatile: return inline_unsafe_access( is_store, T_BYTE, Volatile, false);
367 case vmIntrinsics::_putShortVolatile: return inline_unsafe_access( is_store, T_SHORT, Volatile, false);
368 case vmIntrinsics::_putCharVolatile: return inline_unsafe_access( is_store, T_CHAR, Volatile, false);
369 case vmIntrinsics::_putIntVolatile: return inline_unsafe_access( is_store, T_INT, Volatile, false);
370 case vmIntrinsics::_putLongVolatile: return inline_unsafe_access( is_store, T_LONG, Volatile, false);
371 case vmIntrinsics::_putFloatVolatile: return inline_unsafe_access( is_store, T_FLOAT, Volatile, false);
372 case vmIntrinsics::_putDoubleVolatile: return inline_unsafe_access( is_store, T_DOUBLE, Volatile, false);
404 case vmIntrinsics::_getReferenceOpaque: return inline_unsafe_access(!is_store, T_OBJECT, Opaque, false);
405 case vmIntrinsics::_getBooleanOpaque: return inline_unsafe_access(!is_store, T_BOOLEAN, Opaque, false);
406 case vmIntrinsics::_getByteOpaque: return inline_unsafe_access(!is_store, T_BYTE, Opaque, false);
407 case vmIntrinsics::_getShortOpaque: return inline_unsafe_access(!is_store, T_SHORT, Opaque, false);
408 case vmIntrinsics::_getCharOpaque: return inline_unsafe_access(!is_store, T_CHAR, Opaque, false);
409 case vmIntrinsics::_getIntOpaque: return inline_unsafe_access(!is_store, T_INT, Opaque, false);
410 case vmIntrinsics::_getLongOpaque: return inline_unsafe_access(!is_store, T_LONG, Opaque, false);
411 case vmIntrinsics::_getFloatOpaque: return inline_unsafe_access(!is_store, T_FLOAT, Opaque, false);
412 case vmIntrinsics::_getDoubleOpaque: return inline_unsafe_access(!is_store, T_DOUBLE, Opaque, false);
413
414 case vmIntrinsics::_putReferenceOpaque: return inline_unsafe_access( is_store, T_OBJECT, Opaque, false);
415 case vmIntrinsics::_putBooleanOpaque: return inline_unsafe_access( is_store, T_BOOLEAN, Opaque, false);
416 case vmIntrinsics::_putByteOpaque: return inline_unsafe_access( is_store, T_BYTE, Opaque, false);
417 case vmIntrinsics::_putShortOpaque: return inline_unsafe_access( is_store, T_SHORT, Opaque, false);
418 case vmIntrinsics::_putCharOpaque: return inline_unsafe_access( is_store, T_CHAR, Opaque, false);
419 case vmIntrinsics::_putIntOpaque: return inline_unsafe_access( is_store, T_INT, Opaque, false);
420 case vmIntrinsics::_putLongOpaque: return inline_unsafe_access( is_store, T_LONG, Opaque, false);
421 case vmIntrinsics::_putFloatOpaque: return inline_unsafe_access( is_store, T_FLOAT, Opaque, false);
422 case vmIntrinsics::_putDoubleOpaque: return inline_unsafe_access( is_store, T_DOUBLE, Opaque, false);
423
424 case vmIntrinsics::_getFlatValue: return inline_unsafe_flat_access(!is_store, Relaxed);
425 case vmIntrinsics::_putFlatValue: return inline_unsafe_flat_access( is_store, Relaxed);
426
427 case vmIntrinsics::_compareAndSetReference: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap, Volatile);
428 case vmIntrinsics::_compareAndSetByte: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap, Volatile);
429 case vmIntrinsics::_compareAndSetShort: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap, Volatile);
430 case vmIntrinsics::_compareAndSetInt: return inline_unsafe_load_store(T_INT, LS_cmp_swap, Volatile);
431 case vmIntrinsics::_compareAndSetLong: return inline_unsafe_load_store(T_LONG, LS_cmp_swap, Volatile);
432
433 case vmIntrinsics::_weakCompareAndSetReferencePlain: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Relaxed);
434 case vmIntrinsics::_weakCompareAndSetReferenceAcquire: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Acquire);
435 case vmIntrinsics::_weakCompareAndSetReferenceRelease: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Release);
436 case vmIntrinsics::_weakCompareAndSetReference: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Volatile);
437 case vmIntrinsics::_weakCompareAndSetBytePlain: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Relaxed);
438 case vmIntrinsics::_weakCompareAndSetByteAcquire: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Acquire);
439 case vmIntrinsics::_weakCompareAndSetByteRelease: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Release);
440 case vmIntrinsics::_weakCompareAndSetByte: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Volatile);
441 case vmIntrinsics::_weakCompareAndSetShortPlain: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Relaxed);
442 case vmIntrinsics::_weakCompareAndSetShortAcquire: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Acquire);
443 case vmIntrinsics::_weakCompareAndSetShortRelease: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Release);
444 case vmIntrinsics::_weakCompareAndSetShort: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Volatile);
445 case vmIntrinsics::_weakCompareAndSetIntPlain: return inline_unsafe_load_store(T_INT, LS_cmp_swap_weak, Relaxed);
446 case vmIntrinsics::_weakCompareAndSetIntAcquire: return inline_unsafe_load_store(T_INT, LS_cmp_swap_weak, Acquire);
514 #endif
515 case vmIntrinsics::_currentTimeMillis: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeMillis), "currentTimeMillis");
516 case vmIntrinsics::_nanoTime: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeNanos), "nanoTime");
517 case vmIntrinsics::_writeback0: return inline_unsafe_writeback0();
518 case vmIntrinsics::_writebackPreSync0: return inline_unsafe_writebackSync0(true);
519 case vmIntrinsics::_writebackPostSync0: return inline_unsafe_writebackSync0(false);
520 case vmIntrinsics::_allocateInstance: return inline_unsafe_allocate();
521 case vmIntrinsics::_copyMemory: return inline_unsafe_copyMemory();
522 case vmIntrinsics::_setMemory: return inline_unsafe_setMemory();
523 case vmIntrinsics::_getLength: return inline_native_getLength();
524 case vmIntrinsics::_copyOf: return inline_array_copyOf(false);
525 case vmIntrinsics::_copyOfRange: return inline_array_copyOf(true);
526 case vmIntrinsics::_equalsB: return inline_array_equals(StrIntrinsicNode::LL);
527 case vmIntrinsics::_equalsC: return inline_array_equals(StrIntrinsicNode::UU);
528 case vmIntrinsics::_Preconditions_checkIndex: return inline_preconditions_checkIndex(T_INT);
529 case vmIntrinsics::_Preconditions_checkLongIndex: return inline_preconditions_checkIndex(T_LONG);
530 case vmIntrinsics::_clone: return inline_native_clone(intrinsic()->is_virtual());
531
532 case vmIntrinsics::_allocateUninitializedArray: return inline_unsafe_newArray(true);
533 case vmIntrinsics::_newArray: return inline_unsafe_newArray(false);
534 case vmIntrinsics::_newNullRestrictedNonAtomicArray: return inline_newArray(/* null_free */ true, /* atomic */ false);
535 case vmIntrinsics::_newNullRestrictedAtomicArray: return inline_newArray(/* null_free */ true, /* atomic */ true);
536 case vmIntrinsics::_newNullableAtomicArray: return inline_newArray(/* null_free */ false, /* atomic */ true);
537
538 case vmIntrinsics::_isAssignableFrom: return inline_native_subtype_check();
539
540 case vmIntrinsics::_isInstance:
541 case vmIntrinsics::_isHidden:
542 case vmIntrinsics::_getSuperclass:
543 case vmIntrinsics::_getClassAccessFlags: return inline_native_Class_query(intrinsic_id());
544
545 case vmIntrinsics::_floatToRawIntBits:
546 case vmIntrinsics::_floatToIntBits:
547 case vmIntrinsics::_intBitsToFloat:
548 case vmIntrinsics::_doubleToRawLongBits:
549 case vmIntrinsics::_doubleToLongBits:
550 case vmIntrinsics::_longBitsToDouble:
551 case vmIntrinsics::_floatToFloat16:
552 case vmIntrinsics::_float16ToFloat: return inline_fp_conversions(intrinsic_id());
553 case vmIntrinsics::_sqrt_float16: return inline_fp16_operations(intrinsic_id(), 1);
554 case vmIntrinsics::_fma_float16: return inline_fp16_operations(intrinsic_id(), 3);
555 case vmIntrinsics::_floatIsFinite:
556 case vmIntrinsics::_floatIsInfinite:
2322 case vmIntrinsics::_remainderUnsigned_l: {
2323 zero_check_long(argument(2));
2324 // Compile-time detect of null-exception
2325 if (stopped()) {
2326 return true; // keep the graph constructed so far
2327 }
2328 n = new UModLNode(control(), argument(0), argument(2));
2329 break;
2330 }
2331 default: fatal_unexpected_iid(id); break;
2332 }
2333 set_result(_gvn.transform(n));
2334 return true;
2335 }
2336
2337 //----------------------------inline_unsafe_access----------------------------
2338
2339 const TypeOopPtr* LibraryCallKit::sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type) {
2340 // Attempt to infer a sharper value type from the offset and base type.
2341 ciKlass* sharpened_klass = nullptr;
2342 bool null_free = false;
2343
2344 // See if it is an instance field, with an object type.
2345 if (alias_type->field() != nullptr) {
2346 if (alias_type->field()->type()->is_klass()) {
2347 sharpened_klass = alias_type->field()->type()->as_klass();
2348 null_free = alias_type->field()->is_null_free();
2349 }
2350 }
2351
2352 const TypeOopPtr* result = nullptr;
2353 // See if it is a narrow oop array.
2354 if (adr_type->isa_aryptr()) {
2355 if (adr_type->offset() >= refArrayOopDesc::base_offset_in_bytes()) {
2356 const TypeOopPtr* elem_type = adr_type->is_aryptr()->elem()->make_oopptr();
2357 null_free = adr_type->is_aryptr()->is_null_free();
2358 if (elem_type != nullptr && elem_type->is_loaded()) {
2359 // Sharpen the value type.
2360 result = elem_type;
2361 }
2362 }
2363 }
2364
2365 // The sharpened class might be unloaded if there is no class loader
2366 // contraint in place.
2367 if (result == nullptr && sharpened_klass != nullptr && sharpened_klass->is_loaded()) {
2368 // Sharpen the value type.
2369 result = TypeOopPtr::make_from_klass(sharpened_klass);
2370 if (null_free) {
2371 result = result->join_speculative(TypePtr::NOTNULL)->is_oopptr();
2372 }
2373 }
2374 if (result != nullptr) {
2375 #ifndef PRODUCT
2376 if (C->print_intrinsics() || C->print_inlining()) {
2377 tty->print(" from base type: "); adr_type->dump(); tty->cr();
2378 tty->print(" sharpened value: "); result->dump(); tty->cr();
2379 }
2380 #endif
2381 }
2382 return result;
2383 }
2384
2385 DecoratorSet LibraryCallKit::mo_decorator_for_access_kind(AccessKind kind) {
2386 switch (kind) {
2387 case Relaxed:
2388 return MO_UNORDERED;
2389 case Opaque:
2390 return MO_RELAXED;
2391 case Acquire:
2392 return MO_ACQUIRE;
2393 case Release:
2394 return MO_RELEASE;
2395 case Volatile:
2396 return MO_SEQ_CST;
2397 default:
2398 ShouldNotReachHere();
2399 return 0;
2400 }
2401 }
2402
2403 bool LibraryCallKit::inline_unsafe_access(bool is_store, const BasicType type, const AccessKind kind, const bool unaligned, const bool is_flat) {
2404 if (callee()->is_static()) return false; // caller must have the capability!
2405 DecoratorSet decorators = C2_UNSAFE_ACCESS;
2406 guarantee(!is_store || kind != Acquire, "Acquire accesses can be produced only for loads");
2407 guarantee( is_store || kind != Release, "Release accesses can be produced only for stores");
2408 assert(type != T_OBJECT || !unaligned, "unaligned access not supported with object type");
2409
2410 if (is_reference_type(type)) {
2411 decorators |= ON_UNKNOWN_OOP_REF;
2412 }
2413
2414 if (unaligned) {
2415 decorators |= C2_UNALIGNED;
2416 }
2417
2418 #ifndef PRODUCT
2419 {
2420 ResourceMark rm;
2421 // Check the signatures.
2422 ciSignature* sig = callee()->signature();
2423 #ifdef ASSERT
2424 if (!is_store) {
2425 // Object getReference(Object base, int/long offset), etc.
2426 BasicType rtype = sig->return_type()->basic_type();
2427 assert(rtype == type, "getter must return the expected value");
2428 assert(sig->count() == 2 || (is_flat && sig->count() == 3), "oop getter has 2 or 3 arguments");
2429 assert(sig->type_at(0)->basic_type() == T_OBJECT, "getter base is object");
2430 assert(sig->type_at(1)->basic_type() == T_LONG, "getter offset is correct");
2431 } else {
2432 // void putReference(Object base, int/long offset, Object x), etc.
2433 assert(sig->return_type()->basic_type() == T_VOID, "putter must not return a value");
2434 assert(sig->count() == 3 || (is_flat && sig->count() == 4), "oop putter has 3 arguments");
2435 assert(sig->type_at(0)->basic_type() == T_OBJECT, "putter base is object");
2436 assert(sig->type_at(1)->basic_type() == T_LONG, "putter offset is correct");
2437 BasicType vtype = sig->type_at(sig->count()-1)->basic_type();
2438 assert(vtype == type, "putter must accept the expected value");
2439 }
2440 #endif // ASSERT
2441 }
2442 #endif //PRODUCT
2443
2444 C->set_has_unsafe_access(true); // Mark eventual nmethod as "unsafe".
2445
2446 Node* receiver = argument(0); // type: oop
2447
2448 // Build address expression.
2449 Node* heap_base_oop = top();
2450
2451 // The base is either a Java object or a value produced by Unsafe.staticFieldBase
2452 Node* base = argument(1); // type: oop
2453 // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
2454 Node* offset = argument(2); // type: long
2455 // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2456 // to be plain byte offsets, which are also the same as those accepted
2457 // by oopDesc::field_addr.
2458 assert(Unsafe_field_offset_to_byte_offset(11) == 11,
2459 "fieldOffset must be byte-scaled");
2460
2461 ciInlineKlass* inline_klass = nullptr;
2462 if (is_flat) {
2463 const TypeInstPtr* cls = _gvn.type(argument(4))->isa_instptr();
2464 if (cls == nullptr || cls->const_oop() == nullptr) {
2465 return false;
2466 }
2467 ciType* mirror_type = cls->const_oop()->as_instance()->java_mirror_type();
2468 if (!mirror_type->is_inlinetype()) {
2469 return false;
2470 }
2471 inline_klass = mirror_type->as_inline_klass();
2472 }
2473
2474 if (base->is_InlineType()) {
2475 assert(!is_store, "InlineTypeNodes are non-larval value objects");
2476 InlineTypeNode* vt = base->as_InlineType();
2477 if (offset->is_Con()) {
2478 long off = find_long_con(offset, 0);
2479 ciInlineKlass* vk = vt->type()->inline_klass();
2480 if ((long)(int)off != off || !vk->contains_field_offset(off)) {
2481 return false;
2482 }
2483
2484 ciField* field = vk->get_non_flat_field_by_offset(off);
2485 if (field != nullptr) {
2486 BasicType bt = type2field[field->type()->basic_type()];
2487 if (bt == T_ARRAY || bt == T_NARROWOOP) {
2488 bt = T_OBJECT;
2489 }
2490 if (bt == type && (!field->is_flat() || field->type() == inline_klass)) {
2491 Node* value = vt->field_value_by_offset(off, false);
2492 if (value->is_InlineType()) {
2493 value = value->as_InlineType()->adjust_scalarization_depth(this);
2494 }
2495 set_result(value);
2496 return true;
2497 }
2498 }
2499 }
2500 {
2501 // Re-execute the unsafe access if allocation triggers deoptimization.
2502 PreserveReexecuteState preexecs(this);
2503 jvms()->set_should_reexecute(true);
2504 vt = vt->buffer(this);
2505 }
2506 base = vt->get_oop();
2507 }
2508
2509 // 32-bit machines ignore the high half!
2510 offset = ConvL2X(offset);
2511
2512 // Save state and restore on bailout
2513 uint old_sp = sp();
2514 SafePointNode* old_map = clone_map();
2515
2516 Node* adr = make_unsafe_address(base, offset, type, kind == Relaxed);
2517 assert(!stopped(), "Inlining of unsafe access failed: address construction stopped unexpectedly");
2518
2519 if (_gvn.type(base->uncast())->isa_ptr() == TypePtr::NULL_PTR) {
2520 if (type != T_OBJECT && (inline_klass == nullptr || !inline_klass->has_object_fields())) {
2521 decorators |= IN_NATIVE; // off-heap primitive access
2522 } else {
2523 set_map(old_map);
2524 set_sp(old_sp);
2525 return false; // off-heap oop accesses are not supported
2526 }
2527 } else {
2528 heap_base_oop = base; // on-heap or mixed access
2529 }
2530
2531 // Can base be null? Otherwise, always on-heap access.
2532 bool can_access_non_heap = TypePtr::NULL_PTR->higher_equal(_gvn.type(base));
2533
2534 if (!can_access_non_heap) {
2535 decorators |= IN_HEAP;
2536 }
2537
2538 Node* val = is_store ? argument(4 + (is_flat ? 1 : 0)) : nullptr;
2539
2540 const TypePtr* adr_type = _gvn.type(adr)->isa_ptr();
2541 if (adr_type == TypePtr::NULL_PTR) {
2542 set_map(old_map);
2543 set_sp(old_sp);
2544 return false; // off-heap access with zero address
2545 }
2546
2547 // Try to categorize the address.
2548 Compile::AliasType* alias_type = C->alias_type(adr_type);
2549 assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2550
2551 if (alias_type->adr_type() == TypeInstPtr::KLASS ||
2552 alias_type->adr_type() == TypeAryPtr::RANGE) {
2553 set_map(old_map);
2554 set_sp(old_sp);
2555 return false; // not supported
2556 }
2557
2558 bool mismatched = false;
2559 BasicType bt = T_ILLEGAL;
2560 ciField* field = nullptr;
2561 if (adr_type->isa_instptr()) {
2562 const TypeInstPtr* instptr = adr_type->is_instptr();
2563 ciInstanceKlass* k = instptr->instance_klass();
2564 int off = instptr->offset();
2565 if (instptr->const_oop() != nullptr &&
2566 k == ciEnv::current()->Class_klass() &&
2567 instptr->offset() >= (k->size_helper() * wordSize)) {
2568 k = instptr->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
2569 field = k->get_field_by_offset(off, true);
2570 } else {
2571 field = k->get_non_flat_field_by_offset(off);
2572 }
2573 if (field != nullptr) {
2574 bt = type2field[field->type()->basic_type()];
2575 }
2576 if (bt != alias_type->basic_type()) {
2577 // Type mismatch. Is it an access to a nested flat field?
2578 field = k->get_field_by_offset(off, false);
2579 if (field != nullptr) {
2580 bt = type2field[field->type()->basic_type()];
2581 }
2582 }
2583 assert(bt == alias_type->basic_type() || is_flat, "should match");
2584 } else {
2585 bt = alias_type->basic_type();
2586 }
2587
2588 if (bt != T_ILLEGAL) {
2589 assert(alias_type->adr_type()->is_oopptr(), "should be on-heap access");
2590 if (bt == T_BYTE && adr_type->isa_aryptr()) {
2591 // Alias type doesn't differentiate between byte[] and boolean[]).
2592 // Use address type to get the element type.
2593 bt = adr_type->is_aryptr()->elem()->array_element_basic_type();
2594 }
2595 if (is_reference_type(bt, true)) {
2596 // accessing an array field with getReference is not a mismatch
2597 bt = T_OBJECT;
2598 }
2599 if ((bt == T_OBJECT) != (type == T_OBJECT)) {
2600 // Don't intrinsify mismatched object accesses
2601 set_map(old_map);
2602 set_sp(old_sp);
2603 return false;
2604 }
2605 mismatched = (bt != type);
2606 } else if (alias_type->adr_type()->isa_oopptr()) {
2607 mismatched = true; // conservatively mark all "wide" on-heap accesses as mismatched
2608 }
2609
2610 if (is_flat) {
2611 if (adr_type->isa_instptr()) {
2612 if (field == nullptr || field->type() != inline_klass) {
2613 mismatched = true;
2614 }
2615 } else if (adr_type->isa_aryptr()) {
2616 const Type* elem = adr_type->is_aryptr()->elem();
2617 if (!adr_type->is_flat() || elem->inline_klass() != inline_klass) {
2618 mismatched = true;
2619 }
2620 } else {
2621 mismatched = true;
2622 }
2623 if (is_store) {
2624 const Type* val_t = _gvn.type(val);
2625 if (!val_t->is_inlinetypeptr() || val_t->inline_klass() != inline_klass) {
2626 set_map(old_map);
2627 set_sp(old_sp);
2628 return false;
2629 }
2630 }
2631 }
2632
2633 destruct_map_clone(old_map);
2634 assert(!mismatched || is_flat || alias_type->adr_type()->is_oopptr(), "off-heap access can't be mismatched");
2635
2636 if (mismatched) {
2637 decorators |= C2_MISMATCHED;
2638 }
2639
2640 // First guess at the value type.
2641 const Type *value_type = Type::get_const_basic_type(type);
2642
2643 // Figure out the memory ordering.
2644 decorators |= mo_decorator_for_access_kind(kind);
2645
2646 if (!is_store) {
2647 if (type == T_OBJECT && !is_flat) {
2648 const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2649 if (tjp != nullptr) {
2650 value_type = tjp;
2651 }
2652 }
2653 }
2654
2655 receiver = null_check(receiver);
2656 if (stopped()) {
2657 return true;
2658 }
2659 // Heap pointers get a null-check from the interpreter,
2660 // as a courtesy. However, this is not guaranteed by Unsafe,
2661 // and it is not possible to fully distinguish unintended nulls
2662 // from intended ones in this API.
2663
2664 if (!is_store) {
2665 Node* p = nullptr;
2666 // Try to constant fold a load from a constant field
2667
2668 if (heap_base_oop != top() && field != nullptr && field->is_constant() && !field->is_flat() && !mismatched) {
2669 // final or stable field
2670 p = make_constant_from_field(field, heap_base_oop);
2671 }
2672
2673 if (p == nullptr) { // Could not constant fold the load
2674 if (is_flat) {
2675 p = InlineTypeNode::make_from_flat(this, inline_klass, base, adr, adr_type, false, false, true);
2676 } else {
2677 p = access_load_at(heap_base_oop, adr, adr_type, value_type, type, decorators);
2678 const TypeOopPtr* ptr = value_type->make_oopptr();
2679 if (ptr != nullptr && ptr->is_inlinetypeptr()) {
2680 // Load a non-flattened inline type from memory
2681 p = InlineTypeNode::make_from_oop(this, p, ptr->inline_klass());
2682 }
2683 }
2684 // Normalize the value returned by getBoolean in the following cases
2685 if (type == T_BOOLEAN &&
2686 (mismatched ||
2687 heap_base_oop == top() || // - heap_base_oop is null or
2688 (can_access_non_heap && field == nullptr)) // - heap_base_oop is potentially null
2689 // and the unsafe access is made to large offset
2690 // (i.e., larger than the maximum offset necessary for any
2691 // field access)
2692 ) {
2693 IdealKit ideal = IdealKit(this);
2694 #define __ ideal.
2695 IdealVariable normalized_result(ideal);
2696 __ declarations_done();
2697 __ set(normalized_result, p);
2698 __ if_then(p, BoolTest::ne, ideal.ConI(0));
2699 __ set(normalized_result, ideal.ConI(1));
2700 ideal.end_if();
2701 final_sync(ideal);
2702 p = __ value(normalized_result);
2703 #undef __
2704 }
2705 }
2706 if (type == T_ADDRESS) {
2707 p = gvn().transform(new CastP2XNode(nullptr, p));
2708 p = ConvX2UL(p);
2709 }
2710 // The load node has the control of the preceding MemBarCPUOrder. All
2711 // following nodes will have the control of the MemBarCPUOrder inserted at
2712 // the end of this method. So, pushing the load onto the stack at a later
2713 // point is fine.
2714 set_result(p);
2715 } else {
2716 if (bt == T_ADDRESS) {
2717 // Repackage the long as a pointer.
2718 val = ConvL2X(val);
2719 val = gvn().transform(new CastX2PNode(val));
2720 }
2721 if (is_flat) {
2722 val->as_InlineType()->store_flat(this, base, adr, false, false, true, decorators);
2723 } else {
2724 access_store_at(heap_base_oop, adr, adr_type, val, value_type, type, decorators);
2725 }
2726 }
2727
2728 return true;
2729 }
2730
2731 bool LibraryCallKit::inline_unsafe_flat_access(bool is_store, AccessKind kind) {
2732 #ifdef ASSERT
2733 {
2734 ResourceMark rm;
2735 // Check the signatures.
2736 ciSignature* sig = callee()->signature();
2737 assert(sig->type_at(0)->basic_type() == T_OBJECT, "base should be object, but is %s", type2name(sig->type_at(0)->basic_type()));
2738 assert(sig->type_at(1)->basic_type() == T_LONG, "offset should be long, but is %s", type2name(sig->type_at(1)->basic_type()));
2739 assert(sig->type_at(2)->basic_type() == T_INT, "layout kind should be int, but is %s", type2name(sig->type_at(3)->basic_type()));
2740 assert(sig->type_at(3)->basic_type() == T_OBJECT, "value klass should be object, but is %s", type2name(sig->type_at(4)->basic_type()));
2741 if (is_store) {
2742 assert(sig->return_type()->basic_type() == T_VOID, "putter must not return a value, but returns %s", type2name(sig->return_type()->basic_type()));
2743 assert(sig->count() == 5, "flat putter should have 5 arguments, but has %d", sig->count());
2744 assert(sig->type_at(4)->basic_type() == T_OBJECT, "put value should be object, but is %s", type2name(sig->type_at(5)->basic_type()));
2745 } else {
2746 assert(sig->return_type()->basic_type() == T_OBJECT, "getter must return an object, but returns %s", type2name(sig->return_type()->basic_type()));
2747 assert(sig->count() == 4, "flat getter should have 4 arguments, but has %d", sig->count());
2748 }
2749 }
2750 #endif // ASSERT
2751
2752 assert(kind == Relaxed, "Only plain accesses for now");
2753 if (callee()->is_static()) {
2754 // caller must have the capability!
2755 return false;
2756 }
2757 C->set_has_unsafe_access(true);
2758
2759 const TypeInstPtr* value_klass_node = _gvn.type(argument(5))->isa_instptr();
2760 if (value_klass_node == nullptr || value_klass_node->const_oop() == nullptr) {
2761 // parameter valueType is not a constant
2762 return false;
2763 }
2764 ciType* mirror_type = value_klass_node->const_oop()->as_instance()->java_mirror_type();
2765 if (!mirror_type->is_inlinetype()) {
2766 // Dead code
2767 return false;
2768 }
2769 ciInlineKlass* value_klass = mirror_type->as_inline_klass();
2770
2771 const TypeInt* layout_type = _gvn.type(argument(4))->isa_int();
2772 if (layout_type == nullptr || !layout_type->is_con()) {
2773 // parameter layoutKind is not a constant
2774 return false;
2775 }
2776 assert(layout_type->get_con() >= static_cast<int>(LayoutKind::REFERENCE) &&
2777 layout_type->get_con() <= static_cast<int>(LayoutKind::UNKNOWN),
2778 "invalid layoutKind %d", layout_type->get_con());
2779 LayoutKind layout = static_cast<LayoutKind>(layout_type->get_con());
2780 assert(layout == LayoutKind::REFERENCE || layout == LayoutKind::NON_ATOMIC_FLAT ||
2781 layout == LayoutKind::ATOMIC_FLAT || layout == LayoutKind::NULLABLE_ATOMIC_FLAT,
2782 "unexpected layoutKind %d", layout_type->get_con());
2783
2784 null_check(argument(0));
2785 if (stopped()) {
2786 return true;
2787 }
2788
2789 Node* base = must_be_not_null(argument(1), true);
2790 Node* offset = argument(2);
2791 const Type* base_type = _gvn.type(base);
2792
2793 Node* ptr;
2794 bool immutable_memory = false;
2795 DecoratorSet decorators = C2_UNSAFE_ACCESS | IN_HEAP | MO_UNORDERED;
2796 if (base_type->isa_instptr()) {
2797 const TypeLong* offset_type = _gvn.type(offset)->isa_long();
2798 if (offset_type == nullptr || !offset_type->is_con()) {
2799 // Offset into a non-array should be a constant
2800 decorators |= C2_MISMATCHED;
2801 } else {
2802 int offset_con = checked_cast<int>(offset_type->get_con());
2803 ciInstanceKlass* base_klass = base_type->is_instptr()->instance_klass();
2804 ciField* field = base_klass->get_non_flat_field_by_offset(offset_con);
2805 if (field == nullptr) {
2806 assert(!base_klass->is_final(), "non-existence field at offset %d of class %s", offset_con, base_klass->name()->as_utf8());
2807 decorators |= C2_MISMATCHED;
2808 } else {
2809 assert(field->type() == value_klass, "field at offset %d of %s is of type %s, but valueType is %s",
2810 offset_con, base_klass->name()->as_utf8(), field->type()->name(), value_klass->name()->as_utf8());
2811 immutable_memory = field->is_strict() && field->is_final();
2812
2813 if (base->is_InlineType()) {
2814 assert(!is_store, "Cannot store into a non-larval value object");
2815 set_result(base->as_InlineType()->field_value_by_offset(offset_con, false));
2816 return true;
2817 }
2818 }
2819 }
2820
2821 if (base->is_InlineType()) {
2822 assert(!is_store, "Cannot store into a non-larval value object");
2823 base = base->as_InlineType()->buffer(this, true);
2824 }
2825 ptr = basic_plus_adr(base, ConvL2X(offset));
2826 } else if (base_type->isa_aryptr()) {
2827 decorators |= IS_ARRAY;
2828 if (layout == LayoutKind::REFERENCE) {
2829 if (!base_type->is_aryptr()->is_not_flat()) {
2830 const TypeAryPtr* array_type = base_type->is_aryptr()->cast_to_not_flat();
2831 Node* new_base = _gvn.transform(new CastPPNode(control(), base, array_type, ConstraintCastNode::StrongDependency));
2832 replace_in_map(base, new_base);
2833 base = new_base;
2834 }
2835 ptr = basic_plus_adr(base, ConvL2X(offset));
2836 } else {
2837 if (UseArrayFlattening) {
2838 // Flat array must have an exact type
2839 bool is_null_free = layout != LayoutKind::NULLABLE_ATOMIC_FLAT;
2840 bool is_atomic = layout != LayoutKind::NON_ATOMIC_FLAT;
2841 Node* new_base = cast_to_flat_array(base, value_klass, is_null_free, !is_null_free, is_atomic);
2842 replace_in_map(base, new_base);
2843 base = new_base;
2844 ptr = basic_plus_adr(base, ConvL2X(offset));
2845 const TypeAryPtr* ptr_type = _gvn.type(ptr)->is_aryptr();
2846 if (ptr_type->field_offset().get() != 0) {
2847 ptr = _gvn.transform(new CastPPNode(control(), ptr, ptr_type->with_field_offset(0), ConstraintCastNode::StrongDependency));
2848 }
2849 } else {
2850 uncommon_trap(Deoptimization::Reason_intrinsic,
2851 Deoptimization::Action_none);
2852 return true;
2853 }
2854 }
2855 } else {
2856 decorators |= C2_MISMATCHED;
2857 ptr = basic_plus_adr(base, ConvL2X(offset));
2858 }
2859
2860 if (is_store) {
2861 Node* value = argument(6);
2862 const Type* value_type = _gvn.type(value);
2863 if (!value_type->is_inlinetypeptr()) {
2864 value_type = Type::get_const_type(value_klass)->filter_speculative(value_type);
2865 Node* new_value = _gvn.transform(new CastPPNode(control(), value, value_type, ConstraintCastNode::StrongDependency));
2866 new_value = InlineTypeNode::make_from_oop(this, new_value, value_klass);
2867 replace_in_map(value, new_value);
2868 value = new_value;
2869 }
2870
2871 assert(value_type->inline_klass() == value_klass, "value is of type %s while valueType is %s", value_type->inline_klass()->name()->as_utf8(), value_klass->name()->as_utf8());
2872 if (layout == LayoutKind::REFERENCE) {
2873 const TypePtr* ptr_type = (decorators & C2_MISMATCHED) != 0 ? TypeRawPtr::BOTTOM : _gvn.type(ptr)->is_ptr();
2874 access_store_at(base, ptr, ptr_type, value, value_type, T_OBJECT, decorators);
2875 } else {
2876 bool atomic = layout != LayoutKind::NON_ATOMIC_FLAT;
2877 bool null_free = layout != LayoutKind::NULLABLE_ATOMIC_FLAT;
2878 value->as_InlineType()->store_flat(this, base, ptr, atomic, immutable_memory, null_free, decorators);
2879 }
2880
2881 return true;
2882 } else {
2883 decorators |= (C2_CONTROL_DEPENDENT_LOAD | C2_UNKNOWN_CONTROL_LOAD);
2884 InlineTypeNode* result;
2885 if (layout == LayoutKind::REFERENCE) {
2886 const TypePtr* ptr_type = (decorators & C2_MISMATCHED) != 0 ? TypeRawPtr::BOTTOM : _gvn.type(ptr)->is_ptr();
2887 Node* oop = access_load_at(base, ptr, ptr_type, Type::get_const_type(value_klass), T_OBJECT, decorators);
2888 result = InlineTypeNode::make_from_oop(this, oop, value_klass);
2889 } else {
2890 bool atomic = layout != LayoutKind::NON_ATOMIC_FLAT;
2891 bool null_free = layout != LayoutKind::NULLABLE_ATOMIC_FLAT;
2892 result = InlineTypeNode::make_from_flat(this, value_klass, base, ptr, atomic, immutable_memory, null_free, decorators);
2893 }
2894
2895 set_result(result);
2896 return true;
2897 }
2898 }
2899
2900 bool LibraryCallKit::inline_unsafe_make_private_buffer() {
2901 Node* receiver = argument(0);
2902 Node* value = argument(1);
2903
2904 const Type* type = gvn().type(value);
2905 if (!type->is_inlinetypeptr()) {
2906 C->record_method_not_compilable("value passed to Unsafe::makePrivateBuffer is not of a constant value type");
2907 return false;
2908 }
2909
2910 null_check(receiver);
2911 if (stopped()) {
2912 return true;
2913 }
2914
2915 value = null_check(value);
2916 if (stopped()) {
2917 return true;
2918 }
2919
2920 ciInlineKlass* vk = type->inline_klass();
2921 Node* klass = makecon(TypeKlassPtr::make(vk));
2922 Node* obj = new_instance(klass);
2923 AllocateNode::Ideal_allocation(obj)->_larval = true;
2924
2925 assert(value->is_InlineType(), "must be an InlineTypeNode");
2926 Node* payload_ptr = basic_plus_adr(obj, vk->payload_offset());
2927 value->as_InlineType()->store_flat(this, obj, payload_ptr, false, true, true, IN_HEAP | MO_UNORDERED);
2928
2929 set_result(obj);
2930 return true;
2931 }
2932
2933 bool LibraryCallKit::inline_unsafe_finish_private_buffer() {
2934 Node* receiver = argument(0);
2935 Node* buffer = argument(1);
2936
2937 const Type* type = gvn().type(buffer);
2938 if (!type->is_inlinetypeptr()) {
2939 C->record_method_not_compilable("value passed to Unsafe::finishPrivateBuffer is not of a constant value type");
2940 return false;
2941 }
2942
2943 AllocateNode* alloc = AllocateNode::Ideal_allocation(buffer);
2944 if (alloc == nullptr) {
2945 C->record_method_not_compilable("value passed to Unsafe::finishPrivateBuffer must be allocated by Unsafe::makePrivateBuffer");
2946 return false;
2947 }
2948
2949 null_check(receiver);
2950 if (stopped()) {
2951 return true;
2952 }
2953
2954 // Unset the larval bit in the object header
2955 Node* old_header = make_load(control(), buffer, TypeX_X, TypeX_X->basic_type(), MemNode::unordered, LoadNode::Pinned);
2956 Node* new_header = gvn().transform(new AndXNode(old_header, MakeConX(~markWord::larval_bit_in_place)));
2957 access_store_at(buffer, buffer, type->is_ptr(), new_header, TypeX_X, TypeX_X->basic_type(), MO_UNORDERED | IN_HEAP);
2958
2959 // We must ensure that the buffer is properly published
2960 insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out(AllocateNode::RawAddress));
2961 assert(!type->maybe_null(), "result of an allocation should not be null");
2962 set_result(InlineTypeNode::make_from_oop(this, buffer, type->inline_klass()));
2963 return true;
2964 }
2965
2966 //----------------------------inline_unsafe_load_store----------------------------
2967 // This method serves a couple of different customers (depending on LoadStoreKind):
2968 //
2969 // LS_cmp_swap:
2970 //
2971 // boolean compareAndSetReference(Object o, long offset, Object expected, Object x);
2972 // boolean compareAndSetInt( Object o, long offset, int expected, int x);
2973 // boolean compareAndSetLong( Object o, long offset, long expected, long x);
2974 //
2975 // LS_cmp_swap_weak:
2976 //
2977 // boolean weakCompareAndSetReference( Object o, long offset, Object expected, Object x);
2978 // boolean weakCompareAndSetReferencePlain( Object o, long offset, Object expected, Object x);
2979 // boolean weakCompareAndSetReferenceAcquire(Object o, long offset, Object expected, Object x);
2980 // boolean weakCompareAndSetReferenceRelease(Object o, long offset, Object expected, Object x);
2981 //
2982 // boolean weakCompareAndSetInt( Object o, long offset, int expected, int x);
2983 // boolean weakCompareAndSetIntPlain( Object o, long offset, int expected, int x);
2984 // boolean weakCompareAndSetIntAcquire( Object o, long offset, int expected, int x);
2985 // boolean weakCompareAndSetIntRelease( Object o, long offset, int expected, int x);
3151 }
3152 case LS_cmp_swap:
3153 case LS_cmp_swap_weak:
3154 case LS_get_add:
3155 break;
3156 default:
3157 ShouldNotReachHere();
3158 }
3159
3160 // Null check receiver.
3161 receiver = null_check(receiver);
3162 if (stopped()) {
3163 return true;
3164 }
3165
3166 int alias_idx = C->get_alias_index(adr_type);
3167
3168 if (is_reference_type(type)) {
3169 decorators |= IN_HEAP | ON_UNKNOWN_OOP_REF;
3170
3171 if (oldval != nullptr && oldval->is_InlineType()) {
3172 // Re-execute the unsafe access if allocation triggers deoptimization.
3173 PreserveReexecuteState preexecs(this);
3174 jvms()->set_should_reexecute(true);
3175 oldval = oldval->as_InlineType()->buffer(this)->get_oop();
3176 }
3177 if (newval != nullptr && newval->is_InlineType()) {
3178 // Re-execute the unsafe access if allocation triggers deoptimization.
3179 PreserveReexecuteState preexecs(this);
3180 jvms()->set_should_reexecute(true);
3181 newval = newval->as_InlineType()->buffer(this)->get_oop();
3182 }
3183
3184 // Transformation of a value which could be null pointer (CastPP #null)
3185 // could be delayed during Parse (for example, in adjust_map_after_if()).
3186 // Execute transformation here to avoid barrier generation in such case.
3187 if (_gvn.type(newval) == TypePtr::NULL_PTR)
3188 newval = _gvn.makecon(TypePtr::NULL_PTR);
3189
3190 if (oldval != nullptr && _gvn.type(oldval) == TypePtr::NULL_PTR) {
3191 // Refine the value to a null constant, when it is known to be null
3192 oldval = _gvn.makecon(TypePtr::NULL_PTR);
3193 }
3194 }
3195
3196 Node* result = nullptr;
3197 switch (kind) {
3198 case LS_cmp_exchange: {
3199 result = access_atomic_cmpxchg_val_at(base, adr, adr_type, alias_idx,
3200 oldval, newval, value_type, type, decorators);
3201 break;
3202 }
3203 case LS_cmp_swap_weak:
3350 Deoptimization::Action_make_not_entrant);
3351 }
3352 if (stopped()) {
3353 return true;
3354 }
3355 #endif //INCLUDE_JVMTI
3356
3357 Node* test = nullptr;
3358 if (LibraryCallKit::klass_needs_init_guard(kls)) {
3359 // Note: The argument might still be an illegal value like
3360 // Serializable.class or Object[].class. The runtime will handle it.
3361 // But we must make an explicit check for initialization.
3362 Node* insp = basic_plus_adr(kls, in_bytes(InstanceKlass::init_state_offset()));
3363 // Use T_BOOLEAN for InstanceKlass::_init_state so the compiler
3364 // can generate code to load it as unsigned byte.
3365 Node* inst = make_load(nullptr, insp, TypeInt::UBYTE, T_BOOLEAN, MemNode::acquire);
3366 Node* bits = intcon(InstanceKlass::fully_initialized);
3367 test = _gvn.transform(new SubINode(inst, bits));
3368 // The 'test' is non-zero if we need to take a slow path.
3369 }
3370 Node* obj = nullptr;
3371 const TypeInstKlassPtr* tkls = _gvn.type(kls)->isa_instklassptr();
3372 if (tkls != nullptr && tkls->instance_klass()->is_inlinetype()) {
3373 obj = InlineTypeNode::make_all_zero(_gvn, tkls->instance_klass()->as_inline_klass())->buffer(this);
3374 } else {
3375 obj = new_instance(kls, test);
3376 }
3377 set_result(obj);
3378 return true;
3379 }
3380
3381 //------------------------inline_native_time_funcs--------------
3382 // inline code for System.currentTimeMillis() and System.nanoTime()
3383 // these have the same type and signature
3384 bool LibraryCallKit::inline_native_time_funcs(address funcAddr, const char* funcName) {
3385 const TypeFunc* tf = OptoRuntime::void_long_Type();
3386 const TypePtr* no_memory_effects = nullptr;
3387 Node* time = make_runtime_call(RC_LEAF, tf, funcAddr, funcName, no_memory_effects);
3388 Node* value = _gvn.transform(new ProjNode(time, TypeFunc::Parms+0));
3389 #ifdef ASSERT
3390 Node* value_top = _gvn.transform(new ProjNode(time, TypeFunc::Parms+1));
3391 assert(value_top == top(), "second value must be top");
3392 #endif
3393 set_result(value);
3394 return true;
3395 }
3396
4137 Node* thread = _gvn.transform(new ThreadLocalNode());
4138 Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::vthread_offset()));
4139 Node* thread_obj_handle
4140 = make_load(nullptr, p, p->bottom_type()->is_ptr(), T_OBJECT, MemNode::unordered);
4141 thread_obj_handle = _gvn.transform(thread_obj_handle);
4142 const TypePtr *adr_type = _gvn.type(thread_obj_handle)->isa_ptr();
4143 access_store_at(nullptr, thread_obj_handle, adr_type, arr, _gvn.type(arr), T_OBJECT, IN_NATIVE | MO_UNORDERED);
4144
4145 // Change the _monitor_owner_id of the JavaThread
4146 Node* tid = load_field_from_object(arr, "tid", "J");
4147 Node* monitor_owner_id_offset = basic_plus_adr(thread, in_bytes(JavaThread::monitor_owner_id_offset()));
4148 store_to_memory(control(), monitor_owner_id_offset, tid, T_LONG, MemNode::unordered, true);
4149
4150 JFR_ONLY(extend_setCurrentThread(thread, arr);)
4151 return true;
4152 }
4153
4154 const Type* LibraryCallKit::scopedValueCache_type() {
4155 ciKlass* objects_klass = ciObjArrayKlass::make(env()->Object_klass());
4156 const TypeOopPtr* etype = TypeOopPtr::make_from_klass(env()->Object_klass());
4157 const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS, /* stable= */ false, /* flat= */ false, /* not_flat= */ true, /* not_null_free= */ true);
4158
4159 // Because we create the scopedValue cache lazily we have to make the
4160 // type of the result BotPTR.
4161 bool xk = etype->klass_is_exact();
4162 const Type* objects_type = TypeAryPtr::make(TypePtr::BotPTR, arr0, objects_klass, xk, TypeAryPtr::Offset(0));
4163 return objects_type;
4164 }
4165
4166 Node* LibraryCallKit::scopedValueCache_helper() {
4167 Node* thread = _gvn.transform(new ThreadLocalNode());
4168 Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::scopedValueCache_offset()));
4169 // We cannot use immutable_memory() because we might flip onto a
4170 // different carrier thread, at which point we'll need to use that
4171 // carrier thread's cache.
4172 // return _gvn.transform(LoadNode::make(_gvn, nullptr, immutable_memory(), p, p->bottom_type()->is_ptr(),
4173 // TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered));
4174 return make_load(nullptr, p, p->bottom_type()->is_ptr(), T_ADDRESS, MemNode::unordered);
4175 }
4176
4177 //------------------------inline_native_scopedValueCache------------------
4178 bool LibraryCallKit::inline_native_scopedValueCache() {
4179 Node* cache_obj_handle = scopedValueCache_helper();
4180 const Type* objects_type = scopedValueCache_type();
4181 set_result(access_load(cache_obj_handle, objects_type, T_OBJECT, IN_NATIVE));
4182
4266 store_to_memory(control(), pin_count_offset, next_pin_count, T_INT, MemNode::unordered);
4267
4268 // Result of top level CFG and Memory.
4269 RegionNode* result_rgn = new RegionNode(PATH_LIMIT);
4270 record_for_igvn(result_rgn);
4271 PhiNode* result_mem = new PhiNode(result_rgn, Type::MEMORY, TypePtr::BOTTOM);
4272 record_for_igvn(result_mem);
4273
4274 result_rgn->init_req(_true_path, _gvn.transform(valid_pin_count));
4275 result_rgn->init_req(_false_path, _gvn.transform(continuation_is_null));
4276 result_mem->init_req(_true_path, _gvn.transform(reset_memory()));
4277 result_mem->init_req(_false_path, _gvn.transform(input_memory_state));
4278
4279 // Set output state.
4280 set_control(_gvn.transform(result_rgn));
4281 set_all_memory(_gvn.transform(result_mem));
4282
4283 return true;
4284 }
4285
4286 //-----------------------load_klass_from_mirror_common-------------------------
4287 // Given a java mirror (a java.lang.Class oop), load its corresponding klass oop.
4288 // Test the klass oop for null (signifying a primitive Class like Integer.TYPE),
4289 // and branch to the given path on the region.
4290 // If never_see_null, take an uncommon trap on null, so we can optimistically
4291 // compile for the non-null case.
4292 // If the region is null, force never_see_null = true.
4293 Node* LibraryCallKit::load_klass_from_mirror_common(Node* mirror,
4294 bool never_see_null,
4295 RegionNode* region,
4296 int null_path,
4297 int offset) {
4298 if (region == nullptr) never_see_null = true;
4299 Node* p = basic_plus_adr(mirror, offset);
4300 const TypeKlassPtr* kls_type = TypeInstKlassPtr::OBJECT_OR_NULL;
4301 Node* kls = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), p, TypeRawPtr::BOTTOM, kls_type));
4302 Node* null_ctl = top();
4303 kls = null_check_oop(kls, &null_ctl, never_see_null);
4304 if (region != nullptr) {
4305 // Set region->in(null_path) if the mirror is a primitive (e.g, int.class).
4309 }
4310 return kls;
4311 }
4312
4313 //--------------------(inline_native_Class_query helpers)---------------------
4314 // Use this for JVM_ACC_INTERFACE.
4315 // Fall through if (mods & mask) == bits, take the guard otherwise.
4316 Node* LibraryCallKit::generate_klass_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region,
4317 ByteSize offset, const Type* type, BasicType bt) {
4318 // Branch around if the given klass has the given modifier bit set.
4319 // Like generate_guard, adds a new path onto the region.
4320 Node* modp = basic_plus_adr(kls, in_bytes(offset));
4321 Node* mods = make_load(nullptr, modp, type, bt, MemNode::unordered);
4322 Node* mask = intcon(modifier_mask);
4323 Node* bits = intcon(modifier_bits);
4324 Node* mbit = _gvn.transform(new AndINode(mods, mask));
4325 Node* cmp = _gvn.transform(new CmpINode(mbit, bits));
4326 Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
4327 return generate_fair_guard(bol, region);
4328 }
4329
4330 Node* LibraryCallKit::generate_interface_guard(Node* kls, RegionNode* region) {
4331 return generate_klass_flags_guard(kls, JVM_ACC_INTERFACE, 0, region,
4332 Klass::access_flags_offset(), TypeInt::CHAR, T_CHAR);
4333 }
4334
4335 // Use this for testing if Klass is_hidden, has_finalizer, and is_cloneable_fast.
4336 Node* LibraryCallKit::generate_misc_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region) {
4337 return generate_klass_flags_guard(kls, modifier_mask, modifier_bits, region,
4338 Klass::misc_flags_offset(), TypeInt::UBYTE, T_BOOLEAN);
4339 }
4340
4341 Node* LibraryCallKit::generate_hidden_class_guard(Node* kls, RegionNode* region) {
4342 return generate_misc_flags_guard(kls, KlassFlags::_misc_is_hidden_class, 0, region);
4343 }
4344
4345 //-------------------------inline_native_Class_query-------------------
4346 bool LibraryCallKit::inline_native_Class_query(vmIntrinsics::ID id) {
4347 const Type* return_type = TypeInt::BOOL;
4348 Node* prim_return_value = top(); // what happens if it's a primitive class?
4349 bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
4468
4469 case vmIntrinsics::_getClassAccessFlags:
4470 p = basic_plus_adr(kls, in_bytes(Klass::access_flags_offset()));
4471 query_value = make_load(nullptr, p, TypeInt::CHAR, T_CHAR, MemNode::unordered);
4472 break;
4473
4474 default:
4475 fatal_unexpected_iid(id);
4476 break;
4477 }
4478
4479 // Fall-through is the normal case of a query to a real class.
4480 phi->init_req(1, query_value);
4481 region->init_req(1, control());
4482
4483 C->set_has_split_ifs(true); // Has chance for split-if optimization
4484 set_result(region, phi);
4485 return true;
4486 }
4487
4488
4489 //-------------------------inline_Class_cast-------------------
4490 bool LibraryCallKit::inline_Class_cast() {
4491 Node* mirror = argument(0); // Class
4492 Node* obj = argument(1);
4493 const TypeInstPtr* mirror_con = _gvn.type(mirror)->isa_instptr();
4494 if (mirror_con == nullptr) {
4495 return false; // dead path (mirror->is_top()).
4496 }
4497 if (obj == nullptr || obj->is_top()) {
4498 return false; // dead path
4499 }
4500 const TypeOopPtr* tp = _gvn.type(obj)->isa_oopptr();
4501
4502 // First, see if Class.cast() can be folded statically.
4503 // java_mirror_type() returns non-null for compile-time Class constants.
4504 ciType* tm = mirror_con->java_mirror_type();
4505 if (tm != nullptr && tm->is_klass() &&
4506 tp != nullptr) {
4507 if (!tp->is_loaded()) {
4508 // Don't use intrinsic when class is not loaded.
4509 return false;
4510 } else {
4511 const TypeKlassPtr* tklass = TypeKlassPtr::make(tm->as_klass(), Type::trust_interfaces);
4512 int static_res = C->static_subtype_check(tklass, tp->as_klass_type());
4513 if (static_res == Compile::SSC_always_true) {
4514 // isInstance() is true - fold the code.
4515 set_result(obj);
4516 return true;
4517 } else if (static_res == Compile::SSC_always_false) {
4518 // Don't use intrinsic, have to throw ClassCastException.
4519 // If the reference is null, the non-intrinsic bytecode will
4520 // be optimized appropriately.
4521 return false;
4522 }
4523 }
4524 }
4525
4526 // Bailout intrinsic and do normal inlining if exception path is frequent.
4527 if (too_many_traps(Deoptimization::Reason_intrinsic)) {
4528 return false;
4529 }
4530
4531 // Generate dynamic checks.
4532 // Class.cast() is java implementation of _checkcast bytecode.
4533 // Do checkcast (Parse::do_checkcast()) optimizations here.
4534
4535 mirror = null_check(mirror);
4536 // If mirror is dead, only null-path is taken.
4537 if (stopped()) {
4538 return true;
4539 }
4540
4541 // Not-subtype or the mirror's klass ptr is nullptr (in case it is a primitive).
4542 enum { _bad_type_path = 1, _prim_path = 2, _npe_path = 3, PATH_LIMIT };
4543 RegionNode* region = new RegionNode(PATH_LIMIT);
4544 record_for_igvn(region);
4545
4546 // Now load the mirror's klass metaobject, and null-check it.
4547 // If kls is null, we have a primitive mirror and
4548 // nothing is an instance of a primitive type.
4549 Node* kls = load_klass_from_mirror(mirror, false, region, _prim_path);
4550
4551 Node* res = top();
4552 Node* io = i_o();
4553 Node* mem = merged_memory();
4554 if (!stopped()) {
4555
4556 Node* bad_type_ctrl = top();
4557 // Do checkcast optimizations.
4558 res = gen_checkcast(obj, kls, &bad_type_ctrl);
4559 region->init_req(_bad_type_path, bad_type_ctrl);
4560 }
4561 if (region->in(_prim_path) != top() ||
4562 region->in(_bad_type_path) != top() ||
4563 region->in(_npe_path) != top()) {
4564 // Let Interpreter throw ClassCastException.
4565 PreserveJVMState pjvms(this);
4566 set_control(_gvn.transform(region));
4567 // Set IO and memory because gen_checkcast may override them when buffering inline types
4568 set_i_o(io);
4569 set_all_memory(mem);
4570 uncommon_trap(Deoptimization::Reason_intrinsic,
4571 Deoptimization::Action_maybe_recompile);
4572 }
4573 if (!stopped()) {
4574 set_result(res);
4575 }
4576 return true;
4577 }
4578
4579
4580 //--------------------------inline_native_subtype_check------------------------
4581 // This intrinsic takes the JNI calls out of the heart of
4582 // UnsafeFieldAccessorImpl.set, which improves Field.set, readObject, etc.
4583 bool LibraryCallKit::inline_native_subtype_check() {
4584 // Pull both arguments off the stack.
4585 Node* args[2]; // two java.lang.Class mirrors: superc, subc
4586 args[0] = argument(0);
4587 args[1] = argument(1);
4588 Node* klasses[2]; // corresponding Klasses: superk, subk
4589 klasses[0] = klasses[1] = top();
4590
4591 enum {
4592 // A full decision tree on {superc is prim, subc is prim}:
4593 _prim_0_path = 1, // {P,N} => false
4594 // {P,P} & superc!=subc => false
4595 _prim_same_path, // {P,P} & superc==subc => true
4596 _prim_1_path, // {N,P} => false
4597 _ref_subtype_path, // {N,N} & subtype check wins => true
4598 _both_ref_path, // {N,N} & subtype check loses => false
4599 PATH_LIMIT
4600 };
4601
4602 RegionNode* region = new RegionNode(PATH_LIMIT);
4603 RegionNode* prim_region = new RegionNode(2);
4604 Node* phi = new PhiNode(region, TypeInt::BOOL);
4605 record_for_igvn(region);
4606 record_for_igvn(prim_region);
4607
4608 const TypePtr* adr_type = TypeRawPtr::BOTTOM; // memory type of loads
4609 const TypeKlassPtr* kls_type = TypeInstKlassPtr::OBJECT_OR_NULL;
4610 int class_klass_offset = java_lang_Class::klass_offset();
4611
4612 // First null-check both mirrors and load each mirror's klass metaobject.
4613 int which_arg;
4614 for (which_arg = 0; which_arg <= 1; which_arg++) {
4615 Node* arg = args[which_arg];
4616 arg = null_check(arg);
4617 if (stopped()) break;
4618 args[which_arg] = arg;
4619
4620 Node* p = basic_plus_adr(arg, class_klass_offset);
4621 Node* kls = LoadKlassNode::make(_gvn, immutable_memory(), p, adr_type, kls_type);
4622 klasses[which_arg] = _gvn.transform(kls);
4623 }
4624
4625 // Having loaded both klasses, test each for null.
4626 bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
4627 for (which_arg = 0; which_arg <= 1; which_arg++) {
4628 Node* kls = klasses[which_arg];
4629 Node* null_ctl = top();
4630 kls = null_check_oop(kls, &null_ctl, never_see_null);
4631 if (which_arg == 0) {
4632 prim_region->init_req(1, null_ctl);
4633 } else {
4634 region->init_req(_prim_1_path, null_ctl);
4635 }
4636 if (stopped()) break;
4637 klasses[which_arg] = kls;
4638 }
4639
4640 if (!stopped()) {
4641 // now we have two reference types, in klasses[0..1]
4642 Node* subk = klasses[1]; // the argument to isAssignableFrom
4643 Node* superk = klasses[0]; // the receiver
4644 region->set_req(_both_ref_path, gen_subtype_check(subk, superk));
4645 region->set_req(_ref_subtype_path, control());
4646 }
4647
4648 // If both operands are primitive (both klasses null), then
4649 // we must return true when they are identical primitives.
4650 // It is convenient to test this after the first null klass check.
4651 // This path is also used if superc is a value mirror.
4652 set_control(_gvn.transform(prim_region));
4653 if (!stopped()) {
4654 // Since superc is primitive, make a guard for the superc==subc case.
4655 Node* cmp_eq = _gvn.transform(new CmpPNode(args[0], args[1]));
4656 Node* bol_eq = _gvn.transform(new BoolNode(cmp_eq, BoolTest::eq));
4657 generate_fair_guard(bol_eq, region);
4658 if (region->req() == PATH_LIMIT+1) {
4659 // A guard was added. If the added guard is taken, superc==subc.
4660 region->swap_edges(PATH_LIMIT, _prim_same_path);
4661 region->del_req(PATH_LIMIT);
4662 }
4663 region->set_req(_prim_0_path, control()); // Not equal after all.
4664 }
4665
4666 // these are the only paths that produce 'true':
4667 phi->set_req(_prim_same_path, intcon(1));
4668 phi->set_req(_ref_subtype_path, intcon(1));
4669
4670 // pull together the cases:
4671 assert(region->req() == PATH_LIMIT, "sane region");
4672 for (uint i = 1; i < region->req(); i++) {
4673 Node* ctl = region->in(i);
4674 if (ctl == nullptr || ctl == top()) {
4675 region->set_req(i, top());
4676 phi ->set_req(i, top());
4677 } else if (phi->in(i) == nullptr) {
4678 phi->set_req(i, intcon(0)); // all other paths produce 'false'
4679 }
4680 }
4681
4682 set_control(_gvn.transform(region));
4683 set_result(_gvn.transform(phi));
4684 return true;
4685 }
4686
4687 //---------------------generate_array_guard_common------------------------
4688 Node* LibraryCallKit::generate_array_guard_common(Node* kls, RegionNode* region, ArrayKind kind, Node** obj) {
4689
4690 if (stopped()) {
4691 return nullptr;
4692 }
4693
4694 // Like generate_guard, adds a new path onto the region.
4695 jint layout_con = 0;
4696 Node* layout_val = get_layout_helper(kls, layout_con);
4697 if (layout_val == nullptr) {
4698 bool query = 0;
4699 switch(kind) {
4700 case RefArray: query = Klass::layout_helper_is_refArray(layout_con); break;
4701 case NonRefArray: query = !Klass::layout_helper_is_refArray(layout_con); break;
4702 case TypeArray: query = Klass::layout_helper_is_typeArray(layout_con); break;
4703 case AnyArray: query = Klass::layout_helper_is_array(layout_con); break;
4704 case NonArray: query = !Klass::layout_helper_is_array(layout_con); break;
4705 default:
4706 ShouldNotReachHere();
4707 }
4708 if (!query) {
4709 return nullptr; // never a branch
4710 } else { // always a branch
4711 Node* always_branch = control();
4712 if (region != nullptr)
4713 region->add_req(always_branch);
4714 set_control(top());
4715 return always_branch;
4716 }
4717 }
4718 unsigned int value = 0;
4719 BoolTest::mask btest = BoolTest::illegal;
4720 switch(kind) {
4721 case RefArray:
4722 case NonRefArray: {
4723 value = Klass::_lh_array_tag_ref_value;
4724 layout_val = _gvn.transform(new RShiftINode(layout_val, intcon(Klass::_lh_array_tag_shift)));
4725 btest = (kind == RefArray) ? BoolTest::eq : BoolTest::ne;
4726 break;
4727 }
4728 case TypeArray: {
4729 value = Klass::_lh_array_tag_type_value;
4730 layout_val = _gvn.transform(new RShiftINode(layout_val, intcon(Klass::_lh_array_tag_shift)));
4731 btest = BoolTest::eq;
4732 break;
4733 }
4734 case AnyArray: value = Klass::_lh_neutral_value; btest = BoolTest::lt; break;
4735 case NonArray: value = Klass::_lh_neutral_value; btest = BoolTest::gt; break;
4736 default:
4737 ShouldNotReachHere();
4738 }
4739 // Now test the correct condition.
4740 jint nval = (jint)value;
4741 Node* cmp = _gvn.transform(new CmpINode(layout_val, intcon(nval)));
4742 Node* bol = _gvn.transform(new BoolNode(cmp, btest));
4743 Node* ctrl = generate_fair_guard(bol, region);
4744 Node* is_array_ctrl = kind == NonArray ? control() : ctrl;
4745 if (obj != nullptr && is_array_ctrl != nullptr && is_array_ctrl != top()) {
4746 // Keep track of the fact that 'obj' is an array to prevent
4747 // array specific accesses from floating above the guard.
4748 *obj = _gvn.transform(new CastPPNode(is_array_ctrl, *obj, TypeAryPtr::BOTTOM));
4749 }
4750 return ctrl;
4751 }
4752
4753 // public static native Object[] newNullRestrictedAtomicArray(Class<?> componentType, int length, Object initVal);
4754 // public static native Object[] newNullRestrictedNonAtomicArray(Class<?> componentType, int length, Object initVal);
4755 // public static native Object[] newNullableAtomicArray(Class<?> componentType, int length);
4756 bool LibraryCallKit::inline_newArray(bool null_free, bool atomic) {
4757 assert(null_free || atomic, "nullable implies atomic");
4758 Node* componentType = argument(0);
4759 Node* length = argument(1);
4760 Node* init_val = null_free ? argument(2) : nullptr;
4761
4762 const TypeInstPtr* tp = _gvn.type(componentType)->isa_instptr();
4763 if (tp != nullptr) {
4764 ciInstanceKlass* ik = tp->instance_klass();
4765 if (ik == C->env()->Class_klass()) {
4766 ciType* t = tp->java_mirror_type();
4767 if (t != nullptr && t->is_inlinetype()) {
4768
4769 ciArrayKlass* array_klass = ciArrayKlass::make(t, null_free, atomic, true);
4770 assert(array_klass->is_elem_null_free() == null_free, "inconsistency");
4771 assert(array_klass->is_elem_atomic() == atomic, "inconsistency");
4772
4773 // TOOD 8350865 ZGC needs card marks on initializing oop stores
4774 if (UseZGC && null_free && !array_klass->is_flat_array_klass()) {
4775 return false;
4776 }
4777
4778 if (array_klass->is_loaded() && array_klass->element_klass()->as_inline_klass()->is_initialized()) {
4779 const TypeAryKlassPtr* array_klass_type = TypeAryKlassPtr::make(array_klass, Type::trust_interfaces, true);
4780 if (null_free) {
4781 if (init_val->is_InlineType()) {
4782 if (array_klass_type->is_flat() && init_val->as_InlineType()->is_all_zero(&gvn(), /* flat */ true)) {
4783 // Zeroing is enough because the init value is the all-zero value
4784 init_val = nullptr;
4785 } else {
4786 init_val = init_val->as_InlineType()->buffer(this);
4787 }
4788 }
4789 // TODO 8350865 Should we add a check of the init_val type (maybe in debug only + halt)?
4790 }
4791 Node* obj = new_array(makecon(array_klass_type), length, 0, nullptr, false, init_val);
4792 const TypeAryPtr* arytype = gvn().type(obj)->is_aryptr();
4793 assert(arytype->is_null_free() == null_free, "inconsistency");
4794 assert(arytype->is_not_null_free() == !null_free, "inconsistency");
4795 assert(arytype->is_atomic() == atomic, "inconsistency");
4796 set_result(obj);
4797 return true;
4798 }
4799 }
4800 }
4801 }
4802 return false;
4803 }
4804
4805 Node* LibraryCallKit::load_default_array_klass(Node* klass_node) {
4806 // TODO 8366668
4807 // - Fred suggested that we could just have the first entry in the refined list point to the array with ArrayKlass::ArrayProperties::DEFAULT property
4808 // For now, we just load from ObjArrayKlass::_next_refined_array_klass, which would always be the refKlass for non-values, and deopt if it's not
4809 // - Convert this to an IGVN optimization, so it's also folded after parsing
4810 // - The generate_typeArray_guard is not needed by all callers, double-check that it's folded
4811
4812 const Type* klass_t = _gvn.type(klass_node);
4813 const TypeAryKlassPtr* ary_klass_t = klass_t->isa_aryklassptr();
4814 if (ary_klass_t && ary_klass_t->klass_is_exact()) {
4815 if (ary_klass_t->exact_klass()->is_obj_array_klass()) {
4816 ary_klass_t = ary_klass_t->get_vm_type(false);
4817 return makecon(ary_klass_t);
4818 } else {
4819 return klass_node;
4820 }
4821 }
4822
4823 // Load next refined array klass if klass is an ObjArrayKlass
4824 RegionNode* refined_region = new RegionNode(2);
4825 Node* refined_phi = new PhiNode(refined_region, klass_t);
4826
4827 generate_typeArray_guard(klass_node, refined_region);
4828 if (refined_region->req() == 3) {
4829 refined_phi->add_req(klass_node);
4830 }
4831
4832 Node* adr_refined_klass = basic_plus_adr(klass_node, in_bytes(ObjArrayKlass::next_refined_array_klass_offset()));
4833 Node* refined_klass = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), adr_refined_klass, TypeRawPtr::BOTTOM, TypeInstKlassPtr::OBJECT_OR_NULL));
4834
4835 RegionNode* refined_region2 = new RegionNode(3);
4836 Node* refined_phi2 = new PhiNode(refined_region2, klass_t);
4837
4838 Node* null_ctl = top();
4839 Node* null_free_klass = null_check_common(refined_klass, T_OBJECT, false, &null_ctl);
4840 refined_region2->init_req(1, null_ctl);
4841 refined_phi2->init_req(1, klass_node);
4842
4843 refined_region2->init_req(2, control());
4844 refined_phi2->init_req(2, null_free_klass);
4845
4846 set_control(_gvn.transform(refined_region2));
4847 refined_klass = _gvn.transform(refined_phi2);
4848
4849 Node* adr_properties = basic_plus_adr(refined_klass, in_bytes(ObjArrayKlass::properties_offset()));
4850
4851 Node* properties = _gvn.transform(LoadNode::make(_gvn, control(), immutable_memory(), adr_properties, TypeRawPtr::BOTTOM, TypeInt::INT, T_INT, MemNode::unordered));
4852 Node* default_val = makecon(TypeInt::make(ArrayKlass::ArrayProperties::DEFAULT));
4853 Node* chk = _gvn.transform(new CmpINode(properties, default_val));
4854 Node* tst = _gvn.transform(new BoolNode(chk, BoolTest::eq));
4855
4856 { // Deoptimize if not the default property
4857 BuildCutout unless(this, tst, PROB_MAX);
4858 uncommon_trap_exact(Deoptimization::Reason_class_check, Deoptimization::Action_none);
4859 }
4860
4861 refined_region->init_req(1, control());
4862 refined_phi->init_req(1, refined_klass);
4863
4864 set_control(_gvn.transform(refined_region));
4865 klass_node = _gvn.transform(refined_phi);
4866
4867 return klass_node;
4868 }
4869
4870 //-----------------------inline_native_newArray--------------------------
4871 // private static native Object java.lang.reflect.Array.newArray(Class<?> componentType, int length);
4872 // private native Object Unsafe.allocateUninitializedArray0(Class<?> cls, int size);
4873 bool LibraryCallKit::inline_unsafe_newArray(bool uninitialized) {
4874 Node* mirror;
4875 Node* count_val;
4876 if (uninitialized) {
4877 null_check_receiver();
4878 mirror = argument(1);
4879 count_val = argument(2);
4880 } else {
4881 mirror = argument(0);
4882 count_val = argument(1);
4883 }
4884
4885 mirror = null_check(mirror);
4886 // If mirror or obj is dead, only null-path is taken.
4887 if (stopped()) return true;
4888
4889 enum { _normal_path = 1, _slow_path = 2, PATH_LIMIT };
4890 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
4891 PhiNode* result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
4909 CallJavaNode* slow_call = nullptr;
4910 if (uninitialized) {
4911 // Generate optimized virtual call (holder class 'Unsafe' is final)
4912 slow_call = generate_method_call(vmIntrinsics::_allocateUninitializedArray, false, false, true);
4913 } else {
4914 slow_call = generate_method_call_static(vmIntrinsics::_newArray, true);
4915 }
4916 Node* slow_result = set_results_for_java_call(slow_call);
4917 // this->control() comes from set_results_for_java_call
4918 result_reg->set_req(_slow_path, control());
4919 result_val->set_req(_slow_path, slow_result);
4920 result_io ->set_req(_slow_path, i_o());
4921 result_mem->set_req(_slow_path, reset_memory());
4922 }
4923
4924 set_control(normal_ctl);
4925 if (!stopped()) {
4926 // Normal case: The array type has been cached in the java.lang.Class.
4927 // The following call works fine even if the array type is polymorphic.
4928 // It could be a dynamic mix of int[], boolean[], Object[], etc.
4929
4930 klass_node = load_default_array_klass(klass_node);
4931
4932 Node* obj = new_array(klass_node, count_val, 0); // no arguments to push
4933 result_reg->init_req(_normal_path, control());
4934 result_val->init_req(_normal_path, obj);
4935 result_io ->init_req(_normal_path, i_o());
4936 result_mem->init_req(_normal_path, reset_memory());
4937
4938 if (uninitialized) {
4939 // Mark the allocation so that zeroing is skipped
4940 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(obj);
4941 alloc->maybe_set_complete(&_gvn);
4942 }
4943 }
4944
4945 // Return the combined state.
4946 set_i_o( _gvn.transform(result_io) );
4947 set_all_memory( _gvn.transform(result_mem));
4948
4949 C->set_has_split_ifs(true); // Has chance for split-if optimization
4950 set_result(result_reg, result_val);
4951 return true;
5000 // the bytecode that invokes Arrays.copyOf if deoptimization happens.
5001 { PreserveReexecuteState preexecs(this);
5002 jvms()->set_should_reexecute(true);
5003
5004 array_type_mirror = null_check(array_type_mirror);
5005 original = null_check(original);
5006
5007 // Check if a null path was taken unconditionally.
5008 if (stopped()) return true;
5009
5010 Node* orig_length = load_array_length(original);
5011
5012 Node* klass_node = load_klass_from_mirror(array_type_mirror, false, nullptr, 0);
5013 klass_node = null_check(klass_node);
5014
5015 RegionNode* bailout = new RegionNode(1);
5016 record_for_igvn(bailout);
5017
5018 // Despite the generic type of Arrays.copyOf, the mirror might be int, int[], etc.
5019 // Bail out if that is so.
5020 // Inline type array may have object field that would require a
5021 // write barrier. Conservatively, go to slow path.
5022 // TODO 8251971: Optimize for the case when flat src/dst are later found
5023 // to not contain oops (i.e., move this check to the macro expansion phase).
5024 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
5025 const TypeAryPtr* orig_t = _gvn.type(original)->isa_aryptr();
5026 const TypeKlassPtr* tklass = _gvn.type(klass_node)->is_klassptr();
5027 bool exclude_flat = UseArrayFlattening && bs->array_copy_requires_gc_barriers(true, T_OBJECT, false, false, BarrierSetC2::Parsing) &&
5028 // Can src array be flat and contain oops?
5029 (orig_t == nullptr || (!orig_t->is_not_flat() && (!orig_t->is_flat() || orig_t->elem()->inline_klass()->contains_oops()))) &&
5030 // Can dest array be flat and contain oops?
5031 tklass->can_be_inline_array() && (!tklass->is_flat() || tklass->is_aryklassptr()->elem()->is_instklassptr()->instance_klass()->as_inline_klass()->contains_oops());
5032 // TODO 8366668 generate_non_refArray_guard also passed for ref arrays??
5033 Node* not_objArray = exclude_flat ? generate_non_refArray_guard(klass_node, bailout) : generate_typeArray_guard(klass_node, bailout);
5034
5035 klass_node = load_default_array_klass(klass_node);
5036
5037 if (not_objArray != nullptr) {
5038 // Improve the klass node's type from the new optimistic assumption:
5039 ciKlass* ak = ciArrayKlass::make(env()->Object_klass());
5040 const Type* akls = TypeKlassPtr::make(TypePtr::NotNull, ak, Type::Offset(0));
5041 Node* cast = new CastPPNode(control(), klass_node, akls);
5042 klass_node = _gvn.transform(cast);
5043 }
5044
5045 // Bail out if either start or end is negative.
5046 generate_negative_guard(start, bailout, &start);
5047 generate_negative_guard(end, bailout, &end);
5048
5049 Node* length = end;
5050 if (_gvn.type(start) != TypeInt::ZERO) {
5051 length = _gvn.transform(new SubINode(end, start));
5052 }
5053
5054 // Bail out if length is negative (i.e., if start > end).
5055 // Without this the new_array would throw
5056 // NegativeArraySizeException but IllegalArgumentException is what
5057 // should be thrown
5058 generate_negative_guard(length, bailout, &length);
5059
5060 // Handle inline type arrays
5061 bool can_validate = !too_many_traps(Deoptimization::Reason_class_check);
5062 if (!stopped()) {
5063 // TODO JDK-8329224
5064 if (!orig_t->is_null_free()) {
5065 // Not statically known to be null free, add a check
5066 generate_fair_guard(null_free_array_test(original), bailout);
5067 }
5068 orig_t = _gvn.type(original)->isa_aryptr();
5069 if (orig_t != nullptr && orig_t->is_flat()) {
5070 // Src is flat, check that dest is flat as well
5071 if (exclude_flat) {
5072 // Dest can't be flat, bail out
5073 bailout->add_req(control());
5074 set_control(top());
5075 } else {
5076 generate_fair_guard(flat_array_test(klass_node, /* flat = */ false), bailout);
5077 }
5078 // TODO 8350865 This is not correct anymore. Write tests and fix logic similar to arraycopy.
5079 } else if (UseArrayFlattening && (orig_t == nullptr || !orig_t->is_not_flat()) &&
5080 // If dest is flat, src must be flat as well (guaranteed by src <: dest check if validated).
5081 ((!tklass->is_flat() && tklass->can_be_inline_array()) || !can_validate)) {
5082 // Src might be flat and dest might not be flat. Go to the slow path if src is flat.
5083 // TODO 8251971: Optimize for the case when src/dest are later found to be both flat.
5084 generate_fair_guard(flat_array_test(load_object_klass(original)), bailout);
5085 if (orig_t != nullptr) {
5086 orig_t = orig_t->cast_to_not_flat();
5087 original = _gvn.transform(new CheckCastPPNode(control(), original, orig_t));
5088 }
5089 }
5090 if (!can_validate) {
5091 // No validation. The subtype check emitted at macro expansion time will not go to the slow
5092 // path but call checkcast_arraycopy which can not handle flat/null-free inline type arrays.
5093 // TODO 8251971: Optimize for the case when src/dest are later found to be both flat/null-free.
5094 generate_fair_guard(flat_array_test(klass_node), bailout);
5095 generate_fair_guard(null_free_array_test(original), bailout);
5096 }
5097 }
5098
5099 // Bail out if start is larger than the original length
5100 Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
5101 generate_negative_guard(orig_tail, bailout, &orig_tail);
5102
5103 if (bailout->req() > 1) {
5104 PreserveJVMState pjvms(this);
5105 set_control(_gvn.transform(bailout));
5106 uncommon_trap(Deoptimization::Reason_intrinsic,
5107 Deoptimization::Action_maybe_recompile);
5108 }
5109
5110 if (!stopped()) {
5111 // How many elements will we copy from the original?
5112 // The answer is MinI(orig_tail, length).
5113 Node* moved = _gvn.transform(new MinINode(orig_tail, length));
5114
5115 // Generate a direct call to the right arraycopy function(s).
5116 // We know the copy is disjoint but we might not know if the
5117 // oop stores need checking.
5118 // Extreme case: Arrays.copyOf((Integer[])x, 10, String[].class).
5124 // to the copyOf to be validated, including that the copy to the
5125 // new array won't trigger an ArrayStoreException. That subtype
5126 // check can be optimized if we know something on the type of
5127 // the input array from type speculation.
5128 if (_gvn.type(klass_node)->singleton()) {
5129 const TypeKlassPtr* subk = _gvn.type(load_object_klass(original))->is_klassptr();
5130 const TypeKlassPtr* superk = _gvn.type(klass_node)->is_klassptr();
5131
5132 int test = C->static_subtype_check(superk, subk);
5133 if (test != Compile::SSC_always_true && test != Compile::SSC_always_false) {
5134 const TypeOopPtr* t_original = _gvn.type(original)->is_oopptr();
5135 if (t_original->speculative_type() != nullptr) {
5136 original = maybe_cast_profiled_obj(original, t_original->speculative_type(), true);
5137 }
5138 }
5139 }
5140
5141 bool validated = false;
5142 // Reason_class_check rather than Reason_intrinsic because we
5143 // want to intrinsify even if this traps.
5144 if (can_validate) {
5145 Node* not_subtype_ctrl = gen_subtype_check(original, klass_node);
5146
5147 if (not_subtype_ctrl != top()) {
5148 PreserveJVMState pjvms(this);
5149 set_control(not_subtype_ctrl);
5150 uncommon_trap(Deoptimization::Reason_class_check,
5151 Deoptimization::Action_make_not_entrant);
5152 assert(stopped(), "Should be stopped");
5153 }
5154 validated = true;
5155 }
5156
5157 if (!stopped()) {
5158 newcopy = new_array(klass_node, length, 0); // no arguments to push
5159
5160 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, true, true,
5161 load_object_klass(original), klass_node);
5162 if (!is_copyOfRange) {
5163 ac->set_copyof(validated);
5164 } else {
5210
5211 //-----------------------generate_method_call----------------------------
5212 // Use generate_method_call to make a slow-call to the real
5213 // method if the fast path fails. An alternative would be to
5214 // use a stub like OptoRuntime::slow_arraycopy_Java.
5215 // This only works for expanding the current library call,
5216 // not another intrinsic. (E.g., don't use this for making an
5217 // arraycopy call inside of the copyOf intrinsic.)
5218 CallJavaNode*
5219 LibraryCallKit::generate_method_call(vmIntrinsicID method_id, bool is_virtual, bool is_static, bool res_not_null) {
5220 // When compiling the intrinsic method itself, do not use this technique.
5221 guarantee(callee() != C->method(), "cannot make slow-call to self");
5222
5223 ciMethod* method = callee();
5224 // ensure the JVMS we have will be correct for this call
5225 guarantee(method_id == method->intrinsic_id(), "must match");
5226
5227 const TypeFunc* tf = TypeFunc::make(method);
5228 if (res_not_null) {
5229 assert(tf->return_type() == T_OBJECT, "");
5230 const TypeTuple* range = tf->range_cc();
5231 const Type** fields = TypeTuple::fields(range->cnt());
5232 fields[TypeFunc::Parms] = range->field_at(TypeFunc::Parms)->filter_speculative(TypePtr::NOTNULL);
5233 const TypeTuple* new_range = TypeTuple::make(range->cnt(), fields);
5234 tf = TypeFunc::make(tf->domain_cc(), new_range);
5235 }
5236 CallJavaNode* slow_call;
5237 if (is_static) {
5238 assert(!is_virtual, "");
5239 slow_call = new CallStaticJavaNode(C, tf,
5240 SharedRuntime::get_resolve_static_call_stub(), method);
5241 } else if (is_virtual) {
5242 assert(!gvn().type(argument(0))->maybe_null(), "should not be null");
5243 int vtable_index = Method::invalid_vtable_index;
5244 if (UseInlineCaches) {
5245 // Suppress the vtable call
5246 } else {
5247 // hashCode and clone are not a miranda methods,
5248 // so the vtable index is fixed.
5249 // No need to use the linkResolver to get it.
5250 vtable_index = method->vtable_index();
5251 assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
5252 "bad index %d", vtable_index);
5253 }
5254 slow_call = new CallDynamicJavaNode(tf,
5271 set_edges_for_java_call(slow_call);
5272 return slow_call;
5273 }
5274
5275
5276 /**
5277 * Build special case code for calls to hashCode on an object. This call may
5278 * be virtual (invokevirtual) or bound (invokespecial). For each case we generate
5279 * slightly different code.
5280 */
5281 bool LibraryCallKit::inline_native_hashcode(bool is_virtual, bool is_static) {
5282 assert(is_static == callee()->is_static(), "correct intrinsic selection");
5283 assert(!(is_virtual && is_static), "either virtual, special, or static");
5284
5285 enum { _slow_path = 1, _fast_path, _null_path, PATH_LIMIT };
5286
5287 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
5288 PhiNode* result_val = new PhiNode(result_reg, TypeInt::INT);
5289 PhiNode* result_io = new PhiNode(result_reg, Type::ABIO);
5290 PhiNode* result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
5291 Node* obj = argument(0);
5292
5293 // Don't intrinsify hashcode on inline types for now.
5294 // The "is locked" runtime check below also serves as inline type check and goes to the slow path.
5295 if (gvn().type(obj)->is_inlinetypeptr()) {
5296 return false;
5297 }
5298
5299 if (!is_static) {
5300 // Check for hashing null object
5301 obj = null_check_receiver();
5302 if (stopped()) return true; // unconditionally null
5303 result_reg->init_req(_null_path, top());
5304 result_val->init_req(_null_path, top());
5305 } else {
5306 // Do a null check, and return zero if null.
5307 // System.identityHashCode(null) == 0
5308 Node* null_ctl = top();
5309 obj = null_check_oop(obj, &null_ctl);
5310 result_reg->init_req(_null_path, null_ctl);
5311 result_val->init_req(_null_path, _gvn.intcon(0));
5312 }
5313
5314 // Unconditionally null? Then return right away.
5315 if (stopped()) {
5316 set_control( result_reg->in(_null_path));
5317 if (!stopped())
5318 set_result(result_val->in(_null_path));
5319 return true;
5320 }
5321
5322 // We only go to the fast case code if we pass a number of guards. The
5323 // paths which do not pass are accumulated in the slow_region.
5324 RegionNode* slow_region = new RegionNode(1);
5325 record_for_igvn(slow_region);
5326
5327 // If this is a virtual call, we generate a funny guard. We pull out
5328 // the vtable entry corresponding to hashCode() from the target object.
5329 // If the target method which we are calling happens to be the native
5330 // Object hashCode() method, we pass the guard. We do not need this
5331 // guard for non-virtual calls -- the caller is known to be the native
5332 // Object hashCode().
5333 if (is_virtual) {
5334 // After null check, get the object's klass.
5335 Node* obj_klass = load_object_klass(obj);
5336 generate_virtual_guard(obj_klass, slow_region);
5337 }
5338
5339 // Get the header out of the object, use LoadMarkNode when available
5340 Node* header_addr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes());
5341 // The control of the load must be null. Otherwise, the load can move before
5342 // the null check after castPP removal.
5343 Node* no_ctrl = nullptr;
5344 Node* header = make_load(no_ctrl, header_addr, TypeX_X, TypeX_X->basic_type(), MemNode::unordered);
5345
5346 if (!UseObjectMonitorTable) {
5347 // Test the header to see if it is safe to read w.r.t. locking.
5348 // This also serves as guard against inline types
5349 Node *lock_mask = _gvn.MakeConX(markWord::inline_type_mask_in_place);
5350 Node *lmasked_header = _gvn.transform(new AndXNode(header, lock_mask));
5351 if (LockingMode == LM_LIGHTWEIGHT) {
5352 Node *monitor_val = _gvn.MakeConX(markWord::monitor_value);
5353 Node *chk_monitor = _gvn.transform(new CmpXNode(lmasked_header, monitor_val));
5354 Node *test_monitor = _gvn.transform(new BoolNode(chk_monitor, BoolTest::eq));
5355
5356 generate_slow_guard(test_monitor, slow_region);
5357 } else {
5358 Node *unlocked_val = _gvn.MakeConX(markWord::unlocked_value);
5359 Node *chk_unlocked = _gvn.transform(new CmpXNode(lmasked_header, unlocked_val));
5360 Node *test_not_unlocked = _gvn.transform(new BoolNode(chk_unlocked, BoolTest::ne));
5361
5362 generate_slow_guard(test_not_unlocked, slow_region);
5363 }
5364 }
5365
5366 // Get the hash value and check to see that it has been properly assigned.
5367 // We depend on hash_mask being at most 32 bits and avoid the use of
5368 // hash_mask_in_place because it could be larger than 32 bits in a 64-bit
5369 // vm: see markWord.hpp.
5404 // this->control() comes from set_results_for_java_call
5405 result_reg->init_req(_slow_path, control());
5406 result_val->init_req(_slow_path, slow_result);
5407 result_io ->set_req(_slow_path, i_o());
5408 result_mem ->set_req(_slow_path, reset_memory());
5409 }
5410
5411 // Return the combined state.
5412 set_i_o( _gvn.transform(result_io) );
5413 set_all_memory( _gvn.transform(result_mem));
5414
5415 set_result(result_reg, result_val);
5416 return true;
5417 }
5418
5419 //---------------------------inline_native_getClass----------------------------
5420 // public final native Class<?> java.lang.Object.getClass();
5421 //
5422 // Build special case code for calls to getClass on an object.
5423 bool LibraryCallKit::inline_native_getClass() {
5424 Node* obj = argument(0);
5425 if (obj->is_InlineType()) {
5426 const Type* t = _gvn.type(obj);
5427 if (t->maybe_null()) {
5428 null_check(obj);
5429 }
5430 set_result(makecon(TypeInstPtr::make(t->inline_klass()->java_mirror())));
5431 return true;
5432 }
5433 obj = null_check_receiver();
5434 if (stopped()) return true;
5435 set_result(load_mirror_from_klass(load_object_klass(obj)));
5436 return true;
5437 }
5438
5439 //-----------------inline_native_Reflection_getCallerClass---------------------
5440 // public static native Class<?> sun.reflect.Reflection.getCallerClass();
5441 //
5442 // In the presence of deep enough inlining, getCallerClass() becomes a no-op.
5443 //
5444 // NOTE: This code must perform the same logic as JVM_GetCallerClass
5445 // in that it must skip particular security frames and checks for
5446 // caller sensitive methods.
5447 bool LibraryCallKit::inline_native_Reflection_getCallerClass() {
5448 #ifndef PRODUCT
5449 if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
5450 tty->print_cr("Attempting to inline sun.reflect.Reflection.getCallerClass");
5451 }
5452 #endif
5453
5835 // not cloneable or finalizer => slow path to out-of-line Object.clone
5836 //
5837 // The general case has two steps, allocation and copying.
5838 // Allocation has two cases, and uses GraphKit::new_instance or new_array.
5839 //
5840 // Copying also has two cases, oop arrays and everything else.
5841 // Oop arrays use arrayof_oop_arraycopy (same as System.arraycopy).
5842 // Everything else uses the tight inline loop supplied by CopyArrayNode.
5843 //
5844 // These steps fold up nicely if and when the cloned object's klass
5845 // can be sharply typed as an object array, a type array, or an instance.
5846 //
5847 bool LibraryCallKit::inline_native_clone(bool is_virtual) {
5848 PhiNode* result_val;
5849
5850 // Set the reexecute bit for the interpreter to reexecute
5851 // the bytecode that invokes Object.clone if deoptimization happens.
5852 { PreserveReexecuteState preexecs(this);
5853 jvms()->set_should_reexecute(true);
5854
5855 Node* obj = argument(0);
5856 obj = null_check_receiver();
5857 if (stopped()) return true;
5858
5859 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
5860 if (obj_type->is_inlinetypeptr()) {
5861 // If the object to clone is an inline type, we can simply return it (i.e. a nop) since inline types have
5862 // no identity.
5863 set_result(obj);
5864 return true;
5865 }
5866
5867 // If we are going to clone an instance, we need its exact type to
5868 // know the number and types of fields to convert the clone to
5869 // loads/stores. Maybe a speculative type can help us.
5870 if (!obj_type->klass_is_exact() &&
5871 obj_type->speculative_type() != nullptr &&
5872 obj_type->speculative_type()->is_instance_klass() &&
5873 !obj_type->speculative_type()->is_inlinetype()) {
5874 ciInstanceKlass* spec_ik = obj_type->speculative_type()->as_instance_klass();
5875 if (spec_ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem &&
5876 !spec_ik->has_injected_fields()) {
5877 if (!obj_type->isa_instptr() ||
5878 obj_type->is_instptr()->instance_klass()->has_subklass()) {
5879 obj = maybe_cast_profiled_obj(obj, obj_type->speculative_type(), false);
5880 }
5881 }
5882 }
5883
5884 // Conservatively insert a memory barrier on all memory slices.
5885 // Do not let writes into the original float below the clone.
5886 insert_mem_bar(Op_MemBarCPUOrder);
5887
5888 // paths into result_reg:
5889 enum {
5890 _slow_path = 1, // out-of-line call to clone method (virtual or not)
5891 _objArray_path, // plain array allocation, plus arrayof_oop_arraycopy
5892 _array_path, // plain array allocation, plus arrayof_long_arraycopy
5893 _instance_path, // plain instance allocation, plus arrayof_long_arraycopy
5894 PATH_LIMIT
5895 };
5896 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
5897 result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
5898 PhiNode* result_i_o = new PhiNode(result_reg, Type::ABIO);
5899 PhiNode* result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
5900 record_for_igvn(result_reg);
5901
5902 Node* obj_klass = load_object_klass(obj);
5903 // We only go to the fast case code if we pass a number of guards.
5904 // The paths which do not pass are accumulated in the slow_region.
5905 RegionNode* slow_region = new RegionNode(1);
5906 record_for_igvn(slow_region);
5907
5908 Node* array_obj = obj;
5909 Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)nullptr, &array_obj);
5910 if (array_ctl != nullptr) {
5911 // It's an array.
5912 PreserveJVMState pjvms(this);
5913 set_control(array_ctl);
5914
5915 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
5916 const TypeAryPtr* ary_ptr = obj_type->isa_aryptr();
5917 if (UseArrayFlattening && bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, false, BarrierSetC2::Expansion) &&
5918 obj_type->can_be_inline_array() &&
5919 (ary_ptr == nullptr || (!ary_ptr->is_not_flat() && (!ary_ptr->is_flat() || ary_ptr->elem()->inline_klass()->contains_oops())))) {
5920 // Flat inline type array may have object field that would require a
5921 // write barrier. Conservatively, go to slow path.
5922 generate_fair_guard(flat_array_test(obj_klass), slow_region);
5923 }
5924
5925 if (!stopped()) {
5926 Node* obj_length = load_array_length(array_obj);
5927 Node* array_size = nullptr; // Size of the array without object alignment padding.
5928 Node* alloc_obj = new_array(obj_klass, obj_length, 0, &array_size, /*deoptimize_on_exception=*/true);
5929
5930 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
5931 if (bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, false, BarrierSetC2::Parsing)) {
5932 // If it is an oop array, it requires very special treatment,
5933 // because gc barriers are required when accessing the array.
5934 Node* is_obja = generate_refArray_guard(obj_klass, (RegionNode*)nullptr);
5935 if (is_obja != nullptr) {
5936 PreserveJVMState pjvms2(this);
5937 set_control(is_obja);
5938 // Generate a direct call to the right arraycopy function(s).
5939 // Clones are always tightly coupled.
5940 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, array_obj, intcon(0), alloc_obj, intcon(0), obj_length, true, false);
5941 ac->set_clone_oop_array();
5942 Node* n = _gvn.transform(ac);
5943 assert(n == ac, "cannot disappear");
5944 ac->connect_outputs(this, /*deoptimize_on_exception=*/true);
5945
5946 result_reg->init_req(_objArray_path, control());
5947 result_val->init_req(_objArray_path, alloc_obj);
5948 result_i_o ->set_req(_objArray_path, i_o());
5949 result_mem ->set_req(_objArray_path, reset_memory());
5950 }
5951 }
5952 // Otherwise, there are no barriers to worry about.
5953 // (We can dispense with card marks if we know the allocation
5954 // comes out of eden (TLAB)... In fact, ReduceInitialCardMarks
5955 // causes the non-eden paths to take compensating steps to
5956 // simulate a fresh allocation, so that no further
5957 // card marks are required in compiled code to initialize
5958 // the object.)
5959
5960 if (!stopped()) {
5961 copy_to_clone(obj, alloc_obj, array_size, true);
5962
5963 // Present the results of the copy.
5964 result_reg->init_req(_array_path, control());
5965 result_val->init_req(_array_path, alloc_obj);
5966 result_i_o ->set_req(_array_path, i_o());
5967 result_mem ->set_req(_array_path, reset_memory());
5968 }
5969 }
5970 }
5971
5972 if (!stopped()) {
5973 // It's an instance (we did array above). Make the slow-path tests.
5974 // If this is a virtual call, we generate a funny guard. We grab
5975 // the vtable entry corresponding to clone() from the target object.
5976 // If the target method which we are calling happens to be the
5977 // Object clone() method, we pass the guard. We do not need this
5978 // guard for non-virtual calls; the caller is known to be the native
5979 // Object clone().
5980 if (is_virtual) {
5981 generate_virtual_guard(obj_klass, slow_region);
5982 }
5983
5984 // The object must be easily cloneable and must not have a finalizer.
5985 // Both of these conditions may be checked in a single test.
5986 // We could optimize the test further, but we don't care.
5987 generate_misc_flags_guard(obj_klass,
5988 // Test both conditions:
5989 KlassFlags::_misc_is_cloneable_fast | KlassFlags::_misc_has_finalizer,
5990 // Must be cloneable but not finalizer:
5991 KlassFlags::_misc_is_cloneable_fast,
6083 set_jvms(sfpt->jvms());
6084 _reexecute_sp = jvms()->sp();
6085
6086 return saved_jvms;
6087 }
6088 }
6089 }
6090 return nullptr;
6091 }
6092
6093 // Clone the JVMState of the array allocation and create a new safepoint with it. Re-push the array length to the stack
6094 // such that uncommon traps can be emitted to re-execute the array allocation in the interpreter.
6095 SafePointNode* LibraryCallKit::create_safepoint_with_state_before_array_allocation(const AllocateArrayNode* alloc) const {
6096 JVMState* old_jvms = alloc->jvms()->clone_shallow(C);
6097 uint size = alloc->req();
6098 SafePointNode* sfpt = new SafePointNode(size, old_jvms);
6099 old_jvms->set_map(sfpt);
6100 for (uint i = 0; i < size; i++) {
6101 sfpt->init_req(i, alloc->in(i));
6102 }
6103 int adjustment = 1;
6104 const TypeAryKlassPtr* ary_klass_ptr = alloc->in(AllocateNode::KlassNode)->bottom_type()->is_aryklassptr();
6105 if (ary_klass_ptr->is_null_free()) {
6106 // A null-free, tightly coupled array allocation can only come from LibraryCallKit::inline_newArray which
6107 // also requires the componentType and initVal on stack for re-execution.
6108 // Re-create and push the componentType.
6109 ciArrayKlass* klass = ary_klass_ptr->exact_klass()->as_array_klass();
6110 ciInstance* instance = klass->component_mirror_instance();
6111 const TypeInstPtr* t_instance = TypeInstPtr::make(instance);
6112 sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp(), makecon(t_instance));
6113 adjustment++;
6114 }
6115 // re-push array length for deoptimization
6116 sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp() + adjustment - 1, alloc->in(AllocateNode::ALength));
6117 if (ary_klass_ptr->is_null_free()) {
6118 // Re-create and push the initVal.
6119 Node* init_val = alloc->in(AllocateNode::InitValue);
6120 if (init_val == nullptr) {
6121 init_val = InlineTypeNode::make_all_zero(_gvn, ary_klass_ptr->elem()->is_instklassptr()->instance_klass()->as_inline_klass());
6122 } else if (UseCompressedOops) {
6123 init_val = _gvn.transform(new DecodeNNode(init_val, init_val->bottom_type()->make_ptr()));
6124 }
6125 sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp() + adjustment, init_val);
6126 adjustment++;
6127 }
6128 old_jvms->set_sp(old_jvms->sp() + adjustment);
6129 old_jvms->set_monoff(old_jvms->monoff() + adjustment);
6130 old_jvms->set_scloff(old_jvms->scloff() + adjustment);
6131 old_jvms->set_endoff(old_jvms->endoff() + adjustment);
6132 old_jvms->set_should_reexecute(true);
6133
6134 sfpt->set_i_o(map()->i_o());
6135 sfpt->set_memory(map()->memory());
6136 sfpt->set_control(map()->control());
6137 return sfpt;
6138 }
6139
6140 // In case of a deoptimization, we restart execution at the
6141 // allocation, allocating a new array. We would leave an uninitialized
6142 // array in the heap that GCs wouldn't expect. Move the allocation
6143 // after the traps so we don't allocate the array if we
6144 // deoptimize. This is possible because tightly_coupled_allocation()
6145 // guarantees there's no observer of the allocated array at this point
6146 // and the control flow is simple enough.
6147 void LibraryCallKit::arraycopy_move_allocation_here(AllocateArrayNode* alloc, Node* dest, JVMState* saved_jvms_before_guards,
6148 int saved_reexecute_sp, uint new_idx) {
6149 if (saved_jvms_before_guards != nullptr && !stopped()) {
6150 replace_unrelated_uncommon_traps_with_alloc_state(alloc, saved_jvms_before_guards);
6151
6152 assert(alloc != nullptr, "only with a tightly coupled allocation");
6153 // restore JVM state to the state at the arraycopy
6154 saved_jvms_before_guards->map()->set_control(map()->control());
6155 assert(saved_jvms_before_guards->map()->memory() == map()->memory(), "memory state changed?");
6156 assert(saved_jvms_before_guards->map()->i_o() == map()->i_o(), "IO state changed?");
6157 // If we've improved the types of some nodes (null check) while
6158 // emitting the guards, propagate them to the current state
6159 map()->replaced_nodes().apply(saved_jvms_before_guards->map(), new_idx);
6160 set_jvms(saved_jvms_before_guards);
6161 _reexecute_sp = saved_reexecute_sp;
6162
6163 // Remove the allocation from above the guards
6164 CallProjections* callprojs = alloc->extract_projections(true);
6165 InitializeNode* init = alloc->initialization();
6166 Node* alloc_mem = alloc->in(TypeFunc::Memory);
6167 C->gvn_replace_by(callprojs->fallthrough_ioproj, alloc->in(TypeFunc::I_O));
6168 C->gvn_replace_by(init->proj_out(TypeFunc::Memory), alloc_mem);
6169
6170 // The CastIINode created in GraphKit::new_array (in AllocateArrayNode::make_ideal_length) must stay below
6171 // the allocation (i.e. is only valid if the allocation succeeds):
6172 // 1) replace CastIINode with AllocateArrayNode's length here
6173 // 2) Create CastIINode again once allocation has moved (see below) at the end of this method
6174 //
6175 // Multiple identical CastIINodes might exist here. Each GraphKit::load_array_length() call will generate
6176 // new separate CastIINode (arraycopy guard checks or any array length use between array allocation and ararycopy)
6177 Node* init_control = init->proj_out(TypeFunc::Control);
6178 Node* alloc_length = alloc->Ideal_length();
6179 #ifdef ASSERT
6180 Node* prev_cast = nullptr;
6181 #endif
6182 for (uint i = 0; i < init_control->outcnt(); i++) {
6183 Node* init_out = init_control->raw_out(i);
6184 if (init_out->is_CastII() && init_out->in(TypeFunc::Control) == init_control && init_out->in(1) == alloc_length) {
6185 #ifdef ASSERT
6186 if (prev_cast == nullptr) {
6187 prev_cast = init_out;
6189 if (prev_cast->cmp(*init_out) == false) {
6190 prev_cast->dump();
6191 init_out->dump();
6192 assert(false, "not equal CastIINode");
6193 }
6194 }
6195 #endif
6196 C->gvn_replace_by(init_out, alloc_length);
6197 }
6198 }
6199 C->gvn_replace_by(init->proj_out(TypeFunc::Control), alloc->in(0));
6200
6201 // move the allocation here (after the guards)
6202 _gvn.hash_delete(alloc);
6203 alloc->set_req(TypeFunc::Control, control());
6204 alloc->set_req(TypeFunc::I_O, i_o());
6205 Node *mem = reset_memory();
6206 set_all_memory(mem);
6207 alloc->set_req(TypeFunc::Memory, mem);
6208 set_control(init->proj_out_or_null(TypeFunc::Control));
6209 set_i_o(callprojs->fallthrough_ioproj);
6210
6211 // Update memory as done in GraphKit::set_output_for_allocation()
6212 const TypeInt* length_type = _gvn.find_int_type(alloc->in(AllocateNode::ALength));
6213 const TypeOopPtr* ary_type = _gvn.type(alloc->in(AllocateNode::KlassNode))->is_klassptr()->as_instance_type();
6214 if (ary_type->isa_aryptr() && length_type != nullptr) {
6215 ary_type = ary_type->is_aryptr()->cast_to_size(length_type);
6216 }
6217 const TypePtr* telemref = ary_type->add_offset(Type::OffsetBot);
6218 int elemidx = C->get_alias_index(telemref);
6219 set_memory(init->proj_out_or_null(TypeFunc::Memory), Compile::AliasIdxRaw);
6220 set_memory(init->proj_out_or_null(TypeFunc::Memory), elemidx);
6221
6222 Node* allocx = _gvn.transform(alloc);
6223 assert(allocx == alloc, "where has the allocation gone?");
6224 assert(dest->is_CheckCastPP(), "not an allocation result?");
6225
6226 _gvn.hash_delete(dest);
6227 dest->set_req(0, control());
6228 Node* destx = _gvn.transform(dest);
6229 assert(destx == dest, "where has the allocation result gone?");
6527 top_src = src_type->isa_aryptr();
6528 has_src = (top_src != nullptr && top_src->elem() != Type::BOTTOM);
6529 src_spec = true;
6530 }
6531 if (!has_dest) {
6532 dest = maybe_cast_profiled_obj(dest, dest_k, true);
6533 dest_type = _gvn.type(dest);
6534 top_dest = dest_type->isa_aryptr();
6535 has_dest = (top_dest != nullptr && top_dest->elem() != Type::BOTTOM);
6536 dest_spec = true;
6537 }
6538 }
6539 }
6540
6541 if (has_src && has_dest && can_emit_guards) {
6542 BasicType src_elem = top_src->isa_aryptr()->elem()->array_element_basic_type();
6543 BasicType dest_elem = top_dest->isa_aryptr()->elem()->array_element_basic_type();
6544 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
6545 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
6546
6547 if (src_elem == dest_elem && top_src->is_flat() == top_dest->is_flat() && src_elem == T_OBJECT) {
6548 // If both arrays are object arrays then having the exact types
6549 // for both will remove the need for a subtype check at runtime
6550 // before the call and may make it possible to pick a faster copy
6551 // routine (without a subtype check on every element)
6552 // Do we have the exact type of src?
6553 bool could_have_src = src_spec;
6554 // Do we have the exact type of dest?
6555 bool could_have_dest = dest_spec;
6556 ciKlass* src_k = nullptr;
6557 ciKlass* dest_k = nullptr;
6558 if (!src_spec) {
6559 src_k = src_type->speculative_type_not_null();
6560 if (src_k != nullptr && src_k->is_array_klass()) {
6561 could_have_src = true;
6562 }
6563 }
6564 if (!dest_spec) {
6565 dest_k = dest_type->speculative_type_not_null();
6566 if (dest_k != nullptr && dest_k->is_array_klass()) {
6567 could_have_dest = true;
6568 }
6569 }
6570 if (could_have_src && could_have_dest) {
6571 // If we can have both exact types, emit the missing guards
6572 if (could_have_src && !src_spec) {
6573 src = maybe_cast_profiled_obj(src, src_k, true);
6574 src_type = _gvn.type(src);
6575 top_src = src_type->isa_aryptr();
6576 }
6577 if (could_have_dest && !dest_spec) {
6578 dest = maybe_cast_profiled_obj(dest, dest_k, true);
6579 dest_type = _gvn.type(dest);
6580 top_dest = dest_type->isa_aryptr();
6581 }
6582 }
6583 }
6584 }
6585
6586 ciMethod* trap_method = method();
6587 int trap_bci = bci();
6588 if (saved_jvms_before_guards != nullptr) {
6589 trap_method = alloc->jvms()->method();
6590 trap_bci = alloc->jvms()->bci();
6591 }
6592
6593 bool negative_length_guard_generated = false;
6594
6595 if (!C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_intrinsic) &&
6596 can_emit_guards && !src->is_top() && !dest->is_top()) {
6597 // validate arguments: enables transformation the ArrayCopyNode
6598 validated = true;
6599
6600 RegionNode* slow_region = new RegionNode(1);
6601 record_for_igvn(slow_region);
6602
6603 // (1) src and dest are arrays.
6604 generate_non_array_guard(load_object_klass(src), slow_region, &src);
6605 generate_non_array_guard(load_object_klass(dest), slow_region, &dest);
6606
6607 // (2) src and dest arrays must have elements of the same BasicType
6608 // done at macro expansion or at Ideal transformation time
6609
6610 // (4) src_offset must not be negative.
6611 generate_negative_guard(src_offset, slow_region);
6612
6613 // (5) dest_offset must not be negative.
6614 generate_negative_guard(dest_offset, slow_region);
6615
6616 // (7) src_offset + length must not exceed length of src.
6619 slow_region);
6620
6621 // (8) dest_offset + length must not exceed length of dest.
6622 generate_limit_guard(dest_offset, length,
6623 load_array_length(dest),
6624 slow_region);
6625
6626 // (6) length must not be negative.
6627 // This is also checked in generate_arraycopy() during macro expansion, but
6628 // we also have to check it here for the case where the ArrayCopyNode will
6629 // be eliminated by Escape Analysis.
6630 if (EliminateAllocations) {
6631 generate_negative_guard(length, slow_region);
6632 negative_length_guard_generated = true;
6633 }
6634
6635 // (9) each element of an oop array must be assignable
6636 Node* dest_klass = load_object_klass(dest);
6637 if (src != dest) {
6638 Node* not_subtype_ctrl = gen_subtype_check(src, dest_klass);
6639 slow_region->add_req(not_subtype_ctrl);
6640 }
6641
6642 // TODO 8350865 Fix below logic. Also handle atomicity.
6643 generate_fair_guard(flat_array_test(src), slow_region);
6644 generate_fair_guard(flat_array_test(dest), slow_region);
6645
6646 const TypeKlassPtr* dest_klass_t = _gvn.type(dest_klass)->is_klassptr();
6647 const Type* toop = dest_klass_t->cast_to_exactness(false)->as_instance_type();
6648 src = _gvn.transform(new CheckCastPPNode(control(), src, toop));
6649 src_type = _gvn.type(src);
6650 top_src = src_type->isa_aryptr();
6651
6652 // Handle flat inline type arrays (null-free arrays are handled by the subtype check above)
6653 if (!stopped() && UseArrayFlattening) {
6654 // If dest is flat, src must be flat as well (guaranteed by src <: dest check). Handle flat src here.
6655 assert(top_dest == nullptr || !top_dest->is_flat() || top_src->is_flat(), "src array must be flat");
6656 if (top_src != nullptr && top_src->is_flat()) {
6657 // Src is flat, check that dest is flat as well
6658 if (top_dest != nullptr && !top_dest->is_flat()) {
6659 generate_fair_guard(flat_array_test(dest_klass, /* flat = */ false), slow_region);
6660 // Since dest is flat and src <: dest, dest must have the same type as src.
6661 top_dest = top_src->cast_to_exactness(false);
6662 assert(top_dest->is_flat(), "dest must be flat");
6663 dest = _gvn.transform(new CheckCastPPNode(control(), dest, top_dest));
6664 }
6665 } else if (top_src == nullptr || !top_src->is_not_flat()) {
6666 // Src might be flat and dest might not be flat. Go to the slow path if src is flat.
6667 // TODO 8251971: Optimize for the case when src/dest are later found to be both flat.
6668 assert(top_dest == nullptr || !top_dest->is_flat(), "dest array must not be flat");
6669 generate_fair_guard(flat_array_test(src), slow_region);
6670 if (top_src != nullptr) {
6671 top_src = top_src->cast_to_not_flat();
6672 src = _gvn.transform(new CheckCastPPNode(control(), src, top_src));
6673 }
6674 }
6675 }
6676
6677 {
6678 PreserveJVMState pjvms(this);
6679 set_control(_gvn.transform(slow_region));
6680 uncommon_trap(Deoptimization::Reason_intrinsic,
6681 Deoptimization::Action_make_not_entrant);
6682 assert(stopped(), "Should be stopped");
6683 }
6684 arraycopy_move_allocation_here(alloc, dest, saved_jvms_before_guards, saved_reexecute_sp, new_idx);
6685 }
6686
6687 if (stopped()) {
6688 return true;
6689 }
6690
6691 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, src, src_offset, dest, dest_offset, length, alloc != nullptr, negative_length_guard_generated,
6692 // Create LoadRange and LoadKlass nodes for use during macro expansion here
6693 // so the compiler has a chance to eliminate them: during macro expansion,
6694 // we have to set their control (CastPP nodes are eliminated).
6695 load_object_klass(src), load_object_klass(dest),
6696 load_array_length(src), load_array_length(dest));
6697
6698 ac->set_arraycopy(validated);
6699
6700 Node* n = _gvn.transform(ac);
6701 if (n == ac) {
6702 ac->connect_outputs(this);
6703 } else {
|