< prev index next >

src/hotspot/share/ci/ciEnv.cpp

Print this page

   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 "ci/ciConstant.hpp"
  27 #include "ci/ciEnv.hpp"
  28 #include "ci/ciField.hpp"
  29 #include "ci/ciInstance.hpp"
  30 #include "ci/ciInstanceKlass.hpp"
  31 #include "ci/ciMethod.hpp"
  32 #include "ci/ciNullObject.hpp"
  33 #include "ci/ciReplay.hpp"
  34 #include "ci/ciSymbols.hpp"
  35 #include "ci/ciUtilities.inline.hpp"
  36 #include "classfile/javaClasses.hpp"
  37 #include "classfile/javaClasses.inline.hpp"
  38 #include "classfile/systemDictionary.hpp"
  39 #include "classfile/vmClasses.hpp"
  40 #include "classfile/vmSymbols.hpp"
  41 #include "code/codeCache.hpp"
  42 #include "code/scopeDesc.hpp"

  43 #include "compiler/compilationLog.hpp"
  44 #include "compiler/compilationPolicy.hpp"
  45 #include "compiler/compileBroker.hpp"
  46 #include "compiler/compilerEvent.hpp"
  47 #include "compiler/compileLog.hpp"
  48 #include "compiler/compileTask.hpp"
  49 #include "compiler/disassembler.hpp"
  50 #include "gc/shared/collectedHeap.inline.hpp"

  51 #include "interpreter/bytecodeStream.hpp"
  52 #include "interpreter/linkResolver.hpp"
  53 #include "jfr/jfrEvents.hpp"
  54 #include "jvm.h"
  55 #include "logging/log.hpp"
  56 #include "memory/allocation.inline.hpp"
  57 #include "memory/oopFactory.hpp"
  58 #include "memory/resourceArea.hpp"
  59 #include "memory/universe.hpp"
  60 #include "oops/constantPool.inline.hpp"
  61 #include "oops/cpCache.inline.hpp"
  62 #include "oops/method.inline.hpp"
  63 #include "oops/methodData.hpp"
  64 #include "oops/objArrayKlass.hpp"
  65 #include "oops/objArrayOop.inline.hpp"
  66 #include "oops/oop.inline.hpp"
  67 #include "oops/resolvedIndyEntry.hpp"
  68 #include "oops/symbolHandle.hpp"

  69 #include "prims/jvmtiExport.hpp"
  70 #include "prims/methodHandles.hpp"
  71 #include "runtime/fieldDescriptor.inline.hpp"

  72 #include "runtime/handles.inline.hpp"
  73 #include "runtime/init.hpp"
  74 #include "runtime/javaThread.hpp"
  75 #include "runtime/jniHandles.inline.hpp"
  76 #include "runtime/reflection.hpp"
  77 #include "runtime/safepointVerifiers.hpp"
  78 #include "runtime/sharedRuntime.hpp"
  79 #include "utilities/dtrace.hpp"
  80 #include "utilities/macros.hpp"
  81 #ifdef COMPILER1
  82 #include "c1/c1_Runtime1.hpp"
  83 #endif
  84 #ifdef COMPILER2
  85 #include "opto/runtime.hpp"
  86 #endif
  87 
  88 // ciEnv
  89 //
  90 // This class is the top level broker for requests from the compiler
  91 // to the VM.

 154 
 155   oop o = Universe::null_ptr_exception_instance();
 156   assert(o != nullptr, "should have been initialized");
 157   _NullPointerException_instance = get_object(o)->as_instance();
 158   o = Universe::arithmetic_exception_instance();
 159   assert(o != nullptr, "should have been initialized");
 160   _ArithmeticException_instance = get_object(o)->as_instance();
 161 
 162   _ArrayIndexOutOfBoundsException_instance = nullptr;
 163   _ArrayStoreException_instance = nullptr;
 164   _ClassCastException_instance = nullptr;
 165   _the_null_string = nullptr;
 166   _the_min_jint_string = nullptr;
 167 
 168   _jvmti_redefinition_count = 0;
 169   _jvmti_can_hotswap_or_post_breakpoint = false;
 170   _jvmti_can_access_local_variables = false;
 171   _jvmti_can_post_on_exceptions = false;
 172   _jvmti_can_pop_frame = false;
 173 


 174   _dyno_klasses = nullptr;
 175   _dyno_locs = nullptr;
 176   _dyno_name[0] = '\0';
 177 }
 178 
 179 // Record components of a location descriptor string.  Components are appended by the constructor and
 180 // removed by the destructor, like a stack, so scope matters.  These location descriptors are used to
 181 // locate dynamic classes, and terminate at a Method* or oop field associated with dynamic/hidden class.
 182 //
 183 // Example use:
 184 //
 185 // {
 186 //   RecordLocation fp(this, "field1");
 187 //   // location: "field1"
 188 //   { RecordLocation fp(this, " field2"); // location: "field1 field2" }
 189 //   // location: "field1"
 190 //   { RecordLocation fp(this, " field3"); // location: "field1 field3" }
 191 //   // location: "field1"
 192 // }
 193 // // location: ""

 274   // During VM initialization, these instances have not yet been created.
 275   // Assertions ensure that these instances are not accessed before
 276   // their initialization.
 277 
 278   assert(Universe::is_fully_initialized(), "must be");
 279 
 280   _NullPointerException_instance = nullptr;
 281   _ArithmeticException_instance = nullptr;
 282   _ArrayIndexOutOfBoundsException_instance = nullptr;
 283   _ArrayStoreException_instance = nullptr;
 284   _ClassCastException_instance = nullptr;
 285   _the_null_string = nullptr;
 286   _the_min_jint_string = nullptr;
 287 
 288   _jvmti_redefinition_count = 0;
 289   _jvmti_can_hotswap_or_post_breakpoint = false;
 290   _jvmti_can_access_local_variables = false;
 291   _jvmti_can_post_on_exceptions = false;
 292   _jvmti_can_pop_frame = false;
 293 


 294   _dyno_klasses = nullptr;
 295   _dyno_locs = nullptr;
 296 }
 297 
 298 ciEnv::~ciEnv() {
 299   GUARDED_VM_ENTRY(
 300       CompilerThread* current_thread = CompilerThread::current();
 301       _factory->remove_symbols();
 302       // Need safepoint to clear the env on the thread.  RedefineClasses might
 303       // be reading it.
 304       current_thread->set_env(nullptr);
 305   )
 306 }
 307 
 308 // ------------------------------------------------------------------
 309 // Cache Jvmti state
 310 bool ciEnv::cache_jvmti_state() {
 311   VM_ENTRY_MARK;
 312   // Get Jvmti capabilities under lock to get consistent values.
 313   MutexLocker mu(JvmtiThreadState_lock);

 346   if (!_jvmti_can_get_owned_monitor_info &&
 347       JvmtiExport::can_get_owned_monitor_info()) {
 348     return true;
 349   }
 350   if (!_jvmti_can_walk_any_space &&
 351       JvmtiExport::can_walk_any_space()) {
 352     return true;
 353   }
 354 
 355   return false;
 356 }
 357 
 358 // ------------------------------------------------------------------
 359 // Cache DTrace flags
 360 void ciEnv::cache_dtrace_flags() {
 361   // Need lock?
 362   _dtrace_method_probes = DTraceMethodProbes;
 363   _dtrace_alloc_probes  = DTraceAllocProbes;
 364 }
 365 
 366 // ------------------------------------------------------------------
 367 // helper for lazy exception creation
 368 ciInstance* ciEnv::get_or_create_exception(jobject& handle, Symbol* name) {
 369   VM_ENTRY_MARK;
 370   if (handle == nullptr) {
 371     // Cf. universe.cpp, creation of Universe::_null_ptr_exception_instance.
 372     InstanceKlass* ik = SystemDictionary::find_instance_klass(THREAD, name, Handle(), Handle());
 373     jobject objh = nullptr;
 374     if (ik != nullptr) {
 375       oop obj = ik->allocate_instance(THREAD);
 376       if (!HAS_PENDING_EXCEPTION)
 377         objh = JNIHandles::make_global(Handle(THREAD, obj));
 378     }
 379     if (HAS_PENDING_EXCEPTION) {
 380       CLEAR_PENDING_EXCEPTION;
 381     } else {
 382       handle = objh;
 383     }
 384   }
 385   oop obj = JNIHandles::resolve(handle);
 386   return obj == nullptr? nullptr: get_object(obj)->as_instance();
 387 }
 388 
 389 ciInstanceKlass* ciEnv::get_box_klass_for_primitive_type(BasicType type) {
 390   switch (type) {
 391     case T_BOOLEAN: return Boolean_klass();
 392     case T_BYTE   : return Byte_klass();
 393     case T_CHAR   : return Character_klass();
 394     case T_SHORT  : return Short_klass();
 395     case T_INT    : return Integer_klass();
 396     case T_LONG   : return Long_klass();
 397     case T_FLOAT  : return Float_klass();
 398     case T_DOUBLE : return Double_klass();
 399 
 400     default:
 401       assert(false, "not a primitive: %s", type2name(type));
 402       return nullptr;
 403   }
 404 }
 405 
 406 ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() {
 407   if (_ArrayIndexOutOfBoundsException_instance == nullptr) {
 408     _ArrayIndexOutOfBoundsException_instance
 409           = get_or_create_exception(_ArrayIndexOutOfBoundsException_handle,
 410           vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 411   }
 412   return _ArrayIndexOutOfBoundsException_instance;
 413 }
 414 ciInstance* ciEnv::ArrayStoreException_instance() {
 415   if (_ArrayStoreException_instance == nullptr) {
 416     _ArrayStoreException_instance
 417           = get_or_create_exception(_ArrayStoreException_handle,
 418           vmSymbols::java_lang_ArrayStoreException());
 419   }
 420   return _ArrayStoreException_instance;
 421 }
 422 ciInstance* ciEnv::ClassCastException_instance() {
 423   if (_ClassCastException_instance == nullptr) {
 424     _ClassCastException_instance
 425           = get_or_create_exception(_ClassCastException_handle,
 426           vmSymbols::java_lang_ClassCastException());
 427   }
 428   return _ClassCastException_instance;
 429 }
 430 
 431 ciInstance* ciEnv::the_null_string() {
 432   if (_the_null_string == nullptr) {
 433     VM_ENTRY_MARK;
 434     _the_null_string = get_object(Universe::the_null_string())->as_instance();
 435   }
 436   return _the_null_string;
 437 }
 438 
 439 ciInstance* ciEnv::the_min_jint_string() {
 440   if (_the_min_jint_string == nullptr) {
 441     VM_ENTRY_MARK;
 442     _the_min_jint_string = get_object(Universe::the_min_jint_string())->as_instance();
 443   }
 444   return _the_min_jint_string;
 445 }
 446 

 900       case Bytecodes::_invokevirtual:
 901       case Bytecodes::_invokeinterface:
 902       case Bytecodes::_invokespecial:
 903       case Bytecodes::_invokestatic:
 904         {
 905           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 906           if (m != nullptr) {
 907             return get_method(m);
 908           }
 909         }
 910         break;
 911       default:
 912         break;
 913       }
 914     }
 915 
 916     if (holder_is_accessible) {  // Our declared holder is loaded.
 917       constantTag tag = cpool->tag_ref_at(index, bc);
 918       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 919       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 920       if (m != nullptr &&
 921           (bc == Bytecodes::_invokestatic
 922            ?  m->method_holder()->is_not_initialized()
 923            : !m->method_holder()->is_loaded())) {
 924         m = nullptr;
 925       }
 926       if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
 927         m = nullptr;
 928       }
 929       if (m != nullptr) {
 930         // We found the method.
 931         return get_method(m);
 932       }
 933     }
 934 
 935     // Either the declared holder was not loaded, or the method could
 936     // not be found.  Create a dummy ciMethod to represent the failed
 937     // lookup.
 938     ciSymbol* name      = get_symbol(name_sym);
 939     ciSymbol* signature = get_symbol(sig_sym);
 940     return get_unloaded_method(holder, name, signature, accessor);
 941   }
 942 }
 943 
 944 

 985       _name_buffer =
 986         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 987       _name_buffer_len = req_len;
 988     }
 989   }
 990   return _name_buffer;
 991 }
 992 
 993 // ------------------------------------------------------------------
 994 // ciEnv::is_in_vm
 995 bool ciEnv::is_in_vm() {
 996   return JavaThread::current()->thread_state() == _thread_in_vm;
 997 }
 998 
 999 // ------------------------------------------------------------------
