< prev index next >

src/hotspot/share/c1/c1_Runtime1.cpp

Print this page

  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "asm/codeBuffer.hpp"
  26 #include "c1/c1_CodeStubs.hpp"
  27 #include "c1/c1_Defs.hpp"
  28 #include "c1/c1_LIRAssembler.hpp"
  29 #include "c1/c1_MacroAssembler.hpp"
  30 #include "c1/c1_Runtime1.hpp"
  31 #include "classfile/javaClasses.inline.hpp"
  32 #include "classfile/vmClasses.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "code/aotCodeCache.hpp"
  35 #include "code/codeBlob.hpp"
  36 #include "code/compiledIC.hpp"
  37 #include "code/scopeDesc.hpp"
  38 #include "code/vtableStubs.hpp"
  39 #include "compiler/compilationPolicy.hpp"

  40 #include "compiler/disassembler.hpp"
  41 #include "compiler/oopMap.hpp"
  42 #include "gc/shared/barrierSet.hpp"
  43 #include "gc/shared/c1/barrierSetC1.hpp"
  44 #include "gc/shared/collectedHeap.hpp"
  45 #include "interpreter/bytecode.hpp"
  46 #include "interpreter/interpreter.hpp"
  47 #include "jfr/support/jfrIntrinsics.hpp"
  48 #include "logging/log.hpp"
  49 #include "memory/oopFactory.hpp"
  50 #include "memory/resourceArea.hpp"
  51 #include "memory/universe.hpp"
  52 #include "oops/access.inline.hpp"
  53 #include "oops/objArrayKlass.hpp"
  54 #include "oops/objArrayOop.inline.hpp"
  55 #include "oops/oop.inline.hpp"
  56 #include "prims/jvmtiExport.hpp"
  57 #include "runtime/atomicAccess.hpp"
  58 #include "runtime/fieldDescriptor.inline.hpp"
  59 #include "runtime/frame.inline.hpp"
  60 #include "runtime/handles.inline.hpp"
  61 #include "runtime/interfaceSupport.inline.hpp"
  62 #include "runtime/javaCalls.hpp"


  63 #include "runtime/sharedRuntime.hpp"
  64 #include "runtime/stackWatermarkSet.hpp"
  65 #include "runtime/stubInfo.hpp"
  66 #include "runtime/stubRoutines.hpp"
  67 #include "runtime/vframe.inline.hpp"
  68 #include "runtime/vframeArray.hpp"
  69 #include "runtime/vm_version.hpp"

  70 #include "utilities/copy.hpp"
  71 #include "utilities/events.hpp"
  72 
  73 
  74 // Implementation of StubAssembler
  75 
  76 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) {
  77   _name = name;
  78   _must_gc_arguments = false;
  79   _frame_size = no_frame_size;
  80   _num_rt_args = 0;
  81   _stub_id = stub_id;
  82 }
  83 
  84 
  85 void StubAssembler::set_info(const char* name, bool must_gc_arguments) {
  86   _name = name;
  87   _must_gc_arguments = must_gc_arguments;
  88 }
  89 

 252   case StubId::c1_slow_subtype_check_id:
 253   case StubId::c1_fpu2long_stub_id:
 254   case StubId::c1_unwind_exception_id:
 255   case StubId::c1_counter_overflow_id:
 256   case StubId::c1_is_instance_of_id:
 257     expect_oop_map = false;
 258     break;
 259   default:
 260     break;
 261   }
 262 #endif
 263   C1StubAssemblerCodeGenClosure cl(id);
 264   CodeBlob* blob = generate_blob(buffer_blob, id, name_for(id), expect_oop_map, &cl);
 265   // install blob
 266   int idx = StubInfo::c1_offset(id);   // will assert on non-c1 id
 267   _blobs[idx] = blob;
 268   return blob != nullptr;
 269 }
 270 
 271 bool Runtime1::initialize(BufferBlob* blob) {

 272   // platform-dependent initialization
 273   initialize_pd();
 274   // iterate blobs in C1 group and generate a single stub per blob
 275   StubId id = StubInfo::stub_base(StubGroup::C1);
 276   StubId limit = StubInfo::next(StubInfo::stub_max(StubGroup::C1));
 277   for (; id != limit; id = StubInfo::next(id)) {
 278     if (!generate_blob_for(blob, id)) {
 279       return false;
 280     }
 281     if (id == StubId::c1_forward_exception_id) {
 282       // publish early c1 stubs at this point so later stubs can refer to them
 283       AOTCodeCache::init_early_c1_table();
 284     }
 285   }
 286   // printing
 287 #ifndef PRODUCT
 288   if (PrintSimpleStubs) {
 289     ResourceMark rm;
 290     id = StubInfo::stub_base(StubGroup::C1);
 291     for (; id != limit; id = StubInfo::next(id)) {

 351   FUNCTION_CASE(entry, trace_block_entry);
 352 #ifdef JFR_HAVE_INTRINSICS
 353   FUNCTION_CASE(entry, JfrTime::time_function());
 354 #endif
 355   FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32());
 356   FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32C());
 357   FUNCTION_CASE(entry, StubRoutines::vectorizedMismatch());
 358   FUNCTION_CASE(entry, StubRoutines::dexp());
 359   FUNCTION_CASE(entry, StubRoutines::dlog());
 360   FUNCTION_CASE(entry, StubRoutines::dlog10());
 361   FUNCTION_CASE(entry, StubRoutines::dpow());
 362   FUNCTION_CASE(entry, StubRoutines::dsin());
 363   FUNCTION_CASE(entry, StubRoutines::dcos());
 364   FUNCTION_CASE(entry, StubRoutines::dtan());
 365   FUNCTION_CASE(entry, StubRoutines::dsinh());
 366   FUNCTION_CASE(entry, StubRoutines::dtanh());
 367   FUNCTION_CASE(entry, StubRoutines::dcbrt());
 368 
 369 #undef FUNCTION_CASE
 370 






 371   // Soft float adds more runtime names.
 372   return pd_name_for_address(entry);
 373 }
 374 
 375 
 376 JRT_ENTRY(void, Runtime1::new_instance(JavaThread* current, Klass* klass))
 377 #ifndef PRODUCT
 378   if (PrintC1Statistics) {
 379     _new_instance_slowcase_cnt++;
 380   }
 381 #endif
 382   assert(klass->is_klass(), "not a class");
 383   Handle holder(current, klass->klass_holder()); // keep the klass alive
 384   InstanceKlass* h = InstanceKlass::cast(klass);
 385   h->check_valid_for_instantiation(true, CHECK);
 386   // make sure klass is initialized
 387   h->initialize(CHECK);
 388   // allocate instance and return via TLS
 389   oop obj = h->allocate_instance(CHECK);
 390   current->set_vm_result_oop(obj);
 391 JRT_END
 392 
 393 
 394 JRT_ENTRY(void, Runtime1::new_type_array(JavaThread* current, Klass* klass, jint length))
 395 #ifndef PRODUCT
 396   if (PrintC1Statistics) {
 397     _new_type_array_slowcase_cnt++;
 398   }
 399 #endif
 400   // Note: no handle for klass needed since they are not used
 401   //       anymore after new_typeArray() and no GC can happen before.
 402   //       (This may have to change if this code changes!)
 403   assert(klass->is_klass(), "not a class");
 404   BasicType elt_type = TypeArrayKlass::cast(klass)->element_type();
 405   oop obj = oopFactory::new_typeArray(elt_type, length, CHECK);
 406   current->set_vm_result_oop(obj);
 407   // This is pretty rare but this runtime patch is stressful to deoptimization
 408   // if we deoptimize here so force a deopt to stress the path.
 409   if (DeoptimizeALot) {
 410     deopt_caller(current);
 411   }
 412 
 413 JRT_END
 414 
 415 
 416 JRT_ENTRY(void, Runtime1::new_object_array(JavaThread* current, Klass* array_klass, jint length))
 417 #ifndef PRODUCT
 418   if (PrintC1Statistics) {
 419     _new_object_array_slowcase_cnt++;
 420   }
 421 #endif
 422   // Note: no handle for klass needed since they are not used
 423   //       anymore after new_objArray() and no GC can happen before.
 424   //       (This may have to change if this code changes!)
 425   assert(array_klass->is_klass(), "not a class");
 426   Handle holder(current, array_klass->klass_holder()); // keep the klass alive
 427   Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass();
 428   objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK);
 429   current->set_vm_result_oop(obj);
 430   // This is pretty rare but this runtime patch is stressful to deoptimization
 431   // if we deoptimize here so force a deopt to stress the path.
 432   if (DeoptimizeALot) {
 433     deopt_caller(current);
 434   }
 435 JRT_END
 436 
 437 
 438 JRT_ENTRY(void, Runtime1::new_multi_array(JavaThread* current, Klass* klass, int rank, jint* dims))
 439 #ifndef PRODUCT
 440   if (PrintC1Statistics) {
 441     _new_multi_array_slowcase_cnt++;
 442   }
 443 #endif
 444   assert(klass->is_klass(), "not a class");
 445   assert(rank >= 1, "rank must be nonzero");
 446   Handle holder(current, klass->klass_holder()); // keep the klass alive
 447   oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
 448   current->set_vm_result_oop(obj);
 449 JRT_END
 450 
 451 
 452 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* current, StubId id))
 453   tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", (int)id);
 454 JRT_END
 455 
 456 
 457 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* current, oopDesc* obj))
 458   ResourceMark rm(current);

 489       case Bytecodes::_if_icmplt: case Bytecodes::_iflt:
 490       case Bytecodes::_if_icmpgt: case Bytecodes::_ifgt:
 491       case Bytecodes::_if_icmple: case Bytecodes::_ifle:
 492       case Bytecodes::_if_icmpge: case Bytecodes::_ifge:
 493       case Bytecodes::_if_icmpeq: case Bytecodes::_if_acmpeq: case Bytecodes::_ifeq:
 494       case Bytecodes::_if_icmpne: case Bytecodes::_if_acmpne: case Bytecodes::_ifne:
 495       case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: case Bytecodes::_goto:
 496         offset = (int16_t)Bytes::get_Java_u2(pc + 1);
 497         break;
 498       case Bytecodes::_goto_w:
 499         offset = Bytes::get_Java_u4(pc + 1);
 500         break;
 501       default: ;
 502     }
 503     bci = branch_bci + offset;
 504   }
 505   osr_nm = CompilationPolicy::event(enclosing_method, method, branch_bci, bci, level, nm, current);
 506   return osr_nm;
 507 }
 508 
 509 JRT_BLOCK_ENTRY(address, Runtime1::counter_overflow(JavaThread* current, int bci, Method* method))
 510   nmethod* osr_nm;
 511   JRT_BLOCK_NO_ASYNC
 512     osr_nm = counter_overflow_helper(current, bci, method);
 513     if (osr_nm != nullptr) {
 514       RegisterMap map(current,
 515                       RegisterMap::UpdateMap::skip,
 516                       RegisterMap::ProcessFrames::include,
 517                       RegisterMap::WalkContinuation::skip);
 518       frame fr =  current->last_frame().sender(&map);
 519       Deoptimization::deoptimize_frame(current, fr.id());
 520     }
 521   JRT_BLOCK_END
 522   return nullptr;
 523 JRT_END
 524 
 525 extern void vm_exit(int code);
 526 
 527 // Enter this method from compiled code handler below. This is where we transition
 528 // to VM mode. This is done as a helper routine so that the method called directly
 529 // from compiled code does not have to transition to VM. This allows the entry
 530 // method to see if the nmethod that we have just looked up a handler for has
 531 // been deoptimized while we were in the vm. This simplifies the assembly code
 532 // cpu directories.
 533 //
 534 // We are entering here from exception stub (via the entry method below)
 535 // If there is a compiled exception handler in this method, we will continue there;
 536 // otherwise we will unwind the stack and continue at the caller of top frame method
 537 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
 538 // control the area where we can allow a safepoint. After we exit the safepoint area we can
 539 // check to see if the handler we are going to return is now in a nmethod that has
 540 // been deoptimized. If that is the case we return the deopt blob
 541 // unpack_with_exception entry instead. This makes life for the exception blob easier
 542 // because making that same check and diverting is painful from assembly language.
 543 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* current, oopDesc* ex, address pc, nmethod*& nm))
 544   Handle exception(current, ex);
 545 
 546   // This function is called when we are about to throw an exception. Therefore,
 547   // we have to poll the stack watermark barrier to make sure that not yet safe
 548   // stack frames are made safe before returning into them.
 549   if (current->last_frame().cb() == Runtime1::blob_for(StubId::c1_handle_exception_from_callee_id)) {
 550     // The StubId::c1_handle_exception_from_callee_id handler is invoked after the
 551     // frame has been unwound. It instead builds its own stub frame, to call the
 552     // runtime. But the throwing frame has already been unwound here.
 553     StackWatermarkSet::after_unwind(current);
 554   }
 555 
 556   nm = CodeCache::find_nmethod(pc);
 557   assert(nm != nullptr, "this is not an nmethod");
 558   // Adjust the pc as needed/
 559   if (nm->is_deopt_pc(pc)) {
 560     RegisterMap map(current,
 561                     RegisterMap::UpdateMap::skip,
 562                     RegisterMap::ProcessFrames::include,
 563                     RegisterMap::WalkContinuation::skip);

 748     _throw_class_cast_exception_count++;
 749   }
 750 #endif
 751   ResourceMark rm(current);
 752   char* message = SharedRuntime::generate_class_cast_message(current, object->klass());
 753   SharedRuntime::throw_and_post_jvmti_exception(current, vmSymbols::java_lang_ClassCastException(), message);
 754 JRT_END
 755 
 756 
 757 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* current))
 758 #ifndef PRODUCT
 759   if (PrintC1Statistics) {
 760     _throw_incompatible_class_change_error_count++;
 761   }
 762 #endif
 763   ResourceMark rm(current);
 764   SharedRuntime::throw_and_post_jvmti_exception(current, vmSymbols::java_lang_IncompatibleClassChangeError());
 765 JRT_END
 766 
 767 
 768 JRT_BLOCK_ENTRY(void, Runtime1::monitorenter(JavaThread* current, oopDesc* obj, BasicObjectLock* lock))
 769 #ifndef PRODUCT
 770   if (PrintC1Statistics) {
 771     _monitorenter_slowcase_cnt++;
 772   }
 773 #endif
 774   assert(obj == lock->obj(), "must match");
 775   SharedRuntime::monitor_enter_helper(obj, lock->lock(), current);
 776 JRT_END
 777 
 778 
 779 JRT_LEAF(void, Runtime1::monitorexit(JavaThread* current, BasicObjectLock* lock))
 780   assert(current == JavaThread::current(), "pre-condition");
 781 #ifndef PRODUCT
 782   if (PrintC1Statistics) {
 783     _monitorexit_slowcase_cnt++;
 784   }
 785 #endif
 786   assert(current->last_Java_sp(), "last_Java_sp must be set");
 787   oop obj = lock->obj();
 788   assert(oopDesc::is_oop(obj), "must be null or an object");
 789   SharedRuntime::monitor_exit_helper(obj, lock->lock(), current);
 790 JRT_END
 791 
 792 // Cf. OptoRuntime::deoptimize_caller_frame
 793 JRT_ENTRY(void, Runtime1::deoptimize(JavaThread* current, jint trap_request))
 794   // Called from within the owner thread, so no need for safepoint
 795   RegisterMap reg_map(current,
 796                       RegisterMap::UpdateMap::skip,
 797                       RegisterMap::ProcessFrames::include,
 798                       RegisterMap::WalkContinuation::skip);
 799   frame stub_frame = current->last_frame();
 800   assert(stub_frame.is_runtime_frame(), "Sanity check");
 801   frame caller_frame = stub_frame.sender(&reg_map);
 802   nmethod* nm = caller_frame.cb()->as_nmethod_or_null();
 803   assert(nm != nullptr, "Sanity check");
 804   methodHandle method(current, nm->method());
 805   assert(nm == CodeCache::find_nmethod(caller_frame.pc()), "Should be the same");
 806   Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
 807   Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
 808 
 809   if (action == Deoptimization::Action_make_not_entrant) {
 810     if (nm->make_not_entrant(nmethod::InvalidationReason::C1_DEOPTIMIZE)) {
 811       if (reason == Deoptimization::Reason_tenured) {
 812         MethodData* trap_mdo = Deoptimization::get_method_data(current, method, true /*create_if_missing*/);
 813         if (trap_mdo != nullptr) {

 816       }
 817     }
 818   }
 819 
 820   // Deoptimize the caller frame.
 821   Deoptimization::deoptimize_frame(current, caller_frame.id());
 822   // Return to the now deoptimized frame.
 823 JRT_END
 824 
 825 
 826 #ifndef DEOPTIMIZE_WHEN_PATCHING
 827 
 828 static Klass* resolve_field_return_klass(const methodHandle& caller, int bci, TRAPS) {
 829   Bytecode_field field_access(caller, bci);
 830   // This can be static or non-static field access
 831   Bytecodes::Code code       = field_access.code();
 832 
 833   // We must load class, initialize class and resolve the field
 834   fieldDescriptor result; // initialize class if needed
 835   constantPoolHandle constants(THREAD, caller->constants());
 836   LinkResolver::resolve_field_access(result, constants, field_access.index(), caller, Bytecodes::java_code(code), CHECK_NULL);

 837   return result.field_holder();
 838 }
 839 
 840 
 841 //
 842 // This routine patches sites where a class wasn't loaded or
 843 // initialized at the time the code was generated.  It handles
 844 // references to classes, fields and forcing of initialization.  Most
 845 // of the cases are straightforward and involving simply forcing
 846 // resolution of a class, rewriting the instruction stream with the
 847 // needed constant and replacing the call in this function with the
 848 // patched code.  The case for static field is more complicated since
 849 // the thread which is in the process of initializing a class can
 850 // access it's static fields but other threads can't so the code
 851 // either has to deoptimize when this case is detected or execute a
 852 // check that the current thread is the initializing thread.  The
 853 // current
 854 //
 855 // Patches basically look like this:
 856 //

 919 // always end up with a correct outcome.  This is easiest if there are
 920 // few or no intermediate states.  (Some inline caches have two
 921 // related instructions that must be patched in tandem.  For those,
 922 // intermediate states seem to be unavoidable, but we will get the
 923 // right answer from all possible observation orders.)
 924 //
 925 // When patching the entry instruction at the head of a method, or a
 926 // linkable call instruction inside of a method, we try very hard to
 927 // use a patch sequence which executes as a single memory transaction.
 928 // This means, in practice, that when thread A patches an instruction,
 929 // it should patch a 32-bit or 64-bit word that somehow overlaps the
 930 // instruction or is contained in it.  We believe that memory hardware
 931 // will never break up such a word write, if it is naturally aligned
 932 // for the word being written.  We also know that some CPUs work very
 933 // hard to create atomic updates even of naturally unaligned words,
 934 // but we don't want to bet the farm on this always working.
 935 //
 936 // Therefore, if there is any chance of a race condition, we try to
 937 // patch only naturally aligned words, as single, full-word writes.
 938 
 939 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* current, StubId stub_id ))
 940 #ifndef PRODUCT
 941   if (PrintC1Statistics) {
 942     _patch_code_slowcase_cnt++;
 943   }
 944 #endif
 945 
 946   ResourceMark rm(current);
 947   RegisterMap reg_map(current,
 948                       RegisterMap::UpdateMap::skip,
 949                       RegisterMap::ProcessFrames::include,
 950                       RegisterMap::WalkContinuation::skip);
 951   frame runtime_frame = current->last_frame();
 952   frame caller_frame = runtime_frame.sender(&reg_map);
 953 
 954   // last java frame on stack
 955   vframeStream vfst(current, true);
 956   assert(!vfst.at_end(), "Java frame must exist");
 957 
 958   methodHandle caller_method(current, vfst.method());
 959   // Note that caller_method->code() may not be same as caller_code because of OSR's

 964   Bytecodes::Code code = caller_method()->java_code_at(bci);
 965 
 966   // this is used by assertions in the access_field_patching_id
 967   BasicType patch_field_type = T_ILLEGAL;
 968   bool deoptimize_for_volatile = false;
 969   bool deoptimize_for_atomic = false;
 970   int patch_field_offset = -1;
 971   Klass* init_klass = nullptr; // klass needed by load_klass_patching code
 972   Klass* load_klass = nullptr; // klass needed by load_klass_patching code
 973   Handle mirror(current, nullptr); // oop needed by load_mirror_patching code
 974   Handle appendix(current, nullptr); // oop needed by appendix_patching code
 975   bool load_klass_or_mirror_patch_id =
 976     (stub_id == StubId::c1_load_klass_patching_id || stub_id == StubId::c1_load_mirror_patching_id);
 977 
 978   if (stub_id == StubId::c1_access_field_patching_id) {
 979 
 980     Bytecode_field field_access(caller_method, bci);
 981     fieldDescriptor result; // initialize class if needed
 982     Bytecodes::Code code = field_access.code();
 983     constantPoolHandle constants(current, caller_method->constants());
 984     LinkResolver::resolve_field_access(result, constants, field_access.index(), caller_method, Bytecodes::java_code(code), CHECK);

 985     patch_field_offset = result.offset();
 986 
 987     // If we're patching a field which is volatile then at compile it
 988     // must not have been know to be volatile, so the generated code
 989     // isn't correct for a volatile reference.  The nmethod has to be
 990     // deoptimized so that the code can be regenerated correctly.
 991     // This check is only needed for access_field_patching since this
 992     // is the path for patching field offsets.  load_klass is only
 993     // used for patching references to oops which don't need special
 994     // handling in the volatile case.
 995 
 996     deoptimize_for_volatile = result.access_flags().is_volatile();
 997 
 998     // If we are patching a field which should be atomic, then
 999     // the generated code is not correct either, force deoptimizing.
1000     // We need to only cover T_LONG and T_DOUBLE fields, as we can
1001     // break access atomicity only for them.
1002 
1003     // Strictly speaking, the deoptimization on 64-bit platforms
1004     // is unnecessary, and T_LONG stores on 32-bit platforms need

1302     switch (code) {
1303       case Bytecodes::_new:
1304       case Bytecodes::_anewarray:
1305       case Bytecodes::_multianewarray:
1306       case Bytecodes::_instanceof:
1307       case Bytecodes::_checkcast: {
1308         Bytecode bc(caller_method(), caller_method->bcp_from(bci));
1309         constantTag tag = caller_method->constants()->tag_at(bc.get_index_u2(code));
1310         if (tag.is_unresolved_klass_in_error()) {
1311           return false; // throws resolution error
1312         }
1313         break;
1314       }
1315 
1316       default: break;
1317     }
1318   }
1319   return true;
1320 }
1321 
1322 void Runtime1::patch_code(JavaThread* current, StubId stub_id) {
1323 #ifndef PRODUCT
1324   if (PrintC1Statistics) {
1325     _patch_code_slowcase_cnt++;
1326   }
1327 #endif
1328 
1329   // Enable WXWrite: the function is called by c1 stub as a runtime function
1330   // (see another implementation above).
1331   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
1332 
1333   if (TracePatching) {
1334     tty->print_cr("Deoptimizing because patch is needed");
1335   }
1336 
1337   RegisterMap reg_map(current,
1338                       RegisterMap::UpdateMap::skip,
1339                       RegisterMap::ProcessFrames::include,
1340                       RegisterMap::WalkContinuation::skip);
1341 
1342   frame runtime_frame = current->last_frame();
1343   frame caller_frame = runtime_frame.sender(&reg_map);
1344   assert(caller_frame.is_compiled_frame(), "Wrong frame type");
1345 
1346   if (is_patching_needed(current, stub_id)) {
1347     // Make sure the nmethod is invalidated, i.e. made not entrant.
1348     nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1349     if (nm != nullptr) {
1350       nm->make_not_entrant(nmethod::InvalidationReason::C1_DEOPTIMIZE_FOR_PATCHING);
1351     }
1352   }
1353 
1354   Deoptimization::deoptimize_frame(current, caller_frame.id());
1355   // Return to the now deoptimized frame.
1356   postcond(caller_is_deopted(current));
1357 }
1358 
1359 #endif // DEOPTIMIZE_WHEN_PATCHING
1360 
1361 // Entry point for compiled code. We want to patch a nmethod.
1362 // We don't do a normal VM transition here because we want to
1363 // know after the patching is complete and any safepoint(s) are taken
1364 // if the calling nmethod was deoptimized. We do this by calling a
1365 // helper method which does the normal VM transition and when it
1366 // completes we can check for deoptimization. This simplifies the
1367 // assembly code in the cpu directories.
1368 //
1369 int Runtime1::move_klass_patching(JavaThread* current) {
1370 //
1371 // NOTE: we are still in Java
1372 //
1373   DEBUG_ONLY(NoHandleMark nhm;)
1374   {
1375     // Enter VM mode
1376     ResetNoHandleMark rnhm;
1377     patch_code(current, StubId::c1_load_klass_patching_id);

1428 int Runtime1::access_field_patching(JavaThread* current) {
1429   //
1430   // NOTE: we are still in Java
1431   //
1432   // Handles created in this function will be deleted by the
1433   // HandleMarkCleaner in the transition to the VM.
1434   NoHandleMark nhm;
1435   {
1436     // Enter VM mode
1437     ResetNoHandleMark rnhm;
1438     patch_code(current, StubId::c1_access_field_patching_id);
1439   }
1440   // Back in JAVA, use no oops DON'T safepoint
1441 
1442   // Return true if calling code is deoptimized
1443 
1444   return caller_is_deopted(current);
1445 }
1446 
1447 
1448 JRT_LEAF(void, Runtime1::trace_block_entry(jint block_id))
1449   // for now we just print out the block id
1450   tty->print("%d ", block_id);
1451 JRT_END
1452 
1453 
1454 JRT_LEAF(int, Runtime1::is_instance_of(oopDesc* mirror, oopDesc* obj))
1455   // had to return int instead of bool, otherwise there may be a mismatch
1456   // between the C calling convention and the Java one.
1457   // e.g., on x86, GCC may clear only %al when returning a bool false, but
1458   // JVM takes the whole %eax as the return value, which may misinterpret
1459   // the return value as a boolean true.
1460 
1461   assert(mirror != nullptr, "should null-check on mirror before calling");
1462   Klass* k = java_lang_Class::as_Klass(mirror);
1463   return (k != nullptr && obj != nullptr && obj->is_a(k)) ? 1 : 0;
1464 JRT_END
1465 
1466 JRT_ENTRY(void, Runtime1::predicate_failed_trap(JavaThread* current))
1467   ResourceMark rm;
1468 
1469   RegisterMap reg_map(current,
1470                       RegisterMap::UpdateMap::skip,
1471                       RegisterMap::ProcessFrames::include,
1472                       RegisterMap::WalkContinuation::skip);
1473   frame runtime_frame = current->last_frame();
1474   frame caller_frame = runtime_frame.sender(&reg_map);
1475 
1476   nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1477   assert (nm != nullptr, "no more nmethod?");
1478   nm->make_not_entrant(nmethod::InvalidationReason::C1_PREDICATE_FAILED_TRAP);
1479 
1480   methodHandle m(current, nm->method());
1481   MethodData* mdo = m->method_data();
1482 
1483   if (mdo == nullptr && !HAS_PENDING_EXCEPTION) {
1484     // Build an MDO.  Ignore errors like OutOfMemory;
1485     // that simply means we won't have an MDO to update.
1486     Method::build_profiling_method_data(m, THREAD);

1506   }
1507 
1508 
1509   Deoptimization::deoptimize_frame(current, caller_frame.id());
1510 
1511 JRT_END
1512 
1513 // Check exception if AbortVMOnException flag set
1514 JRT_LEAF(void, Runtime1::check_abort_on_vm_exception(oopDesc* ex))
1515   ResourceMark rm;
1516   const char* message = nullptr;
1517   if (ex->is_a(vmClasses::Throwable_klass())) {
1518     oop msg = java_lang_Throwable::message(ex);
1519     if (msg != nullptr) {
1520       message = java_lang_String::as_utf8_string(msg);
1521     }
1522   }
1523   Exceptions::debug_check_abort(ex->klass()->external_name(), message);
1524 JRT_END
1525 
























































1526 #ifndef PRODUCT
1527 void Runtime1::print_statistics() {
1528   tty->print_cr("C1 Runtime statistics:");
1529   tty->print_cr(" _resolve_invoke_virtual_cnt:     %u", SharedRuntime::_resolve_virtual_ctr);
1530   tty->print_cr(" _resolve_invoke_opt_virtual_cnt: %u", SharedRuntime::_resolve_opt_virtual_ctr);
1531   tty->print_cr(" _resolve_invoke_static_cnt:      %u", SharedRuntime::_resolve_static_ctr);
1532   tty->print_cr(" _handle_wrong_method_cnt:        %u", SharedRuntime::_wrong_method_ctr);
1533   tty->print_cr(" _ic_miss_cnt:                    %u", SharedRuntime::_ic_miss_ctr);
1534   tty->print_cr(" _generic_arraycopystub_cnt:      %u", _generic_arraycopystub_cnt);
1535   tty->print_cr(" _byte_arraycopy_cnt:             %u", _byte_arraycopy_stub_cnt);
1536   tty->print_cr(" _short_arraycopy_cnt:            %u", _short_arraycopy_stub_cnt);
1537   tty->print_cr(" _int_arraycopy_cnt:              %u", _int_arraycopy_stub_cnt);
1538   tty->print_cr(" _long_arraycopy_cnt:             %u", _long_arraycopy_stub_cnt);
1539   tty->print_cr(" _oop_arraycopy_cnt:              %u", _oop_arraycopy_stub_cnt);
1540   tty->print_cr(" _arraycopy_slowcase_cnt:         %u", _arraycopy_slowcase_cnt);
1541   tty->print_cr(" _arraycopy_checkcast_cnt:        %u", _arraycopy_checkcast_cnt);
1542   tty->print_cr(" _arraycopy_checkcast_attempt_cnt:%u", _arraycopy_checkcast_attempt_cnt);
1543 
1544   tty->print_cr(" _new_type_array_slowcase_cnt:    %u", _new_type_array_slowcase_cnt);
1545   tty->print_cr(" _new_object_array_slowcase_cnt:  %u", _new_object_array_slowcase_cnt);
1546   tty->print_cr(" _new_instance_slowcase_cnt:      %u", _new_instance_slowcase_cnt);
1547   tty->print_cr(" _new_multi_array_slowcase_cnt:   %u", _new_multi_array_slowcase_cnt);
1548   tty->print_cr(" _monitorenter_slowcase_cnt:      %u", _monitorenter_slowcase_cnt);
1549   tty->print_cr(" _monitorexit_slowcase_cnt:       %u", _monitorexit_slowcase_cnt);
1550   tty->print_cr(" _patch_code_slowcase_cnt:        %u", _patch_code_slowcase_cnt);
1551 
1552   tty->print_cr(" _throw_range_check_exception_count:            %u:", _throw_range_check_exception_count);
1553   tty->print_cr(" _throw_index_exception_count:                  %u:", _throw_index_exception_count);
1554   tty->print_cr(" _throw_div0_exception_count:                   %u:", _throw_div0_exception_count);
1555   tty->print_cr(" _throw_null_pointer_exception_count:           %u:", _throw_null_pointer_exception_count);
1556   tty->print_cr(" _throw_class_cast_exception_count:             %u:", _throw_class_cast_exception_count);
1557   tty->print_cr(" _throw_incompatible_class_change_error_count:  %u:", _throw_incompatible_class_change_error_count);
1558   tty->print_cr(" _throw_count:                                  %u:", _throw_count);
1559 
1560   SharedRuntime::print_ic_miss_histogram();
1561   tty->cr();
1562 }
1563 #endif // PRODUCT

  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "asm/codeBuffer.hpp"
  26 #include "c1/c1_CodeStubs.hpp"
  27 #include "c1/c1_Defs.hpp"
  28 #include "c1/c1_LIRAssembler.hpp"
  29 #include "c1/c1_MacroAssembler.hpp"
  30 #include "c1/c1_Runtime1.hpp"
  31 #include "classfile/javaClasses.inline.hpp"
  32 #include "classfile/vmClasses.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "code/aotCodeCache.hpp"
  35 #include "code/codeBlob.hpp"
  36 #include "code/compiledIC.hpp"
  37 #include "code/scopeDesc.hpp"
  38 #include "code/vtableStubs.hpp"
  39 #include "compiler/compilationPolicy.hpp"
  40 #include "compiler/compilerDefinitions.inline.hpp"
  41 #include "compiler/disassembler.hpp"
  42 #include "compiler/oopMap.hpp"
  43 #include "gc/shared/barrierSet.hpp"
  44 #include "gc/shared/c1/barrierSetC1.hpp"
  45 #include "gc/shared/collectedHeap.hpp"
  46 #include "interpreter/bytecode.hpp"
  47 #include "interpreter/interpreter.hpp"
  48 #include "jfr/support/jfrIntrinsics.hpp"
  49 #include "logging/log.hpp"
  50 #include "memory/oopFactory.hpp"
  51 #include "memory/resourceArea.hpp"
  52 #include "memory/universe.hpp"
  53 #include "oops/access.inline.hpp"
  54 #include "oops/objArrayKlass.hpp"
  55 #include "oops/objArrayOop.inline.hpp"
  56 #include "oops/oop.inline.hpp"
  57 #include "prims/jvmtiExport.hpp"
  58 #include "runtime/atomicAccess.hpp"
  59 #include "runtime/fieldDescriptor.inline.hpp"
  60 #include "runtime/frame.inline.hpp"
  61 #include "runtime/handles.inline.hpp"
  62 #include "runtime/interfaceSupport.inline.hpp"
  63 #include "runtime/javaCalls.hpp"
  64 #include "runtime/perfData.inline.hpp"
  65 #include "runtime/runtimeUpcalls.hpp"
  66 #include "runtime/sharedRuntime.hpp"
  67 #include "runtime/stackWatermarkSet.hpp"
  68 #include "runtime/stubInfo.hpp"
  69 #include "runtime/stubRoutines.hpp"
  70 #include "runtime/vframe.inline.hpp"
  71 #include "runtime/vframeArray.hpp"
  72 #include "runtime/vm_version.hpp"
  73 #include "services/management.hpp"
  74 #include "utilities/copy.hpp"
  75 #include "utilities/events.hpp"
  76 
  77 
  78 // Implementation of StubAssembler
  79 
  80 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) {
  81   _name = name;
  82   _must_gc_arguments = false;
  83   _frame_size = no_frame_size;
  84   _num_rt_args = 0;
  85   _stub_id = stub_id;
  86 }
  87 
  88 
  89 void StubAssembler::set_info(const char* name, bool must_gc_arguments) {
  90   _name = name;
  91   _must_gc_arguments = must_gc_arguments;
  92 }
  93 

 256   case StubId::c1_slow_subtype_check_id:
 257   case StubId::c1_fpu2long_stub_id:
 258   case StubId::c1_unwind_exception_id:
 259   case StubId::c1_counter_overflow_id:
 260   case StubId::c1_is_instance_of_id:
 261     expect_oop_map = false;
 262     break;
 263   default:
 264     break;
 265   }
 266 #endif
 267   C1StubAssemblerCodeGenClosure cl(id);
 268   CodeBlob* blob = generate_blob(buffer_blob, id, name_for(id), expect_oop_map, &cl);
 269   // install blob
 270   int idx = StubInfo::c1_offset(id);   // will assert on non-c1 id
 271   _blobs[idx] = blob;
 272   return blob != nullptr;
 273 }
 274 
 275 bool Runtime1::initialize(BufferBlob* blob) {
 276   init_counters();
 277   // platform-dependent initialization
 278   initialize_pd();
 279   // iterate blobs in C1 group and generate a single stub per blob
 280   StubId id = StubInfo::stub_base(StubGroup::C1);
 281   StubId limit = StubInfo::next(StubInfo::stub_max(StubGroup::C1));
 282   for (; id != limit; id = StubInfo::next(id)) {
 283     if (!generate_blob_for(blob, id)) {
 284       return false;
 285     }
 286     if (id == StubId::c1_forward_exception_id) {
 287       // publish early c1 stubs at this point so later stubs can refer to them
 288       AOTCodeCache::init_early_c1_table();
 289     }
 290   }
 291   // printing
 292 #ifndef PRODUCT
 293   if (PrintSimpleStubs) {
 294     ResourceMark rm;
 295     id = StubInfo::stub_base(StubGroup::C1);
 296     for (; id != limit; id = StubInfo::next(id)) {

 356   FUNCTION_CASE(entry, trace_block_entry);
 357 #ifdef JFR_HAVE_INTRINSICS
 358   FUNCTION_CASE(entry, JfrTime::time_function());
 359 #endif
 360   FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32());
 361   FUNCTION_CASE(entry, StubRoutines::updateBytesCRC32C());
 362   FUNCTION_CASE(entry, StubRoutines::vectorizedMismatch());
 363   FUNCTION_CASE(entry, StubRoutines::dexp());
 364   FUNCTION_CASE(entry, StubRoutines::dlog());
 365   FUNCTION_CASE(entry, StubRoutines::dlog10());
 366   FUNCTION_CASE(entry, StubRoutines::dpow());
 367   FUNCTION_CASE(entry, StubRoutines::dsin());
 368   FUNCTION_CASE(entry, StubRoutines::dcos());
 369   FUNCTION_CASE(entry, StubRoutines::dtan());
 370   FUNCTION_CASE(entry, StubRoutines::dsinh());
 371   FUNCTION_CASE(entry, StubRoutines::dtanh());
 372   FUNCTION_CASE(entry, StubRoutines::dcbrt());
 373 
 374 #undef FUNCTION_CASE
 375 
 376   // Runtime upcalls also has a map of addresses to names
 377   const char* upcall_name = RuntimeUpcalls::get_name_for_upcall_address(entry);
 378   if (upcall_name != nullptr) {
 379     return upcall_name;
 380   }
 381 
 382   // Soft float adds more runtime names.
 383   return pd_name_for_address(entry);
 384 }
 385 
 386 
 387 JRT_ENTRY_PROF(void, Runtime1, new_instance, Runtime1::new_instance(JavaThread* current, Klass* klass))
 388 #ifndef PRODUCT
 389   if (PrintC1Statistics) {
 390     _new_instance_slowcase_cnt++;
 391   }
 392 #endif
 393   assert(klass->is_klass(), "not a class");
 394   Handle holder(current, klass->klass_holder()); // keep the klass alive
 395   InstanceKlass* h = InstanceKlass::cast(klass);
 396   h->check_valid_for_instantiation(true, CHECK);
 397   // make sure klass is initialized
 398   h->initialize(CHECK);
 399   // allocate instance and return via TLS
 400   oop obj = h->allocate_instance(CHECK);
 401   current->set_vm_result_oop(obj);
 402 JRT_END
 403 
 404 
 405 JRT_ENTRY_PROF(void, Runtime1, new_type_array, Runtime1::new_type_array(JavaThread* current, Klass* klass, jint length))
 406 #ifndef PRODUCT
 407   if (PrintC1Statistics) {
 408     _new_type_array_slowcase_cnt++;
 409   }
 410 #endif
 411   // Note: no handle for klass needed since they are not used
 412   //       anymore after new_typeArray() and no GC can happen before.
 413   //       (This may have to change if this code changes!)
 414   assert(klass->is_klass(), "not a class");
 415   BasicType elt_type = TypeArrayKlass::cast(klass)->element_type();
 416   oop obj = oopFactory::new_typeArray(elt_type, length, CHECK);
 417   current->set_vm_result_oop(obj);
 418   // This is pretty rare but this runtime patch is stressful to deoptimization
 419   // if we deoptimize here so force a deopt to stress the path.
 420   if (DeoptimizeALot) {
 421     deopt_caller(current);
 422   }
 423 
 424 JRT_END
 425 
 426 
 427 JRT_ENTRY_PROF(void, Runtime1, new_object_array, Runtime1::new_object_array(JavaThread* current, Klass* array_klass, jint length))
 428 #ifndef PRODUCT
 429   if (PrintC1Statistics) {
 430     _new_object_array_slowcase_cnt++;
 431   }
 432 #endif
 433   // Note: no handle for klass needed since they are not used
 434   //       anymore after new_objArray() and no GC can happen before.
 435   //       (This may have to change if this code changes!)
 436   assert(array_klass->is_klass(), "not a class");
 437   Handle holder(current, array_klass->klass_holder()); // keep the klass alive
 438   Klass* elem_klass = ObjArrayKlass::cast(array_klass)->element_klass();
 439   objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK);
 440   current->set_vm_result_oop(obj);
 441   // This is pretty rare but this runtime patch is stressful to deoptimization
 442   // if we deoptimize here so force a deopt to stress the path.
 443   if (DeoptimizeALot) {
 444     deopt_caller(current);
 445   }
 446 JRT_END
 447 
 448 
 449 JRT_ENTRY_PROF(void, Runtime1, new_multi_array, Runtime1::new_multi_array(JavaThread* current, Klass* klass, int rank, jint* dims))
 450 #ifndef PRODUCT
 451   if (PrintC1Statistics) {
 452     _new_multi_array_slowcase_cnt++;
 453   }
 454 #endif
 455   assert(klass->is_klass(), "not a class");
 456   assert(rank >= 1, "rank must be nonzero");
 457   Handle holder(current, klass->klass_holder()); // keep the klass alive
 458   oop obj = ArrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
 459   current->set_vm_result_oop(obj);
 460 JRT_END
 461 
 462 
 463 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* current, StubId id))
 464   tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", (int)id);
 465 JRT_END
 466 
 467 
 468 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* current, oopDesc* obj))
 469   ResourceMark rm(current);

 500       case Bytecodes::_if_icmplt: case Bytecodes::_iflt:
 501       case Bytecodes::_if_icmpgt: case Bytecodes::_ifgt:
 502       case Bytecodes::_if_icmple: case Bytecodes::_ifle:
 503       case Bytecodes::_if_icmpge: case Bytecodes::_ifge:
 504       case Bytecodes::_if_icmpeq: case Bytecodes::_if_acmpeq: case Bytecodes::_ifeq:
 505       case Bytecodes::_if_icmpne: case Bytecodes::_if_acmpne: case Bytecodes::_ifne:
 506       case Bytecodes::_ifnull: case Bytecodes::_ifnonnull: case Bytecodes::_goto:
 507         offset = (int16_t)Bytes::get_Java_u2(pc + 1);
 508         break;
 509       case Bytecodes::_goto_w:
 510         offset = Bytes::get_Java_u4(pc + 1);
 511         break;
 512       default: ;
 513     }
 514     bci = branch_bci + offset;
 515   }
 516   osr_nm = CompilationPolicy::event(enclosing_method, method, branch_bci, bci, level, nm, current);
 517   return osr_nm;
 518 }
 519 
 520 JRT_BLOCK_ENTRY_PROF(address, Runtime1, counter_overflow, Runtime1::counter_overflow(JavaThread* current, int bci, Method* method))
 521   nmethod* osr_nm;
 522   JRT_BLOCK_NO_ASYNC
 523     osr_nm = counter_overflow_helper(current, bci, method);
 524     if (osr_nm != nullptr) {
 525       RegisterMap map(current,
 526                       RegisterMap::UpdateMap::skip,
 527                       RegisterMap::ProcessFrames::include,
 528                       RegisterMap::WalkContinuation::skip);
 529       frame fr =  current->last_frame().sender(&map);
 530       Deoptimization::deoptimize_frame(current, fr.id());
 531     }
 532   JRT_BLOCK_END
 533   return nullptr;
 534 JRT_END
 535 
 536 extern void vm_exit(int code);
 537 
 538 // Enter this method from compiled code handler below. This is where we transition
 539 // to VM mode. This is done as a helper routine so that the method called directly
 540 // from compiled code does not have to transition to VM. This allows the entry
 541 // method to see if the nmethod that we have just looked up a handler for has
 542 // been deoptimized while we were in the vm. This simplifies the assembly code
 543 // cpu directories.
 544 //
 545 // We are entering here from exception stub (via the entry method below)
 546 // If there is a compiled exception handler in this method, we will continue there;
 547 // otherwise we will unwind the stack and continue at the caller of top frame method
 548 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
 549 // control the area where we can allow a safepoint. After we exit the safepoint area we can
 550 // check to see if the handler we are going to return is now in a nmethod that has
 551 // been deoptimized. If that is the case we return the deopt blob
 552 // unpack_with_exception entry instead. This makes life for the exception blob easier
 553 // because making that same check and diverting is painful from assembly language.
 554 JRT_ENTRY_NO_ASYNC_PROF(static address, Runtime1, exception_handler_for_pc_helper, exception_handler_for_pc_helper(JavaThread* current, oopDesc* ex, address pc, nmethod*& nm))
 555   Handle exception(current, ex);
 556 
 557   // This function is called when we are about to throw an exception. Therefore,
 558   // we have to poll the stack watermark barrier to make sure that not yet safe
 559   // stack frames are made safe before returning into them.
 560   if (current->last_frame().cb() == Runtime1::blob_for(StubId::c1_handle_exception_from_callee_id)) {
 561     // The StubId::c1_handle_exception_from_callee_id handler is invoked after the
 562     // frame has been unwound. It instead builds its own stub frame, to call the
 563     // runtime. But the throwing frame has already been unwound here.
 564     StackWatermarkSet::after_unwind(current);
 565   }
 566 
 567   nm = CodeCache::find_nmethod(pc);
 568   assert(nm != nullptr, "this is not an nmethod");
 569   // Adjust the pc as needed/
 570   if (nm->is_deopt_pc(pc)) {
 571     RegisterMap map(current,
 572                     RegisterMap::UpdateMap::skip,
 573                     RegisterMap::ProcessFrames::include,
 574                     RegisterMap::WalkContinuation::skip);

 759     _throw_class_cast_exception_count++;
 760   }
 761 #endif
 762   ResourceMark rm(current);
 763   char* message = SharedRuntime::generate_class_cast_message(current, object->klass());
 764   SharedRuntime::throw_and_post_jvmti_exception(current, vmSymbols::java_lang_ClassCastException(), message);
 765 JRT_END
 766 
 767 
 768 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* current))
 769 #ifndef PRODUCT
 770   if (PrintC1Statistics) {
 771     _throw_incompatible_class_change_error_count++;
 772   }
 773 #endif
 774   ResourceMark rm(current);
 775   SharedRuntime::throw_and_post_jvmti_exception(current, vmSymbols::java_lang_IncompatibleClassChangeError());
 776 JRT_END
 777 
 778 
 779 JRT_BLOCK_ENTRY_PROF(void, Runtime1, monitorenter, Runtime1::monitorenter(JavaThread* current, oopDesc* obj, BasicObjectLock* lock))
 780 #ifndef PRODUCT
 781   if (PrintC1Statistics) {
 782     _monitorenter_slowcase_cnt++;
 783   }
 784 #endif
 785   assert(obj == lock->obj(), "must match");
 786   SharedRuntime::monitor_enter_helper(obj, lock->lock(), current);
 787 JRT_END
 788 
 789 
 790 JRT_LEAF_PROF(void, Runtime1, monitorexit, Runtime1::monitorexit(JavaThread* current, BasicObjectLock* lock))
 791   assert(current == JavaThread::current(), "pre-condition");
 792 #ifndef PRODUCT
 793   if (PrintC1Statistics) {
 794     _monitorexit_slowcase_cnt++;
 795   }
 796 #endif
 797   assert(current->last_Java_sp(), "last_Java_sp must be set");
 798   oop obj = lock->obj();
 799   assert(oopDesc::is_oop(obj), "must be null or an object");
 800   SharedRuntime::monitor_exit_helper(obj, lock->lock(), current);
 801 JRT_END
 802 
 803 // Cf. OptoRuntime::deoptimize_caller_frame
 804 JRT_ENTRY_PROF(void, Runtime1, deoptimize, Runtime1::deoptimize(JavaThread* current, jint trap_request))
 805   // Called from within the owner thread, so no need for safepoint
 806   RegisterMap reg_map(current,
 807                       RegisterMap::UpdateMap::skip,
 808                       RegisterMap::ProcessFrames::include,
 809                       RegisterMap::WalkContinuation::skip);
 810   frame stub_frame = current->last_frame();
 811   assert(stub_frame.is_runtime_frame(), "Sanity check");
 812   frame caller_frame = stub_frame.sender(&reg_map);
 813   nmethod* nm = caller_frame.cb()->as_nmethod_or_null();
 814   assert(nm != nullptr, "Sanity check");
 815   methodHandle method(current, nm->method());
 816   assert(nm == CodeCache::find_nmethod(caller_frame.pc()), "Should be the same");
 817   Deoptimization::DeoptAction action = Deoptimization::trap_request_action(trap_request);
 818   Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(trap_request);
 819 
 820   if (action == Deoptimization::Action_make_not_entrant) {
 821     if (nm->make_not_entrant(nmethod::InvalidationReason::C1_DEOPTIMIZE)) {
 822       if (reason == Deoptimization::Reason_tenured) {
 823         MethodData* trap_mdo = Deoptimization::get_method_data(current, method, true /*create_if_missing*/);
 824         if (trap_mdo != nullptr) {

 827       }
 828     }
 829   }
 830 
 831   // Deoptimize the caller frame.
 832   Deoptimization::deoptimize_frame(current, caller_frame.id());
 833   // Return to the now deoptimized frame.
 834 JRT_END
 835 
 836 
 837 #ifndef DEOPTIMIZE_WHEN_PATCHING
 838 
 839 static Klass* resolve_field_return_klass(const methodHandle& caller, int bci, TRAPS) {
 840   Bytecode_field field_access(caller, bci);
 841   // This can be static or non-static field access
 842   Bytecodes::Code code       = field_access.code();
 843 
 844   // We must load class, initialize class and resolve the field
 845   fieldDescriptor result; // initialize class if needed
 846   constantPoolHandle constants(THREAD, caller->constants());
 847   LinkResolver::resolve_field_access(result, constants, field_access.index(), caller,
 848                                      Bytecodes::java_code(code), true /*initialize_class*/, CHECK_NULL);
 849   return result.field_holder();
 850 }
 851 
 852 
 853 //
 854 // This routine patches sites where a class wasn't loaded or
 855 // initialized at the time the code was generated.  It handles
 856 // references to classes, fields and forcing of initialization.  Most
 857 // of the cases are straightforward and involving simply forcing
 858 // resolution of a class, rewriting the instruction stream with the
 859 // needed constant and replacing the call in this function with the
 860 // patched code.  The case for static field is more complicated since
 861 // the thread which is in the process of initializing a class can
 862 // access it's static fields but other threads can't so the code
 863 // either has to deoptimize when this case is detected or execute a
 864 // check that the current thread is the initializing thread.  The
 865 // current
 866 //
 867 // Patches basically look like this:
 868 //

 931 // always end up with a correct outcome.  This is easiest if there are
 932 // few or no intermediate states.  (Some inline caches have two
 933 // related instructions that must be patched in tandem.  For those,
 934 // intermediate states seem to be unavoidable, but we will get the
 935 // right answer from all possible observation orders.)
 936 //
 937 // When patching the entry instruction at the head of a method, or a
 938 // linkable call instruction inside of a method, we try very hard to
 939 // use a patch sequence which executes as a single memory transaction.
 940 // This means, in practice, that when thread A patches an instruction,
 941 // it should patch a 32-bit or 64-bit word that somehow overlaps the
 942 // instruction or is contained in it.  We believe that memory hardware
 943 // will never break up such a word write, if it is naturally aligned
 944 // for the word being written.  We also know that some CPUs work very
 945 // hard to create atomic updates even of naturally unaligned words,
 946 // but we don't want to bet the farm on this always working.
 947 //
 948 // Therefore, if there is any chance of a race condition, we try to
 949 // patch only naturally aligned words, as single, full-word writes.
 950 
 951 JRT_ENTRY_PROF(void, Runtime1, patch_code, Runtime1::patch_code(JavaThread* current, StubId stub_id))
 952 #ifndef PRODUCT
 953   if (PrintC1Statistics) {
 954     _patch_code_slowcase_cnt++;
 955   }
 956 #endif
 957 
 958   ResourceMark rm(current);
 959   RegisterMap reg_map(current,
 960                       RegisterMap::UpdateMap::skip,
 961                       RegisterMap::ProcessFrames::include,
 962                       RegisterMap::WalkContinuation::skip);
 963   frame runtime_frame = current->last_frame();
 964   frame caller_frame = runtime_frame.sender(&reg_map);
 965 
 966   // last java frame on stack
 967   vframeStream vfst(current, true);
 968   assert(!vfst.at_end(), "Java frame must exist");
 969 
 970   methodHandle caller_method(current, vfst.method());
 971   // Note that caller_method->code() may not be same as caller_code because of OSR's

 976   Bytecodes::Code code = caller_method()->java_code_at(bci);
 977 
 978   // this is used by assertions in the access_field_patching_id
 979   BasicType patch_field_type = T_ILLEGAL;
 980   bool deoptimize_for_volatile = false;
 981   bool deoptimize_for_atomic = false;
 982   int patch_field_offset = -1;
 983   Klass* init_klass = nullptr; // klass needed by load_klass_patching code
 984   Klass* load_klass = nullptr; // klass needed by load_klass_patching code
 985   Handle mirror(current, nullptr); // oop needed by load_mirror_patching code
 986   Handle appendix(current, nullptr); // oop needed by appendix_patching code
 987   bool load_klass_or_mirror_patch_id =
 988     (stub_id == StubId::c1_load_klass_patching_id || stub_id == StubId::c1_load_mirror_patching_id);
 989 
 990   if (stub_id == StubId::c1_access_field_patching_id) {
 991 
 992     Bytecode_field field_access(caller_method, bci);
 993     fieldDescriptor result; // initialize class if needed
 994     Bytecodes::Code code = field_access.code();
 995     constantPoolHandle constants(current, caller_method->constants());
 996     LinkResolver::resolve_field_access(result, constants, field_access.index(), caller_method,
 997                                        Bytecodes::java_code(code), true /*initialize_class*/, CHECK);
 998     patch_field_offset = result.offset();
 999 
1000     // If we're patching a field which is volatile then at compile it
1001     // must not have been know to be volatile, so the generated code
1002     // isn't correct for a volatile reference.  The nmethod has to be
1003     // deoptimized so that the code can be regenerated correctly.
1004     // This check is only needed for access_field_patching since this
1005     // is the path for patching field offsets.  load_klass is only
1006     // used for patching references to oops which don't need special
1007     // handling in the volatile case.
1008 
1009     deoptimize_for_volatile = result.access_flags().is_volatile();
1010 
1011     // If we are patching a field which should be atomic, then
1012     // the generated code is not correct either, force deoptimizing.
1013     // We need to only cover T_LONG and T_DOUBLE fields, as we can
1014     // break access atomicity only for them.
1015 
1016     // Strictly speaking, the deoptimization on 64-bit platforms
1017     // is unnecessary, and T_LONG stores on 32-bit platforms need

1315     switch (code) {
1316       case Bytecodes::_new:
1317       case Bytecodes::_anewarray:
1318       case Bytecodes::_multianewarray:
1319       case Bytecodes::_instanceof:
1320       case Bytecodes::_checkcast: {
1321         Bytecode bc(caller_method(), caller_method->bcp_from(bci));
1322         constantTag tag = caller_method->constants()->tag_at(bc.get_index_u2(code));
1323         if (tag.is_unresolved_klass_in_error()) {
1324           return false; // throws resolution error
1325         }
1326         break;
1327       }
1328 
1329       default: break;
1330     }
1331   }
1332   return true;
1333 }
1334 
1335 PROF_ENTRY(void, Runtime1, patch_code, Runtime1::patch_code(JavaThread* current, StubId stub_id))
1336 #ifndef PRODUCT
1337   if (PrintC1Statistics) {
1338     _patch_code_slowcase_cnt++;
1339   }
1340 #endif
1341 
1342   // Enable WXWrite: the function is called by c1 stub as a runtime function
1343   // (see another implementation above).
1344   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
1345 
1346   if (TracePatching) {
1347     tty->print_cr("Deoptimizing because patch is needed");
1348   }
1349 
1350   RegisterMap reg_map(current,
1351                       RegisterMap::UpdateMap::skip,
1352                       RegisterMap::ProcessFrames::include,
1353                       RegisterMap::WalkContinuation::skip);
1354 
1355   frame runtime_frame = current->last_frame();
1356   frame caller_frame = runtime_frame.sender(&reg_map);
1357   assert(caller_frame.is_compiled_frame(), "Wrong frame type");
1358 
1359   if (is_patching_needed(current, stub_id)) {
1360     // Make sure the nmethod is invalidated, i.e. made not entrant.
1361     nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1362     if (nm != nullptr) {
1363       nm->make_not_entrant(nmethod::InvalidationReason::C1_DEOPTIMIZE_FOR_PATCHING);
1364     }
1365   }
1366 
1367   Deoptimization::deoptimize_frame(current, caller_frame.id());
1368   // Return to the now deoptimized frame.
1369   postcond(caller_is_deopted(current));
1370 PROF_END
1371 
1372 #endif // DEOPTIMIZE_WHEN_PATCHING
1373 
1374 // Entry point for compiled code. We want to patch a nmethod.
1375 // We don't do a normal VM transition here because we want to
1376 // know after the patching is complete and any safepoint(s) are taken
1377 // if the calling nmethod was deoptimized. We do this by calling a
1378 // helper method which does the normal VM transition and when it
1379 // completes we can check for deoptimization. This simplifies the
1380 // assembly code in the cpu directories.
1381 //
1382 int Runtime1::move_klass_patching(JavaThread* current) {
1383 //
1384 // NOTE: we are still in Java
1385 //
1386   DEBUG_ONLY(NoHandleMark nhm;)
1387   {
1388     // Enter VM mode
1389     ResetNoHandleMark rnhm;
1390     patch_code(current, StubId::c1_load_klass_patching_id);

1441 int Runtime1::access_field_patching(JavaThread* current) {
1442   //
1443   // NOTE: we are still in Java
1444   //
1445   // Handles created in this function will be deleted by the
1446   // HandleMarkCleaner in the transition to the VM.
1447   NoHandleMark nhm;
1448   {
1449     // Enter VM mode
1450     ResetNoHandleMark rnhm;
1451     patch_code(current, StubId::c1_access_field_patching_id);
1452   }
1453   // Back in JAVA, use no oops DON'T safepoint
1454 
1455   // Return true if calling code is deoptimized
1456 
1457   return caller_is_deopted(current);
1458 }
1459 
1460 
1461 JRT_LEAF_PROF_NO_THREAD(void, Runtime1, trace_block_entry, Runtime1::trace_block_entry(jint block_id))
1462   // for now we just print out the block id
1463   tty->print("%d ", block_id);
1464 JRT_END
1465 
1466 
1467 JRT_LEAF_PROF_NO_THREAD(int, Runtime1, is_instance_of, Runtime1::is_instance_of(oopDesc* mirror, oopDesc* obj))
1468   // had to return int instead of bool, otherwise there may be a mismatch
1469   // between the C calling convention and the Java one.
1470   // e.g., on x86, GCC may clear only %al when returning a bool false, but
1471   // JVM takes the whole %eax as the return value, which may misinterpret
1472   // the return value as a boolean true.
1473 
1474   assert(mirror != nullptr, "should null-check on mirror before calling");
1475   Klass* k = java_lang_Class::as_Klass(mirror);
1476   return (k != nullptr && obj != nullptr && obj->is_a(k)) ? 1 : 0;
1477 JRT_END
1478 
1479 JRT_ENTRY_PROF(void, Runtime1, predicate_failed_trap, Runtime1::predicate_failed_trap(JavaThread* current))
1480   ResourceMark rm;
1481 
1482   RegisterMap reg_map(current,
1483                       RegisterMap::UpdateMap::skip,
1484                       RegisterMap::ProcessFrames::include,
1485                       RegisterMap::WalkContinuation::skip);
1486   frame runtime_frame = current->last_frame();
1487   frame caller_frame = runtime_frame.sender(&reg_map);
1488 
1489   nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
1490   assert (nm != nullptr, "no more nmethod?");
1491   nm->make_not_entrant(nmethod::InvalidationReason::C1_PREDICATE_FAILED_TRAP);
1492 
1493   methodHandle m(current, nm->method());
1494   MethodData* mdo = m->method_data();
1495 
1496   if (mdo == nullptr && !HAS_PENDING_EXCEPTION) {
1497     // Build an MDO.  Ignore errors like OutOfMemory;
1498     // that simply means we won't have an MDO to update.
1499     Method::build_profiling_method_data(m, THREAD);

1519   }
1520 
1521 
1522   Deoptimization::deoptimize_frame(current, caller_frame.id());
1523 
1524 JRT_END
1525 
1526 // Check exception if AbortVMOnException flag set
1527 JRT_LEAF(void, Runtime1::check_abort_on_vm_exception(oopDesc* ex))
1528   ResourceMark rm;
1529   const char* message = nullptr;
1530   if (ex->is_a(vmClasses::Throwable_klass())) {
1531     oop msg = java_lang_Throwable::message(ex);
1532     if (msg != nullptr) {
1533       message = java_lang_String::as_utf8_string(msg);
1534     }
1535   }
1536   Exceptions::debug_check_abort(ex->klass()->external_name(), message);
1537 JRT_END
1538 
1539 #define DO_COUNTERS(macro) \
1540   macro(Runtime1, new_instance) \
1541   macro(Runtime1, new_type_array) \
1542   macro(Runtime1, new_object_array) \
1543   macro(Runtime1, new_multi_array) \
1544   macro(Runtime1, counter_overflow) \
1545   macro(Runtime1, exception_handler_for_pc_helper) \
1546   macro(Runtime1, monitorenter) \
1547   macro(Runtime1, monitorexit) \
1548   macro(Runtime1, deoptimize) \
1549   macro(Runtime1, is_instance_of) \
1550   macro(Runtime1, predicate_failed_trap) \
1551   macro(Runtime1, patch_code)
1552 
1553 #define INIT_COUNTER(sub, name) \
1554   NEWPERFTICKCOUNTERS(_perf_##sub##_##name##_timer, SUN_CI, #sub "::" #name); \
1555   NEWPERFEVENTCOUNTER(_perf_##sub##_##name##_count, SUN_CI, #sub "::" #name "_count");
1556 
1557 void Runtime1::init_counters() {
1558   assert(CompilerConfig::is_c1_enabled(), "");
1559 
1560   if (UsePerfData) {
1561     EXCEPTION_MARK;
1562 
1563     DO_COUNTERS(INIT_COUNTER)
1564 
1565     if (HAS_PENDING_EXCEPTION) {
1566       vm_exit_during_initialization("Runtime1::init_counters() failed unexpectedly");
1567     }
1568   }
1569 }
1570 #undef INIT_COUNTER
1571 
1572 #define PRINT_COUNTER(sub, name) { \
1573   if (_perf_##sub##_##name##_count != nullptr) {  \
1574     jlong count = _perf_##sub##_##name##_count->get_value(); \
1575     if (count > 0) { \
1576       st->print_cr("  %-50s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #sub "::" #name, \
1577                    _perf_##sub##_##name##_timer->elapsed_counter_value_us(), \
1578                    _perf_##sub##_##name##_timer->thread_counter_value_us(), \
1579                    count); \
1580     }}}
1581 
1582 
1583 void Runtime1::print_counters_on(outputStream* st) {
1584   if (UsePerfData && ProfileRuntimeCalls && CompilerConfig::is_c1_enabled()) {
1585     DO_COUNTERS(PRINT_COUNTER)
1586   } else {
1587     st->print_cr("  Runtime1: no info (%s is disabled)",
1588                  (!CompilerConfig::is_c1_enabled() ? "C1" : (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData")));
1589   }
1590 }
1591 
1592 #undef PRINT_COUNTER
1593 #undef DO_COUNTERS
1594 
1595 #ifndef PRODUCT
1596 void Runtime1::print_statistics_on(outputStream* st) {
1597   st->print_cr("C1 Runtime statistics:");
1598   st->print_cr(" _resolve_invoke_virtual_cnt:     %u", SharedRuntime::_resolve_virtual_ctr);
1599   st->print_cr(" _resolve_invoke_opt_virtual_cnt: %u", SharedRuntime::_resolve_opt_virtual_ctr);
1600   st->print_cr(" _resolve_invoke_static_cnt:      %u", SharedRuntime::_resolve_static_ctr);
1601   st->print_cr(" _handle_wrong_method_cnt:        %u", SharedRuntime::_wrong_method_ctr);
1602   st->print_cr(" _ic_miss_cnt:                    %u", SharedRuntime::_ic_miss_ctr);
1603   st->print_cr(" _generic_arraycopystub_cnt:      %u", _generic_arraycopystub_cnt);
1604   st->print_cr(" _byte_arraycopy_cnt:             %u", _byte_arraycopy_stub_cnt);
1605   st->print_cr(" _short_arraycopy_cnt:            %u", _short_arraycopy_stub_cnt);
1606   st->print_cr(" _int_arraycopy_cnt:              %u", _int_arraycopy_stub_cnt);
1607   st->print_cr(" _long_arraycopy_cnt:             %u", _long_arraycopy_stub_cnt);
1608   st->print_cr(" _oop_arraycopy_cnt:              %u", _oop_arraycopy_stub_cnt);
1609   st->print_cr(" _arraycopy_slowcase_cnt:         %u", _arraycopy_slowcase_cnt);
1610   st->print_cr(" _arraycopy_checkcast_cnt:        %u", _arraycopy_checkcast_cnt);
1611   st->print_cr(" _arraycopy_checkcast_attempt_cnt:%u", _arraycopy_checkcast_attempt_cnt);
1612 
1613   st->print_cr(" _new_type_array_slowcase_cnt:    %u", _new_type_array_slowcase_cnt);
1614   st->print_cr(" _new_object_array_slowcase_cnt:  %u", _new_object_array_slowcase_cnt);
1615   st->print_cr(" _new_instance_slowcase_cnt:      %u", _new_instance_slowcase_cnt);
1616   st->print_cr(" _new_multi_array_slowcase_cnt:   %u", _new_multi_array_slowcase_cnt);
1617   st->print_cr(" _monitorenter_slowcase_cnt:      %u", _monitorenter_slowcase_cnt);
1618   st->print_cr(" _monitorexit_slowcase_cnt:       %u", _monitorexit_slowcase_cnt);
1619   st->print_cr(" _patch_code_slowcase_cnt:        %u", _patch_code_slowcase_cnt);
1620 
1621   st->print_cr(" _throw_range_check_exception_count:            %u:", _throw_range_check_exception_count);
1622   st->print_cr(" _throw_index_exception_count:                  %u:", _throw_index_exception_count);
1623   st->print_cr(" _throw_div0_exception_count:                   %u:", _throw_div0_exception_count);
1624   st->print_cr(" _throw_null_pointer_exception_count:           %u:", _throw_null_pointer_exception_count);
1625   st->print_cr(" _throw_class_cast_exception_count:             %u:", _throw_class_cast_exception_count);
1626   st->print_cr(" _throw_incompatible_class_change_error_count:  %u:", _throw_incompatible_class_change_error_count);
1627   st->print_cr(" _throw_count:                                  %u:", _throw_count);
1628 
1629   SharedRuntime::print_ic_miss_histogram_on(st);
1630   st->cr();
1631 }
1632 #endif // PRODUCT
< prev index next >