< 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 "precompiled.hpp"
  26 #include "classfile/vmClasses.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/compiledIC.hpp"
  30 #include "code/nmethod.hpp"
  31 #include "code/pcDesc.hpp"
  32 #include "code/scopeDesc.hpp"
  33 #include "code/vtableStubs.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/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/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/threadCritical.hpp"
  74 #include "runtime/threadWXSetters.inline.hpp"
  75 #include "runtime/vframe.hpp"
  76 #include "runtime/vframeArray.hpp"
  77 #include "runtime/vframe_hp.hpp"

  78 #include "utilities/copy.hpp"
  79 #include "utilities/preserveException.hpp"
  80 
  81 
  82 // For debugging purposes:
  83 //  To force FullGCALot inside a runtime function, add the following two lines
  84 //
  85 //  Universe::release_fullgc_alot_dummy();
  86 //  Universe::heap()->collect();
  87 //
  88 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
  89 
  90 
  91 
  92 
  93 // Compiled code entry points
  94 address OptoRuntime::_new_instance_Java                           = nullptr;
  95 address OptoRuntime::_new_array_Java                              = nullptr;
  96 address OptoRuntime::_new_array_nozero_Java                       = nullptr;
  97 address OptoRuntime::_multianewarray2_Java                        = nullptr;
  98 address OptoRuntime::_multianewarray3_Java                        = nullptr;
  99 address OptoRuntime::_multianewarray4_Java                        = nullptr;
 100 address OptoRuntime::_multianewarray5_Java                        = nullptr;
 101 address OptoRuntime::_multianewarrayN_Java                        = nullptr;
 102 address OptoRuntime::_vtable_must_compile_Java                    = nullptr;
 103 address OptoRuntime::_complete_monitor_locking_Java               = nullptr;
 104 address OptoRuntime::_monitor_notify_Java                         = nullptr;
 105 address OptoRuntime::_monitor_notifyAll_Java                      = nullptr;
 106 address OptoRuntime::_rethrow_Java                                = nullptr;
 107 
 108 address OptoRuntime::_slow_arraycopy_Java                         = nullptr;
 109 address OptoRuntime::_register_finalizer_Java                     = nullptr;

 110 #if INCLUDE_JVMTI
 111 address OptoRuntime::_notify_jvmti_vthread_start                  = nullptr;
 112 address OptoRuntime::_notify_jvmti_vthread_end                    = nullptr;
 113 address OptoRuntime::_notify_jvmti_vthread_mount                  = nullptr;
 114 address OptoRuntime::_notify_jvmti_vthread_unmount                = nullptr;
 115 #endif
 116 
 117 ExceptionBlob* OptoRuntime::_exception_blob;
 118 


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

 140 
 141   generate_exception_blob();
 142 
 143   // Note: tls: Means fetching the return oop out of the thread-local storage
 144   //
 145   //   variable/name                       type-function-gen              , runtime method                  ,fncy_jp, tls,retpc
 146   // -------------------------------------------------------------------------------------------------------------------------------
 147   gen(env, _new_instance_Java              , new_instance_Type            , new_instance_C                  ,    0 , true, false);
 148   gen(env, _new_array_Java                 , new_array_Type               , new_array_C                     ,    0 , true, false);
 149   gen(env, _new_array_nozero_Java          , new_array_Type               , new_array_nozero_C              ,    0 , true, false);
 150   gen(env, _multianewarray2_Java           , multianewarray2_Type         , multianewarray2_C               ,    0 , true, false);
 151   gen(env, _multianewarray3_Java           , multianewarray3_Type         , multianewarray3_C               ,    0 , true, false);
 152   gen(env, _multianewarray4_Java           , multianewarray4_Type         , multianewarray4_C               ,    0 , true, false);
 153   gen(env, _multianewarray5_Java           , multianewarray5_Type         , multianewarray5_C               ,    0 , true, false);
 154   gen(env, _multianewarrayN_Java           , multianewarrayN_Type         , multianewarrayN_C               ,    0 , true, false);
 155 #if INCLUDE_JVMTI
 156   gen(env, _notify_jvmti_vthread_start     , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_start, 0, true, false);
 157   gen(env, _notify_jvmti_vthread_end       , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_end,   0, true, false);
 158   gen(env, _notify_jvmti_vthread_mount     , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_mount, 0, true, false);
 159   gen(env, _notify_jvmti_vthread_unmount   , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_unmount, 0, true, false);
 160 #endif
 161   gen(env, _complete_monitor_locking_Java  , complete_monitor_enter_Type  , SharedRuntime::complete_monitor_locking_C, 0, false, false);
 162   gen(env, _monitor_notify_Java            , monitor_notify_Type          , monitor_notify_C                ,    0 , false, false);
 163   gen(env, _monitor_notifyAll_Java         , monitor_notify_Type          , monitor_notifyAll_C             ,    0 , false, false);
 164   gen(env, _rethrow_Java                   , rethrow_Type                 , rethrow_C                       ,    2 , true , true );
 165 
 166   gen(env, _slow_arraycopy_Java            , slow_arraycopy_Type          , SharedRuntime::slow_arraycopy_C ,    0 , false, false);
 167   gen(env, _register_finalizer_Java        , register_finalizer_Type      , register_finalizer              ,    0 , false, false);

 168 
 169   return true;
 170 }
 171 
 172 #undef gen
 173 
 174 
 175 // Helper method to do generation of RunTimeStub's
 176 address OptoRuntime::generate_stub(ciEnv* env,
 177                                    TypeFunc_generator gen, address C_function,
 178                                    const char *name, int is_fancy_jump,
 179                                    bool pass_tls,
 180                                    bool return_pc) {
 181 
 182   // Matching the default directive, we currently have no method to match.
 183   DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompileBroker::compiler(CompLevel_full_optimization));
 184   ResourceMark rm;
 185   Compile C(env, gen, C_function, name, is_fancy_jump, pass_tls, return_pc, directive);
 186   DirectivesStack::release(directive);
 187   return  C.stub_entry_point();
 188 }
 189 
 190 const char* OptoRuntime::stub_name(address entry) {
 191 #ifndef PRODUCT
 192   CodeBlob* cb = CodeCache::find_blob(entry);
 193   RuntimeStub* rs =(RuntimeStub *)cb;
 194   assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
 195   return rs->name();
 196 #else
 197   // Fast implementation for product mode (maybe it should be inlined too)
 198   return "runtime stub";
 199 #endif
 200 }
 201 
 202 
 203 //=============================================================================
 204 // Opto compiler runtime routines
 205 //=============================================================================
 206 
 207 
 208 //=============================allocation======================================
 209 // We failed the fast-path allocation.  Now we need to do a scavenge or GC
 210 // and try allocation again.
 211 
 212 // object allocation
 213 JRT_BLOCK_ENTRY(void, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
 214   JRT_BLOCK;
 215 #ifndef PRODUCT
 216   SharedRuntime::_new_instance_ctr++;         // new instance requires GC
 217 #endif
 218   assert(check_compiled_frame(current), "incorrect caller");
 219 
 220   // These checks are cheap to make and support reflective allocation.
 221   int lh = klass->layout_helper();
 222   if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
 223     Handle holder(current, klass->klass_holder()); // keep the klass alive
 224     klass->check_valid_for_instantiation(false, THREAD);
 225     if (!HAS_PENDING_EXCEPTION) {
 226       InstanceKlass::cast(klass)->initialize(THREAD);
 227     }
 228   }
 229 
 230   if (!HAS_PENDING_EXCEPTION) {
 231     // Scavenge and allocate an instance.
 232     Handle holder(current, klass->klass_holder()); // keep the klass alive
 233     oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
 234     current->set_vm_result(result);
 235 
 236     // Pass oops back through thread local storage.  Our apparent type to Java
 237     // is that we return an oop, but we can block on exit from this routine and
 238     // a GC can trash the oop in C's return register.  The generated stub will
 239     // fetch the oop from TLS after any possible GC.
 240   }
 241 
 242   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 243   JRT_BLOCK_END;
 244 
 245   // inform GC that we won't do card marks for initializing writes.
 246   SharedRuntime::on_slowpath_allocation_exit(current);
 247 JRT_END
 248 
 249 
 250 // array allocation
 251 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
 252   JRT_BLOCK;
 253 #ifndef PRODUCT
 254   SharedRuntime::_new_array_ctr++;            // new array requires GC
 255 #endif
 256   assert(check_compiled_frame(current), "incorrect caller");
 257 
 258   // Scavenge and allocate an instance.
 259   oop result;
 260 
 261   if (array_type->is_typeArray_klass()) {
 262     // The oopFactory likes to work with the element type.
 263     // (We could bypass the oopFactory, since it doesn't add much value.)
 264     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 265     result = oopFactory::new_typeArray(elem_type, len, THREAD);
 266   } else {
 267     // Although the oopFactory likes to work with the elem_type,
 268     // the compiler prefers the array_type, since it must already have
 269     // that latter value in hand for the fast path.
 270     Handle holder(current, array_type->klass_holder()); // keep the array klass alive
 271     Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
 272     result = oopFactory::new_objArray(elem_type, len, THREAD);
 273   }
 274 
 275   // Pass oops back through thread local storage.  Our apparent type to Java
 276   // is that we return an oop, but we can block on exit from this routine and
 277   // a GC can trash the oop in C's return register.  The generated stub will
 278   // fetch the oop from TLS after any possible GC.
 279   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 280   current->set_vm_result(result);
 281   JRT_BLOCK_END;
 282 
 283   // inform GC that we won't do card marks for initializing writes.
 284   SharedRuntime::on_slowpath_allocation_exit(current);
 285 JRT_END
 286 
 287 // array allocation without zeroing
 288 JRT_BLOCK_ENTRY(void, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
 289   JRT_BLOCK;
 290 #ifndef PRODUCT
 291   SharedRuntime::_new_array_ctr++;            // new array requires GC
 292 #endif
 293   assert(check_compiled_frame(current), "incorrect caller");
 294 
 295   // Scavenge and allocate an instance.
 296   oop result;
 297 
 298   assert(array_type->is_typeArray_klass(), "should be called only for type array");
 299   // The oopFactory likes to work with the element type.
 300   BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 301   result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
 302 
 303   // Pass oops back through thread local storage.  Our apparent type to Java
 304   // is that we return an oop, but we can block on exit from this routine and
 305   // a GC can trash the oop in C's return register.  The generated stub will
 306   // fetch the oop from TLS after any possible GC.
 307   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 308   current->set_vm_result(result);

 320     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 321     size_t hs_bytes = arrayOopDesc::base_offset_in_bytes(elem_type);
 322     assert(is_aligned(hs_bytes, BytesPerInt), "must be 4 byte aligned");
 323     HeapWord* obj = cast_from_oop<HeapWord*>(result);
 324     if (!is_aligned(hs_bytes, BytesPerLong)) {
 325       *reinterpret_cast<jint*>(reinterpret_cast<char*>(obj) + hs_bytes) = 0;
 326       hs_bytes += BytesPerInt;
 327     }
 328 
 329     // Optimized zeroing.
 330     assert(is_aligned(hs_bytes, BytesPerLong), "must be 8-byte aligned");
 331     const size_t aligned_hs = hs_bytes / BytesPerLong;
 332     Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
 333   }
 334 
 335 JRT_END
 336 
 337 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
 338 
 339 // multianewarray for 2 dimensions
 340 JRT_ENTRY(void, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
 341 #ifndef PRODUCT
 342   SharedRuntime::_multi2_ctr++;                // multianewarray for 1 dimension
 343 #endif
 344   assert(check_compiled_frame(current), "incorrect caller");
 345   assert(elem_type->is_klass(), "not a class");
 346   jint dims[2];
 347   dims[0] = len1;
 348   dims[1] = len2;
 349   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 350   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
 351   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 352   current->set_vm_result(obj);
 353 JRT_END
 354 
 355 // multianewarray for 3 dimensions
 356 JRT_ENTRY(void, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
 357 #ifndef PRODUCT
 358   SharedRuntime::_multi3_ctr++;                // multianewarray for 1 dimension
 359 #endif
 360   assert(check_compiled_frame(current), "incorrect caller");
 361   assert(elem_type->is_klass(), "not a class");
 362   jint dims[3];
 363   dims[0] = len1;
 364   dims[1] = len2;
 365   dims[2] = len3;
 366   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 367   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
 368   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 369   current->set_vm_result(obj);
 370 JRT_END
 371 
 372 // multianewarray for 4 dimensions
 373 JRT_ENTRY(void, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
 374 #ifndef PRODUCT
 375   SharedRuntime::_multi4_ctr++;                // multianewarray for 1 dimension
 376 #endif
 377   assert(check_compiled_frame(current), "incorrect caller");
 378   assert(elem_type->is_klass(), "not a class");
 379   jint dims[4];
 380   dims[0] = len1;
 381   dims[1] = len2;
 382   dims[2] = len3;
 383   dims[3] = len4;
 384   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 385   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
 386   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 387   current->set_vm_result(obj);
 388 JRT_END
 389 
 390 // multianewarray for 5 dimensions
 391 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
 392 #ifndef PRODUCT
 393   SharedRuntime::_multi5_ctr++;                // multianewarray for 1 dimension
 394 #endif
 395   assert(check_compiled_frame(current), "incorrect caller");
 396   assert(elem_type->is_klass(), "not a class");
 397   jint dims[5];
 398   dims[0] = len1;
 399   dims[1] = len2;
 400   dims[2] = len3;
 401   dims[3] = len4;
 402   dims[4] = len5;
 403   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 404   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
 405   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 406   current->set_vm_result(obj);
 407 JRT_END
 408 
 409 JRT_ENTRY(void, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
 410   assert(check_compiled_frame(current), "incorrect caller");
 411   assert(elem_type->is_klass(), "not a class");
 412   assert(oop(dims)->is_typeArray(), "not an array");
 413 
 414   ResourceMark rm;
 415   jint len = dims->length();
 416   assert(len > 0, "Dimensions array should contain data");
 417   jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
 418   ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
 419                                        c_dims, len);
 420 
 421   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 422   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
 423   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 424   current->set_vm_result(obj);
 425 JRT_END
 426 
 427 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
 428 
 429   // Very few notify/notifyAll operations find any threads on the waitset, so
 430   // the dominant fast-path is to simply return.
 431   // Relatedly, it's critical that notify/notifyAll be fast in order to
 432   // reduce lock hold times.
 433   if (!SafepointSynchronize::is_synchronizing()) {
 434     if (ObjectSynchronizer::quick_notify(obj, current, false)) {
 435       return;
 436     }
 437   }
 438 
 439   // This is the case the fast-path above isn't provisioned to handle.
 440   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 441   // (The fast-path is just a degenerate variant of the slow-path).
 442   // Perform the dreaded state transition and pass control into the slow-path.
 443   JRT_BLOCK;
 444   Handle h_obj(current, obj);
 445   ObjectSynchronizer::notify(h_obj, CHECK);
 446   JRT_BLOCK_END;
 447 JRT_END
 448 
 449 JRT_BLOCK_ENTRY(void, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
 450 
 451   if (!SafepointSynchronize::is_synchronizing() ) {
 452     if (ObjectSynchronizer::quick_notify(obj, current, true)) {
 453       return;
 454     }
 455   }
 456 
 457   // This is the case the fast-path above isn't provisioned to handle.
 458   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 459   // (The fast-path is just a degenerate variant of the slow-path).
 460   // Perform the dreaded state transition and pass control into the slow-path.
 461   JRT_BLOCK;
 462   Handle h_obj(current, obj);
 463   ObjectSynchronizer::notifyall(h_obj, CHECK);
 464   JRT_BLOCK_END;
 465 JRT_END
 466 
 467 const TypeFunc *OptoRuntime::new_instance_Type() {
 468   // create input type (domain)
 469   const Type **fields = TypeTuple::fields(1);

1423   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1424   switch (register_save_policy[reg]) {
1425     case 'C': return false; //SOC
1426     case 'E': return true ; //SOE
1427     case 'N': return false; //NS
1428     case 'A': return false; //AS
1429   }
1430   ShouldNotReachHere();
1431   return false;
1432 }
1433 
1434 //-----------------------------------------------------------------------
1435 // Exceptions
1436 //
1437 
1438 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1439 
1440 // The method is an entry that is always called by a C++ method not
1441 // directly from compiled code. Compiled code will call the C++ method following.
1442 // We can't allow async exception to be installed during  exception processing.
1443 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1444   // The frame we rethrow the exception to might not have been processed by the GC yet.
1445   // The stack watermark barrier takes care of detecting that and ensuring the frame
1446   // has updated oops.
1447   StackWatermarkSet::after_unwind(current);
1448 
1449   // Do not confuse exception_oop with pending_exception. The exception_oop
1450   // is only used to pass arguments into the method. Not for general
1451   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1452   // the runtime stubs checks this on exit.
1453   assert(current->exception_oop() != nullptr, "exception oop is found");
1454   address handler_address = nullptr;
1455 
1456   Handle exception(current, current->exception_oop());
1457   address pc = current->exception_pc();
1458 
1459   // Clear out the exception oop and pc since looking up an
1460   // exception handler can cause class loading, which might throw an
1461   // exception and those fields are expected to be clear during
1462   // normal bytecode execution.
1463   current->clear_exception_oop_and_pc();

1699   return caller_frame.is_deoptimized_frame();
1700 }
1701 
1702 
1703 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1704   // create input type (domain)
1705   const Type **fields = TypeTuple::fields(1);
1706   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1707   // // The JavaThread* is passed to each routine as the last argument
1708   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1709   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1710 
1711   // create result type (range)
1712   fields = TypeTuple::fields(0);
1713 
1714   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1715 
1716   return TypeFunc::make(domain,range);
1717 }
1718 














1719 #if INCLUDE_JFR
1720 const TypeFunc *OptoRuntime::class_id_load_barrier_Type() {
1721   // create input type (domain)
1722   const Type **fields = TypeTuple::fields(1);
1723   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1724   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1725 
1726   // create result type (range)
1727   fields = TypeTuple::fields(0);
1728 
1729   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1730 
1731   return TypeFunc::make(domain,range);
1732 }
1733 #endif
1734 
1735 //-----------------------------------------------------------------------------
1736 // Dtrace support.  entry and exit probes have the same signature
1737 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1738   // create input type (domain)

1749   return TypeFunc::make(domain,range);
1750 }
1751 
1752 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1753   // create input type (domain)
1754   const Type **fields = TypeTuple::fields(2);
1755   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1756   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1757 
1758   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1759 
1760   // create result type (range)
1761   fields = TypeTuple::fields(0);
1762 
1763   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1764 
1765   return TypeFunc::make(domain,range);
1766 }
1767 
1768 
1769 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1770   assert(oopDesc::is_oop(obj), "must be a valid oop");
1771   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1772   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1773 JRT_END
1774 









