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 "classfile/vmClasses.hpp"
26 #include "classfile/vmSymbols.hpp"
27 #include "code/codeCache.hpp"
28 #include "code/compiledIC.hpp"
29 #include "code/nmethod.hpp"
30 #include "code/pcDesc.hpp"
31 #include "code/scopeDesc.hpp"
32 #include "code/vtableStubs.hpp"
33 #include "compiler/compilationMemoryStatistic.hpp"
34 #include "compiler/compileBroker.hpp"
35 #include "compiler/oopMap.hpp"
36 #include "gc/g1/g1HeapRegion.hpp"
37 #include "gc/shared/barrierSet.hpp"
38 #include "gc/shared/collectedHeap.hpp"
39 #include "gc/shared/gcLocker.hpp"
40 #include "interpreter/bytecode.hpp"
41 #include "interpreter/interpreter.hpp"
42 #include "interpreter/linkResolver.hpp"
43 #include "logging/log.hpp"
44 #include "logging/logStream.hpp"
45 #include "memory/oopFactory.hpp"
46 #include "memory/resourceArea.hpp"
47 #include "oops/klass.inline.hpp"
48 #include "oops/objArrayKlass.hpp"
49 #include "oops/oop.inline.hpp"
50 #include "oops/typeArrayOop.inline.hpp"
51 #include "opto/ad.hpp"
52 #include "opto/addnode.hpp"
53 #include "opto/callnode.hpp"
54 #include "opto/cfgnode.hpp"
55 #include "opto/graphKit.hpp"
56 #include "opto/machnode.hpp"
57 #include "opto/matcher.hpp"
58 #include "opto/memnode.hpp"
59 #include "opto/mulnode.hpp"
60 #include "opto/output.hpp"
61 #include "opto/runtime.hpp"
62 #include "opto/subnode.hpp"
63 #include "prims/jvmtiExport.hpp"
64 #include "runtime/atomic.hpp"
65 #include "runtime/frame.inline.hpp"
66 #include "runtime/handles.inline.hpp"
67 #include "runtime/interfaceSupport.inline.hpp"
68 #include "runtime/javaCalls.hpp"
69 #include "runtime/sharedRuntime.hpp"
70 #include "runtime/signature.hpp"
71 #include "runtime/stackWatermarkSet.hpp"
72 #include "runtime/synchronizer.hpp"
73 #include "runtime/threadWXSetters.inline.hpp"
74 #include "runtime/vframe.hpp"
75 #include "runtime/vframe_hp.hpp"
76 #include "runtime/vframeArray.hpp"
77 #include "utilities/copy.hpp"
78 #include "utilities/preserveException.hpp"
79
80
81 // For debugging purposes:
82 // To force FullGCALot inside a runtime function, add the following two lines
83 //
84 // Universe::release_fullgc_alot_dummy();
85 // Universe::heap()->collect();
86 //
87 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
88
89
90 #define C2_BLOB_FIELD_DEFINE(name, type) \
91 type* OptoRuntime:: BLOB_FIELD_NAME(name) = nullptr;
92 #define C2_STUB_FIELD_NAME(name) _ ## name ## _Java
93 #define C2_STUB_FIELD_DEFINE(name, f, t, r) \
94 address OptoRuntime:: C2_STUB_FIELD_NAME(name) = nullptr;
95 #define C2_JVMTI_STUB_FIELD_DEFINE(name) \
96 address OptoRuntime:: STUB_FIELD_NAME(name) = nullptr;
97 C2_STUBS_DO(C2_BLOB_FIELD_DEFINE, C2_STUB_FIELD_DEFINE, C2_JVMTI_STUB_FIELD_DEFINE)
98 #undef C2_BLOB_FIELD_DEFINE
99 #undef C2_STUB_FIELD_DEFINE
100 #undef C2_JVMTI_STUB_FIELD_DEFINE
101
102 // This should be called in an assertion at the start of OptoRuntime routines
103 // which are entered from compiled code (all of them)
104 #ifdef ASSERT
105 static bool check_compiled_frame(JavaThread* thread) {
106 assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
107 RegisterMap map(thread,
108 RegisterMap::UpdateMap::skip,
109 RegisterMap::ProcessFrames::include,
110 RegisterMap::WalkContinuation::skip);
111 frame caller = thread->last_frame().sender(&map);
112 assert(caller.is_compiled_frame(), "not being called from compiled like code");
113 return true;
114 }
115 #endif // ASSERT
116
117 /*
118 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
119 var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
120 if (var == nullptr) { return false; }
121 */
151 fancy_jump, \
152 pass_tls, \
153 pass_retpc); \
154 if (C2_STUB_FIELD_NAME(name) == nullptr) { return false; } \
155
156 #define C2_JVMTI_STUB_C_FUNC(name) CAST_FROM_FN_PTR(address, SharedRuntime::name)
157
158 #define GEN_C2_JVMTI_STUB(name) \
159 STUB_FIELD_NAME(name) = \
160 generate_stub(env, \
161 notify_jvmti_vthread_Type, \
162 C2_JVMTI_STUB_C_FUNC(name), \
163 C2_STUB_NAME(name), \
164 C2_STUB_ID(name), \
165 0, \
166 true, \
167 false); \
168 if (STUB_FIELD_NAME(name) == nullptr) { return false; } \
169
170 bool OptoRuntime::generate(ciEnv* env) {
171
172 C2_STUBS_DO(GEN_C2_BLOB, GEN_C2_STUB, GEN_C2_JVMTI_STUB)
173
174 return true;
175 }
176
177 #undef GEN_C2_BLOB
178
179 #undef C2_STUB_FIELD_NAME
180 #undef C2_STUB_TYPEFUNC
181 #undef C2_STUB_C_FUNC
182 #undef C2_STUB_NAME
183 #undef GEN_C2_STUB
184
185 #undef C2_JVMTI_STUB_C_FUNC
186 #undef GEN_C2_JVMTI_STUB
187 // #undef gen
188
189 const TypeFunc* OptoRuntime::_new_instance_Type = nullptr;
190 const TypeFunc* OptoRuntime::_new_array_Type = nullptr;
257 const TypeFunc* OptoRuntime::_updateBytesAdler32_Type = nullptr;
258 const TypeFunc* OptoRuntime::_osr_end_Type = nullptr;
259 const TypeFunc* OptoRuntime::_register_finalizer_Type = nullptr;
260 #if INCLUDE_JFR
261 const TypeFunc* OptoRuntime::_class_id_load_barrier_Type = nullptr;
262 #endif // INCLUDE_JFR
263 #if INCLUDE_JVMTI
264 const TypeFunc* OptoRuntime::_notify_jvmti_vthread_Type = nullptr;
265 #endif // INCLUDE_JVMTI
266 const TypeFunc* OptoRuntime::_dtrace_method_entry_exit_Type = nullptr;
267 const TypeFunc* OptoRuntime::_dtrace_object_alloc_Type = nullptr;
268
269 // Helper method to do generation of RunTimeStub's
270 address OptoRuntime::generate_stub(ciEnv* env,
271 TypeFunc_generator gen, address C_function,
272 const char *name, StubId stub_id,
273 int is_fancy_jump, bool pass_tls,
274 bool return_pc) {
275
276 // Matching the default directive, we currently have no method to match.
277 DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompileBroker::compiler(CompLevel_full_optimization));
278 CompilationMemoryStatisticMark cmsm(directive);
279 ResourceMark rm;
280 Compile C(env, gen, C_function, name, stub_id, is_fancy_jump, pass_tls, return_pc, directive);
281 DirectivesStack::release(directive);
282 return C.stub_entry_point();
283 }
284
285 const char* OptoRuntime::stub_name(address entry) {
286 #ifndef PRODUCT
287 CodeBlob* cb = CodeCache::find_blob(entry);
288 RuntimeStub* rs =(RuntimeStub *)cb;
289 assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
290 return rs->name();
291 #else
292 // Fast implementation for product mode (maybe it should be inlined too)
293 return "runtime stub";
294 #endif
295 }
296
297 // local methods passed as arguments to stub generator that forward
301 oopDesc* dest, jint dest_pos,
302 jint length, JavaThread* thread) {
303 SharedRuntime::slow_arraycopy_C(src, src_pos, dest, dest_pos, length, thread);
304 }
305
306 void OptoRuntime::complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current) {
307 SharedRuntime::complete_monitor_locking_C(obj, lock, current);
308 }
309
310
311 //=============================================================================
312 // Opto compiler runtime routines
313 //=============================================================================
314
315
316 //=============================allocation======================================
317 // We failed the fast-path allocation. Now we need to do a scavenge or GC
318 // and try allocation again.
319
320 // object allocation
321 JRT_BLOCK_ENTRY(void, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
322 JRT_BLOCK;
323 #ifndef PRODUCT
324 SharedRuntime::_new_instance_ctr++; // new instance requires GC
325 #endif
326 assert(check_compiled_frame(current), "incorrect caller");
327
328 // These checks are cheap to make and support reflective allocation.
329 int lh = klass->layout_helper();
330 if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
331 Handle holder(current, klass->klass_holder()); // keep the klass alive
332 klass->check_valid_for_instantiation(false, THREAD);
333 if (!HAS_PENDING_EXCEPTION) {
334 InstanceKlass::cast(klass)->initialize(THREAD);
335 }
336 }
337
338 if (!HAS_PENDING_EXCEPTION) {
339 // Scavenge and allocate an instance.
340 Handle holder(current, klass->klass_holder()); // keep the klass alive
341 oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
342 current->set_vm_result_oop(result);
343
344 // Pass oops back through thread local storage. Our apparent type to Java
345 // is that we return an oop, but we can block on exit from this routine and
346 // a GC can trash the oop in C's return register. The generated stub will
347 // fetch the oop from TLS after any possible GC.
348 }
349
350 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
351 JRT_BLOCK_END;
352
353 // inform GC that we won't do card marks for initializing writes.
354 SharedRuntime::on_slowpath_allocation_exit(current);
355 JRT_END
356
357
358 // array allocation
359 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
360 JRT_BLOCK;
361 #ifndef PRODUCT
362 SharedRuntime::_new_array_ctr++; // new array requires GC
363 #endif
364 assert(check_compiled_frame(current), "incorrect caller");
365
366 // Scavenge and allocate an instance.
367 oop result;
368
369 if (array_type->is_typeArray_klass()) {
370 // The oopFactory likes to work with the element type.
371 // (We could bypass the oopFactory, since it doesn't add much value.)
372 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
373 result = oopFactory::new_typeArray(elem_type, len, THREAD);
374 } else {
375 // Although the oopFactory likes to work with the elem_type,
376 // the compiler prefers the array_type, since it must already have
377 // that latter value in hand for the fast path.
378 Handle holder(current, array_type->klass_holder()); // keep the array klass alive
379 Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
380 result = oopFactory::new_objArray(elem_type, len, THREAD);
381 }
382
383 // Pass oops back through thread local storage. Our apparent type to Java
384 // is that we return an oop, but we can block on exit from this routine and
385 // a GC can trash the oop in C's return register. The generated stub will
386 // fetch the oop from TLS after any possible GC.
387 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
388 current->set_vm_result_oop(result);
389 JRT_BLOCK_END;
390
391 // inform GC that we won't do card marks for initializing writes.
392 SharedRuntime::on_slowpath_allocation_exit(current);
393 JRT_END
394
395 // array allocation without zeroing
396 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
397 JRT_BLOCK;
398 #ifndef PRODUCT
399 SharedRuntime::_new_array_ctr++; // new array requires GC
400 #endif
401 assert(check_compiled_frame(current), "incorrect caller");
402
403 // Scavenge and allocate an instance.
404 oop result;
405
406 assert(array_type->is_typeArray_klass(), "should be called only for type array");
407 // The oopFactory likes to work with the element type.
408 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
409 result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
410
411 // Pass oops back through thread local storage. Our apparent type to Java
412 // is that we return an oop, but we can block on exit from this routine and
413 // a GC can trash the oop in C's return register. The generated stub will
414 // fetch the oop from TLS after any possible GC.
415 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
416 current->set_vm_result_oop(result);
428 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
429 size_t hs_bytes = arrayOopDesc::base_offset_in_bytes(elem_type);
430 assert(is_aligned(hs_bytes, BytesPerInt), "must be 4 byte aligned");
431 HeapWord* obj = cast_from_oop<HeapWord*>(result);
432 if (!is_aligned(hs_bytes, BytesPerLong)) {
433 *reinterpret_cast<jint*>(reinterpret_cast<char*>(obj) + hs_bytes) = 0;
434 hs_bytes += BytesPerInt;
435 }
436
437 // Optimized zeroing.
438 assert(is_aligned(hs_bytes, BytesPerLong), "must be 8-byte aligned");
439 const size_t aligned_hs = hs_bytes / BytesPerLong;
440 Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
441 }
442
443 JRT_END
444
445 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
446
447 // multianewarray for 2 dimensions
448 JRT_ENTRY(void, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
449 #ifndef PRODUCT
450 SharedRuntime::_multi2_ctr++; // multianewarray for 1 dimension
451 #endif
452 assert(check_compiled_frame(current), "incorrect caller");
453 assert(elem_type->is_klass(), "not a class");
454 jint dims[2];
455 dims[0] = len1;
456 dims[1] = len2;
457 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
458 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
459 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
460 current->set_vm_result_oop(obj);
461 JRT_END
462
463 // multianewarray for 3 dimensions
464 JRT_ENTRY(void, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
465 #ifndef PRODUCT
466 SharedRuntime::_multi3_ctr++; // multianewarray for 1 dimension
467 #endif
468 assert(check_compiled_frame(current), "incorrect caller");
469 assert(elem_type->is_klass(), "not a class");
470 jint dims[3];
471 dims[0] = len1;
472 dims[1] = len2;
473 dims[2] = len3;
474 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
475 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
476 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
477 current->set_vm_result_oop(obj);
478 JRT_END
479
480 // multianewarray for 4 dimensions
481 JRT_ENTRY(void, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
482 #ifndef PRODUCT
483 SharedRuntime::_multi4_ctr++; // multianewarray for 1 dimension
484 #endif
485 assert(check_compiled_frame(current), "incorrect caller");
486 assert(elem_type->is_klass(), "not a class");
487 jint dims[4];
488 dims[0] = len1;
489 dims[1] = len2;
490 dims[2] = len3;
491 dims[3] = len4;
492 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
493 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
494 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
495 current->set_vm_result_oop(obj);
496 JRT_END
497
498 // multianewarray for 5 dimensions
499 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
500 #ifndef PRODUCT
501 SharedRuntime::_multi5_ctr++; // multianewarray for 1 dimension
502 #endif
503 assert(check_compiled_frame(current), "incorrect caller");
504 assert(elem_type->is_klass(), "not a class");
505 jint dims[5];
506 dims[0] = len1;
507 dims[1] = len2;
508 dims[2] = len3;
509 dims[3] = len4;
510 dims[4] = len5;
511 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
512 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
513 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
514 current->set_vm_result_oop(obj);
515 JRT_END
516
517 JRT_ENTRY(void, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
518 assert(check_compiled_frame(current), "incorrect caller");
519 assert(elem_type->is_klass(), "not a class");
520 assert(oop(dims)->is_typeArray(), "not an array");
521
522 ResourceMark rm;
523 jint len = dims->length();
524 assert(len > 0, "Dimensions array should contain data");
525 jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
526 ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
527 c_dims, len);
528
529 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
530 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
531 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
532 current->set_vm_result_oop(obj);
533 JRT_END
534
535 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
536
537 // Very few notify/notifyAll operations find any threads on the waitset, so
538 // the dominant fast-path is to simply return.
539 // Relatedly, it's critical that notify/notifyAll be fast in order to
540 // reduce lock hold times.
541 if (!SafepointSynchronize::is_synchronizing()) {
542 if (ObjectSynchronizer::quick_notify(obj, current, false)) {
543 return;
544 }
545 }
546
547 // This is the case the fast-path above isn't provisioned to handle.
548 // The fast-path is designed to handle frequently arising cases in an efficient manner.
549 // (The fast-path is just a degenerate variant of the slow-path).
550 // Perform the dreaded state transition and pass control into the slow-path.
551 JRT_BLOCK;
552 Handle h_obj(current, obj);
553 ObjectSynchronizer::notify(h_obj, CHECK);
554 JRT_BLOCK_END;
555 JRT_END
556
557 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
558
559 if (!SafepointSynchronize::is_synchronizing() ) {
560 if (ObjectSynchronizer::quick_notify(obj, current, true)) {
561 return;
562 }
563 }
564
565 // This is the case the fast-path above isn't provisioned to handle.
566 // The fast-path is designed to handle frequently arising cases in an efficient manner.
567 // (The fast-path is just a degenerate variant of the slow-path).
568 // Perform the dreaded state transition and pass control into the slow-path.
569 JRT_BLOCK;
570 Handle h_obj(current, obj);
571 ObjectSynchronizer::notifyall(h_obj, CHECK);
572 JRT_BLOCK_END;
573 JRT_END
574
575 static const TypeFunc* make_new_instance_Type() {
576 // create input type (domain)
577 const Type **fields = TypeTuple::fields(1);
1787 assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1788 switch (register_save_policy[reg]) {
1789 case 'C': return false; //SOC
1790 case 'E': return true ; //SOE
1791 case 'N': return false; //NS
1792 case 'A': return false; //AS
1793 }
1794 ShouldNotReachHere();
1795 return false;
1796 }
1797
1798 //-----------------------------------------------------------------------
1799 // Exceptions
1800 //
1801
1802 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1803
1804 // The method is an entry that is always called by a C++ method not
1805 // directly from compiled code. Compiled code will call the C++ method following.
1806 // We can't allow async exception to be installed during exception processing.
1807 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1808 // The frame we rethrow the exception to might not have been processed by the GC yet.
1809 // The stack watermark barrier takes care of detecting that and ensuring the frame
1810 // has updated oops.
1811 StackWatermarkSet::after_unwind(current);
1812
1813 // Do not confuse exception_oop with pending_exception. The exception_oop
1814 // is only used to pass arguments into the method. Not for general
1815 // exception handling. DO NOT CHANGE IT to use pending_exception, since
1816 // the runtime stubs checks this on exit.
1817 assert(current->exception_oop() != nullptr, "exception oop is found");
1818 address handler_address = nullptr;
1819
1820 Handle exception(current, current->exception_oop());
1821 address pc = current->exception_pc();
1822
1823 // Clear out the exception oop and pc since looking up an
1824 // exception handler can cause class loading, which might throw an
1825 // exception and those fields are expected to be clear during
1826 // normal bytecode execution.
1827 current->clear_exception_oop_and_pc();
2060 frame caller_frame = stub_frame.sender(®_map);
2061 return caller_frame.is_deoptimized_frame();
2062 }
2063
2064 static const TypeFunc* make_register_finalizer_Type() {
2065 // create input type (domain)
2066 const Type **fields = TypeTuple::fields(1);
2067 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // oop; Receiver
2068 // // The JavaThread* is passed to each routine as the last argument
2069 // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // JavaThread *; Executing thread
2070 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2071
2072 // create result type (range)
2073 fields = TypeTuple::fields(0);
2074
2075 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2076
2077 return TypeFunc::make(domain,range);
2078 }
2079
2080 #if INCLUDE_JFR
2081 static const TypeFunc* make_class_id_load_barrier_Type() {
2082 // create input type (domain)
2083 const Type **fields = TypeTuple::fields(1);
2084 fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
2085 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
2086
2087 // create result type (range)
2088 fields = TypeTuple::fields(0);
2089
2090 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
2091
2092 return TypeFunc::make(domain,range);
2093 }
2094 #endif // INCLUDE_JFR
2095
2096 //-----------------------------------------------------------------------------
2097 static const TypeFunc* make_dtrace_method_entry_exit_Type() {
2098 // create input type (domain)
2099 const Type **fields = TypeTuple::fields(2);
2100 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2101 fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM; // Method*; Method we are entering
2102 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2103
2104 // create result type (range)
2105 fields = TypeTuple::fields(0);
2106
2107 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2108
2109 return TypeFunc::make(domain,range);
2110 }
2111
2112 static const TypeFunc* make_dtrace_object_alloc_Type() {
2113 // create input type (domain)
2114 const Type **fields = TypeTuple::fields(2);
2115 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2116 fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL; // oop; newly allocated object
2117
2118 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2119
2120 // create result type (range)
2121 fields = TypeTuple::fields(0);
2122
2123 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2124
2125 return TypeFunc::make(domain,range);
2126 }
2127
2128 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer_C(oopDesc* obj, JavaThread* current))
2129 assert(oopDesc::is_oop(obj), "must be a valid oop");
2130 assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
2131 InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
2132 JRT_END
2133
2134 //-----------------------------------------------------------------------------
2135
2136 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
2137
2138 //
2139 // dump the collected NamedCounters.
2140 //
2141 void OptoRuntime::print_named_counters() {
2142 int total_lock_count = 0;
2143 int eliminated_lock_count = 0;
2144
2145 NamedCounter* c = _named_counters;
2146 while (c) {
2147 if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
2148 int count = c->count();
2149 if (count > 0) {
2150 bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
2151 if (Verbose) {
2152 tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
2153 }
2296 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
2297 trace_exception_counter++;
2298 stringStream tempst;
2299
2300 tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
2301 exception_oop->print_value_on(&tempst);
2302 tempst.print(" in ");
2303 CodeBlob* blob = CodeCache::find_blob(exception_pc);
2304 if (blob->is_nmethod()) {
2305 blob->as_nmethod()->method()->print_value_on(&tempst);
2306 } else if (blob->is_runtime_stub()) {
2307 tempst.print("<runtime-stub>");
2308 } else {
2309 tempst.print("<unknown>");
2310 }
2311 tempst.print(" at " INTPTR_FORMAT, p2i(exception_pc));
2312 tempst.print("]");
2313
2314 st->print_raw_cr(tempst.freeze());
2315 }
|
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 "classfile/vmClasses.hpp"
26 #include "classfile/vmSymbols.hpp"
27 #include "code/codeCache.hpp"
28 #include "code/compiledIC.hpp"
29 #include "code/nmethod.hpp"
30 #include "code/pcDesc.hpp"
31 #include "code/scopeDesc.hpp"
32 #include "code/vtableStubs.hpp"
33 #include "compiler/compilationMemoryStatistic.hpp"
34 #include "compiler/compileBroker.hpp"
35 #include "compiler/compilerDefinitions.inline.hpp"
36 #include "compiler/oopMap.hpp"
37 #include "gc/g1/g1HeapRegion.hpp"
38 #include "gc/shared/barrierSet.hpp"
39 #include "gc/shared/collectedHeap.hpp"
40 #include "gc/shared/gcLocker.hpp"
41 #include "interpreter/bytecode.hpp"
42 #include "interpreter/interpreter.hpp"
43 #include "interpreter/linkResolver.hpp"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "memory/oopFactory.hpp"
47 #include "memory/resourceArea.hpp"
48 #include "oops/klass.inline.hpp"
49 #include "oops/objArrayKlass.hpp"
50 #include "oops/oop.inline.hpp"
51 #include "oops/typeArrayOop.inline.hpp"
52 #include "opto/ad.hpp"
53 #include "opto/addnode.hpp"
54 #include "opto/callnode.hpp"
55 #include "opto/cfgnode.hpp"
56 #include "opto/graphKit.hpp"
57 #include "opto/machnode.hpp"
58 #include "opto/matcher.hpp"
59 #include "opto/memnode.hpp"
60 #include "opto/mulnode.hpp"
61 #include "opto/output.hpp"
62 #include "opto/runtime.hpp"
63 #include "opto/subnode.hpp"
64 #include "prims/jvmtiExport.hpp"
65 #include "runtime/atomic.hpp"
66 #include "runtime/frame.inline.hpp"
67 #include "runtime/handles.inline.hpp"
68 #include "runtime/interfaceSupport.inline.hpp"
69 #include "runtime/java.hpp"
70 #include "runtime/javaCalls.hpp"
71 #include "runtime/perfData.inline.hpp"
72 #include "runtime/sharedRuntime.hpp"
73 #include "runtime/signature.hpp"
74 #include "runtime/stackWatermarkSet.hpp"
75 #include "runtime/synchronizer.hpp"
76 #include "runtime/threadWXSetters.inline.hpp"
77 #include "runtime/vframe.hpp"
78 #include "runtime/vframe_hp.hpp"
79 #include "runtime/vframeArray.hpp"
80 #include "services/management.hpp"
81 #include "utilities/copy.hpp"
82 #include "utilities/preserveException.hpp"
83
84
85 // For debugging purposes:
86 // To force FullGCALot inside a runtime function, add the following two lines
87 //
88 // Universe::release_fullgc_alot_dummy();
89 // Universe::heap()->collect();
90 //
91 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
92
93
94 #define C2_BLOB_FIELD_DEFINE(name, type) \
95 type* OptoRuntime:: BLOB_FIELD_NAME(name) = nullptr;
96 #define C2_STUB_FIELD_NAME(name) _ ## name ## _Java
97 #define C2_STUB_FIELD_DEFINE(name, f, t, r) \
98 address OptoRuntime:: C2_STUB_FIELD_NAME(name) = nullptr;
99 #define C2_JVMTI_STUB_FIELD_DEFINE(name) \
100 address OptoRuntime:: STUB_FIELD_NAME(name) = nullptr;
101 C2_STUBS_DO(C2_BLOB_FIELD_DEFINE, C2_STUB_FIELD_DEFINE, C2_JVMTI_STUB_FIELD_DEFINE)
102 #undef C2_BLOB_FIELD_DEFINE
103 #undef C2_STUB_FIELD_DEFINE
104 #undef C2_JVMTI_STUB_FIELD_DEFINE
105
106 address OptoRuntime::_vtable_must_compile_Java = nullptr;
107
108 PerfCounter* _perf_OptoRuntime_class_init_barrier_redundant_count = nullptr;
109
110 // This should be called in an assertion at the start of OptoRuntime routines
111 // which are entered from compiled code (all of them)
112 #ifdef ASSERT
113 static bool check_compiled_frame(JavaThread* thread) {
114 assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
115 RegisterMap map(thread,
116 RegisterMap::UpdateMap::skip,
117 RegisterMap::ProcessFrames::include,
118 RegisterMap::WalkContinuation::skip);
119 frame caller = thread->last_frame().sender(&map);
120 assert(caller.is_compiled_frame(), "not being called from compiled like code");
121 return true;
122 }
123 #endif // ASSERT
124
125 /*
126 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
127 var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
128 if (var == nullptr) { return false; }
129 */
159 fancy_jump, \
160 pass_tls, \
161 pass_retpc); \
162 if (C2_STUB_FIELD_NAME(name) == nullptr) { return false; } \
163
164 #define C2_JVMTI_STUB_C_FUNC(name) CAST_FROM_FN_PTR(address, SharedRuntime::name)
165
166 #define GEN_C2_JVMTI_STUB(name) \
167 STUB_FIELD_NAME(name) = \
168 generate_stub(env, \
169 notify_jvmti_vthread_Type, \
170 C2_JVMTI_STUB_C_FUNC(name), \
171 C2_STUB_NAME(name), \
172 C2_STUB_ID(name), \
173 0, \
174 true, \
175 false); \
176 if (STUB_FIELD_NAME(name) == nullptr) { return false; } \
177
178 bool OptoRuntime::generate(ciEnv* env) {
179 init_counters();
180
181 C2_STUBS_DO(GEN_C2_BLOB, GEN_C2_STUB, GEN_C2_JVMTI_STUB)
182
183 return true;
184 }
185
186 #undef GEN_C2_BLOB
187
188 #undef C2_STUB_FIELD_NAME
189 #undef C2_STUB_TYPEFUNC
190 #undef C2_STUB_C_FUNC
191 #undef C2_STUB_NAME
192 #undef GEN_C2_STUB
193
194 #undef C2_JVMTI_STUB_C_FUNC
195 #undef GEN_C2_JVMTI_STUB
196 // #undef gen
197
198 const TypeFunc* OptoRuntime::_new_instance_Type = nullptr;
199 const TypeFunc* OptoRuntime::_new_array_Type = nullptr;
266 const TypeFunc* OptoRuntime::_updateBytesAdler32_Type = nullptr;
267 const TypeFunc* OptoRuntime::_osr_end_Type = nullptr;
268 const TypeFunc* OptoRuntime::_register_finalizer_Type = nullptr;
269 #if INCLUDE_JFR
270 const TypeFunc* OptoRuntime::_class_id_load_barrier_Type = nullptr;
271 #endif // INCLUDE_JFR
272 #if INCLUDE_JVMTI
273 const TypeFunc* OptoRuntime::_notify_jvmti_vthread_Type = nullptr;
274 #endif // INCLUDE_JVMTI
275 const TypeFunc* OptoRuntime::_dtrace_method_entry_exit_Type = nullptr;
276 const TypeFunc* OptoRuntime::_dtrace_object_alloc_Type = nullptr;
277
278 // Helper method to do generation of RunTimeStub's
279 address OptoRuntime::generate_stub(ciEnv* env,
280 TypeFunc_generator gen, address C_function,
281 const char *name, StubId stub_id,
282 int is_fancy_jump, bool pass_tls,
283 bool return_pc) {
284
285 // Matching the default directive, we currently have no method to match.
286 DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompilerThread::current()->compiler());
287 CompilationMemoryStatisticMark cmsm(directive);
288 ResourceMark rm;
289 Compile C(env, gen, C_function, name, stub_id, is_fancy_jump, pass_tls, return_pc, directive);
290 DirectivesStack::release(directive);
291 return C.stub_entry_point();
292 }
293
294 const char* OptoRuntime::stub_name(address entry) {
295 #ifndef PRODUCT
296 CodeBlob* cb = CodeCache::find_blob(entry);
297 RuntimeStub* rs =(RuntimeStub *)cb;
298 assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
299 return rs->name();
300 #else
301 // Fast implementation for product mode (maybe it should be inlined too)
302 return "runtime stub";
303 #endif
304 }
305
306 // local methods passed as arguments to stub generator that forward
310 oopDesc* dest, jint dest_pos,
311 jint length, JavaThread* thread) {
312 SharedRuntime::slow_arraycopy_C(src, src_pos, dest, dest_pos, length, thread);
313 }
314
315 void OptoRuntime::complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current) {
316 SharedRuntime::complete_monitor_locking_C(obj, lock, current);
317 }
318
319
320 //=============================================================================
321 // Opto compiler runtime routines
322 //=============================================================================
323
324
325 //=============================allocation======================================
326 // We failed the fast-path allocation. Now we need to do a scavenge or GC
327 // and try allocation again.
328
329 // object allocation
330 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_instance_C, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
331 JRT_BLOCK;
332 #ifndef PRODUCT
333 SharedRuntime::_new_instance_ctr++; // new instance requires GC
334 #endif
335 assert(check_compiled_frame(current), "incorrect caller");
336
337 // These checks are cheap to make and support reflective allocation.
338 int lh = klass->layout_helper();
339 if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
340 Handle holder(current, klass->klass_holder()); // keep the klass alive
341 klass->check_valid_for_instantiation(false, THREAD);
342 if (!HAS_PENDING_EXCEPTION) {
343 InstanceKlass::cast(klass)->initialize(THREAD);
344 }
345 }
346
347 if (!HAS_PENDING_EXCEPTION) {
348 // Scavenge and allocate an instance.
349 Handle holder(current, klass->klass_holder()); // keep the klass alive
350 oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
351 current->set_vm_result_oop(result);
352
353 // Pass oops back through thread local storage. Our apparent type to Java
354 // is that we return an oop, but we can block on exit from this routine and
355 // a GC can trash the oop in C's return register. The generated stub will
356 // fetch the oop from TLS after any possible GC.
357 }
358
359 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
360 JRT_BLOCK_END;
361
362 // inform GC that we won't do card marks for initializing writes.
363 SharedRuntime::on_slowpath_allocation_exit(current);
364 JRT_END
365
366
367 // array allocation
368 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_C, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
369 JRT_BLOCK;
370 #ifndef PRODUCT
371 SharedRuntime::_new_array_ctr++; // new array requires GC
372 #endif
373 assert(check_compiled_frame(current), "incorrect caller");
374
375 // Scavenge and allocate an instance.
376 oop result;
377
378 if (array_type->is_typeArray_klass()) {
379 // The oopFactory likes to work with the element type.
380 // (We could bypass the oopFactory, since it doesn't add much value.)
381 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
382 result = oopFactory::new_typeArray(elem_type, len, THREAD);
383 } else {
384 // Although the oopFactory likes to work with the elem_type,
385 // the compiler prefers the array_type, since it must already have
386 // that latter value in hand for the fast path.
387 Handle holder(current, array_type->klass_holder()); // keep the array klass alive
388 Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
389 result = oopFactory::new_objArray(elem_type, len, THREAD);
390 }
391
392 // Pass oops back through thread local storage. Our apparent type to Java
393 // is that we return an oop, but we can block on exit from this routine and
394 // a GC can trash the oop in C's return register. The generated stub will
395 // fetch the oop from TLS after any possible GC.
396 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
397 current->set_vm_result_oop(result);
398 JRT_BLOCK_END;
399
400 // inform GC that we won't do card marks for initializing writes.
401 SharedRuntime::on_slowpath_allocation_exit(current);
402 JRT_END
403
404 // array allocation without zeroing
405 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_nozero_C, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
406 JRT_BLOCK;
407 #ifndef PRODUCT
408 SharedRuntime::_new_array_ctr++; // new array requires GC
409 #endif
410 assert(check_compiled_frame(current), "incorrect caller");
411
412 // Scavenge and allocate an instance.
413 oop result;
414
415 assert(array_type->is_typeArray_klass(), "should be called only for type array");
416 // The oopFactory likes to work with the element type.
417 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
418 result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
419
420 // Pass oops back through thread local storage. Our apparent type to Java
421 // is that we return an oop, but we can block on exit from this routine and
422 // a GC can trash the oop in C's return register. The generated stub will
423 // fetch the oop from TLS after any possible GC.
424 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
425 current->set_vm_result_oop(result);
437 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
438 size_t hs_bytes = arrayOopDesc::base_offset_in_bytes(elem_type);
439 assert(is_aligned(hs_bytes, BytesPerInt), "must be 4 byte aligned");
440 HeapWord* obj = cast_from_oop<HeapWord*>(result);
441 if (!is_aligned(hs_bytes, BytesPerLong)) {
442 *reinterpret_cast<jint*>(reinterpret_cast<char*>(obj) + hs_bytes) = 0;
443 hs_bytes += BytesPerInt;
444 }
445
446 // Optimized zeroing.
447 assert(is_aligned(hs_bytes, BytesPerLong), "must be 8-byte aligned");
448 const size_t aligned_hs = hs_bytes / BytesPerLong;
449 Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
450 }
451
452 JRT_END
453
454 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
455
456 // multianewarray for 2 dimensions
457 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray2_C, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
458 #ifndef PRODUCT
459 SharedRuntime::_multi2_ctr++; // multianewarray for 1 dimension
460 #endif
461 assert(check_compiled_frame(current), "incorrect caller");
462 assert(elem_type->is_klass(), "not a class");
463 jint dims[2];
464 dims[0] = len1;
465 dims[1] = len2;
466 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
467 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
468 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
469 current->set_vm_result_oop(obj);
470 JRT_END
471
472 // multianewarray for 3 dimensions
473 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray3_C, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
474 #ifndef PRODUCT
475 SharedRuntime::_multi3_ctr++; // multianewarray for 1 dimension
476 #endif
477 assert(check_compiled_frame(current), "incorrect caller");
478 assert(elem_type->is_klass(), "not a class");
479 jint dims[3];
480 dims[0] = len1;
481 dims[1] = len2;
482 dims[2] = len3;
483 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
484 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
485 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
486 current->set_vm_result_oop(obj);
487 JRT_END
488
489 // multianewarray for 4 dimensions
490 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray4_C, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
491 #ifndef PRODUCT
492 SharedRuntime::_multi4_ctr++; // multianewarray for 1 dimension
493 #endif
494 assert(check_compiled_frame(current), "incorrect caller");
495 assert(elem_type->is_klass(), "not a class");
496 jint dims[4];
497 dims[0] = len1;
498 dims[1] = len2;
499 dims[2] = len3;
500 dims[3] = len4;
501 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
502 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
503 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
504 current->set_vm_result_oop(obj);
505 JRT_END
506
507 // multianewarray for 5 dimensions
508 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
509 #ifndef PRODUCT
510 SharedRuntime::_multi5_ctr++; // multianewarray for 1 dimension
511 #endif
512 assert(check_compiled_frame(current), "incorrect caller");
513 assert(elem_type->is_klass(), "not a class");
514 jint dims[5];
515 dims[0] = len1;
516 dims[1] = len2;
517 dims[2] = len3;
518 dims[3] = len4;
519 dims[4] = len5;
520 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
521 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
522 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
523 current->set_vm_result_oop(obj);
524 JRT_END
525
526 JRT_ENTRY_PROF(void, OptoRuntime, multianewarrayN_C, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
527 assert(check_compiled_frame(current), "incorrect caller");
528 assert(elem_type->is_klass(), "not a class");
529 assert(oop(dims)->is_typeArray(), "not an array");
530
531 ResourceMark rm;
532 jint len = dims->length();
533 assert(len > 0, "Dimensions array should contain data");
534 jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
535 ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
536 c_dims, len);
537
538 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
539 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
540 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
541 current->set_vm_result_oop(obj);
542 JRT_END
543
544 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notify_C, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
545
546 // Very few notify/notifyAll operations find any threads on the waitset, so
547 // the dominant fast-path is to simply return.
548 // Relatedly, it's critical that notify/notifyAll be fast in order to
549 // reduce lock hold times.
550 if (!SafepointSynchronize::is_synchronizing()) {
551 if (ObjectSynchronizer::quick_notify(obj, current, false)) {
552 return;
553 }
554 }
555
556 // This is the case the fast-path above isn't provisioned to handle.
557 // The fast-path is designed to handle frequently arising cases in an efficient manner.
558 // (The fast-path is just a degenerate variant of the slow-path).
559 // Perform the dreaded state transition and pass control into the slow-path.
560 JRT_BLOCK;
561 Handle h_obj(current, obj);
562 ObjectSynchronizer::notify(h_obj, CHECK);
563 JRT_BLOCK_END;
564 JRT_END
565
566 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notifyAll_C, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
567
568 if (!SafepointSynchronize::is_synchronizing() ) {
569 if (ObjectSynchronizer::quick_notify(obj, current, true)) {
570 return;
571 }
572 }
573
574 // This is the case the fast-path above isn't provisioned to handle.
575 // The fast-path is designed to handle frequently arising cases in an efficient manner.
576 // (The fast-path is just a degenerate variant of the slow-path).
577 // Perform the dreaded state transition and pass control into the slow-path.
578 JRT_BLOCK;
579 Handle h_obj(current, obj);
580 ObjectSynchronizer::notifyall(h_obj, CHECK);
581 JRT_BLOCK_END;
582 JRT_END
583
584 static const TypeFunc* make_new_instance_Type() {
585 // create input type (domain)
586 const Type **fields = TypeTuple::fields(1);
1796 assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1797 switch (register_save_policy[reg]) {
1798 case 'C': return false; //SOC
1799 case 'E': return true ; //SOE
1800 case 'N': return false; //NS
1801 case 'A': return false; //AS
1802 }
1803 ShouldNotReachHere();
1804 return false;
1805 }
1806
1807 //-----------------------------------------------------------------------
1808 // Exceptions
1809 //
1810
1811 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1812
1813 // The method is an entry that is always called by a C++ method not
1814 // directly from compiled code. Compiled code will call the C++ method following.
1815 // We can't allow async exception to be installed during exception processing.
1816 JRT_ENTRY_NO_ASYNC_PROF(address, OptoRuntime, handle_exception_C_helper, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1817 // The frame we rethrow the exception to might not have been processed by the GC yet.
1818 // The stack watermark barrier takes care of detecting that and ensuring the frame
1819 // has updated oops.
1820 StackWatermarkSet::after_unwind(current);
1821
1822 // Do not confuse exception_oop with pending_exception. The exception_oop
1823 // is only used to pass arguments into the method. Not for general
1824 // exception handling. DO NOT CHANGE IT to use pending_exception, since
1825 // the runtime stubs checks this on exit.
1826 assert(current->exception_oop() != nullptr, "exception oop is found");
1827 address handler_address = nullptr;
1828
1829 Handle exception(current, current->exception_oop());
1830 address pc = current->exception_pc();
1831
1832 // Clear out the exception oop and pc since looking up an
1833 // exception handler can cause class loading, which might throw an
1834 // exception and those fields are expected to be clear during
1835 // normal bytecode execution.
1836 current->clear_exception_oop_and_pc();
2069 frame caller_frame = stub_frame.sender(®_map);
2070 return caller_frame.is_deoptimized_frame();
2071 }
2072
2073 static const TypeFunc* make_register_finalizer_Type() {
2074 // create input type (domain)
2075 const Type **fields = TypeTuple::fields(1);
2076 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // oop; Receiver
2077 // // The JavaThread* is passed to each routine as the last argument
2078 // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // JavaThread *; Executing thread
2079 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2080
2081 // create result type (range)
2082 fields = TypeTuple::fields(0);
2083
2084 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2085
2086 return TypeFunc::make(domain,range);
2087 }
2088
2089 const TypeFunc *OptoRuntime::class_init_barrier_Type() {
2090 // create input type (domain)
2091 const Type** fields = TypeTuple::fields(1);
2092 fields[TypeFunc::Parms+0] = TypeKlassPtr::NOTNULL;
2093 // // The JavaThread* is passed to each routine as the last argument
2094 // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // JavaThread *; Executing thread
2095 const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+1, fields);
2096
2097 // create result type (range)
2098 fields = TypeTuple::fields(0);
2099 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
2100 return TypeFunc::make(domain,range);
2101 }
2102
2103 #if INCLUDE_JFR
2104 static const TypeFunc* make_class_id_load_barrier_Type() {
2105 // create input type (domain)
2106 const Type **fields = TypeTuple::fields(1);
2107 fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
2108 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
2109
2110 // create result type (range)
2111 fields = TypeTuple::fields(0);
2112
2113 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
2114
2115 return TypeFunc::make(domain,range);
2116 }
2117 #endif // INCLUDE_JFR
2118
2119 //-----------------------------------------------------------------------------
2120 // runtime upcall support
2121 const TypeFunc *OptoRuntime::runtime_up_call_Type() {
2122 // create input type (domain)
2123 const Type **fields = TypeTuple::fields(1);
2124 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2125 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2126
2127 // create result type (range)
2128 fields = TypeTuple::fields(0);
2129
2130 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2131
2132 return TypeFunc::make(domain,range);
2133 }
2134
2135 //-----------------------------------------------------------------------------
2136 static const TypeFunc* make_dtrace_method_entry_exit_Type() {
2137 // create input type (domain)
2138 const Type **fields = TypeTuple::fields(2);
2139 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2140 fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM; // Method*; Method we are entering
2141 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2142
2143 // create result type (range)
2144 fields = TypeTuple::fields(0);
2145
2146 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2147
2148 return TypeFunc::make(domain,range);
2149 }
2150
2151 static const TypeFunc* make_dtrace_object_alloc_Type() {
2152 // create input type (domain)
2153 const Type **fields = TypeTuple::fields(2);
2154 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2155 fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL; // oop; newly allocated object
2156
2157 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2158
2159 // create result type (range)
2160 fields = TypeTuple::fields(0);
2161
2162 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2163
2164 return TypeFunc::make(domain,range);
2165 }
2166
2167 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, register_finalizer_C, OptoRuntime::register_finalizer_C(oopDesc* obj, JavaThread* current))
2168 assert(oopDesc::is_oop(obj), "must be a valid oop");
2169 assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
2170 InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
2171 JRT_END
2172
2173 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, class_init_barrier_C, OptoRuntime::class_init_barrier_C(Klass* k, JavaThread* current))
2174 InstanceKlass* ik = InstanceKlass::cast(k);
2175 if (ik->should_be_initialized()) {
2176 ik->initialize(CHECK);
2177 } else if (UsePerfData) {
2178 _perf_OptoRuntime_class_init_barrier_redundant_count->inc();
2179 }
2180 JRT_END
2181
2182 //-----------------------------------------------------------------------------
2183
2184 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
2185
2186 //
2187 // dump the collected NamedCounters.
2188 //
2189 void OptoRuntime::print_named_counters() {
2190 int total_lock_count = 0;
2191 int eliminated_lock_count = 0;
2192
2193 NamedCounter* c = _named_counters;
2194 while (c) {
2195 if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
2196 int count = c->count();
2197 if (count > 0) {
2198 bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
2199 if (Verbose) {
2200 tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
2201 }
2344 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
2345 trace_exception_counter++;
2346 stringStream tempst;
2347
2348 tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
2349 exception_oop->print_value_on(&tempst);
2350 tempst.print(" in ");
2351 CodeBlob* blob = CodeCache::find_blob(exception_pc);
2352 if (blob->is_nmethod()) {
2353 blob->as_nmethod()->method()->print_value_on(&tempst);
2354 } else if (blob->is_runtime_stub()) {
2355 tempst.print("<runtime-stub>");
2356 } else {
2357 tempst.print("<unknown>");
2358 }
2359 tempst.print(" at " INTPTR_FORMAT, p2i(exception_pc));
2360 tempst.print("]");
2361
2362 st->print_raw_cr(tempst.freeze());
2363 }
2364
2365 #define DO_COUNTERS2(macro2, macro1) \
2366 macro2(OptoRuntime, new_instance_C) \
2367 macro2(OptoRuntime, new_array_C) \
2368 macro2(OptoRuntime, new_array_nozero_C) \
2369 macro2(OptoRuntime, multianewarray2_C) \
2370 macro2(OptoRuntime, multianewarray3_C) \
2371 macro2(OptoRuntime, multianewarray4_C) \
2372 macro2(OptoRuntime, multianewarrayN_C) \
2373 macro2(OptoRuntime, monitor_notify_C) \
2374 macro2(OptoRuntime, monitor_notifyAll_C) \
2375 macro2(OptoRuntime, handle_exception_C_helper) \
2376 macro2(OptoRuntime, register_finalizer_C) \
2377 macro2(OptoRuntime, class_init_barrier_C) \
2378 macro1(OptoRuntime, class_init_barrier_redundant)
2379
2380 #define INIT_COUNTER_TIME_AND_CNT(sub, name) \
2381 NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
2382 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
2383
2384 #define INIT_COUNTER_CNT(sub, name) \
2385 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
2386
2387 void OptoRuntime::init_counters() {
2388 assert(CompilerConfig::is_c2_enabled(), "");
2389
2390 if (UsePerfData) {
2391 EXCEPTION_MARK;
2392
2393 DO_COUNTERS2(INIT_COUNTER_TIME_AND_CNT, INIT_COUNTER_CNT)
2394
2395 if (HAS_PENDING_EXCEPTION) {
2396 vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
2397 }
2398 }
2399 }
2400 #undef INIT_COUNTER_TIME_AND_CNT
2401 #undef INIT_COUNTER_CNT
2402
2403 #define PRINT_COUNTER_TIME_AND_CNT(sub, name) { \
2404 jlong count = _perf_##sub##_##name##_count->get_value(); \
2405 if (count > 0) { \
2406 st->print_cr(" %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
2407 _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
2408 _perf_##sub##_##name##_timer->thread_counter_value_us(), \
2409 count); \
2410 }}
2411
2412 #define PRINT_COUNTER_CNT(sub, name) { \
2413 jlong count = _perf_##sub##_##name##_count->get_value(); \
2414 if (count > 0) { \
2415 st->print_cr(" %-30s = " JLONG_FORMAT_W(5) " events", #name, count); \
2416 }}
2417
2418 void OptoRuntime::print_counters_on(outputStream* st) {
2419 if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c2_enabled()) {
2420 DO_COUNTERS2(PRINT_COUNTER_TIME_AND_CNT, PRINT_COUNTER_CNT)
2421 } else {
2422 st->print_cr(" OptoRuntime: no info (%s is disabled)",
2423 (!CompilerConfig::is_c2_enabled() ? "C2" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
2424 }
2425 }
2426
2427 #undef PRINT_COUNTER_TIME_AND_CNT
2428 #undef PRINT_COUNTER_CNT
2429 #undef DO_COUNTERS2
|