< 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);

1482   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1483   switch (register_save_policy[reg]) {
1484     case 'C': return false; //SOC
1485     case 'E': return true ; //SOE
1486     case 'N': return false; //NS
1487     case 'A': return false; //AS
1488   }
1489   ShouldNotReachHere();
1490   return false;
1491 }
1492 
1493 //-----------------------------------------------------------------------
1494 // Exceptions
1495 //
1496 
1497 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1498 
1499 // The method is an entry that is always called by a C++ method not
1500 // directly from compiled code. Compiled code will call the C++ method following.
1501 // We can't allow async exception to be installed during  exception processing.
1502 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1503   // The frame we rethrow the exception to might not have been processed by the GC yet.
1504   // The stack watermark barrier takes care of detecting that and ensuring the frame
1505   // has updated oops.
1506   StackWatermarkSet::after_unwind(current);
1507 
1508   // Do not confuse exception_oop with pending_exception. The exception_oop
1509   // is only used to pass arguments into the method. Not for general
1510   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1511   // the runtime stubs checks this on exit.
1512   assert(current->exception_oop() != nullptr, "exception oop is found");
1513   address handler_address = nullptr;
1514 
1515   Handle exception(current, current->exception_oop());
1516   address pc = current->exception_pc();
1517 
1518   // Clear out the exception oop and pc since looking up an
1519   // exception handler can cause class loading, which might throw an
1520   // exception and those fields are expected to be clear during
1521   // normal bytecode execution.
1522   current->clear_exception_oop_and_pc();

1758   return caller_frame.is_deoptimized_frame();
1759 }
1760 
1761 
1762 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1763   // create input type (domain)
1764   const Type **fields = TypeTuple::fields(1);
1765   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1766   // // The JavaThread* is passed to each routine as the last argument
1767   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1768   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1769 
1770   // create result type (range)
1771   fields = TypeTuple::fields(0);
1772 
1773   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1774 
1775   return TypeFunc::make(domain,range);
1776 }
1777 














1778 #if INCLUDE_JFR
1779 const TypeFunc *OptoRuntime::class_id_load_barrier_Type() {
1780   // create input type (domain)
1781   const Type **fields = TypeTuple::fields(1);
1782   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1783   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1784 
1785   // create result type (range)
1786   fields = TypeTuple::fields(0);
1787 
1788   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1789 
1790   return TypeFunc::make(domain,range);
1791 }
1792 #endif
1793 
1794 //-----------------------------------------------------------------------------
1795 // Dtrace support.  entry and exit probes have the same signature
1796 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1797   // create input type (domain)

1808   return TypeFunc::make(domain,range);
1809 }
1810 
1811 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1812   // create input type (domain)
1813   const Type **fields = TypeTuple::fields(2);
1814   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1815   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1816 
1817   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1818 
1819   // create result type (range)
1820   fields = TypeTuple::fields(0);
1821 
1822   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1823 
1824   return TypeFunc::make(domain,range);
1825 }
1826 
1827 
1828 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1829   assert(oopDesc::is_oop(obj), "must be a valid oop");
1830   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1831   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1832 JRT_END
1833 