1775 //-----------------------------------------------------------------------------
1776 
1777 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1778 
1779 //
1780 // dump the collected NamedCounters.
1781 //
1782 void OptoRuntime::print_named_counters() {
1783   int total_lock_count = 0;
1784   int eliminated_lock_count = 0;
1785 
1786   NamedCounter* c = _named_counters;
1787   while (c) {
1788     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1789       int count = c->count();
1790       if (count > 0) {
1791         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1792         if (Verbose) {
1793           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1794         }

1868 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1869   trace_exception_counter++;
1870   stringStream tempst;
1871 
1872   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1873   exception_oop->print_value_on(&tempst);
1874   tempst.print(" in ");
1875   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1876   if (blob->is_nmethod()) {
1877     blob->as_nmethod()->method()->print_value_on(&tempst);
1878   } else if (blob->is_runtime_stub()) {
1879     tempst.print("<runtime-stub>");
1880   } else {
1881     tempst.print("<unknown>");
1882   }
1883   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1884   tempst.print("]");
1885 
1886   st->print_raw_cr(tempst.freeze());
1887 }



































































  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 "precompiled.hpp"
  26 #include "classfile/vmClasses.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/compiledIC.hpp"
  30 #include "code/nmethod.hpp"
  31 #include "code/pcDesc.hpp"
  32 #include "code/scopeDesc.hpp"
  33 #include "code/vtableStubs.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/objArrayKlass.hpp"
  49 #include "oops/klass.inline.hpp"
  50 #include "oops/oop.inline.hpp"
  51 #include "oops/typeArrayOop.inline.hpp"
  52 #include "opto/ad.hpp"
  53 #include "opto/addnode.hpp"
  54 #include "opto/callnode.hpp"
  55 #include "opto/cfgnode.hpp"
  56 #include "opto/graphKit.hpp"
  57 #include "opto/machnode.hpp"
  58 #include "opto/matcher.hpp"
  59 #include "opto/memnode.hpp"
  60 #include "opto/mulnode.hpp"
  61 #include "opto/output.hpp"
  62 #include "opto/runtime.hpp"
  63 #include "opto/subnode.hpp"
  64 #include "prims/jvmtiExport.hpp"
  65 #include "runtime/atomic.hpp"
  66 #include "runtime/frame.inline.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/interfaceSupport.inline.hpp"
  69 #include "runtime/java.hpp"
  70 #include "runtime/javaCalls.hpp"
  71 #include "runtime/perfData.inline.hpp"
  72 #include "runtime/sharedRuntime.hpp"
  73 #include "runtime/signature.hpp"
  74 #include "runtime/stackWatermarkSet.hpp"
  75 #include "runtime/synchronizer.hpp"
  76 #include "runtime/threadCritical.hpp"
  77 #include "runtime/threadWXSetters.inline.hpp"
  78 #include "runtime/vframe.hpp"
  79 #include "runtime/vframeArray.hpp"
  80 #include "runtime/vframe_hp.hpp"
  81 #include "services/management.hpp"
  82 #include "utilities/copy.hpp"
  83 #include "utilities/preserveException.hpp"
  84 
  85 
  86 // For debugging purposes:
  87 //  To force FullGCALot inside a runtime function, add the following two lines
  88 //
  89 //  Universe::release_fullgc_alot_dummy();
  90 //  Universe::heap()->collect();
  91 //
  92 // At command line specify the parameters: -XX:+FullGCALot -XX:FullGCALotStart=100000000
  93 
  94 
  95 
  96 
  97 // Compiled code entry points
  98 address OptoRuntime::_new_instance_Java                           = nullptr;
  99 address OptoRuntime::_new_array_Java                              = nullptr;
 100 address OptoRuntime::_new_array_nozero_Java                       = nullptr;
 101 address OptoRuntime::_multianewarray2_Java                        = nullptr;
 102 address OptoRuntime::_multianewarray3_Java                        = nullptr;
 103 address OptoRuntime::_multianewarray4_Java                        = nullptr;
 104 address OptoRuntime::_multianewarray5_Java                        = nullptr;
 105 address OptoRuntime::_multianewarrayN_Java                        = nullptr;
 106 address OptoRuntime::_vtable_must_compile_Java                    = nullptr;
 107 address OptoRuntime::_complete_monitor_locking_Java               = nullptr;
 108 address OptoRuntime::_monitor_notify_Java                         = nullptr;
 109 address OptoRuntime::_monitor_notifyAll_Java                      = nullptr;
 110 address OptoRuntime::_rethrow_Java                                = nullptr;
 111 
 112 address OptoRuntime::_slow_arraycopy_Java                         = nullptr;
 113 address OptoRuntime::_register_finalizer_Java                     = nullptr;
 114 address OptoRuntime::_class_init_barrier_Java                     = nullptr;
 115 #if INCLUDE_JVMTI
 116 address OptoRuntime::_notify_jvmti_vthread_start                  = nullptr;
 117 address OptoRuntime::_notify_jvmti_vthread_end                    = nullptr;
 118 address OptoRuntime::_notify_jvmti_vthread_mount                  = nullptr;
 119 address OptoRuntime::_notify_jvmti_vthread_unmount                = nullptr;
 120 #endif
 121 
 122 ExceptionBlob* OptoRuntime::_exception_blob;
 123 
 124 PerfCounter* _perf_OptoRuntime_class_init_barrier_redundant_count = nullptr;
 125 
 126 // This should be called in an assertion at the start of OptoRuntime routines
 127 // which are entered from compiled code (all of them)
 128 #ifdef ASSERT
 129 static bool check_compiled_frame(JavaThread* thread) {
 130   assert(thread->last_frame().is_runtime_frame(), "cannot call runtime directly from compiled code");
 131   RegisterMap map(thread,
 132                   RegisterMap::UpdateMap::skip,
 133                   RegisterMap::ProcessFrames::include,
 134                   RegisterMap::WalkContinuation::skip);
 135   frame caller = thread->last_frame().sender(&map);
 136   assert(caller.is_compiled_frame(), "not being called from compiled like code");
 137   return true;
 138 }
 139 #endif // ASSERT
 140 
 141 
 142 #define gen(env, var, type_func_gen, c_func, fancy_jump, pass_tls, return_pc) \
 143   var = generate_stub(env, type_func_gen, CAST_FROM_FN_PTR(address, c_func), #var, fancy_jump, pass_tls, return_pc); \
 144   if (var == nullptr) { return false; }
 145 
 146 bool OptoRuntime::generate(ciEnv* env) {
 147   init_counters();
 148 
 149   generate_exception_blob();
 150 
 151   // Note: tls: Means fetching the return oop out of the thread-local storage
 152   //
 153   //   variable/name                       type-function-gen              , runtime method                  ,fncy_jp, tls,retpc
 154   // -------------------------------------------------------------------------------------------------------------------------------
 155   gen(env, _new_instance_Java              , new_instance_Type            , new_instance_C                  ,    0 , true, false);
 156   gen(env, _new_array_Java                 , new_array_Type               , new_array_C                     ,    0 , true, false);
 157   gen(env, _new_array_nozero_Java          , new_array_Type               , new_array_nozero_C              ,    0 , true, false);
 158   gen(env, _multianewarray2_Java           , multianewarray2_Type         , multianewarray2_C               ,    0 , true, false);
 159   gen(env, _multianewarray3_Java           , multianewarray3_Type         , multianewarray3_C               ,    0 , true, false);
 160   gen(env, _multianewarray4_Java           , multianewarray4_Type         , multianewarray4_C               ,    0 , true, false);
 161   gen(env, _multianewarray5_Java           , multianewarray5_Type         , multianewarray5_C               ,    0 , true, false);
 162   gen(env, _multianewarrayN_Java           , multianewarrayN_Type         , multianewarrayN_C               ,    0 , true, false);
 163 #if INCLUDE_JVMTI
 164   gen(env, _notify_jvmti_vthread_start     , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_start, 0, true, false);
 165   gen(env, _notify_jvmti_vthread_end       , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_end,   0, true, false);
 166   gen(env, _notify_jvmti_vthread_mount     , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_mount, 0, true, false);
 167   gen(env, _notify_jvmti_vthread_unmount   , notify_jvmti_vthread_Type    , SharedRuntime::notify_jvmti_vthread_unmount, 0, true, false);
 168 #endif
 169   gen(env, _complete_monitor_locking_Java  , complete_monitor_enter_Type  , SharedRuntime::complete_monitor_locking_C, 0, false, false);
 170   gen(env, _monitor_notify_Java            , monitor_notify_Type          , monitor_notify_C                ,    0 , false, false);
 171   gen(env, _monitor_notifyAll_Java         , monitor_notify_Type          , monitor_notifyAll_C             ,    0 , false, false);
 172   gen(env, _rethrow_Java                   , rethrow_Type                 , rethrow_C                       ,    2 , true , true );
 173 
 174   gen(env, _slow_arraycopy_Java            , slow_arraycopy_Type          , SharedRuntime::slow_arraycopy_C ,    0 , false, false);
 175   gen(env, _register_finalizer_Java        , register_finalizer_Type      , register_finalizer              ,    0 , false, false);
 176   gen(env, _class_init_barrier_Java        , class_init_barrier_Type      , class_init_barrier              ,    0 , false, false);
 177 
 178   return true;
 179 }
 180 
 181 #undef gen
 182 
 183 
 184 // Helper method to do generation of RunTimeStub's
 185 address OptoRuntime::generate_stub(ciEnv* env,
 186                                    TypeFunc_generator gen, address C_function,
 187                                    const char *name, int is_fancy_jump,
 188                                    bool pass_tls,
 189                                    bool return_pc) {
 190 
 191   // Matching the default directive, we currently have no method to match.
 192   DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompilerThread::current()->compiler());
 193   ResourceMark rm;
 194   Compile C(env, gen, C_function, name, is_fancy_jump, pass_tls, return_pc, directive);
 195   DirectivesStack::release(directive);
 196   return  C.stub_entry_point();
 197 }
 198 
 199 const char* OptoRuntime::stub_name(address entry) {
 200 #ifndef PRODUCT
 201   CodeBlob* cb = CodeCache::find_blob(entry);
 202   RuntimeStub* rs =(RuntimeStub *)cb;
 203   assert(rs != nullptr && rs->is_runtime_stub(), "not a runtime stub");
 204   return rs->name();
 205 #else
 206   // Fast implementation for product mode (maybe it should be inlined too)
 207   return "runtime stub";
 208 #endif
 209 }
 210 
 211 
 212 //=============================================================================
 213 // Opto compiler runtime routines
 214 //=============================================================================
 215 
 216 
 217 //=============================allocation======================================
 218 // We failed the fast-path allocation.  Now we need to do a scavenge or GC
 219 // and try allocation again.
 220 
 221 // object allocation
 222 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_instance_C, OptoRuntime::new_instance_C(Klass* klass, JavaThread* current))
 223   JRT_BLOCK;
 224 #ifndef PRODUCT
 225   SharedRuntime::_new_instance_ctr++;         // new instance requires GC
 226 #endif
 227   assert(check_compiled_frame(current), "incorrect caller");
 228 
 229   // These checks are cheap to make and support reflective allocation.
 230   int lh = klass->layout_helper();
 231   if (Klass::layout_helper_needs_slow_path(lh) || !InstanceKlass::cast(klass)->is_initialized()) {
 232     Handle holder(current, klass->klass_holder()); // keep the klass alive
 233     klass->check_valid_for_instantiation(false, THREAD);
 234     if (!HAS_PENDING_EXCEPTION) {
 235       InstanceKlass::cast(klass)->initialize(THREAD);
 236     }
 237   }
 238 
 239   if (!HAS_PENDING_EXCEPTION) {
 240     // Scavenge and allocate an instance.
 241     Handle holder(current, klass->klass_holder()); // keep the klass alive
 242     oop result = InstanceKlass::cast(klass)->allocate_instance(THREAD);
 243     current->set_vm_result(result);
 244 
 245     // Pass oops back through thread local storage.  Our apparent type to Java
 246     // is that we return an oop, but we can block on exit from this routine and
 247     // a GC can trash the oop in C's return register.  The generated stub will
 248     // fetch the oop from TLS after any possible GC.
 249   }
 250 
 251   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 252   JRT_BLOCK_END;
 253 
 254   // inform GC that we won't do card marks for initializing writes.
 255   SharedRuntime::on_slowpath_allocation_exit(current);
 256 JRT_END
 257 
 258 
 259 // array allocation
 260 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_C, OptoRuntime::new_array_C(Klass* array_type, int len, JavaThread* current))
 261   JRT_BLOCK;
 262 #ifndef PRODUCT
 263   SharedRuntime::_new_array_ctr++;            // new array requires GC
 264 #endif
 265   assert(check_compiled_frame(current), "incorrect caller");
 266 
 267   // Scavenge and allocate an instance.
 268   oop result;
 269 
 270   if (array_type->is_typeArray_klass()) {
 271     // The oopFactory likes to work with the element type.
 272     // (We could bypass the oopFactory, since it doesn't add much value.)
 273     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 274     result = oopFactory::new_typeArray(elem_type, len, THREAD);
 275   } else {
 276     // Although the oopFactory likes to work with the elem_type,
 277     // the compiler prefers the array_type, since it must already have
 278     // that latter value in hand for the fast path.
 279     Handle holder(current, array_type->klass_holder()); // keep the array klass alive
 280     Klass* elem_type = ObjArrayKlass::cast(array_type)->element_klass();
 281     result = oopFactory::new_objArray(elem_type, len, THREAD);
 282   }
 283 
 284   // Pass oops back through thread local storage.  Our apparent type to Java
 285   // is that we return an oop, but we can block on exit from this routine and
 286   // a GC can trash the oop in C's return register.  The generated stub will
 287   // fetch the oop from TLS after any possible GC.
 288   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 289   current->set_vm_result(result);
 290   JRT_BLOCK_END;
 291 
 292   // inform GC that we won't do card marks for initializing writes.
 293   SharedRuntime::on_slowpath_allocation_exit(current);
 294 JRT_END
 295 
 296 // array allocation without zeroing
 297 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, new_array_nozero_C, OptoRuntime::new_array_nozero_C(Klass* array_type, int len, JavaThread* current))
 298   JRT_BLOCK;
 299 #ifndef PRODUCT
 300   SharedRuntime::_new_array_ctr++;            // new array requires GC
 301 #endif
 302   assert(check_compiled_frame(current), "incorrect caller");
 303 
 304   // Scavenge and allocate an instance.
 305   oop result;
 306 
 307   assert(array_type->is_typeArray_klass(), "should be called only for type array");
 308   // The oopFactory likes to work with the element type.
 309   BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 310   result = oopFactory::new_typeArray_nozero(elem_type, len, THREAD);
 311 
 312   // Pass oops back through thread local storage.  Our apparent type to Java
 313   // is that we return an oop, but we can block on exit from this routine and
 314   // a GC can trash the oop in C's return register.  The generated stub will
 315   // fetch the oop from TLS after any possible GC.
 316   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 317   current->set_vm_result(result);

 329     BasicType elem_type = TypeArrayKlass::cast(array_type)->element_type();
 330     size_t hs_bytes = arrayOopDesc::base_offset_in_bytes(elem_type);
 331     assert(is_aligned(hs_bytes, BytesPerInt), "must be 4 byte aligned");
 332     HeapWord* obj = cast_from_oop<HeapWord*>(result);
 333     if (!is_aligned(hs_bytes, BytesPerLong)) {
 334       *reinterpret_cast<jint*>(reinterpret_cast<char*>(obj) + hs_bytes) = 0;
 335       hs_bytes += BytesPerInt;
 336     }
 337 
 338     // Optimized zeroing.
 339     assert(is_aligned(hs_bytes, BytesPerLong), "must be 8-byte aligned");
 340     const size_t aligned_hs = hs_bytes / BytesPerLong;
 341     Copy::fill_to_aligned_words(obj+aligned_hs, size-aligned_hs);
 342   }
 343 
 344 JRT_END
 345 
 346 // Note: multianewarray for one dimension is handled inline by GraphKit::new_array.
 347 
 348 // multianewarray for 2 dimensions
 349 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray2_C, OptoRuntime::multianewarray2_C(Klass* elem_type, int len1, int len2, JavaThread* current))
 350 #ifndef PRODUCT
 351   SharedRuntime::_multi2_ctr++;                // multianewarray for 1 dimension
 352 #endif
 353   assert(check_compiled_frame(current), "incorrect caller");
 354   assert(elem_type->is_klass(), "not a class");
 355   jint dims[2];
 356   dims[0] = len1;
 357   dims[1] = len2;
 358   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 359   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(2, dims, THREAD);
 360   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 361   current->set_vm_result(obj);
 362 JRT_END
 363 
 364 // multianewarray for 3 dimensions
 365 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray3_C, OptoRuntime::multianewarray3_C(Klass* elem_type, int len1, int len2, int len3, JavaThread* current))
 366 #ifndef PRODUCT
 367   SharedRuntime::_multi3_ctr++;                // multianewarray for 1 dimension
 368 #endif
 369   assert(check_compiled_frame(current), "incorrect caller");
 370   assert(elem_type->is_klass(), "not a class");
 371   jint dims[3];
 372   dims[0] = len1;
 373   dims[1] = len2;
 374   dims[2] = len3;
 375   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 376   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(3, dims, THREAD);
 377   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 378   current->set_vm_result(obj);
 379 JRT_END
 380 
 381 // multianewarray for 4 dimensions
 382 JRT_ENTRY_PROF(void, OptoRuntime, multianewarray4_C, OptoRuntime::multianewarray4_C(Klass* elem_type, int len1, int len2, int len3, int len4, JavaThread* current))
 383 #ifndef PRODUCT
 384   SharedRuntime::_multi4_ctr++;                // multianewarray for 1 dimension
 385 #endif
 386   assert(check_compiled_frame(current), "incorrect caller");
 387   assert(elem_type->is_klass(), "not a class");
 388   jint dims[4];
 389   dims[0] = len1;
 390   dims[1] = len2;
 391   dims[2] = len3;
 392   dims[3] = len4;
 393   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 394   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(4, dims, THREAD);
 395   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 396   current->set_vm_result(obj);
 397 JRT_END
 398 
 399 // multianewarray for 5 dimensions
 400 JRT_ENTRY(void, OptoRuntime::multianewarray5_C(Klass* elem_type, int len1, int len2, int len3, int len4, int len5, JavaThread* current))
 401 #ifndef PRODUCT
 402   SharedRuntime::_multi5_ctr++;                // multianewarray for 1 dimension
 403 #endif
 404   assert(check_compiled_frame(current), "incorrect caller");
 405   assert(elem_type->is_klass(), "not a class");
 406   jint dims[5];
 407   dims[0] = len1;
 408   dims[1] = len2;
 409   dims[2] = len3;
 410   dims[3] = len4;
 411   dims[4] = len5;
 412   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 413   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(5, dims, THREAD);
 414   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 415   current->set_vm_result(obj);
 416 JRT_END
 417 
 418 JRT_ENTRY_PROF(void, OptoRuntime, multianewarrayN_C, OptoRuntime::multianewarrayN_C(Klass* elem_type, arrayOopDesc* dims, JavaThread* current))
 419   assert(check_compiled_frame(current), "incorrect caller");
 420   assert(elem_type->is_klass(), "not a class");
 421   assert(oop(dims)->is_typeArray(), "not an array");
 422 
 423   ResourceMark rm;
 424   jint len = dims->length();
 425   assert(len > 0, "Dimensions array should contain data");
 426   jint *c_dims = NEW_RESOURCE_ARRAY(jint, len);
 427   ArrayAccess<>::arraycopy_to_native<>(dims, typeArrayOopDesc::element_offset<jint>(0),
 428                                        c_dims, len);
 429 
 430   Handle holder(current, elem_type->klass_holder()); // keep the klass alive
 431   oop obj = ArrayKlass::cast(elem_type)->multi_allocate(len, c_dims, THREAD);
 432   deoptimize_caller_frame(current, HAS_PENDING_EXCEPTION);
 433   current->set_vm_result(obj);
 434 JRT_END
 435 
 436 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notify_C, OptoRuntime::monitor_notify_C(oopDesc* obj, JavaThread* current))
 437 
 438   // Very few notify/notifyAll operations find any threads on the waitset, so
 439   // the dominant fast-path is to simply return.
 440   // Relatedly, it's critical that notify/notifyAll be fast in order to
 441   // reduce lock hold times.
 442   if (!SafepointSynchronize::is_synchronizing()) {
 443     if (ObjectSynchronizer::quick_notify(obj, current, false)) {
 444       return;
 445     }
 446   }
 447 
 448   // This is the case the fast-path above isn't provisioned to handle.
 449   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 450   // (The fast-path is just a degenerate variant of the slow-path).
 451   // Perform the dreaded state transition and pass control into the slow-path.
 452   JRT_BLOCK;
 453   Handle h_obj(current, obj);
 454   ObjectSynchronizer::notify(h_obj, CHECK);
 455   JRT_BLOCK_END;
 456 JRT_END
 457 
 458 JRT_BLOCK_ENTRY_PROF(void, OptoRuntime, monitor_notifyAll_C, OptoRuntime::monitor_notifyAll_C(oopDesc* obj, JavaThread* current))
 459 
 460   if (!SafepointSynchronize::is_synchronizing() ) {
 461     if (ObjectSynchronizer::quick_notify(obj, current, true)) {
 462       return;
 463     }
 464   }
 465 
 466   // This is the case the fast-path above isn't provisioned to handle.
 467   // The fast-path is designed to handle frequently arising cases in an efficient manner.
 468   // (The fast-path is just a degenerate variant of the slow-path).
 469   // Perform the dreaded state transition and pass control into the slow-path.
 470   JRT_BLOCK;
 471   Handle h_obj(current, obj);
 472   ObjectSynchronizer::notifyall(h_obj, CHECK);
 473   JRT_BLOCK_END;
 474 JRT_END
 475 
 476 const TypeFunc *OptoRuntime::new_instance_Type() {
 477   // create input type (domain)
 478   const Type **fields = TypeTuple::fields(1);

1432   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1433   switch (register_save_policy[reg]) {
1434     case 'C': return false; //SOC
1435     case 'E': return true ; //SOE
1436     case 'N': return false; //NS
1437     case 'A': return false; //AS
1438   }
1439   ShouldNotReachHere();
1440   return false;
1441 }
1442 
1443 //-----------------------------------------------------------------------
1444 // Exceptions
1445 //
1446 
1447 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1448 
1449 // The method is an entry that is always called by a C++ method not
1450 // directly from compiled code. Compiled code will call the C++ method following.
1451 // We can't allow async exception to be installed during  exception processing.
1452 JRT_ENTRY_NO_ASYNC_PROF(address, OptoRuntime, handle_exception_C_helper, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1453   // The frame we rethrow the exception to might not have been processed by the GC yet.
1454   // The stack watermark barrier takes care of detecting that and ensuring the frame
1455   // has updated oops.
1456   StackWatermarkSet::after_unwind(current);
1457 
1458   // Do not confuse exception_oop with pending_exception. The exception_oop
1459   // is only used to pass arguments into the method. Not for general
1460   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1461   // the runtime stubs checks this on exit.
1462   assert(current->exception_oop() != nullptr, "exception oop is found");
1463   address handler_address = nullptr;
1464 
1465   Handle exception(current, current->exception_oop());
1466   address pc = current->exception_pc();
1467 
1468   // Clear out the exception oop and pc since looking up an
1469   // exception handler can cause class loading, which might throw an
1470   // exception and those fields are expected to be clear during
1471   // normal bytecode execution.
1472   current->clear_exception_oop_and_pc();

1708   return caller_frame.is_deoptimized_frame();
1709 }
1710 
1711 
1712 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1713   // create input type (domain)
1714   const Type **fields = TypeTuple::fields(1);
1715   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1716   // // The JavaThread* is passed to each routine as the last argument
1717   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1718   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1719 
1720   // create result type (range)
1721   fields = TypeTuple::fields(0);
1722 
1723   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1724 
1725   return TypeFunc::make(domain,range);
1726 }
1727 
1728 const TypeFunc *OptoRuntime::class_init_barrier_Type() {
1729   // create input type (domain)
1730   const Type** fields = TypeTuple::fields(1);
1731   fields[TypeFunc::Parms+0] = TypeKlassPtr::NOTNULL;
1732   // // The JavaThread* is passed to each routine as the last argument
1733   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1734   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1735 
1736   // create result type (range)
1737   fields = TypeTuple::fields(0);
1738   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
1739   return TypeFunc::make(domain,range);
1740 }
1741 
1742 #if INCLUDE_JFR
1743 const TypeFunc *OptoRuntime::class_id_load_barrier_Type() {
1744   // create input type (domain)
1745   const Type **fields = TypeTuple::fields(1);
1746   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1747   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1748 
1749   // create result type (range)
1750   fields = TypeTuple::fields(0);
1751 
1752   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1753 
1754   return TypeFunc::make(domain,range);
1755 }
1756 #endif
1757 
1758 //-----------------------------------------------------------------------------
1759 // Dtrace support.  entry and exit probes have the same signature
1760 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1761   // create input type (domain)

