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.
301 case vmIntrinsics::_indexOfIL: return inline_string_indexOfI(StrIntrinsicNode::LL);
302 case vmIntrinsics::_indexOfIU: return inline_string_indexOfI(StrIntrinsicNode::UU);
303 case vmIntrinsics::_indexOfIUL: return inline_string_indexOfI(StrIntrinsicNode::UL);
304 case vmIntrinsics::_indexOfU_char: return inline_string_indexOfChar(StrIntrinsicNode::U);
305 case vmIntrinsics::_indexOfL_char: return inline_string_indexOfChar(StrIntrinsicNode::L);
306
307 case vmIntrinsics::_equalsL: return inline_string_equals(StrIntrinsicNode::LL);
308
309 case vmIntrinsics::_vectorizedHashCode: return inline_vectorizedHashCode();
310
311 case vmIntrinsics::_toBytesStringU: return inline_string_toBytesU();
312 case vmIntrinsics::_getCharsStringU: return inline_string_getCharsU();
313 case vmIntrinsics::_getCharStringU: return inline_string_char_access(!is_store);
314 case vmIntrinsics::_putCharStringU: return inline_string_char_access( is_store);
315
316 case vmIntrinsics::_compressStringC:
317 case vmIntrinsics::_compressStringB: return inline_string_copy( is_compress);
318 case vmIntrinsics::_inflateStringC:
319 case vmIntrinsics::_inflateStringB: return inline_string_copy(!is_compress);
320
321 case vmIntrinsics::_getReference: return inline_unsafe_access(!is_store, T_OBJECT, Relaxed, false);
322 case vmIntrinsics::_getBoolean: return inline_unsafe_access(!is_store, T_BOOLEAN, Relaxed, false);
323 case vmIntrinsics::_getByte: return inline_unsafe_access(!is_store, T_BYTE, Relaxed, false);
324 case vmIntrinsics::_getShort: return inline_unsafe_access(!is_store, T_SHORT, Relaxed, false);
325 case vmIntrinsics::_getChar: return inline_unsafe_access(!is_store, T_CHAR, Relaxed, false);
326 case vmIntrinsics::_getInt: return inline_unsafe_access(!is_store, T_INT, Relaxed, false);
327 case vmIntrinsics::_getLong: return inline_unsafe_access(!is_store, T_LONG, Relaxed, false);
328 case vmIntrinsics::_getFloat: return inline_unsafe_access(!is_store, T_FLOAT, Relaxed, false);
329 case vmIntrinsics::_getDouble: return inline_unsafe_access(!is_store, T_DOUBLE, Relaxed, false);
330
331 case vmIntrinsics::_putReference: return inline_unsafe_access( is_store, T_OBJECT, Relaxed, false);
332 case vmIntrinsics::_putBoolean: return inline_unsafe_access( is_store, T_BOOLEAN, Relaxed, false);
333 case vmIntrinsics::_putByte: return inline_unsafe_access( is_store, T_BYTE, Relaxed, false);
334 case vmIntrinsics::_putShort: return inline_unsafe_access( is_store, T_SHORT, Relaxed, false);
335 case vmIntrinsics::_putChar: return inline_unsafe_access( is_store, T_CHAR, Relaxed, false);
336 case vmIntrinsics::_putInt: return inline_unsafe_access( is_store, T_INT, Relaxed, false);
337 case vmIntrinsics::_putLong: return inline_unsafe_access( is_store, T_LONG, Relaxed, false);
338 case vmIntrinsics::_putFloat: return inline_unsafe_access( is_store, T_FLOAT, Relaxed, false);
339 case vmIntrinsics::_putDouble: return inline_unsafe_access( is_store, T_DOUBLE, Relaxed, false);
340
341 case vmIntrinsics::_getReferenceVolatile: return inline_unsafe_access(!is_store, T_OBJECT, Volatile, false);
342 case vmIntrinsics::_getBooleanVolatile: return inline_unsafe_access(!is_store, T_BOOLEAN, Volatile, false);
343 case vmIntrinsics::_getByteVolatile: return inline_unsafe_access(!is_store, T_BYTE, Volatile, false);
344 case vmIntrinsics::_getShortVolatile: return inline_unsafe_access(!is_store, T_SHORT, Volatile, false);
345 case vmIntrinsics::_getCharVolatile: return inline_unsafe_access(!is_store, T_CHAR, Volatile, false);
346 case vmIntrinsics::_getIntVolatile: return inline_unsafe_access(!is_store, T_INT, Volatile, false);
347 case vmIntrinsics::_getLongVolatile: return inline_unsafe_access(!is_store, T_LONG, Volatile, false);
348 case vmIntrinsics::_getFloatVolatile: return inline_unsafe_access(!is_store, T_FLOAT, Volatile, false);
349 case vmIntrinsics::_getDoubleVolatile: return inline_unsafe_access(!is_store, T_DOUBLE, Volatile, false);
350
351 case vmIntrinsics::_putReferenceVolatile: return inline_unsafe_access( is_store, T_OBJECT, Volatile, false);
352 case vmIntrinsics::_putBooleanVolatile: return inline_unsafe_access( is_store, T_BOOLEAN, Volatile, false);
353 case vmIntrinsics::_putByteVolatile: return inline_unsafe_access( is_store, T_BYTE, Volatile, false);
354 case vmIntrinsics::_putShortVolatile: return inline_unsafe_access( is_store, T_SHORT, Volatile, false);
355 case vmIntrinsics::_putCharVolatile: return inline_unsafe_access( is_store, T_CHAR, Volatile, false);
356 case vmIntrinsics::_putIntVolatile: return inline_unsafe_access( is_store, T_INT, Volatile, false);
357 case vmIntrinsics::_putLongVolatile: return inline_unsafe_access( is_store, T_LONG, Volatile, false);
358 case vmIntrinsics::_putFloatVolatile: return inline_unsafe_access( is_store, T_FLOAT, Volatile, false);
359 case vmIntrinsics::_putDoubleVolatile: return inline_unsafe_access( is_store, T_DOUBLE, Volatile, false);
391 case vmIntrinsics::_getReferenceOpaque: return inline_unsafe_access(!is_store, T_OBJECT, Opaque, false);
392 case vmIntrinsics::_getBooleanOpaque: return inline_unsafe_access(!is_store, T_BOOLEAN, Opaque, false);
393 case vmIntrinsics::_getByteOpaque: return inline_unsafe_access(!is_store, T_BYTE, Opaque, false);
394 case vmIntrinsics::_getShortOpaque: return inline_unsafe_access(!is_store, T_SHORT, Opaque, false);
395 case vmIntrinsics::_getCharOpaque: return inline_unsafe_access(!is_store, T_CHAR, Opaque, false);
396 case vmIntrinsics::_getIntOpaque: return inline_unsafe_access(!is_store, T_INT, Opaque, false);
397 case vmIntrinsics::_getLongOpaque: return inline_unsafe_access(!is_store, T_LONG, Opaque, false);
398 case vmIntrinsics::_getFloatOpaque: return inline_unsafe_access(!is_store, T_FLOAT, Opaque, false);
399 case vmIntrinsics::_getDoubleOpaque: return inline_unsafe_access(!is_store, T_DOUBLE, Opaque, false);
400
401 case vmIntrinsics::_putReferenceOpaque: return inline_unsafe_access( is_store, T_OBJECT, Opaque, false);
402 case vmIntrinsics::_putBooleanOpaque: return inline_unsafe_access( is_store, T_BOOLEAN, Opaque, false);
403 case vmIntrinsics::_putByteOpaque: return inline_unsafe_access( is_store, T_BYTE, Opaque, false);
404 case vmIntrinsics::_putShortOpaque: return inline_unsafe_access( is_store, T_SHORT, Opaque, false);
405 case vmIntrinsics::_putCharOpaque: return inline_unsafe_access( is_store, T_CHAR, Opaque, false);
406 case vmIntrinsics::_putIntOpaque: return inline_unsafe_access( is_store, T_INT, Opaque, false);
407 case vmIntrinsics::_putLongOpaque: return inline_unsafe_access( is_store, T_LONG, Opaque, false);
408 case vmIntrinsics::_putFloatOpaque: return inline_unsafe_access( is_store, T_FLOAT, Opaque, false);
409 case vmIntrinsics::_putDoubleOpaque: return inline_unsafe_access( is_store, T_DOUBLE, Opaque, false);
410
411 case vmIntrinsics::_compareAndSetReference: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap, Volatile);
412 case vmIntrinsics::_compareAndSetByte: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap, Volatile);
413 case vmIntrinsics::_compareAndSetShort: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap, Volatile);
414 case vmIntrinsics::_compareAndSetInt: return inline_unsafe_load_store(T_INT, LS_cmp_swap, Volatile);
415 case vmIntrinsics::_compareAndSetLong: return inline_unsafe_load_store(T_LONG, LS_cmp_swap, Volatile);
416
417 case vmIntrinsics::_weakCompareAndSetReferencePlain: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Relaxed);
418 case vmIntrinsics::_weakCompareAndSetReferenceAcquire: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Acquire);
419 case vmIntrinsics::_weakCompareAndSetReferenceRelease: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Release);
420 case vmIntrinsics::_weakCompareAndSetReference: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Volatile);
421 case vmIntrinsics::_weakCompareAndSetBytePlain: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Relaxed);
422 case vmIntrinsics::_weakCompareAndSetByteAcquire: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Acquire);
423 case vmIntrinsics::_weakCompareAndSetByteRelease: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Release);
424 case vmIntrinsics::_weakCompareAndSetByte: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Volatile);
425 case vmIntrinsics::_weakCompareAndSetShortPlain: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Relaxed);
426 case vmIntrinsics::_weakCompareAndSetShortAcquire: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Acquire);
427 case vmIntrinsics::_weakCompareAndSetShortRelease: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Release);
428 case vmIntrinsics::_weakCompareAndSetShort: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Volatile);
429 case vmIntrinsics::_weakCompareAndSetIntPlain: return inline_unsafe_load_store(T_INT, LS_cmp_swap_weak, Relaxed);
430 case vmIntrinsics::_weakCompareAndSetIntAcquire: return inline_unsafe_load_store(T_INT, LS_cmp_swap_weak, Acquire);
498 #endif
499 case vmIntrinsics::_currentTimeMillis: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeMillis), "currentTimeMillis");
500 case vmIntrinsics::_nanoTime: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeNanos), "nanoTime");
501 case vmIntrinsics::_writeback0: return inline_unsafe_writeback0();
502 case vmIntrinsics::_writebackPreSync0: return inline_unsafe_writebackSync0(true);
503 case vmIntrinsics::_writebackPostSync0: return inline_unsafe_writebackSync0(false);
504 case vmIntrinsics::_allocateInstance: return inline_unsafe_allocate();
505 case vmIntrinsics::_copyMemory: return inline_unsafe_copyMemory();
506 case vmIntrinsics::_setMemory: return inline_unsafe_setMemory();
507 case vmIntrinsics::_getLength: return inline_native_getLength();
508 case vmIntrinsics::_copyOf: return inline_array_copyOf(false);
509 case vmIntrinsics::_copyOfRange: return inline_array_copyOf(true);
510 case vmIntrinsics::_equalsB: return inline_array_equals(StrIntrinsicNode::LL);
511 case vmIntrinsics::_equalsC: return inline_array_equals(StrIntrinsicNode::UU);
512 case vmIntrinsics::_Preconditions_checkIndex: return inline_preconditions_checkIndex(T_INT);
513 case vmIntrinsics::_Preconditions_checkLongIndex: return inline_preconditions_checkIndex(T_LONG);
514 case vmIntrinsics::_clone: return inline_native_clone(intrinsic()->is_virtual());
515
516 case vmIntrinsics::_allocateUninitializedArray: return inline_unsafe_newArray(true);
517 case vmIntrinsics::_newArray: return inline_unsafe_newArray(false);
518
519 case vmIntrinsics::_isAssignableFrom: return inline_native_subtype_check();
520
521 case vmIntrinsics::_isInstance:
522 case vmIntrinsics::_isHidden:
523 case vmIntrinsics::_getSuperclass: return inline_native_Class_query(intrinsic_id());
524
525 case vmIntrinsics::_floatToRawIntBits:
526 case vmIntrinsics::_floatToIntBits:
527 case vmIntrinsics::_intBitsToFloat:
528 case vmIntrinsics::_doubleToRawLongBits:
529 case vmIntrinsics::_doubleToLongBits:
530 case vmIntrinsics::_longBitsToDouble:
531 case vmIntrinsics::_floatToFloat16:
532 case vmIntrinsics::_float16ToFloat: return inline_fp_conversions(intrinsic_id());
533 case vmIntrinsics::_sqrt_float16: return inline_fp16_operations(intrinsic_id(), 1);
534 case vmIntrinsics::_fma_float16: return inline_fp16_operations(intrinsic_id(), 3);
535 case vmIntrinsics::_floatIsFinite:
536 case vmIntrinsics::_floatIsInfinite:
537 case vmIntrinsics::_doubleIsFinite:
2315 case vmIntrinsics::_remainderUnsigned_l: {
2316 zero_check_long(argument(2));
2317 // Compile-time detect of null-exception
2318 if (stopped()) {
2319 return true; // keep the graph constructed so far
2320 }
2321 n = new UModLNode(control(), argument(0), argument(2));
2322 break;
2323 }
2324 default: fatal_unexpected_iid(id); break;
2325 }
2326 set_result(_gvn.transform(n));
2327 return true;
2328 }
2329
2330 //----------------------------inline_unsafe_access----------------------------
2331
2332 const TypeOopPtr* LibraryCallKit::sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type) {
2333 // Attempt to infer a sharper value type from the offset and base type.
2334 ciKlass* sharpened_klass = nullptr;
2335
2336 // See if it is an instance field, with an object type.
2337 if (alias_type->field() != nullptr) {
2338 if (alias_type->field()->type()->is_klass()) {
2339 sharpened_klass = alias_type->field()->type()->as_klass();
2340 }
2341 }
2342
2343 const TypeOopPtr* result = nullptr;
2344 // See if it is a narrow oop array.
2345 if (adr_type->isa_aryptr()) {
2346 if (adr_type->offset() >= objArrayOopDesc::base_offset_in_bytes()) {
2347 const TypeOopPtr* elem_type = adr_type->is_aryptr()->elem()->make_oopptr();
2348 if (elem_type != nullptr && elem_type->is_loaded()) {
2349 // Sharpen the value type.
2350 result = elem_type;
2351 }
2352 }
2353 }
2354
2355 // The sharpened class might be unloaded if there is no class loader
2356 // contraint in place.
2357 if (result == nullptr && sharpened_klass != nullptr && sharpened_klass->is_loaded()) {
2358 // Sharpen the value type.
2359 result = TypeOopPtr::make_from_klass(sharpened_klass);
2360 }
2361 if (result != nullptr) {
2362 #ifndef PRODUCT
2363 if (C->print_intrinsics() || C->print_inlining()) {
2364 tty->print(" from base type: "); adr_type->dump(); tty->cr();
2365 tty->print(" sharpened value: "); result->dump(); tty->cr();
2366 }
2367 #endif
2368 }
2369 return result;
2370 }
2371
2372 DecoratorSet LibraryCallKit::mo_decorator_for_access_kind(AccessKind kind) {
2373 switch (kind) {
2374 case Relaxed:
2375 return MO_UNORDERED;
2376 case Opaque:
2377 return MO_RELAXED;
2378 case Acquire:
2379 return MO_ACQUIRE;
2411 _kit->jvms()->set_sp(_sp);
2412 _map->set_jvms(_kit->jvms());
2413 _kit->set_map(_map);
2414 _kit->set_sp(_sp);
2415 for (DUIterator_Fast imax, i = _kit->control()->fast_outs(imax); i < imax; i++) {
2416 Node* out = _kit->control()->fast_out(i);
2417 if (out->is_CFG() && out->in(0) == _kit->control() && out != _kit->map() && !_ctrl_succ.member(out)) {
2418 _kit->_gvn.hash_delete(out);
2419 out->set_req(0, _kit->C->top());
2420 _kit->C->record_for_igvn(out);
2421 --i; --imax;
2422 _kit->_gvn.hash_find_insert(out);
2423 }
2424 }
2425 }
2426
2427 void LibraryCallKit::SavedState::discard() {
2428 _discarded = true;
2429 }
2430
2431 bool LibraryCallKit::inline_unsafe_access(bool is_store, const BasicType type, const AccessKind kind, const bool unaligned) {
2432 if (callee()->is_static()) return false; // caller must have the capability!
2433 DecoratorSet decorators = C2_UNSAFE_ACCESS;
2434 guarantee(!is_store || kind != Acquire, "Acquire accesses can be produced only for loads");
2435 guarantee( is_store || kind != Release, "Release accesses can be produced only for stores");
2436 assert(type != T_OBJECT || !unaligned, "unaligned access not supported with object type");
2437
2438 if (is_reference_type(type)) {
2439 decorators |= ON_UNKNOWN_OOP_REF;
2440 }
2441
2442 if (unaligned) {
2443 decorators |= C2_UNALIGNED;
2444 }
2445
2446 #ifndef PRODUCT
2447 {
2448 ResourceMark rm;
2449 // Check the signatures.
2450 ciSignature* sig = callee()->signature();
2451 #ifdef ASSERT
2452 if (!is_store) {
2453 // Object getReference(Object base, int/long offset), etc.
2454 BasicType rtype = sig->return_type()->basic_type();
2455 assert(rtype == type, "getter must return the expected value");
2456 assert(sig->count() == 2, "oop getter has 2 arguments");
2457 assert(sig->type_at(0)->basic_type() == T_OBJECT, "getter base is object");
2458 assert(sig->type_at(1)->basic_type() == T_LONG, "getter offset is correct");
2459 } else {
2460 // void putReference(Object base, int/long offset, Object x), etc.
2461 assert(sig->return_type()->basic_type() == T_VOID, "putter must not return a value");
2462 assert(sig->count() == 3, "oop putter has 3 arguments");
2463 assert(sig->type_at(0)->basic_type() == T_OBJECT, "putter base is object");
2464 assert(sig->type_at(1)->basic_type() == T_LONG, "putter offset is correct");
2465 BasicType vtype = sig->type_at(sig->count()-1)->basic_type();
2466 assert(vtype == type, "putter must accept the expected value");
2467 }
2468 #endif // ASSERT
2469 }
2470 #endif //PRODUCT
2471
2472 C->set_has_unsafe_access(true); // Mark eventual nmethod as "unsafe".
2473
2474 Node* receiver = argument(0); // type: oop
2475
2476 // Build address expression.
2477 Node* heap_base_oop = top();
2478
2479 // The base is either a Java object or a value produced by Unsafe.staticFieldBase
2480 Node* base = argument(1); // type: oop
2481 // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
2482 Node* offset = argument(2); // type: long
2483 // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2484 // to be plain byte offsets, which are also the same as those accepted
2485 // by oopDesc::field_addr.
2486 assert(Unsafe_field_offset_to_byte_offset(11) == 11,
2487 "fieldOffset must be byte-scaled");
2488 // 32-bit machines ignore the high half!
2489 offset = ConvL2X(offset);
2490
2491 // Save state and restore on bailout
2492 SavedState old_state(this);
2493
2494 Node* adr = make_unsafe_address(base, offset, type, kind == Relaxed);
2495 assert(!stopped(), "Inlining of unsafe access failed: address construction stopped unexpectedly");
2496
2497 if (_gvn.type(base->uncast())->isa_ptr() == TypePtr::NULL_PTR) {
2498 if (type != T_OBJECT) {
2499 decorators |= IN_NATIVE; // off-heap primitive access
2500 } else {
2501 return false; // off-heap oop accesses are not supported
2502 }
2503 } else {
2504 heap_base_oop = base; // on-heap or mixed access
2505 }
2506
2507 // Can base be null? Otherwise, always on-heap access.
2508 bool can_access_non_heap = TypePtr::NULL_PTR->higher_equal(_gvn.type(base));
2509
2510 if (!can_access_non_heap) {
2511 decorators |= IN_HEAP;
2512 }
2513
2514 Node* val = is_store ? argument(4) : nullptr;
2515
2516 const TypePtr* adr_type = _gvn.type(adr)->isa_ptr();
2517 if (adr_type == TypePtr::NULL_PTR) {
2518 return false; // off-heap access with zero address
2519 }
2520
2521 // Try to categorize the address.
2522 Compile::AliasType* alias_type = C->alias_type(adr_type);
2523 assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2524
2525 if (alias_type->adr_type() == TypeInstPtr::KLASS ||
2526 alias_type->adr_type() == TypeAryPtr::RANGE) {
2527 return false; // not supported
2528 }
2529
2530 bool mismatched = false;
2531 BasicType bt = alias_type->basic_type();
2532 if (bt != T_ILLEGAL) {
2533 assert(alias_type->adr_type()->is_oopptr(), "should be on-heap access");
2534 if (bt == T_BYTE && adr_type->isa_aryptr()) {
2535 // Alias type doesn't differentiate between byte[] and boolean[]).
2536 // Use address type to get the element type.
2537 bt = adr_type->is_aryptr()->elem()->array_element_basic_type();
2538 }
2539 if (is_reference_type(bt, true)) {
2540 // accessing an array field with getReference is not a mismatch
2541 bt = T_OBJECT;
2542 }
2543 if ((bt == T_OBJECT) != (type == T_OBJECT)) {
2544 // Don't intrinsify mismatched object accesses
2545 return false;
2546 }
2547 mismatched = (bt != type);
2548 } else if (alias_type->adr_type()->isa_oopptr()) {
2549 mismatched = true; // conservatively mark all "wide" on-heap accesses as mismatched
2550 }
2551
2552 old_state.discard();
2553 assert(!mismatched || alias_type->adr_type()->is_oopptr(), "off-heap access can't be mismatched");
2554
2555 if (mismatched) {
2556 decorators |= C2_MISMATCHED;
2557 }
2558
2559 // First guess at the value type.
2560 const Type *value_type = Type::get_const_basic_type(type);
2561
2562 // Figure out the memory ordering.
2563 decorators |= mo_decorator_for_access_kind(kind);
2564
2565 if (!is_store && type == T_OBJECT) {
2566 const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2567 if (tjp != nullptr) {
2568 value_type = tjp;
2569 }
2570 }
2571
2572 receiver = null_check(receiver);
2573 if (stopped()) {
2574 return true;
2575 }
2576 // Heap pointers get a null-check from the interpreter,
2577 // as a courtesy. However, this is not guaranteed by Unsafe,
2578 // and it is not possible to fully distinguish unintended nulls
2579 // from intended ones in this API.
2580
2581 if (!is_store) {
2582 Node* p = nullptr;
2583 // Try to constant fold a load from a constant field
2584 ciField* field = alias_type->field();
2585 if (heap_base_oop != top() && field != nullptr && field->is_constant() && !mismatched) {
2586 // final or stable field
2587 p = make_constant_from_field(field, heap_base_oop);
2588 }
2589
2590 if (p == nullptr) { // Could not constant fold the load
2591 p = access_load_at(heap_base_oop, adr, adr_type, value_type, type, decorators);
2592 // Normalize the value returned by getBoolean in the following cases
2593 if (type == T_BOOLEAN &&
2594 (mismatched ||
2595 heap_base_oop == top() || // - heap_base_oop is null or
2596 (can_access_non_heap && field == nullptr)) // - heap_base_oop is potentially null
2597 // and the unsafe access is made to large offset
2598 // (i.e., larger than the maximum offset necessary for any
2599 // field access)
2600 ) {
2601 IdealKit ideal = IdealKit(this);
2602 #define __ ideal.
2603 IdealVariable normalized_result(ideal);
2604 __ declarations_done();
2605 __ set(normalized_result, p);
2606 __ if_then(p, BoolTest::ne, ideal.ConI(0));
2607 __ set(normalized_result, ideal.ConI(1));
2608 ideal.end_if();
2609 final_sync(ideal);
2610 p = __ value(normalized_result);
2611 #undef __
2612 }
2613 }
2614 if (type == T_ADDRESS) {
2615 p = gvn().transform(new CastP2XNode(nullptr, p));
2616 p = ConvX2UL(p);
2617 }
2618 // The load node has the control of the preceding MemBarCPUOrder. All
2619 // following nodes will have the control of the MemBarCPUOrder inserted at
2620 // the end of this method. So, pushing the load onto the stack at a later
2621 // point is fine.
2622 set_result(p);
2623 } else {
2624 if (bt == T_ADDRESS) {
2625 // Repackage the long as a pointer.
2626 val = ConvL2X(val);
2627 val = gvn().transform(new CastX2PNode(val));
2628 }
2629 access_store_at(heap_base_oop, adr, adr_type, val, value_type, type, decorators);
2630 }
2631
2632 return true;
2633 }
2634
2635 //----------------------------inline_unsafe_load_store----------------------------
2636 // This method serves a couple of different customers (depending on LoadStoreKind):
2637 //
2638 // LS_cmp_swap:
2639 //
2640 // boolean compareAndSetReference(Object o, long offset, Object expected, Object x);
2641 // boolean compareAndSetInt( Object o, long offset, int expected, int x);
2642 // boolean compareAndSetLong( Object o, long offset, long expected, long x);
2643 //
2644 // LS_cmp_swap_weak:
2645 //
2646 // boolean weakCompareAndSetReference( Object o, long offset, Object expected, Object x);
2647 // boolean weakCompareAndSetReferencePlain( Object o, long offset, Object expected, Object x);
2648 // boolean weakCompareAndSetReferenceAcquire(Object o, long offset, Object expected, Object x);
2649 // boolean weakCompareAndSetReferenceRelease(Object o, long offset, Object expected, Object x);
2650 //
2651 // boolean weakCompareAndSetInt( Object o, long offset, int expected, int x);
2652 // boolean weakCompareAndSetIntPlain( Object o, long offset, int expected, int x);
2653 // boolean weakCompareAndSetIntAcquire( Object o, long offset, int expected, int x);
2654 // boolean weakCompareAndSetIntRelease( Object o, long offset, int expected, int x);
2817 }
2818 case LS_cmp_swap:
2819 case LS_cmp_swap_weak:
2820 case LS_get_add:
2821 break;
2822 default:
2823 ShouldNotReachHere();
2824 }
2825
2826 // Null check receiver.
2827 receiver = null_check(receiver);
2828 if (stopped()) {
2829 return true;
2830 }
2831
2832 int alias_idx = C->get_alias_index(adr_type);
2833
2834 if (is_reference_type(type)) {
2835 decorators |= IN_HEAP | ON_UNKNOWN_OOP_REF;
2836
2837 // Transformation of a value which could be null pointer (CastPP #null)
2838 // could be delayed during Parse (for example, in adjust_map_after_if()).
2839 // Execute transformation here to avoid barrier generation in such case.
2840 if (_gvn.type(newval) == TypePtr::NULL_PTR)
2841 newval = _gvn.makecon(TypePtr::NULL_PTR);
2842
2843 if (oldval != nullptr && _gvn.type(oldval) == TypePtr::NULL_PTR) {
2844 // Refine the value to a null constant, when it is known to be null
2845 oldval = _gvn.makecon(TypePtr::NULL_PTR);
2846 }
2847 }
2848
2849 Node* result = nullptr;
2850 switch (kind) {
2851 case LS_cmp_exchange: {
2852 result = access_atomic_cmpxchg_val_at(base, adr, adr_type, alias_idx,
2853 oldval, newval, value_type, type, decorators);
2854 break;
2855 }
2856 case LS_cmp_swap_weak:
3003 Deoptimization::Action_make_not_entrant);
3004 }
3005 if (stopped()) {
3006 return true;
3007 }
3008 #endif //INCLUDE_JVMTI
3009
3010 Node* test = nullptr;
3011 if (LibraryCallKit::klass_needs_init_guard(kls)) {
3012 // Note: The argument might still be an illegal value like
3013 // Serializable.class or Object[].class. The runtime will handle it.
3014 // But we must make an explicit check for initialization.
3015 Node* insp = basic_plus_adr(kls, in_bytes(InstanceKlass::init_state_offset()));
3016 // Use T_BOOLEAN for InstanceKlass::_init_state so the compiler
3017 // can generate code to load it as unsigned byte.
3018 Node* inst = make_load(nullptr, insp, TypeInt::UBYTE, T_BOOLEAN, MemNode::acquire);
3019 Node* bits = intcon(InstanceKlass::fully_initialized);
3020 test = _gvn.transform(new SubINode(inst, bits));
3021 // The 'test' is non-zero if we need to take a slow path.
3022 }
3023
3024 Node* obj = new_instance(kls, test);
3025 set_result(obj);
3026 return true;
3027 }
3028
3029 //------------------------inline_native_time_funcs--------------
3030 // inline code for System.currentTimeMillis() and System.nanoTime()
3031 // these have the same type and signature
3032 bool LibraryCallKit::inline_native_time_funcs(address funcAddr, const char* funcName) {
3033 const TypeFunc* tf = OptoRuntime::void_long_Type();
3034 const TypePtr* no_memory_effects = nullptr;
3035 Node* time = make_runtime_call(RC_LEAF, tf, funcAddr, funcName, no_memory_effects);
3036 Node* value = _gvn.transform(new ProjNode(time, TypeFunc::Parms+0));
3037 #ifdef ASSERT
3038 Node* value_top = _gvn.transform(new ProjNode(time, TypeFunc::Parms+1));
3039 assert(value_top == top(), "second value must be top");
3040 #endif
3041 set_result(value);
3042 return true;
3043 }
3044
3785 Node* thread = _gvn.transform(new ThreadLocalNode());
3786 Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::vthread_offset()));
3787 Node* thread_obj_handle
3788 = make_load(nullptr, p, p->bottom_type()->is_ptr(), T_OBJECT, MemNode::unordered);
3789 thread_obj_handle = _gvn.transform(thread_obj_handle);
3790 const TypePtr *adr_type = _gvn.type(thread_obj_handle)->isa_ptr();
3791 access_store_at(nullptr, thread_obj_handle, adr_type, arr, _gvn.type(arr), T_OBJECT, IN_NATIVE | MO_UNORDERED);
3792
3793 // Change the _monitor_owner_id of the JavaThread
3794 Node* tid = load_field_from_object(arr, "tid", "J");
3795 Node* monitor_owner_id_offset = basic_plus_adr(thread, in_bytes(JavaThread::monitor_owner_id_offset()));
3796 store_to_memory(control(), monitor_owner_id_offset, tid, T_LONG, MemNode::unordered, true);
3797
3798 JFR_ONLY(extend_setCurrentThread(thread, arr);)
3799 return true;
3800 }
3801
3802 const Type* LibraryCallKit::scopedValueCache_type() {
3803 ciKlass* objects_klass = ciObjArrayKlass::make(env()->Object_klass());
3804 const TypeOopPtr* etype = TypeOopPtr::make_from_klass(env()->Object_klass());
3805 const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS);
3806
3807 // Because we create the scopedValue cache lazily we have to make the
3808 // type of the result BotPTR.
3809 bool xk = etype->klass_is_exact();
3810 const Type* objects_type = TypeAryPtr::make(TypePtr::BotPTR, arr0, objects_klass, xk, 0);
3811 return objects_type;
3812 }
3813
3814 Node* LibraryCallKit::scopedValueCache_helper() {
3815 Node* thread = _gvn.transform(new ThreadLocalNode());
3816 Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::scopedValueCache_offset()));
3817 // We cannot use immutable_memory() because we might flip onto a
3818 // different carrier thread, at which point we'll need to use that
3819 // carrier thread's cache.
3820 // return _gvn.transform(LoadNode::make(_gvn, nullptr, immutable_memory(), p, p->bottom_type()->is_ptr(),
3821 // TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered));
3822 return make_load(nullptr, p, p->bottom_type()->is_ptr(), T_ADDRESS, MemNode::unordered);
3823 }
3824
3825 //------------------------inline_native_scopedValueCache------------------
3826 bool LibraryCallKit::inline_native_scopedValueCache() {
3827 Node* cache_obj_handle = scopedValueCache_helper();
3828 const Type* objects_type = scopedValueCache_type();
3829 set_result(access_load(cache_obj_handle, objects_type, T_OBJECT, IN_NATIVE));
3830
3914 store_to_memory(control(), pin_count_offset, next_pin_count, T_INT, MemNode::unordered);
3915
3916 // Result of top level CFG and Memory.
3917 RegionNode* result_rgn = new RegionNode(PATH_LIMIT);
3918 record_for_igvn(result_rgn);
3919 PhiNode* result_mem = new PhiNode(result_rgn, Type::MEMORY, TypePtr::BOTTOM);
3920 record_for_igvn(result_mem);
3921
3922 result_rgn->init_req(_true_path, _gvn.transform(valid_pin_count));
3923 result_rgn->init_req(_false_path, _gvn.transform(continuation_is_null));
3924 result_mem->init_req(_true_path, _gvn.transform(reset_memory()));
3925 result_mem->init_req(_false_path, _gvn.transform(input_memory_state));
3926
3927 // Set output state.
3928 set_control(_gvn.transform(result_rgn));
3929 set_all_memory(_gvn.transform(result_mem));
3930
3931 return true;
3932 }
3933
3934 //---------------------------load_mirror_from_klass----------------------------
3935 // Given a klass oop, load its java mirror (a java.lang.Class oop).
3936 Node* LibraryCallKit::load_mirror_from_klass(Node* klass) {
3937 Node* p = basic_plus_adr(klass, in_bytes(Klass::java_mirror_offset()));
3938 Node* load = make_load(nullptr, p, TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered);
3939 // mirror = ((OopHandle)mirror)->resolve();
3940 return access_load(load, TypeInstPtr::MIRROR, T_OBJECT, IN_NATIVE);
3941 }
3942
3943 //-----------------------load_klass_from_mirror_common-------------------------
3944 // Given a java mirror (a java.lang.Class oop), load its corresponding klass oop.
3945 // Test the klass oop for null (signifying a primitive Class like Integer.TYPE),
3946 // and branch to the given path on the region.
3947 // If never_see_null, take an uncommon trap on null, so we can optimistically
3948 // compile for the non-null case.
3949 // If the region is null, force never_see_null = true.
3950 Node* LibraryCallKit::load_klass_from_mirror_common(Node* mirror,
3951 bool never_see_null,
3952 RegionNode* region,
3953 int null_path,
3954 int offset) {
3955 if (region == nullptr) never_see_null = true;
3956 Node* p = basic_plus_adr(mirror, offset);
3957 const TypeKlassPtr* kls_type = TypeInstKlassPtr::OBJECT_OR_NULL;
3958 Node* kls = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), p, TypeRawPtr::BOTTOM, kls_type));
3959 Node* null_ctl = top();
3960 kls = null_check_oop(kls, &null_ctl, never_see_null);
3961 if (region != nullptr) {
3962 // Set region->in(null_path) if the mirror is a primitive (e.g, int.class).
3966 }
3967 return kls;
3968 }
3969
3970 //--------------------(inline_native_Class_query helpers)---------------------
3971 // Use this for JVM_ACC_INTERFACE.
3972 // Fall through if (mods & mask) == bits, take the guard otherwise.
3973 Node* LibraryCallKit::generate_klass_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region,
3974 ByteSize offset, const Type* type, BasicType bt) {
3975 // Branch around if the given klass has the given modifier bit set.
3976 // Like generate_guard, adds a new path onto the region.
3977 Node* modp = basic_plus_adr(kls, in_bytes(offset));
3978 Node* mods = make_load(nullptr, modp, type, bt, MemNode::unordered);
3979 Node* mask = intcon(modifier_mask);
3980 Node* bits = intcon(modifier_bits);
3981 Node* mbit = _gvn.transform(new AndINode(mods, mask));
3982 Node* cmp = _gvn.transform(new CmpINode(mbit, bits));
3983 Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
3984 return generate_fair_guard(bol, region);
3985 }
3986 Node* LibraryCallKit::generate_interface_guard(Node* kls, RegionNode* region) {
3987 return generate_klass_flags_guard(kls, JVM_ACC_INTERFACE, 0, region,
3988 Klass::access_flags_offset(), TypeInt::CHAR, T_CHAR);
3989 }
3990
3991 // Use this for testing if Klass is_hidden, has_finalizer, and is_cloneable_fast.
3992 Node* LibraryCallKit::generate_misc_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region) {
3993 return generate_klass_flags_guard(kls, modifier_mask, modifier_bits, region,
3994 Klass::misc_flags_offset(), TypeInt::UBYTE, T_BOOLEAN);
3995 }
3996
3997 Node* LibraryCallKit::generate_hidden_class_guard(Node* kls, RegionNode* region) {
3998 return generate_misc_flags_guard(kls, KlassFlags::_misc_is_hidden_class, 0, region);
3999 }
4000
4001 //-------------------------inline_native_Class_query-------------------
4002 bool LibraryCallKit::inline_native_Class_query(vmIntrinsics::ID id) {
4003 const Type* return_type = TypeInt::BOOL;
4004 Node* prim_return_value = top(); // what happens if it's a primitive class?
4005 bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
4115 }
4116 if (!stopped()) {
4117 query_value = load_mirror_from_klass(kls);
4118 }
4119 break;
4120
4121 default:
4122 fatal_unexpected_iid(id);
4123 break;
4124 }
4125
4126 // Fall-through is the normal case of a query to a real class.
4127 phi->init_req(1, query_value);
4128 region->init_req(1, control());
4129
4130 C->set_has_split_ifs(true); // Has chance for split-if optimization
4131 set_result(region, phi);
4132 return true;
4133 }
4134
4135 //-------------------------inline_Class_cast-------------------
4136 bool LibraryCallKit::inline_Class_cast() {
4137 Node* mirror = argument(0); // Class
4138 Node* obj = argument(1);
4139 const TypeInstPtr* mirror_con = _gvn.type(mirror)->isa_instptr();
4140 if (mirror_con == nullptr) {
4141 return false; // dead path (mirror->is_top()).
4142 }
4143 if (obj == nullptr || obj->is_top()) {
4144 return false; // dead path
4145 }
4146 const TypeOopPtr* tp = _gvn.type(obj)->isa_oopptr();
4147
4148 // First, see if Class.cast() can be folded statically.
4149 // java_mirror_type() returns non-null for compile-time Class constants.
4150 ciType* tm = mirror_con->java_mirror_type();
4151 if (tm != nullptr && tm->is_klass() &&
4152 tp != nullptr) {
4153 if (!tp->is_loaded()) {
4154 // Don't use intrinsic when class is not loaded.
4155 return false;
4156 } else {
4157 int static_res = C->static_subtype_check(TypeKlassPtr::make(tm->as_klass(), Type::trust_interfaces), tp->as_klass_type());
4158 if (static_res == Compile::SSC_always_true) {
4159 // isInstance() is true - fold the code.
4160 set_result(obj);
4161 return true;
4162 } else if (static_res == Compile::SSC_always_false) {
4163 // Don't use intrinsic, have to throw ClassCastException.
4164 // If the reference is null, the non-intrinsic bytecode will
4165 // be optimized appropriately.
4166 return false;
4167 }
4168 }
4169 }
4170
4171 // Bailout intrinsic and do normal inlining if exception path is frequent.
4172 if (too_many_traps(Deoptimization::Reason_intrinsic)) {
4173 return false;
4174 }
4175
4176 // Generate dynamic checks.
4177 // Class.cast() is java implementation of _checkcast bytecode.
4178 // Do checkcast (Parse::do_checkcast()) optimizations here.
4179
4180 mirror = null_check(mirror);
4181 // If mirror is dead, only null-path is taken.
4182 if (stopped()) {
4183 return true;
4184 }
4185
4186 // Not-subtype or the mirror's klass ptr is null (in case it is a primitive).
4187 enum { _bad_type_path = 1, _prim_path = 2, PATH_LIMIT };
4188 RegionNode* region = new RegionNode(PATH_LIMIT);
4189 record_for_igvn(region);
4190
4191 // Now load the mirror's klass metaobject, and null-check it.
4192 // If kls is null, we have a primitive mirror and
4193 // nothing is an instance of a primitive type.
4194 Node* kls = load_klass_from_mirror(mirror, false, region, _prim_path);
4195
4196 Node* res = top();
4197 if (!stopped()) {
4198 Node* bad_type_ctrl = top();
4199 // Do checkcast optimizations.
4200 res = gen_checkcast(obj, kls, &bad_type_ctrl);
4201 region->init_req(_bad_type_path, bad_type_ctrl);
4202 }
4203 if (region->in(_prim_path) != top() ||
4204 region->in(_bad_type_path) != top()) {
4205 // Let Interpreter throw ClassCastException.
4206 PreserveJVMState pjvms(this);
4207 set_control(_gvn.transform(region));
4208 uncommon_trap(Deoptimization::Reason_intrinsic,
4209 Deoptimization::Action_maybe_recompile);
4210 }
4211 if (!stopped()) {
4212 set_result(res);
4213 }
4214 return true;
4215 }
4216
4217
4218 //--------------------------inline_native_subtype_check------------------------
4219 // This intrinsic takes the JNI calls out of the heart of
4220 // UnsafeFieldAccessorImpl.set, which improves Field.set, readObject, etc.
4221 bool LibraryCallKit::inline_native_subtype_check() {
4222 // Pull both arguments off the stack.
4223 Node* args[2]; // two java.lang.Class mirrors: superc, subc
4224 args[0] = argument(0);
4225 args[1] = argument(1);
4226 Node* klasses[2]; // corresponding Klasses: superk, subk
4227 klasses[0] = klasses[1] = top();
4228
4229 enum {
4230 // A full decision tree on {superc is prim, subc is prim}:
4231 _prim_0_path = 1, // {P,N} => false
4232 // {P,P} & superc!=subc => false
4233 _prim_same_path, // {P,P} & superc==subc => true
4234 _prim_1_path, // {N,P} => false
4235 _ref_subtype_path, // {N,N} & subtype check wins => true
4236 _both_ref_path, // {N,N} & subtype check loses => false
4237 PATH_LIMIT
4238 };
4239
4240 RegionNode* region = new RegionNode(PATH_LIMIT);
4241 Node* phi = new PhiNode(region, TypeInt::BOOL);
4242 record_for_igvn(region);
4243
4244 const TypePtr* adr_type = TypeRawPtr::BOTTOM; // memory type of loads
4245 const TypeKlassPtr* kls_type = TypeInstKlassPtr::OBJECT_OR_NULL;
4246 int class_klass_offset = java_lang_Class::klass_offset();
4247
4248 // First null-check both mirrors and load each mirror's klass metaobject.
4249 int which_arg;
4250 for (which_arg = 0; which_arg <= 1; which_arg++) {
4251 Node* arg = args[which_arg];
4252 arg = null_check(arg);
4253 if (stopped()) break;
4254 args[which_arg] = arg;
4255
4256 Node* p = basic_plus_adr(arg, class_klass_offset);
4257 Node* kls = LoadKlassNode::make(_gvn, immutable_memory(), p, adr_type, kls_type);
4258 klasses[which_arg] = _gvn.transform(kls);
4259 }
4260
4261 // Having loaded both klasses, test each for null.
4262 bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
4263 for (which_arg = 0; which_arg <= 1; which_arg++) {
4264 Node* kls = klasses[which_arg];
4265 Node* null_ctl = top();
4266 kls = null_check_oop(kls, &null_ctl, never_see_null);
4267 int prim_path = (which_arg == 0 ? _prim_0_path : _prim_1_path);
4268 region->init_req(prim_path, null_ctl);
4269 if (stopped()) break;
4270 klasses[which_arg] = kls;
4271 }
4272
4273 if (!stopped()) {
4274 // now we have two reference types, in klasses[0..1]
4275 Node* subk = klasses[1]; // the argument to isAssignableFrom
4276 Node* superk = klasses[0]; // the receiver
4277 region->set_req(_both_ref_path, gen_subtype_check(subk, superk));
4278 // now we have a successful reference subtype check
4279 region->set_req(_ref_subtype_path, control());
4280 }
4281
4282 // If both operands are primitive (both klasses null), then
4283 // we must return true when they are identical primitives.
4284 // It is convenient to test this after the first null klass check.
4285 set_control(region->in(_prim_0_path)); // go back to first null check
4286 if (!stopped()) {
4287 // Since superc is primitive, make a guard for the superc==subc case.
4288 Node* cmp_eq = _gvn.transform(new CmpPNode(args[0], args[1]));
4289 Node* bol_eq = _gvn.transform(new BoolNode(cmp_eq, BoolTest::eq));
4290 generate_guard(bol_eq, region, PROB_FAIR);
4291 if (region->req() == PATH_LIMIT+1) {
4292 // A guard was added. If the added guard is taken, superc==subc.
4293 region->swap_edges(PATH_LIMIT, _prim_same_path);
4294 region->del_req(PATH_LIMIT);
4295 }
4296 region->set_req(_prim_0_path, control()); // Not equal after all.
4297 }
4298
4299 // these are the only paths that produce 'true':
4300 phi->set_req(_prim_same_path, intcon(1));
4301 phi->set_req(_ref_subtype_path, intcon(1));
4302
4303 // pull together the cases:
4304 assert(region->req() == PATH_LIMIT, "sane region");
4305 for (uint i = 1; i < region->req(); i++) {
4306 Node* ctl = region->in(i);
4307 if (ctl == nullptr || ctl == top()) {
4308 region->set_req(i, top());
4309 phi ->set_req(i, top());
4310 } else if (phi->in(i) == nullptr) {
4311 phi->set_req(i, intcon(0)); // all other paths produce 'false'
4312 }
4313 }
4314
4315 set_control(_gvn.transform(region));
4316 set_result(_gvn.transform(phi));
4317 return true;
4318 }
4319
4320 //---------------------generate_array_guard_common------------------------
4321 Node* LibraryCallKit::generate_array_guard_common(Node* kls, RegionNode* region,
4322 bool obj_array, bool not_array, Node** obj) {
4323
4324 if (stopped()) {
4325 return nullptr;
4326 }
4327
4328 // If obj_array/non_array==false/false:
4329 // Branch around if the given klass is in fact an array (either obj or prim).
4330 // If obj_array/non_array==false/true:
4331 // Branch around if the given klass is not an array klass of any kind.
4332 // If obj_array/non_array==true/true:
4333 // Branch around if the kls is not an oop array (kls is int[], String, etc.)
4334 // If obj_array/non_array==true/false:
4335 // Branch around if the kls is an oop array (Object[] or subtype)
4336 //
4337 // Like generate_guard, adds a new path onto the region.
4338 jint layout_con = 0;
4339 Node* layout_val = get_layout_helper(kls, layout_con);
4340 if (layout_val == nullptr) {
4341 bool query = (obj_array
4342 ? Klass::layout_helper_is_objArray(layout_con)
4343 : Klass::layout_helper_is_array(layout_con));
4344 if (query == not_array) {
4345 return nullptr; // never a branch
4346 } else { // always a branch
4347 Node* always_branch = control();
4348 if (region != nullptr)
4349 region->add_req(always_branch);
4350 set_control(top());
4351 return always_branch;
4352 }
4353 }
4354 // Now test the correct condition.
4355 jint nval = (obj_array
4356 ? (jint)(Klass::_lh_array_tag_type_value
4357 << Klass::_lh_array_tag_shift)
4358 : Klass::_lh_neutral_value);
4359 Node* cmp = _gvn.transform(new CmpINode(layout_val, intcon(nval)));
4360 BoolTest::mask btest = BoolTest::lt; // correct for testing is_[obj]array
4361 // invert the test if we are looking for a non-array
4362 if (not_array) btest = BoolTest(btest).negate();
4363 Node* bol = _gvn.transform(new BoolNode(cmp, btest));
4364 Node* ctrl = generate_fair_guard(bol, region);
4365 Node* is_array_ctrl = not_array ? control() : ctrl;
4366 if (obj != nullptr && is_array_ctrl != nullptr && is_array_ctrl != top()) {
4367 // Keep track of the fact that 'obj' is an array to prevent
4368 // array specific accesses from floating above the guard.
4369 *obj = _gvn.transform(new CastPPNode(is_array_ctrl, *obj, TypeAryPtr::BOTTOM));
4370 }
4371 return ctrl;
4372 }
4373
4374
4375 //-----------------------inline_native_newArray--------------------------
4376 // private static native Object java.lang.reflect.newArray(Class<?> componentType, int length);
4377 // private native Object Unsafe.allocateUninitializedArray0(Class<?> cls, int size);
4378 bool LibraryCallKit::inline_unsafe_newArray(bool uninitialized) {
4379 Node* mirror;
4380 Node* count_val;
4381 if (uninitialized) {
4382 null_check_receiver();
4383 mirror = argument(1);
4384 count_val = argument(2);
4385 } else {
4386 mirror = argument(0);
4387 count_val = argument(1);
4388 }
4389
4390 mirror = null_check(mirror);
4391 // If mirror or obj is dead, only null-path is taken.
4392 if (stopped()) return true;
4393
4394 enum { _normal_path = 1, _slow_path = 2, PATH_LIMIT };
4395 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
4396 PhiNode* result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
4414 CallJavaNode* slow_call = nullptr;
4415 if (uninitialized) {
4416 // Generate optimized virtual call (holder class 'Unsafe' is final)
4417 slow_call = generate_method_call(vmIntrinsics::_allocateUninitializedArray, false, false, true);
4418 } else {
4419 slow_call = generate_method_call_static(vmIntrinsics::_newArray, true);
4420 }
4421 Node* slow_result = set_results_for_java_call(slow_call);
4422 // this->control() comes from set_results_for_java_call
4423 result_reg->set_req(_slow_path, control());
4424 result_val->set_req(_slow_path, slow_result);
4425 result_io ->set_req(_slow_path, i_o());
4426 result_mem->set_req(_slow_path, reset_memory());
4427 }
4428
4429 set_control(normal_ctl);
4430 if (!stopped()) {
4431 // Normal case: The array type has been cached in the java.lang.Class.
4432 // The following call works fine even if the array type is polymorphic.
4433 // It could be a dynamic mix of int[], boolean[], Object[], etc.
4434 Node* obj = new_array(klass_node, count_val, 0); // no arguments to push
4435 result_reg->init_req(_normal_path, control());
4436 result_val->init_req(_normal_path, obj);
4437 result_io ->init_req(_normal_path, i_o());
4438 result_mem->init_req(_normal_path, reset_memory());
4439
4440 if (uninitialized) {
4441 // Mark the allocation so that zeroing is skipped
4442 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(obj);
4443 alloc->maybe_set_complete(&_gvn);
4444 }
4445 }
4446
4447 // Return the combined state.
4448 set_i_o( _gvn.transform(result_io) );
4449 set_all_memory( _gvn.transform(result_mem));
4450
4451 C->set_has_split_ifs(true); // Has chance for split-if optimization
4452 set_result(result_reg, result_val);
4453 return true;
4502 // the bytecode that invokes Arrays.copyOf if deoptimization happens.
4503 { PreserveReexecuteState preexecs(this);
4504 jvms()->set_should_reexecute(true);
4505
4506 array_type_mirror = null_check(array_type_mirror);
4507 original = null_check(original);
4508
4509 // Check if a null path was taken unconditionally.
4510 if (stopped()) return true;
4511
4512 Node* orig_length = load_array_length(original);
4513
4514 Node* klass_node = load_klass_from_mirror(array_type_mirror, false, nullptr, 0);
4515 klass_node = null_check(klass_node);
4516
4517 RegionNode* bailout = new RegionNode(1);
4518 record_for_igvn(bailout);
4519
4520 // Despite the generic type of Arrays.copyOf, the mirror might be int, int[], etc.
4521 // Bail out if that is so.
4522 Node* not_objArray = generate_non_objArray_guard(klass_node, bailout);
4523 if (not_objArray != nullptr) {
4524 // Improve the klass node's type from the new optimistic assumption:
4525 ciKlass* ak = ciArrayKlass::make(env()->Object_klass());
4526 const Type* akls = TypeKlassPtr::make(TypePtr::NotNull, ak, 0/*offset*/);
4527 Node* cast = new CastPPNode(control(), klass_node, akls);
4528 klass_node = _gvn.transform(cast);
4529 }
4530
4531 // Bail out if either start or end is negative.
4532 generate_negative_guard(start, bailout, &start);
4533 generate_negative_guard(end, bailout, &end);
4534
4535 Node* length = end;
4536 if (_gvn.type(start) != TypeInt::ZERO) {
4537 length = _gvn.transform(new SubINode(end, start));
4538 }
4539
4540 // Bail out if length is negative (i.e., if start > end).
4541 // Without this the new_array would throw
4542 // NegativeArraySizeException but IllegalArgumentException is what
4543 // should be thrown
4544 generate_negative_guard(length, bailout, &length);
4545
4546 // Bail out if start is larger than the original length
4547 Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
4548 generate_negative_guard(orig_tail, bailout, &orig_tail);
4549
4550 if (bailout->req() > 1) {
4551 PreserveJVMState pjvms(this);
4552 set_control(_gvn.transform(bailout));
4553 uncommon_trap(Deoptimization::Reason_intrinsic,
4554 Deoptimization::Action_maybe_recompile);
4555 }
4556
4557 if (!stopped()) {
4558 // How many elements will we copy from the original?
4559 // The answer is MinI(orig_tail, length).
4560 Node* moved = _gvn.transform(new MinINode(orig_tail, length));
4561
4562 // Generate a direct call to the right arraycopy function(s).
4563 // We know the copy is disjoint but we might not know if the
4564 // oop stores need checking.
4565 // Extreme case: Arrays.copyOf((Integer[])x, 10, String[].class).
4571 // to the copyOf to be validated, including that the copy to the
4572 // new array won't trigger an ArrayStoreException. That subtype
4573 // check can be optimized if we know something on the type of
4574 // the input array from type speculation.
4575 if (_gvn.type(klass_node)->singleton()) {
4576 const TypeKlassPtr* subk = _gvn.type(load_object_klass(original))->is_klassptr();
4577 const TypeKlassPtr* superk = _gvn.type(klass_node)->is_klassptr();
4578
4579 int test = C->static_subtype_check(superk, subk);
4580 if (test != Compile::SSC_always_true && test != Compile::SSC_always_false) {
4581 const TypeOopPtr* t_original = _gvn.type(original)->is_oopptr();
4582 if (t_original->speculative_type() != nullptr) {
4583 original = maybe_cast_profiled_obj(original, t_original->speculative_type(), true);
4584 }
4585 }
4586 }
4587
4588 bool validated = false;
4589 // Reason_class_check rather than Reason_intrinsic because we
4590 // want to intrinsify even if this traps.
4591 if (!too_many_traps(Deoptimization::Reason_class_check)) {
4592 Node* not_subtype_ctrl = gen_subtype_check(original, klass_node);
4593
4594 if (not_subtype_ctrl != top()) {
4595 PreserveJVMState pjvms(this);
4596 set_control(not_subtype_ctrl);
4597 uncommon_trap(Deoptimization::Reason_class_check,
4598 Deoptimization::Action_make_not_entrant);
4599 assert(stopped(), "Should be stopped");
4600 }
4601 validated = true;
4602 }
4603
4604 if (!stopped()) {
4605 newcopy = new_array(klass_node, length, 0); // no arguments to push
4606
4607 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, true, true,
4608 load_object_klass(original), klass_node);
4609 if (!is_copyOfRange) {
4610 ac->set_copyof(validated);
4611 } else {
4657
4658 //-----------------------generate_method_call----------------------------
4659 // Use generate_method_call to make a slow-call to the real
4660 // method if the fast path fails. An alternative would be to
4661 // use a stub like OptoRuntime::slow_arraycopy_Java.
4662 // This only works for expanding the current library call,
4663 // not another intrinsic. (E.g., don't use this for making an
4664 // arraycopy call inside of the copyOf intrinsic.)
4665 CallJavaNode*
4666 LibraryCallKit::generate_method_call(vmIntrinsicID method_id, bool is_virtual, bool is_static, bool res_not_null) {
4667 // When compiling the intrinsic method itself, do not use this technique.
4668 guarantee(callee() != C->method(), "cannot make slow-call to self");
4669
4670 ciMethod* method = callee();
4671 // ensure the JVMS we have will be correct for this call
4672 guarantee(method_id == method->intrinsic_id(), "must match");
4673
4674 const TypeFunc* tf = TypeFunc::make(method);
4675 if (res_not_null) {
4676 assert(tf->return_type() == T_OBJECT, "");
4677 const TypeTuple* range = tf->range();
4678 const Type** fields = TypeTuple::fields(range->cnt());
4679 fields[TypeFunc::Parms] = range->field_at(TypeFunc::Parms)->filter_speculative(TypePtr::NOTNULL);
4680 const TypeTuple* new_range = TypeTuple::make(range->cnt(), fields);
4681 tf = TypeFunc::make(tf->domain(), new_range);
4682 }
4683 CallJavaNode* slow_call;
4684 if (is_static) {
4685 assert(!is_virtual, "");
4686 slow_call = new CallStaticJavaNode(C, tf,
4687 SharedRuntime::get_resolve_static_call_stub(), method);
4688 } else if (is_virtual) {
4689 assert(!gvn().type(argument(0))->maybe_null(), "should not be null");
4690 int vtable_index = Method::invalid_vtable_index;
4691 if (UseInlineCaches) {
4692 // Suppress the vtable call
4693 } else {
4694 // hashCode and clone are not a miranda methods,
4695 // so the vtable index is fixed.
4696 // No need to use the linkResolver to get it.
4697 vtable_index = method->vtable_index();
4698 assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
4699 "bad index %d", vtable_index);
4700 }
4701 slow_call = new CallDynamicJavaNode(tf,
4718 set_edges_for_java_call(slow_call);
4719 return slow_call;
4720 }
4721
4722
4723 /**
4724 * Build special case code for calls to hashCode on an object. This call may
4725 * be virtual (invokevirtual) or bound (invokespecial). For each case we generate
4726 * slightly different code.
4727 */
4728 bool LibraryCallKit::inline_native_hashcode(bool is_virtual, bool is_static) {
4729 assert(is_static == callee()->is_static(), "correct intrinsic selection");
4730 assert(!(is_virtual && is_static), "either virtual, special, or static");
4731
4732 enum { _slow_path = 1, _fast_path, _null_path, PATH_LIMIT };
4733
4734 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
4735 PhiNode* result_val = new PhiNode(result_reg, TypeInt::INT);
4736 PhiNode* result_io = new PhiNode(result_reg, Type::ABIO);
4737 PhiNode* result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
4738 Node* obj = nullptr;
4739 if (!is_static) {
4740 // Check for hashing null object
4741 obj = null_check_receiver();
4742 if (stopped()) return true; // unconditionally null
4743 result_reg->init_req(_null_path, top());
4744 result_val->init_req(_null_path, top());
4745 } else {
4746 // Do a null check, and return zero if null.
4747 // System.identityHashCode(null) == 0
4748 obj = argument(0);
4749 Node* null_ctl = top();
4750 obj = null_check_oop(obj, &null_ctl);
4751 result_reg->init_req(_null_path, null_ctl);
4752 result_val->init_req(_null_path, _gvn.intcon(0));
4753 }
4754
4755 // Unconditionally null? Then return right away.
4756 if (stopped()) {
4757 set_control( result_reg->in(_null_path));
4758 if (!stopped())
4759 set_result(result_val->in(_null_path));
4760 return true;
4761 }
4762
4763 // We only go to the fast case code if we pass a number of guards. The
4764 // paths which do not pass are accumulated in the slow_region.
4765 RegionNode* slow_region = new RegionNode(1);
4766 record_for_igvn(slow_region);
4767
4768 // If this is a virtual call, we generate a funny guard. We pull out
4769 // the vtable entry corresponding to hashCode() from the target object.
4770 // If the target method which we are calling happens to be the native
4771 // Object hashCode() method, we pass the guard. We do not need this
4772 // guard for non-virtual calls -- the caller is known to be the native
4773 // Object hashCode().
4774 if (is_virtual) {
4775 // After null check, get the object's klass.
4776 Node* obj_klass = load_object_klass(obj);
4777 generate_virtual_guard(obj_klass, slow_region);
4778 }
4779
4780 // Get the header out of the object, use LoadMarkNode when available
4781 Node* header_addr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes());
4782 // The control of the load must be null. Otherwise, the load can move before
4783 // the null check after castPP removal.
4784 Node* no_ctrl = nullptr;
4785 Node* header = make_load(no_ctrl, header_addr, TypeX_X, TypeX_X->basic_type(), MemNode::unordered);
4786
4787 if (!UseObjectMonitorTable) {
4788 // Test the header to see if it is safe to read w.r.t. locking.
4789 Node *lock_mask = _gvn.MakeConX(markWord::lock_mask_in_place);
4790 Node *lmasked_header = _gvn.transform(new AndXNode(header, lock_mask));
4791 if (LockingMode == LM_LIGHTWEIGHT) {
4792 Node *monitor_val = _gvn.MakeConX(markWord::monitor_value);
4793 Node *chk_monitor = _gvn.transform(new CmpXNode(lmasked_header, monitor_val));
4794 Node *test_monitor = _gvn.transform(new BoolNode(chk_monitor, BoolTest::eq));
4795
4796 generate_slow_guard(test_monitor, slow_region);
4797 } else {
4798 Node *unlocked_val = _gvn.MakeConX(markWord::unlocked_value);
4799 Node *chk_unlocked = _gvn.transform(new CmpXNode(lmasked_header, unlocked_val));
4800 Node *test_not_unlocked = _gvn.transform(new BoolNode(chk_unlocked, BoolTest::ne));
4801
4802 generate_slow_guard(test_not_unlocked, slow_region);
4803 }
4804 }
4805
4806 // Get the hash value and check to see that it has been properly assigned.
4807 // We depend on hash_mask being at most 32 bits and avoid the use of
4808 // hash_mask_in_place because it could be larger than 32 bits in a 64-bit
4809 // vm: see markWord.hpp.
4844 // this->control() comes from set_results_for_java_call
4845 result_reg->init_req(_slow_path, control());
4846 result_val->init_req(_slow_path, slow_result);
4847 result_io ->set_req(_slow_path, i_o());
4848 result_mem ->set_req(_slow_path, reset_memory());
4849 }
4850
4851 // Return the combined state.
4852 set_i_o( _gvn.transform(result_io) );
4853 set_all_memory( _gvn.transform(result_mem));
4854
4855 set_result(result_reg, result_val);
4856 return true;
4857 }
4858
4859 //---------------------------inline_native_getClass----------------------------
4860 // public final native Class<?> java.lang.Object.getClass();
4861 //
4862 // Build special case code for calls to getClass on an object.
4863 bool LibraryCallKit::inline_native_getClass() {
4864 Node* obj = null_check_receiver();
4865 if (stopped()) return true;
4866 set_result(load_mirror_from_klass(load_object_klass(obj)));
4867 return true;
4868 }
4869
4870 //-----------------inline_native_Reflection_getCallerClass---------------------
4871 // public static native Class<?> sun.reflect.Reflection.getCallerClass();
4872 //
4873 // In the presence of deep enough inlining, getCallerClass() becomes a no-op.
4874 //
4875 // NOTE: This code must perform the same logic as JVM_GetCallerClass
4876 // in that it must skip particular security frames and checks for
4877 // caller sensitive methods.
4878 bool LibraryCallKit::inline_native_Reflection_getCallerClass() {
4879 #ifndef PRODUCT
4880 if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
4881 tty->print_cr("Attempting to inline sun.reflect.Reflection.getCallerClass");
4882 }
4883 #endif
4884
5266 // not cloneable or finalizer => slow path to out-of-line Object.clone
5267 //
5268 // The general case has two steps, allocation and copying.
5269 // Allocation has two cases, and uses GraphKit::new_instance or new_array.
5270 //
5271 // Copying also has two cases, oop arrays and everything else.
5272 // Oop arrays use arrayof_oop_arraycopy (same as System.arraycopy).
5273 // Everything else uses the tight inline loop supplied by CopyArrayNode.
5274 //
5275 // These steps fold up nicely if and when the cloned object's klass
5276 // can be sharply typed as an object array, a type array, or an instance.
5277 //
5278 bool LibraryCallKit::inline_native_clone(bool is_virtual) {
5279 PhiNode* result_val;
5280
5281 // Set the reexecute bit for the interpreter to reexecute
5282 // the bytecode that invokes Object.clone if deoptimization happens.
5283 { PreserveReexecuteState preexecs(this);
5284 jvms()->set_should_reexecute(true);
5285
5286 Node* obj = null_check_receiver();
5287 if (stopped()) return true;
5288
5289 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
5290
5291 // If we are going to clone an instance, we need its exact type to
5292 // know the number and types of fields to convert the clone to
5293 // loads/stores. Maybe a speculative type can help us.
5294 if (!obj_type->klass_is_exact() &&
5295 obj_type->speculative_type() != nullptr &&
5296 obj_type->speculative_type()->is_instance_klass()) {
5297 ciInstanceKlass* spec_ik = obj_type->speculative_type()->as_instance_klass();
5298 if (spec_ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem &&
5299 !spec_ik->has_injected_fields()) {
5300 if (!obj_type->isa_instptr() ||
5301 obj_type->is_instptr()->instance_klass()->has_subklass()) {
5302 obj = maybe_cast_profiled_obj(obj, obj_type->speculative_type(), false);
5303 }
5304 }
5305 }
5306
5307 // Conservatively insert a memory barrier on all memory slices.
5308 // Do not let writes into the original float below the clone.
5309 insert_mem_bar(Op_MemBarCPUOrder);
5310
5311 // paths into result_reg:
5312 enum {
5313 _slow_path = 1, // out-of-line call to clone method (virtual or not)
5314 _objArray_path, // plain array allocation, plus arrayof_oop_arraycopy
5315 _array_path, // plain array allocation, plus arrayof_long_arraycopy
5316 _instance_path, // plain instance allocation, plus arrayof_long_arraycopy
5317 PATH_LIMIT
5318 };
5319 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
5320 result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
5321 PhiNode* result_i_o = new PhiNode(result_reg, Type::ABIO);
5322 PhiNode* result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
5323 record_for_igvn(result_reg);
5324
5325 Node* obj_klass = load_object_klass(obj);
5326 Node* array_obj = obj;
5327 Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)nullptr, &array_obj);
5328 if (array_ctl != nullptr) {
5329 // It's an array.
5330 PreserveJVMState pjvms(this);
5331 set_control(array_ctl);
5332 Node* obj_length = load_array_length(array_obj);
5333 Node* array_size = nullptr; // Size of the array without object alignment padding.
5334 Node* alloc_obj = new_array(obj_klass, obj_length, 0, &array_size, /*deoptimize_on_exception=*/true);
5335
5336 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
5337 if (bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, false, BarrierSetC2::Parsing)) {
5338 // If it is an oop array, it requires very special treatment,
5339 // because gc barriers are required when accessing the array.
5340 Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)nullptr);
5341 if (is_obja != nullptr) {
5342 PreserveJVMState pjvms2(this);
5343 set_control(is_obja);
5344 // Generate a direct call to the right arraycopy function(s).
5345 // Clones are always tightly coupled.
5346 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, array_obj, intcon(0), alloc_obj, intcon(0), obj_length, true, false);
5347 ac->set_clone_oop_array();
5348 Node* n = _gvn.transform(ac);
5349 assert(n == ac, "cannot disappear");
5350 ac->connect_outputs(this, /*deoptimize_on_exception=*/true);
5351
5352 result_reg->init_req(_objArray_path, control());
5353 result_val->init_req(_objArray_path, alloc_obj);
5354 result_i_o ->set_req(_objArray_path, i_o());
5355 result_mem ->set_req(_objArray_path, reset_memory());
5356 }
5357 }
5358 // Otherwise, there are no barriers to worry about.
5359 // (We can dispense with card marks if we know the allocation
5360 // comes out of eden (TLAB)... In fact, ReduceInitialCardMarks
5361 // causes the non-eden paths to take compensating steps to
5362 // simulate a fresh allocation, so that no further
5363 // card marks are required in compiled code to initialize
5364 // the object.)
5365
5366 if (!stopped()) {
5367 copy_to_clone(array_obj, alloc_obj, array_size, true);
5368
5369 // Present the results of the copy.
5370 result_reg->init_req(_array_path, control());
5371 result_val->init_req(_array_path, alloc_obj);
5372 result_i_o ->set_req(_array_path, i_o());
5373 result_mem ->set_req(_array_path, reset_memory());
5374 }
5375 }
5376
5377 // We only go to the instance fast case code if we pass a number of guards.
5378 // The paths which do not pass are accumulated in the slow_region.
5379 RegionNode* slow_region = new RegionNode(1);
5380 record_for_igvn(slow_region);
5381 if (!stopped()) {
5382 // It's an instance (we did array above). Make the slow-path tests.
5383 // If this is a virtual call, we generate a funny guard. We grab
5384 // the vtable entry corresponding to clone() from the target object.
5385 // If the target method which we are calling happens to be the
5386 // Object clone() method, we pass the guard. We do not need this
5387 // guard for non-virtual calls; the caller is known to be the native
5388 // Object clone().
5389 if (is_virtual) {
5390 generate_virtual_guard(obj_klass, slow_region);
5391 }
5392
5393 // The object must be easily cloneable and must not have a finalizer.
5394 // Both of these conditions may be checked in a single test.
5395 // We could optimize the test further, but we don't care.
5396 generate_misc_flags_guard(obj_klass,
5397 // Test both conditions:
5398 KlassFlags::_misc_is_cloneable_fast | KlassFlags::_misc_has_finalizer,
5399 // Must be cloneable but not finalizer:
5400 KlassFlags::_misc_is_cloneable_fast,
5492 set_jvms(sfpt->jvms());
5493 _reexecute_sp = jvms()->sp();
5494
5495 return saved_jvms;
5496 }
5497 }
5498 }
5499 return nullptr;
5500 }
5501
5502 // Clone the JVMState of the array allocation and create a new safepoint with it. Re-push the array length to the stack
5503 // such that uncommon traps can be emitted to re-execute the array allocation in the interpreter.
5504 SafePointNode* LibraryCallKit::create_safepoint_with_state_before_array_allocation(const AllocateArrayNode* alloc) const {
5505 JVMState* old_jvms = alloc->jvms()->clone_shallow(C);
5506 uint size = alloc->req();
5507 SafePointNode* sfpt = new SafePointNode(size, old_jvms);
5508 old_jvms->set_map(sfpt);
5509 for (uint i = 0; i < size; i++) {
5510 sfpt->init_req(i, alloc->in(i));
5511 }
5512 // re-push array length for deoptimization
5513 sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp(), alloc->in(AllocateNode::ALength));
5514 old_jvms->set_sp(old_jvms->sp()+1);
5515 old_jvms->set_monoff(old_jvms->monoff()+1);
5516 old_jvms->set_scloff(old_jvms->scloff()+1);
5517 old_jvms->set_endoff(old_jvms->endoff()+1);
5518 old_jvms->set_should_reexecute(true);
5519
5520 sfpt->set_i_o(map()->i_o());
5521 sfpt->set_memory(map()->memory());
5522 sfpt->set_control(map()->control());
5523 return sfpt;
5524 }
5525
5526 // In case of a deoptimization, we restart execution at the
5527 // allocation, allocating a new array. We would leave an uninitialized
5528 // array in the heap that GCs wouldn't expect. Move the allocation
5529 // after the traps so we don't allocate the array if we
5530 // deoptimize. This is possible because tightly_coupled_allocation()
5531 // guarantees there's no observer of the allocated array at this point
5532 // and the control flow is simple enough.
5533 void LibraryCallKit::arraycopy_move_allocation_here(AllocateArrayNode* alloc, Node* dest, JVMState* saved_jvms_before_guards,
5534 int saved_reexecute_sp, uint new_idx) {
5535 if (saved_jvms_before_guards != nullptr && !stopped()) {
5536 replace_unrelated_uncommon_traps_with_alloc_state(alloc, saved_jvms_before_guards);
5537
5538 assert(alloc != nullptr, "only with a tightly coupled allocation");
5539 // restore JVM state to the state at the arraycopy
5540 saved_jvms_before_guards->map()->set_control(map()->control());
5541 assert(saved_jvms_before_guards->map()->memory() == map()->memory(), "memory state changed?");
5542 assert(saved_jvms_before_guards->map()->i_o() == map()->i_o(), "IO state changed?");
5543 // If we've improved the types of some nodes (null check) while
5544 // emitting the guards, propagate them to the current state
5545 map()->replaced_nodes().apply(saved_jvms_before_guards->map(), new_idx);
5546 set_jvms(saved_jvms_before_guards);
5547 _reexecute_sp = saved_reexecute_sp;
5548
5549 // Remove the allocation from above the guards
5550 CallProjections callprojs;
5551 alloc->extract_projections(&callprojs, true);
5552 InitializeNode* init = alloc->initialization();
5553 Node* alloc_mem = alloc->in(TypeFunc::Memory);
5554 C->gvn_replace_by(callprojs.fallthrough_ioproj, alloc->in(TypeFunc::I_O));
5555 C->gvn_replace_by(init->proj_out(TypeFunc::Memory), alloc_mem);
5556
5557 // The CastIINode created in GraphKit::new_array (in AllocateArrayNode::make_ideal_length) must stay below
5558 // the allocation (i.e. is only valid if the allocation succeeds):
5559 // 1) replace CastIINode with AllocateArrayNode's length here
5560 // 2) Create CastIINode again once allocation has moved (see below) at the end of this method
5561 //
5562 // Multiple identical CastIINodes might exist here. Each GraphKit::load_array_length() call will generate
5563 // new separate CastIINode (arraycopy guard checks or any array length use between array allocation and ararycopy)
5564 Node* init_control = init->proj_out(TypeFunc::Control);
5565 Node* alloc_length = alloc->Ideal_length();
5566 #ifdef ASSERT
5567 Node* prev_cast = nullptr;
5568 #endif
5569 for (uint i = 0; i < init_control->outcnt(); i++) {
5570 Node* init_out = init_control->raw_out(i);
5571 if (init_out->is_CastII() && init_out->in(TypeFunc::Control) == init_control && init_out->in(1) == alloc_length) {
5572 #ifdef ASSERT
5573 if (prev_cast == nullptr) {
5574 prev_cast = init_out;
5576 if (prev_cast->cmp(*init_out) == false) {
5577 prev_cast->dump();
5578 init_out->dump();
5579 assert(false, "not equal CastIINode");
5580 }
5581 }
5582 #endif
5583 C->gvn_replace_by(init_out, alloc_length);
5584 }
5585 }
5586 C->gvn_replace_by(init->proj_out(TypeFunc::Control), alloc->in(0));
5587
5588 // move the allocation here (after the guards)
5589 _gvn.hash_delete(alloc);
5590 alloc->set_req(TypeFunc::Control, control());
5591 alloc->set_req(TypeFunc::I_O, i_o());
5592 Node *mem = reset_memory();
5593 set_all_memory(mem);
5594 alloc->set_req(TypeFunc::Memory, mem);
5595 set_control(init->proj_out_or_null(TypeFunc::Control));
5596 set_i_o(callprojs.fallthrough_ioproj);
5597
5598 // Update memory as done in GraphKit::set_output_for_allocation()
5599 const TypeInt* length_type = _gvn.find_int_type(alloc->in(AllocateNode::ALength));
5600 const TypeOopPtr* ary_type = _gvn.type(alloc->in(AllocateNode::KlassNode))->is_klassptr()->as_instance_type();
5601 if (ary_type->isa_aryptr() && length_type != nullptr) {
5602 ary_type = ary_type->is_aryptr()->cast_to_size(length_type);
5603 }
5604 const TypePtr* telemref = ary_type->add_offset(Type::OffsetBot);
5605 int elemidx = C->get_alias_index(telemref);
5606 set_memory(init->proj_out_or_null(TypeFunc::Memory), Compile::AliasIdxRaw);
5607 set_memory(init->proj_out_or_null(TypeFunc::Memory), elemidx);
5608
5609 Node* allocx = _gvn.transform(alloc);
5610 assert(allocx == alloc, "where has the allocation gone?");
5611 assert(dest->is_CheckCastPP(), "not an allocation result?");
5612
5613 _gvn.hash_delete(dest);
5614 dest->set_req(0, control());
5615 Node* destx = _gvn.transform(dest);
5616 assert(destx == dest, "where has the allocation result gone?");
5914 top_src = src_type->isa_aryptr();
5915 has_src = (top_src != nullptr && top_src->elem() != Type::BOTTOM);
5916 src_spec = true;
5917 }
5918 if (!has_dest) {
5919 dest = maybe_cast_profiled_obj(dest, dest_k, true);
5920 dest_type = _gvn.type(dest);
5921 top_dest = dest_type->isa_aryptr();
5922 has_dest = (top_dest != nullptr && top_dest->elem() != Type::BOTTOM);
5923 dest_spec = true;
5924 }
5925 }
5926 }
5927
5928 if (has_src && has_dest && can_emit_guards) {
5929 BasicType src_elem = top_src->isa_aryptr()->elem()->array_element_basic_type();
5930 BasicType dest_elem = top_dest->isa_aryptr()->elem()->array_element_basic_type();
5931 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
5932 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
5933
5934 if (src_elem == dest_elem && src_elem == T_OBJECT) {
5935 // If both arrays are object arrays then having the exact types
5936 // for both will remove the need for a subtype check at runtime
5937 // before the call and may make it possible to pick a faster copy
5938 // routine (without a subtype check on every element)
5939 // Do we have the exact type of src?
5940 bool could_have_src = src_spec;
5941 // Do we have the exact type of dest?
5942 bool could_have_dest = dest_spec;
5943 ciKlass* src_k = nullptr;
5944 ciKlass* dest_k = nullptr;
5945 if (!src_spec) {
5946 src_k = src_type->speculative_type_not_null();
5947 if (src_k != nullptr && src_k->is_array_klass()) {
5948 could_have_src = true;
5949 }
5950 }
5951 if (!dest_spec) {
5952 dest_k = dest_type->speculative_type_not_null();
5953 if (dest_k != nullptr && dest_k->is_array_klass()) {
5954 could_have_dest = true;
5955 }
5956 }
5957 if (could_have_src && could_have_dest) {
5958 // If we can have both exact types, emit the missing guards
5959 if (could_have_src && !src_spec) {
5960 src = maybe_cast_profiled_obj(src, src_k, true);
5961 }
5962 if (could_have_dest && !dest_spec) {
5963 dest = maybe_cast_profiled_obj(dest, dest_k, true);
5964 }
5965 }
5966 }
5967 }
5968
5969 ciMethod* trap_method = method();
5970 int trap_bci = bci();
5971 if (saved_jvms_before_guards != nullptr) {
5972 trap_method = alloc->jvms()->method();
5973 trap_bci = alloc->jvms()->bci();
5974 }
5975
5976 bool negative_length_guard_generated = false;
5977
5978 if (!C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_intrinsic) &&
5979 can_emit_guards &&
5980 !src->is_top() && !dest->is_top()) {
5981 // validate arguments: enables transformation the ArrayCopyNode
5982 validated = true;
5983
5984 RegionNode* slow_region = new RegionNode(1);
5985 record_for_igvn(slow_region);
5986
5987 // (1) src and dest are arrays.
5988 generate_non_array_guard(load_object_klass(src), slow_region, &src);
5989 generate_non_array_guard(load_object_klass(dest), slow_region, &dest);
5990
5991 // (2) src and dest arrays must have elements of the same BasicType
5992 // done at macro expansion or at Ideal transformation time
5993
5994 // (4) src_offset must not be negative.
5995 generate_negative_guard(src_offset, slow_region);
5996
5997 // (5) dest_offset must not be negative.
5998 generate_negative_guard(dest_offset, slow_region);
5999
6000 // (7) src_offset + length must not exceed length of src.
6003 slow_region);
6004
6005 // (8) dest_offset + length must not exceed length of dest.
6006 generate_limit_guard(dest_offset, length,
6007 load_array_length(dest),
6008 slow_region);
6009
6010 // (6) length must not be negative.
6011 // This is also checked in generate_arraycopy() during macro expansion, but
6012 // we also have to check it here for the case where the ArrayCopyNode will
6013 // be eliminated by Escape Analysis.
6014 if (EliminateAllocations) {
6015 generate_negative_guard(length, slow_region);
6016 negative_length_guard_generated = true;
6017 }
6018
6019 // (9) each element of an oop array must be assignable
6020 Node* dest_klass = load_object_klass(dest);
6021 if (src != dest) {
6022 Node* not_subtype_ctrl = gen_subtype_check(src, dest_klass);
6023
6024 if (not_subtype_ctrl != top()) {
6025 PreserveJVMState pjvms(this);
6026 set_control(not_subtype_ctrl);
6027 uncommon_trap(Deoptimization::Reason_intrinsic,
6028 Deoptimization::Action_make_not_entrant);
6029 assert(stopped(), "Should be stopped");
6030 }
6031 }
6032 {
6033 PreserveJVMState pjvms(this);
6034 set_control(_gvn.transform(slow_region));
6035 uncommon_trap(Deoptimization::Reason_intrinsic,
6036 Deoptimization::Action_make_not_entrant);
6037 assert(stopped(), "Should be stopped");
6038 }
6039
6040 const TypeKlassPtr* dest_klass_t = _gvn.type(dest_klass)->is_klassptr();
6041 const Type *toop = dest_klass_t->cast_to_exactness(false)->as_instance_type();
6042 src = _gvn.transform(new CheckCastPPNode(control(), src, toop));
6043 arraycopy_move_allocation_here(alloc, dest, saved_jvms_before_guards, saved_reexecute_sp, new_idx);
6044 }
6045
6046 if (stopped()) {
6047 return true;
6048 }
6049
6050 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, src, src_offset, dest, dest_offset, length, alloc != nullptr, negative_length_guard_generated,
6051 // Create LoadRange and LoadKlass nodes for use during macro expansion here
6052 // so the compiler has a chance to eliminate them: during macro expansion,
6053 // we have to set their control (CastPP nodes are eliminated).
6054 load_object_klass(src), load_object_klass(dest),
6055 load_array_length(src), load_array_length(dest));
6056
6057 ac->set_arraycopy(validated);
6058
6059 Node* n = _gvn.transform(ac);
6060 if (n == ac) {
6061 ac->connect_outputs(this);
6062 } 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.
312 case vmIntrinsics::_indexOfIL: return inline_string_indexOfI(StrIntrinsicNode::LL);
313 case vmIntrinsics::_indexOfIU: return inline_string_indexOfI(StrIntrinsicNode::UU);
314 case vmIntrinsics::_indexOfIUL: return inline_string_indexOfI(StrIntrinsicNode::UL);
315 case vmIntrinsics::_indexOfU_char: return inline_string_indexOfChar(StrIntrinsicNode::U);
316 case vmIntrinsics::_indexOfL_char: return inline_string_indexOfChar(StrIntrinsicNode::L);
317
318 case vmIntrinsics::_equalsL: return inline_string_equals(StrIntrinsicNode::LL);
319
320 case vmIntrinsics::_vectorizedHashCode: return inline_vectorizedHashCode();
321
322 case vmIntrinsics::_toBytesStringU: return inline_string_toBytesU();
323 case vmIntrinsics::_getCharsStringU: return inline_string_getCharsU();
324 case vmIntrinsics::_getCharStringU: return inline_string_char_access(!is_store);
325 case vmIntrinsics::_putCharStringU: return inline_string_char_access( is_store);
326
327 case vmIntrinsics::_compressStringC:
328 case vmIntrinsics::_compressStringB: return inline_string_copy( is_compress);
329 case vmIntrinsics::_inflateStringC:
330 case vmIntrinsics::_inflateStringB: return inline_string_copy(!is_compress);
331
332 case vmIntrinsics::_makePrivateBuffer: return inline_unsafe_make_private_buffer();
333 case vmIntrinsics::_finishPrivateBuffer: return inline_unsafe_finish_private_buffer();
334 case vmIntrinsics::_getReference: return inline_unsafe_access(!is_store, T_OBJECT, Relaxed, false);
335 case vmIntrinsics::_getBoolean: return inline_unsafe_access(!is_store, T_BOOLEAN, Relaxed, false);
336 case vmIntrinsics::_getByte: return inline_unsafe_access(!is_store, T_BYTE, Relaxed, false);
337 case vmIntrinsics::_getShort: return inline_unsafe_access(!is_store, T_SHORT, Relaxed, false);
338 case vmIntrinsics::_getChar: return inline_unsafe_access(!is_store, T_CHAR, Relaxed, false);
339 case vmIntrinsics::_getInt: return inline_unsafe_access(!is_store, T_INT, Relaxed, false);
340 case vmIntrinsics::_getLong: return inline_unsafe_access(!is_store, T_LONG, Relaxed, false);
341 case vmIntrinsics::_getFloat: return inline_unsafe_access(!is_store, T_FLOAT, Relaxed, false);
342 case vmIntrinsics::_getDouble: return inline_unsafe_access(!is_store, T_DOUBLE, Relaxed, false);
343 case vmIntrinsics::_getValue: return inline_unsafe_access(!is_store, T_OBJECT, Relaxed, false, true);
344
345 case vmIntrinsics::_putReference: return inline_unsafe_access( is_store, T_OBJECT, Relaxed, false);
346 case vmIntrinsics::_putBoolean: return inline_unsafe_access( is_store, T_BOOLEAN, Relaxed, false);
347 case vmIntrinsics::_putByte: return inline_unsafe_access( is_store, T_BYTE, Relaxed, false);
348 case vmIntrinsics::_putShort: return inline_unsafe_access( is_store, T_SHORT, Relaxed, false);
349 case vmIntrinsics::_putChar: return inline_unsafe_access( is_store, T_CHAR, Relaxed, false);
350 case vmIntrinsics::_putInt: return inline_unsafe_access( is_store, T_INT, Relaxed, false);
351 case vmIntrinsics::_putLong: return inline_unsafe_access( is_store, T_LONG, Relaxed, false);
352 case vmIntrinsics::_putFloat: return inline_unsafe_access( is_store, T_FLOAT, Relaxed, false);
353 case vmIntrinsics::_putDouble: return inline_unsafe_access( is_store, T_DOUBLE, Relaxed, false);
354 case vmIntrinsics::_putValue: return inline_unsafe_access( is_store, T_OBJECT, Relaxed, false, true);
355
356 case vmIntrinsics::_getReferenceVolatile: return inline_unsafe_access(!is_store, T_OBJECT, Volatile, false);
357 case vmIntrinsics::_getBooleanVolatile: return inline_unsafe_access(!is_store, T_BOOLEAN, Volatile, false);
358 case vmIntrinsics::_getByteVolatile: return inline_unsafe_access(!is_store, T_BYTE, Volatile, false);
359 case vmIntrinsics::_getShortVolatile: return inline_unsafe_access(!is_store, T_SHORT, Volatile, false);
360 case vmIntrinsics::_getCharVolatile: return inline_unsafe_access(!is_store, T_CHAR, Volatile, false);
361 case vmIntrinsics::_getIntVolatile: return inline_unsafe_access(!is_store, T_INT, Volatile, false);
362 case vmIntrinsics::_getLongVolatile: return inline_unsafe_access(!is_store, T_LONG, Volatile, false);
363 case vmIntrinsics::_getFloatVolatile: return inline_unsafe_access(!is_store, T_FLOAT, Volatile, false);
364 case vmIntrinsics::_getDoubleVolatile: return inline_unsafe_access(!is_store, T_DOUBLE, Volatile, false);
365
366 case vmIntrinsics::_putReferenceVolatile: return inline_unsafe_access( is_store, T_OBJECT, Volatile, false);
367 case vmIntrinsics::_putBooleanVolatile: return inline_unsafe_access( is_store, T_BOOLEAN, Volatile, false);
368 case vmIntrinsics::_putByteVolatile: return inline_unsafe_access( is_store, T_BYTE, Volatile, false);
369 case vmIntrinsics::_putShortVolatile: return inline_unsafe_access( is_store, T_SHORT, Volatile, false);
370 case vmIntrinsics::_putCharVolatile: return inline_unsafe_access( is_store, T_CHAR, Volatile, false);
371 case vmIntrinsics::_putIntVolatile: return inline_unsafe_access( is_store, T_INT, Volatile, false);
372 case vmIntrinsics::_putLongVolatile: return inline_unsafe_access( is_store, T_LONG, Volatile, false);
373 case vmIntrinsics::_putFloatVolatile: return inline_unsafe_access( is_store, T_FLOAT, Volatile, false);
374 case vmIntrinsics::_putDoubleVolatile: return inline_unsafe_access( is_store, T_DOUBLE, Volatile, false);
406 case vmIntrinsics::_getReferenceOpaque: return inline_unsafe_access(!is_store, T_OBJECT, Opaque, false);
407 case vmIntrinsics::_getBooleanOpaque: return inline_unsafe_access(!is_store, T_BOOLEAN, Opaque, false);
408 case vmIntrinsics::_getByteOpaque: return inline_unsafe_access(!is_store, T_BYTE, Opaque, false);
409 case vmIntrinsics::_getShortOpaque: return inline_unsafe_access(!is_store, T_SHORT, Opaque, false);
410 case vmIntrinsics::_getCharOpaque: return inline_unsafe_access(!is_store, T_CHAR, Opaque, false);
411 case vmIntrinsics::_getIntOpaque: return inline_unsafe_access(!is_store, T_INT, Opaque, false);
412 case vmIntrinsics::_getLongOpaque: return inline_unsafe_access(!is_store, T_LONG, Opaque, false);
413 case vmIntrinsics::_getFloatOpaque: return inline_unsafe_access(!is_store, T_FLOAT, Opaque, false);
414 case vmIntrinsics::_getDoubleOpaque: return inline_unsafe_access(!is_store, T_DOUBLE, Opaque, false);
415
416 case vmIntrinsics::_putReferenceOpaque: return inline_unsafe_access( is_store, T_OBJECT, Opaque, false);
417 case vmIntrinsics::_putBooleanOpaque: return inline_unsafe_access( is_store, T_BOOLEAN, Opaque, false);
418 case vmIntrinsics::_putByteOpaque: return inline_unsafe_access( is_store, T_BYTE, Opaque, false);
419 case vmIntrinsics::_putShortOpaque: return inline_unsafe_access( is_store, T_SHORT, Opaque, false);
420 case vmIntrinsics::_putCharOpaque: return inline_unsafe_access( is_store, T_CHAR, Opaque, false);
421 case vmIntrinsics::_putIntOpaque: return inline_unsafe_access( is_store, T_INT, Opaque, false);
422 case vmIntrinsics::_putLongOpaque: return inline_unsafe_access( is_store, T_LONG, Opaque, false);
423 case vmIntrinsics::_putFloatOpaque: return inline_unsafe_access( is_store, T_FLOAT, Opaque, false);
424 case vmIntrinsics::_putDoubleOpaque: return inline_unsafe_access( is_store, T_DOUBLE, Opaque, false);
425
426 case vmIntrinsics::_getFlatValue: return inline_unsafe_flat_access(!is_store, Relaxed);
427 case vmIntrinsics::_putFlatValue: return inline_unsafe_flat_access( is_store, Relaxed);
428
429 case vmIntrinsics::_compareAndSetReference: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap, Volatile);
430 case vmIntrinsics::_compareAndSetByte: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap, Volatile);
431 case vmIntrinsics::_compareAndSetShort: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap, Volatile);
432 case vmIntrinsics::_compareAndSetInt: return inline_unsafe_load_store(T_INT, LS_cmp_swap, Volatile);
433 case vmIntrinsics::_compareAndSetLong: return inline_unsafe_load_store(T_LONG, LS_cmp_swap, Volatile);
434
435 case vmIntrinsics::_weakCompareAndSetReferencePlain: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Relaxed);
436 case vmIntrinsics::_weakCompareAndSetReferenceAcquire: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Acquire);
437 case vmIntrinsics::_weakCompareAndSetReferenceRelease: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Release);
438 case vmIntrinsics::_weakCompareAndSetReference: return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Volatile);
439 case vmIntrinsics::_weakCompareAndSetBytePlain: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Relaxed);
440 case vmIntrinsics::_weakCompareAndSetByteAcquire: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Acquire);
441 case vmIntrinsics::_weakCompareAndSetByteRelease: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Release);
442 case vmIntrinsics::_weakCompareAndSetByte: return inline_unsafe_load_store(T_BYTE, LS_cmp_swap_weak, Volatile);
443 case vmIntrinsics::_weakCompareAndSetShortPlain: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Relaxed);
444 case vmIntrinsics::_weakCompareAndSetShortAcquire: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Acquire);
445 case vmIntrinsics::_weakCompareAndSetShortRelease: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Release);
446 case vmIntrinsics::_weakCompareAndSetShort: return inline_unsafe_load_store(T_SHORT, LS_cmp_swap_weak, Volatile);
447 case vmIntrinsics::_weakCompareAndSetIntPlain: return inline_unsafe_load_store(T_INT, LS_cmp_swap_weak, Relaxed);
448 case vmIntrinsics::_weakCompareAndSetIntAcquire: return inline_unsafe_load_store(T_INT, LS_cmp_swap_weak, Acquire);
516 #endif
517 case vmIntrinsics::_currentTimeMillis: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeMillis), "currentTimeMillis");
518 case vmIntrinsics::_nanoTime: return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeNanos), "nanoTime");
519 case vmIntrinsics::_writeback0: return inline_unsafe_writeback0();
520 case vmIntrinsics::_writebackPreSync0: return inline_unsafe_writebackSync0(true);
521 case vmIntrinsics::_writebackPostSync0: return inline_unsafe_writebackSync0(false);
522 case vmIntrinsics::_allocateInstance: return inline_unsafe_allocate();
523 case vmIntrinsics::_copyMemory: return inline_unsafe_copyMemory();
524 case vmIntrinsics::_setMemory: return inline_unsafe_setMemory();
525 case vmIntrinsics::_getLength: return inline_native_getLength();
526 case vmIntrinsics::_copyOf: return inline_array_copyOf(false);
527 case vmIntrinsics::_copyOfRange: return inline_array_copyOf(true);
528 case vmIntrinsics::_equalsB: return inline_array_equals(StrIntrinsicNode::LL);
529 case vmIntrinsics::_equalsC: return inline_array_equals(StrIntrinsicNode::UU);
530 case vmIntrinsics::_Preconditions_checkIndex: return inline_preconditions_checkIndex(T_INT);
531 case vmIntrinsics::_Preconditions_checkLongIndex: return inline_preconditions_checkIndex(T_LONG);
532 case vmIntrinsics::_clone: return inline_native_clone(intrinsic()->is_virtual());
533
534 case vmIntrinsics::_allocateUninitializedArray: return inline_unsafe_newArray(true);
535 case vmIntrinsics::_newArray: return inline_unsafe_newArray(false);
536 case vmIntrinsics::_newNullRestrictedNonAtomicArray: return inline_newArray(/* null_free */ true, /* atomic */ false);
537 case vmIntrinsics::_newNullRestrictedAtomicArray: return inline_newArray(/* null_free */ true, /* atomic */ true);
538 case vmIntrinsics::_newNullableAtomicArray: return inline_newArray(/* null_free */ false, /* atomic */ true);
539
540 case vmIntrinsics::_isAssignableFrom: return inline_native_subtype_check();
541
542 case vmIntrinsics::_isInstance:
543 case vmIntrinsics::_isHidden:
544 case vmIntrinsics::_getSuperclass: return inline_native_Class_query(intrinsic_id());
545
546 case vmIntrinsics::_floatToRawIntBits:
547 case vmIntrinsics::_floatToIntBits:
548 case vmIntrinsics::_intBitsToFloat:
549 case vmIntrinsics::_doubleToRawLongBits:
550 case vmIntrinsics::_doubleToLongBits:
551 case vmIntrinsics::_longBitsToDouble:
552 case vmIntrinsics::_floatToFloat16:
553 case vmIntrinsics::_float16ToFloat: return inline_fp_conversions(intrinsic_id());
554 case vmIntrinsics::_sqrt_float16: return inline_fp16_operations(intrinsic_id(), 1);
555 case vmIntrinsics::_fma_float16: return inline_fp16_operations(intrinsic_id(), 3);
556 case vmIntrinsics::_floatIsFinite:
557 case vmIntrinsics::_floatIsInfinite:
558 case vmIntrinsics::_doubleIsFinite:
2336 case vmIntrinsics::_remainderUnsigned_l: {
2337 zero_check_long(argument(2));
2338 // Compile-time detect of null-exception
2339 if (stopped()) {
2340 return true; // keep the graph constructed so far
2341 }
2342 n = new UModLNode(control(), argument(0), argument(2));
2343 break;
2344 }
2345 default: fatal_unexpected_iid(id); break;
2346 }
2347 set_result(_gvn.transform(n));
2348 return true;
2349 }
2350
2351 //----------------------------inline_unsafe_access----------------------------
2352
2353 const TypeOopPtr* LibraryCallKit::sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type) {
2354 // Attempt to infer a sharper value type from the offset and base type.
2355 ciKlass* sharpened_klass = nullptr;
2356 bool null_free = false;
2357
2358 // See if it is an instance field, with an object type.
2359 if (alias_type->field() != nullptr) {
2360 if (alias_type->field()->type()->is_klass()) {
2361 sharpened_klass = alias_type->field()->type()->as_klass();
2362 null_free = alias_type->field()->is_null_free();
2363 }
2364 }
2365
2366 const TypeOopPtr* result = nullptr;
2367 // See if it is a narrow oop array.
2368 if (adr_type->isa_aryptr()) {
2369 if (adr_type->offset() >= refArrayOopDesc::base_offset_in_bytes()) {
2370 const TypeOopPtr* elem_type = adr_type->is_aryptr()->elem()->make_oopptr();
2371 null_free = adr_type->is_aryptr()->is_null_free();
2372 if (elem_type != nullptr && elem_type->is_loaded()) {
2373 // Sharpen the value type.
2374 result = elem_type;
2375 }
2376 }
2377 }
2378
2379 // The sharpened class might be unloaded if there is no class loader
2380 // contraint in place.
2381 if (result == nullptr && sharpened_klass != nullptr && sharpened_klass->is_loaded()) {
2382 // Sharpen the value type.
2383 result = TypeOopPtr::make_from_klass(sharpened_klass);
2384 if (null_free) {
2385 result = result->join_speculative(TypePtr::NOTNULL)->is_oopptr();
2386 }
2387 }
2388 if (result != nullptr) {
2389 #ifndef PRODUCT
2390 if (C->print_intrinsics() || C->print_inlining()) {
2391 tty->print(" from base type: "); adr_type->dump(); tty->cr();
2392 tty->print(" sharpened value: "); result->dump(); tty->cr();
2393 }
2394 #endif
2395 }
2396 return result;
2397 }
2398
2399 DecoratorSet LibraryCallKit::mo_decorator_for_access_kind(AccessKind kind) {
2400 switch (kind) {
2401 case Relaxed:
2402 return MO_UNORDERED;
2403 case Opaque:
2404 return MO_RELAXED;
2405 case Acquire:
2406 return MO_ACQUIRE;
2438 _kit->jvms()->set_sp(_sp);
2439 _map->set_jvms(_kit->jvms());
2440 _kit->set_map(_map);
2441 _kit->set_sp(_sp);
2442 for (DUIterator_Fast imax, i = _kit->control()->fast_outs(imax); i < imax; i++) {
2443 Node* out = _kit->control()->fast_out(i);
2444 if (out->is_CFG() && out->in(0) == _kit->control() && out != _kit->map() && !_ctrl_succ.member(out)) {
2445 _kit->_gvn.hash_delete(out);
2446 out->set_req(0, _kit->C->top());
2447 _kit->C->record_for_igvn(out);
2448 --i; --imax;
2449 _kit->_gvn.hash_find_insert(out);
2450 }
2451 }
2452 }
2453
2454 void LibraryCallKit::SavedState::discard() {
2455 _discarded = true;
2456 }
2457
2458 bool LibraryCallKit::inline_unsafe_access(bool is_store, const BasicType type, const AccessKind kind, const bool unaligned, const bool is_flat) {
2459 if (callee()->is_static()) return false; // caller must have the capability!
2460 DecoratorSet decorators = C2_UNSAFE_ACCESS;
2461 guarantee(!is_store || kind != Acquire, "Acquire accesses can be produced only for loads");
2462 guarantee( is_store || kind != Release, "Release accesses can be produced only for stores");
2463 assert(type != T_OBJECT || !unaligned, "unaligned access not supported with object type");
2464
2465 if (is_reference_type(type)) {
2466 decorators |= ON_UNKNOWN_OOP_REF;
2467 }
2468
2469 if (unaligned) {
2470 decorators |= C2_UNALIGNED;
2471 }
2472
2473 #ifndef PRODUCT
2474 {
2475 ResourceMark rm;
2476 // Check the signatures.
2477 ciSignature* sig = callee()->signature();
2478 #ifdef ASSERT
2479 if (!is_store) {
2480 // Object getReference(Object base, int/long offset), etc.
2481 BasicType rtype = sig->return_type()->basic_type();
2482 assert(rtype == type, "getter must return the expected value");
2483 assert(sig->count() == 2 || (is_flat && sig->count() == 3), "oop getter has 2 or 3 arguments");
2484 assert(sig->type_at(0)->basic_type() == T_OBJECT, "getter base is object");
2485 assert(sig->type_at(1)->basic_type() == T_LONG, "getter offset is correct");
2486 } else {
2487 // void putReference(Object base, int/long offset, Object x), etc.
2488 assert(sig->return_type()->basic_type() == T_VOID, "putter must not return a value");
2489 assert(sig->count() == 3 || (is_flat && sig->count() == 4), "oop putter has 3 arguments");
2490 assert(sig->type_at(0)->basic_type() == T_OBJECT, "putter base is object");
2491 assert(sig->type_at(1)->basic_type() == T_LONG, "putter offset is correct");
2492 BasicType vtype = sig->type_at(sig->count()-1)->basic_type();
2493 assert(vtype == type, "putter must accept the expected value");
2494 }
2495 #endif // ASSERT
2496 }
2497 #endif //PRODUCT
2498
2499 C->set_has_unsafe_access(true); // Mark eventual nmethod as "unsafe".
2500
2501 Node* receiver = argument(0); // type: oop
2502
2503 // Build address expression.
2504 Node* heap_base_oop = top();
2505
2506 // The base is either a Java object or a value produced by Unsafe.staticFieldBase
2507 Node* base = argument(1); // type: oop
2508 // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
2509 Node* offset = argument(2); // type: long
2510 // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2511 // to be plain byte offsets, which are also the same as those accepted
2512 // by oopDesc::field_addr.
2513 assert(Unsafe_field_offset_to_byte_offset(11) == 11,
2514 "fieldOffset must be byte-scaled");
2515
2516 ciInlineKlass* inline_klass = nullptr;
2517 if (is_flat) {
2518 const TypeInstPtr* cls = _gvn.type(argument(4))->isa_instptr();
2519 if (cls == nullptr || cls->const_oop() == nullptr) {
2520 return false;
2521 }
2522 ciType* mirror_type = cls->const_oop()->as_instance()->java_mirror_type();
2523 if (!mirror_type->is_inlinetype()) {
2524 return false;
2525 }
2526 inline_klass = mirror_type->as_inline_klass();
2527 }
2528
2529 if (base->is_InlineType()) {
2530 assert(!is_store, "InlineTypeNodes are non-larval value objects");
2531 InlineTypeNode* vt = base->as_InlineType();
2532 if (offset->is_Con()) {
2533 long off = find_long_con(offset, 0);
2534 ciInlineKlass* vk = vt->type()->inline_klass();
2535 if ((long)(int)off != off || !vk->contains_field_offset(off)) {
2536 return false;
2537 }
2538
2539 ciField* field = vk->get_non_flat_field_by_offset(off);
2540 if (field != nullptr) {
2541 BasicType bt = type2field[field->type()->basic_type()];
2542 if (bt == T_ARRAY || bt == T_NARROWOOP) {
2543 bt = T_OBJECT;
2544 }
2545 if (bt == type && (!field->is_flat() || field->type() == inline_klass)) {
2546 Node* value = vt->field_value_by_offset(off, false);
2547 if (value->is_InlineType()) {
2548 value = value->as_InlineType()->adjust_scalarization_depth(this);
2549 }
2550 set_result(value);
2551 return true;
2552 }
2553 }
2554 }
2555 {
2556 // Re-execute the unsafe access if allocation triggers deoptimization.
2557 PreserveReexecuteState preexecs(this);
2558 jvms()->set_should_reexecute(true);
2559 vt = vt->buffer(this);
2560 }
2561 base = vt->get_oop();
2562 }
2563
2564 // 32-bit machines ignore the high half!
2565 offset = ConvL2X(offset);
2566
2567 // Save state and restore on bailout
2568 SavedState old_state(this);
2569
2570 Node* adr = make_unsafe_address(base, offset, type, kind == Relaxed);
2571 assert(!stopped(), "Inlining of unsafe access failed: address construction stopped unexpectedly");
2572
2573 if (_gvn.type(base->uncast())->isa_ptr() == TypePtr::NULL_PTR) {
2574 if (type != T_OBJECT && (inline_klass == nullptr || !inline_klass->has_object_fields())) {
2575 decorators |= IN_NATIVE; // off-heap primitive access
2576 } else {
2577 return false; // off-heap oop accesses are not supported
2578 }
2579 } else {
2580 heap_base_oop = base; // on-heap or mixed access
2581 }
2582
2583 // Can base be null? Otherwise, always on-heap access.
2584 bool can_access_non_heap = TypePtr::NULL_PTR->higher_equal(_gvn.type(base));
2585
2586 if (!can_access_non_heap) {
2587 decorators |= IN_HEAP;
2588 }
2589
2590 Node* val = is_store ? argument(4 + (is_flat ? 1 : 0)) : nullptr;
2591
2592 const TypePtr* adr_type = _gvn.type(adr)->isa_ptr();
2593 if (adr_type == TypePtr::NULL_PTR) {
2594 return false; // off-heap access with zero address
2595 }
2596
2597 // Try to categorize the address.
2598 Compile::AliasType* alias_type = C->alias_type(adr_type);
2599 assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2600
2601 if (alias_type->adr_type() == TypeInstPtr::KLASS ||
2602 alias_type->adr_type() == TypeAryPtr::RANGE) {
2603 return false; // not supported
2604 }
2605
2606 bool mismatched = false;
2607 BasicType bt = T_ILLEGAL;
2608 ciField* field = nullptr;
2609 if (adr_type->isa_instptr()) {
2610 const TypeInstPtr* instptr = adr_type->is_instptr();
2611 ciInstanceKlass* k = instptr->instance_klass();
2612 int off = instptr->offset();
2613 if (instptr->const_oop() != nullptr &&
2614 k == ciEnv::current()->Class_klass() &&
2615 instptr->offset() >= (k->size_helper() * wordSize)) {
2616 k = instptr->const_oop()->as_instance()->java_lang_Class_klass()->as_instance_klass();
2617 field = k->get_field_by_offset(off, true);
2618 } else {
2619 field = k->get_non_flat_field_by_offset(off);
2620 }
2621 if (field != nullptr) {
2622 bt = type2field[field->type()->basic_type()];
2623 }
2624 if (bt != alias_type->basic_type()) {
2625 // Type mismatch. Is it an access to a nested flat field?
2626 field = k->get_field_by_offset(off, false);
2627 if (field != nullptr) {
2628 bt = type2field[field->type()->basic_type()];
2629 }
2630 }
2631 assert(bt == alias_type->basic_type() || is_flat, "should match");
2632 } else {
2633 bt = alias_type->basic_type();
2634 }
2635
2636 if (bt != T_ILLEGAL) {
2637 assert(alias_type->adr_type()->is_oopptr(), "should be on-heap access");
2638 if (bt == T_BYTE && adr_type->isa_aryptr()) {
2639 // Alias type doesn't differentiate between byte[] and boolean[]).
2640 // Use address type to get the element type.
2641 bt = adr_type->is_aryptr()->elem()->array_element_basic_type();
2642 }
2643 if (is_reference_type(bt, true)) {
2644 // accessing an array field with getReference is not a mismatch
2645 bt = T_OBJECT;
2646 }
2647 if ((bt == T_OBJECT) != (type == T_OBJECT)) {
2648 // Don't intrinsify mismatched object accesses
2649 return false;
2650 }
2651 mismatched = (bt != type);
2652 } else if (alias_type->adr_type()->isa_oopptr()) {
2653 mismatched = true; // conservatively mark all "wide" on-heap accesses as mismatched
2654 }
2655
2656 if (is_flat) {
2657 if (adr_type->isa_instptr()) {
2658 if (field == nullptr || field->type() != inline_klass) {
2659 mismatched = true;
2660 }
2661 } else if (adr_type->isa_aryptr()) {
2662 const Type* elem = adr_type->is_aryptr()->elem();
2663 if (!adr_type->is_flat() || elem->inline_klass() != inline_klass) {
2664 mismatched = true;
2665 }
2666 } else {
2667 mismatched = true;
2668 }
2669 if (is_store) {
2670 const Type* val_t = _gvn.type(val);
2671 if (!val_t->is_inlinetypeptr() || val_t->inline_klass() != inline_klass) {
2672 return false;
2673 }
2674 }
2675 }
2676
2677 old_state.discard();
2678 assert(!mismatched || alias_type->adr_type()->is_oopptr(), "off-heap access can't be mismatched");
2679
2680 if (mismatched) {
2681 decorators |= C2_MISMATCHED;
2682 }
2683
2684 // First guess at the value type.
2685 const Type *value_type = Type::get_const_basic_type(type);
2686
2687 // Figure out the memory ordering.
2688 decorators |= mo_decorator_for_access_kind(kind);
2689
2690 if (!is_store) {
2691 if (type == T_OBJECT && !is_flat) {
2692 const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2693 if (tjp != nullptr) {
2694 value_type = tjp;
2695 }
2696 }
2697 }
2698
2699 receiver = null_check(receiver);
2700 if (stopped()) {
2701 return true;
2702 }
2703 // Heap pointers get a null-check from the interpreter,
2704 // as a courtesy. However, this is not guaranteed by Unsafe,
2705 // and it is not possible to fully distinguish unintended nulls
2706 // from intended ones in this API.
2707
2708 if (!is_store) {
2709 Node* p = nullptr;
2710 // Try to constant fold a load from a constant field
2711
2712 if (heap_base_oop != top() && field != nullptr && field->is_constant() && !field->is_flat() && !mismatched) {
2713 // final or stable field
2714 p = make_constant_from_field(field, heap_base_oop);
2715 }
2716
2717 if (p == nullptr) { // Could not constant fold the load
2718 if (is_flat) {
2719 p = InlineTypeNode::make_from_flat(this, inline_klass, base, adr, adr_type, false, false, true);
2720 } else {
2721 p = access_load_at(heap_base_oop, adr, adr_type, value_type, type, decorators);
2722 const TypeOopPtr* ptr = value_type->make_oopptr();
2723 if (ptr != nullptr && ptr->is_inlinetypeptr()) {
2724 // Load a non-flattened inline type from memory
2725 p = InlineTypeNode::make_from_oop(this, p, ptr->inline_klass());
2726 }
2727 }
2728 // Normalize the value returned by getBoolean in the following cases
2729 if (type == T_BOOLEAN &&
2730 (mismatched ||
2731 heap_base_oop == top() || // - heap_base_oop is null or
2732 (can_access_non_heap && field == nullptr)) // - heap_base_oop is potentially null
2733 // and the unsafe access is made to large offset
2734 // (i.e., larger than the maximum offset necessary for any
2735 // field access)
2736 ) {
2737 IdealKit ideal = IdealKit(this);
2738 #define __ ideal.
2739 IdealVariable normalized_result(ideal);
2740 __ declarations_done();
2741 __ set(normalized_result, p);
2742 __ if_then(p, BoolTest::ne, ideal.ConI(0));
2743 __ set(normalized_result, ideal.ConI(1));
2744 ideal.end_if();
2745 final_sync(ideal);
2746 p = __ value(normalized_result);
2747 #undef __
2748 }
2749 }
2750 if (type == T_ADDRESS) {
2751 p = gvn().transform(new CastP2XNode(nullptr, p));
2752 p = ConvX2UL(p);
2753 }
2754 // The load node has the control of the preceding MemBarCPUOrder. All
2755 // following nodes will have the control of the MemBarCPUOrder inserted at
2756 // the end of this method. So, pushing the load onto the stack at a later
2757 // point is fine.
2758 set_result(p);
2759 } else {
2760 if (bt == T_ADDRESS) {
2761 // Repackage the long as a pointer.
2762 val = ConvL2X(val);
2763 val = gvn().transform(new CastX2PNode(val));
2764 }
2765 if (is_flat) {
2766 val->as_InlineType()->store_flat(this, base, adr, false, false, true, decorators);
2767 } else {
2768 access_store_at(heap_base_oop, adr, adr_type, val, value_type, type, decorators);
2769 }
2770 }
2771
2772 return true;
2773 }
2774
2775 bool LibraryCallKit::inline_unsafe_flat_access(bool is_store, AccessKind kind) {
2776 #ifdef ASSERT
2777 {
2778 ResourceMark rm;
2779 // Check the signatures.
2780 ciSignature* sig = callee()->signature();
2781 assert(sig->type_at(0)->basic_type() == T_OBJECT, "base should be object, but is %s", type2name(sig->type_at(0)->basic_type()));
2782 assert(sig->type_at(1)->basic_type() == T_LONG, "offset should be long, but is %s", type2name(sig->type_at(1)->basic_type()));
2783 assert(sig->type_at(2)->basic_type() == T_INT, "layout kind should be int, but is %s", type2name(sig->type_at(3)->basic_type()));
2784 assert(sig->type_at(3)->basic_type() == T_OBJECT, "value klass should be object, but is %s", type2name(sig->type_at(4)->basic_type()));
2785 if (is_store) {
2786 assert(sig->return_type()->basic_type() == T_VOID, "putter must not return a value, but returns %s", type2name(sig->return_type()->basic_type()));
2787 assert(sig->count() == 5, "flat putter should have 5 arguments, but has %d", sig->count());
2788 assert(sig->type_at(4)->basic_type() == T_OBJECT, "put value should be object, but is %s", type2name(sig->type_at(5)->basic_type()));
2789 } else {
2790 assert(sig->return_type()->basic_type() == T_OBJECT, "getter must return an object, but returns %s", type2name(sig->return_type()->basic_type()));
2791 assert(sig->count() == 4, "flat getter should have 4 arguments, but has %d", sig->count());
2792 }
2793 }
2794 #endif // ASSERT
2795
2796 assert(kind == Relaxed, "Only plain accesses for now");
2797 if (callee()->is_static()) {
2798 // caller must have the capability!
2799 return false;
2800 }
2801 C->set_has_unsafe_access(true);
2802
2803 const TypeInstPtr* value_klass_node = _gvn.type(argument(5))->isa_instptr();
2804 if (value_klass_node == nullptr || value_klass_node->const_oop() == nullptr) {
2805 // parameter valueType is not a constant
2806 return false;
2807 }
2808 ciType* mirror_type = value_klass_node->const_oop()->as_instance()->java_mirror_type();
2809 if (!mirror_type->is_inlinetype()) {
2810 // Dead code
2811 return false;
2812 }
2813 ciInlineKlass* value_klass = mirror_type->as_inline_klass();
2814
2815 const TypeInt* layout_type = _gvn.type(argument(4))->isa_int();
2816 if (layout_type == nullptr || !layout_type->is_con()) {
2817 // parameter layoutKind is not a constant
2818 return false;
2819 }
2820 assert(layout_type->get_con() >= static_cast<int>(LayoutKind::REFERENCE) &&
2821 layout_type->get_con() <= static_cast<int>(LayoutKind::UNKNOWN),
2822 "invalid layoutKind %d", layout_type->get_con());
2823 LayoutKind layout = static_cast<LayoutKind>(layout_type->get_con());
2824 assert(layout == LayoutKind::REFERENCE || layout == LayoutKind::NON_ATOMIC_FLAT ||
2825 layout == LayoutKind::ATOMIC_FLAT || layout == LayoutKind::NULLABLE_ATOMIC_FLAT,
2826 "unexpected layoutKind %d", layout_type->get_con());
2827
2828 null_check(argument(0));
2829 if (stopped()) {
2830 return true;
2831 }
2832
2833 Node* base = must_be_not_null(argument(1), true);
2834 Node* offset = argument(2);
2835 const Type* base_type = _gvn.type(base);
2836
2837 Node* ptr;
2838 bool immutable_memory = false;
2839 DecoratorSet decorators = C2_UNSAFE_ACCESS | IN_HEAP | MO_UNORDERED;
2840 if (base_type->isa_instptr()) {
2841 const TypeLong* offset_type = _gvn.type(offset)->isa_long();
2842 if (offset_type == nullptr || !offset_type->is_con()) {
2843 // Offset into a non-array should be a constant
2844 decorators |= C2_MISMATCHED;
2845 } else {
2846 int offset_con = checked_cast<int>(offset_type->get_con());
2847 ciInstanceKlass* base_klass = base_type->is_instptr()->instance_klass();
2848 ciField* field = base_klass->get_non_flat_field_by_offset(offset_con);
2849 if (field == nullptr) {
2850 assert(!base_klass->is_final(), "non-existence field at offset %d of class %s", offset_con, base_klass->name()->as_utf8());
2851 decorators |= C2_MISMATCHED;
2852 } else {
2853 assert(field->type() == value_klass, "field at offset %d of %s is of type %s, but valueType is %s",
2854 offset_con, base_klass->name()->as_utf8(), field->type()->name(), value_klass->name()->as_utf8());
2855 immutable_memory = field->is_strict() && field->is_final();
2856
2857 if (base->is_InlineType()) {
2858 assert(!is_store, "Cannot store into a non-larval value object");
2859 set_result(base->as_InlineType()->field_value_by_offset(offset_con, false));
2860 return true;
2861 }
2862 }
2863 }
2864
2865 if (base->is_InlineType()) {
2866 assert(!is_store, "Cannot store into a non-larval value object");
2867 base = base->as_InlineType()->buffer(this, true);
2868 }
2869 ptr = basic_plus_adr(base, ConvL2X(offset));
2870 } else if (base_type->isa_aryptr()) {
2871 decorators |= IS_ARRAY;
2872 if (layout == LayoutKind::REFERENCE) {
2873 if (!base_type->is_aryptr()->is_not_flat()) {
2874 const TypeAryPtr* array_type = base_type->is_aryptr()->cast_to_not_flat();
2875 Node* new_base = _gvn.transform(new CastPPNode(control(), base, array_type, ConstraintCastNode::StrongDependency));
2876 replace_in_map(base, new_base);
2877 base = new_base;
2878 }
2879 ptr = basic_plus_adr(base, ConvL2X(offset));
2880 } else {
2881 if (UseArrayFlattening) {
2882 // Flat array must have an exact type
2883 bool is_null_free = layout != LayoutKind::NULLABLE_ATOMIC_FLAT;
2884 bool is_atomic = layout != LayoutKind::NON_ATOMIC_FLAT;
2885 Node* new_base = cast_to_flat_array(base, value_klass, is_null_free, !is_null_free, is_atomic);
2886 replace_in_map(base, new_base);
2887 base = new_base;
2888 ptr = basic_plus_adr(base, ConvL2X(offset));
2889 const TypeAryPtr* ptr_type = _gvn.type(ptr)->is_aryptr();
2890 if (ptr_type->field_offset().get() != 0) {
2891 ptr = _gvn.transform(new CastPPNode(control(), ptr, ptr_type->with_field_offset(0), ConstraintCastNode::StrongDependency));
2892 }
2893 } else {
2894 uncommon_trap(Deoptimization::Reason_intrinsic,
2895 Deoptimization::Action_none);
2896 return true;
2897 }
2898 }
2899 } else {
2900 decorators |= C2_MISMATCHED;
2901 ptr = basic_plus_adr(base, ConvL2X(offset));
2902 }
2903
2904 if (is_store) {
2905 Node* value = argument(6);
2906 const Type* value_type = _gvn.type(value);
2907 if (!value_type->is_inlinetypeptr()) {
2908 value_type = Type::get_const_type(value_klass)->filter_speculative(value_type);
2909 Node* new_value = _gvn.transform(new CastPPNode(control(), value, value_type, ConstraintCastNode::StrongDependency));
2910 new_value = InlineTypeNode::make_from_oop(this, new_value, value_klass);
2911 replace_in_map(value, new_value);
2912 value = new_value;
2913 }
2914
2915 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());
2916 if (layout == LayoutKind::REFERENCE) {
2917 const TypePtr* ptr_type = (decorators & C2_MISMATCHED) != 0 ? TypeRawPtr::BOTTOM : _gvn.type(ptr)->is_ptr();
2918 access_store_at(base, ptr, ptr_type, value, value_type, T_OBJECT, decorators);
2919 } else {
2920 bool atomic = layout != LayoutKind::NON_ATOMIC_FLAT;
2921 bool null_free = layout != LayoutKind::NULLABLE_ATOMIC_FLAT;
2922 value->as_InlineType()->store_flat(this, base, ptr, atomic, immutable_memory, null_free, decorators);
2923 }
2924
2925 return true;
2926 } else {
2927 decorators |= (C2_CONTROL_DEPENDENT_LOAD | C2_UNKNOWN_CONTROL_LOAD);
2928 InlineTypeNode* result;
2929 if (layout == LayoutKind::REFERENCE) {
2930 const TypePtr* ptr_type = (decorators & C2_MISMATCHED) != 0 ? TypeRawPtr::BOTTOM : _gvn.type(ptr)->is_ptr();
2931 Node* oop = access_load_at(base, ptr, ptr_type, Type::get_const_type(value_klass), T_OBJECT, decorators);
2932 result = InlineTypeNode::make_from_oop(this, oop, value_klass);
2933 } else {
2934 bool atomic = layout != LayoutKind::NON_ATOMIC_FLAT;
2935 bool null_free = layout != LayoutKind::NULLABLE_ATOMIC_FLAT;
2936 result = InlineTypeNode::make_from_flat(this, value_klass, base, ptr, atomic, immutable_memory, null_free, decorators);
2937 }
2938
2939 set_result(result);
2940 return true;
2941 }
2942 }
2943
2944 bool LibraryCallKit::inline_unsafe_make_private_buffer() {
2945 Node* receiver = argument(0);
2946 Node* value = argument(1);
2947
2948 const Type* type = gvn().type(value);
2949 if (!type->is_inlinetypeptr()) {
2950 C->record_method_not_compilable("value passed to Unsafe::makePrivateBuffer is not of a constant value type");
2951 return false;
2952 }
2953
2954 null_check(receiver);
2955 if (stopped()) {
2956 return true;
2957 }
2958
2959 value = null_check(value);
2960 if (stopped()) {
2961 return true;
2962 }
2963
2964 ciInlineKlass* vk = type->inline_klass();
2965 Node* klass = makecon(TypeKlassPtr::make(vk));
2966 Node* obj = new_instance(klass);
2967 AllocateNode::Ideal_allocation(obj)->_larval = true;
2968
2969 assert(value->is_InlineType(), "must be an InlineTypeNode");
2970 Node* payload_ptr = basic_plus_adr(obj, vk->payload_offset());
2971 value->as_InlineType()->store_flat(this, obj, payload_ptr, false, true, true, IN_HEAP | MO_UNORDERED);
2972
2973 set_result(obj);
2974 return true;
2975 }
2976
2977 bool LibraryCallKit::inline_unsafe_finish_private_buffer() {
2978 Node* receiver = argument(0);
2979 Node* buffer = argument(1);
2980
2981 const Type* type = gvn().type(buffer);
2982 if (!type->is_inlinetypeptr()) {
2983 C->record_method_not_compilable("value passed to Unsafe::finishPrivateBuffer is not of a constant value type");
2984 return false;
2985 }
2986
2987 AllocateNode* alloc = AllocateNode::Ideal_allocation(buffer);
2988 if (alloc == nullptr) {
2989 C->record_method_not_compilable("value passed to Unsafe::finishPrivateBuffer must be allocated by Unsafe::makePrivateBuffer");
2990 return false;
2991 }
2992
2993 null_check(receiver);
2994 if (stopped()) {
2995 return true;
2996 }
2997
2998 // Unset the larval bit in the object header
2999 Node* old_header = make_load(control(), buffer, TypeX_X, TypeX_X->basic_type(), MemNode::unordered, LoadNode::Pinned);
3000 Node* new_header = gvn().transform(new AndXNode(old_header, MakeConX(~markWord::larval_bit_in_place)));
3001 access_store_at(buffer, buffer, type->is_ptr(), new_header, TypeX_X, TypeX_X->basic_type(), MO_UNORDERED | IN_HEAP);
3002
3003 // We must ensure that the buffer is properly published
3004 insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out(AllocateNode::RawAddress));
3005 assert(!type->maybe_null(), "result of an allocation should not be null");
3006 set_result(InlineTypeNode::make_from_oop(this, buffer, type->inline_klass()));
3007 return true;
3008 }
3009
3010 //----------------------------inline_unsafe_load_store----------------------------
3011 // This method serves a couple of different customers (depending on LoadStoreKind):
3012 //
3013 // LS_cmp_swap:
3014 //
3015 // boolean compareAndSetReference(Object o, long offset, Object expected, Object x);
3016 // boolean compareAndSetInt( Object o, long offset, int expected, int x);
3017 // boolean compareAndSetLong( Object o, long offset, long expected, long x);
3018 //
3019 // LS_cmp_swap_weak:
3020 //
3021 // boolean weakCompareAndSetReference( Object o, long offset, Object expected, Object x);
3022 // boolean weakCompareAndSetReferencePlain( Object o, long offset, Object expected, Object x);
3023 // boolean weakCompareAndSetReferenceAcquire(Object o, long offset, Object expected, Object x);
3024 // boolean weakCompareAndSetReferenceRelease(Object o, long offset, Object expected, Object x);
3025 //
3026 // boolean weakCompareAndSetInt( Object o, long offset, int expected, int x);
3027 // boolean weakCompareAndSetIntPlain( Object o, long offset, int expected, int x);
3028 // boolean weakCompareAndSetIntAcquire( Object o, long offset, int expected, int x);
3029 // boolean weakCompareAndSetIntRelease( Object o, long offset, int expected, int x);
3192 }
3193 case LS_cmp_swap:
3194 case LS_cmp_swap_weak:
3195 case LS_get_add:
3196 break;
3197 default:
3198 ShouldNotReachHere();
3199 }
3200
3201 // Null check receiver.
3202 receiver = null_check(receiver);
3203 if (stopped()) {
3204 return true;
3205 }
3206
3207 int alias_idx = C->get_alias_index(adr_type);
3208
3209 if (is_reference_type(type)) {
3210 decorators |= IN_HEAP | ON_UNKNOWN_OOP_REF;
3211
3212 if (oldval != nullptr && oldval->is_InlineType()) {
3213 // Re-execute the unsafe access if allocation triggers deoptimization.
3214 PreserveReexecuteState preexecs(this);
3215 jvms()->set_should_reexecute(true);
3216 oldval = oldval->as_InlineType()->buffer(this)->get_oop();
3217 }
3218 if (newval != nullptr && newval->is_InlineType()) {
3219 // Re-execute the unsafe access if allocation triggers deoptimization.
3220 PreserveReexecuteState preexecs(this);
3221 jvms()->set_should_reexecute(true);
3222 newval = newval->as_InlineType()->buffer(this)->get_oop();
3223 }
3224
3225 // Transformation of a value which could be null pointer (CastPP #null)
3226 // could be delayed during Parse (for example, in adjust_map_after_if()).
3227 // Execute transformation here to avoid barrier generation in such case.
3228 if (_gvn.type(newval) == TypePtr::NULL_PTR)
3229 newval = _gvn.makecon(TypePtr::NULL_PTR);
3230
3231 if (oldval != nullptr && _gvn.type(oldval) == TypePtr::NULL_PTR) {
3232 // Refine the value to a null constant, when it is known to be null
3233 oldval = _gvn.makecon(TypePtr::NULL_PTR);
3234 }
3235 }
3236
3237 Node* result = nullptr;
3238 switch (kind) {
3239 case LS_cmp_exchange: {
3240 result = access_atomic_cmpxchg_val_at(base, adr, adr_type, alias_idx,
3241 oldval, newval, value_type, type, decorators);
3242 break;
3243 }
3244 case LS_cmp_swap_weak:
3391 Deoptimization::Action_make_not_entrant);
3392 }
3393 if (stopped()) {
3394 return true;
3395 }
3396 #endif //INCLUDE_JVMTI
3397
3398 Node* test = nullptr;
3399 if (LibraryCallKit::klass_needs_init_guard(kls)) {
3400 // Note: The argument might still be an illegal value like
3401 // Serializable.class or Object[].class. The runtime will handle it.
3402 // But we must make an explicit check for initialization.
3403 Node* insp = basic_plus_adr(kls, in_bytes(InstanceKlass::init_state_offset()));
3404 // Use T_BOOLEAN for InstanceKlass::_init_state so the compiler
3405 // can generate code to load it as unsigned byte.
3406 Node* inst = make_load(nullptr, insp, TypeInt::UBYTE, T_BOOLEAN, MemNode::acquire);
3407 Node* bits = intcon(InstanceKlass::fully_initialized);
3408 test = _gvn.transform(new SubINode(inst, bits));
3409 // The 'test' is non-zero if we need to take a slow path.
3410 }
3411 Node* obj = nullptr;
3412 const TypeInstKlassPtr* tkls = _gvn.type(kls)->isa_instklassptr();
3413 if (tkls != nullptr && tkls->instance_klass()->is_inlinetype()) {
3414 obj = InlineTypeNode::make_all_zero(_gvn, tkls->instance_klass()->as_inline_klass())->buffer(this);
3415 } else {
3416 obj = new_instance(kls, test);
3417 }
3418 set_result(obj);
3419 return true;
3420 }
3421
3422 //------------------------inline_native_time_funcs--------------
3423 // inline code for System.currentTimeMillis() and System.nanoTime()
3424 // these have the same type and signature
3425 bool LibraryCallKit::inline_native_time_funcs(address funcAddr, const char* funcName) {
3426 const TypeFunc* tf = OptoRuntime::void_long_Type();
3427 const TypePtr* no_memory_effects = nullptr;
3428 Node* time = make_runtime_call(RC_LEAF, tf, funcAddr, funcName, no_memory_effects);
3429 Node* value = _gvn.transform(new ProjNode(time, TypeFunc::Parms+0));
3430 #ifdef ASSERT
3431 Node* value_top = _gvn.transform(new ProjNode(time, TypeFunc::Parms+1));
3432 assert(value_top == top(), "second value must be top");
3433 #endif
3434 set_result(value);
3435 return true;
3436 }
3437
4178 Node* thread = _gvn.transform(new ThreadLocalNode());
4179 Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::vthread_offset()));
4180 Node* thread_obj_handle
4181 = make_load(nullptr, p, p->bottom_type()->is_ptr(), T_OBJECT, MemNode::unordered);
4182 thread_obj_handle = _gvn.transform(thread_obj_handle);
4183 const TypePtr *adr_type = _gvn.type(thread_obj_handle)->isa_ptr();
4184 access_store_at(nullptr, thread_obj_handle, adr_type, arr, _gvn.type(arr), T_OBJECT, IN_NATIVE | MO_UNORDERED);
4185
4186 // Change the _monitor_owner_id of the JavaThread
4187 Node* tid = load_field_from_object(arr, "tid", "J");
4188 Node* monitor_owner_id_offset = basic_plus_adr(thread, in_bytes(JavaThread::monitor_owner_id_offset()));
4189 store_to_memory(control(), monitor_owner_id_offset, tid, T_LONG, MemNode::unordered, true);
4190
4191 JFR_ONLY(extend_setCurrentThread(thread, arr);)
4192 return true;
4193 }
4194
4195 const Type* LibraryCallKit::scopedValueCache_type() {
4196 ciKlass* objects_klass = ciObjArrayKlass::make(env()->Object_klass());
4197 const TypeOopPtr* etype = TypeOopPtr::make_from_klass(env()->Object_klass());
4198 const TypeAry* arr0 = TypeAry::make(etype, TypeInt::POS, /* stable= */ false, /* flat= */ false, /* not_flat= */ true, /* not_null_free= */ true);
4199
4200 // Because we create the scopedValue cache lazily we have to make the
4201 // type of the result BotPTR.
4202 bool xk = etype->klass_is_exact();
4203 const Type* objects_type = TypeAryPtr::make(TypePtr::BotPTR, arr0, objects_klass, xk, TypeAryPtr::Offset(0));
4204 return objects_type;
4205 }
4206
4207 Node* LibraryCallKit::scopedValueCache_helper() {
4208 Node* thread = _gvn.transform(new ThreadLocalNode());
4209 Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::scopedValueCache_offset()));
4210 // We cannot use immutable_memory() because we might flip onto a
4211 // different carrier thread, at which point we'll need to use that
4212 // carrier thread's cache.
4213 // return _gvn.transform(LoadNode::make(_gvn, nullptr, immutable_memory(), p, p->bottom_type()->is_ptr(),
4214 // TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered));
4215 return make_load(nullptr, p, p->bottom_type()->is_ptr(), T_ADDRESS, MemNode::unordered);
4216 }
4217
4218 //------------------------inline_native_scopedValueCache------------------
4219 bool LibraryCallKit::inline_native_scopedValueCache() {
4220 Node* cache_obj_handle = scopedValueCache_helper();
4221 const Type* objects_type = scopedValueCache_type();
4222 set_result(access_load(cache_obj_handle, objects_type, T_OBJECT, IN_NATIVE));
4223
4307 store_to_memory(control(), pin_count_offset, next_pin_count, T_INT, MemNode::unordered);
4308
4309 // Result of top level CFG and Memory.
4310 RegionNode* result_rgn = new RegionNode(PATH_LIMIT);
4311 record_for_igvn(result_rgn);
4312 PhiNode* result_mem = new PhiNode(result_rgn, Type::MEMORY, TypePtr::BOTTOM);
4313 record_for_igvn(result_mem);
4314
4315 result_rgn->init_req(_true_path, _gvn.transform(valid_pin_count));
4316 result_rgn->init_req(_false_path, _gvn.transform(continuation_is_null));
4317 result_mem->init_req(_true_path, _gvn.transform(reset_memory()));
4318 result_mem->init_req(_false_path, _gvn.transform(input_memory_state));
4319
4320 // Set output state.
4321 set_control(_gvn.transform(result_rgn));
4322 set_all_memory(_gvn.transform(result_mem));
4323
4324 return true;
4325 }
4326
4327 //-----------------------load_klass_from_mirror_common-------------------------
4328 // Given a java mirror (a java.lang.Class oop), load its corresponding klass oop.
4329 // Test the klass oop for null (signifying a primitive Class like Integer.TYPE),
4330 // and branch to the given path on the region.
4331 // If never_see_null, take an uncommon trap on null, so we can optimistically
4332 // compile for the non-null case.
4333 // If the region is null, force never_see_null = true.
4334 Node* LibraryCallKit::load_klass_from_mirror_common(Node* mirror,
4335 bool never_see_null,
4336 RegionNode* region,
4337 int null_path,
4338 int offset) {
4339 if (region == nullptr) never_see_null = true;
4340 Node* p = basic_plus_adr(mirror, offset);
4341 const TypeKlassPtr* kls_type = TypeInstKlassPtr::OBJECT_OR_NULL;
4342 Node* kls = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), p, TypeRawPtr::BOTTOM, kls_type));
4343 Node* null_ctl = top();
4344 kls = null_check_oop(kls, &null_ctl, never_see_null);
4345 if (region != nullptr) {
4346 // Set region->in(null_path) if the mirror is a primitive (e.g, int.class).
4350 }
4351 return kls;
4352 }
4353
4354 //--------------------(inline_native_Class_query helpers)---------------------
4355 // Use this for JVM_ACC_INTERFACE.
4356 // Fall through if (mods & mask) == bits, take the guard otherwise.
4357 Node* LibraryCallKit::generate_klass_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region,
4358 ByteSize offset, const Type* type, BasicType bt) {
4359 // Branch around if the given klass has the given modifier bit set.
4360 // Like generate_guard, adds a new path onto the region.
4361 Node* modp = basic_plus_adr(kls, in_bytes(offset));
4362 Node* mods = make_load(nullptr, modp, type, bt, MemNode::unordered);
4363 Node* mask = intcon(modifier_mask);
4364 Node* bits = intcon(modifier_bits);
4365 Node* mbit = _gvn.transform(new AndINode(mods, mask));
4366 Node* cmp = _gvn.transform(new CmpINode(mbit, bits));
4367 Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
4368 return generate_fair_guard(bol, region);
4369 }
4370
4371 Node* LibraryCallKit::generate_interface_guard(Node* kls, RegionNode* region) {
4372 return generate_klass_flags_guard(kls, JVM_ACC_INTERFACE, 0, region,
4373 Klass::access_flags_offset(), TypeInt::CHAR, T_CHAR);
4374 }
4375
4376 // Use this for testing if Klass is_hidden, has_finalizer, and is_cloneable_fast.
4377 Node* LibraryCallKit::generate_misc_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region) {
4378 return generate_klass_flags_guard(kls, modifier_mask, modifier_bits, region,
4379 Klass::misc_flags_offset(), TypeInt::UBYTE, T_BOOLEAN);
4380 }
4381
4382 Node* LibraryCallKit::generate_hidden_class_guard(Node* kls, RegionNode* region) {
4383 return generate_misc_flags_guard(kls, KlassFlags::_misc_is_hidden_class, 0, region);
4384 }
4385
4386 //-------------------------inline_native_Class_query-------------------
4387 bool LibraryCallKit::inline_native_Class_query(vmIntrinsics::ID id) {
4388 const Type* return_type = TypeInt::BOOL;
4389 Node* prim_return_value = top(); // what happens if it's a primitive class?
4390 bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
4500 }
4501 if (!stopped()) {
4502 query_value = load_mirror_from_klass(kls);
4503 }
4504 break;
4505
4506 default:
4507 fatal_unexpected_iid(id);
4508 break;
4509 }
4510
4511 // Fall-through is the normal case of a query to a real class.
4512 phi->init_req(1, query_value);
4513 region->init_req(1, control());
4514
4515 C->set_has_split_ifs(true); // Has chance for split-if optimization
4516 set_result(region, phi);
4517 return true;
4518 }
4519
4520
4521 //-------------------------inline_Class_cast-------------------
4522 bool LibraryCallKit::inline_Class_cast() {
4523 Node* mirror = argument(0); // Class
4524 Node* obj = argument(1);
4525 const TypeInstPtr* mirror_con = _gvn.type(mirror)->isa_instptr();
4526 if (mirror_con == nullptr) {
4527 return false; // dead path (mirror->is_top()).
4528 }
4529 if (obj == nullptr || obj->is_top()) {
4530 return false; // dead path
4531 }
4532 const TypeOopPtr* tp = _gvn.type(obj)->isa_oopptr();
4533
4534 // First, see if Class.cast() can be folded statically.
4535 // java_mirror_type() returns non-null for compile-time Class constants.
4536 ciType* tm = mirror_con->java_mirror_type();
4537 if (tm != nullptr && tm->is_klass() &&
4538 tp != nullptr) {
4539 if (!tp->is_loaded()) {
4540 // Don't use intrinsic when class is not loaded.
4541 return false;
4542 } else {
4543 const TypeKlassPtr* tklass = TypeKlassPtr::make(tm->as_klass(), Type::trust_interfaces);
4544 int static_res = C->static_subtype_check(tklass, tp->as_klass_type());
4545 if (static_res == Compile::SSC_always_true) {
4546 // isInstance() is true - fold the code.
4547 set_result(obj);
4548 return true;
4549 } else if (static_res == Compile::SSC_always_false) {
4550 // Don't use intrinsic, have to throw ClassCastException.
4551 // If the reference is null, the non-intrinsic bytecode will
4552 // be optimized appropriately.
4553 return false;
4554 }
4555 }
4556 }
4557
4558 // Bailout intrinsic and do normal inlining if exception path is frequent.
4559 if (too_many_traps(Deoptimization::Reason_intrinsic)) {
4560 return false;
4561 }
4562
4563 // Generate dynamic checks.
4564 // Class.cast() is java implementation of _checkcast bytecode.
4565 // Do checkcast (Parse::do_checkcast()) optimizations here.
4566
4567 mirror = null_check(mirror);
4568 // If mirror is dead, only null-path is taken.
4569 if (stopped()) {
4570 return true;
4571 }
4572
4573 // Not-subtype or the mirror's klass ptr is nullptr (in case it is a primitive).
4574 enum { _bad_type_path = 1, _prim_path = 2, _npe_path = 3, PATH_LIMIT };
4575 RegionNode* region = new RegionNode(PATH_LIMIT);
4576 record_for_igvn(region);
4577
4578 // Now load the mirror's klass metaobject, and null-check it.
4579 // If kls is null, we have a primitive mirror and
4580 // nothing is an instance of a primitive type.
4581 Node* kls = load_klass_from_mirror(mirror, false, region, _prim_path);
4582
4583 Node* res = top();
4584 Node* io = i_o();
4585 Node* mem = merged_memory();
4586 if (!stopped()) {
4587
4588 Node* bad_type_ctrl = top();
4589 // Do checkcast optimizations.
4590 res = gen_checkcast(obj, kls, &bad_type_ctrl);
4591 region->init_req(_bad_type_path, bad_type_ctrl);
4592 }
4593 if (region->in(_prim_path) != top() ||
4594 region->in(_bad_type_path) != top() ||
4595 region->in(_npe_path) != top()) {
4596 // Let Interpreter throw ClassCastException.
4597 PreserveJVMState pjvms(this);
4598 set_control(_gvn.transform(region));
4599 // Set IO and memory because gen_checkcast may override them when buffering inline types
4600 set_i_o(io);
4601 set_all_memory(mem);
4602 uncommon_trap(Deoptimization::Reason_intrinsic,
4603 Deoptimization::Action_maybe_recompile);
4604 }
4605 if (!stopped()) {
4606 set_result(res);
4607 }
4608 return true;
4609 }
4610
4611
4612 //--------------------------inline_native_subtype_check------------------------
4613 // This intrinsic takes the JNI calls out of the heart of
4614 // UnsafeFieldAccessorImpl.set, which improves Field.set, readObject, etc.
4615 bool LibraryCallKit::inline_native_subtype_check() {
4616 // Pull both arguments off the stack.
4617 Node* args[2]; // two java.lang.Class mirrors: superc, subc
4618 args[0] = argument(0);
4619 args[1] = argument(1);
4620 Node* klasses[2]; // corresponding Klasses: superk, subk
4621 klasses[0] = klasses[1] = top();
4622
4623 enum {
4624 // A full decision tree on {superc is prim, subc is prim}:
4625 _prim_0_path = 1, // {P,N} => false
4626 // {P,P} & superc!=subc => false
4627 _prim_same_path, // {P,P} & superc==subc => true
4628 _prim_1_path, // {N,P} => false
4629 _ref_subtype_path, // {N,N} & subtype check wins => true
4630 _both_ref_path, // {N,N} & subtype check loses => false
4631 PATH_LIMIT
4632 };
4633
4634 RegionNode* region = new RegionNode(PATH_LIMIT);
4635 RegionNode* prim_region = new RegionNode(2);
4636 Node* phi = new PhiNode(region, TypeInt::BOOL);
4637 record_for_igvn(region);
4638 record_for_igvn(prim_region);
4639
4640 const TypePtr* adr_type = TypeRawPtr::BOTTOM; // memory type of loads
4641 const TypeKlassPtr* kls_type = TypeInstKlassPtr::OBJECT_OR_NULL;
4642 int class_klass_offset = java_lang_Class::klass_offset();
4643
4644 // First null-check both mirrors and load each mirror's klass metaobject.
4645 int which_arg;
4646 for (which_arg = 0; which_arg <= 1; which_arg++) {
4647 Node* arg = args[which_arg];
4648 arg = null_check(arg);
4649 if (stopped()) break;
4650 args[which_arg] = arg;
4651
4652 Node* p = basic_plus_adr(arg, class_klass_offset);
4653 Node* kls = LoadKlassNode::make(_gvn, immutable_memory(), p, adr_type, kls_type);
4654 klasses[which_arg] = _gvn.transform(kls);
4655 }
4656
4657 // Having loaded both klasses, test each for null.
4658 bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
4659 for (which_arg = 0; which_arg <= 1; which_arg++) {
4660 Node* kls = klasses[which_arg];
4661 Node* null_ctl = top();
4662 kls = null_check_oop(kls, &null_ctl, never_see_null);
4663 if (which_arg == 0) {
4664 prim_region->init_req(1, null_ctl);
4665 } else {
4666 region->init_req(_prim_1_path, null_ctl);
4667 }
4668 if (stopped()) break;
4669 klasses[which_arg] = kls;
4670 }
4671
4672 if (!stopped()) {
4673 // now we have two reference types, in klasses[0..1]
4674 Node* subk = klasses[1]; // the argument to isAssignableFrom
4675 Node* superk = klasses[0]; // the receiver
4676 region->set_req(_both_ref_path, gen_subtype_check(subk, superk));
4677 region->set_req(_ref_subtype_path, control());
4678 }
4679
4680 // If both operands are primitive (both klasses null), then
4681 // we must return true when they are identical primitives.
4682 // It is convenient to test this after the first null klass check.
4683 // This path is also used if superc is a value mirror.
4684 set_control(_gvn.transform(prim_region));
4685 if (!stopped()) {
4686 // Since superc is primitive, make a guard for the superc==subc case.
4687 Node* cmp_eq = _gvn.transform(new CmpPNode(args[0], args[1]));
4688 Node* bol_eq = _gvn.transform(new BoolNode(cmp_eq, BoolTest::eq));
4689 generate_fair_guard(bol_eq, region);
4690 if (region->req() == PATH_LIMIT+1) {
4691 // A guard was added. If the added guard is taken, superc==subc.
4692 region->swap_edges(PATH_LIMIT, _prim_same_path);
4693 region->del_req(PATH_LIMIT);
4694 }
4695 region->set_req(_prim_0_path, control()); // Not equal after all.
4696 }
4697
4698 // these are the only paths that produce 'true':
4699 phi->set_req(_prim_same_path, intcon(1));
4700 phi->set_req(_ref_subtype_path, intcon(1));
4701
4702 // pull together the cases:
4703 assert(region->req() == PATH_LIMIT, "sane region");
4704 for (uint i = 1; i < region->req(); i++) {
4705 Node* ctl = region->in(i);
4706 if (ctl == nullptr || ctl == top()) {
4707 region->set_req(i, top());
4708 phi ->set_req(i, top());
4709 } else if (phi->in(i) == nullptr) {
4710 phi->set_req(i, intcon(0)); // all other paths produce 'false'
4711 }
4712 }
4713
4714 set_control(_gvn.transform(region));
4715 set_result(_gvn.transform(phi));
4716 return true;
4717 }
4718
4719 //---------------------generate_array_guard_common------------------------
4720 Node* LibraryCallKit::generate_array_guard_common(Node* kls, RegionNode* region, ArrayKind kind, Node** obj) {
4721
4722 if (stopped()) {
4723 return nullptr;
4724 }
4725
4726 // Like generate_guard, adds a new path onto the region.
4727 jint layout_con = 0;
4728 Node* layout_val = get_layout_helper(kls, layout_con);
4729 if (layout_val == nullptr) {
4730 bool query = 0;
4731 switch(kind) {
4732 case RefArray: query = Klass::layout_helper_is_refArray(layout_con); break;
4733 case NonRefArray: query = !Klass::layout_helper_is_refArray(layout_con); break;
4734 case TypeArray: query = Klass::layout_helper_is_typeArray(layout_con); break;
4735 case AnyArray: query = Klass::layout_helper_is_array(layout_con); break;
4736 case NonArray: query = !Klass::layout_helper_is_array(layout_con); break;
4737 default:
4738 ShouldNotReachHere();
4739 }
4740 if (!query) {
4741 return nullptr; // never a branch
4742 } else { // always a branch
4743 Node* always_branch = control();
4744 if (region != nullptr)
4745 region->add_req(always_branch);
4746 set_control(top());
4747 return always_branch;
4748 }
4749 }
4750 unsigned int value = 0;
4751 BoolTest::mask btest = BoolTest::illegal;
4752 switch(kind) {
4753 case RefArray:
4754 case NonRefArray: {
4755 value = Klass::_lh_array_tag_ref_value;
4756 layout_val = _gvn.transform(new RShiftINode(layout_val, intcon(Klass::_lh_array_tag_shift)));
4757 btest = (kind == RefArray) ? BoolTest::eq : BoolTest::ne;
4758 break;
4759 }
4760 case TypeArray: {
4761 value = Klass::_lh_array_tag_type_value;
4762 layout_val = _gvn.transform(new RShiftINode(layout_val, intcon(Klass::_lh_array_tag_shift)));
4763 btest = BoolTest::eq;
4764 break;
4765 }
4766 case AnyArray: value = Klass::_lh_neutral_value; btest = BoolTest::lt; break;
4767 case NonArray: value = Klass::_lh_neutral_value; btest = BoolTest::gt; break;
4768 default:
4769 ShouldNotReachHere();
4770 }
4771 // Now test the correct condition.
4772 jint nval = (jint)value;
4773 Node* cmp = _gvn.transform(new CmpINode(layout_val, intcon(nval)));
4774 Node* bol = _gvn.transform(new BoolNode(cmp, btest));
4775 Node* ctrl = generate_fair_guard(bol, region);
4776 Node* is_array_ctrl = kind == NonArray ? control() : ctrl;
4777 if (obj != nullptr && is_array_ctrl != nullptr && is_array_ctrl != top()) {
4778 // Keep track of the fact that 'obj' is an array to prevent
4779 // array specific accesses from floating above the guard.
4780 *obj = _gvn.transform(new CastPPNode(is_array_ctrl, *obj, TypeAryPtr::BOTTOM));
4781 }
4782 return ctrl;
4783 }
4784
4785 // public static native Object[] newNullRestrictedAtomicArray(Class<?> componentType, int length, Object initVal);
4786 // public static native Object[] newNullRestrictedNonAtomicArray(Class<?> componentType, int length, Object initVal);
4787 // public static native Object[] newNullableAtomicArray(Class<?> componentType, int length);
4788 bool LibraryCallKit::inline_newArray(bool null_free, bool atomic) {
4789 assert(null_free || atomic, "nullable implies atomic");
4790 Node* componentType = argument(0);
4791 Node* length = argument(1);
4792 Node* init_val = null_free ? argument(2) : nullptr;
4793
4794 const TypeInstPtr* tp = _gvn.type(componentType)->isa_instptr();
4795 if (tp != nullptr) {
4796 ciInstanceKlass* ik = tp->instance_klass();
4797 if (ik == C->env()->Class_klass()) {
4798 ciType* t = tp->java_mirror_type();
4799 if (t != nullptr && t->is_inlinetype()) {
4800
4801 ciArrayKlass* array_klass = ciArrayKlass::make(t, null_free, atomic, true);
4802 assert(array_klass->is_elem_null_free() == null_free, "inconsistency");
4803 assert(array_klass->is_elem_atomic() == atomic, "inconsistency");
4804
4805 // TOOD 8350865 ZGC needs card marks on initializing oop stores
4806 if (UseZGC && null_free && !array_klass->is_flat_array_klass()) {
4807 return false;
4808 }
4809
4810 if (array_klass->is_loaded() && array_klass->element_klass()->as_inline_klass()->is_initialized()) {
4811 const TypeAryKlassPtr* array_klass_type = TypeAryKlassPtr::make(array_klass, Type::trust_interfaces, true);
4812 if (null_free) {
4813 if (init_val->is_InlineType()) {
4814 if (array_klass_type->is_flat() && init_val->as_InlineType()->is_all_zero(&gvn(), /* flat */ true)) {
4815 // Zeroing is enough because the init value is the all-zero value
4816 init_val = nullptr;
4817 } else {
4818 init_val = init_val->as_InlineType()->buffer(this);
4819 }
4820 }
4821 // TODO 8350865 Should we add a check of the init_val type (maybe in debug only + halt)?
4822 }
4823 Node* obj = new_array(makecon(array_klass_type), length, 0, nullptr, false, init_val);
4824 const TypeAryPtr* arytype = gvn().type(obj)->is_aryptr();
4825 assert(arytype->is_null_free() == null_free, "inconsistency");
4826 assert(arytype->is_not_null_free() == !null_free, "inconsistency");
4827 assert(arytype->is_atomic() == atomic, "inconsistency");
4828 set_result(obj);
4829 return true;
4830 }
4831 }
4832 }
4833 }
4834 return false;
4835 }
4836
4837 Node* LibraryCallKit::load_default_array_klass(Node* klass_node) {
4838 // TODO 8366668
4839 // - Fred suggested that we could just have the first entry in the refined list point to the array with ArrayKlass::ArrayProperties::DEFAULT property
4840 // 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
4841 // - Convert this to an IGVN optimization, so it's also folded after parsing
4842 // - The generate_typeArray_guard is not needed by all callers, double-check that it's folded
4843
4844 const Type* klass_t = _gvn.type(klass_node);
4845 const TypeAryKlassPtr* ary_klass_t = klass_t->isa_aryklassptr();
4846 if (ary_klass_t && ary_klass_t->klass_is_exact()) {
4847 if (ary_klass_t->exact_klass()->is_obj_array_klass()) {
4848 ary_klass_t = ary_klass_t->get_vm_type(false);
4849 return makecon(ary_klass_t);
4850 } else {
4851 return klass_node;
4852 }
4853 }
4854
4855 // Load next refined array klass if klass is an ObjArrayKlass
4856 RegionNode* refined_region = new RegionNode(2);
4857 Node* refined_phi = new PhiNode(refined_region, klass_t);
4858
4859 generate_typeArray_guard(klass_node, refined_region);
4860 if (refined_region->req() == 3) {
4861 refined_phi->add_req(klass_node);
4862 }
4863
4864 Node* adr_refined_klass = basic_plus_adr(klass_node, in_bytes(ObjArrayKlass::next_refined_array_klass_offset()));
4865 Node* refined_klass = _gvn.transform(LoadKlassNode::make(_gvn, immutable_memory(), adr_refined_klass, TypeRawPtr::BOTTOM, TypeInstKlassPtr::OBJECT_OR_NULL));
4866
4867 RegionNode* refined_region2 = new RegionNode(3);
4868 Node* refined_phi2 = new PhiNode(refined_region2, klass_t);
4869
4870 Node* null_ctl = top();
4871 Node* null_free_klass = null_check_common(refined_klass, T_OBJECT, false, &null_ctl);
4872 refined_region2->init_req(1, null_ctl);
4873 refined_phi2->init_req(1, klass_node);
4874
4875 refined_region2->init_req(2, control());
4876 refined_phi2->init_req(2, null_free_klass);
4877
4878 set_control(_gvn.transform(refined_region2));
4879 refined_klass = _gvn.transform(refined_phi2);
4880
4881 Node* adr_properties = basic_plus_adr(refined_klass, in_bytes(ObjArrayKlass::properties_offset()));
4882
4883 Node* properties = _gvn.transform(LoadNode::make(_gvn, control(), immutable_memory(), adr_properties, TypeRawPtr::BOTTOM, TypeInt::INT, T_INT, MemNode::unordered));
4884 Node* default_val = makecon(TypeInt::make(ArrayKlass::ArrayProperties::DEFAULT));
4885 Node* chk = _gvn.transform(new CmpINode(properties, default_val));
4886 Node* tst = _gvn.transform(new BoolNode(chk, BoolTest::eq));
4887
4888 { // Deoptimize if not the default property
4889 BuildCutout unless(this, tst, PROB_MAX);
4890 uncommon_trap_exact(Deoptimization::Reason_class_check, Deoptimization::Action_none);
4891 }
4892
4893 refined_region->init_req(1, control());
4894 refined_phi->init_req(1, refined_klass);
4895
4896 set_control(_gvn.transform(refined_region));
4897 klass_node = _gvn.transform(refined_phi);
4898
4899 return klass_node;
4900 }
4901
4902 //-----------------------inline_native_newArray--------------------------
4903 // private static native Object java.lang.reflect.Array.newArray(Class<?> componentType, int length);
4904 // private native Object Unsafe.allocateUninitializedArray0(Class<?> cls, int size);
4905 bool LibraryCallKit::inline_unsafe_newArray(bool uninitialized) {
4906 Node* mirror;
4907 Node* count_val;
4908 if (uninitialized) {
4909 null_check_receiver();
4910 mirror = argument(1);
4911 count_val = argument(2);
4912 } else {
4913 mirror = argument(0);
4914 count_val = argument(1);
4915 }
4916
4917 mirror = null_check(mirror);
4918 // If mirror or obj is dead, only null-path is taken.
4919 if (stopped()) return true;
4920
4921 enum { _normal_path = 1, _slow_path = 2, PATH_LIMIT };
4922 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
4923 PhiNode* result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
4941 CallJavaNode* slow_call = nullptr;
4942 if (uninitialized) {
4943 // Generate optimized virtual call (holder class 'Unsafe' is final)
4944 slow_call = generate_method_call(vmIntrinsics::_allocateUninitializedArray, false, false, true);
4945 } else {
4946 slow_call = generate_method_call_static(vmIntrinsics::_newArray, true);
4947 }
4948 Node* slow_result = set_results_for_java_call(slow_call);
4949 // this->control() comes from set_results_for_java_call
4950 result_reg->set_req(_slow_path, control());
4951 result_val->set_req(_slow_path, slow_result);
4952 result_io ->set_req(_slow_path, i_o());
4953 result_mem->set_req(_slow_path, reset_memory());
4954 }
4955
4956 set_control(normal_ctl);
4957 if (!stopped()) {
4958 // Normal case: The array type has been cached in the java.lang.Class.
4959 // The following call works fine even if the array type is polymorphic.
4960 // It could be a dynamic mix of int[], boolean[], Object[], etc.
4961
4962 klass_node = load_default_array_klass(klass_node);
4963
4964 Node* obj = new_array(klass_node, count_val, 0); // no arguments to push
4965 result_reg->init_req(_normal_path, control());
4966 result_val->init_req(_normal_path, obj);
4967 result_io ->init_req(_normal_path, i_o());
4968 result_mem->init_req(_normal_path, reset_memory());
4969
4970 if (uninitialized) {
4971 // Mark the allocation so that zeroing is skipped
4972 AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(obj);
4973 alloc->maybe_set_complete(&_gvn);
4974 }
4975 }
4976
4977 // Return the combined state.
4978 set_i_o( _gvn.transform(result_io) );
4979 set_all_memory( _gvn.transform(result_mem));
4980
4981 C->set_has_split_ifs(true); // Has chance for split-if optimization
4982 set_result(result_reg, result_val);
4983 return true;
5032 // the bytecode that invokes Arrays.copyOf if deoptimization happens.
5033 { PreserveReexecuteState preexecs(this);
5034 jvms()->set_should_reexecute(true);
5035
5036 array_type_mirror = null_check(array_type_mirror);
5037 original = null_check(original);
5038
5039 // Check if a null path was taken unconditionally.
5040 if (stopped()) return true;
5041
5042 Node* orig_length = load_array_length(original);
5043
5044 Node* klass_node = load_klass_from_mirror(array_type_mirror, false, nullptr, 0);
5045 klass_node = null_check(klass_node);
5046
5047 RegionNode* bailout = new RegionNode(1);
5048 record_for_igvn(bailout);
5049
5050 // Despite the generic type of Arrays.copyOf, the mirror might be int, int[], etc.
5051 // Bail out if that is so.
5052 // Inline type array may have object field that would require a
5053 // write barrier. Conservatively, go to slow path.
5054 // TODO 8251971: Optimize for the case when flat src/dst are later found
5055 // to not contain oops (i.e., move this check to the macro expansion phase).
5056 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
5057 const TypeAryPtr* orig_t = _gvn.type(original)->isa_aryptr();
5058 const TypeKlassPtr* tklass = _gvn.type(klass_node)->is_klassptr();
5059 bool exclude_flat = UseArrayFlattening && bs->array_copy_requires_gc_barriers(true, T_OBJECT, false, false, BarrierSetC2::Parsing) &&
5060 // Can src array be flat and contain oops?
5061 (orig_t == nullptr || (!orig_t->is_not_flat() && (!orig_t->is_flat() || orig_t->elem()->inline_klass()->contains_oops()))) &&
5062 // Can dest array be flat and contain oops?
5063 tklass->can_be_inline_array() && (!tklass->is_flat() || tklass->is_aryklassptr()->elem()->is_instklassptr()->instance_klass()->as_inline_klass()->contains_oops());
5064 // TODO 8366668 generate_non_refArray_guard also passed for ref arrays??
5065 Node* not_objArray = exclude_flat ? generate_non_refArray_guard(klass_node, bailout) : generate_typeArray_guard(klass_node, bailout);
5066
5067 klass_node = load_default_array_klass(klass_node);
5068
5069 if (not_objArray != nullptr) {
5070 // Improve the klass node's type from the new optimistic assumption:
5071 ciKlass* ak = ciArrayKlass::make(env()->Object_klass());
5072 const Type* akls = TypeKlassPtr::make(TypePtr::NotNull, ak, Type::Offset(0));
5073 Node* cast = new CastPPNode(control(), klass_node, akls);
5074 klass_node = _gvn.transform(cast);
5075 }
5076
5077 // Bail out if either start or end is negative.
5078 generate_negative_guard(start, bailout, &start);
5079 generate_negative_guard(end, bailout, &end);
5080
5081 Node* length = end;
5082 if (_gvn.type(start) != TypeInt::ZERO) {
5083 length = _gvn.transform(new SubINode(end, start));
5084 }
5085
5086 // Bail out if length is negative (i.e., if start > end).
5087 // Without this the new_array would throw
5088 // NegativeArraySizeException but IllegalArgumentException is what
5089 // should be thrown
5090 generate_negative_guard(length, bailout, &length);
5091
5092 // Handle inline type arrays
5093 bool can_validate = !too_many_traps(Deoptimization::Reason_class_check);
5094 if (!stopped()) {
5095 // TODO JDK-8329224
5096 if (!orig_t->is_null_free()) {
5097 // Not statically known to be null free, add a check
5098 generate_fair_guard(null_free_array_test(original), bailout);
5099 }
5100 orig_t = _gvn.type(original)->isa_aryptr();
5101 if (orig_t != nullptr && orig_t->is_flat()) {
5102 // Src is flat, check that dest is flat as well
5103 if (exclude_flat) {
5104 // Dest can't be flat, bail out
5105 bailout->add_req(control());
5106 set_control(top());
5107 } else {
5108 generate_fair_guard(flat_array_test(klass_node, /* flat = */ false), bailout);
5109 }
5110 // TODO 8350865 This is not correct anymore. Write tests and fix logic similar to arraycopy.
5111 } else if (UseArrayFlattening && (orig_t == nullptr || !orig_t->is_not_flat()) &&
5112 // If dest is flat, src must be flat as well (guaranteed by src <: dest check if validated).
5113 ((!tklass->is_flat() && tklass->can_be_inline_array()) || !can_validate)) {
5114 // Src might be flat and dest might not be flat. Go to the slow path if src is flat.
5115 // TODO 8251971: Optimize for the case when src/dest are later found to be both flat.
5116 generate_fair_guard(flat_array_test(load_object_klass(original)), bailout);
5117 if (orig_t != nullptr) {
5118 orig_t = orig_t->cast_to_not_flat();
5119 original = _gvn.transform(new CheckCastPPNode(control(), original, orig_t));
5120 }
5121 }
5122 if (!can_validate) {
5123 // No validation. The subtype check emitted at macro expansion time will not go to the slow
5124 // path but call checkcast_arraycopy which can not handle flat/null-free inline type arrays.
5125 // TODO 8251971: Optimize for the case when src/dest are later found to be both flat/null-free.
5126 generate_fair_guard(flat_array_test(klass_node), bailout);
5127 generate_fair_guard(null_free_array_test(original), bailout);
5128 }
5129 }
5130
5131 // Bail out if start is larger than the original length
5132 Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
5133 generate_negative_guard(orig_tail, bailout, &orig_tail);
5134
5135 if (bailout->req() > 1) {
5136 PreserveJVMState pjvms(this);
5137 set_control(_gvn.transform(bailout));
5138 uncommon_trap(Deoptimization::Reason_intrinsic,
5139 Deoptimization::Action_maybe_recompile);
5140 }
5141
5142 if (!stopped()) {
5143 // How many elements will we copy from the original?
5144 // The answer is MinI(orig_tail, length).
5145 Node* moved = _gvn.transform(new MinINode(orig_tail, length));
5146
5147 // Generate a direct call to the right arraycopy function(s).
5148 // We know the copy is disjoint but we might not know if the
5149 // oop stores need checking.
5150 // Extreme case: Arrays.copyOf((Integer[])x, 10, String[].class).
5156 // to the copyOf to be validated, including that the copy to the
5157 // new array won't trigger an ArrayStoreException. That subtype
5158 // check can be optimized if we know something on the type of
5159 // the input array from type speculation.
5160 if (_gvn.type(klass_node)->singleton()) {
5161 const TypeKlassPtr* subk = _gvn.type(load_object_klass(original))->is_klassptr();
5162 const TypeKlassPtr* superk = _gvn.type(klass_node)->is_klassptr();
5163
5164 int test = C->static_subtype_check(superk, subk);
5165 if (test != Compile::SSC_always_true && test != Compile::SSC_always_false) {
5166 const TypeOopPtr* t_original = _gvn.type(original)->is_oopptr();
5167 if (t_original->speculative_type() != nullptr) {
5168 original = maybe_cast_profiled_obj(original, t_original->speculative_type(), true);
5169 }
5170 }
5171 }
5172
5173 bool validated = false;
5174 // Reason_class_check rather than Reason_intrinsic because we
5175 // want to intrinsify even if this traps.
5176 if (can_validate) {
5177 Node* not_subtype_ctrl = gen_subtype_check(original, klass_node);
5178
5179 if (not_subtype_ctrl != top()) {
5180 PreserveJVMState pjvms(this);
5181 set_control(not_subtype_ctrl);
5182 uncommon_trap(Deoptimization::Reason_class_check,
5183 Deoptimization::Action_make_not_entrant);
5184 assert(stopped(), "Should be stopped");
5185 }
5186 validated = true;
5187 }
5188
5189 if (!stopped()) {
5190 newcopy = new_array(klass_node, length, 0); // no arguments to push
5191
5192 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, true, true,
5193 load_object_klass(original), klass_node);
5194 if (!is_copyOfRange) {
5195 ac->set_copyof(validated);
5196 } else {
5242
5243 //-----------------------generate_method_call----------------------------
5244 // Use generate_method_call to make a slow-call to the real
5245 // method if the fast path fails. An alternative would be to
5246 // use a stub like OptoRuntime::slow_arraycopy_Java.
5247 // This only works for expanding the current library call,
5248 // not another intrinsic. (E.g., don't use this for making an
5249 // arraycopy call inside of the copyOf intrinsic.)
5250 CallJavaNode*
5251 LibraryCallKit::generate_method_call(vmIntrinsicID method_id, bool is_virtual, bool is_static, bool res_not_null) {
5252 // When compiling the intrinsic method itself, do not use this technique.
5253 guarantee(callee() != C->method(), "cannot make slow-call to self");
5254
5255 ciMethod* method = callee();
5256 // ensure the JVMS we have will be correct for this call
5257 guarantee(method_id == method->intrinsic_id(), "must match");
5258
5259 const TypeFunc* tf = TypeFunc::make(method);
5260 if (res_not_null) {
5261 assert(tf->return_type() == T_OBJECT, "");
5262 const TypeTuple* range = tf->range_cc();
5263 const Type** fields = TypeTuple::fields(range->cnt());
5264 fields[TypeFunc::Parms] = range->field_at(TypeFunc::Parms)->filter_speculative(TypePtr::NOTNULL);
5265 const TypeTuple* new_range = TypeTuple::make(range->cnt(), fields);
5266 tf = TypeFunc::make(tf->domain_cc(), new_range);
5267 }
5268 CallJavaNode* slow_call;
5269 if (is_static) {
5270 assert(!is_virtual, "");
5271 slow_call = new CallStaticJavaNode(C, tf,
5272 SharedRuntime::get_resolve_static_call_stub(), method);
5273 } else if (is_virtual) {
5274 assert(!gvn().type(argument(0))->maybe_null(), "should not be null");
5275 int vtable_index = Method::invalid_vtable_index;
5276 if (UseInlineCaches) {
5277 // Suppress the vtable call
5278 } else {
5279 // hashCode and clone are not a miranda methods,
5280 // so the vtable index is fixed.
5281 // No need to use the linkResolver to get it.
5282 vtable_index = method->vtable_index();
5283 assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
5284 "bad index %d", vtable_index);
5285 }
5286 slow_call = new CallDynamicJavaNode(tf,
5303 set_edges_for_java_call(slow_call);
5304 return slow_call;
5305 }
5306
5307
5308 /**
5309 * Build special case code for calls to hashCode on an object. This call may
5310 * be virtual (invokevirtual) or bound (invokespecial). For each case we generate
5311 * slightly different code.
5312 */
5313 bool LibraryCallKit::inline_native_hashcode(bool is_virtual, bool is_static) {
5314 assert(is_static == callee()->is_static(), "correct intrinsic selection");
5315 assert(!(is_virtual && is_static), "either virtual, special, or static");
5316
5317 enum { _slow_path = 1, _fast_path, _null_path, PATH_LIMIT };
5318
5319 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
5320 PhiNode* result_val = new PhiNode(result_reg, TypeInt::INT);
5321 PhiNode* result_io = new PhiNode(result_reg, Type::ABIO);
5322 PhiNode* result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
5323 Node* obj = argument(0);
5324
5325 // Don't intrinsify hashcode on inline types for now.
5326 // The "is locked" runtime check below also serves as inline type check and goes to the slow path.
5327 if (gvn().type(obj)->is_inlinetypeptr()) {
5328 return false;
5329 }
5330
5331 if (!is_static) {
5332 // Check for hashing null object
5333 obj = null_check_receiver();
5334 if (stopped()) return true; // unconditionally null
5335 result_reg->init_req(_null_path, top());
5336 result_val->init_req(_null_path, top());
5337 } else {
5338 // Do a null check, and return zero if null.
5339 // System.identityHashCode(null) == 0
5340 Node* null_ctl = top();
5341 obj = null_check_oop(obj, &null_ctl);
5342 result_reg->init_req(_null_path, null_ctl);
5343 result_val->init_req(_null_path, _gvn.intcon(0));
5344 }
5345
5346 // Unconditionally null? Then return right away.
5347 if (stopped()) {
5348 set_control( result_reg->in(_null_path));
5349 if (!stopped())
5350 set_result(result_val->in(_null_path));
5351 return true;
5352 }
5353
5354 // We only go to the fast case code if we pass a number of guards. The
5355 // paths which do not pass are accumulated in the slow_region.
5356 RegionNode* slow_region = new RegionNode(1);
5357 record_for_igvn(slow_region);
5358
5359 // If this is a virtual call, we generate a funny guard. We pull out
5360 // the vtable entry corresponding to hashCode() from the target object.
5361 // If the target method which we are calling happens to be the native
5362 // Object hashCode() method, we pass the guard. We do not need this
5363 // guard for non-virtual calls -- the caller is known to be the native
5364 // Object hashCode().
5365 if (is_virtual) {
5366 // After null check, get the object's klass.
5367 Node* obj_klass = load_object_klass(obj);
5368 generate_virtual_guard(obj_klass, slow_region);
5369 }
5370
5371 // Get the header out of the object, use LoadMarkNode when available
5372 Node* header_addr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes());
5373 // The control of the load must be null. Otherwise, the load can move before
5374 // the null check after castPP removal.
5375 Node* no_ctrl = nullptr;
5376 Node* header = make_load(no_ctrl, header_addr, TypeX_X, TypeX_X->basic_type(), MemNode::unordered);
5377
5378 if (!UseObjectMonitorTable) {
5379 // Test the header to see if it is safe to read w.r.t. locking.
5380 // This also serves as guard against inline types
5381 Node *lock_mask = _gvn.MakeConX(markWord::inline_type_mask_in_place);
5382 Node *lmasked_header = _gvn.transform(new AndXNode(header, lock_mask));
5383 if (LockingMode == LM_LIGHTWEIGHT) {
5384 Node *monitor_val = _gvn.MakeConX(markWord::monitor_value);
5385 Node *chk_monitor = _gvn.transform(new CmpXNode(lmasked_header, monitor_val));
5386 Node *test_monitor = _gvn.transform(new BoolNode(chk_monitor, BoolTest::eq));
5387
5388 generate_slow_guard(test_monitor, slow_region);
5389 } else {
5390 Node *unlocked_val = _gvn.MakeConX(markWord::unlocked_value);
5391 Node *chk_unlocked = _gvn.transform(new CmpXNode(lmasked_header, unlocked_val));
5392 Node *test_not_unlocked = _gvn.transform(new BoolNode(chk_unlocked, BoolTest::ne));
5393
5394 generate_slow_guard(test_not_unlocked, slow_region);
5395 }
5396 }
5397
5398 // Get the hash value and check to see that it has been properly assigned.
5399 // We depend on hash_mask being at most 32 bits and avoid the use of
5400 // hash_mask_in_place because it could be larger than 32 bits in a 64-bit
5401 // vm: see markWord.hpp.
5436 // this->control() comes from set_results_for_java_call
5437 result_reg->init_req(_slow_path, control());
5438 result_val->init_req(_slow_path, slow_result);
5439 result_io ->set_req(_slow_path, i_o());
5440 result_mem ->set_req(_slow_path, reset_memory());
5441 }
5442
5443 // Return the combined state.
5444 set_i_o( _gvn.transform(result_io) );
5445 set_all_memory( _gvn.transform(result_mem));
5446
5447 set_result(result_reg, result_val);
5448 return true;
5449 }
5450
5451 //---------------------------inline_native_getClass----------------------------
5452 // public final native Class<?> java.lang.Object.getClass();
5453 //
5454 // Build special case code for calls to getClass on an object.
5455 bool LibraryCallKit::inline_native_getClass() {
5456 Node* obj = argument(0);
5457 if (obj->is_InlineType()) {
5458 const Type* t = _gvn.type(obj);
5459 if (t->maybe_null()) {
5460 null_check(obj);
5461 }
5462 set_result(makecon(TypeInstPtr::make(t->inline_klass()->java_mirror())));
5463 return true;
5464 }
5465 obj = null_check_receiver();
5466 if (stopped()) return true;
5467 set_result(load_mirror_from_klass(load_object_klass(obj)));
5468 return true;
5469 }
5470
5471 //-----------------inline_native_Reflection_getCallerClass---------------------
5472 // public static native Class<?> sun.reflect.Reflection.getCallerClass();
5473 //
5474 // In the presence of deep enough inlining, getCallerClass() becomes a no-op.
5475 //
5476 // NOTE: This code must perform the same logic as JVM_GetCallerClass
5477 // in that it must skip particular security frames and checks for
5478 // caller sensitive methods.
5479 bool LibraryCallKit::inline_native_Reflection_getCallerClass() {
5480 #ifndef PRODUCT
5481 if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
5482 tty->print_cr("Attempting to inline sun.reflect.Reflection.getCallerClass");
5483 }
5484 #endif
5485
5867 // not cloneable or finalizer => slow path to out-of-line Object.clone
5868 //
5869 // The general case has two steps, allocation and copying.
5870 // Allocation has two cases, and uses GraphKit::new_instance or new_array.
5871 //
5872 // Copying also has two cases, oop arrays and everything else.
5873 // Oop arrays use arrayof_oop_arraycopy (same as System.arraycopy).
5874 // Everything else uses the tight inline loop supplied by CopyArrayNode.
5875 //
5876 // These steps fold up nicely if and when the cloned object's klass
5877 // can be sharply typed as an object array, a type array, or an instance.
5878 //
5879 bool LibraryCallKit::inline_native_clone(bool is_virtual) {
5880 PhiNode* result_val;
5881
5882 // Set the reexecute bit for the interpreter to reexecute
5883 // the bytecode that invokes Object.clone if deoptimization happens.
5884 { PreserveReexecuteState preexecs(this);
5885 jvms()->set_should_reexecute(true);
5886
5887 Node* obj = argument(0);
5888 obj = null_check_receiver();
5889 if (stopped()) return true;
5890
5891 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
5892 if (obj_type->is_inlinetypeptr()) {
5893 // If the object to clone is an inline type, we can simply return it (i.e. a nop) since inline types have
5894 // no identity.
5895 set_result(obj);
5896 return true;
5897 }
5898
5899 // If we are going to clone an instance, we need its exact type to
5900 // know the number and types of fields to convert the clone to
5901 // loads/stores. Maybe a speculative type can help us.
5902 if (!obj_type->klass_is_exact() &&
5903 obj_type->speculative_type() != nullptr &&
5904 obj_type->speculative_type()->is_instance_klass() &&
5905 !obj_type->speculative_type()->is_inlinetype()) {
5906 ciInstanceKlass* spec_ik = obj_type->speculative_type()->as_instance_klass();
5907 if (spec_ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem &&
5908 !spec_ik->has_injected_fields()) {
5909 if (!obj_type->isa_instptr() ||
5910 obj_type->is_instptr()->instance_klass()->has_subklass()) {
5911 obj = maybe_cast_profiled_obj(obj, obj_type->speculative_type(), false);
5912 }
5913 }
5914 }
5915
5916 // Conservatively insert a memory barrier on all memory slices.
5917 // Do not let writes into the original float below the clone.
5918 insert_mem_bar(Op_MemBarCPUOrder);
5919
5920 // paths into result_reg:
5921 enum {
5922 _slow_path = 1, // out-of-line call to clone method (virtual or not)
5923 _objArray_path, // plain array allocation, plus arrayof_oop_arraycopy
5924 _array_path, // plain array allocation, plus arrayof_long_arraycopy
5925 _instance_path, // plain instance allocation, plus arrayof_long_arraycopy
5926 PATH_LIMIT
5927 };
5928 RegionNode* result_reg = new RegionNode(PATH_LIMIT);
5929 result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
5930 PhiNode* result_i_o = new PhiNode(result_reg, Type::ABIO);
5931 PhiNode* result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
5932 record_for_igvn(result_reg);
5933
5934 Node* obj_klass = load_object_klass(obj);
5935 // We only go to the fast case code if we pass a number of guards.
5936 // The paths which do not pass are accumulated in the slow_region.
5937 RegionNode* slow_region = new RegionNode(1);
5938 record_for_igvn(slow_region);
5939
5940 Node* array_obj = obj;
5941 Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)nullptr, &array_obj);
5942 if (array_ctl != nullptr) {
5943 // It's an array.
5944 PreserveJVMState pjvms(this);
5945 set_control(array_ctl);
5946
5947 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
5948 const TypeAryPtr* ary_ptr = obj_type->isa_aryptr();
5949 if (UseArrayFlattening && bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, false, BarrierSetC2::Expansion) &&
5950 obj_type->can_be_inline_array() &&
5951 (ary_ptr == nullptr || (!ary_ptr->is_not_flat() && (!ary_ptr->is_flat() || ary_ptr->elem()->inline_klass()->contains_oops())))) {
5952 // Flat inline type array may have object field that would require a
5953 // write barrier. Conservatively, go to slow path.
5954 generate_fair_guard(flat_array_test(obj_klass), slow_region);
5955 }
5956
5957 if (!stopped()) {
5958 Node* obj_length = load_array_length(array_obj);
5959 Node* array_size = nullptr; // Size of the array without object alignment padding.
5960 Node* alloc_obj = new_array(obj_klass, obj_length, 0, &array_size, /*deoptimize_on_exception=*/true);
5961
5962 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
5963 if (bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, false, BarrierSetC2::Parsing)) {
5964 // If it is an oop array, it requires very special treatment,
5965 // because gc barriers are required when accessing the array.
5966 Node* is_obja = generate_refArray_guard(obj_klass, (RegionNode*)nullptr);
5967 if (is_obja != nullptr) {
5968 PreserveJVMState pjvms2(this);
5969 set_control(is_obja);
5970 // Generate a direct call to the right arraycopy function(s).
5971 // Clones are always tightly coupled.
5972 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, array_obj, intcon(0), alloc_obj, intcon(0), obj_length, true, false);
5973 ac->set_clone_oop_array();
5974 Node* n = _gvn.transform(ac);
5975 assert(n == ac, "cannot disappear");
5976 ac->connect_outputs(this, /*deoptimize_on_exception=*/true);
5977
5978 result_reg->init_req(_objArray_path, control());
5979 result_val->init_req(_objArray_path, alloc_obj);
5980 result_i_o ->set_req(_objArray_path, i_o());
5981 result_mem ->set_req(_objArray_path, reset_memory());
5982 }
5983 }
5984 // Otherwise, there are no barriers to worry about.
5985 // (We can dispense with card marks if we know the allocation
5986 // comes out of eden (TLAB)... In fact, ReduceInitialCardMarks
5987 // causes the non-eden paths to take compensating steps to
5988 // simulate a fresh allocation, so that no further
5989 // card marks are required in compiled code to initialize
5990 // the object.)
5991
5992 if (!stopped()) {
5993 copy_to_clone(obj, alloc_obj, array_size, true);
5994
5995 // Present the results of the copy.
5996 result_reg->init_req(_array_path, control());
5997 result_val->init_req(_array_path, alloc_obj);
5998 result_i_o ->set_req(_array_path, i_o());
5999 result_mem ->set_req(_array_path, reset_memory());
6000 }
6001 }
6002 }
6003
6004 if (!stopped()) {
6005 // It's an instance (we did array above). Make the slow-path tests.
6006 // If this is a virtual call, we generate a funny guard. We grab
6007 // the vtable entry corresponding to clone() from the target object.
6008 // If the target method which we are calling happens to be the
6009 // Object clone() method, we pass the guard. We do not need this
6010 // guard for non-virtual calls; the caller is known to be the native
6011 // Object clone().
6012 if (is_virtual) {
6013 generate_virtual_guard(obj_klass, slow_region);
6014 }
6015
6016 // The object must be easily cloneable and must not have a finalizer.
6017 // Both of these conditions may be checked in a single test.
6018 // We could optimize the test further, but we don't care.
6019 generate_misc_flags_guard(obj_klass,
6020 // Test both conditions:
6021 KlassFlags::_misc_is_cloneable_fast | KlassFlags::_misc_has_finalizer,
6022 // Must be cloneable but not finalizer:
6023 KlassFlags::_misc_is_cloneable_fast,
6115 set_jvms(sfpt->jvms());
6116 _reexecute_sp = jvms()->sp();
6117
6118 return saved_jvms;
6119 }
6120 }
6121 }
6122 return nullptr;
6123 }
6124
6125 // Clone the JVMState of the array allocation and create a new safepoint with it. Re-push the array length to the stack
6126 // such that uncommon traps can be emitted to re-execute the array allocation in the interpreter.
6127 SafePointNode* LibraryCallKit::create_safepoint_with_state_before_array_allocation(const AllocateArrayNode* alloc) const {
6128 JVMState* old_jvms = alloc->jvms()->clone_shallow(C);
6129 uint size = alloc->req();
6130 SafePointNode* sfpt = new SafePointNode(size, old_jvms);
6131 old_jvms->set_map(sfpt);
6132 for (uint i = 0; i < size; i++) {
6133 sfpt->init_req(i, alloc->in(i));
6134 }
6135 int adjustment = 1;
6136 const TypeAryKlassPtr* ary_klass_ptr = alloc->in(AllocateNode::KlassNode)->bottom_type()->is_aryklassptr();
6137 if (ary_klass_ptr->is_null_free()) {
6138 // A null-free, tightly coupled array allocation can only come from LibraryCallKit::inline_newArray which
6139 // also requires the componentType and initVal on stack for re-execution.
6140 // Re-create and push the componentType.
6141 ciArrayKlass* klass = ary_klass_ptr->exact_klass()->as_array_klass();
6142 ciInstance* instance = klass->component_mirror_instance();
6143 const TypeInstPtr* t_instance = TypeInstPtr::make(instance);
6144 sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp(), makecon(t_instance));
6145 adjustment++;
6146 }
6147 // re-push array length for deoptimization
6148 sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp() + adjustment - 1, alloc->in(AllocateNode::ALength));
6149 if (ary_klass_ptr->is_null_free()) {
6150 // Re-create and push the initVal.
6151 Node* init_val = alloc->in(AllocateNode::InitValue);
6152 if (init_val == nullptr) {
6153 init_val = InlineTypeNode::make_all_zero(_gvn, ary_klass_ptr->elem()->is_instklassptr()->instance_klass()->as_inline_klass());
6154 } else if (UseCompressedOops) {
6155 init_val = _gvn.transform(new DecodeNNode(init_val, init_val->bottom_type()->make_ptr()));
6156 }
6157 sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp() + adjustment, init_val);
6158 adjustment++;
6159 }
6160 old_jvms->set_sp(old_jvms->sp() + adjustment);
6161 old_jvms->set_monoff(old_jvms->monoff() + adjustment);
6162 old_jvms->set_scloff(old_jvms->scloff() + adjustment);
6163 old_jvms->set_endoff(old_jvms->endoff() + adjustment);
6164 old_jvms->set_should_reexecute(true);
6165
6166 sfpt->set_i_o(map()->i_o());
6167 sfpt->set_memory(map()->memory());
6168 sfpt->set_control(map()->control());
6169 return sfpt;
6170 }
6171
6172 // In case of a deoptimization, we restart execution at the
6173 // allocation, allocating a new array. We would leave an uninitialized
6174 // array in the heap that GCs wouldn't expect. Move the allocation
6175 // after the traps so we don't allocate the array if we
6176 // deoptimize. This is possible because tightly_coupled_allocation()
6177 // guarantees there's no observer of the allocated array at this point
6178 // and the control flow is simple enough.
6179 void LibraryCallKit::arraycopy_move_allocation_here(AllocateArrayNode* alloc, Node* dest, JVMState* saved_jvms_before_guards,
6180 int saved_reexecute_sp, uint new_idx) {
6181 if (saved_jvms_before_guards != nullptr && !stopped()) {
6182 replace_unrelated_uncommon_traps_with_alloc_state(alloc, saved_jvms_before_guards);
6183
6184 assert(alloc != nullptr, "only with a tightly coupled allocation");
6185 // restore JVM state to the state at the arraycopy
6186 saved_jvms_before_guards->map()->set_control(map()->control());
6187 assert(saved_jvms_before_guards->map()->memory() == map()->memory(), "memory state changed?");
6188 assert(saved_jvms_before_guards->map()->i_o() == map()->i_o(), "IO state changed?");
6189 // If we've improved the types of some nodes (null check) while
6190 // emitting the guards, propagate them to the current state
6191 map()->replaced_nodes().apply(saved_jvms_before_guards->map(), new_idx);
6192 set_jvms(saved_jvms_before_guards);
6193 _reexecute_sp = saved_reexecute_sp;
6194
6195 // Remove the allocation from above the guards
6196 CallProjections* callprojs = alloc->extract_projections(true);
6197 InitializeNode* init = alloc->initialization();
6198 Node* alloc_mem = alloc->in(TypeFunc::Memory);
6199 C->gvn_replace_by(callprojs->fallthrough_ioproj, alloc->in(TypeFunc::I_O));
6200 C->gvn_replace_by(init->proj_out(TypeFunc::Memory), alloc_mem);
6201
6202 // The CastIINode created in GraphKit::new_array (in AllocateArrayNode::make_ideal_length) must stay below
6203 // the allocation (i.e. is only valid if the allocation succeeds):
6204 // 1) replace CastIINode with AllocateArrayNode's length here
6205 // 2) Create CastIINode again once allocation has moved (see below) at the end of this method
6206 //
6207 // Multiple identical CastIINodes might exist here. Each GraphKit::load_array_length() call will generate
6208 // new separate CastIINode (arraycopy guard checks or any array length use between array allocation and ararycopy)
6209 Node* init_control = init->proj_out(TypeFunc::Control);
6210 Node* alloc_length = alloc->Ideal_length();
6211 #ifdef ASSERT
6212 Node* prev_cast = nullptr;
6213 #endif
6214 for (uint i = 0; i < init_control->outcnt(); i++) {
6215 Node* init_out = init_control->raw_out(i);
6216 if (init_out->is_CastII() && init_out->in(TypeFunc::Control) == init_control && init_out->in(1) == alloc_length) {
6217 #ifdef ASSERT
6218 if (prev_cast == nullptr) {
6219 prev_cast = init_out;
6221 if (prev_cast->cmp(*init_out) == false) {
6222 prev_cast->dump();
6223 init_out->dump();
6224 assert(false, "not equal CastIINode");
6225 }
6226 }
6227 #endif
6228 C->gvn_replace_by(init_out, alloc_length);
6229 }
6230 }
6231 C->gvn_replace_by(init->proj_out(TypeFunc::Control), alloc->in(0));
6232
6233 // move the allocation here (after the guards)
6234 _gvn.hash_delete(alloc);
6235 alloc->set_req(TypeFunc::Control, control());
6236 alloc->set_req(TypeFunc::I_O, i_o());
6237 Node *mem = reset_memory();
6238 set_all_memory(mem);
6239 alloc->set_req(TypeFunc::Memory, mem);
6240 set_control(init->proj_out_or_null(TypeFunc::Control));
6241 set_i_o(callprojs->fallthrough_ioproj);
6242
6243 // Update memory as done in GraphKit::set_output_for_allocation()
6244 const TypeInt* length_type = _gvn.find_int_type(alloc->in(AllocateNode::ALength));
6245 const TypeOopPtr* ary_type = _gvn.type(alloc->in(AllocateNode::KlassNode))->is_klassptr()->as_instance_type();
6246 if (ary_type->isa_aryptr() && length_type != nullptr) {
6247 ary_type = ary_type->is_aryptr()->cast_to_size(length_type);
6248 }
6249 const TypePtr* telemref = ary_type->add_offset(Type::OffsetBot);
6250 int elemidx = C->get_alias_index(telemref);
6251 set_memory(init->proj_out_or_null(TypeFunc::Memory), Compile::AliasIdxRaw);
6252 set_memory(init->proj_out_or_null(TypeFunc::Memory), elemidx);
6253
6254 Node* allocx = _gvn.transform(alloc);
6255 assert(allocx == alloc, "where has the allocation gone?");
6256 assert(dest->is_CheckCastPP(), "not an allocation result?");
6257
6258 _gvn.hash_delete(dest);
6259 dest->set_req(0, control());
6260 Node* destx = _gvn.transform(dest);
6261 assert(destx == dest, "where has the allocation result gone?");
6559 top_src = src_type->isa_aryptr();
6560 has_src = (top_src != nullptr && top_src->elem() != Type::BOTTOM);
6561 src_spec = true;
6562 }
6563 if (!has_dest) {
6564 dest = maybe_cast_profiled_obj(dest, dest_k, true);
6565 dest_type = _gvn.type(dest);
6566 top_dest = dest_type->isa_aryptr();
6567 has_dest = (top_dest != nullptr && top_dest->elem() != Type::BOTTOM);
6568 dest_spec = true;
6569 }
6570 }
6571 }
6572
6573 if (has_src && has_dest && can_emit_guards) {
6574 BasicType src_elem = top_src->isa_aryptr()->elem()->array_element_basic_type();
6575 BasicType dest_elem = top_dest->isa_aryptr()->elem()->array_element_basic_type();
6576 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
6577 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
6578
6579 if (src_elem == dest_elem && top_src->is_flat() == top_dest->is_flat() && src_elem == T_OBJECT) {
6580 // If both arrays are object arrays then having the exact types
6581 // for both will remove the need for a subtype check at runtime
6582 // before the call and may make it possible to pick a faster copy
6583 // routine (without a subtype check on every element)
6584 // Do we have the exact type of src?
6585 bool could_have_src = src_spec;
6586 // Do we have the exact type of dest?
6587 bool could_have_dest = dest_spec;
6588 ciKlass* src_k = nullptr;
6589 ciKlass* dest_k = nullptr;
6590 if (!src_spec) {
6591 src_k = src_type->speculative_type_not_null();
6592 if (src_k != nullptr && src_k->is_array_klass()) {
6593 could_have_src = true;
6594 }
6595 }
6596 if (!dest_spec) {
6597 dest_k = dest_type->speculative_type_not_null();
6598 if (dest_k != nullptr && dest_k->is_array_klass()) {
6599 could_have_dest = true;
6600 }
6601 }
6602 if (could_have_src && could_have_dest) {
6603 // If we can have both exact types, emit the missing guards
6604 if (could_have_src && !src_spec) {
6605 src = maybe_cast_profiled_obj(src, src_k, true);
6606 src_type = _gvn.type(src);
6607 top_src = src_type->isa_aryptr();
6608 }
6609 if (could_have_dest && !dest_spec) {
6610 dest = maybe_cast_profiled_obj(dest, dest_k, true);
6611 dest_type = _gvn.type(dest);
6612 top_dest = dest_type->isa_aryptr();
6613 }
6614 }
6615 }
6616 }
6617
6618 ciMethod* trap_method = method();
6619 int trap_bci = bci();
6620 if (saved_jvms_before_guards != nullptr) {
6621 trap_method = alloc->jvms()->method();
6622 trap_bci = alloc->jvms()->bci();
6623 }
6624
6625 bool negative_length_guard_generated = false;
6626
6627 if (!C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_intrinsic) &&
6628 can_emit_guards && !src->is_top() && !dest->is_top()) {
6629 // validate arguments: enables transformation the ArrayCopyNode
6630 validated = true;
6631
6632 RegionNode* slow_region = new RegionNode(1);
6633 record_for_igvn(slow_region);
6634
6635 // (1) src and dest are arrays.
6636 generate_non_array_guard(load_object_klass(src), slow_region, &src);
6637 generate_non_array_guard(load_object_klass(dest), slow_region, &dest);
6638
6639 // (2) src and dest arrays must have elements of the same BasicType
6640 // done at macro expansion or at Ideal transformation time
6641
6642 // (4) src_offset must not be negative.
6643 generate_negative_guard(src_offset, slow_region);
6644
6645 // (5) dest_offset must not be negative.
6646 generate_negative_guard(dest_offset, slow_region);
6647
6648 // (7) src_offset + length must not exceed length of src.
6651 slow_region);
6652
6653 // (8) dest_offset + length must not exceed length of dest.
6654 generate_limit_guard(dest_offset, length,
6655 load_array_length(dest),
6656 slow_region);
6657
6658 // (6) length must not be negative.
6659 // This is also checked in generate_arraycopy() during macro expansion, but
6660 // we also have to check it here for the case where the ArrayCopyNode will
6661 // be eliminated by Escape Analysis.
6662 if (EliminateAllocations) {
6663 generate_negative_guard(length, slow_region);
6664 negative_length_guard_generated = true;
6665 }
6666
6667 // (9) each element of an oop array must be assignable
6668 Node* dest_klass = load_object_klass(dest);
6669 if (src != dest) {
6670 Node* not_subtype_ctrl = gen_subtype_check(src, dest_klass);
6671 slow_region->add_req(not_subtype_ctrl);
6672 }
6673
6674 // TODO 8350865 Fix below logic. Also handle atomicity.
6675 generate_fair_guard(flat_array_test(src), slow_region);
6676 generate_fair_guard(flat_array_test(dest), slow_region);
6677
6678 const TypeKlassPtr* dest_klass_t = _gvn.type(dest_klass)->is_klassptr();
6679 const Type* toop = dest_klass_t->cast_to_exactness(false)->as_instance_type();
6680 src = _gvn.transform(new CheckCastPPNode(control(), src, toop));
6681 src_type = _gvn.type(src);
6682 top_src = src_type->isa_aryptr();
6683
6684 // Handle flat inline type arrays (null-free arrays are handled by the subtype check above)
6685 if (!stopped() && UseArrayFlattening) {
6686 // If dest is flat, src must be flat as well (guaranteed by src <: dest check). Handle flat src here.
6687 assert(top_dest == nullptr || !top_dest->is_flat() || top_src->is_flat(), "src array must be flat");
6688 if (top_src != nullptr && top_src->is_flat()) {
6689 // Src is flat, check that dest is flat as well
6690 if (top_dest != nullptr && !top_dest->is_flat()) {
6691 generate_fair_guard(flat_array_test(dest_klass, /* flat = */ false), slow_region);
6692 // Since dest is flat and src <: dest, dest must have the same type as src.
6693 top_dest = top_src->cast_to_exactness(false);
6694 assert(top_dest->is_flat(), "dest must be flat");
6695 dest = _gvn.transform(new CheckCastPPNode(control(), dest, top_dest));
6696 }
6697 } else if (top_src == nullptr || !top_src->is_not_flat()) {
6698 // Src might be flat and dest might not be flat. Go to the slow path if src is flat.
6699 // TODO 8251971: Optimize for the case when src/dest are later found to be both flat.
6700 assert(top_dest == nullptr || !top_dest->is_flat(), "dest array must not be flat");
6701 generate_fair_guard(flat_array_test(src), slow_region);
6702 if (top_src != nullptr) {
6703 top_src = top_src->cast_to_not_flat();
6704 src = _gvn.transform(new CheckCastPPNode(control(), src, top_src));
6705 }
6706 }
6707 }
6708
6709 {
6710 PreserveJVMState pjvms(this);
6711 set_control(_gvn.transform(slow_region));
6712 uncommon_trap(Deoptimization::Reason_intrinsic,
6713 Deoptimization::Action_make_not_entrant);
6714 assert(stopped(), "Should be stopped");
6715 }
6716 arraycopy_move_allocation_here(alloc, dest, saved_jvms_before_guards, saved_reexecute_sp, new_idx);
6717 }
6718
6719 if (stopped()) {
6720 return true;
6721 }
6722
6723 ArrayCopyNode* ac = ArrayCopyNode::make(this, true, src, src_offset, dest, dest_offset, length, alloc != nullptr, negative_length_guard_generated,
6724 // Create LoadRange and LoadKlass nodes for use during macro expansion here
6725 // so the compiler has a chance to eliminate them: during macro expansion,
6726 // we have to set their control (CastPP nodes are eliminated).
6727 load_object_klass(src), load_object_klass(dest),
6728 load_array_length(src), load_array_length(dest));
6729
6730 ac->set_arraycopy(validated);
6731
6732 Node* n = _gvn.transform(ac);
6733 if (n == ac) {
6734 ac->connect_outputs(this);
6735 } else {
|