< prev index next >

src/hotspot/share/opto/runtime.cpp

Print this page

  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "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/compileBroker.hpp"

  34 #include "compiler/oopMap.hpp"
  35 #include "gc/g1/g1HeapRegion.hpp"
  36 #include "gc/shared/barrierSet.hpp"
  37 #include "gc/shared/collectedHeap.hpp"
  38 #include "gc/shared/gcLocker.hpp"
  39 #include "interpreter/bytecode.hpp"
  40 #include "interpreter/interpreter.hpp"
  41 #include "interpreter/linkResolver.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/oopFactory.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "oops/objArrayKlass.hpp"
  47 #include "oops/klass.inline.hpp"
  48 #include "oops/oop.inline.hpp"
  49 #include "oops/typeArrayOop.inline.hpp"
  50 #include "opto/ad.hpp"
  51 #include "opto/addnode.hpp"
  52 #include "opto/callnode.hpp"
  53 #include "opto/cfgnode.hpp"
  54 #include "opto/graphKit.hpp"
  55 #include "opto/machnode.hpp"
  56 #include "opto/matcher.hpp"
  57 #include "opto/memnode.hpp"
  58 #include "opto/mulnode.hpp"
  59 #include "opto/output.hpp"
  60 #include "opto/runtime.hpp"
  61 #include "opto/subnode.hpp"
  62 #include "prims/jvmtiExport.hpp"
  63 #include "runtime/atomic.hpp"
  64 #include "runtime/frame.inline.hpp"
  65 #include "runtime/handles.inline.hpp"
  66 #include "runtime/interfaceSupport.inline.hpp"

  67 #include "runtime/javaCalls.hpp"

  68 #include "runtime/sharedRuntime.hpp"
  69 #include "runtime/signature.hpp"
  70 #include "runtime/stackWatermarkSet.hpp"
  71 #include "runtime/synchronizer.hpp"
  72 #include "runtime/threadCritical.hpp"
  73 #include "runtime/threadWXSetters.inline.hpp"
  74 #include "runtime/vframe.hpp"
  75 #include "runtime/vframeArray.hpp"
  76 #include "runtime/vframe_hp.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 #define C2_BLOB_NAME_DEFINE(name, type)  "C2 Runtime " # name "_blob",
 103 #define C2_STUB_NAME_DEFINE(name, f, t, r)  "C2 Runtime " # name,
 104 #define C2_JVMTI_STUB_NAME_DEFINE(name)  "C2 Runtime " # name,
 105 const char* OptoRuntime::_stub_names[] = {
 106   C2_STUBS_DO(C2_BLOB_NAME_DEFINE, C2_STUB_NAME_DEFINE, C2_JVMTI_STUB_NAME_DEFINE)
 107 };
 108 #undef C2_BLOB_NAME_DEFINE
 109 #undef C2_STUB_NAME_DEFINE
 110 #undef C2_JVMTI_STUB_NAME_DEFINE
 111 




 112 // This should be called in an assertion at the start of OptoRuntime routines
 113 // which are entered from compiled code (all of them)
 114 #ifdef ASSERT
 115 static bool check_compiled_frame(JavaThread* thread) {
 116   assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
 117   RegisterMap map(thread,
 118                   RegisterMap::UpdateMap::skip,
 119                   RegisterMap::ProcessFrames::include,
 120                   RegisterMap::WalkContinuation::skip);
 121   frame caller = thread->last_frame().sender(&map);
 122   assert(caller.is_compiled_frame(), "not being called from compiled like code");
 123   return true;
 124 }
 125 #endif // ASSERT
 126 
 127 /*
 128 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
 129   var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
 130   if (var == nullptr) { return false; }
 131 */

 156                   C2_STUB_NAME(name),                                 \
 157                   fancy_jump,                                           \
 158                   pass_tls,                                             \
 159                   pass_retpc);                                          \
 160   if (C2_STUB_FIELD_NAME(name) == nullptr) { return false; }          \
 161 
 162 #define C2_JVMTI_STUB_C_FUNC(name) CAST_FROM_FN_PTR(address, SharedRuntime::name)
 163 
 164 #define GEN_C2_JVMTI_STUB(name)                                       \
 165   STUB_FIELD_NAME(name) =                                               \
 166     generate_stub(env,                                                  \
 167                   notify_jvmti_vthread_Type,                            \
 168                   C2_JVMTI_STUB_C_FUNC(name),                         \
 169                   C2_STUB_NAME(name),                                 \
 170                   0,                                                    \
 171                   true,                                                 \
 172                   false);                                               \
 173   if (STUB_FIELD_NAME(name) == nullptr) { return false; }               \
 174 
 175 bool OptoRuntime::generate(ciEnv* env) {

 176 
 177   C2_STUBS_DO(GEN_C2_BLOB, GEN_C2_STUB, GEN_C2_JVMTI_STUB)
 178 
 179   return true;
 180 }
 181 
 182 #undef GEN_C2_BLOB
 183 
 184 #undef C2_STUB_FIELD_NAME
 185 #undef C2_STUB_TYPEFUNC
 186 #undef C2_STUB_C_FUNC
 187 #undef C2_STUB_NAME
 188 #undef GEN_C2_STUB
 189 
 190 #undef C2_JVMTI_STUB_C_FUNC
 191 #undef GEN_C2_JVMTI_STUB
 192 // #undef gen
 193 
 194 const TypeFunc* OptoRuntime::_new_instance_Type                   = nullptr;
 195 const TypeFunc* OptoRuntime::_new_array_Type                      = nullptr;

 249 const TypeFunc* OptoRuntime::_updateBytesAdler32_Type             = nullptr;
 250 const TypeFunc* OptoRuntime::_osr_end_Type                        = nullptr;
 251 const TypeFunc* OptoRuntime::_register_finalizer_Type             = nullptr;
 252 #if INCLUDE_JFR
 253 const TypeFunc* OptoRuntime::_class_id_load_barrier_Type          = nullptr;
 254 #endif // INCLUDE_JFR
 255 #if INCLUDE_JVMTI
 256 const TypeFunc* OptoRuntime::_notify_jvmti_vthread_Type           = nullptr;
 257 #endif // INCLUDE_JVMTI
 258 const TypeFunc* OptoRuntime::_dtrace_method_entry_exit_Type       = nullptr;
 259 const TypeFunc* OptoRuntime::_dtrace_object_alloc_Type            = nullptr;
 260 
 261 // Helper method to do generation of RunTimeStub's
 262 address OptoRuntime::generate_stub(ciEnv* env,
 263                                    TypeFunc_generator gen, address C_function,
 264                                    const char *name, int is_fancy_jump,
 265                                    bool pass_tls,
 266                                    bool return_pc) {
 267 
 268   // Matching the default directive, we currently have no method to match.
 269   DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompileBroker::compiler(CompLevel_full_optimization));
 270   ResourceMark rm;
 271   Compile C(env, gen, C_function, name, is_fancy_jump, pass_tls, return_pc, directive);
 272   DirectivesStack::release(directive);
 273   return  C.stub_entry_point();
 274 }
 275 
 276 const char* OptoRuntime::stub_name(address entry) {
 277 #ifndef PRODUCT
 278   CodeBlob* cb = CodeCache::find_blob(entry);
 279   RuntimeStub* rs =(RuntimeStub *)cb;
 280   assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
 281   return rs->name();
 282 #else
 283   // Fast implementation for product mode (maybe it should be inlined too)
 284   return "runtime stub";
 285 #endif
 286 }
 287 
 288 // local methods passed as arguments to stub generator that forward
 289 // control to corresponding JRT methods of SharedRuntime

 292                                    oopDesc* dest, jint dest_pos,
 293                                    jint length, JavaThread* thread) {
 294   SharedRuntime::slow_arraycopy_C(src,  src_pos, dest, dest_pos, length, thread);
 295 }
 296 
 297 void OptoRuntime::complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current) {
 298   SharedRuntime::complete_monitor_locking_C(obj, lock, current);
 299 }
 300 
 301 
 302 //=============================================================================
 303 // Opto compiler runtime routines
 304 //=============================================================================
 305 
 306 
 307 //=============================allocation======================================
 308 // We failed the fast-path allocation.  Now we need to do a scavenge or GC
 309 // and try allocation again.
 310 
 311 // object allocation
 312 JRT_BLOCK_ENTRY(void, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
 313   JRT_BLOCK;
 314 #ifndef PRODUCT
 315   SharedRuntime::_new_instance_ctr++;         // new instance requires GC
 316 #endif
 317   assert(check_compiled_frame(current), "incorrect caller");
 318 
 319   // These checks are cheap to make and support reflective allocation.
 320   int lh = klass->layout_helper();
 321   if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
 322     Handle holder(current, klass->klass_holder()); // keep the klass alive
 323     klass->check_valid_for_instantiation(false, THREAD);
 324     if (!HAS_PENDING_EXCEPTION) {
 325       InstanceKlass::cast(klass)->initialize(THREAD);
 326     }
 327   }
 328 
 329   if (!HAS_PENDING_EXCEPTION) {
 330     // Scavenge and allocate an instance.
 331     Handle holder(current, klass->klass_holder()); // keep the klass alive
 332     oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
 333     current->set_vm_result(result);
 334 
 335     // Pass oops back through thread local storage.  Our apparent type to Java
 336     // is that we return an oop, but we can block on exit from this routine and
 337     // a GC can trash the oop in C's return register.  The generated stub will
 338     // fetch the oop from TLS after any possible GC.
 339   }
 340 
 341   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 342   JRT_BLOCK_END;
 343 
 344   // inform GC that we won't do card marks for initializing writes.
 345   SharedRuntime::on_slowpath_allocation_exit(current);
 346 JRT_END
 347 
 348 
 349 // array allocation
 350 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
 351   JRT_BLOCK;
 352 #ifndef PRODUCT
 353   SharedRuntime::_new_array_ctr++;            // new array requires GC
 354 #endif
 355   assert(check_compiled_frame(current), "incorrect caller");
 356 
 357   // Scavenge and allocate an instance.
 358   oop result;
 359 
 360   if (array_type->is_typeArray_klass()) {
 361     // The oopFactory likes to work with the element type.
 362     // (We could bypass the oopFactory, since it doesn't add much value.)
 363     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 364     result = oopFactory::new_typeArray(elem_type, len, THREAD);
 365   } else {
 366     // Although the oopFactory likes to work with the elem_type,
 367     // the compiler prefers the array_type, since it must already have
 368     // that latter value in hand for the fast path.
 369     Handle holder(current, array_type->klass_holder()); // keep the array klass alive
 370     Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
 371     result = oopFactory::new_objArray(elem_type, len, THREAD);
 372   }
 373 
 374   // Pass oops back through thread local storage.  Our apparent type to Java
 375   // is that we return an oop, but we can block on exit from this routine and
 376   // a GC can trash the oop in C's return register.  The generated stub will
 377   // fetch the oop from TLS after any possible GC.
 378   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 379   current->set_vm_result(result);
 380   JRT_BLOCK_END;
 381 
 382   // inform GC that we won't do card marks for initializing writes.
 383   SharedRuntime::on_slowpath_allocation_exit(current);
 384 JRT_END
 385 
 386 // array allocation without zeroing
 387 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
 388   JRT_BLOCK;
 389 #ifndef PRODUCT
 390   SharedRuntime::_new_array_ctr++;            // new array requires GC
 391 #endif
 392   assert(check_compiled_frame(current), "incorrect caller");
 393 
 394   // Scavenge and allocate an instance.
 395   oop result;
 396 
 397   assert(array_type->is_typeArray_klass(), "should be called only for type array");
 398   // The oopFactory likes to work with the element type.
 399   BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 400   result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
 401 
 402   // Pass oops back through thread local storage.  Our apparent type to Java
 403   // is that we return an oop, but we can block on exit from this routine and
 404   // a GC can trash the oop in C's return register.  The generated stub will
 405   // fetch the oop from TLS after any possible GC.
 406   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 407   current->set_vm_result(result);

 419     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 420     size_t hs_bytes = arrayOopDesc::base_offset_in_bytes(elem_type);
 421     assert(is_aligned(hs_bytes, BytesPerInt), "must be 4 byte aligned");
 422     HeapWord* obj = cast_from_oop<HeapWord*>(result);
 423     if (!is_aligned(hs_bytes, BytesPerLong)) {
 424       *reinterpret_cast<jint*>(reinterpret_cast<char*>(obj) + hs_bytes) = 0;
 425       hs_bytes += BytesPerInt;
 426     }
 427 
 428     // Optimized zeroing.
 429     assert(is_aligned(hs_bytes, BytesPerLong), "must be 8-byte aligned");
 430     const size_t aligned_hs = hs_bytes / BytesPerLong;
 431     Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
 432   }
 433 
 434 JRT_END
 435 
 436 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
 437 
 438 // multianewarray for 2 dimensions
 439 JRT_ENTRY(void, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
 440 #ifndef PRODUCT
 441   SharedRuntime::_multi2_ctr++;                // multianewarray for 1 dimension
 442 #endif
 443   assert(check_compiled_frame(current), "incorrect caller");
 444   assert(elem_type->is_klass(), "not a class");
 445   jint dims[2];
 446   dims[0] = len1;
 447   dims[1] = len2;
 448   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 449   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
 450   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 451   current->set_vm_result(obj);
 452 JRT_END
 453 
 454 // multianewarray for 3 dimensions
 455 JRT_ENTRY(void, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
 456 #ifndef PRODUCT
 457   SharedRuntime::_multi3_ctr++;                // multianewarray for 1 dimension
 458 #endif
 459   assert(check_compiled_frame(current), "incorrect caller");
 460   assert(elem_type->is_klass(), "not a class");
 461   jint dims[3];
 462   dims[0] = len1;
 463   dims[1] = len2;
 464   dims[2] = len3;
 465   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 466   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
 467   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 468   current->set_vm_result(obj);
 469 JRT_END
 470 
 471 // multianewarray for 4 dimensions
 472 JRT_ENTRY(void, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
 473 #ifndef PRODUCT
 474   SharedRuntime::_multi4_ctr++;                // multianewarray for 1 dimension
 475 #endif
 476   assert(check_compiled_frame(current), "incorrect caller");
 477   assert(elem_type->is_klass(), "not a class");
 478   jint dims[4];
 479   dims[0] = len1;
 480   dims[1] = len2;
 481   dims[2] = len3;
 482   dims[3] = len4;
 483   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 484   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
 485   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 486   current->set_vm_result(obj);
 487 JRT_END
 488 
 489 // multianewarray for 5 dimensions
 490 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
 491 #ifndef PRODUCT
 492   SharedRuntime::_multi5_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[5];
 497   dims[0] = len1;
 498   dims[1] = len2;
 499   dims[2] = len3;
 500   dims[3] = len4;
 501   dims[4] = len5;
 502   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 503   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
 504   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 505   current->set_vm_result(obj);
 506 JRT_END
 507 
 508 JRT_ENTRY(void, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
 509   assert(check_compiled_frame(current), "incorrect caller");
 510   assert(elem_type->is_klass(), "not a class");
 511   assert(oop(dims)->is_typeArray(), "not an array");
 512 
 513   ResourceMark rm;
 514   jint len = dims->length();
 515   assert(len > 0, "Dimensions array should contain data");
 516   jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
 517   ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
 518                                        c_dims, len);
 519 
 520   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 521   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
 522   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 523   current->set_vm_result(obj);
 524 JRT_END
 525 
 526 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
 527 
 528   // Very few notify/notifyAll operations find any threads on the waitset, so
 529   // the dominant fast-path is to simply return.
 530   // Relatedly, it's critical that notify/notifyAll be fast in order to
 531   // reduce lock hold times.
 532   if (!SafepointSynchronize::is_synchronizing()) {
 533     if (ObjectSynchronizer::quick_notify(obj, current, false)) {
 534       return;
 535     }
 536   }
 537 
 538   // This is the case the fast-path above isn't provisioned to handle.
 539   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 540   // (The fast-path is just a degenerate variant of the slow-path).
 541   // Perform the dreaded state transition and pass control into the slow-path.
 542   JRT_BLOCK;
 543   Handle h_obj(current, obj);
 544   ObjectSynchronizer::notify(h_obj, CHECK);
 545   JRT_BLOCK_END;
 546 JRT_END
 547 
 548 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
 549 
 550   if (!SafepointSynchronize::is_synchronizing() ) {
 551     if (ObjectSynchronizer::quick_notify(obj, current, true)) {
 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::notifyall(h_obj, CHECK);
 563   JRT_BLOCK_END;
 564 JRT_END
 565 
 566 static const TypeFunc* make_new_instance_Type() {
 567   // create input type (domain)
 568   const Type **fields = TypeTuple::fields(1);

1515   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1516   switch (register_save_policy[reg]) {
1517     case 'C': return false; //SOC
1518     case 'E': return true ; //SOE
1519     case 'N': return false; //NS
1520     case 'A': return false; //AS
1521   }
1522   ShouldNotReachHere();
1523   return false;
1524 }
1525 
1526 //-----------------------------------------------------------------------
1527 // Exceptions
1528 //
1529 
1530 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1531 
1532 // The method is an entry that is always called by a C++ method not
1533 // directly from compiled code. Compiled code will call the C++ method following.
1534 // We can't allow async exception to be installed during  exception processing.
1535 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1536   // The frame we rethrow the exception to might not have been processed by the GC yet.
1537   // The stack watermark barrier takes care of detecting that and ensuring the frame
1538   // has updated oops.
1539   StackWatermarkSet::after_unwind(current);
1540 
1541   // Do not confuse exception_oop with pending_exception. The exception_oop
1542   // is only used to pass arguments into the method. Not for general
1543   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1544   // the runtime stubs checks this on exit.
1545   assert(current->exception_oop() != nullptr, "exception oop is found");
1546   address handler_address = nullptr;
1547 
1548   Handle exception(current, current->exception_oop());
1549   address pc = current->exception_pc();
1550 
1551   // Clear out the exception oop and pc since looking up an
1552   // exception handler can cause class loading, which might throw an
1553   // exception and those fields are expected to be clear during
1554   // normal bytecode execution.
1555   current->clear_exception_oop_and_pc();

1789   frame caller_frame = stub_frame.sender(&reg_map);
1790   return caller_frame.is_deoptimized_frame();
1791 }
1792 
1793 static const TypeFunc* make_register_finalizer_Type() {
1794   // create input type (domain)
1795   const Type **fields = TypeTuple::fields(1);
1796   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1797   // // The JavaThread* is passed to each routine as the last argument
1798   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1799   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1800 
1801   // create result type (range)
1802   fields = TypeTuple::fields(0);
1803 
1804   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1805 
1806   return TypeFunc::make(domain,range);
1807 }
1808 














1809 #if INCLUDE_JFR
1810 static const TypeFunc* make_class_id_load_barrier_Type() {
1811   // create input type (domain)
1812   const Type **fields = TypeTuple::fields(1);
1813   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1814   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1815 
1816   // create result type (range)
1817   fields = TypeTuple::fields(0);
1818 
1819   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1820 
1821   return TypeFunc::make(domain,range);
1822 }
1823 #endif // INCLUDE_JFR
1824 
















1825 //-----------------------------------------------------------------------------
1826 static const TypeFunc* make_dtrace_method_entry_exit_Type() {
1827   // create input type (domain)
1828   const Type **fields = TypeTuple::fields(2);
1829   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1830   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
1831   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1832 
1833   // create result type (range)
1834   fields = TypeTuple::fields(0);
1835 
1836   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1837 
1838   return TypeFunc::make(domain,range);
1839 }
1840 
1841 static const TypeFunc* make_dtrace_object_alloc_Type() {
1842   // create input type (domain)
1843   const Type **fields = TypeTuple::fields(2);
1844   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1845   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1846 
1847   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1848 
1849   // create result type (range)
1850   fields = TypeTuple::fields(0);
1851 
1852   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1853 
1854   return TypeFunc::make(domain,range);
1855 }
1856 
1857 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer_C(oopDesc* obj, JavaThread* current))
1858   assert(oopDesc::is_oop(obj), "must be a valid oop");
1859   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1860   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1861 JRT_END
1862 









1863 //-----------------------------------------------------------------------------
1864 
1865 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1866 
1867 //
1868 // dump the collected NamedCounters.
1869 //
1870 void OptoRuntime::print_named_counters() {
1871   int total_lock_count = 0;
1872   int eliminated_lock_count = 0;
1873 
1874   NamedCounter* c = _named_counters;
1875   while (c) {
1876     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1877       int count = c->count();
1878       if (count > 0) {
1879         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1880         if (Verbose) {
1881           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1882         }

2012 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
2013   trace_exception_counter++;
2014   stringStream tempst;
2015 
2016   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
2017   exception_oop->print_value_on(&tempst);
2018   tempst.print(" in ");
2019   CodeBlob* blob = CodeCache::find_blob(exception_pc);
2020   if (blob->is_nmethod()) {
2021     blob->as_nmethod()->method()->print_value_on(&tempst);
2022   } else if (blob->is_runtime_stub()) {
2023     tempst.print("<runtime-stub>");
2024   } else {
2025     tempst.print("<unknown>");
2026   }
2027   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
2028   tempst.print("]");
2029 
2030   st->print_raw_cr(tempst.freeze());
2031 }



































































  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "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/compileBroker.hpp"
  34 #include "compiler/compilerDefinitions.inline.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/objArrayKlass.hpp"
  48 #include "oops/klass.inline.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/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/perfData.inline.hpp"
  71 #include "runtime/sharedRuntime.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "runtime/stackWatermarkSet.hpp"
  74 #include "runtime/synchronizer.hpp"
  75 #include "runtime/threadCritical.hpp"
  76 #include "runtime/threadWXSetters.inline.hpp"
  77 #include "runtime/vframe.hpp"
  78 #include "runtime/vframeArray.hpp"
  79 #include "runtime/vframe_hp.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 
 107 #define C2_BLOB_NAME_DEFINE(name, type)  "C2 Runtime " # name "_blob",
 108 #define C2_STUB_NAME_DEFINE(name, f, t, r)  "C2 Runtime " # name,
 109 #define C2_JVMTI_STUB_NAME_DEFINE(name)  "C2 Runtime " # name,
 110 const char* OptoRuntime::_stub_names[] = {
 111   C2_STUBS_DO(C2_BLOB_NAME_DEFINE, C2_STUB_NAME_DEFINE, C2_JVMTI_STUB_NAME_DEFINE)
 112 };
 113 #undef C2_BLOB_NAME_DEFINE
 114 #undef C2_STUB_NAME_DEFINE
 115 #undef C2_JVMTI_STUB_NAME_DEFINE
 116 
 117 address OptoRuntime::_vtable_must_compile_Java                    = nullptr;
 118 
 119 PerfCounter* _perf_OptoRuntime_class_init_barrier_redundant_count = nullptr;
 120 
 121 // This should be called in an assertion at the start of OptoRuntime routines
 122 // which are entered from compiled code (all of them)
 123 #ifdef ASSERT
 124 static bool check_compiled_frame(JavaThread* thread) {
 125   assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
 126   RegisterMap map(thread,
 127                   RegisterMap::UpdateMap::skip,
 128                   RegisterMap::ProcessFrames::include,
 129                   RegisterMap::WalkContinuation::skip);
 130   frame caller = thread->last_frame().sender(&map);
 131   assert(caller.is_compiled_frame(), "not being called from compiled like code");
 132   return true;
 133 }
 134 #endif // ASSERT
 135 
 136 /*
 137 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
 138   var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
 139   if (var == nullptr) { return false; }
 140 */

 165                   C2_STUB_NAME(name),                                 \
 166                   fancy_jump,                                           \
 167                   pass_tls,                                             \
 168                   pass_retpc);                                          \
 169   if (C2_STUB_FIELD_NAME(name) == nullptr) { return false; }          \
 170 
 171 #define C2_JVMTI_STUB_C_FUNC(name) CAST_FROM_FN_PTR(address, SharedRuntime::name)
 172 
 173 #define GEN_C2_JVMTI_STUB(name)                                       \
 174   STUB_FIELD_NAME(name) =                                               \
 175     generate_stub(env,                                                  \
 176                   notify_jvmti_vthread_Type,                            \
 177                   C2_JVMTI_STUB_C_FUNC(name),                         \
 178                   C2_STUB_NAME(name),                                 \
 179                   0,                                                    \
 180                   true,                                                 \
 181                   false);                                               \
 182   if (STUB_FIELD_NAME(name) == nullptr) { return false; }               \
 183 
 184 bool OptoRuntime::generate(ciEnv* env) {
 185   init_counters();
 186 
 187   C2_STUBS_DO(GEN_C2_BLOB, GEN_C2_STUB, GEN_C2_JVMTI_STUB)
 188 
 189   return true;
 190 }
 191 
 192 #undef GEN_C2_BLOB
 193 
 194 #undef C2_STUB_FIELD_NAME
 195 #undef C2_STUB_TYPEFUNC
 196 #undef C2_STUB_C_FUNC
 197 #undef C2_STUB_NAME
 198 #undef GEN_C2_STUB
 199 
 200 #undef C2_JVMTI_STUB_C_FUNC
 201 #undef GEN_C2_JVMTI_STUB
 202 // #undef gen
 203 
 204 const TypeFunc* OptoRuntime::_new_instance_Type                   = nullptr;
 205 const TypeFunc* OptoRuntime::_new_array_Type                      = nullptr;

 259 const TypeFunc* OptoRuntime::_updateBytesAdler32_Type             = nullptr;
 260 const TypeFunc* OptoRuntime::_osr_end_Type                        = nullptr;
 261 const TypeFunc* OptoRuntime::_register_finalizer_Type             = nullptr;
 262 #if INCLUDE_JFR
 263 const TypeFunc* OptoRuntime::_class_id_load_barrier_Type          = nullptr;
 264 #endif // INCLUDE_JFR
 265 #if INCLUDE_JVMTI
 266 const TypeFunc* OptoRuntime::_notify_jvmti_vthread_Type           = nullptr;
 267 #endif // INCLUDE_JVMTI
 268 const TypeFunc* OptoRuntime::_dtrace_method_entry_exit_Type       = nullptr;
 269 const TypeFunc* OptoRuntime::_dtrace_object_alloc_Type            = nullptr;
 270 
 271 // Helper method to do generation of RunTimeStub's
 272 address OptoRuntime::generate_stub(ciEnv* env,
 273                                    TypeFunc_generator gen, address C_function,
 274                                    const char *name, int is_fancy_jump,
 275                                    bool pass_tls,
 276                                    bool return_pc) {
 277 
 278   // Matching the default directive, we currently have no method to match.
 279   DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompilerThread::current()->compiler());
 280   ResourceMark rm;
 281   Compile C(env, gen, C_function, name, is_fancy_jump, pass_tls, return_pc, directive);
 282   DirectivesStack::release(directive);
 283   return  C.stub_entry_point();
 284 }
 285 
 286 const char* OptoRuntime::stub_name(address entry) {
 287 #ifndef PRODUCT
 288   CodeBlob* cb = CodeCache::find_blob(entry);
 289   RuntimeStub* rs =(RuntimeStub *)cb;
 290   assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
 291   return rs->name();
 292 #else
 293   // Fast implementation for product mode (maybe it should be inlined too)
 294   return "runtime stub";
 295 #endif
 296 }
 297 
 298 // local methods passed as arguments to stub generator that forward
 299 // control to corresponding JRT methods of SharedRuntime

 302                                    oopDesc* dest, jint dest_pos,
 303                                    jint length, JavaThread* thread) {
 304   SharedRuntime::slow_arraycopy_C(src,  src_pos, dest, dest_pos, length, thread);
 305 }
 306 
 307 void OptoRuntime::complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current) {
 308   SharedRuntime::complete_monitor_locking_C(obj, lock, current);
 309 }
 310 
 311 
 312 //=============================================================================
 313 // Opto compiler runtime routines
 314 //=============================================================================
 315 
 316 
 317 //=============================allocation======================================
 318 // We failed the fast-path allocation.  Now we need to do a scavenge or GC
 319 // and try allocation again.
 320 
 321 // object allocation
 322 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_instance_C, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
 323   JRT_BLOCK;
 324 #ifndef PRODUCT
 325   SharedRuntime::_new_instance_ctr++;         // new instance requires GC
 326 #endif
 327   assert(check_compiled_frame(current), "incorrect caller");
 328 
 329   // These checks are cheap to make and support reflective allocation.
 330   int lh = klass->layout_helper();
 331   if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
 332     Handle holder(current, klass->klass_holder()); // keep the klass alive
 333     klass->check_valid_for_instantiation(false, THREAD);
 334     if (!HAS_PENDING_EXCEPTION) {
 335       InstanceKlass::cast(klass)->initialize(THREAD);
 336     }
 337   }
 338 
 339   if (!HAS_PENDING_EXCEPTION) {
 340     // Scavenge and allocate an instance.
 341     Handle holder(current, klass->klass_holder()); // keep the klass alive
 342     oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
 343     current->set_vm_result(result);
 344 
 345     // Pass oops back through thread local storage.  Our apparent type to Java
 346     // is that we return an oop, but we can block on exit from this routine and
 347     // a GC can trash the oop in C's return register.  The generated stub will
 348     // fetch the oop from TLS after any possible GC.
 349   }
 350 
 351   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 352   JRT_BLOCK_END;
 353 
 354   // inform GC that we won't do card marks for initializing writes.
 355   SharedRuntime::on_slowpath_allocation_exit(current);
 356 JRT_END
 357 
 358 
 359 // array allocation
 360 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_C, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
 361   JRT_BLOCK;
 362 #ifndef PRODUCT
 363   SharedRuntime::_new_array_ctr++;            // new array requires GC
 364 #endif
 365   assert(check_compiled_frame(current), "incorrect caller");
 366 
 367   // Scavenge and allocate an instance.
 368   oop result;
 369 
 370   if (array_type->is_typeArray_klass()) {
 371     // The oopFactory likes to work with the element type.
 372     // (We could bypass the oopFactory, since it doesn't add much value.)
 373     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 374     result = oopFactory::new_typeArray(elem_type, len, THREAD);
 375   } else {
 376     // Although the oopFactory likes to work with the elem_type,
 377     // the compiler prefers the array_type, since it must already have
 378     // that latter value in hand for the fast path.
 379     Handle holder(current, array_type->klass_holder()); // keep the array klass alive
 380     Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
 381     result = oopFactory::new_objArray(elem_type, len, THREAD);
 382   }
 383 
 384   // Pass oops back through thread local storage.  Our apparent type to Java
 385   // is that we return an oop, but we can block on exit from this routine and
 386   // a GC can trash the oop in C's return register.  The generated stub will
 387   // fetch the oop from TLS after any possible GC.
 388   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 389   current->set_vm_result(result);
 390   JRT_BLOCK_END;
 391 
 392   // inform GC that we won't do card marks for initializing writes.
 393   SharedRuntime::on_slowpath_allocation_exit(current);
 394 JRT_END
 395 
 396 // array allocation without zeroing
 397 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_nozero_C, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
 398   JRT_BLOCK;
 399 #ifndef PRODUCT
 400   SharedRuntime::_new_array_ctr++;            // new array requires GC
 401 #endif
 402   assert(check_compiled_frame(current), "incorrect caller");
 403 
 404   // Scavenge and allocate an instance.
 405   oop result;
 406 
 407   assert(array_type->is_typeArray_klass(), "should be called only for type array");
 408   // The oopFactory likes to work with the element type.
 409   BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 410   result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
 411 
 412   // Pass oops back through thread local storage.  Our apparent type to Java
 413   // is that we return an oop, but we can block on exit from this routine and
 414   // a GC can trash the oop in C's return register.  The generated stub will
 415   // fetch the oop from TLS after any possible GC.
 416   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 417   current->set_vm_result(result);

 429     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 430     size_t hs_bytes = arrayOopDesc::base_offset_in_bytes(elem_type);
 431     assert(is_aligned(hs_bytes, BytesPerInt), "must be 4 byte aligned");
 432     HeapWord* obj = cast_from_oop<HeapWord*>(result);
 433     if (!is_aligned(hs_bytes, BytesPerLong)) {
 434       *reinterpret_cast<jint*>(reinterpret_cast<char*>(obj) + hs_bytes) = 0;
 435       hs_bytes += BytesPerInt;
 436     }
 437 
 438     // Optimized zeroing.
 439     assert(is_aligned(hs_bytes, BytesPerLong), "must be 8-byte aligned");
 440     const size_t aligned_hs = hs_bytes / BytesPerLong;
 441     Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
 442   }
 443 
 444 JRT_END
 445 
 446 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
 447 
 448 // multianewarray for 2 dimensions
 449 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray2_C, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
 450 #ifndef PRODUCT
 451   SharedRuntime::_multi2_ctr++;                // multianewarray for 1 dimension
 452 #endif
 453   assert(check_compiled_frame(current), "incorrect caller");
 454   assert(elem_type->is_klass(), "not a class");
 455   jint dims[2];
 456   dims[0] = len1;
 457   dims[1] = len2;
 458   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 459   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
 460   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 461   current->set_vm_result(obj);
 462 JRT_END
 463 
 464 // multianewarray for 3 dimensions
 465 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray3_C, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
 466 #ifndef PRODUCT
 467   SharedRuntime::_multi3_ctr++;                // multianewarray for 1 dimension
 468 #endif
 469   assert(check_compiled_frame(current), "incorrect caller");
 470   assert(elem_type->is_klass(), "not a class");
 471   jint dims[3];
 472   dims[0] = len1;
 473   dims[1] = len2;
 474   dims[2] = len3;
 475   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 476   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
 477   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 478   current->set_vm_result(obj);
 479 JRT_END
 480 
 481 // multianewarray for 4 dimensions
 482 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray4_C, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
 483 #ifndef PRODUCT
 484   SharedRuntime::_multi4_ctr++;                // multianewarray for 1 dimension
 485 #endif
 486   assert(check_compiled_frame(current), "incorrect caller");
 487   assert(elem_type->is_klass(), "not a class");
 488   jint dims[4];
 489   dims[0] = len1;
 490   dims[1] = len2;
 491   dims[2] = len3;
 492   dims[3] = len4;
 493   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 494   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
 495   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 496   current->set_vm_result(obj);
 497 JRT_END
 498 
 499 // multianewarray for 5 dimensions
 500 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
 501 #ifndef PRODUCT
 502   SharedRuntime::_multi5_ctr++;                // multianewarray for 1 dimension
 503 #endif
 504   assert(check_compiled_frame(current), "incorrect caller");
 505   assert(elem_type->is_klass(), "not a class");
 506   jint dims[5];
 507   dims[0] = len1;
 508   dims[1] = len2;
 509   dims[2] = len3;
 510   dims[3] = len4;
 511   dims[4] = len5;
 512   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 513   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
 514   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 515   current->set_vm_result(obj);
 516 JRT_END
 517 
 518 JRT_ENTRY_PROF(void, OptoRuntime, multianewarrayN_C, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
 519   assert(check_compiled_frame(current), "incorrect caller");
 520   assert(elem_type->is_klass(), "not a class");
 521   assert(oop(dims)->is_typeArray(), "not an array");
 522 
 523   ResourceMark rm;
 524   jint len = dims->length();
 525   assert(len > 0, "Dimensions array should contain data");
 526   jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
 527   ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
 528                                        c_dims, len);
 529 
 530   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 531   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
 532   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 533   current->set_vm_result(obj);
 534 JRT_END
 535 
 536 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notify_C, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
 537 
 538   // Very few notify/notifyAll operations find any threads on the waitset, so
 539   // the dominant fast-path is to simply return.
 540   // Relatedly, it's critical that notify/notifyAll be fast in order to
 541   // reduce lock hold times.
 542   if (!SafepointSynchronize::is_synchronizing()) {
 543     if (ObjectSynchronizer::quick_notify(obj, current, false)) {
 544       return;
 545     }
 546   }
 547 
 548   // This is the case the fast-path above isn't provisioned to handle.
 549   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 550   // (The fast-path is just a degenerate variant of the slow-path).
 551   // Perform the dreaded state transition and pass control into the slow-path.
 552   JRT_BLOCK;
 553   Handle h_obj(current, obj);
 554   ObjectSynchronizer::notify(h_obj, CHECK);
 555   JRT_BLOCK_END;
 556 JRT_END
 557 
 558 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notifyAll_C, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
 559 
 560   if (!SafepointSynchronize::is_synchronizing() ) {
 561     if (ObjectSynchronizer::quick_notify(obj, current, true)) {
 562       return;
 563     }
 564   }
 565 
 566   // This is the case the fast-path above isn't provisioned to handle.
 567   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 568   // (The fast-path is just a degenerate variant of the slow-path).
 569   // Perform the dreaded state transition and pass control into the slow-path.
 570   JRT_BLOCK;
 571   Handle h_obj(current, obj);
 572   ObjectSynchronizer::notifyall(h_obj, CHECK);
 573   JRT_BLOCK_END;
 574 JRT_END
 575 
 576 static const TypeFunc* make_new_instance_Type() {
 577   // create input type (domain)
 578   const Type **fields = TypeTuple::fields(1);

1525   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1526   switch (register_save_policy[reg]) {
1527     case 'C': return false; //SOC
1528     case 'E': return true ; //SOE
1529     case 'N': return false; //NS
1530     case 'A': return false; //AS
1531   }
1532   ShouldNotReachHere();
1533   return false;
1534 }
1535 
1536 //-----------------------------------------------------------------------
1537 // Exceptions
1538 //
1539 
1540 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1541 
1542 // The method is an entry that is always called by a C++ method not
1543 // directly from compiled code. Compiled code will call the C++ method following.
1544 // We can't allow async exception to be installed during  exception processing.
1545 JRT_ENTRY_NO_ASYNC_PROF(address, OptoRuntime, handle_exception_C_helper, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1546   // The frame we rethrow the exception to might not have been processed by the GC yet.
1547   // The stack watermark barrier takes care of detecting that and ensuring the frame
1548   // has updated oops.
1549   StackWatermarkSet::after_unwind(current);
1550 
1551   // Do not confuse exception_oop with pending_exception. The exception_oop
1552   // is only used to pass arguments into the method. Not for general
1553   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1554   // the runtime stubs checks this on exit.
1555   assert(current->exception_oop() != nullptr, "exception oop is found");
1556   address handler_address = nullptr;
1557 
1558   Handle exception(current, current->exception_oop());
1559   address pc = current->exception_pc();
1560 
1561   // Clear out the exception oop and pc since looking up an
1562   // exception handler can cause class loading, which might throw an
1563   // exception and those fields are expected to be clear during
1564   // normal bytecode execution.
1565   current->clear_exception_oop_and_pc();

1799   frame caller_frame = stub_frame.sender(&reg_map);
1800   return caller_frame.is_deoptimized_frame();
1801 }
1802 
1803 static const TypeFunc* make_register_finalizer_Type() {
1804   // create input type (domain)
1805   const Type **fields = TypeTuple::fields(1);
1806   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1807   // // The JavaThread* is passed to each routine as the last argument
1808   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1809   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1810 
1811   // create result type (range)
1812   fields = TypeTuple::fields(0);
1813 
1814   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1815 
1816   return TypeFunc::make(domain,range);
1817 }
1818 
1819 const TypeFunc *OptoRuntime::class_init_barrier_Type() {
1820   // create input type (domain)
1821   const Type** fields = TypeTuple::fields(1);
1822   fields[TypeFunc::Parms+0] = TypeKlassPtr::NOTNULL;
1823   // // The JavaThread* is passed to each routine as the last argument
1824   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1825   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1826 
1827   // create result type (range)
1828   fields = TypeTuple::fields(0);
1829   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
1830   return TypeFunc::make(domain,range);
1831 }
1832 
1833 #if INCLUDE_JFR
1834 static const TypeFunc* make_class_id_load_barrier_Type() {
1835   // create input type (domain)
1836   const Type **fields = TypeTuple::fields(1);
1837   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1838   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1839 
1840   // create result type (range)
1841   fields = TypeTuple::fields(0);
1842 
1843   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1844 
1845   return TypeFunc::make(domain,range);
1846 }
1847 #endif // INCLUDE_JFR
1848 
1849 //-----------------------------------------------------------------------------
1850 // runtime upcall support
1851 const TypeFunc *OptoRuntime::runtime_up_call_Type() {
1852   // create input type (domain)
1853   const Type **fields = TypeTuple::fields(1);
1854   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1855   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1856 
1857   // create result type (range)
1858   fields = TypeTuple::fields(0);
1859 
1860   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1861 
1862   return TypeFunc::make(domain,range);
1863 }
1864 
1865 //-----------------------------------------------------------------------------
1866 static const TypeFunc* make_dtrace_method_entry_exit_Type() {
1867   // create input type (domain)
1868   const Type **fields = TypeTuple::fields(2);
1869   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1870   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
1871   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1872 
1873   // create result type (range)
1874   fields = TypeTuple::fields(0);
1875 
1876   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1877 
1878   return TypeFunc::make(domain,range);
1879 }
1880 
1881 static const TypeFunc* make_dtrace_object_alloc_Type() {
1882   // create input type (domain)
1883   const Type **fields = TypeTuple::fields(2);
1884   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1885   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1886 
1887   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1888 
1889   // create result type (range)
1890   fields = TypeTuple::fields(0);
1891 
1892   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1893 
1894   return TypeFunc::make(domain,range);
1895 }
1896 
1897 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, register_finalizer_C, OptoRuntime::register_finalizer_C(oopDesc* obj, JavaThread* current))
1898   assert(oopDesc::is_oop(obj), "must be a valid oop");
1899   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1900   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1901 JRT_END
1902 
1903 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, class_init_barrier_C, OptoRuntime::class_init_barrier_C(Klass* k, JavaThread* current))
1904   InstanceKlass* ik = InstanceKlass::cast(k);
1905   if (ik->should_be_initialized()) {
1906     ik->initialize(CHECK);
1907   } else if (UsePerfData) {
1908     _perf_OptoRuntime_class_init_barrier_redundant_count->inc();
1909   }
1910 JRT_END
1911 
1912 //-----------------------------------------------------------------------------
1913 
1914 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1915 
1916 //
1917 // dump the collected NamedCounters.
1918 //
1919 void OptoRuntime::print_named_counters() {
1920   int total_lock_count = 0;
1921   int eliminated_lock_count = 0;
1922 
1923   NamedCounter* c = _named_counters;
1924   while (c) {
1925     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1926       int count = c->count();
1927       if (count > 0) {
1928         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1929         if (Verbose) {
1930           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1931         }

2061 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
2062   trace_exception_counter++;
2063   stringStream tempst;
2064 
2065   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
2066   exception_oop->print_value_on(&tempst);
2067   tempst.print(" in ");
2068   CodeBlob* blob = CodeCache::find_blob(exception_pc);
2069   if (blob->is_nmethod()) {
2070     blob->as_nmethod()->method()->print_value_on(&tempst);
2071   } else if (blob->is_runtime_stub()) {
2072     tempst.print("<runtime-stub>");
2073   } else {
2074     tempst.print("<unknown>");
2075   }
2076   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
2077   tempst.print("]");
2078 
2079   st->print_raw_cr(tempst.freeze());
2080 }
2081 
2082 #define DO_COUNTERS2(macro2, macro1) \
2083   macro2(OptoRuntime, new_instance_C) \
2084   macro2(OptoRuntime, new_array_C) \
2085   macro2(OptoRuntime, new_array_nozero_C) \
2086   macro2(OptoRuntime, multianewarray2_C) \
2087   macro2(OptoRuntime, multianewarray3_C) \
2088   macro2(OptoRuntime, multianewarray4_C) \
2089   macro2(OptoRuntime, multianewarrayN_C) \
2090   macro2(OptoRuntime, monitor_notify_C) \
2091   macro2(OptoRuntime, monitor_notifyAll_C) \
2092   macro2(OptoRuntime, handle_exception_C_helper) \
2093   macro2(OptoRuntime, register_finalizer_C) \
2094   macro2(OptoRuntime, class_init_barrier_C) \
2095   macro1(OptoRuntime, class_init_barrier_redundant)
2096 
2097 #define INIT_COUNTER_TIME_AND_CNT(sub, name) \
2098   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
2099   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
2100 
2101 #define INIT_COUNTER_CNT(sub, name) \
2102   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
2103 
2104 void OptoRuntime::init_counters() {
2105   assert(CompilerConfig::is_c2_enabled(), "");
2106 
2107   if (UsePerfData) {
2108     EXCEPTION_MARK;
2109 
2110     DO_COUNTERS2(INIT_COUNTER_TIME_AND_CNT, INIT_COUNTER_CNT)
2111 
2112     if (HAS_PENDING_EXCEPTION) {
2113       vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
2114     }
2115   }
2116 }
2117 #undef INIT_COUNTER_TIME_AND_CNT
2118 #undef INIT_COUNTER_CNT
2119 
2120 #define PRINT_COUNTER_TIME_AND_CNT(sub, name) { \
2121   jlong count = _perf_##sub##_##name##_count->get_value(); \
2122   if (count > 0) { \
2123     st->print_cr("  %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
2124                  _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
2125                  _perf_##sub##_##name##_timer->thread_counter_value_us(), \
2126                  count); \
2127   }}
2128 
2129 #define PRINT_COUNTER_CNT(sub, name) { \
2130   jlong count = _perf_##sub##_##name##_count->get_value(); \
2131   if (count > 0) { \
2132     st->print_cr("  %-30s = " JLONG_FORMAT_W(5) " events", #name, count); \
2133   }}
2134 
2135 void OptoRuntime::print_counters_on(outputStream* st) {
2136   if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c2_enabled()) {
2137     DO_COUNTERS2(PRINT_COUNTER_TIME_AND_CNT, PRINT_COUNTER_CNT)
2138   } else {
2139     st->print_cr("  OptoRuntime: no info (%s is disabled)",
2140                  (!CompilerConfig::is_c2_enabled() ? "C2" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
2141   }
2142 }
2143 
2144 #undef PRINT_COUNTER_TIME_AND_CNT
2145 #undef PRINT_COUNTER_CNT
2146 #undef DO_COUNTERS2
< prev index next >