1772   return TypeFunc::make(domain,range);
1773 }
1774 
1775 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1776   // create input type (domain)
1777   const Type **fields = TypeTuple::fields(2);
1778   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1779   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1780 
1781   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1782 
1783   // create result type (range)
1784   fields = TypeTuple::fields(0);
1785 
1786   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1787 
1788   return TypeFunc::make(domain,range);
1789 }
1790 
1791 
1792 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, register_finalizer, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1793   assert(oopDesc::is_oop(obj), "must be a valid oop");
1794   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1795   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1796 JRT_END
1797 
1798 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, class_init_barrier, OptoRuntime::class_init_barrier(Klass* k, JavaThread* current))
1799   InstanceKlass* ik = InstanceKlass::cast(k);
1800   if (ik->should_be_initialized()) {
1801     ik->initialize(CHECK);
1802   } else if (UsePerfData) {
1803     _perf_OptoRuntime_class_init_barrier_redundant_count->inc();
1804   }
1805 JRT_END
1806 
1807 //-----------------------------------------------------------------------------
1808 
1809 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1810 
1811 //
1812 // dump the collected NamedCounters.
1813 //
1814 void OptoRuntime::print_named_counters() {
1815   int total_lock_count = 0;
1816   int eliminated_lock_count = 0;
1817 
1818   NamedCounter* c = _named_counters;
1819   while (c) {
1820     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1821       int count = c->count();
1822       if (count > 0) {
1823         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1824         if (Verbose) {
1825           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1826         }

1900 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1901   trace_exception_counter++;
1902   stringStream tempst;
1903 
1904   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1905   exception_oop->print_value_on(&tempst);
1906   tempst.print(" in ");
1907   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1908   if (blob->is_nmethod()) {
1909     blob->as_nmethod()->method()->print_value_on(&tempst);
1910   } else if (blob->is_runtime_stub()) {
1911     tempst.print("<runtime-stub>");
1912   } else {
1913     tempst.print("<unknown>");
1914   }
1915   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1916   tempst.print("]");
1917 
1918   st->print_raw_cr(tempst.freeze());
1919 }
1920 
1921 #define DO_COUNTERS2(macro2, macro1) \
1922   macro2(OptoRuntime, new_instance_C) \
1923   macro2(OptoRuntime, new_array_C) \
1924   macro2(OptoRuntime, new_array_nozero_C) \
1925   macro2(OptoRuntime, multianewarray2_C) \
1926   macro2(OptoRuntime, multianewarray3_C) \
1927   macro2(OptoRuntime, multianewarray4_C) \
1928   macro2(OptoRuntime, multianewarrayN_C) \
1929   macro2(OptoRuntime, monitor_notify_C) \
1930   macro2(OptoRuntime, monitor_notifyAll_C) \
1931   macro2(OptoRuntime, handle_exception_C_helper) \
1932   macro2(OptoRuntime, register_finalizer) \
1933   macro2(OptoRuntime, class_init_barrier) \
1934   macro1(OptoRuntime, class_init_barrier_redundant)
1935 
1936 #define INIT_COUNTER_TIME_AND_CNT(sub, name) \
1937   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
1938   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1939 
1940 #define INIT_COUNTER_CNT(sub, name) \
1941   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1942 
1943 void OptoRuntime::init_counters() {
1944   assert(CompilerConfig::is_c2_enabled(), "");
1945 
1946   if (UsePerfData) {
1947     EXCEPTION_MARK;
1948 
1949     DO_COUNTERS2(INIT_COUNTER_TIME_AND_CNT, INIT_COUNTER_CNT)
1950 
1951     if (HAS_PENDING_EXCEPTION) {
1952       vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
1953     }
1954   }
1955 }
1956 #undef INIT_COUNTER_TIME_AND_CNT
1957 #undef INIT_COUNTER_CNT
1958 
1959 #define PRINT_COUNTER_TIME_AND_CNT(sub, name) { \
1960   jlong count = _perf_##sub##_##name##_count->get_value(); \
1961   if (count > 0) { \
1962     st->print_cr("  %-30s = %4ldms (elapsed) %4ldms (thread) (%5ld events)", #sub "::" #name, \
1963                  _perf_##sub##_##name##_timer->elapsed_counter_value_ms(), \
1964                  _perf_##sub##_##name##_timer->thread_counter_value_ms(), \
1965                  count); \
1966   }}
1967 
1968 #define PRINT_COUNTER_CNT(sub, name) { \
1969   jlong count = _perf_##sub##_##name##_count->get_value(); \
1970   if (count > 0) { \
1971     st->print_cr("  %-30s = %5ld events", #name, count); \
1972   }}
1973 
1974 void OptoRuntime::print_counters_on(outputStream* st) {
1975   if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c2_enabled()) {
1976     DO_COUNTERS2(PRINT_COUNTER_TIME_AND_CNT, PRINT_COUNTER_CNT)
1977   } else {
1978     st->print_cr("  OptoRuntime: no info (%s is disabled)",
1979                  (!CompilerConfig::is_c2_enabled() ? "C2" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
1980   }
1981 }
1982 
1983 #undef PRINT_COUNTER_TIME_AND_CNT
1984 #undef PRINT_COUNTER_CNT
1985 #undef DO_COUNTERS2
< prev index next >