< prev index next >

src/hotspot/share/opto/runtime.cpp

Print this page

   1 /*
   2  * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmClasses.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/compiledMethod.inline.hpp"
  30 #include "code/compiledIC.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "code/pcDesc.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #include "code/vtableStubs.hpp"
  35 #include "compiler/compileBroker.hpp"

  36 #include "compiler/oopMap.hpp"
  37 #include "gc/g1/heapRegion.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/javaCalls.hpp"

  70 #include "runtime/sharedRuntime.hpp"
  71 #include "runtime/signature.hpp"
  72 #include "runtime/stackWatermarkSet.hpp"
  73 #include "runtime/synchronizer.hpp"
  74 #include "runtime/threadCritical.hpp"
  75 #include "runtime/threadWXSetters.inline.hpp"
  76 #include "runtime/vframe.hpp"
  77 #include "runtime/vframeArray.hpp"
  78 #include "runtime/vframe_hp.hpp"

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

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


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

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

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

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

1398   assert(reg >= 0 && reg < _last_Mach_Reg, "must be a machine register");
1399   switch (register_save_policy[reg]) {
1400     case 'C': return false; //SOC
1401     case 'E': return true ; //SOE
1402     case 'N': return false; //NS
1403     case 'A': return false; //AS
1404   }
1405   ShouldNotReachHere();
1406   return false;
1407 }
1408 
1409 //-----------------------------------------------------------------------
1410 // Exceptions
1411 //
1412 
1413 static void trace_exception(outputStream* st, oop exception_oop, address exception_pc, const char* msg);
1414 
1415 // The method is an entry that is always called by a C++ method not
1416 // directly from compiled code. Compiled code will call the C++ method following.
1417 // We can't allow async exception to be installed during  exception processing.
1418 JRT_ENTRY_NO_ASYNC(address, OptoRuntime::handle_exception_C_helper(JavaThread* current, nmethod* &nm))
1419   // The frame we rethrow the exception to might not have been processed by the GC yet.
1420   // The stack watermark barrier takes care of detecting that and ensuring the frame
1421   // has updated oops.
1422   StackWatermarkSet::after_unwind(current);
1423 
1424   // Do not confuse exception_oop with pending_exception. The exception_oop
1425   // is only used to pass arguments into the method. Not for general
1426   // exception handling.  DO NOT CHANGE IT to use pending_exception, since
1427   // the runtime stubs checks this on exit.
1428   assert(current->exception_oop() != nullptr, "exception oop is found");
1429   address handler_address = nullptr;
1430 
1431   Handle exception(current, current->exception_oop());
1432   address pc = current->exception_pc();
1433 
1434   // Clear out the exception oop and pc since looking up an
1435   // exception handler can cause class loading, which might throw an
1436   // exception and those fields are expected to be clear during
1437   // normal bytecode execution.
1438   current->clear_exception_oop_and_pc();

1674   return caller_frame.is_deoptimized_frame();
1675 }
1676 
1677 
1678 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1679   // create input type (domain)
1680   const Type **fields = TypeTuple::fields(1);
1681   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1682   // // The JavaThread* is passed to each routine as the last argument
1683   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1684   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1685 
1686   // create result type (range)
1687   fields = TypeTuple::fields(0);
1688 
1689   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1690 
1691   return TypeFunc::make(domain,range);
1692 }
1693 














1694 #if INCLUDE_JFR
1695 const TypeFunc *OptoRuntime::class_id_load_barrier_Type() {
1696   // create input type (domain)
1697   const Type **fields = TypeTuple::fields(1);
1698   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1699   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1700 
1701   // create result type (range)
1702   fields = TypeTuple::fields(0);
1703 
1704   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1705 
1706   return TypeFunc::make(domain,range);
1707 }
1708 #endif
1709 
1710 //-----------------------------------------------------------------------------
1711 // Dtrace support.  entry and exit probes have the same signature
1712 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1713   // create input type (domain)

1724   return TypeFunc::make(domain,range);
1725 }
1726 
1727 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1728   // create input type (domain)
1729   const Type **fields = TypeTuple::fields(2);
1730   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1731   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1732 
1733   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1734 
1735   // create result type (range)
1736   fields = TypeTuple::fields(0);
1737 
1738   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1739 
1740   return TypeFunc::make(domain,range);
1741 }
1742 
1743 
1744 JRT_ENTRY_NO_ASYNC(void, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1745   assert(oopDesc::is_oop(obj), "must be a valid oop");
1746   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1747   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1748 JRT_END
1749 









1750 //-----------------------------------------------------------------------------
1751 
1752 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1753 
1754 //
1755 // dump the collected NamedCounters.
1756 //
1757 void OptoRuntime::print_named_counters() {
1758   int total_lock_count = 0;
1759   int eliminated_lock_count = 0;
1760 
1761   NamedCounter* c = _named_counters;
1762   while (c) {
1763     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1764       int count = c->count();
1765       if (count > 0) {
1766         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1767         if (Verbose) {
1768           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1769         }

1844   trace_exception_counter++;
1845   stringStream tempst;
1846 
1847   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1848   exception_oop->print_value_on(&tempst);
1849   tempst.print(" in ");
1850   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1851   if (blob->is_compiled()) {
1852     CompiledMethod* cm = blob->as_compiled_method_or_null();
1853     cm->method()->print_value_on(&tempst);
1854   } else if (blob->is_runtime_stub()) {
1855     tempst.print("<runtime-stub>");
1856   } else {
1857     tempst.print("<unknown>");
1858   }
1859   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1860   tempst.print("]");
1861 
1862   st->print_raw_cr(tempst.freeze());
1863 }



































































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

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

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

1683   return caller_frame.is_deoptimized_frame();
1684 }
1685 
1686 
1687 const TypeFunc *OptoRuntime::register_finalizer_Type() {
1688   // create input type (domain)
1689   const Type **fields = TypeTuple::fields(1);
1690   fields[TypeFunc::Parms+0] = TypeInstPtr::NOTNULL;  // oop;          Receiver
1691   // // The JavaThread* is passed to each routine as the last argument
1692   // fields[TypeFunc::Parms+1] = TypeRawPtr::NOTNULL;  // JavaThread *; Executing thread
1693   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+1,fields);
1694 
1695   // create result type (range)
1696   fields = TypeTuple::fields(0);
1697 
1698   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1699 
1700   return TypeFunc::make(domain,range);
1701 }
1702 
1703 const TypeFunc *OptoRuntime::class_init_barrier_Type() {
1704   // create input type (domain)
1705   const Type** fields = TypeTuple::fields(1);
1706   fields[TypeFunc::Parms+0] = TypeKlassPtr::NOTNULL;
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   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
1714   return TypeFunc::make(domain,range);
1715 }
1716 
1717 #if INCLUDE_JFR
1718 const TypeFunc *OptoRuntime::class_id_load_barrier_Type() {
1719   // create input type (domain)
1720   const Type **fields = TypeTuple::fields(1);
1721   fields[TypeFunc::Parms+0] = TypeInstPtr::KLASS;
1722   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms + 1, fields);
1723 
1724   // create result type (range)
1725   fields = TypeTuple::fields(0);
1726 
1727   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms + 0, fields);
1728 
1729   return TypeFunc::make(domain,range);
1730 }
1731 #endif
1732 
1733 //-----------------------------------------------------------------------------
1734 // Dtrace support.  entry and exit probes have the same signature
1735 const TypeFunc *OptoRuntime::dtrace_method_entry_exit_Type() {
1736   // create input type (domain)

1747   return TypeFunc::make(domain,range);
1748 }
1749 
1750 const TypeFunc *OptoRuntime::dtrace_object_alloc_Type() {
1751   // create input type (domain)
1752   const Type **fields = TypeTuple::fields(2);
1753   fields[TypeFunc::Parms+0] = TypeRawPtr::BOTTOM; // Thread-local storage
1754   fields[TypeFunc::Parms+1] = TypeInstPtr::NOTNULL;  // oop;    newly allocated object
1755 
1756   const TypeTuple *domain = TypeTuple::make(TypeFunc::Parms+2,fields);
1757 
1758   // create result type (range)
1759   fields = TypeTuple::fields(0);
1760 
1761   const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0,fields);
1762 
1763   return TypeFunc::make(domain,range);
1764 }
1765 
1766 
1767 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, register_finalizer, OptoRuntime::register_finalizer(oopDesc* obj, JavaThread* current))
1768   assert(oopDesc::is_oop(obj), "must be a valid oop");
1769   assert(obj->klass()->has_finalizer(), "shouldn't be here otherwise");
1770   InstanceKlass::register_finalizer(instanceOop(obj), CHECK);
1771 JRT_END
1772 
1773 JRT_ENTRY_NO_ASYNC_PROF(void, OptoRuntime, class_init_barrier, OptoRuntime::class_init_barrier(Klass* k, JavaThread* current))
1774   InstanceKlass* ik = InstanceKlass::cast(k);
1775   if (ik->should_be_initialized()) {
1776     ik->initialize(CHECK);
1777   } else if (UsePerfData) {
1778     _perf_OptoRuntime_class_init_barrier_redundant_count->inc();
1779   }
1780 JRT_END
1781 
1782 //-----------------------------------------------------------------------------
1783 
1784 NamedCounter * volatile OptoRuntime::_named_counters = nullptr;
1785 
1786 //
1787 // dump the collected NamedCounters.
1788 //
1789 void OptoRuntime::print_named_counters() {
1790   int total_lock_count = 0;
1791   int eliminated_lock_count = 0;
1792 
1793   NamedCounter* c = _named_counters;
1794   while (c) {
1795     if (c->tag() == NamedCounter::LockCounter || c->tag() == NamedCounter::EliminatedLockCounter) {
1796       int count = c->count();
1797       if (count > 0) {
1798         bool eliminated = c->tag() == NamedCounter::EliminatedLockCounter;
1799         if (Verbose) {
1800           tty->print_cr("%d %s%s", count, c->name(), eliminated ? " (eliminated)" : "");
1801         }

1876   trace_exception_counter++;
1877   stringStream tempst;
1878 
1879   tempst.print("%d [Exception (%s): ", trace_exception_counter, msg);
1880   exception_oop->print_value_on(&tempst);
1881   tempst.print(" in ");
1882   CodeBlob* blob = CodeCache::find_blob(exception_pc);
1883   if (blob->is_compiled()) {
1884     CompiledMethod* cm = blob->as_compiled_method_or_null();
1885     cm->method()->print_value_on(&tempst);
1886   } else if (blob->is_runtime_stub()) {
1887     tempst.print("<runtime-stub>");
1888   } else {
1889     tempst.print("<unknown>");
1890   }
1891   tempst.print(" at " INTPTR_FORMAT,  p2i(exception_pc));
1892   tempst.print("]");
1893 
1894   st->print_raw_cr(tempst.freeze());
1895 }
1896 
1897 #define DO_COUNTERS2(macro2, macro1) \
1898   macro2(OptoRuntime, new_instance_C) \
1899   macro2(OptoRuntime, new_array_C) \
1900   macro2(OptoRuntime, new_array_nozero_C) \
1901   macro2(OptoRuntime, multianewarray2_C) \
1902   macro2(OptoRuntime, multianewarray3_C) \
1903   macro2(OptoRuntime, multianewarray4_C) \
1904   macro2(OptoRuntime, multianewarrayN_C) \
1905   macro2(OptoRuntime, monitor_notify_C) \
1906   macro2(OptoRuntime, monitor_notifyAll_C) \
1907   macro2(OptoRuntime, handle_exception_C_helper) \
1908   macro2(OptoRuntime, register_finalizer) \
1909   macro2(OptoRuntime, class_init_barrier) \
1910   macro1(OptoRuntime, class_init_barrier_redundant)
1911 
1912 #define INIT_COUNTER_TIME_AND_CNT(sub, name) \
1913   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
1914   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1915 
1916 #define INIT_COUNTER_CNT(sub, name) \
1917   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1918 
1919 void OptoRuntime::init_counters() {
1920   assert(CompilerConfig::is_c2_enabled(), "");
1921 
1922   if (UsePerfData) {
1923     EXCEPTION_MARK;
1924 
1925     DO_COUNTERS2(INIT_COUNTER_TIME_AND_CNT, INIT_COUNTER_CNT)
1926 
1927     if (HAS_PENDING_EXCEPTION) {
1928       vm_exit_during_initialization("jvm_perf_init failed unexpectedly");
1929     }
1930   }
1931 }
1932 #undef INIT_COUNTER_TIME_AND_CNT
1933 #undef INIT_COUNTER_CNT
1934 
1935 #define PRINT_COUNTER_TIME_AND_CNT(sub, name) { \
1936   jlong count = _perf_##sub##_##name##_count->get_value(); \
1937   if (count > 0) { \
1938     st->print_cr("  %-30s = %4ldms (elapsed) %4ldms (thread) (%5ld events)", #sub "::" #name, \
1939                  _perf_##sub##_##name##_timer->elapsed_counter_value_ms(), \
1940                  _perf_##sub##_##name##_timer->thread_counter_value_ms(), \
1941                  count); \
1942   }}
1943 
1944 #define PRINT_COUNTER_CNT(sub, name) { \
1945   jlong count = _perf_##sub##_##name##_count->get_value(); \
1946   if (count > 0) { \
1947     st->print_cr("  %-30s = %5ld events", #name, count); \
1948   }}
1949 
1950 void OptoRuntime::print_counters_on(outputStream* st) {
1951   if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c2_enabled()) {
1952     DO_COUNTERS2(PRINT_COUNTER_TIME_AND_CNT, PRINT_COUNTER_CNT)
1953   } else {
1954     st->print_cr("  OptoRuntime: no info (%s is disabled)",
1955                  (!CompilerConfig::is_c2_enabled() ? "C2" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
1956   }
1957 }
1958 
1959 #undef PRINT_COUNTER_TIME_AND_CNT
1960 #undef PRINT_COUNTER_CNT
1961 #undef DO_COUNTERS2
< prev index next >