< prev index next >

src/hotspot/share/opto/runtime.cpp

Print this page

  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/sharedRuntime.hpp"
  70 #include "runtime/signature.hpp"
  71 #include "runtime/stackWatermarkSet.hpp"
  72 #include "runtime/synchronizer.hpp"
  73 #include "runtime/threadWXSetters.inline.hpp"
  74 #include "runtime/vframe.hpp"
  75 #include "runtime/vframe_hp.hpp"
  76 #include "runtime/vframeArray.hpp"

  77 #include "utilities/copy.hpp"
  78 #include "utilities/preserveException.hpp"
  79 
  80 
  81 // For debugging purposes:
  82 //  To force FullGCALot inside a runtime function, add the following two lines
  83 //
  84 //  Universe::release_fullgc_alot_dummy();
  85 //  Universe::heap()->collect();
  86 //
  87 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
  88 
  89 
  90 #define C2_BLOB_FIELD_DEFINE(name, type) \
  91   type* OptoRuntime:: BLOB_FIELD_NAME(name)  = nullptr;
  92 #define C2_STUB_FIELD_NAME(name) _ ## name ## _Java
  93 #define C2_STUB_FIELD_DEFINE(name, f, t, r) \
  94   address OptoRuntime:: C2_STUB_FIELD_NAME(name) = nullptr;
  95 #define C2_JVMTI_STUB_FIELD_DEFINE(name) \
  96   address OptoRuntime:: STUB_FIELD_NAME(name) = nullptr;
  97 C2_STUBS_DO(C2_BLOB_FIELD_DEFINE, C2_STUB_FIELD_DEFINE, C2_JVMTI_STUB_FIELD_DEFINE)
  98 #undef C2_BLOB_FIELD_DEFINE
  99 #undef C2_STUB_FIELD_DEFINE
 100 #undef C2_JVMTI_STUB_FIELD_DEFINE
 101 




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

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

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

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

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

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

1843   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1844   switch (register_save_policy[reg]) {
1845     case 'C': return false; //SOC
1846     case 'E': return true ; //SOE
1847     case 'N': return false; //NS
1848     case 'A': return false; //AS
1849   }
1850   ShouldNotReachHere();
1851   return false;
1852 }
1853 
1854 //-----------------------------------------------------------------------
1855 // Exceptions
1856 //
1857 
1858 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1859 
1860 // The method is an entry that is always called by a C++ method not
1861 // directly from compiled code. Compiled code will call the C++ method following.
1862 // We can't allow async exception to be installed during  exception processing.
1863 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1864   // The frame we rethrow the exception to might not have been processed by the GC yet.
1865   // The stack watermark barrier takes care of detecting that and ensuring the frame
1866   // has updated oops.
1867   StackWatermarkSet::after_unwind(current);
1868 
1869   // Do not confuse exception_oop with pending_exception. The exception_oop
1870   // is only used to pass arguments into the method. Not for general
1871   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1872   // the runtime stubs checks this on exit.
1873   assert(current->exception_oop() != nullptr, "exception oop is found");
1874   address handler_address = nullptr;
1875 
1876   Handle exception(current, current->exception_oop());
1877   address pc = current->exception_pc();
1878 
1879   // Clear out the exception oop and pc since looking up an
1880   // exception handler can cause class loading, which might throw an
1881   // exception and those fields are expected to be clear during
1882   // normal bytecode execution.
1883   current->clear_exception_oop_and_pc();

2113   frame caller_frame = stub_frame.sender(&reg_map);
2114   return caller_frame.is_deoptimized_frame();
2115 }
2116 
2117 static const TypeFunc* make_register_finalizer_Type() {
2118   // create input type (domain)
2119   const Type **fields = TypeTuple::fields(1);
2120   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
2121   // // The JavaThread* is passed to each routine as the last argument
2122   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
2123   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2124 
2125   // create result type (range)
2126   fields = TypeTuple::fields(0);
2127 
2128   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2129 
2130   return TypeFunc::make(domain,range);
2131 }
2132 














2133 #if INCLUDE_JFR
2134 static const TypeFunc* make_class_id_load_barrier_Type() {
2135   // create input type (domain)
2136   const Type **fields = TypeTuple::fields(1);
2137   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
2138   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
2139 
2140   // create result type (range)
2141   fields = TypeTuple::fields(0);
2142 
2143   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
2144 
2145   return TypeFunc::make(domain,range);
2146 }
2147 #endif // INCLUDE_JFR
2148 
















