< prev index next >

src/hotspot/share/ci/ciEnv.cpp

Print this page

   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 


  25 #include "ci/ciConstant.hpp"
  26 #include "ci/ciEnv.hpp"
  27 #include "ci/ciField.hpp"
  28 #include "ci/ciInstance.hpp"
  29 #include "ci/ciInstanceKlass.hpp"
  30 #include "ci/ciMethod.hpp"
  31 #include "ci/ciNullObject.hpp"
  32 #include "ci/ciReplay.hpp"
  33 #include "ci/ciSymbols.hpp"
  34 #include "ci/ciUtilities.inline.hpp"
  35 #include "classfile/javaClasses.hpp"
  36 #include "classfile/javaClasses.inline.hpp"
  37 #include "classfile/systemDictionary.hpp"
  38 #include "classfile/vmClasses.hpp"
  39 #include "classfile/vmSymbols.hpp"
  40 #include "code/codeCache.hpp"

  41 #include "code/scopeDesc.hpp"
  42 #include "compiler/compilationLog.hpp"
  43 #include "compiler/compilationPolicy.hpp"
  44 #include "compiler/compileBroker.hpp"
  45 #include "compiler/compileLog.hpp"
  46 #include "compiler/compilerEvent.hpp"
  47 #include "compiler/compileTask.hpp"
  48 #include "compiler/disassembler.hpp"

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

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

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

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


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

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


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

 650       if (is_java_primitive(bt)) {
 651         assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
 652         return unbox_primitive_value(ciobj, bt);
 653       } else {
 654         assert(ciobj->is_instance(), "should be an instance");
 655         return ciConstant(T_OBJECT, ciobj);
 656       }
 657     }
 658   }
 659 }
 660 
 661 // ------------------------------------------------------------------
 662 // ciEnv::get_constant_by_index_impl
 663 //
 664 // Implementation of get_constant_by_index().
 665 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 666                                              int index, int obj_index,
 667                                              ciInstanceKlass* accessor) {
 668   if (obj_index >= 0) {
 669     ciConstant con = get_resolved_constant(cpool, obj_index);
 670     if (con.is_valid()) {
 671       return con;
 672     }
 673   }
 674   constantTag tag = cpool->tag_at(index);
 675   if (tag.is_int()) {
 676     return ciConstant(T_INT, (jint)cpool->int_at(index));
 677   } else if (tag.is_long()) {
 678     return ciConstant((jlong)cpool->long_at(index));
 679   } else if (tag.is_float()) {
 680     return ciConstant((jfloat)cpool->float_at(index));
 681   } else if (tag.is_double()) {
 682     return ciConstant((jdouble)cpool->double_at(index));
 683   } else if (tag.is_string()) {
 684     EXCEPTION_CONTEXT;
 685     assert(obj_index >= 0, "should have an object index");
 686     oop string = cpool->string_at(index, obj_index, THREAD);
 687     if (HAS_PENDING_EXCEPTION) {
 688       CLEAR_PENDING_EXCEPTION;
 689       record_out_of_memory_failure();
 690       return ciConstant();

 851       case Bytecodes::_invokevirtual:
 852       case Bytecodes::_invokeinterface:
 853       case Bytecodes::_invokespecial:
 854       case Bytecodes::_invokestatic:
 855         {
 856           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 857           if (m != nullptr) {
 858             return get_method(m);
 859           }
 860         }
 861         break;
 862       default:
 863         break;
 864       }
 865     }
 866 
 867     if (holder_is_accessible) {  // Our declared holder is loaded.
 868       constantTag tag = cpool->tag_ref_at(index, bc);
 869       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 870       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 871       if (m != nullptr &&
 872           (bc == Bytecodes::_invokestatic
 873            ?  m->method_holder()->is_not_initialized()
 874            : !m->method_holder()->is_loaded())) {
 875         m = nullptr;
 876       }
 877       if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
 878         m = nullptr;
 879       }
 880       if (m != nullptr) {
 881         // We found the method.
 882         return get_method(m);
 883       }
 884     }
 885 
 886     // Either the declared holder was not loaded, or the method could
 887     // not be found.  Create a dummy ciMethod to represent the failed
 888     // lookup.
 889     ciSymbol* name      = get_symbol(name_sym);
 890     ciSymbol* signature = get_symbol(sig_sym);
 891     return get_unloaded_method(holder, name, signature, accessor);
 892   }
 893 }
 894 
 895 

 936       _name_buffer =
 937         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 938       _name_buffer_len = req_len;
 939     }
 940   }
 941   return _name_buffer;
 942 }
 943 
 944 // ------------------------------------------------------------------
 945 // ciEnv::is_in_vm
 946 bool ciEnv::is_in_vm() {
 947   return JavaThread::current()->thread_state() == _thread_in_vm;
 948 }
 949 
 950 // ------------------------------------------------------------------
 951 // ciEnv::validate_compile_task_dependencies
 952 //
 953 // Check for changes during compilation (e.g. class loads, evolution,
 954 // breakpoints, call site invalidation).
 955 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
 956   if (failing())  return;  // no need for further checks
 957 
 958   Dependencies::DepType result = dependencies()->validate_dependencies(_task);
 959   if (result != Dependencies::end_marker) {
 960     if (result == Dependencies::call_site_target_value) {
 961       _inc_decompile_count_on_failure = false;
 962       record_failure("call site target change");
 963     } else if (Dependencies::is_klass_type(result)) {
 964       record_failure("concurrent class loading");
 965     } else {
 966       record_failure("invalid non-klass dependency");
 967     }
 968   }
 969 }
 970 
















































































































































































































 971 // ------------------------------------------------------------------
 972 // ciEnv::register_method
 973 void ciEnv::register_method(ciMethod* target,
 974                             int entry_bci,
 975                             CodeOffsets* offsets,
 976                             int orig_pc_offset,
 977                             CodeBuffer* code_buffer,
 978                             int frame_words,
 979                             OopMapSet* oop_map_set,
 980                             ExceptionHandlerTable* handler_table,
 981                             ImplicitExceptionTable* inc_table,
 982                             AbstractCompiler* compiler,


 983                             bool has_unsafe_access,
 984                             bool has_wide_vectors,
 985                             bool has_monitors,
 986                             bool has_scoped_access,
 987                             int immediate_oops_patched) {


 988   VM_ENTRY_MARK;
 989   nmethod* nm = nullptr;
 990   {
 991     methodHandle method(THREAD, target->get_Method());
 992 
 993     // We require method counters to store some method state (max compilation levels) required by the compilation policy.
 994     if (method->get_method_counters(THREAD) == nullptr) {
 995       record_failure("can't create method counters");
 996       // All buffers in the CodeBuffer are allocated in the CodeCache.
 997       // If the code buffer is created on each compile attempt
 998       // as in C2, then it must be freed.
 999       code_buffer->free_blob();
1000       return;
1001     }
1002 
1003     // Check if memory should be freed before allocation
1004     CodeCache::gc_on_allocation();
1005 
1006     // To prevent compile queue updates.
1007     MutexLocker locker(THREAD, MethodCompileQueue_lock);
1008 
1009     // Prevent InstanceKlass::add_to_hierarchy from running
1010     // and invalidating our dependencies until we install this method.
1011     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1012     MutexLocker ml(Compile_lock);
1013     NoSafepointVerifier nsv;
1014 
1015     // Change in Jvmti state may invalidate compilation.
1016     if (!failing() && jvmti_state_changed()) {
1017       record_failure("Jvmti state change invalidated dependencies");
1018     }
1019 
1020     // Change in DTrace flags may invalidate compilation.
1021     if (!failing() &&
1022         ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1023           (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1024       record_failure("DTrace flags change invalidated dependencies");
1025     }
1026 
1027     if (!failing() && target->needs_clinit_barrier() &&
1028         target->holder()->is_in_error_state()) {
1029       record_failure("method holder is in error state");
1030     }
1031 
1032     if (!failing()) {
1033       if (log() != nullptr) {
1034         // Log the dependencies which this compilation declares.
1035         dependencies()->log_all_dependencies();
1036       }
1037 
1038       // Encode the dependencies now, so we can check them right away.
1039       dependencies()->encode_content_bytes();
1040 
1041       // Check for {class loads, evolution, breakpoints, ...} during compilation
1042       validate_compile_task_dependencies(target);
1043     }
1044 
1045     if (failing()) {
1046       // While not a true deoptimization, it is a preemptive decompile.
1047       MethodData* mdo = method()->method_data();
1048       if (mdo != nullptr && _inc_decompile_count_on_failure) {
1049         mdo->inc_decompile_count();
1050       }
1051 
1052       // All buffers in the CodeBuffer are allocated in the CodeCache.
1053       // If the code buffer is created on each compile attempt
1054       // as in C2, then it must be freed.
1055       code_buffer->free_blob();
1056       return;
1057     }
1058 
1059     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1060     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1061 
1062     nm =  nmethod::new_nmethod(method,
1063                                compile_id(),
1064                                entry_bci,
1065                                offsets,
1066                                orig_pc_offset,
1067                                debug_info(), dependencies(), code_buffer,
1068                                frame_words, oop_map_set,
1069                                handler_table, inc_table,
1070                                compiler, CompLevel(task()->comp_level()));
1071 


1072     // Free codeBlobs
1073     code_buffer->free_blob();
1074 
1075     if (nm != nullptr) {
1076       nm->set_has_unsafe_access(has_unsafe_access);
1077       nm->set_has_wide_vectors(has_wide_vectors);
1078       nm->set_has_monitors(has_monitors);
1079       nm->set_has_scoped_access(has_scoped_access);


1080       assert(!method->is_synchronized() || nm->has_monitors(), "");
1081 
1082       if (entry_bci == InvocationEntryBci) {
1083         if (TieredCompilation) {
1084           // If there is an old version we're done with it
1085           nmethod* old = method->code();
1086           if (TraceMethodReplacement && old != nullptr) {
1087             ResourceMark rm;
1088             char *method_name = method->name_and_sig_as_C_string();
1089             tty->print_cr("Replacing method %s", method_name);


1090           }
1091           if (old != nullptr) {
1092             old->make_not_used();
1093           }
1094         }
1095 
1096         LogTarget(Info, nmethod, install) lt;
1097         if (lt.is_enabled()) {
1098           ResourceMark rm;
1099           char *method_name = method->name_and_sig_as_C_string();
1100           lt.print("Installing method (%d) %s ",
1101                     task()->comp_level(), method_name);
1102         }
1103         // Allow the code to be executed
1104         MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1105         if (nm->make_in_use()) {
1106           method->set_code(method, nm);
1107         }
1108       } else {
1109         LogTarget(Info, nmethod, install) lt;
1110         if (lt.is_enabled()) {
1111           ResourceMark rm;
1112           char *method_name = method->name_and_sig_as_C_string();
1113           lt.print("Installing osr method (%d) %s @ %d",
1114                     task()->comp_level(), method_name, entry_bci);
1115         }
1116         MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1117         if (nm->make_in_use()) {
1118           method->method_holder()->add_osr_nmethod(nm);
1119         }
1120       }

1121     }
1122   }
1123 
1124   NoSafepointVerifier nsv;
1125   if (nm != nullptr) {
1126     // Compilation succeeded, post what we know about it
1127     nm->post_compiled_method(task());
1128     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1129   } else {
1130     // The CodeCache is full.
1131     record_failure("code cache is full");


1132   }
1133 
1134   // safepoints are allowed again
1135 }
1136 
1137 // ------------------------------------------------------------------
1138 // ciEnv::find_system_klass
1139 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1140   VM_ENTRY_MARK;
1141   return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1142 }
1143 
1144 // ------------------------------------------------------------------
1145 // ciEnv::comp_level
1146 int ciEnv::comp_level() {
1147   if (task() == nullptr)  return CompilationPolicy::highest_compile_level();
1148   return task()->comp_level();
1149 }
1150 
1151 // ------------------------------------------------------------------
1152 // ciEnv::compile_id
1153 int ciEnv::compile_id() {
1154   if (task() == nullptr)  return 0;
1155   return task()->compile_id();
1156 }
1157 
1158 // ------------------------------------------------------------------
1159 // ciEnv::notice_inlined_method()
1160 void ciEnv::notice_inlined_method(ciMethod* method) {
1161   _num_inlined_bytecodes += method->code_size_for_inlining();







1162 }
1163 
1164 // ------------------------------------------------------------------
1165 // ciEnv::num_inlined_bytecodes()
1166 int ciEnv::num_inlined_bytecodes() const {
1167   return _num_inlined_bytecodes;
1168 }
1169 
1170 // ------------------------------------------------------------------
1171 // ciEnv::record_failure()
1172 void ciEnv::record_failure(const char* reason) {
1173   if (_failure_reason.get() == nullptr) {
1174     // Record the first failure reason.
1175     _failure_reason.set(reason);
1176   }
1177 }
1178 
1179 void ciEnv::report_failure(const char* reason) {
1180   EventCompilationFailure event;
1181   if (event.should_commit()) {

1665         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1666         GUARDED_VM_ENTRY(
1667           MutexLocker ml(Compile_lock);
1668           dump_replay_data_version(&replay_data_stream);
1669           dump_compile_data(&replay_data_stream);
1670         )
1671         replay_data_stream.flush();
1672         tty->print("# Compiler inline data is saved as: ");
1673         tty->print_cr("%s", buffer);
1674       } else {
1675         tty->print_cr("# Can't open file to dump inline data.");
1676         close(fd);
1677       }
1678     }
1679   }
1680 }
1681 
1682 void ciEnv::dump_replay_data_version(outputStream* out) {
1683   out->print_cr("version %d", REPLAY_VERSION);
1684 }









































































   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "cds/archiveBuilder.hpp"
  26 #include "cds/cdsConfig.hpp"
  27 #include "ci/ciConstant.hpp"
  28 #include "ci/ciEnv.hpp"
  29 #include "ci/ciField.hpp"
  30 #include "ci/ciInstance.hpp"
  31 #include "ci/ciInstanceKlass.hpp"
  32 #include "ci/ciMethod.hpp"
  33 #include "ci/ciNullObject.hpp"
  34 #include "ci/ciReplay.hpp"
  35 #include "ci/ciSymbols.hpp"
  36 #include "ci/ciUtilities.inline.hpp"
  37 #include "classfile/javaClasses.hpp"
  38 #include "classfile/javaClasses.inline.hpp"
  39 #include "classfile/systemDictionary.hpp"
  40 #include "classfile/vmClasses.hpp"
  41 #include "classfile/vmSymbols.hpp"
  42 #include "code/codeCache.hpp"
  43 #include "code/SCCache.hpp"
  44 #include "code/scopeDesc.hpp"
  45 #include "compiler/compilationLog.hpp"
  46 #include "compiler/compilationPolicy.hpp"
  47 #include "compiler/compileBroker.hpp"
  48 #include "compiler/compileLog.hpp"
  49 #include "compiler/compilerEvent.hpp"
  50 #include "compiler/compileTask.hpp"
  51 #include "compiler/disassembler.hpp"
  52 #include "gc/shared/barrierSetNMethod.hpp"
  53 #include "gc/shared/collectedHeap.inline.hpp"
  54 #include "interpreter/bytecodeStream.hpp"
  55 #include "interpreter/linkResolver.hpp"
  56 #include "jfr/jfrEvents.hpp"
  57 #include "jvm.h"
  58 #include "logging/log.hpp"
  59 #include "memory/allocation.inline.hpp"
  60 #include "memory/oopFactory.hpp"
  61 #include "memory/resourceArea.hpp"
  62 #include "memory/universe.hpp"
  63 #include "oops/constantPool.inline.hpp"
  64 #include "oops/cpCache.inline.hpp"
  65 #include "oops/method.inline.hpp"
  66 #include "oops/methodData.hpp"
  67 #include "oops/objArrayKlass.hpp"
  68 #include "oops/objArrayOop.inline.hpp"
  69 #include "oops/oop.inline.hpp"
  70 #include "oops/resolvedIndyEntry.hpp"
  71 #include "oops/symbolHandle.hpp"
  72 #include "oops/trainingData.hpp"
  73 #include "prims/jvmtiExport.hpp"
  74 #include "prims/methodHandles.hpp"
  75 #include "runtime/fieldDescriptor.inline.hpp"
  76 #include "runtime/flags/flagSetting.hpp"
  77 #include "runtime/handles.inline.hpp"
  78 #include "runtime/init.hpp"
  79 #include "runtime/javaThread.hpp"
  80 #include "runtime/jniHandles.inline.hpp"
  81 #include "runtime/reflection.hpp"
  82 #include "runtime/safepointVerifiers.hpp"
  83 #include "runtime/sharedRuntime.hpp"
  84 #include "utilities/dtrace.hpp"
  85 #include "utilities/macros.hpp"
  86 #ifdef COMPILER1
  87 #include "c1/c1_Runtime1.hpp"
  88 #endif
  89 #ifdef COMPILER2
  90 #include "opto/runtime.hpp"
  91 #endif
  92 
  93 // ciEnv
  94 //
  95 // This class is the top level broker for requests from the compiler
  96 // to the VM.

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

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

 660       if (is_java_primitive(bt)) {
 661         assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
 662         return unbox_primitive_value(ciobj, bt);
 663       } else {
 664         assert(ciobj->is_instance(), "should be an instance");
 665         return ciConstant(T_OBJECT, ciobj);
 666       }
 667     }
 668   }
 669 }
 670 
 671 // ------------------------------------------------------------------
 672 // ciEnv::get_constant_by_index_impl
 673 //
 674 // Implementation of get_constant_by_index().
 675 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 676                                              int index, int obj_index,
 677                                              ciInstanceKlass* accessor) {
 678   if (obj_index >= 0) {
 679     ciConstant con = get_resolved_constant(cpool, obj_index);
 680     if (con.should_be_constant()) {
 681       return con;
 682     }
 683   }
 684   constantTag tag = cpool->tag_at(index);
 685   if (tag.is_int()) {
 686     return ciConstant(T_INT, (jint)cpool->int_at(index));
 687   } else if (tag.is_long()) {
 688     return ciConstant((jlong)cpool->long_at(index));
 689   } else if (tag.is_float()) {
 690     return ciConstant((jfloat)cpool->float_at(index));
 691   } else if (tag.is_double()) {
 692     return ciConstant((jdouble)cpool->double_at(index));
 693   } else if (tag.is_string()) {
 694     EXCEPTION_CONTEXT;
 695     assert(obj_index >= 0, "should have an object index");
 696     oop string = cpool->string_at(index, obj_index, THREAD);
 697     if (HAS_PENDING_EXCEPTION) {
 698       CLEAR_PENDING_EXCEPTION;
 699       record_out_of_memory_failure();
 700       return ciConstant();

 861       case Bytecodes::_invokevirtual:
 862       case Bytecodes::_invokeinterface:
 863       case Bytecodes::_invokespecial:
 864       case Bytecodes::_invokestatic:
 865         {
 866           Method* m = ConstantPool::method_at_if_loaded(cpool, index);
 867           if (m != nullptr) {
 868             return get_method(m);
 869           }
 870         }
 871         break;
 872       default:
 873         break;
 874       }
 875     }
 876 
 877     if (holder_is_accessible) {  // Our declared holder is loaded.
 878       constantTag tag = cpool->tag_ref_at(index, bc);
 879       assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?");
 880       Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag);
 881       if (m != nullptr) {
 882         ciInstanceKlass* cik = get_instance_klass(m->method_holder());
 883         if ((bc == Bytecodes::_invokestatic && cik->is_not_initialized()) || !cik->is_loaded()) {
 884           m = nullptr;
 885         }
 886       }
 887       if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) {
 888         m = nullptr;
 889       }
 890       if (m != nullptr) {
 891         // We found the method.
 892         return get_method(m);
 893       }
 894     }
 895 
 896     // Either the declared holder was not loaded, or the method could
 897     // not be found.  Create a dummy ciMethod to represent the failed
 898     // lookup.
 899     ciSymbol* name      = get_symbol(name_sym);
 900     ciSymbol* signature = get_symbol(sig_sym);
 901     return get_unloaded_method(holder, name, signature, accessor);
 902   }
 903 }
 904 
 905 

 946       _name_buffer =
 947         (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len);
 948       _name_buffer_len = req_len;
 949     }
 950   }
 951   return _name_buffer;
 952 }
 953 
 954 // ------------------------------------------------------------------
 955 // ciEnv::is_in_vm
 956 bool ciEnv::is_in_vm() {
 957   return JavaThread::current()->thread_state() == _thread_in_vm;
 958 }
 959 
 960 // ------------------------------------------------------------------
 961 // ciEnv::validate_compile_task_dependencies
 962 //
 963 // Check for changes during compilation (e.g. class loads, evolution,
 964 // breakpoints, call site invalidation).
 965 void ciEnv::validate_compile_task_dependencies(ciMethod* target) {
 966   assert(!failing(), "should not call this when failing");
 967 
 968   Dependencies::DepType result = dependencies()->validate_dependencies(_task);
 969   if (result != Dependencies::end_marker) {
 970     if (result == Dependencies::call_site_target_value) {
 971       _inc_decompile_count_on_failure = false;
 972       record_failure("call site target change");
 973     } else if (Dependencies::is_klass_type(result)) {
 974       record_failure("concurrent class loading");
 975     } else {
 976       record_failure("invalid non-klass dependency");
 977     }
 978   }
 979 }
 980 
 981 // scc_entry != nullptr implies loading compiled code from AOT code cache
 982 bool ciEnv::is_compilation_valid(JavaThread* thread, ciMethod* target, bool preload, bool install_code, CodeBuffer* code_buffer, SCCEntry* scc_entry) {
 983   methodHandle method(thread, target->get_Method());
 984 
 985   // We require method counters to store some method state (max compilation levels) required by the compilation policy.
 986   if (!preload && method->get_method_counters(thread) == nullptr) {
 987     record_failure("can't create method counters");
 988     if (code_buffer != nullptr) {
 989       code_buffer->free_blob();
 990     }
 991     return false;
 992   }
 993 
 994   if (scc_entry != nullptr) {
 995     // Invalid compilation states:
 996     //  - SCCache is closed, SCC entry is garbage.
 997     //  - SCC entry indicates this shared code was marked invalid while it was loaded.
 998     if (!SCCache::is_on() || scc_entry->not_entrant()) {
 999       return false;
1000     }
1001   }
1002 
1003   // Change in Jvmti state may invalidate compilation.
1004   if (!failing() && jvmti_state_changed()) {
1005     record_failure("Jvmti state change invalidated dependencies");
1006   }
1007 
1008   // Change in DTrace flags may invalidate compilation.
1009   if (!failing() &&
1010       ( (!dtrace_method_probes() && DTraceMethodProbes) ||
1011         (!dtrace_alloc_probes() && DTraceAllocProbes) )) {
1012     record_failure("DTrace flags change invalidated dependencies");
1013   }
1014 
1015   if (!preload && !failing() && target->needs_clinit_barrier() &&
1016       target->holder()->is_in_error_state()) {
1017     record_failure("method holder is in error state");
1018   }
1019 
1020   if (!failing() && (scc_entry == nullptr)) {
1021     if (log() != nullptr) {
1022       // Log the dependencies which this compilation declares.
1023       dependencies()->log_all_dependencies();
1024     }
1025 
1026     // Encode the dependencies now, so we can check them right away.
1027     dependencies()->encode_content_bytes();
1028   }
1029   // Check for {class loads, evolution, breakpoints, ...} during compilation
1030   if (!failing() && install_code) {
1031     // Check for {class loads, evolution, breakpoints, ...} during compilation
1032     validate_compile_task_dependencies(target);
1033     if (failing() && preload) {
1034       ResourceMark rm;
1035       char *method_name = method->name_and_sig_as_C_string();
1036       log_info(scc)("preload code for '%s' failed dependency check", method_name);
1037     }
1038   }
1039 
1040   if (failing()) {
1041     // While not a true deoptimization, it is a preemptive decompile.
1042     MethodData* mdo = method()->method_data();
1043     if (mdo != nullptr && _inc_decompile_count_on_failure) {
1044       mdo->inc_decompile_count();
1045     }
1046 
1047     if (code_buffer != nullptr) {
1048       code_buffer->free_blob();
1049     }
1050     return false;
1051   }
1052   return true;
1053 }
1054 
1055 void ciEnv::make_code_usable(JavaThread* thread, ciMethod* target, bool preload, int entry_bci, SCCEntry* scc_entry, nmethod* nm) {
1056   methodHandle method(thread, target->get_Method());
1057 
1058   if (entry_bci == InvocationEntryBci) {
1059     if (TieredCompilation) {
1060       // If there is an old version we're done with it
1061       nmethod* old = method->code();
1062       if (TraceMethodReplacement && old != nullptr) {
1063         ResourceMark rm;
1064         char *method_name = method->name_and_sig_as_C_string();
1065         tty->print_cr("Replacing method %s", method_name);
1066       }
1067       if (old != nullptr) {
1068         old->make_not_used();
1069       }
1070     }
1071 
1072     LogTarget(Info, nmethod, install) lt;
1073     if (lt.is_enabled()) {
1074       ResourceMark rm;
1075       char *method_name = method->name_and_sig_as_C_string();
1076       lt.print("Installing method (L%d) %s id=%d scc=%s%s%u",
1077                task()->comp_level(), method_name, compile_id(),
1078                task()->is_scc() ? "A" : "", preload ? "P" : "",
1079                (scc_entry != nullptr ? scc_entry->offset() : 0));
1080     }
1081     // Allow the code to be executed
1082     MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1083     if (nm->make_in_use()) {
1084 #ifdef ASSERT
1085       BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
1086       if (bs_nm != nullptr && bs_nm->supports_entry_barrier(nm)) {
1087         if (!bs_nm->is_armed(nm)) {
1088           log_info(init)("nmethod %d %d not armed", nm->compile_id(), nm->comp_level());
1089         }
1090       }
1091 #endif // ASSERT
1092       if (preload) {
1093         nm->set_preloaded(true);
1094         method->set_preload_code(nm);
1095       }
1096       if (!preload || target->holder()->is_linked()) {
1097         method->set_code(method, nm);
1098       }
1099     }
1100   } else {
1101     LogTarget(Info, nmethod, install) lt;
1102     if (lt.is_enabled()) {
1103       ResourceMark rm;
1104       char *method_name = method->name_and_sig_as_C_string();
1105       lt.print("Installing osr method (L%d) %s @ %d id=%u scc=%s%u",
1106                task()->comp_level(), method_name, entry_bci, compile_id(),
1107                task()->is_scc() ? "A" : "",
1108                (scc_entry != nullptr ? scc_entry->offset() : 0));
1109     }
1110     MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1111     if (nm->make_in_use()) {
1112       method->method_holder()->add_osr_nmethod(nm);
1113     }
1114   }
1115 }
1116 
1117 void ciEnv::register_aot_method(JavaThread* thread,
1118                                 ciMethod* target,
1119                                 AbstractCompiler* compiler,
1120                                 nmethod* archived_nm,
1121                                 address reloc_data,
1122                                 GrowableArray<Handle>& oop_list,
1123                                 GrowableArray<Metadata*>& metadata_list,
1124                                 ImmutableOopMapSet* oopmaps,
1125                                 address immutable_data,
1126                                 GrowableArray<Handle>& reloc_imm_oop_list,
1127                                 GrowableArray<Metadata*>& reloc_imm_metadata_list,
1128 #ifndef PRODUCT
1129                                 AsmRemarks& asm_remarks,
1130                                 DbgStrings& dbg_strings,
1131 #endif /* PRODUCT */
1132                                 SCCReader* scc_reader)
1133 {
1134   SCCEntry* scc_entry = task()->scc_entry();
1135   assert(scc_entry != nullptr, "must be");
1136   nmethod* nm = nullptr;
1137   {
1138     methodHandle method(thread, target->get_Method());
1139     bool preload = task()->preload(); // Code is preloaded before Java method execution
1140 
1141     // Check if memory should be freed before allocation
1142     CodeCache::gc_on_allocation();
1143 
1144     // To prevent compile queue updates.
1145     MutexLocker locker(thread, task()->compile_queue()->lock());
1146 
1147     // Prevent InstanceKlass::add_to_hierarchy from running
1148     // and invalidating our dependencies until we install this method.
1149     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1150     MutexLocker ml(Compile_lock);
1151     NoSafepointVerifier nsv;
1152 
1153     if (!is_compilation_valid(thread, target, preload, true /*install_code*/, nullptr /*code_buffer*/, scc_entry)) {
1154       return;
1155     }
1156 
1157     nm = nmethod::new_nmethod(archived_nm,
1158                               method,
1159                               compiler,
1160                               compile_id(),
1161                               reloc_data,
1162                               oop_list,
1163                               metadata_list,
1164                               oopmaps,
1165                               immutable_data,
1166                               reloc_imm_oop_list,
1167                               reloc_imm_metadata_list,
1168                               NOT_PRODUCT_ARG(asm_remarks)
1169                               NOT_PRODUCT_ARG(dbg_strings)
1170                               scc_reader);
1171 
1172     if (nm != nullptr) {
1173       make_code_usable(thread, target, preload, InvocationEntryBci, scc_entry, nm);
1174     }
1175   }
1176 
1177   NoSafepointVerifier nsv;
1178   if (nm != nullptr) {
1179     // Compilation succeeded, post what we know about it
1180     nm->post_compiled_method(task());
1181     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1182   } else {
1183     // The CodeCache is full.
1184     record_failure("code cache is full");
1185   }
1186   // safepoints are allowed again
1187 }
1188 
1189 // ------------------------------------------------------------------
1190 // ciEnv::register_method
1191 void ciEnv::register_method(ciMethod* target,
1192                             int entry_bci,
1193                             CodeOffsets* offsets,
1194                             int orig_pc_offset,
1195                             CodeBuffer* code_buffer,
1196                             int frame_words,
1197                             OopMapSet* oop_map_set,
1198                             ExceptionHandlerTable* handler_table,
1199                             ImplicitExceptionTable* inc_table,
1200                             AbstractCompiler* compiler,
1201                             bool has_clinit_barriers,
1202                             bool for_preload,
1203                             bool has_unsafe_access,
1204                             bool has_wide_vectors,
1205                             bool has_monitors,
1206                             bool has_scoped_access,
1207                             int immediate_oops_patched,
1208                             bool install_code,
1209                             SCCEntry* scc_entry) {
1210   VM_ENTRY_MARK;
1211   nmethod* nm = nullptr;
1212   {
1213     methodHandle method(THREAD, target->get_Method());
1214     bool preload = task()->preload(); // Code is preloaded before Java method execution









1215 
1216     // Check if memory should be freed before allocation
1217     CodeCache::gc_on_allocation();
1218 
1219     // To prevent compile queue updates.
1220     MutexLocker locker(THREAD, task()->compile_queue()->lock());
1221 
1222     // Prevent InstanceKlass::add_to_hierarchy from running
1223     // and invalidating our dependencies until we install this method.
1224     // No safepoints are allowed. Otherwise, class redefinition can occur in between.
1225     MutexLocker ml(Compile_lock);
1226     NoSafepointVerifier nsv;
1227 
1228     if (!is_compilation_valid(THREAD, target, preload, install_code, code_buffer, scc_entry)) {








































1229       return;
1230     }
1231 
1232     assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry");
1233     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry");
1234 
1235     if (install_code) {
1236       nm =  nmethod::new_nmethod(method,
1237                                  compile_id(),
1238                                  entry_bci,
1239                                  offsets,
1240                                  orig_pc_offset,
1241                                  debug_info(), dependencies(), code_buffer,
1242                                  frame_words, oop_map_set,
1243                                  handler_table, inc_table,
1244                                  compiler, CompLevel(task()->comp_level()),
1245                                  scc_entry);
1246     }
1247     // Free codeBlobs
1248     code_buffer->free_blob();
1249 
1250     if (nm != nullptr) {
1251       nm->set_has_unsafe_access(has_unsafe_access);
1252       nm->set_has_wide_vectors(has_wide_vectors);
1253       nm->set_has_monitors(has_monitors);
1254       nm->set_has_scoped_access(has_scoped_access);
1255       nm->set_preloaded(preload);
1256       nm->set_has_clinit_barriers(has_clinit_barriers);
1257       assert(!method->is_synchronized() || nm->has_monitors(), "");
1258 
1259       if (scc_entry == nullptr) {
1260         scc_entry = SCCache::store_nmethod(nm, compiler, for_preload);
1261         if (scc_entry != nullptr) {
1262           scc_entry->set_inlined_bytecodes(num_inlined_bytecodes());
1263           if (has_clinit_barriers) {
1264             set_scc_clinit_barriers_entry(scc_entry); // Record it
1265             return;
1266           } else if (!for_preload) {
1267             SCCEntry* previous_entry = scc_clinit_barriers_entry();
1268             scc_entry->set_next(previous_entry); // Link it for case of deoptimization
1269           }




























1270         }
1271       }
1272       make_code_usable(THREAD, target, preload, entry_bci, scc_entry, nm);
1273     }
1274   }
1275 
1276   NoSafepointVerifier nsv;
1277   if (nm != nullptr) {
1278     // Compilation succeeded, post what we know about it
1279     nm->post_compiled_method(task());
1280     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1281   } else if (install_code) {
1282     // The CodeCache is full.
1283     record_failure("code cache is full");
1284   } else {
1285     task()->set_num_inlined_bytecodes(num_inlined_bytecodes());
1286   }
1287 
1288   // safepoints are allowed again
1289 }
1290 
1291 // ------------------------------------------------------------------
1292 // ciEnv::find_system_klass
1293 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) {
1294   VM_ENTRY_MARK;
1295   return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false);
1296 }
1297 
1298 // ------------------------------------------------------------------
1299 // ciEnv::comp_level
1300 int ciEnv::comp_level() {
1301   if (task() == nullptr)  return CompilationPolicy::highest_compile_level();
1302   return task()->comp_level();
1303 }
1304 
1305 // ------------------------------------------------------------------
1306 // ciEnv::compile_id
1307 int ciEnv::compile_id() {
1308   if (task() == nullptr)  return 0;
1309   return task()->compile_id();
1310 }
1311 
1312 // ------------------------------------------------------------------
1313 // ciEnv::notice_inlined_method()
1314 void ciEnv::notice_inlined_method(ciMethod* method) {
1315   _num_inlined_bytecodes += method->code_size_for_inlining();
1316   CompileTrainingData* td = task()->training_data();
1317   if (td != nullptr) {
1318     GUARDED_VM_ENTRY({
1319       methodHandle mh(Thread::current(), method->get_Method());
1320       td->notice_inlined_method(task(), mh);
1321     });
1322   }
1323 }
1324 
1325 // ------------------------------------------------------------------
1326 // ciEnv::num_inlined_bytecodes()
1327 int ciEnv::num_inlined_bytecodes() const {
1328   return _num_inlined_bytecodes;
1329 }
1330 
1331 // ------------------------------------------------------------------
1332 // ciEnv::record_failure()
1333 void ciEnv::record_failure(const char* reason) {
1334   if (_failure_reason.get() == nullptr) {
1335     // Record the first failure reason.
1336     _failure_reason.set(reason);
1337   }
1338 }
1339 
1340 void ciEnv::report_failure(const char* reason) {
1341   EventCompilationFailure event;
1342   if (event.should_commit()) {

1826         fileStream replay_data_stream(inline_data_file, /*need_close=*/true);
1827         GUARDED_VM_ENTRY(
1828           MutexLocker ml(Compile_lock);
1829           dump_replay_data_version(&replay_data_stream);
1830           dump_compile_data(&replay_data_stream);
1831         )
1832         replay_data_stream.flush();
1833         tty->print("# Compiler inline data is saved as: ");
1834         tty->print_cr("%s", buffer);
1835       } else {
1836         tty->print_cr("# Can't open file to dump inline data.");
1837         close(fd);
1838       }
1839     }
1840   }
1841 }
1842 
1843 void ciEnv::dump_replay_data_version(outputStream* out) {
1844   out->print_cr("version %d", REPLAY_VERSION);
1845 }
1846 
1847 bool ciEnv::is_precompiled() {
1848   return (task() != nullptr) && (task()->compile_reason() == CompileTask::Reason_Precompile          ||
1849                                  task()->compile_reason() == CompileTask::Reason_PrecompileForPreload);
1850 }
1851 
1852 bool ciEnv::is_fully_initialized(InstanceKlass* ik) {
1853   assert(is_precompiled(), "");
1854   if (task()->method()->method_holder() == ik) {
1855     return true; // FIXME: may be too strong; being_initialized, at least
1856   }
1857   switch (task()->compile_reason()) {
1858     case CompileTask::Reason_Precompile: {
1859       // check init dependencies
1860       MethodTrainingData* mtd = nullptr;
1861       GUARDED_VM_ENTRY(mtd = MethodTrainingData::find(methodHandle(Thread::current(), task()->method())); )
1862       if (mtd != nullptr) {
1863         CompileTrainingData* ctd = mtd->last_toplevel_compile(task()->comp_level());
1864         if (ctd != nullptr) {
1865           for (int i = 0; i < ctd->init_dep_count(); i++) {
1866             KlassTrainingData* ktd = ctd->init_dep(i);
1867             if (ktd->has_holder() && (ktd->holder() == ik)) {
1868               log_trace(precompile)("%d: init_dependency: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1869               return true; // init dependency present
1870             }
1871           }
1872         }
1873       }
1874       return false; // no init dependency
1875     }
1876     case CompileTask::Reason_PrecompileForPreload: {
1877       // FIXME: assumes that all shared classes are initialized
1878       if (ik->is_shared()) {
1879         return true; // class init barriers
1880       }
1881       if (CDSConfig::is_dumping_final_static_archive() && ArchiveBuilder::is_active() &&
1882           ArchiveBuilder::current()->has_been_archived((address)ik)) {
1883         return true; // class init barriers
1884       }
1885       return false;
1886     }
1887     default: fatal("%s", CompileTask::reason_name(task()->compile_reason()));
1888   }
1889   return false;
1890 }
1891 
1892 InstanceKlass::ClassState ciEnv::compute_init_state_for_precompiled(InstanceKlass* ik) {
1893   ASSERT_IN_VM;
1894   assert(is_precompiled(), "");
1895   ResourceMark rm;
1896   if (is_fully_initialized(ik)) {
1897     log_trace(precompile)("%d: fully_initialized: %s", task()->compile_id(), ik->external_name());
1898     return InstanceKlass::ClassState::fully_initialized;
1899   } else if (MetaspaceObj::is_shared(ik)) {
1900     guarantee(ik->is_loaded(), ""); // FIXME: assumes pre-loading by CDS; ik->is_linked() requires pre-linking
1901     log_trace(precompile)("%d: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1902     return ik->init_state(); // not yet initialized
1903   } else if (CDSConfig::is_dumping_final_static_archive() && ArchiveBuilder::is_active()) {
1904     if (!ArchiveBuilder::current()->has_been_archived((address)ik)) {
1905       fatal("New workflow: should not compile code for unarchived class: %s", ik->external_name());
1906     }
1907     guarantee(ik->is_loaded(), "");
1908     log_trace(precompile)("%d: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name());
1909     return ik->init_state(); // not yet initialized
1910   } else {
1911     // Not present in the archive.
1912     fatal("unloaded: %s", ik->external_name());
1913 //    guarantee(SystemDictionaryShared::lookup_init_state(ik) == ik->init_state(), "");
1914     log_trace(precompile)("%d: allocated: %s", task()->compile_id(), ik->external_name());
1915     return InstanceKlass::ClassState::allocated; // not yet linked
1916   }
1917 }
< prev index next >