1834 //-----------------------------------------------------------------------------
1835 
1836 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1837 
1838 //
1839 // dump the collected NamedCounters.
1840 //
1841 void OptoRuntime::print_named_counters() {
1842   int total_lock_count = 0;
1843   int eliminated_lock_count = 0;
1844 
1845   NamedCounter* c = _named_counters;
1846   while (c) {
1847     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1848       int count = c->count();
1849       if (count > 0) {
1850         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1851         if (Verbose) {
1852           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1853         }

1914 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1915   trace_exception_counter++;
1916   stringStream tempst;
1917 
1918   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1919   exception_oop->print_value_on(&tempst);
1920   tempst.print(" in ");
1921   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1922   if (blob->is_nmethod()) {
1923     blob->as_nmethod()->method()->print_value_on(&tempst);
1924   } else if (blob->is_runtime_stub()) {
1925     tempst.print("<runtime-stub>");
1926   } else {
1927     tempst.print("<unknown>");
1928   }
1929   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1930   tempst.print("]");
1931 
1932   st->print_raw_cr(tempst.freeze());
1933 }



































































  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);

1491   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1492   switch (register_save_policy[reg]) {
1493     case 'C': return false; //SOC
1494     case 'E': return true ; //SOE
1495     case 'N': return false; //NS
1496     case 'A': return false; //AS
1497   }
1498   ShouldNotReachHere();
1499   return false;
1500 }
1501 
1502 //-----------------------------------------------------------------------
1503 // Exceptions
1504 //
1505 
1506 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1507 
1508 // The method is an entry that is always called by a C++ method not
1509 // directly from compiled code. Compiled code will call the C++ method following.
1510 // We can't allow async exception to be installed during  exception processing.
1511 JRT_ENTRY_NO_ASYNC_PROF(address, OptoRuntime, handle_exception_C_helper, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1512   // The frame we rethrow the exception to might not have been processed by the GC yet.
1513   // The stack watermark barrier takes care of detecting that and ensuring the frame
1514   // has updated oops.
1515   StackWatermarkSet::after_unwind(current);
1516 
1517   // Do not confuse exception_oop with pending_exception. The exception_oop
1518   // is only used to pass arguments into the method. Not for general
1519   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1520   // the runtime stubs checks this on exit.
1521   assert(current->exception_oop() != nullptr, "exception oop is found");
1522   address handler_address = nullptr;
1523 
1524   Handle exception(current, current->exception_oop());
1525   address pc = current->exception_pc();
1526 
1527   // Clear out the exception oop and pc since looking up an
1528   // exception handler can cause class loading, which might throw an
1529   // exception and those fields are expected to be clear during
1530   // normal bytecode execution.
1531   current->clear_exception_oop_and_pc();

1767   return caller_frame.is_deoptimized_frame();
1768 }
1769 
1770 
1771 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1772   // create input type (domain)
1773   const Type **fields = TypeTuple::fields(1);
1774   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1775   // // The JavaThread* is passed to each routine as the last argument
1776   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1777   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1778 
1779   // create result type (range)
1780   fields = TypeTuple::fields(0);
1781 
1782   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1783 
1784   return TypeFunc::make(domain,range);
1785 }
1786 
1787 const TypeFunc *OptoRuntime::class_init_barrier_Type() {
1788   // create input type (domain)
1789   const Type** fields = TypeTuple::fields(1);
1790   fields[TypeFunc::Parms+0] = TypeKlassPtr::NOTNULL;
1791   // // The JavaThread* is passed to each routine as the last argument
1792   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1793   const TypeTuple* domain = TypeTuple::make(TypeFunc::Parms+1, fields);
1794 
1795   // create result type (range)
1796   fields = TypeTuple::fields(0);
1797   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
1798   return TypeFunc::make(domain,range);
1799 }
1800 
1801 #if INCLUDE_JFR
1802 const TypeFunc *OptoRuntime::class_id_load_barrier_Type() {
1803   // create input type (domain)
1804   const Type **fields = TypeTuple::fields(1);
1805   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1806   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1807 
1808   // create result type (range)
1809   fields = TypeTuple::fields(0);
1810 
1811   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1812 
1813   return TypeFunc::make(domain,range);
1814 }
1815 #endif
1816 
1817 //-----------------------------------------------------------------------------
1818 // Dtrace support.  entry and exit probes have the same signature
1819 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1820   // create input type (domain)

