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/atomicAccess.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/mountUnmountDisabler.hpp"
70 #include "runtime/sharedRuntime.hpp"
71 #include "runtime/signature.hpp"
72 #include "runtime/stackWatermarkSet.hpp"
73 #include "runtime/synchronizer.hpp"
74 #include "runtime/threadWXSetters.inline.hpp"
75 #include "runtime/vframe.hpp"
76 #include "runtime/vframe_hp.hpp"
77 #include "runtime/vframeArray.hpp"
78 #include "utilities/copy.hpp"
79 #include "utilities/preserveException.hpp"
80
81
82 // For debugging purposes:
83 // To force FullGCALot inside a runtime function, add the following two lines
84 //
85 // Universe::release_fullgc_alot_dummy();
86 // Universe::heap()->collect();
87 //
88 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
89
90
91 #define C2_BLOB_FIELD_DEFINE(name, type) \
92 type* OptoRuntime:: BLOB_FIELD_NAME(name) = nullptr;
93 #define C2_STUB_FIELD_NAME(name) _ ## name ## _Java
94 #define C2_STUB_FIELD_DEFINE(name, f, t, r) \
95 address OptoRuntime:: C2_STUB_FIELD_NAME(name) = nullptr;
96 C2_STUBS_DO(C2_BLOB_FIELD_DEFINE, C2_STUB_FIELD_DEFINE)
97 #undef C2_BLOB_FIELD_DEFINE
98 #undef C2_STUB_FIELD_DEFINE
99
100 // This should be called in an assertion at the start of OptoRuntime routines
101 // which are entered from compiled code (all of them)
102 #ifdef ASSERT
103 static bool check_compiled_frame(JavaThread* thread) {
104 assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
105 RegisterMap map(thread,
106 RegisterMap::UpdateMap::skip,
107 RegisterMap::ProcessFrames::include,
108 RegisterMap::WalkContinuation::skip);
109 frame caller = thread->last_frame().sender(&map);
110 assert(caller.is_compiled_frame(), "not being called from compiled like code");
111 return true;
112 }
113 #endif // ASSERT
114
115 /*
116 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
117 var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
118 if (var == nullptr) { return false; }
119 */
135 // from the stub name by appending suffix '_C'. However, in two cases
136 // a common target method also needs to be called from shared runtime
137 // stubs. In these two cases the opto stubs rely on method
138 // imlementations defined in class SharedRuntime. The following
139 // defines temporarily rebind the generated names to reference the
140 // relevant implementations.
141
142 #define GEN_C2_STUB(name, fancy_jump, pass_tls, pass_retpc ) \
143 C2_STUB_FIELD_NAME(name) = \
144 generate_stub(env, \
145 C2_STUB_TYPEFUNC(name), \
146 C2_STUB_C_FUNC(name), \
147 C2_STUB_NAME(name), \
148 C2_STUB_ID(name), \
149 fancy_jump, \
150 pass_tls, \
151 pass_retpc); \
152 if (C2_STUB_FIELD_NAME(name) == nullptr) { return false; } \
153
154 bool OptoRuntime::generate(ciEnv* env) {
155
156 C2_STUBS_DO(GEN_C2_BLOB, GEN_C2_STUB)
157
158 return true;
159 }
160
161 #undef GEN_C2_BLOB
162
163 #undef C2_STUB_FIELD_NAME
164 #undef C2_STUB_TYPEFUNC
165 #undef C2_STUB_C_FUNC
166 #undef C2_STUB_NAME
167 #undef GEN_C2_STUB
168
169 // #undef gen
170
171 const TypeFunc* OptoRuntime::_new_instance_Type = nullptr;
172 const TypeFunc* OptoRuntime::_new_array_Type = nullptr;
173 const TypeFunc* OptoRuntime::_multianewarray2_Type = nullptr;
174 const TypeFunc* OptoRuntime::_multianewarray3_Type = nullptr;
237 const TypeFunc* OptoRuntime::_updateBytesCRC32_Type = nullptr;
238 const TypeFunc* OptoRuntime::_updateBytesCRC32C_Type = nullptr;
239 const TypeFunc* OptoRuntime::_updateBytesAdler32_Type = nullptr;
240 const TypeFunc* OptoRuntime::_osr_end_Type = nullptr;
241 const TypeFunc* OptoRuntime::_register_finalizer_Type = nullptr;
242 const TypeFunc* OptoRuntime::_vthread_transition_Type = nullptr;
243 #if INCLUDE_JFR
244 const TypeFunc* OptoRuntime::_class_id_load_barrier_Type = nullptr;
245 #endif // INCLUDE_JFR
246 const TypeFunc* OptoRuntime::_dtrace_method_entry_exit_Type = nullptr;
247 const TypeFunc* OptoRuntime::_dtrace_object_alloc_Type = nullptr;
248
249 // Helper method to do generation of RunTimeStub's
250 address OptoRuntime::generate_stub(ciEnv* env,
251 TypeFunc_generator gen, address C_function,
252 const char *name, StubId stub_id,
253 int is_fancy_jump, bool pass_tls,
254 bool return_pc) {
255
256 // Matching the default directive, we currently have no method to match.
257 DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompileBroker::compiler(CompLevel_full_optimization));
258 CompilationMemoryStatisticMark cmsm(directive);
259 ResourceMark rm;
260 Compile C(env, gen, C_function, name, stub_id, is_fancy_jump, pass_tls, return_pc, directive);
261 DirectivesStack::release(directive);
262 return C.stub_entry_point();
263 }
264
265 const char* OptoRuntime::stub_name(address entry) {
266 #ifndef PRODUCT
267 CodeBlob* cb = CodeCache::find_blob(entry);
268 RuntimeStub* rs =(RuntimeStub *)cb;
269 assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
270 return rs->name();
271 #else
272 // Fast implementation for product mode (maybe it should be inlined too)
273 return "runtime stub";
274 #endif
275 }
276
277 // local methods passed as arguments to stub generator that forward
281 oopDesc* dest, jint dest_pos,
282 jint length, JavaThread* thread) {
283 SharedRuntime::slow_arraycopy_C(src, src_pos, dest, dest_pos, length, thread);
284 }
285
286 void OptoRuntime::complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current) {
287 SharedRuntime::complete_monitor_locking_C(obj, lock, current);
288 }
289
290
291 //=============================================================================
292 // Opto compiler runtime routines
293 //=============================================================================
294
295
296 //=============================allocation======================================
297 // We failed the fast-path allocation. Now we need to do a scavenge or GC
298 // and try allocation again.
299
300 // object allocation
301 JRT_BLOCK_ENTRY(void, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
302 JRT_BLOCK;
303 #ifndef PRODUCT
304 SharedRuntime::_new_instance_ctr++; // new instance requires GC
305 #endif
306 assert(check_compiled_frame(current), "incorrect caller");
307
308 // These checks are cheap to make and support reflective allocation.
309 int lh = klass->layout_helper();
310 if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
311 Handle holder(current, klass->klass_holder()); // keep the klass alive
312 klass->check_valid_for_instantiation(false, THREAD);
313 if (!HAS_PENDING_EXCEPTION) {
314 InstanceKlass::cast(klass)->initialize(THREAD);
315 }
316 }
317
318 if (!HAS_PENDING_EXCEPTION) {
319 // Scavenge and allocate an instance.
320 Handle holder(current, klass->klass_holder()); // keep the klass alive
321 oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
322 current->set_vm_result_oop(result);
323
324 // Pass oops back through thread local storage. Our apparent type to Java
325 // is that we return an oop, but we can block on exit from this routine and
326 // a GC can trash the oop in C's return register. The generated stub will
327 // fetch the oop from TLS after any possible GC.
328 }
329
330 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
331 JRT_BLOCK_END;
332
333 // inform GC that we won't do card marks for initializing writes.
334 SharedRuntime::on_slowpath_allocation_exit(current);
335 JRT_END
336
337
338 // array allocation
339 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
340 JRT_BLOCK;
341 #ifndef PRODUCT
342 SharedRuntime::_new_array_ctr++; // new array requires GC
343 #endif
344 assert(check_compiled_frame(current), "incorrect caller");
345
346 // Scavenge and allocate an instance.
347 oop result;
348
349 if (array_type->is_typeArray_klass()) {
350 // The oopFactory likes to work with the element type.
351 // (We could bypass the oopFactory, since it doesn't add much value.)
352 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
353 result = oopFactory::new_typeArray(elem_type, len, THREAD);
354 } else {
355 // Although the oopFactory likes to work with the elem_type,
356 // the compiler prefers the array_type, since it must already have
357 // that latter value in hand for the fast path.
358 Handle holder(current, array_type->klass_holder()); // keep the array klass alive
359 Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
360 result = oopFactory::new_objArray(elem_type, len, THREAD);
361 }
362
363 // Pass oops back through thread local storage. Our apparent type to Java
364 // is that we return an oop, but we can block on exit from this routine and
365 // a GC can trash the oop in C's return register. The generated stub will
366 // fetch the oop from TLS after any possible GC.
367 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
368 current->set_vm_result_oop(result);
369 JRT_BLOCK_END;
370
371 // inform GC that we won't do card marks for initializing writes.
372 SharedRuntime::on_slowpath_allocation_exit(current);
373 JRT_END
374
375 // array allocation without zeroing
376 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
377 JRT_BLOCK;
378 #ifndef PRODUCT
379 SharedRuntime::_new_array_ctr++; // new array requires GC
380 #endif
381 assert(check_compiled_frame(current), "incorrect caller");
382
383 // Scavenge and allocate an instance.
384 oop result;
385
386 assert(array_type->is_typeArray_klass(), "should be called only for type array");
387 // The oopFactory likes to work with the element type.
388 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
389 result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
390
391 // Pass oops back through thread local storage. Our apparent type to Java
392 // is that we return an oop, but we can block on exit from this routine and
393 // a GC can trash the oop in C's return register. The generated stub will
394 // fetch the oop from TLS after any possible GC.
395 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
396 current->set_vm_result_oop(result);
408 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
409 size_t hs_bytes = arrayOopDesc::base_offset_in_bytes(elem_type);
410 assert(is_aligned(hs_bytes, BytesPerInt), "must be 4 byte aligned");
411 HeapWord* obj = cast_from_oop<HeapWord*>(result);
412 if (!is_aligned(hs_bytes, BytesPerLong)) {
413 *reinterpret_cast<jint*>(reinterpret_cast<char*>(obj) + hs_bytes) = 0;
414 hs_bytes += BytesPerInt;
415 }
416
417 // Optimized zeroing.
418 assert(is_aligned(hs_bytes, BytesPerLong), "must be 8-byte aligned");
419 const size_t aligned_hs = hs_bytes / BytesPerLong;
420 Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
421 }
422
423 JRT_END
424
425 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
426
427 // multianewarray for 2 dimensions
428 JRT_ENTRY(void, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
429 #ifndef PRODUCT
430 SharedRuntime::_multi2_ctr++; // multianewarray for 1 dimension
431 #endif
432 assert(check_compiled_frame(current), "incorrect caller");
433 assert(elem_type->is_klass(), "not a class");
434 jint dims[2];
435 dims[0] = len1;
436 dims[1] = len2;
437 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
438 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
439 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
440 current->set_vm_result_oop(obj);
441 JRT_END
442
443 // multianewarray for 3 dimensions
444 JRT_ENTRY(void, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
445 #ifndef PRODUCT
446 SharedRuntime::_multi3_ctr++; // multianewarray for 1 dimension
447 #endif
448 assert(check_compiled_frame(current), "incorrect caller");
449 assert(elem_type->is_klass(), "not a class");
450 jint dims[3];
451 dims[0] = len1;
452 dims[1] = len2;
453 dims[2] = len3;
454 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
455 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
456 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
457 current->set_vm_result_oop(obj);
458 JRT_END
459
460 // multianewarray for 4 dimensions
461 JRT_ENTRY(void, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
462 #ifndef PRODUCT
463 SharedRuntime::_multi4_ctr++; // multianewarray for 1 dimension
464 #endif
465 assert(check_compiled_frame(current), "incorrect caller");
466 assert(elem_type->is_klass(), "not a class");
467 jint dims[4];
468 dims[0] = len1;
469 dims[1] = len2;
470 dims[2] = len3;
471 dims[3] = len4;
472 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
473 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
474 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
475 current->set_vm_result_oop(obj);
476 JRT_END
477
478 // multianewarray for 5 dimensions
479 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
480 #ifndef PRODUCT
481 SharedRuntime::_multi5_ctr++; // multianewarray for 1 dimension
482 #endif
483 assert(check_compiled_frame(current), "incorrect caller");
484 assert(elem_type->is_klass(), "not a class");
485 jint dims[5];
486 dims[0] = len1;
487 dims[1] = len2;
488 dims[2] = len3;
489 dims[3] = len4;
490 dims[4] = len5;
491 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
492 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
493 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
494 current->set_vm_result_oop(obj);
495 JRT_END
496
497 JRT_ENTRY(void, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
498 assert(check_compiled_frame(current), "incorrect caller");
499 assert(elem_type->is_klass(), "not a class");
500 assert(oop(dims)->is_typeArray(), "not an array");
501
502 ResourceMark rm;
503 jint len = dims->length();
504 assert(len > 0, "Dimensions array should contain data");
505 jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
506 ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
507 c_dims, len);
508
509 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
510 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
511 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
512 current->set_vm_result_oop(obj);
513 JRT_END
514
515 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
516
517 // Very few notify/notifyAll operations find any threads on the waitset, so
518 // the dominant fast-path is to simply return.
519 // Relatedly, it's critical that notify/notifyAll be fast in order to
520 // reduce lock hold times.
521 if (!SafepointSynchronize::is_synchronizing()) {
522 if (ObjectSynchronizer::quick_notify(obj, current, false)) {
523 return;
524 }
525 }
526
527 // This is the case the fast-path above isn't provisioned to handle.
528 // The fast-path is designed to handle frequently arising cases in an efficient manner.
529 // (The fast-path is just a degenerate variant of the slow-path).
530 // Perform the dreaded state transition and pass control into the slow-path.
531 JRT_BLOCK;
532 Handle h_obj(current, obj);
533 ObjectSynchronizer::notify(h_obj, CHECK);
534 JRT_BLOCK_END;
535 JRT_END
536
537 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
538
539 if (!SafepointSynchronize::is_synchronizing() ) {
540 if (ObjectSynchronizer::quick_notify(obj, current, true)) {
541 return;
542 }
543 }
544
545 // This is the case the fast-path above isn't provisioned to handle.
546 // The fast-path is designed to handle frequently arising cases in an efficient manner.
547 // (The fast-path is just a degenerate variant of the slow-path).
548 // Perform the dreaded state transition and pass control into the slow-path.
549 JRT_BLOCK;
550 Handle h_obj(current, obj);
551 ObjectSynchronizer::notifyall(h_obj, CHECK);
552 JRT_BLOCK_END;
553 JRT_END
554
555 JRT_ENTRY(void, OptoRuntime::vthread_end_first_transition_C(oopDesc* vt, jboolean is_mount, JavaThread* current))
556 MountUnmountDisabler::end_transition(current, vt, true /*is_mount*/, true /*is_thread_start*/);
557 JRT_END
1842 assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1843 switch (register_save_policy[reg]) {
1844 case 'C': return false; //SOC
1845 case 'E': return true ; //SOE
1846 case 'N': return false; //NS
1847 case 'A': return false; //AS
1848 }
1849 ShouldNotReachHere();
1850 return false;
1851 }
1852
1853 //-----------------------------------------------------------------------
1854 // Exceptions
1855 //
1856
1857 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1858
1859 // The method is an entry that is always called by a C++ method not
1860 // directly from compiled code. Compiled code will call the C++ method following.
1861 // We can't allow async exception to be installed during exception processing.
1862 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1863 // The frame we rethrow the exception to might not have been processed by the GC yet.
1864 // The stack watermark barrier takes care of detecting that and ensuring the frame
1865 // has updated oops.
1866 StackWatermarkSet::after_unwind(current);
1867
1868 // Do not confuse exception_oop with pending_exception. The exception_oop
1869 // is only used to pass arguments into the method. Not for general
1870 // exception handling. DO NOT CHANGE IT to use pending_exception, since
1871 // the runtime stubs checks this on exit.
1872 assert(current->exception_oop() != nullptr, "exception oop is found");
1873 address handler_address = nullptr;
1874
1875 Handle exception(current, current->exception_oop());
1876 address pc = current->exception_pc();
1877
1878 // Clear out the exception oop and pc since looking up an
1879 // exception handler can cause class loading, which might throw an
1880 // exception and those fields are expected to be clear during
1881 // normal bytecode execution.
1882 current->clear_exception_oop_and_pc();
2112 frame caller_frame = stub_frame.sender(®_map);
2113 return caller_frame.is_deoptimized_frame();
2114 }
2115
2116 static const TypeFunc* make_register_finalizer_Type() {
2117 // create input type (domain)
2118 const Type **fields = TypeTuple::fields(1);
2119 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // oop; Receiver
2120 // // The JavaThread* is passed to each routine as the last argument
2121 // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // JavaThread *; Executing thread
2122 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2123
2124 // create result type (range)
2125 fields = TypeTuple::fields(0);
2126
2127 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2128
2129 return TypeFunc::make(domain,range);
2130 }
2131
2132 #if INCLUDE_JFR
2133 static const TypeFunc* make_class_id_load_barrier_Type() {
2134 // create input type (domain)
2135 const Type **fields = TypeTuple::fields(1);
2136 fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
2137 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
2138
2139 // create result type (range)
2140 fields = TypeTuple::fields(0);
2141
2142 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
2143
2144 return TypeFunc::make(domain,range);
2145 }
2146 #endif // INCLUDE_JFR
2147
2148 //-----------------------------------------------------------------------------
2149 static const TypeFunc* make_dtrace_method_entry_exit_Type() {
2150 // create input type (domain)
2151 const Type **fields = TypeTuple::fields(2);
2152 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2153 fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM; // Method*; Method we are entering
2154 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2155
2156 // create result type (range)
2157 fields = TypeTuple::fields(0);
2158
2159 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2160
2161 return TypeFunc::make(domain,range);
2162 }
2163
2164 static const TypeFunc* make_dtrace_object_alloc_Type() {
2165 // create input type (domain)
2166 const Type **fields = TypeTuple::fields(2);
2167 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2168 fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL; // oop; newly allocated object
2169
2170 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2171
2172 // create result type (range)
2173 fields = TypeTuple::fields(0);
2174
2175 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2176
2177 return TypeFunc::make(domain,range);
2178 }
2179
2180 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer_C(oopDesc* obj, JavaThread* current))
2181 assert(oopDesc::is_oop(obj), "must be a valid oop");
2182 assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
2183 InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
2184 JRT_END
2185
2186 //-----------------------------------------------------------------------------
2187
2188 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
2189
2190 //
2191 // dump the collected NamedCounters.
2192 //
2193 void OptoRuntime::print_named_counters() {
2194 int total_lock_count = 0;
2195 int eliminated_lock_count = 0;
2196
2197 NamedCounter* c = _named_counters;
2198 while (c) {
2199 if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
2200 int count = c->count();
2201 if (count > 0) {
2202 bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
2203 if (Verbose) {
2204 tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
2205 }
2346 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
2347 trace_exception_counter++;
2348 stringStream tempst;
2349
2350 tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
2351 exception_oop->print_value_on(&tempst);
2352 tempst.print(" in ");
2353 CodeBlob* blob = CodeCache::find_blob(exception_pc);
2354 if (blob->is_nmethod()) {
2355 blob->as_nmethod()->method()->print_value_on(&tempst);
2356 } else if (blob->is_runtime_stub()) {
2357 tempst.print("<runtime-stub>");
2358 } else {
2359 tempst.print("<unknown>");
2360 }
2361 tempst.print(" at " INTPTR_FORMAT, p2i(exception_pc));
2362 tempst.print("]");
2363
2364 st->print_raw_cr(tempst.freeze());
2365 }
|
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/atomicAccess.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/mountUnmountDisabler.hpp"
72 #include "runtime/perfData.inline.hpp"
73 #include "runtime/sharedRuntime.hpp"
74 #include "runtime/signature.hpp"
75 #include "runtime/stackWatermarkSet.hpp"
76 #include "runtime/synchronizer.hpp"
77 #include "runtime/threadWXSetters.inline.hpp"
78 #include "runtime/vframe.hpp"
79 #include "runtime/vframe_hp.hpp"
80 #include "runtime/vframeArray.hpp"
81 #include "services/management.hpp"
82 #include "utilities/copy.hpp"
83 #include "utilities/preserveException.hpp"
84
85
86 // For debugging purposes:
87 // To force FullGCALot inside a runtime function, add the following two lines
88 //
89 // Universe::release_fullgc_alot_dummy();
90 // Universe::heap()->collect();
91 //
92 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
93
94
95 #define C2_BLOB_FIELD_DEFINE(name, type) \
96 type* OptoRuntime:: BLOB_FIELD_NAME(name) = nullptr;
97 #define C2_STUB_FIELD_NAME(name) _ ## name ## _Java
98 #define C2_STUB_FIELD_DEFINE(name, f, t, r) \
99 address OptoRuntime:: C2_STUB_FIELD_NAME(name) = nullptr;
100 C2_STUBS_DO(C2_BLOB_FIELD_DEFINE, C2_STUB_FIELD_DEFINE)
101 #undef C2_BLOB_FIELD_DEFINE
102 #undef C2_STUB_FIELD_DEFINE
103
104 address OptoRuntime::_vtable_must_compile_Java = nullptr;
105
106 PerfCounter* _perf_OptoRuntime_class_init_barrier_redundant_count = nullptr;
107
108 // This should be called in an assertion at the start of OptoRuntime routines
109 // which are entered from compiled code (all of them)
110 #ifdef ASSERT
111 static bool check_compiled_frame(JavaThread* thread) {
112 assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
113 RegisterMap map(thread,
114 RegisterMap::UpdateMap::skip,
115 RegisterMap::ProcessFrames::include,
116 RegisterMap::WalkContinuation::skip);
117 frame caller = thread->last_frame().sender(&map);
118 assert(caller.is_compiled_frame(), "not being called from compiled like code");
119 return true;
120 }
121 #endif // ASSERT
122
123 /*
124 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
125 var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
126 if (var == nullptr) { return false; }
127 */
143 // from the stub name by appending suffix '_C'. However, in two cases
144 // a common target method also needs to be called from shared runtime
145 // stubs. In these two cases the opto stubs rely on method
146 // imlementations defined in class SharedRuntime. The following
147 // defines temporarily rebind the generated names to reference the
148 // relevant implementations.
149
150 #define GEN_C2_STUB(name, fancy_jump, pass_tls, pass_retpc ) \
151 C2_STUB_FIELD_NAME(name) = \
152 generate_stub(env, \
153 C2_STUB_TYPEFUNC(name), \
154 C2_STUB_C_FUNC(name), \
155 C2_STUB_NAME(name), \
156 C2_STUB_ID(name), \
157 fancy_jump, \
158 pass_tls, \
159 pass_retpc); \
160 if (C2_STUB_FIELD_NAME(name) == nullptr) { return false; } \
161
162 bool OptoRuntime::generate(ciEnv* env) {
163 init_counters();
164
165 C2_STUBS_DO(GEN_C2_BLOB, GEN_C2_STUB)
166
167 return true;
168 }
169
170 #undef GEN_C2_BLOB
171
172 #undef C2_STUB_FIELD_NAME
173 #undef C2_STUB_TYPEFUNC
174 #undef C2_STUB_C_FUNC
175 #undef C2_STUB_NAME
176 #undef GEN_C2_STUB
177
178 // #undef gen
179
180 const TypeFunc* OptoRuntime::_new_instance_Type = nullptr;
181 const TypeFunc* OptoRuntime::_new_array_Type = nullptr;
182 const TypeFunc* OptoRuntime::_multianewarray2_Type = nullptr;
183 const TypeFunc* OptoRuntime::_multianewarray3_Type = nullptr;
246 const TypeFunc* OptoRuntime::_updateBytesCRC32_Type = nullptr;
247 const TypeFunc* OptoRuntime::_updateBytesCRC32C_Type = nullptr;
248 const TypeFunc* OptoRuntime::_updateBytesAdler32_Type = nullptr;
249 const TypeFunc* OptoRuntime::_osr_end_Type = nullptr;
250 const TypeFunc* OptoRuntime::_register_finalizer_Type = nullptr;
251 const TypeFunc* OptoRuntime::_vthread_transition_Type = nullptr;
252 #if INCLUDE_JFR
253 const TypeFunc* OptoRuntime::_class_id_load_barrier_Type = nullptr;
254 #endif // INCLUDE_JFR
255 const TypeFunc* OptoRuntime::_dtrace_method_entry_exit_Type = nullptr;
256 const TypeFunc* OptoRuntime::_dtrace_object_alloc_Type = nullptr;
257
258 // Helper method to do generation of RunTimeStub's
259 address OptoRuntime::generate_stub(ciEnv* env,
260 TypeFunc_generator gen, address C_function,
261 const char *name, StubId stub_id,
262 int is_fancy_jump, bool pass_tls,
263 bool return_pc) {
264
265 // Matching the default directive, we currently have no method to match.
266 DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompilerThread::current()->compiler());
267 CompilationMemoryStatisticMark cmsm(directive);
268 ResourceMark rm;
269 Compile C(env, gen, C_function, name, stub_id, is_fancy_jump, pass_tls, return_pc, directive);
270 DirectivesStack::release(directive);
271 return C.stub_entry_point();
272 }
273
274 const char* OptoRuntime::stub_name(address entry) {
275 #ifndef PRODUCT
276 CodeBlob* cb = CodeCache::find_blob(entry);
277 RuntimeStub* rs =(RuntimeStub *)cb;
278 assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
279 return rs->name();
280 #else
281 // Fast implementation for product mode (maybe it should be inlined too)
282 return "runtime stub";
283 #endif
284 }
285
286 // local methods passed as arguments to stub generator that forward
290 oopDesc* dest, jint dest_pos,
291 jint length, JavaThread* thread) {
292 SharedRuntime::slow_arraycopy_C(src, src_pos, dest, dest_pos, length, thread);
293 }
294
295 void OptoRuntime::complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current) {
296 SharedRuntime::complete_monitor_locking_C(obj, lock, current);
297 }
298
299
300 //=============================================================================
301 // Opto compiler runtime routines
302 //=============================================================================
303
304
305 //=============================allocation======================================
306 // We failed the fast-path allocation. Now we need to do a scavenge or GC
307 // and try allocation again.
308
309 // object allocation
310 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_instance_C, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
311 JRT_BLOCK;
312 #ifndef PRODUCT
313 SharedRuntime::_new_instance_ctr++; // new instance requires GC
314 #endif
315 assert(check_compiled_frame(current), "incorrect caller");
316
317 // These checks are cheap to make and support reflective allocation.
318 int lh = klass->layout_helper();
319 if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
320 Handle holder(current, klass->klass_holder()); // keep the klass alive
321 klass->check_valid_for_instantiation(false, THREAD);
322 if (!HAS_PENDING_EXCEPTION) {
323 InstanceKlass::cast(klass)->initialize(THREAD);
324 }
325 }
326
327 if (!HAS_PENDING_EXCEPTION) {
328 // Scavenge and allocate an instance.
329 Handle holder(current, klass->klass_holder()); // keep the klass alive
330 oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
331 current->set_vm_result_oop(result);
332
333 // Pass oops back through thread local storage. Our apparent type to Java
334 // is that we return an oop, but we can block on exit from this routine and
335 // a GC can trash the oop in C's return register. The generated stub will
336 // fetch the oop from TLS after any possible GC.
337 }
338
339 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
340 JRT_BLOCK_END;
341
342 // inform GC that we won't do card marks for initializing writes.
343 SharedRuntime::on_slowpath_allocation_exit(current);
344 JRT_END
345
346
347 // array allocation
348 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_C, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
349 JRT_BLOCK;
350 #ifndef PRODUCT
351 SharedRuntime::_new_array_ctr++; // new array requires GC
352 #endif
353 assert(check_compiled_frame(current), "incorrect caller");
354
355 // Scavenge and allocate an instance.
356 oop result;
357
358 if (array_type->is_typeArray_klass()) {
359 // The oopFactory likes to work with the element type.
360 // (We could bypass the oopFactory, since it doesn't add much value.)
361 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
362 result = oopFactory::new_typeArray(elem_type, len, THREAD);
363 } else {
364 // Although the oopFactory likes to work with the elem_type,
365 // the compiler prefers the array_type, since it must already have
366 // that latter value in hand for the fast path.
367 Handle holder(current, array_type->klass_holder()); // keep the array klass alive
368 Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
369 result = oopFactory::new_objArray(elem_type, len, THREAD);
370 }
371
372 // Pass oops back through thread local storage. Our apparent type to Java
373 // is that we return an oop, but we can block on exit from this routine and
374 // a GC can trash the oop in C's return register. The generated stub will
375 // fetch the oop from TLS after any possible GC.
376 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
377 current->set_vm_result_oop(result);
378 JRT_BLOCK_END;
379
380 // inform GC that we won't do card marks for initializing writes.
381 SharedRuntime::on_slowpath_allocation_exit(current);
382 JRT_END
383
384 // array allocation without zeroing
385 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_nozero_C, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
386 JRT_BLOCK;
387 #ifndef PRODUCT
388 SharedRuntime::_new_array_ctr++; // new array requires GC
389 #endif
390 assert(check_compiled_frame(current), "incorrect caller");
391
392 // Scavenge and allocate an instance.
393 oop result;
394
395 assert(array_type->is_typeArray_klass(), "should be called only for type array");
396 // The oopFactory likes to work with the element type.
397 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
398 result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
399
400 // Pass oops back through thread local storage. Our apparent type to Java
401 // is that we return an oop, but we can block on exit from this routine and
402 // a GC can trash the oop in C's return register. The generated stub will
403 // fetch the oop from TLS after any possible GC.
404 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
405 current->set_vm_result_oop(result);
417 BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
418 size_t hs_bytes = arrayOopDesc::base_offset_in_bytes(elem_type);
419 assert(is_aligned(hs_bytes, BytesPerInt), "must be 4 byte aligned");
420 HeapWord* obj = cast_from_oop<HeapWord*>(result);
421 if (!is_aligned(hs_bytes, BytesPerLong)) {
422 *reinterpret_cast<jint*>(reinterpret_cast<char*>(obj) + hs_bytes) = 0;
423 hs_bytes += BytesPerInt;
424 }
425
426 // Optimized zeroing.
427 assert(is_aligned(hs_bytes, BytesPerLong), "must be 8-byte aligned");
428 const size_t aligned_hs = hs_bytes / BytesPerLong;
429 Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
430 }
431
432 JRT_END
433
434 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
435
436 // multianewarray for 2 dimensions
437 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray2_C, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
438 #ifndef PRODUCT
439 SharedRuntime::_multi2_ctr++; // multianewarray for 1 dimension
440 #endif
441 assert(check_compiled_frame(current), "incorrect caller");
442 assert(elem_type->is_klass(), "not a class");
443 jint dims[2];
444 dims[0] = len1;
445 dims[1] = len2;
446 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
447 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
448 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
449 current->set_vm_result_oop(obj);
450 JRT_END
451
452 // multianewarray for 3 dimensions
453 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray3_C, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
454 #ifndef PRODUCT
455 SharedRuntime::_multi3_ctr++; // multianewarray for 1 dimension
456 #endif
457 assert(check_compiled_frame(current), "incorrect caller");
458 assert(elem_type->is_klass(), "not a class");
459 jint dims[3];
460 dims[0] = len1;
461 dims[1] = len2;
462 dims[2] = len3;
463 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
464 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
465 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
466 current->set_vm_result_oop(obj);
467 JRT_END
468
469 // multianewarray for 4 dimensions
470 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray4_C, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
471 #ifndef PRODUCT
472 SharedRuntime::_multi4_ctr++; // multianewarray for 1 dimension
473 #endif
474 assert(check_compiled_frame(current), "incorrect caller");
475 assert(elem_type->is_klass(), "not a class");
476 jint dims[4];
477 dims[0] = len1;
478 dims[1] = len2;
479 dims[2] = len3;
480 dims[3] = len4;
481 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
482 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
483 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
484 current->set_vm_result_oop(obj);
485 JRT_END
486
487 // multianewarray for 5 dimensions
488 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
489 #ifndef PRODUCT
490 SharedRuntime::_multi5_ctr++; // multianewarray for 1 dimension
491 #endif
492 assert(check_compiled_frame(current), "incorrect caller");
493 assert(elem_type->is_klass(), "not a class");
494 jint dims[5];
495 dims[0] = len1;
496 dims[1] = len2;
497 dims[2] = len3;
498 dims[3] = len4;
499 dims[4] = len5;
500 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
501 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
502 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
503 current->set_vm_result_oop(obj);
504 JRT_END
505
506 JRT_ENTRY_PROF(void, OptoRuntime, multianewarrayN_C, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
507 assert(check_compiled_frame(current), "incorrect caller");
508 assert(elem_type->is_klass(), "not a class");
509 assert(oop(dims)->is_typeArray(), "not an array");
510
511 ResourceMark rm;
512 jint len = dims->length();
513 assert(len > 0, "Dimensions array should contain data");
514 jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
515 ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
516 c_dims, len);
517
518 Handle holder(current, elem_type->klass_holder()); // keep the klass alive
519 oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
520 deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
521 current->set_vm_result_oop(obj);
522 JRT_END
523
524 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notify_C, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
525
526 // Very few notify/notifyAll operations find any threads on the waitset, so
527 // the dominant fast-path is to simply return.
528 // Relatedly, it's critical that notify/notifyAll be fast in order to
529 // reduce lock hold times.
530 if (!SafepointSynchronize::is_synchronizing()) {
531 if (ObjectSynchronizer::quick_notify(obj, current, false)) {
532 return;
533 }
534 }
535
536 // This is the case the fast-path above isn't provisioned to handle.
537 // The fast-path is designed to handle frequently arising cases in an efficient manner.
538 // (The fast-path is just a degenerate variant of the slow-path).
539 // Perform the dreaded state transition and pass control into the slow-path.
540 JRT_BLOCK;
541 Handle h_obj(current, obj);
542 ObjectSynchronizer::notify(h_obj, CHECK);
543 JRT_BLOCK_END;
544 JRT_END
545
546 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notifyAll_C, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
547
548 if (!SafepointSynchronize::is_synchronizing() ) {
549 if (ObjectSynchronizer::quick_notify(obj, current, true)) {
550 return;
551 }
552 }
553
554 // This is the case the fast-path above isn't provisioned to handle.
555 // The fast-path is designed to handle frequently arising cases in an efficient manner.
556 // (The fast-path is just a degenerate variant of the slow-path).
557 // Perform the dreaded state transition and pass control into the slow-path.
558 JRT_BLOCK;
559 Handle h_obj(current, obj);
560 ObjectSynchronizer::notifyall(h_obj, CHECK);
561 JRT_BLOCK_END;
562 JRT_END
563
564 JRT_ENTRY(void, OptoRuntime::vthread_end_first_transition_C(oopDesc* vt, jboolean is_mount, JavaThread* current))
565 MountUnmountDisabler::end_transition(current, vt, true /*is_mount*/, true /*is_thread_start*/);
566 JRT_END
1851 assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1852 switch (register_save_policy[reg]) {
1853 case 'C': return false; //SOC
1854 case 'E': return true ; //SOE
1855 case 'N': return false; //NS
1856 case 'A': return false; //AS
1857 }
1858 ShouldNotReachHere();
1859 return false;
1860 }
1861
1862 //-----------------------------------------------------------------------
1863 // Exceptions
1864 //
1865
1866 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1867
1868 // The method is an entry that is always called by a C++ method not
1869 // directly from compiled code. Compiled code will call the C++ method following.
1870 // We can't allow async exception to be installed during exception processing.
1871 JRT_ENTRY_NO_ASYNC_PROF(address, OptoRuntime, handle_exception_C_helper, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1872 // The frame we rethrow the exception to might not have been processed by the GC yet.
1873 // The stack watermark barrier takes care of detecting that and ensuring the frame
1874 // has updated oops.
1875 StackWatermarkSet::after_unwind(current);
1876
1877 // Do not confuse exception_oop with pending_exception. The exception_oop
1878 // is only used to pass arguments into the method. Not for general
1879 // exception handling. DO NOT CHANGE IT to use pending_exception, since
1880 // the runtime stubs checks this on exit.
1881 assert(current->exception_oop() != nullptr, "exception oop is found");
1882 address handler_address = nullptr;
1883
1884 Handle exception(current, current->exception_oop());
1885 address pc = current->exception_pc();
1886
1887 // Clear out the exception oop and pc since looking up an
1888 // exception handler can cause class loading, which might throw an
1889 // exception and those fields are expected to be clear during
1890 // normal bytecode execution.
1891 current->clear_exception_oop_and_pc();
2121 frame caller_frame = stub_frame.sender(®_map);
2122 return caller_frame.is_deoptimized_frame();
2123 }
2124
2125 static const TypeFunc* make_register_finalizer_Type() {
2126 // create input type (domain)
2127 const Type **fields = TypeTuple::fields(1);
2128 fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL; // oop; Receiver
2129 // // The JavaThread* is passed to each routine as the last argument
2130 // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // JavaThread *; Executing thread
2131 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2132
2133 // create result type (range)
2134 fields = TypeTuple::fields(0);
2135
2136 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2137
2138 return TypeFunc::make(domain,range);
2139 }
2140
2141 const TypeFunc *OptoRuntime::class_init_barrier_Type() {
2142 // create input type (domain)
2143 const Type** fields = TypeTuple::fields(1);
2144 fields[TypeFunc::Parms+0] = TypeKlassPtr::NOTNULL;
2145 // // The JavaThread* is passed to each routine as the last argument
2146 // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL; // JavaThread *; Executing thread
2147 const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+1, fields);
2148
2149 // create result type (range)
2150 fields = TypeTuple::fields(0);
2151 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
2152 return TypeFunc::make(domain,range);
2153 }
2154
2155 #if INCLUDE_JFR
2156 static const TypeFunc* make_class_id_load_barrier_Type() {
2157 // create input type (domain)
2158 const Type **fields = TypeTuple::fields(1);
2159 fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
2160 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
2161
2162 // create result type (range)
2163 fields = TypeTuple::fields(0);
2164
2165 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
2166
2167 return TypeFunc::make(domain,range);
2168 }
2169 #endif // INCLUDE_JFR
2170
2171 //-----------------------------------------------------------------------------
2172 // runtime upcall support
2173 const TypeFunc *OptoRuntime::runtime_up_call_Type() {
2174 // create input type (domain)
2175 const Type **fields = TypeTuple::fields(1);
2176 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2177 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2178
2179 // create result type (range)
2180 fields = TypeTuple::fields(0);
2181
2182 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2183
2184 return TypeFunc::make(domain,range);
2185 }
2186
2187 //-----------------------------------------------------------------------------
2188 static const TypeFunc* make_dtrace_method_entry_exit_Type() {
2189 // create input type (domain)
2190 const Type **fields = TypeTuple::fields(2);
2191 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2192 fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM; // Method*; Method we are entering
2193 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2194
2195 // create result type (range)
2196 fields = TypeTuple::fields(0);
2197
2198 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2199
2200 return TypeFunc::make(domain,range);
2201 }
2202
2203 static const TypeFunc* make_dtrace_object_alloc_Type() {
2204 // create input type (domain)
2205 const Type **fields = TypeTuple::fields(2);
2206 fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2207 fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL; // oop; newly allocated object
2208
2209 const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2210
2211 // create result type (range)
2212 fields = TypeTuple::fields(0);
2213
2214 const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2215
2216 return TypeFunc::make(domain,range);
2217 }
2218
2219 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, register_finalizer_C, OptoRuntime::register_finalizer_C(oopDesc* obj, JavaThread* current))
2220 assert(oopDesc::is_oop(obj), "must be a valid oop");
2221 assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
2222 InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
2223 JRT_END
2224
2225 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, class_init_barrier_C, OptoRuntime::class_init_barrier_C(Klass* k, JavaThread* current))
2226 InstanceKlass* ik = InstanceKlass::cast(k);
2227 if (ik->should_be_initialized()) {
2228 ik->initialize(CHECK);
2229 } else if (UsePerfData) {
2230 _perf_OptoRuntime_class_init_barrier_redundant_count->inc();
2231 }
2232 JRT_END
2233
2234 //-----------------------------------------------------------------------------
2235
2236 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
2237
2238 //
2239 // dump the collected NamedCounters.
2240 //
2241 void OptoRuntime::print_named_counters() {
2242 int total_lock_count = 0;
2243 int eliminated_lock_count = 0;
2244
2245 NamedCounter* c = _named_counters;
2246 while (c) {
2247 if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
2248 int count = c->count();
2249 if (count > 0) {
2250 bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
2251 if (Verbose) {
2252 tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
2253 }
2394 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
2395 trace_exception_counter++;
2396 stringStream tempst;
2397
2398 tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
2399 exception_oop->print_value_on(&tempst);
2400 tempst.print(" in ");
2401 CodeBlob* blob = CodeCache::find_blob(exception_pc);
2402 if (blob->is_nmethod()) {
2403 blob->as_nmethod()->method()->print_value_on(&tempst);
2404 } else if (blob->is_runtime_stub()) {
2405 tempst.print("<runtime-stub>");
2406 } else {
2407 tempst.print("<unknown>");
2408 }
2409 tempst.print(" at " INTPTR_FORMAT, p2i(exception_pc));
2410 tempst.print("]");
2411
2412 st->print_raw_cr(tempst.freeze());
2413 }
2414
2415 #define DO_COUNTERS2(macro2, macro1) \
2416 macro2(OptoRuntime, new_instance_C) \
2417 macro2(OptoRuntime, new_array_C) \
2418 macro2(OptoRuntime, new_array_nozero_C) \
2419 macro2(OptoRuntime, multianewarray2_C) \
2420 macro2(OptoRuntime, multianewarray3_C) \
2421 macro2(OptoRuntime, multianewarray4_C) \
2422 macro2(OptoRuntime, multianewarrayN_C) \
2423 macro2(OptoRuntime, monitor_notify_C) \
2424 macro2(OptoRuntime, monitor_notifyAll_C) \
2425 macro2(OptoRuntime, handle_exception_C_helper) \
2426 macro2(OptoRuntime, register_finalizer_C) \
2427 macro2(OptoRuntime, class_init_barrier_C) \
2428 macro1(OptoRuntime, class_init_barrier_redundant)
2429
2430 #define INIT_COUNTER_TIME_AND_CNT(sub, name) \
2431 NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
2432 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
2433
2434 #define INIT_COUNTER_CNT(sub, name) \
2435 NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
2436
2437 void OptoRuntime::init_counters() {
2438 assert(CompilerConfig::is_c2_enabled(), "");
2439
2440 if (UsePerfData) {
2441 EXCEPTION_MARK;
2442
2443 DO_COUNTERS2(INIT_COUNTER_TIME_AND_CNT, INIT_COUNTER_CNT)
2444
2445 if (HAS_PENDING_EXCEPTION) {
2446 vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
2447 }
2448 }
2449 }
2450 #undef INIT_COUNTER_TIME_AND_CNT
2451 #undef INIT_COUNTER_CNT
2452
2453 #define PRINT_COUNTER_TIME_AND_CNT(sub, name) { \
2454 jlong count = _perf_##sub##_##name##_count->get_value(); \
2455 if (count > 0) { \
2456 st->print_cr(" %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
2457 _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
2458 _perf_##sub##_##name##_timer->thread_counter_value_us(), \
2459 count); \
2460 }}
2461
2462 #define PRINT_COUNTER_CNT(sub, name) { \
2463 jlong count = _perf_##sub##_##name##_count->get_value(); \
2464 if (count > 0) { \
2465 st->print_cr(" %-30s = " JLONG_FORMAT_W(5) " events", #name, count); \
2466 }}
2467
2468 void OptoRuntime::print_counters_on(outputStream* st) {
2469 if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c2_enabled()) {
2470 DO_COUNTERS2(PRINT_COUNTER_TIME_AND_CNT, PRINT_COUNTER_CNT)
2471 } else {
2472 st->print_cr(" OptoRuntime: no info (%s is disabled)",
2473 (!CompilerConfig::is_c2_enabled() ? "C2" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
2474 }
2475 }
2476
2477 #undef PRINT_COUNTER_TIME_AND_CNT
2478 #undef PRINT_COUNTER_CNT
2479 #undef DO_COUNTERS2
|