1000 // ciEnv::validate_compile_task_dependencies
1001 //
1002 // Check for changes during compilation (e.g. class loads, evolution,
1003 // breakpoints, call site invalidation).
1004 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
1005   if (failing())  return;  // no need for further checks
1006 
1007   Dependencies::DepType result = dependencies()->validate_dependencies(_task);
1008   if (result != Dependencies::end_marker) {
1009     if (result == Dependencies::call_site_target_value) {
1010       _inc_decompile_count_on_failure = false;
1011       record_failure("call site target change");
1012     } else if (Dependencies::is_klass_type(result)) {
1013       record_failure("concurrent class loading");
1014     } else {
1015       record_failure("invalid non-klass dependency");
1016     }
1017   }
1018 }
1019 
1020 // ------------------------------------------------------------------
1021 // ciEnv::register_method
1022 void ciEnv::register_method(ciMethod* target,
1023                             int entry_bci,
1024                             CodeOffsets* offsets,
1025                             int orig_pc_offset,
1026                             CodeBuffer* code_buffer,
1027                             int frame_words,
1028                             OopMapSet* oop_map_set,
1029                             ExceptionHandlerTable* handler_table,
1030                             ImplicitExceptionTable* inc_table,
1031                             AbstractCompiler* compiler,


1032                             bool has_unsafe_access,
1033                             bool has_wide_vectors,
1034                             bool has_monitors,
1035                             int immediate_oops_patched) {


1036   VM_ENTRY_MARK;
1037   nmethod* nm = nullptr;
1038   {
1039     methodHandle method(THREAD, target->get_Method());

1040 
1041     // We require method counters to store some method state (max compilation levels) required by the compilation policy.
1042     if (method->get_method_counters(THREAD) == nullptr) {
1043       record_failure("can't create method counters");
1044       // All buffers in the CodeBuffer are allocated in the CodeCache.
1045       // If the code buffer is created on each compile attempt
1046       // as in C2, then it must be freed.

1047       code_buffer->free_blob();
1048       return;
1049     }
1050 
1051     // Check if memory should be freed before allocation
1052     CodeCache::gc_on_allocation();
1053 
1054     // To prevent compile queue updates.
1055     MutexLocker locker(THREAD, MethodCompileQueue_lock);
1056 
1057     // Prevent InstanceKlass::add_to_hierarchy from running
1058     // and invalidating our dependencies until we install this method.
1059     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1060     MutexLocker ml(Compile_lock);
1061     NoSafepointVerifier nsv;
1062 






1063     // Change in Jvmti state may invalidate compilation.
1064     if (!failing() && jvmti_state_changed()) {
1065       record_failure("Jvmti state change invalidated dependencies");
1066     }
1067 
1068     // Change in DTrace flags may invalidate compilation.
1069     if (!failing() &&
1070         ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1071           (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1072       record_failure("DTrace flags change invalidated dependencies");
1073     }
1074 
1075     if (!failing() && target->needs_clinit_barrier() &&
1076         target->holder()->is_in_error_state()) {
1077       record_failure("method holder is in error state");
1078     }
1079 
1080     if (!failing()) {
1081       if (log() != nullptr) {
1082         // Log the dependencies which this compilation declares.
1083         dependencies()->log_all_dependencies();
1084       }
1085 
1086       // Encode the dependencies now, so we can check them right away.
1087       dependencies()->encode_content_bytes();
1088 


1089       // Check for {class loads, evolution, breakpoints, ...} during compilation
1090       validate_compile_task_dependencies(target);





1091     }
1092 
1093     if (failing()) {
1094       // While not a true deoptimization, it is a preemptive decompile.
1095       MethodData* mdo = method()->method_data();
1096       if (mdo != nullptr && _inc_decompile_count_on_failure) {
1097         mdo->inc_decompile_count();
1098       }
1099 
1100       // All buffers in the CodeBuffer are allocated in the CodeCache.
1101       // If the code buffer is created on each compile attempt
1102       // as in C2, then it must be freed.
1103       code_buffer->free_blob();
1104       return;
1105     }
1106 
1107     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1108     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1109 
1110     nm =  nmethod::new_nmethod(method,
1111                                compile_id(),
1112                                entry_bci,
1113                                offsets,
1114                                orig_pc_offset,
1115                                debug_info(), dependencies(), code_buffer,
1116                                frame_words, oop_map_set,
1117                                handler_table, inc_table,
1118                                compiler, CompLevel(task()->comp_level()));
1119 































1120     // Free codeBlobs
1121     code_buffer->free_blob();
1122 
1123     if (nm != nullptr) {
1124       nm->set_has_unsafe_access(has_unsafe_access);
1125       nm->set_has_wide_vectors(has_wide_vectors);
1126       nm->set_has_monitors(has_monitors);


1127       assert(!method->is_synchronized() || nm->has_monitors(), "");
1128 
1129       if (entry_bci == InvocationEntryBci) {
1130         if (TieredCompilation) {
1131           // If there is an old version we're done with it
1132           nmethod* old = method->code();
1133           if (TraceMethodReplacement && old != nullptr) {
1134             ResourceMark rm;
1135             char *method_name = method->name_and_sig_as_C_string();
1136             tty->print_cr("Replacing method %s", method_name);
1137           }
1138           if (old != nullptr) {
1139             old->make_not_used();
1140           }
1141         }
1142 
1143         LogTarget(Info, nmethod, install) lt;
1144         if (lt.is_enabled()) {
1145           ResourceMark rm;
1146           char *method_name = method->name_and_sig_as_C_string();
1147           lt.print("Installing method (%d) %s ",
1148                     task()->comp_level(), method_name);


1149         }
1150         // Allow the code to be executed
1151         MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1152         if (nm->make_in_use()) {
1153           method->set_code(method, nm);













1154         }
1155       } else {
1156         LogTarget(Info, nmethod, install) lt;
1157         if (lt.is_enabled()) {
1158           ResourceMark rm;
1159           char *method_name = method->name_and_sig_as_C_string();
1160           lt.print("Installing osr method (%d) %s @ %d",
1161                     task()->comp_level(), method_name, entry_bci);


1162         }
1163         MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1164         if (nm->make_in_use()) {
1165           method->method_holder()->add_osr_nmethod(nm);
1166         }
1167       }
1168     }
1169   }
1170 
1171   NoSafepointVerifier nsv;
1172   if (nm != nullptr) {
1173     // Compilation succeeded, post what we know about it
1174     nm->post_compiled_method(task());
1175     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1176   } else {
1177     // The CodeCache is full.
1178     record_failure("code cache is full");


1179   }
1180 
1181   // safepoints are allowed again
1182 }
1183 
1184 // ------------------------------------------------------------------
1185 // ciEnv::find_system_klass
1186 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1187   VM_ENTRY_MARK;
1188   return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1189 }
1190 
1191 // ------------------------------------------------------------------
1192 // ciEnv::comp_level
1193 int ciEnv::comp_level() {
1194   if (task() == nullptr)  return CompilationPolicy::highest_compile_level();
1195   return task()->comp_level();
1196 }
1197 
1198 // ------------------------------------------------------------------
1199 // ciEnv::compile_id
1200 int ciEnv::compile_id() {
1201   if (task() == nullptr)  return 0;
1202   return task()->compile_id();
1203 }
1204 
1205 // ------------------------------------------------------------------
1206 // ciEnv::notice_inlined_method()
1207 void ciEnv::notice_inlined_method(ciMethod* method) {
1208   _num_inlined_bytecodes += method->code_size_for_inlining();







1209 }
1210 
1211 // ------------------------------------------------------------------
1212 // ciEnv::num_inlined_bytecodes()
1213 int ciEnv::num_inlined_bytecodes() const {
1214   return _num_inlined_bytecodes;
1215 }
1216 
1217 // ------------------------------------------------------------------
1218 // ciEnv::record_failure()
1219 void ciEnv::record_failure(const char* reason) {
1220   if (_failure_reason.get() == nullptr) {
1221     // Record the first failure reason.
1222     _failure_reason.set(reason);
1223   }
1224 }
1225 
1226 void ciEnv::report_failure(const char* reason) {
1227   EventCompilationFailure event;
1228   if (event.should_commit()) {

1710         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1711         GUARDED_VM_ENTRY(
1712           MutexLocker ml(Compile_lock);
1713           dump_replay_data_version(&replay_data_stream);
1714           dump_compile_data(&replay_data_stream);
1715         )
1716         replay_data_stream.flush();
1717         tty->print("# Compiler inline data is saved as: ");
1718         tty->print_cr("%s", buffer);
1719       } else {
1720         tty->print_cr("# Can't open file to dump inline data.");
1721         close(fd);
1722       }
1723     }
1724   }
1725 }
1726 
1727 void ciEnv::dump_replay_data_version(outputStream* out) {
1728   out->print_cr("version %d", REPLAY_VERSION);
1729 }








































































   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 "cds/archiveBuilder.hpp"
  27 #include "cds/cdsConfig.hpp"
  28 #include "ci/ciConstant.hpp"
  29 #include "ci/ciEnv.hpp"
  30 #include "ci/ciField.hpp"
  31 #include "ci/ciInstance.hpp"
  32 #include "ci/ciInstanceKlass.hpp"
  33 #include "ci/ciMethod.hpp"
  34 #include "ci/ciNullObject.hpp"
  35 #include "ci/ciReplay.hpp"
  36 #include "ci/ciSymbols.hpp"
  37 #include "ci/ciUtilities.inline.hpp"
  38 #include "classfile/javaClasses.hpp"
  39 #include "classfile/javaClasses.inline.hpp"
  40 #include "classfile/systemDictionary.hpp"
  41 #include "classfile/vmClasses.hpp"
  42 #include "classfile/vmSymbols.hpp"
  43 #include "code/codeCache.hpp"
  44 #include "code/scopeDesc.hpp"
  45 #include "code/SCCache.hpp"
  46 #include "compiler/compilationLog.hpp"
  47 #include "compiler/compilationPolicy.hpp"
  48 #include "compiler/compileBroker.hpp"
  49 #include "compiler/compilerEvent.hpp"
  50 #include "compiler/compileLog.hpp"
  51 #include "compiler/compileTask.hpp"
  52 #include "compiler/disassembler.hpp"
  53 #include "gc/shared/collectedHeap.inline.hpp"
  54 #include "gc/shared/barrierSetNMethod.hpp"
  55 #include "interpreter/bytecodeStream.hpp"
  56 #include "interpreter/linkResolver.hpp"
  57 #include "jfr/jfrEvents.hpp"
  58 #include "jvm.h"
  59 #include "logging/log.hpp"
  60 #include "memory/allocation.inline.hpp"
  61 #include "memory/oopFactory.hpp"
  62 #include "memory/resourceArea.hpp"
  63 #include "memory/universe.hpp"
  64 #include "oops/constantPool.inline.hpp"
  65 #include "oops/cpCache.inline.hpp"
  66 #include "oops/method.inline.hpp"
  67 #include "oops/methodData.hpp"
  68 #include "oops/objArrayKlass.hpp"
  69 #include "oops/objArrayOop.inline.hpp"
  70 #include "oops/oop.inline.hpp"
  71 #include "oops/resolvedIndyEntry.hpp"
  72 #include "oops/symbolHandle.hpp"
  73 #include "oops/trainingData.hpp"
  74 #include "prims/jvmtiExport.hpp"
  75 #include "prims/methodHandles.hpp"
  76 #include "runtime/fieldDescriptor.inline.hpp"
  77 #include "runtime/flags/flagSetting.hpp"
  78 #include "runtime/handles.inline.hpp"
  79 #include "runtime/init.hpp"
  80 #include "runtime/javaThread.hpp"
  81 #include "runtime/jniHandles.inline.hpp"
  82 #include "runtime/reflection.hpp"
  83 #include "runtime/safepointVerifiers.hpp"
  84 #include "runtime/sharedRuntime.hpp"
  85 #include "utilities/dtrace.hpp"
  86 #include "utilities/macros.hpp"
  87 #ifdef COMPILER1
  88 #include "c1/c1_Runtime1.hpp"
  89 #endif
  90 #ifdef COMPILER2
  91 #include "opto/runtime.hpp"
  92 #endif
  93 
  94 // ciEnv
  95 //
  96 // This class is the top level broker for requests from the compiler
  97 // to the VM.

 160 
 161   oop o = Universe::null_ptr_exception_instance();
 162   assert(o != nullptr, "should have been initialized");
 163   _NullPointerException_instance = get_object(o)->as_instance();
 164   o = Universe::arithmetic_exception_instance();
 165   assert(o != nullptr, "should have been initialized");
 166   _ArithmeticException_instance = get_object(o)->as_instance();
 167 
 168   _ArrayIndexOutOfBoundsException_instance = nullptr;
 169   _ArrayStoreException_instance = nullptr;
 170   _ClassCastException_instance = nullptr;
 171   _the_null_string = nullptr;
 172   _the_min_jint_string = nullptr;
 173 
 174   _jvmti_redefinition_count = 0;
 175   _jvmti_can_hotswap_or_post_breakpoint = false;
 176   _jvmti_can_access_local_variables = false;
 177   _jvmti_can_post_on_exceptions = false;
 178   _jvmti_can_pop_frame = false;
 179 
 180   _scc_clinit_barriers_entry = nullptr;
 181 
 182   _dyno_klasses = nullptr;
 183   _dyno_locs = nullptr;
 184   _dyno_name[0] = '\0';
 185 }
 186 
 187 // Record components of a location descriptor string.  Components are appended by the constructor and
 188 // removed by the destructor, like a stack, so scope matters.  These location descriptors are used to
 189 // locate dynamic classes, and terminate at a Method* or oop field associated with dynamic/hidden class.
 190 //
 191 // Example use:
 192 //
 193 // {
 194 //   RecordLocation fp(this, "field1");
 195 //   // location: "field1"
 196 //   { RecordLocation fp(this, " field2"); // location: "field1 field2" }
 197 //   // location: "field1"
 198 //   { RecordLocation fp(this, " field3"); // location: "field1 field3" }
 199 //   // location: "field1"
 200 // }
 201 // // location: ""

 282   // During VM initialization, these instances have not yet been created.
 283   // Assertions ensure that these instances are not accessed before
 284   // their initialization.
 285 
 286   assert(Universe::is_fully_initialized(), "must be");
 287 
 288   _NullPointerException_instance = nullptr;
 289   _ArithmeticException_instance = nullptr;
 290   _ArrayIndexOutOfBoundsException_instance = nullptr;
 291   _ArrayStoreException_instance = nullptr;
 292   _ClassCastException_instance = nullptr;
 293   _the_null_string = nullptr;
 294   _the_min_jint_string = nullptr;
 295 
 296   _jvmti_redefinition_count = 0;
 297   _jvmti_can_hotswap_or_post_breakpoint = false;
 298   _jvmti_can_access_local_variables = false;
 299   _jvmti_can_post_on_exceptions = false;
 300   _jvmti_can_pop_frame = false;
 301 
 302   _scc_clinit_barriers_entry = nullptr;
 303 
 304   _dyno_klasses = nullptr;
 305   _dyno_locs = nullptr;
 306 }
 307 
 308 ciEnv::~ciEnv() {
 309   GUARDED_VM_ENTRY(
 310       CompilerThread* current_thread = CompilerThread::current();
 311       _factory->remove_symbols();
 312       // Need safepoint to clear the env on the thread.  RedefineClasses might
 313       // be reading it.
 314       current_thread->set_env(nullptr);
 315   )
 316 }
 317 
 318 // ------------------------------------------------------------------
 319 // Cache Jvmti state
 320 bool ciEnv::cache_jvmti_state() {
 321   VM_ENTRY_MARK;
 322   // Get Jvmti capabilities under lock to get consistent values.
 323   MutexLocker mu(JvmtiThreadState_lock);

 356   if (!_jvmti_can_get_owned_monitor_info &&
 357       JvmtiExport::can_get_owned_monitor_info()) {
 358     return true;
 359   }
 360   if (!_jvmti_can_walk_any_space &&
 361       JvmtiExport::can_walk_any_space()) {
 362     return true;
 363   }
 364 
 365   return false;
 366 }
 367 
 368 // ------------------------------------------------------------------
 369 // Cache DTrace flags
 370 void ciEnv::cache_dtrace_flags() {
 371   // Need lock?
 372   _dtrace_method_probes = DTraceMethodProbes;
 373   _dtrace_alloc_probes  = DTraceAllocProbes;
 374 }
 375 























 376 ciInstanceKlass* ciEnv::get_box_klass_for_primitive_type(BasicType type) {
 377   switch (type) {
 378     case T_BOOLEAN: return Boolean_klass();
 379     case T_BYTE   : return Byte_klass();
 380     case T_CHAR   : return Character_klass();
 381     case T_SHORT  : return Short_klass();
 382     case T_INT    : return Integer_klass();
 383     case T_LONG   : return Long_klass();
 384     case T_FLOAT  : return Float_klass();
 385     case T_DOUBLE : return Double_klass();
 386 
 387     default:
 388       assert(false, "not a primitive: %s", type2name(type));
 389       return nullptr;
 390   }
 391 }
 392 
 393 ciInstance* ciEnv::ArrayIndexOutOfBoundsException_instance() {
 394   if (_ArrayIndexOutOfBoundsException_instance == nullptr) {
 395     VM_ENTRY_MARK;
 396     _ArrayIndexOutOfBoundsException_instance = get_object(Universe::array_index_oob_exception_instance())->as_instance();

 397   }
 398   return _ArrayIndexOutOfBoundsException_instance;
 399 }
 400 ciInstance* ciEnv::ArrayStoreException_instance() {
 401   if (_ArrayStoreException_instance == nullptr) {
 402     VM_ENTRY_MARK;
 403     _ArrayStoreException_instance = get_object(Universe::array_store_exception_instance())->as_instance();

 404   }
 405   return _ArrayStoreException_instance;
 406 }
 407 ciInstance* ciEnv::ClassCastException_instance() {
 408   if (_ClassCastException_instance == nullptr) {
 409     VM_ENTRY_MARK;
 410     _ClassCastException_instance = get_object(Universe::class_cast_exception_instance())->as_instance();

 411   }
 412   return _ClassCastException_instance;
 413 }
 414 
 415 ciInstance* ciEnv::the_null_string() {
 416   if (_the_null_string == nullptr) {
 417     VM_ENTRY_MARK;
 418     _the_null_string = get_object(Universe::the_null_string())->as_instance();
 419   }
 420   return _the_null_string;
 421 }
 422 
 423 ciInstance* ciEnv::the_min_jint_string() {
 424   if (_the_min_jint_string == nullptr) {
 425     VM_ENTRY_MARK;
 426     _the_min_jint_string = get_object(Universe::the_min_jint_string())->as_instance();
 427   }
 428   return _the_min_jint_string;
 429 }
 430 

 884       case Bytecodes::_invokevirtual:
 885       case Bytecodes::_invokeinterface:
 886       case Bytecodes::_invokespecial:
 887       case Bytecodes::_invokestatic:
 888         {
 889           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 890           if (m != nullptr) {
 891             return get_method(m);
 892           }
 893         }
 894         break;
 895       default:
 896         break;
 897       }
 898     }
 899 
 900     if (holder_is_accessible) {  // Our declared holder is loaded.
 901       constantTag tag = cpool->tag_ref_at(index, bc);
 902       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 903       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 904       if (m != nullptr) {
 905         ciInstanceKlass* cik = get_instance_klass(m->method_holder());
 906         if ((bc == Bytecodes::_invokestatic && cik->is_not_initialized()) || !cik->is_loaded()) {
 907           m = nullptr;
 908         }
 909       }
 910       if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
 911         m = nullptr;
 912       }
 913       if (m != nullptr) {
 914         // We found the method.
 915         return get_method(m);
 916       }
 917     }
 918 
 919     // Either the declared holder was not loaded, or the method could
 920     // not be found.  Create a dummy ciMethod to represent the failed
 921     // lookup.
 922     ciSymbol* name      = get_symbol(name_sym);
 923     ciSymbol* signature = get_symbol(sig_sym);
 924     return get_unloaded_method(holder, name, signature, accessor);
 925   }
 926 }
 927 
 928 

 969       _name_buffer =
 970         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 971       _name_buffer_len = req_len;
 972     }
 973   }
 974   return _name_buffer;
 975 }
 976 
 977 // ------------------------------------------------------------------
 978 // ciEnv::is_in_vm
 979 bool ciEnv::is_in_vm() {
 980   return JavaThread::current()->thread_state() == _thread_in_vm;
 981 }
 982 
 983 // ------------------------------------------------------------------
 984 // ciEnv::validate_compile_task_dependencies
 985 //
 986 // Check for changes during compilation (e.g. class loads, evolution,
 987 // breakpoints, call site invalidation).
 988 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
 989   assert(!failing(), "should not call this when failing");
 990 
 991   Dependencies::DepType result = dependencies()->validate_dependencies(_task);
 992   if (result != Dependencies::end_marker) {
 993     if (result == Dependencies::call_site_target_value) {
 994       _inc_decompile_count_on_failure = false;
 995       record_failure("call site target change");
 996     } else if (Dependencies::is_klass_type(result)) {
 997       record_failure("concurrent class loading");
 998     } else {
 999       record_failure("invalid non-klass dependency");
1000     }
1001   }
1002 }
1003 
1004 // ------------------------------------------------------------------
1005 // ciEnv::register_method
1006 void ciEnv::register_method(ciMethod* target,
1007                             int entry_bci,
1008                             CodeOffsets* offsets,
1009                             int orig_pc_offset,
1010                             CodeBuffer* code_buffer,
1011                             int frame_words,
1012                             OopMapSet* oop_map_set,
1013                             ExceptionHandlerTable* handler_table,
1014                             ImplicitExceptionTable* inc_table,
1015                             AbstractCompiler* compiler,
1016                             bool has_clinit_barriers,
1017                             bool for_preload,
1018                             bool has_unsafe_access,
1019                             bool has_wide_vectors,
1020                             bool has_monitors,
1021                             int immediate_oops_patched,
1022                             bool install_code,
1023                             SCCEntry* scc_entry) {
1024   VM_ENTRY_MARK;
1025   nmethod* nm = nullptr;
1026   {
1027     methodHandle method(THREAD, target->get_Method());
1028     bool preload = task()->preload(); // Code is preloaded before Java method execution
1029 
1030     // We require method counters to store some method state (max compilation levels) required by the compilation policy.
1031     if (!preload && method->get_method_counters(THREAD) == nullptr) {
1032       record_failure("can't create method counters");
1033       // All buffers in the CodeBuffer are allocated in the CodeCache.
1034       // If the code buffer is created on each compile attempt
1035       // as in C2, then it must be freed.
1036       // But keep shared code.
1037       code_buffer->free_blob();
1038       return;
1039     }
1040 
1041     // Check if memory should be freed before allocation
1042     CodeCache::gc_on_allocation();
1043 
1044     // To prevent compile queue updates.
1045     MutexLocker locker(THREAD, MethodCompileQueue_lock);
1046 
1047     // Prevent InstanceKlass::add_to_hierarchy from running
1048     // and invalidating our dependencies until we install this method.
1049     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1050     MutexLocker ml(Compile_lock);
1051     NoSafepointVerifier nsv;
1052 
1053     if (scc_entry != nullptr && scc_entry->not_entrant()) {
1054       // This shared code was marked invalid while it was loaded
1055       code_buffer->free_blob();
1056       return;
1057     }
1058 
1059     // Change in Jvmti state may invalidate compilation.
1060     if (!failing() && jvmti_state_changed()) {
1061       record_failure("Jvmti state change invalidated dependencies");
1062     }
1063 
1064     // Change in DTrace flags may invalidate compilation.
1065     if (!failing() &&
1066         ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1067           (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1068       record_failure("DTrace flags change invalidated dependencies");
1069     }
1070 
1071     if (!preload && !failing() && target->needs_clinit_barrier() &&
1072         target->holder()->is_in_error_state()) {
1073       record_failure("method holder is in error state");
1074     }
1075 
1076     if (!failing() && (scc_entry == nullptr)) {
1077       if (log() != nullptr) {
1078         // Log the dependencies which this compilation declares.
1079         dependencies()->log_all_dependencies();
1080       }
1081 
1082       // Encode the dependencies now, so we can check them right away.
1083       dependencies()->encode_content_bytes();
1084     }
1085     // Check for {class loads, evolution, breakpoints, ...} during compilation
1086     if (!failing() && install_code) {
1087       // Check for {class loads, evolution, breakpoints, ...} during compilation
1088       validate_compile_task_dependencies(target);
1089       if (failing() && preload) {
1090         ResourceMark rm;
1091         char *method_name = method->name_and_sig_as_C_string();
1092         log_info(scc)("preload code for '%s' failed dependency check", method_name);
1093       }
1094     }
1095 
1096     if (failing()) {
1097       // While not a true deoptimization, it is a preemptive decompile.
1098       MethodData* mdo = method()->method_data();
1099       if (mdo != nullptr && _inc_decompile_count_on_failure) {
1100         mdo->inc_decompile_count();
1101       }
1102 
1103       // All buffers in the CodeBuffer are allocated in the CodeCache.
1104       // If the code buffer is created on each compile attempt
1105       // as in C2, then it must be freed.
1106       code_buffer->free_blob();
1107       return;
1108     }
1109 
1110     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1111     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1112 
1113     if (scc_entry == nullptr) {
1114       scc_entry = SCCache::store_nmethod(method,
1115                              compile_id(),
1116                              entry_bci,
1117                              offsets,
1118                              orig_pc_offset,
1119                              debug_info(), dependencies(), code_buffer,
1120                              frame_words, oop_map_set,
1121                              handler_table, inc_table,
1122                              compiler,
1123                              CompLevel(task()->comp_level()),
1124                              has_clinit_barriers,
1125                              for_preload,
1126                              has_unsafe_access,
1127                              has_wide_vectors,
1128                              has_monitors);
1129       if (scc_entry != nullptr) {
1130         scc_entry->set_inlined_bytecodes(num_inlined_bytecodes());
1131         if (has_clinit_barriers) {
1132           set_scc_clinit_barriers_entry(scc_entry); // Record it
1133           // Build second version of code without class initialization barriers
1134           code_buffer->free_blob();
1135           return;
1136         } else if (!for_preload) {
1137           SCCEntry* previous_entry = scc_clinit_barriers_entry();
1138           scc_entry->set_next(previous_entry); // Link it for case of deoptimization
1139         }
1140       }
1141     }
1142     if (install_code) {
1143       nm =  nmethod::new_nmethod(method,
1144                                  compile_id(),
1145                                  entry_bci,
1146                                  offsets,
1147                                  orig_pc_offset,
1148                                  debug_info(), dependencies(), code_buffer,
1149                                  frame_words, oop_map_set,
1150                                  handler_table, inc_table,
1151                                  compiler, CompLevel(task()->comp_level()),
1152                                  scc_entry);
1153     }
1154     // Free codeBlobs
1155     code_buffer->free_blob();
1156 
1157     if (nm != nullptr) {
1158       nm->set_has_unsafe_access(has_unsafe_access);
1159       nm->set_has_wide_vectors(has_wide_vectors);
1160       nm->set_has_monitors(has_monitors);
1161       nm->set_preloaded(preload);
1162       nm->set_has_clinit_barriers(has_clinit_barriers);
1163       assert(!method->is_synchronized() || nm->has_monitors(), "");
1164 
1165       if (entry_bci == InvocationEntryBci) {
1166         if (TieredCompilation) {
1167           // If there is an old version we're done with it
1168           nmethod* old = method->code();
1169           if (TraceMethodReplacement && old != nullptr) {
1170             ResourceMark rm;
1171             char *method_name = method->name_and_sig_as_C_string();
1172             tty->print_cr("Replacing method %s", method_name);
1173           }
1174           if (old != nullptr) {
1175             old->make_not_used();
1176           }
1177         }
1178 
1179         LogTarget(Info, nmethod, install) lt;
1180         if (lt.is_enabled()) {
1181           ResourceMark rm;
1182           char *method_name = method->name_and_sig_as_C_string();
1183           lt.print("Installing method (L%d) %s id=%d scc=%s%s%u",
1184                     task()->comp_level(), method_name, compile_id(),
1185                     task()->is_scc() ? "A" : "", preload ? "P" : "",
1186                     (scc_entry != nullptr ? scc_entry->offset() : 0));
1187         }
1188         // Allow the code to be executed
1189         MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1190         if (nm->make_in_use()) {
1191 #ifdef ASSERT
1192           BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
1193           if (bs_nm != nullptr && bs_nm->supports_entry_barrier(nm)) {
1194             if (!bs_nm->is_armed(nm)) {
1195               log_info(init)("nmethod %d %d not armed", nm->compile_id(), nm->comp_level());
1196             }
1197           }
1198 #endif // ASSERT
1199           if (preload) {
1200             method->set_preload_code(nm);
1201           }
1202           if (!preload || target->holder()->is_linked()) {
1203             method->set_code(method, nm);
1204           }
1205         }
1206       } else {
1207         LogTarget(Info, nmethod, install) lt;
1208         if (lt.is_enabled()) {
1209           ResourceMark rm;
1210           char *method_name = method->name_and_sig_as_C_string();
1211           lt.print("Installing osr method (L%d) %s @ %d id=%u scc=%s%u",
1212                    task()->comp_level(), method_name, entry_bci, compile_id(),
1213                    task()->is_scc() ? "A" : "",
1214                    (scc_entry != nullptr ? scc_entry->offset() : 0));
1215         }
1216         MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1217         if (nm->make_in_use()) {
1218           method->method_holder()->add_osr_nmethod(nm);
1219         }
1220       }
1221     }
1222   }
1223 
1224   NoSafepointVerifier nsv;
1225   if (nm != nullptr) {
1226     // Compilation succeeded, post what we know about it
1227     nm->post_compiled_method(task());
1228     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1229   } else if (install_code) {
1230     // The CodeCache is full.
1231     record_failure("code cache is full");
1232   } else {
1233     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1234   }
1235 
1236   // safepoints are allowed again
1237 }
1238 
1239 // ------------------------------------------------------------------
1240 // ciEnv::find_system_klass
1241 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1242   VM_ENTRY_MARK;
1243   return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1244 }
1245 
1246 // ------------------------------------------------------------------
1247 // ciEnv::comp_level
1248 int ciEnv::comp_level() {
1249   if (task() == nullptr)  return CompilationPolicy::highest_compile_level();
1250   return task()->comp_level();
1251 }
1252 
1253 // ------------------------------------------------------------------
1254 // ciEnv::compile_id
1255 int ciEnv::compile_id() {
1256   if (task() == nullptr)  return 0;
1257   return task()->compile_id();
1258 }
1259 
1260 // ------------------------------------------------------------------
1261 // ciEnv::notice_inlined_method()
1262 void ciEnv::notice_inlined_method(ciMethod* method) {
1263   _num_inlined_bytecodes += method->code_size_for_inlining();
1264   CompileTrainingData* tdata = task()->training_data();
1265   if (tdata != nullptr) {
1266     GUARDED_VM_ENTRY({
1267       methodHandle mh(Thread::current(), method->get_Method());
1268       tdata->notice_inlined_method(task(), mh);
1269     });
1270   }
1271 }
1272 
1273 // ------------------------------------------------------------------
1274 // ciEnv::num_inlined_bytecodes()
1275 int ciEnv::num_inlined_bytecodes() const {
1276   return _num_inlined_bytecodes;
1277 }
1278 
1279 // ------------------------------------------------------------------
1280 // ciEnv::record_failure()
1281 void ciEnv::record_failure(const char* reason) {
1282   if (_failure_reason.get() == nullptr) {
1283     // Record the first failure reason.
1284     _failure_reason.set(reason);
1285   }
1286 }
1287 
1288 void ciEnv::report_failure(const char* reason) {
1289   EventCompilationFailure event;
1290   if (event.should_commit()) {

1772         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1773         GUARDED_VM_ENTRY(
1774           MutexLocker ml(Compile_lock);
1775           dump_replay_data_version(&replay_data_stream);
1776           dump_compile_data(&replay_data_stream);
1777         )
1778         replay_data_stream.flush();
1779         tty->print("# Compiler inline data is saved as: ");
1780         tty->print_cr("%s", buffer);
1781       } else {
1782         tty->print_cr("# Can't open file to dump inline data.");
1783         close(fd);
1784       }
1785     }
1786   }
1787 }
1788 
1789 void ciEnv::dump_replay_data_version(outputStream* out) {
1790   out->print_cr("version %d", REPLAY_VERSION);
1791 }
1792 
1793 bool ciEnv::is_precompiled() {
1794   return (task() != nullptr) && (task()->compile_reason() == CompileTask::Reason_Precompile          ||
1795                                  task()->compile_reason() == CompileTask::Reason_PrecompileForPreload);
1796 }
1797 
1798 bool ciEnv::is_fully_initialized(InstanceKlass* ik) {
1799   assert(is_precompiled(), "");
1800   if (task()->method()->method_holder() == ik) {
1801     return true; // FIXME: may be too strong; being_initialized, at least
1802   }
1803   switch (task()->compile_reason()) {
1804     case CompileTask::Reason_Precompile: {
1805       // check init dependencies
1806       MethodTrainingData* mtd = TrainingData::lookup_for(task()->method());
1807       if (mtd != nullptr) {
1808         CompileTrainingData* ctd = mtd->last_toplevel_compile(task()->comp_level());
1809         if (ctd != nullptr) {
1810           for (int i = 0; i < ctd->init_dep_count(); i++) {
1811             KlassTrainingData* ktd = ctd->init_dep(i);
1812             if (ktd->has_holder() && (ktd->holder() == ik)) {
1813               log_trace(precompile)("%d: init_dependency: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1814               return true; // init dependency present
1815             }
1816           }
1817         }
1818       }
1819       return false; // no init dependency
1820     }
1821     case CompileTask::Reason_PrecompileForPreload: {
1822       // FIXME: assumes that all shared classes are initialized
1823       if (ik->is_shared()) {
1824         return true; // class init barriers
1825       }
1826       if (CDSConfig::is_dumping_final_static_archive() && ArchiveBuilder::is_active() &&
1827           ArchiveBuilder::current()->has_been_archived((address)ik)) {
1828         return true; // class init barriers
1829       }
1830       return false;
1831     }
1832     default: fatal("%s", CompileTask::reason_name(task()->compile_reason()));
1833   }
1834   return false;
1835 }
1836 
1837 InstanceKlass::ClassState ciEnv::compute_init_state_for_precompiled(InstanceKlass* ik) {
1838   ASSERT_IN_VM;
1839   assert(is_precompiled(), "");
1840   ResourceMark rm;
1841   if (is_fully_initialized(ik)) {
1842     log_trace(precompile)("%d: fully_initialized: %s", task()->compile_id(), ik->external_name());
1843     return InstanceKlass::ClassState::fully_initialized;
1844   } else if (MetaspaceObj::is_shared(ik)) {
1845     guarantee(ik->is_loaded(), ""); // FIXME: assumes pre-loading by CDS; ik->is_linked() requires pre-linking
1846     log_trace(precompile)("%d: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1847     return ik->init_state(); // not yet initialized
1848   } else if (CDSConfig::is_dumping_final_static_archive() && ArchiveBuilder::is_active()) {
1849     if (!ArchiveBuilder::current()->has_been_archived((address)ik)) {
1850       fatal("New workflow: should not compile code for unarchived class: %s", ik->external_name());
1851     }
1852     guarantee(ik->is_loaded(), "");
1853     log_trace(precompile)("%d: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1854     return ik->init_state(); // not yet initialized
1855   } else {
1856     // Not present in the archive.
1857     fatal("unloaded: %s", ik->external_name());
1858 //    guarantee(SystemDictionaryShared::lookup_init_state(ik) == ik->init_state(), "");
1859     log_trace(precompile)("%d: allocated: %s", task()->compile_id(), ik->external_name());
1860     return InstanceKlass::ClassState::allocated; // not yet linked
1861   }
1862 }
< prev index next >