2149 //-----------------------------------------------------------------------------
2150 static const TypeFunc* make_dtrace_method_entry_exit_Type() {
2151   // create input type (domain)
2152   const Type **fields = TypeTuple::fields(2);
2153   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2154   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
2155   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2156 
2157   // create result type (range)
2158   fields = TypeTuple::fields(0);
2159 
2160   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2161 
2162   return TypeFunc::make(domain,range);
2163 }
2164 
2165 static const TypeFunc* make_dtrace_object_alloc_Type() {
2166   // create input type (domain)
2167   const Type **fields = TypeTuple::fields(2);
2168   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2169   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
2170 
2171   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2172 
2173   // create result type (range)
2174   fields = TypeTuple::fields(0);
2175 
2176   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2177 
2178   return TypeFunc::make(domain,range);
2179 }
2180 
2181 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer_C(oopDesc* obj, JavaThread* current))
2182   assert(oopDesc::is_oop(obj), "must be a valid oop");
2183   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
2184   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
2185 JRT_END
2186 









2187 //-----------------------------------------------------------------------------
2188 
2189 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
2190 
2191 //
2192 // dump the collected NamedCounters.
2193 //
2194 void OptoRuntime::print_named_counters() {
2195   int total_lock_count = 0;
2196   int eliminated_lock_count = 0;
2197 
2198   NamedCounter* c = _named_counters;
2199   while (c) {
2200     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
2201       int count = c->count();
2202       if (count > 0) {
2203         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
2204         if (Verbose) {
2205           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
2206         }

2349 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
2350   trace_exception_counter++;
2351   stringStream tempst;
2352 
2353   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
2354   exception_oop->print_value_on(&tempst);
2355   tempst.print(" in ");
2356   CodeBlob* blob = CodeCache::find_blob(exception_pc);
2357   if (blob->is_nmethod()) {
2358     blob->as_nmethod()->method()->print_value_on(&tempst);
2359   } else if (blob->is_runtime_stub()) {
2360     tempst.print("<runtime-stub>");
2361   } else {
2362     tempst.print("<unknown>");
2363   }
2364   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
2365   tempst.print("]");
2366 
2367   st->print_raw_cr(tempst.freeze());
2368 }



































































  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/perfData.inline.hpp"
  72 #include "runtime/sharedRuntime.hpp"
  73 #include "runtime/signature.hpp"
  74 #include "runtime/stackWatermarkSet.hpp"
  75 #include "runtime/synchronizer.hpp"
  76 #include "runtime/threadWXSetters.inline.hpp"
  77 #include "runtime/vframe.hpp"
  78 #include "runtime/vframe_hp.hpp"
  79 #include "runtime/vframeArray.hpp"
  80 #include "services/management.hpp"
  81 #include "utilities/copy.hpp"
  82 #include "utilities/preserveException.hpp"
  83 
  84 
  85 // For debugging purposes:
  86 //  To force FullGCALot inside a runtime function, add the following two lines
  87 //
  88 //  Universe::release_fullgc_alot_dummy();
  89 //  Universe::heap()->collect();
  90 //
  91 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
  92 
  93 
  94 #define C2_BLOB_FIELD_DEFINE(name, type) \
  95   type* OptoRuntime:: BLOB_FIELD_NAME(name)  = nullptr;
  96 #define C2_STUB_FIELD_NAME(name) _ ## name ## _Java
  97 #define C2_STUB_FIELD_DEFINE(name, f, t, r) \
  98   address OptoRuntime:: C2_STUB_FIELD_NAME(name) = nullptr;
  99 #define C2_JVMTI_STUB_FIELD_DEFINE(name) \
 100   address OptoRuntime:: STUB_FIELD_NAME(name) = nullptr;
 101 C2_STUBS_DO(C2_BLOB_FIELD_DEFINE, C2_STUB_FIELD_DEFINE, C2_JVMTI_STUB_FIELD_DEFINE)
 102 #undef C2_BLOB_FIELD_DEFINE
 103 #undef C2_STUB_FIELD_DEFINE
 104 #undef C2_JVMTI_STUB_FIELD_DEFINE
 105 
 106 address OptoRuntime::_vtable_must_compile_Java                    = nullptr;
 107 
 108 PerfCounter* _perf_OptoRuntime_class_init_barrier_redundant_count = nullptr;
 109 
 110 // This should be called in an assertion at the start of OptoRuntime routines
 111 // which are entered from compiled code (all of them)
 112 #ifdef ASSERT
 113 static bool check_compiled_frame(JavaThread* thread) {
 114   assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
 115   RegisterMap map(thread,
 116                   RegisterMap::UpdateMap::skip,
 117                   RegisterMap::ProcessFrames::include,
 118                   RegisterMap::WalkContinuation::skip);
 119   frame caller = thread->last_frame().sender(&map);
 120   assert(caller.is_compiled_frame(), "not being called from compiled like code");
 121   return true;
 122 }
 123 #endif // ASSERT
 124 
 125 /*
 126 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
 127   var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
 128   if (var == nullptr) { return false; }
 129 */

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

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

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

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

1852   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1853   switch (register_save_policy[reg]) {
1854     case 'C': return false; //SOC
1855     case 'E': return true ; //SOE
1856     case 'N': return false; //NS
1857     case 'A': return false; //AS
1858   }
1859   ShouldNotReachHere();
1860   return false;
1861 }
1862 
1863 //-----------------------------------------------------------------------
1864 // Exceptions
1865 //
1866 
1867 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1868 
1869 // The method is an entry that is always called by a C++ method not
1870 // directly from compiled code. Compiled code will call the C++ method following.
1871 // We can't allow async exception to be installed during  exception processing.
1872 JRT_ENTRY_NO_ASYNC_PROF(address, OptoRuntime, handle_exception_C_helper, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1873   // The frame we rethrow the exception to might not have been processed by the GC yet.
1874   // The stack watermark barrier takes care of detecting that and ensuring the frame
1875   // has updated oops.
1876   StackWatermarkSet::after_unwind(current);
1877 
1878   // Do not confuse exception_oop with pending_exception. The exception_oop
1879   // is only used to pass arguments into the method. Not for general
1880   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1881   // the runtime stubs checks this on exit.
1882   assert(current->exception_oop() != nullptr, "exception oop is found");
1883   address handler_address = nullptr;
1884 
1885   Handle exception(current, current->exception_oop());
1886   address pc = current->exception_pc();
1887 
1888   // Clear out the exception oop and pc since looking up an
1889   // exception handler can cause class loading, which might throw an
1890   // exception and those fields are expected to be clear during
1891   // normal bytecode execution.
1892   current->clear_exception_oop_and_pc();

2122   frame caller_frame = stub_frame.sender(&reg_map);
2123   return caller_frame.is_deoptimized_frame();
2124 }
2125 
2126 static const TypeFunc* make_register_finalizer_Type() {
2127   // create input type (domain)
2128   const Type **fields = TypeTuple::fields(1);
2129   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
2130   // // The JavaThread* is passed to each routine as the last argument
2131   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
2132   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2133 
2134   // create result type (range)
2135   fields = TypeTuple::fields(0);
2136 
2137   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2138 
2139   return TypeFunc::make(domain,range);
2140 }
2141 
2142 const TypeFunc *OptoRuntime::class_init_barrier_Type() {
2143   // create input type (domain)
2144   const Type** fields = TypeTuple::fields(1);
2145   fields[TypeFunc::Parms+0] = TypeKlassPtr::NOTNULL;
2146   // // The JavaThread* is passed to each routine as the last argument
2147   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
2148   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+1, fields);
2149 
2150   // create result type (range)
2151   fields = TypeTuple::fields(0);
2152   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
2153   return TypeFunc::make(domain,range);
2154 }
2155 
2156 #if INCLUDE_JFR
2157 static const TypeFunc* make_class_id_load_barrier_Type() {
2158   // create input type (domain)
2159   const Type **fields = TypeTuple::fields(1);
2160   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
2161   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
2162 
2163   // create result type (range)
2164   fields = TypeTuple::fields(0);
2165 
2166   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
2167 
2168   return TypeFunc::make(domain,range);
2169 }
2170 #endif // INCLUDE_JFR
2171 
2172 //-----------------------------------------------------------------------------
2173 // runtime upcall support
2174 const TypeFunc *OptoRuntime::runtime_up_call_Type() {
2175   // create input type (domain)
2176   const Type **fields = TypeTuple::fields(1);
2177   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2178   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
2179 
2180   // create result type (range)
2181   fields = TypeTuple::fields(0);
2182 
2183   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2184 
2185   return TypeFunc::make(domain,range);
2186 }
2187 
2188 //-----------------------------------------------------------------------------
2189 static const TypeFunc* make_dtrace_method_entry_exit_Type() {
2190   // create input type (domain)
2191   const Type **fields = TypeTuple::fields(2);
2192   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2193   fields[TypeFunc::Parms+1] = TypeMetadataPtr::BOTTOM;  // Method*;    Method we are entering
2194   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2195 
2196   // create result type (range)
2197   fields = TypeTuple::fields(0);
2198 
2199   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2200 
2201   return TypeFunc::make(domain,range);
2202 }
2203 
2204 static const TypeFunc* make_dtrace_object_alloc_Type() {
2205   // create input type (domain)
2206   const Type **fields = TypeTuple::fields(2);
2207   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
2208   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
2209 
2210   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
2211 
2212   // create result type (range)
2213   fields = TypeTuple::fields(0);
2214 
2215   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
2216 
2217   return TypeFunc::make(domain,range);
2218 }
2219 
2220 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, register_finalizer_C, OptoRuntime::register_finalizer_C(oopDesc* obj, JavaThread* current))
2221   assert(oopDesc::is_oop(obj), "must be a valid oop");
2222   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
2223   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
2224 JRT_END
2225 
2226 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, class_init_barrier_C, OptoRuntime::class_init_barrier_C(Klass* k, JavaThread* current))
2227   InstanceKlass* ik = InstanceKlass::cast(k);
2228   if (ik->should_be_initialized()) {
2229     ik->initialize(CHECK);
2230   } else if (UsePerfData) {
2231     _perf_OptoRuntime_class_init_barrier_redundant_count->inc();
2232   }
2233 JRT_END
2234 
2235 //-----------------------------------------------------------------------------
2236 
2237 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
2238 
2239 //
2240 // dump the collected NamedCounters.
2241 //
2242 void OptoRuntime::print_named_counters() {
2243   int total_lock_count = 0;
2244   int eliminated_lock_count = 0;
2245 
2246   NamedCounter* c = _named_counters;
2247   while (c) {
2248     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
2249       int count = c->count();
2250       if (count > 0) {
2251         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
2252         if (Verbose) {
2253           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
2254         }

2397 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
2398   trace_exception_counter++;
2399   stringStream tempst;
2400 
2401   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
2402   exception_oop->print_value_on(&tempst);
2403   tempst.print(" in ");
2404   CodeBlob* blob = CodeCache::find_blob(exception_pc);
2405   if (blob->is_nmethod()) {
2406     blob->as_nmethod()->method()->print_value_on(&tempst);
2407   } else if (blob->is_runtime_stub()) {
2408     tempst.print("<runtime-stub>");
2409   } else {
2410     tempst.print("<unknown>");
2411   }
2412   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
2413   tempst.print("]");
2414 
2415   st->print_raw_cr(tempst.freeze());
2416 }
2417 
2418 #define DO_COUNTERS2(macro2, macro1) \
2419   macro2(OptoRuntime, new_instance_C) \
2420   macro2(OptoRuntime, new_array_C) \
2421   macro2(OptoRuntime, new_array_nozero_C) \
2422   macro2(OptoRuntime, multianewarray2_C) \
2423   macro2(OptoRuntime, multianewarray3_C) \
2424   macro2(OptoRuntime, multianewarray4_C) \
2425   macro2(OptoRuntime, multianewarrayN_C) \
2426   macro2(OptoRuntime, monitor_notify_C) \
2427   macro2(OptoRuntime, monitor_notifyAll_C) \
2428   macro2(OptoRuntime, handle_exception_C_helper) \
2429   macro2(OptoRuntime, register_finalizer_C) \
2430   macro2(OptoRuntime, class_init_barrier_C) \
2431   macro1(OptoRuntime, class_init_barrier_redundant)
2432 
2433 #define INIT_COUNTER_TIME_AND_CNT(sub, name) \
2434   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
2435   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
2436 
2437 #define INIT_COUNTER_CNT(sub, name) \
2438   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
2439 
2440 void OptoRuntime::init_counters() {
2441   assert(CompilerConfig::is_c2_enabled(), "");
2442 
2443   if (UsePerfData) {
2444     EXCEPTION_MARK;
2445 
2446     DO_COUNTERS2(INIT_COUNTER_TIME_AND_CNT, INIT_COUNTER_CNT)
2447 
2448     if (HAS_PENDING_EXCEPTION) {
2449       vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
2450     }
2451   }
2452 }
2453 #undef INIT_COUNTER_TIME_AND_CNT
2454 #undef INIT_COUNTER_CNT
2455 
2456 #define PRINT_COUNTER_TIME_AND_CNT(sub, name) { \
2457   jlong count = _perf_##sub##_##name##_count->get_value(); \
2458   if (count > 0) { \
2459     st->print_cr("  %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
2460                  _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
2461                  _perf_##sub##_##name##_timer->thread_counter_value_us(), \
2462                  count); \
2463   }}
2464 
2465 #define PRINT_COUNTER_CNT(sub, name) { \
2466   jlong count = _perf_##sub##_##name##_count->get_value(); \
2467   if (count > 0) { \
2468     st->print_cr("  %-30s = " JLONG_FORMAT_W(5) " events", #name, count); \
2469   }}
2470 
2471 void OptoRuntime::print_counters_on(outputStream* st) {
2472   if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c2_enabled()) {
2473     DO_COUNTERS2(PRINT_COUNTER_TIME_AND_CNT, PRINT_COUNTER_CNT)
2474   } else {
2475     st->print_cr("  OptoRuntime: no info (%s is disabled)",
2476                  (!CompilerConfig::is_c2_enabled() ? "C2" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
2477   }
2478 }
2479 
2480 #undef PRINT_COUNTER_TIME_AND_CNT
2481 #undef PRINT_COUNTER_CNT
2482 #undef DO_COUNTERS2
< prev index next >