1831   return TypeFunc::make(domain,range);
1832 }
1833 
1834 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1835   // create input type (domain)
1836   const Type **fields = TypeTuple::fields(2);
1837   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1838   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1839 
1840   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1841 
1842   // create result type (range)
1843   fields = TypeTuple::fields(0);
1844 
1845   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1846 
1847   return TypeFunc::make(domain,range);
1848 }
1849 
1850 
1851 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, register_finalizer, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1852   assert(oopDesc::is_oop(obj), "must be a valid oop");
1853   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1854   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1855 JRT_END
1856 
1857 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, class_init_barrier, OptoRuntime::class_init_barrier(Klass* k, JavaThread* current))
1858   InstanceKlass* ik = InstanceKlass::cast(k);
1859   if (ik->should_be_initialized()) {
1860     ik->initialize(CHECK);
1861   } else if (UsePerfData) {
1862     _perf_OptoRuntime_class_init_barrier_redundant_count->inc();
1863   }
1864 JRT_END
1865 
1866 //-----------------------------------------------------------------------------
1867 
1868 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1869 
1870 //
1871 // dump the collected NamedCounters.
1872 //
1873 void OptoRuntime::print_named_counters() {
1874   int total_lock_count = 0;
1875   int eliminated_lock_count = 0;
1876 
1877   NamedCounter* c = _named_counters;
1878   while (c) {
1879     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1880       int count = c->count();
1881       if (count > 0) {
1882         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1883         if (Verbose) {
1884           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1885         }

1946 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg) {
1947   trace_exception_counter++;
1948   stringStream tempst;
1949 
1950   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1951   exception_oop->print_value_on(&tempst);
1952   tempst.print(" in ");
1953   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1954   if (blob->is_nmethod()) {
1955     blob->as_nmethod()->method()->print_value_on(&tempst);
1956   } else if (blob->is_runtime_stub()) {
1957     tempst.print("<runtime-stub>");
1958   } else {
1959     tempst.print("<unknown>");
1960   }
1961   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1962   tempst.print("]");
1963 
1964   st->print_raw_cr(tempst.freeze());
1965 }
1966 
1967 #define DO_COUNTERS2(macro2, macro1) \
1968   macro2(OptoRuntime, new_instance_C) \
1969   macro2(OptoRuntime, new_array_C) \
1970   macro2(OptoRuntime, new_array_nozero_C) \
1971   macro2(OptoRuntime, multianewarray2_C) \
1972   macro2(OptoRuntime, multianewarray3_C) \
1973   macro2(OptoRuntime, multianewarray4_C) \
1974   macro2(OptoRuntime, multianewarrayN_C) \
1975   macro2(OptoRuntime, monitor_notify_C) \
1976   macro2(OptoRuntime, monitor_notifyAll_C) \
1977   macro2(OptoRuntime, handle_exception_C_helper) \
1978   macro2(OptoRuntime, register_finalizer) \
1979   macro2(OptoRuntime, class_init_barrier) \
1980   macro1(OptoRuntime, class_init_barrier_redundant)
1981 
1982 #define INIT_COUNTER_TIME_AND_CNT(sub, name) \
1983   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
1984   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1985 
1986 #define INIT_COUNTER_CNT(sub, name) \
1987   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1988 
1989 void OptoRuntime::init_counters() {
1990   assert(CompilerConfig::is_c2_enabled(), "");
1991 
1992   if (UsePerfData) {
1993     EXCEPTION_MARK;
1994 
1995     DO_COUNTERS2(INIT_COUNTER_TIME_AND_CNT, INIT_COUNTER_CNT)
1996 
1997     if (HAS_PENDING_EXCEPTION) {
1998       vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
1999     }
2000   }
2001 }
2002 #undef INIT_COUNTER_TIME_AND_CNT
2003 #undef INIT_COUNTER_CNT
2004 
2005 #define PRINT_COUNTER_TIME_AND_CNT(sub, name) { \
2006   jlong count = _perf_##sub##_##name##_count->get_value(); \
2007   if (count > 0) { \
2008     st->print_cr("  %-30s = %4ldms (elapsed) %4ldms (thread) (%5ld events)", #sub "::" #name, \
2009                  _perf_##sub##_##name##_timer->elapsed_counter_value_ms(), \
2010                  _perf_##sub##_##name##_timer->thread_counter_value_ms(), \
2011                  count); \
2012   }}
2013 
2014 #define PRINT_COUNTER_CNT(sub, name) { \
2015   jlong count = _perf_##sub##_##name##_count->get_value(); \
2016   if (count > 0) { \
2017     st->print_cr("  %-30s = %5ld events", #name, count); \
2018   }}
2019 
2020 void OptoRuntime::print_counters_on(outputStream* st) {
2021   if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c2_enabled()) {
2022     DO_COUNTERS2(PRINT_COUNTER_TIME_AND_CNT, PRINT_COUNTER_CNT)
2023   } else {
2024     st->print_cr("  OptoRuntime: no info (%s is disabled)",
2025                  (!CompilerConfig::is_c2_enabled() ? "C2" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
2026   }
2027 }
2028 
2029 #undef PRINT_COUNTER_TIME_AND_CNT
2030 #undef PRINT_COUNTER_CNT
2031 #undef DO_COUNTERS2
< prev index next >