< 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 

 699       if (is_java_primitive(bt)) {
 700         assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
 701         return unbox_primitive_value(ciobj, bt);
 702       } else {
 703         assert(ciobj->is_instance(), "should be an instance");
 704         return ciConstant(T_OBJECT, ciobj);
 705       }
 706     }
 707   }
 708 }
 709 
 710 // ------------------------------------------------------------------
 711 // ciEnv::get_constant_by_index_impl
 712 //
 713 // Implementation of get_constant_by_index().
 714 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 715                                              int index, int obj_index,
 716                                              ciInstanceKlass* accessor) {
 717   if (obj_index >= 0) {
 718     ciConstant con = get_resolved_constant(cpool, obj_index);
 719     if (con.is_valid()) {
 720       return con;
 721     }
 722   }
 723   constantTag tag = cpool->tag_at(index);
 724   if (tag.is_int()) {
 725     return ciConstant(T_INT, (jint)cpool->int_at(index));
 726   } else if (tag.is_long()) {
 727     return ciConstant((jlong)cpool->long_at(index));
 728   } else if (tag.is_float()) {
 729     return ciConstant((jfloat)cpool->float_at(index));
 730   } else if (tag.is_double()) {
 731     return ciConstant((jdouble)cpool->double_at(index));
 732   } else if (tag.is_string()) {
 733     EXCEPTION_CONTEXT;
 734     assert(obj_index >= 0, "should have an object index");
 735     oop string = cpool->string_at(index, obj_index, THREAD);
 736     if (HAS_PENDING_EXCEPTION) {
 737       CLEAR_PENDING_EXCEPTION;
 738       record_out_of_memory_failure();
 739       return ciConstant();

 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                             bool has_scoped_access,
1036                             int immediate_oops_patched) {


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

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

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






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


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





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
































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


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


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













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


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


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







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

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








































































   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 

 683       if (is_java_primitive(bt)) {
 684         assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity");
 685         return unbox_primitive_value(ciobj, bt);
 686       } else {
 687         assert(ciobj->is_instance(), "should be an instance");
 688         return ciConstant(T_OBJECT, ciobj);
 689       }
 690     }
 691   }
 692 }
 693 
 694 // ------------------------------------------------------------------
 695 // ciEnv::get_constant_by_index_impl
 696 //
 697 // Implementation of get_constant_by_index().
 698 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool,
 699                                              int index, int obj_index,
 700                                              ciInstanceKlass* accessor) {
 701   if (obj_index >= 0) {
 702     ciConstant con = get_resolved_constant(cpool, obj_index);
 703     if (con.should_be_constant()) {
 704       return con;
 705     }
 706   }
 707   constantTag tag = cpool->tag_at(index);
 708   if (tag.is_int()) {
 709     return ciConstant(T_INT, (jint)cpool->int_at(index));
 710   } else if (tag.is_long()) {
 711     return ciConstant((jlong)cpool->long_at(index));
 712   } else if (tag.is_float()) {
 713     return ciConstant((jfloat)cpool->float_at(index));
 714   } else if (tag.is_double()) {
 715     return ciConstant((jdouble)cpool->double_at(index));
 716   } else if (tag.is_string()) {
 717     EXCEPTION_CONTEXT;
 718     assert(obj_index >= 0, "should have an object index");
 719     oop string = cpool->string_at(index, obj_index, THREAD);
 720     if (HAS_PENDING_EXCEPTION) {
 721       CLEAR_PENDING_EXCEPTION;
 722       record_out_of_memory_failure();
 723       return ciConstant();

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

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