1 /* 2 * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "cds/aotCacheAccess.hpp" 26 #include "cds/aotConstantPoolResolver.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/aotCodeCache.hpp" 44 #include "code/codeCache.hpp" 45 #include "code/scopeDesc.hpp" 46 #include "compiler/compilationLog.hpp" 47 #include "compiler/compilationPolicy.hpp" 48 #include "compiler/compileBroker.hpp" 49 #include "compiler/compileLog.hpp" 50 #include "compiler/compilerEvent.hpp" 51 #include "compiler/compileTask.hpp" 52 #include "compiler/disassembler.hpp" 53 #include "gc/shared/barrierSetNMethod.hpp" 54 #include "gc/shared/collectedHeap.inline.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. 98 99 ciObject* ciEnv::_null_object_instance; 100 101 #define VM_CLASS_DEFN(name, ignore_s) ciInstanceKlass* ciEnv::_##name = nullptr; 102 VM_CLASSES_DO(VM_CLASS_DEFN) 103 #undef VM_CLASS_DEFN 104 105 ciSymbol* ciEnv::_unloaded_cisymbol = nullptr; 106 ciInstanceKlass* ciEnv::_unloaded_ciinstance_klass = nullptr; 107 ciObjArrayKlass* ciEnv::_unloaded_ciobjarrayklass = nullptr; 108 109 #ifndef PRODUCT 110 static bool firstEnv = true; 111 #endif /* PRODUCT */ 112 113 // ------------------------------------------------------------------ 114 // ciEnv::ciEnv 115 ciEnv::ciEnv(CompileTask* task) 116 : _ciEnv_arena(mtCompiler, Arena::Tag::tag_cienv) { 117 VM_ENTRY_MARK; 118 119 // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc. 120 thread->set_env(this); 121 assert(ciEnv::current() == this, "sanity"); 122 123 _oop_recorder = nullptr; 124 _debug_info = nullptr; 125 _dependencies = nullptr; 126 _inc_decompile_count_on_failure = true; 127 _compilable = MethodCompilable; 128 _break_at_compile = false; 129 _compiler_data = nullptr; 130 #ifndef PRODUCT 131 assert(!firstEnv, "not initialized properly"); 132 #endif /* !PRODUCT */ 133 134 _num_inlined_bytecodes = 0; 135 assert(task == nullptr || thread->task() == task, "sanity"); 136 if (task != nullptr) { 137 task->mark_started(os::elapsed_counter()); 138 } 139 _task = task; 140 _log = nullptr; 141 142 // Temporary buffer for creating symbols and such. 143 _name_buffer = nullptr; 144 _name_buffer_len = 0; 145 146 _arena = &_ciEnv_arena; 147 _factory = new (_arena) ciObjectFactory(_arena, 128); 148 149 // Preload commonly referenced system ciObjects. 150 151 // During VM initialization, these instances have not yet been created. 152 // Assertions ensure that these instances are not accessed before 153 // their initialization. 154 155 assert(Universe::is_fully_initialized(), "should be complete"); 156 157 oop o = Universe::null_ptr_exception_instance(); 158 assert(o != nullptr, "should have been initialized"); 159 _NullPointerException_instance = get_object(o)->as_instance(); 160 o = Universe::arithmetic_exception_instance(); 161 assert(o != nullptr, "should have been initialized"); 162 _ArithmeticException_instance = get_object(o)->as_instance(); 163 o = Universe::array_index_out_of_bounds_exception_instance(); 164 assert(o != nullptr, "should have been initialized"); 165 _ArrayIndexOutOfBoundsException_instance = get_object(o)->as_instance(); 166 o = Universe::array_store_exception_instance(); 167 assert(o != nullptr, "should have been initialized"); 168 _ArrayStoreException_instance = get_object(o)->as_instance(); 169 o = Universe::class_cast_exception_instance(); 170 assert(o != nullptr, "should have been initialized"); 171 _ClassCastException_instance = get_object(o)->as_instance(); 172 173 _the_null_string = nullptr; 174 _the_min_jint_string = nullptr; 175 176 _jvmti_redefinition_count = 0; 177 _jvmti_can_hotswap_or_post_breakpoint = false; 178 _jvmti_can_access_local_variables = false; 179 _jvmti_can_post_on_exceptions = false; 180 _jvmti_can_pop_frame = false; 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: "" 202 // 203 // Examples of actual locations 204 // @bci compiler/ciReplay/CiReplayBase$TestMain test (I)V 1 <appendix> argL0 ; 205 // // resolve invokedynamic at bci 1 of TestMain.test, then read field "argL0" from appendix 206 // @bci compiler/ciReplay/CiReplayBase$TestMain main ([Ljava/lang/String;)V 0 <appendix> form vmentry <vmtarget> ; 207 // // resolve invokedynamic at bci 0 of TestMain.main, then read field "form.vmentry.method.vmtarget" from appendix 208 // @cpi compiler/ciReplay/CiReplayBase$TestMain 56 form vmentry <vmtarget> ; 209 // // resolve MethodHandle at cpi 56 of TestMain, then read field "vmentry.method.vmtarget" from resolved MethodHandle 210 class RecordLocation { 211 private: 212 char* end; 213 214 ATTRIBUTE_PRINTF(3, 4) 215 void push(ciEnv* ci, const char* fmt, ...) { 216 va_list args; 217 va_start(args, fmt); 218 push_va(ci, fmt, args); 219 va_end(args); 220 } 221 222 public: 223 ATTRIBUTE_PRINTF(3, 0) 224 void push_va(ciEnv* ci, const char* fmt, va_list args) { 225 char *e = ci->_dyno_name + strlen(ci->_dyno_name); 226 char *m = ci->_dyno_name + ARRAY_SIZE(ci->_dyno_name) - 1; 227 (void) os::vsnprintf(e, m - e, fmt, args); 228 assert(strlen(ci->_dyno_name) < (ARRAY_SIZE(ci->_dyno_name) - 1), "overflow"); 229 } 230 231 // append a new component 232 ATTRIBUTE_PRINTF(3, 4) 233 RecordLocation(ciEnv* ci, const char* fmt, ...) { 234 end = ci->_dyno_name + strlen(ci->_dyno_name); 235 va_list args; 236 va_start(args, fmt); 237 push(ci, " "); 238 push_va(ci, fmt, args); 239 va_end(args); 240 } 241 242 // reset to previous state 243 ~RecordLocation() { 244 *end = '\0'; 245 } 246 }; 247 248 ciEnv::ciEnv(Arena* arena) : _ciEnv_arena(mtCompiler, Arena::Tag::tag_cienv) { 249 ASSERT_IN_VM; 250 251 // Set up ciEnv::current immediately, for the sake of ciObjectFactory, etc. 252 CompilerThread* current_thread = CompilerThread::current(); 253 assert(current_thread->env() == nullptr, "must be"); 254 current_thread->set_env(this); 255 assert(ciEnv::current() == this, "sanity"); 256 257 _oop_recorder = nullptr; 258 _debug_info = nullptr; 259 _dependencies = nullptr; 260 _inc_decompile_count_on_failure = true; 261 _compilable = MethodCompilable_never; 262 _break_at_compile = false; 263 _compiler_data = nullptr; 264 #ifndef PRODUCT 265 assert(firstEnv, "must be first"); 266 firstEnv = false; 267 #endif /* !PRODUCT */ 268 269 _num_inlined_bytecodes = 0; 270 _task = nullptr; 271 _log = nullptr; 272 273 // Temporary buffer for creating symbols and such. 274 _name_buffer = nullptr; 275 _name_buffer_len = 0; 276 277 _arena = arena; 278 _factory = new (_arena) ciObjectFactory(_arena, 128); 279 280 // Preload commonly referenced system ciObjects. 281 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 _dyno_klasses = nullptr; 303 _dyno_locs = nullptr; 304 } 305 306 ciEnv::~ciEnv() { 307 GUARDED_VM_ENTRY( 308 CompilerThread* current_thread = CompilerThread::current(); 309 _factory->remove_symbols(); 310 // Need safepoint to clear the env on the thread. RedefineClasses might 311 // be reading it. 312 current_thread->set_env(nullptr); 313 ) 314 } 315 316 // ------------------------------------------------------------------ 317 // Cache Jvmti state 318 bool ciEnv::cache_jvmti_state() { 319 VM_ENTRY_MARK; 320 // Get Jvmti capabilities under lock to get consistent values. 321 MutexLocker mu(JvmtiThreadState_lock); 322 _jvmti_redefinition_count = JvmtiExport::redefinition_count(); 323 _jvmti_can_hotswap_or_post_breakpoint = JvmtiExport::can_hotswap_or_post_breakpoint(); 324 _jvmti_can_access_local_variables = JvmtiExport::can_access_local_variables(); 325 _jvmti_can_post_on_exceptions = JvmtiExport::can_post_on_exceptions(); 326 _jvmti_can_pop_frame = JvmtiExport::can_pop_frame(); 327 _jvmti_can_get_owned_monitor_info = JvmtiExport::can_get_owned_monitor_info(); 328 _jvmti_can_walk_any_space = JvmtiExport::can_walk_any_space(); 329 return _task != nullptr && _task->method()->is_old(); 330 } 331 332 bool ciEnv::jvmti_state_changed() const { 333 // Some classes were redefined 334 if (_jvmti_redefinition_count != JvmtiExport::redefinition_count()) { 335 return true; 336 } 337 338 if (!_jvmti_can_access_local_variables && 339 JvmtiExport::can_access_local_variables()) { 340 return true; 341 } 342 if (!_jvmti_can_hotswap_or_post_breakpoint && 343 JvmtiExport::can_hotswap_or_post_breakpoint()) { 344 return true; 345 } 346 if (!_jvmti_can_post_on_exceptions && 347 JvmtiExport::can_post_on_exceptions()) { 348 return true; 349 } 350 if (!_jvmti_can_pop_frame && 351 JvmtiExport::can_pop_frame()) { 352 return true; 353 } 354 if (!_jvmti_can_get_owned_monitor_info && 355 JvmtiExport::can_get_owned_monitor_info()) { 356 return true; 357 } 358 if (!_jvmti_can_walk_any_space && 359 JvmtiExport::can_walk_any_space()) { 360 return true; 361 } 362 363 return false; 364 } 365 366 // ------------------------------------------------------------------ 367 // Cache DTrace flags 368 void ciEnv::cache_dtrace_flags() { 369 // Need lock? 370 _dtrace_method_probes = DTraceMethodProbes; 371 _dtrace_alloc_probes = DTraceAllocProbes; 372 } 373 374 ciInstanceKlass* ciEnv::get_box_klass_for_primitive_type(BasicType type) { 375 switch (type) { 376 case T_BOOLEAN: return Boolean_klass(); 377 case T_BYTE : return Byte_klass(); 378 case T_CHAR : return Character_klass(); 379 case T_SHORT : return Short_klass(); 380 case T_INT : return Integer_klass(); 381 case T_LONG : return Long_klass(); 382 case T_FLOAT : return Float_klass(); 383 case T_DOUBLE : return Double_klass(); 384 385 default: 386 assert(false, "not a primitive: %s", type2name(type)); 387 return nullptr; 388 } 389 } 390 391 ciInstance* ciEnv::the_null_string() { 392 if (_the_null_string == nullptr) { 393 VM_ENTRY_MARK; 394 _the_null_string = get_object(Universe::the_null_string())->as_instance(); 395 } 396 return _the_null_string; 397 } 398 399 ciInstance* ciEnv::the_min_jint_string() { 400 if (_the_min_jint_string == nullptr) { 401 VM_ENTRY_MARK; 402 _the_min_jint_string = get_object(Universe::the_min_jint_string())->as_instance(); 403 } 404 return _the_min_jint_string; 405 } 406 407 // ------------------------------------------------------------------ 408 // ciEnv::get_method_from_handle 409 ciMethod* ciEnv::get_method_from_handle(Method* method) { 410 VM_ENTRY_MARK; 411 return get_metadata(method)->as_method(); 412 } 413 414 // ------------------------------------------------------------------ 415 // ciEnv::check_klass_accessiblity 416 // 417 // Note: the logic of this method should mirror the logic of 418 // ConstantPool::verify_constant_pool_resolve. 419 bool ciEnv::check_klass_accessibility(ciKlass* accessing_klass, 420 Klass* resolved_klass) { 421 if (accessing_klass == nullptr || !accessing_klass->is_loaded()) { 422 return true; 423 } 424 if (accessing_klass->is_obj_array_klass()) { 425 accessing_klass = accessing_klass->as_obj_array_klass()->base_element_klass(); 426 } 427 if (!accessing_klass->is_instance_klass()) { 428 return true; 429 } 430 431 if (resolved_klass->is_objArray_klass()) { 432 // Find the element klass, if this is an array. 433 resolved_klass = ObjArrayKlass::cast(resolved_klass)->bottom_klass(); 434 } 435 if (resolved_klass->is_instance_klass()) { 436 return (Reflection::verify_class_access(accessing_klass->get_Klass(), 437 InstanceKlass::cast(resolved_klass), 438 true) == Reflection::ACCESS_OK); 439 } 440 return true; 441 } 442 443 // ------------------------------------------------------------------ 444 // ciEnv::get_klass_by_name_impl 445 ciKlass* ciEnv::get_klass_by_name_impl(ciKlass* accessing_klass, 446 const constantPoolHandle& cpool, 447 ciSymbol* name, 448 bool require_local) { 449 ASSERT_IN_VM; 450 Thread* current = Thread::current(); 451 452 // Now we need to check the SystemDictionary 453 Symbol* sym = name->get_symbol(); 454 if (Signature::has_envelope(sym)) { 455 // This is a name from a signature. Strip off the trimmings. 456 // Call recursive to keep scope of strippedsym. 457 TempNewSymbol strippedsym = Signature::strip_envelope(sym); 458 ciSymbol* strippedname = get_symbol(strippedsym); 459 return get_klass_by_name_impl(accessing_klass, cpool, strippedname, require_local); 460 } 461 462 // Check for prior unloaded klass. The SystemDictionary's answers 463 // can vary over time but the compiler needs consistency. 464 ciKlass* unloaded_klass = check_get_unloaded_klass(accessing_klass, name); 465 if (unloaded_klass != nullptr) { 466 if (require_local) return nullptr; 467 return unloaded_klass; 468 } 469 470 Handle loader; 471 if (accessing_klass != nullptr) { 472 loader = Handle(current, accessing_klass->loader()); 473 } 474 475 Klass* found_klass = require_local ? 476 SystemDictionary::find_instance_or_array_klass(current, sym, loader) : 477 SystemDictionary::find_constrained_instance_or_array_klass(current, sym, loader); 478 479 // If we fail to find an array klass, look again for its element type. 480 // The element type may be available either locally or via constraints. 481 // In either case, if we can find the element type in the system dictionary, 482 // we must build an array type around it. The CI requires array klasses 483 // to be loaded if their element klasses are loaded, except when memory 484 // is exhausted. 485 if (Signature::is_array(sym) && 486 (sym->char_at(1) == JVM_SIGNATURE_ARRAY || sym->char_at(1) == JVM_SIGNATURE_CLASS)) { 487 // We have an unloaded array. 488 // Build it on the fly if the element class exists. 489 SignatureStream ss(sym, false); 490 ss.skip_array_prefix(1); 491 // Get element ciKlass recursively. 492 ciKlass* elem_klass = 493 get_klass_by_name_impl(accessing_klass, 494 cpool, 495 get_symbol(ss.as_symbol()), 496 require_local); 497 if (elem_klass != nullptr && elem_klass->is_loaded()) { 498 // Now make an array for it 499 return ciObjArrayKlass::make_impl(elem_klass); 500 } 501 } 502 503 if (found_klass == nullptr && !cpool.is_null() && cpool->has_preresolution()) { 504 // Look inside the constant pool for pre-resolved class entries. 505 for (int i = cpool->length() - 1; i >= 1; i--) { 506 if (cpool->tag_at(i).is_klass()) { 507 Klass* kls = cpool->resolved_klass_at(i); 508 if (kls->name() == sym) { 509 found_klass = kls; 510 break; 511 } 512 } 513 } 514 } 515 516 if (found_klass != nullptr) { 517 // Found it. Build a CI handle. 518 return get_klass(found_klass); 519 } 520 521 if (require_local) return nullptr; 522 523 // Not yet loaded into the VM, or not governed by loader constraints. 524 // Make a CI representative for it. 525 return get_unloaded_klass(accessing_klass, name); 526 } 527 528 // ------------------------------------------------------------------ 529 // ciEnv::get_klass_by_name 530 ciKlass* ciEnv::get_klass_by_name(ciKlass* accessing_klass, 531 ciSymbol* klass_name, 532 bool require_local) { 533 GUARDED_VM_ENTRY(return get_klass_by_name_impl(accessing_klass, 534 constantPoolHandle(), 535 klass_name, 536 require_local);) 537 } 538 539 // ------------------------------------------------------------------ 540 // ciEnv::get_klass_by_index_impl 541 // 542 // Implementation of get_klass_by_index. 543 ciKlass* ciEnv::get_klass_by_index_impl(const constantPoolHandle& cpool, 544 int index, 545 bool& is_accessible, 546 ciInstanceKlass* accessor) { 547 Klass* klass = nullptr; 548 Symbol* klass_name = nullptr; 549 550 if (cpool->tag_at(index).is_symbol()) { 551 klass_name = cpool->symbol_at(index); 552 } else { 553 // Check if it's resolved if it's not a symbol constant pool entry. 554 klass = ConstantPool::klass_at_if_loaded(cpool, index); 555 // Try to look it up by name. 556 if (klass == nullptr) { 557 klass_name = cpool->klass_name_at(index); 558 } 559 } 560 561 if (klass == nullptr) { 562 // Not found in constant pool. Use the name to do the lookup. 563 ciKlass* k = get_klass_by_name_impl(accessor, 564 cpool, 565 get_symbol(klass_name), 566 false); 567 // Calculate accessibility the hard way. 568 if (!k->is_loaded()) { 569 is_accessible = false; 570 } else if (k->loader() != accessor->loader() && 571 get_klass_by_name_impl(accessor, cpool, k->name(), true) == nullptr) { 572 // Loaded only remotely. Not linked yet. 573 is_accessible = false; 574 } else { 575 // Linked locally, and we must also check public/private, etc. 576 is_accessible = check_klass_accessibility(accessor, k->get_Klass()); 577 } 578 return k; 579 } 580 581 // Check for prior unloaded klass. The SystemDictionary's answers 582 // can vary over time but the compiler needs consistency. 583 ciSymbol* name = get_symbol(klass->name()); 584 ciKlass* unloaded_klass = check_get_unloaded_klass(accessor, name); 585 if (unloaded_klass != nullptr) { 586 is_accessible = false; 587 return unloaded_klass; 588 } 589 590 // It is known to be accessible, since it was found in the constant pool. 591 ciKlass* ciKlass = get_klass(klass); 592 is_accessible = true; 593 if (ReplayCompiles && ciKlass == _unloaded_ciinstance_klass) { 594 // Klass was unresolved at replay dump time and therefore not accessible. 595 is_accessible = false; 596 } 597 return ciKlass; 598 } 599 600 // ------------------------------------------------------------------ 601 // ciEnv::get_klass_by_index 602 // 603 // Get a klass from the constant pool. 604 ciKlass* ciEnv::get_klass_by_index(const constantPoolHandle& cpool, 605 int index, 606 bool& is_accessible, 607 ciInstanceKlass* accessor) { 608 GUARDED_VM_ENTRY(return get_klass_by_index_impl(cpool, index, is_accessible, accessor);) 609 } 610 611 // ------------------------------------------------------------------ 612 // ciEnv::unbox_primitive_value 613 // 614 // Unbox a primitive and return it as a ciConstant. 615 ciConstant ciEnv::unbox_primitive_value(ciObject* cibox, BasicType expected_bt) { 616 jvalue value; 617 BasicType bt = java_lang_boxing_object::get_value(cibox->get_oop(), &value); 618 if (bt != expected_bt && expected_bt != T_ILLEGAL) { 619 assert(false, "type mismatch: %s vs %s", type2name(expected_bt), cibox->klass()->name()->as_klass_external_name()); 620 return ciConstant(); 621 } 622 switch (bt) { 623 case T_BOOLEAN: return ciConstant(bt, value.z); 624 case T_BYTE: return ciConstant(bt, value.b); 625 case T_SHORT: return ciConstant(bt, value.s); 626 case T_CHAR: return ciConstant(bt, value.c); 627 case T_INT: return ciConstant(bt, value.i); 628 case T_LONG: return ciConstant(value.j); 629 case T_FLOAT: return ciConstant(value.f); 630 case T_DOUBLE: return ciConstant(value.d); 631 632 default: 633 assert(false, "not a primitive type: %s", type2name(bt)); 634 return ciConstant(); 635 } 636 } 637 638 // ------------------------------------------------------------------ 639 // ciEnv::get_resolved_constant 640 // 641 ciConstant ciEnv::get_resolved_constant(const constantPoolHandle& cpool, int obj_index) { 642 assert(obj_index >= 0, ""); 643 oop obj = cpool->resolved_reference_at(obj_index); 644 if (obj == nullptr) { 645 // Unresolved constant. It is resolved when the corresponding slot contains a non-null reference. 646 // Null constant is represented as a sentinel (non-null) value. 647 return ciConstant(); 648 } else if (obj == Universe::the_null_sentinel()) { 649 return ciConstant(T_OBJECT, get_object(nullptr)); 650 } else { 651 ciObject* ciobj = get_object(obj); 652 if (ciobj->is_array()) { 653 return ciConstant(T_ARRAY, ciobj); 654 } else { 655 int cp_index = cpool->object_to_cp_index(obj_index); 656 BasicType bt = cpool->basic_type_for_constant_at(cp_index); 657 if (is_java_primitive(bt)) { 658 assert(cpool->tag_at(cp_index).is_dynamic_constant(), "sanity"); 659 return unbox_primitive_value(ciobj, bt); 660 } else { 661 assert(ciobj->is_instance(), "should be an instance"); 662 return ciConstant(T_OBJECT, ciobj); 663 } 664 } 665 } 666 } 667 668 // ------------------------------------------------------------------ 669 // ciEnv::get_constant_by_index_impl 670 // 671 // Implementation of get_constant_by_index(). 672 ciConstant ciEnv::get_constant_by_index_impl(const constantPoolHandle& cpool, 673 int index, int obj_index, 674 ciInstanceKlass* accessor) { 675 if (obj_index >= 0) { 676 ciConstant con = get_resolved_constant(cpool, obj_index); 677 if (con.should_be_constant()) { 678 return con; 679 } 680 } 681 constantTag tag = cpool->tag_at(index); 682 if (tag.is_int()) { 683 return ciConstant(T_INT, (jint)cpool->int_at(index)); 684 } else if (tag.is_long()) { 685 return ciConstant((jlong)cpool->long_at(index)); 686 } else if (tag.is_float()) { 687 return ciConstant((jfloat)cpool->float_at(index)); 688 } else if (tag.is_double()) { 689 return ciConstant((jdouble)cpool->double_at(index)); 690 } else if (tag.is_string()) { 691 EXCEPTION_CONTEXT; 692 assert(obj_index >= 0, "should have an object index"); 693 oop string = cpool->string_at(index, obj_index, THREAD); 694 if (HAS_PENDING_EXCEPTION) { 695 CLEAR_PENDING_EXCEPTION; 696 record_out_of_memory_failure(); 697 return ciConstant(); 698 } 699 ciInstance* constant = get_object(string)->as_instance(); 700 return ciConstant(T_OBJECT, constant); 701 } else if (tag.is_unresolved_klass_in_error()) { 702 return ciConstant(T_OBJECT, get_unloaded_klass_mirror(nullptr)); 703 } else if (tag.is_klass() || tag.is_unresolved_klass()) { 704 bool will_link; 705 ciKlass* klass = get_klass_by_index_impl(cpool, index, will_link, accessor); 706 ciInstance* mirror = (will_link ? klass->java_mirror() : get_unloaded_klass_mirror(klass)); 707 return ciConstant(T_OBJECT, mirror); 708 } else if (tag.is_method_type() || tag.is_method_type_in_error()) { 709 // must execute Java code to link this CP entry into cache[i].f1 710 assert(obj_index >= 0, "should have an object index"); 711 ciSymbol* signature = get_symbol(cpool->method_type_signature_at(index)); 712 ciObject* ciobj = get_unloaded_method_type_constant(signature); 713 return ciConstant(T_OBJECT, ciobj); 714 } else if (tag.is_method_handle() || tag.is_method_handle_in_error()) { 715 // must execute Java code to link this CP entry into cache[i].f1 716 assert(obj_index >= 0, "should have an object index"); 717 bool ignore_will_link; 718 int ref_kind = cpool->method_handle_ref_kind_at(index); 719 int callee_index = cpool->method_handle_klass_index_at(index); 720 ciKlass* callee = get_klass_by_index_impl(cpool, callee_index, ignore_will_link, accessor); 721 ciSymbol* name = get_symbol(cpool->method_handle_name_ref_at(index)); 722 ciSymbol* signature = get_symbol(cpool->method_handle_signature_ref_at(index)); 723 ciObject* ciobj = get_unloaded_method_handle_constant(callee, name, signature, ref_kind); 724 return ciConstant(T_OBJECT, ciobj); 725 } else if (tag.is_dynamic_constant() || tag.is_dynamic_constant_in_error()) { 726 assert(obj_index >= 0, "should have an object index"); 727 return ciConstant(T_OBJECT, unloaded_ciinstance()); // unresolved dynamic constant 728 } else { 729 assert(false, "unknown tag: %d (%s)", tag.value(), tag.internal_name()); 730 return ciConstant(); 731 } 732 } 733 734 // ------------------------------------------------------------------ 735 // ciEnv::get_constant_by_index 736 // 737 // Pull a constant out of the constant pool. How appropriate. 738 // 739 // Implementation note: this query is currently in no way cached. 740 ciConstant ciEnv::get_constant_by_index(const constantPoolHandle& cpool, 741 int pool_index, int cache_index, 742 ciInstanceKlass* accessor) { 743 GUARDED_VM_ENTRY(return get_constant_by_index_impl(cpool, pool_index, cache_index, accessor);) 744 } 745 746 // ------------------------------------------------------------------ 747 // ciEnv::get_field_by_index_impl 748 // 749 // Implementation of get_field_by_index. 750 // 751 // Implementation note: the results of field lookups are cached 752 // in the accessor klass. 753 ciField* ciEnv::get_field_by_index_impl(ciInstanceKlass* accessor, 754 int index, Bytecodes::Code bc) { 755 ciConstantPoolCache* cache = accessor->field_cache(); 756 if (cache == nullptr) { 757 ciField* field = new (arena()) ciField(accessor, index, bc); 758 return field; 759 } else { 760 ciField* field = (ciField*)cache->get(index); 761 if (field == nullptr) { 762 field = new (arena()) ciField(accessor, index, bc); 763 cache->insert(index, field); 764 } 765 return field; 766 } 767 } 768 769 // ------------------------------------------------------------------ 770 // ciEnv::get_field_by_index 771 // 772 // Get a field by index from a klass's constant pool. 773 ciField* ciEnv::get_field_by_index(ciInstanceKlass* accessor, 774 int index, Bytecodes::Code bc) { 775 GUARDED_VM_ENTRY(return get_field_by_index_impl(accessor, index, bc);) 776 } 777 778 // ------------------------------------------------------------------ 779 // ciEnv::lookup_method 780 // 781 // Perform an appropriate method lookup based on accessor, holder, 782 // name, signature, and bytecode. 783 Method* ciEnv::lookup_method(ciInstanceKlass* accessor, 784 ciKlass* holder, 785 Symbol* name, 786 Symbol* sig, 787 Bytecodes::Code bc, 788 constantTag tag) { 789 InstanceKlass* accessor_klass = accessor->get_instanceKlass(); 790 Klass* holder_klass = holder->get_Klass(); 791 792 // Accessibility checks are performed in ciEnv::get_method_by_index_impl. 793 assert(check_klass_accessibility(accessor, holder_klass), "holder not accessible"); 794 795 LinkInfo link_info(holder_klass, name, sig, accessor_klass, 796 LinkInfo::AccessCheck::required, 797 LinkInfo::LoaderConstraintCheck::required, 798 tag); 799 switch (bc) { 800 case Bytecodes::_invokestatic: 801 return LinkResolver::resolve_static_call_or_null(link_info); 802 case Bytecodes::_invokespecial: 803 return LinkResolver::resolve_special_call_or_null(link_info); 804 case Bytecodes::_invokeinterface: 805 return LinkResolver::linktime_resolve_interface_method_or_null(link_info); 806 case Bytecodes::_invokevirtual: 807 return LinkResolver::linktime_resolve_virtual_method_or_null(link_info); 808 default: 809 fatal("Unhandled bytecode: %s", Bytecodes::name(bc)); 810 return nullptr; // silence compiler warnings 811 } 812 } 813 814 815 // ------------------------------------------------------------------ 816 // ciEnv::get_method_by_index_impl 817 ciMethod* ciEnv::get_method_by_index_impl(const constantPoolHandle& cpool, 818 int index, Bytecodes::Code bc, 819 ciInstanceKlass* accessor) { 820 assert(cpool.not_null(), "need constant pool"); 821 assert(accessor != nullptr, "need origin of access"); 822 if (bc == Bytecodes::_invokedynamic) { 823 // FIXME: code generation could allow for null (unlinked) call site 824 // The call site could be made patchable as follows: 825 // Load the appendix argument from the constant pool. 826 // Test the appendix argument and jump to a known deopt routine if it is null. 827 // Jump through a patchable call site, which is initially a deopt routine. 828 // Patch the call site to the nmethod entry point of the static compiled lambda form. 829 // As with other two-component call sites, both values must be independently verified. 830 assert(index < cpool->cache()->resolved_indy_entries_length(), "impossible"); 831 ResolvedIndyEntry* indy_info = cpool->resolved_indy_entry_at(index); 832 Method* adapter = indy_info->method(); 833 #if INCLUDE_CDS 834 if (is_precompile() && !AOTConstantPoolResolver::is_resolution_deterministic(cpool(), indy_info->constant_pool_index())) { 835 // This is an indy callsite that was resolved as a side effect of VM bootstrap, but 836 // it cannot be cached in the resolved state, so AOT code should not reference it. 837 adapter = nullptr; 838 } 839 #endif 840 // Resolved if the adapter is non null. 841 if (adapter != nullptr) { 842 return get_method(adapter); 843 } 844 845 // Fake a method that is equivalent to a declared method. 846 ciInstanceKlass* holder = get_instance_klass(vmClasses::MethodHandle_klass()); 847 ciSymbol* name = ciSymbols::invokeBasic_name(); 848 ciSymbol* signature = get_symbol(cpool->signature_ref_at(index, bc)); 849 return get_unloaded_method(holder, name, signature, accessor); 850 } else { 851 const int holder_index = cpool->klass_ref_index_at(index, bc); 852 bool holder_is_accessible; 853 ciKlass* holder = get_klass_by_index_impl(cpool, holder_index, holder_is_accessible, accessor); 854 855 // Get the method's name and signature. 856 Symbol* name_sym = cpool->name_ref_at(index, bc); 857 Symbol* sig_sym = cpool->signature_ref_at(index, bc); 858 859 if (cpool->has_preresolution() 860 || ((holder == ciEnv::MethodHandle_klass() || holder == ciEnv::VarHandle_klass()) && 861 MethodHandles::is_signature_polymorphic_name(holder->get_Klass(), name_sym))) { 862 // Short-circuit lookups for JSR 292-related call sites. 863 // That is, do not rely only on name-based lookups, because they may fail 864 // if the names are not resolvable in the boot class loader (7056328). 865 switch (bc) { 866 case Bytecodes::_invokevirtual: 867 case Bytecodes::_invokeinterface: 868 case Bytecodes::_invokespecial: 869 case Bytecodes::_invokestatic: 870 { 871 Method* m = ConstantPool::method_at_if_loaded(cpool, index); 872 if (m != nullptr) { 873 return get_method(m); 874 } 875 } 876 break; 877 default: 878 break; 879 } 880 } 881 882 if (holder_is_accessible) { // Our declared holder is loaded. 883 constantTag tag = cpool->tag_ref_at(index, bc); 884 assert(accessor->get_instanceKlass() == cpool->pool_holder(), "not the pool holder?"); 885 Method* m = lookup_method(accessor, holder, name_sym, sig_sym, bc, tag); 886 if (m != nullptr) { 887 ciInstanceKlass* cik = get_instance_klass(m->method_holder()); 888 if ((bc == Bytecodes::_invokestatic && cik->is_not_initialized()) || !cik->is_loaded()) { 889 m = nullptr; 890 } 891 } 892 if (m != nullptr && ReplayCompiles && !ciReplay::is_loaded(m)) { 893 m = nullptr; 894 } 895 if (m != nullptr) { 896 // We found the method. 897 return get_method(m); 898 } 899 } 900 901 // Either the declared holder was not loaded, or the method could 902 // not be found. Create a dummy ciMethod to represent the failed 903 // lookup. 904 ciSymbol* name = get_symbol(name_sym); 905 ciSymbol* signature = get_symbol(sig_sym); 906 return get_unloaded_method(holder, name, signature, accessor); 907 } 908 } 909 910 911 // ------------------------------------------------------------------ 912 // ciEnv::get_instance_klass_for_declared_method_holder 913 ciInstanceKlass* ciEnv::get_instance_klass_for_declared_method_holder(ciKlass* method_holder) { 914 // For the case of <array>.clone(), the method holder can be a ciArrayKlass 915 // instead of a ciInstanceKlass. For that case simply pretend that the 916 // declared holder is Object.clone since that's where the call will bottom out. 917 // A more correct fix would trickle out through many interfaces in CI, 918 // requiring ciInstanceKlass* to become ciKlass* and many more places would 919 // require checks to make sure the expected type was found. Given that this 920 // only occurs for clone() the more extensive fix seems like overkill so 921 // instead we simply smear the array type into Object. 922 guarantee(method_holder != nullptr, "no method holder"); 923 if (method_holder->is_instance_klass()) { 924 return method_holder->as_instance_klass(); 925 } else if (method_holder->is_array_klass()) { 926 return current()->Object_klass(); 927 } else { 928 ShouldNotReachHere(); 929 } 930 return nullptr; 931 } 932 933 934 // ------------------------------------------------------------------ 935 // ciEnv::get_method_by_index 936 ciMethod* ciEnv::get_method_by_index(const constantPoolHandle& cpool, 937 int index, Bytecodes::Code bc, 938 ciInstanceKlass* accessor) { 939 GUARDED_VM_ENTRY(return get_method_by_index_impl(cpool, index, bc, accessor);) 940 } 941 942 943 // ------------------------------------------------------------------ 944 // ciEnv::name_buffer 945 char *ciEnv::name_buffer(int req_len) { 946 if (_name_buffer_len < req_len) { 947 if (_name_buffer == nullptr) { 948 _name_buffer = (char*)arena()->Amalloc(sizeof(char)*req_len); 949 _name_buffer_len = req_len; 950 } else { 951 _name_buffer = 952 (char*)arena()->Arealloc(_name_buffer, _name_buffer_len, req_len); 953 _name_buffer_len = req_len; 954 } 955 } 956 return _name_buffer; 957 } 958 959 // ------------------------------------------------------------------ 960 // ciEnv::is_in_vm 961 bool ciEnv::is_in_vm() { 962 return JavaThread::current()->thread_state() == _thread_in_vm; 963 } 964 965 // ------------------------------------------------------------------ 966 // ciEnv::validate_compile_task_dependencies 967 // 968 // Check for changes during compilation (e.g. class loads, evolution, 969 // breakpoints, call site invalidation). 970 void ciEnv::validate_compile_task_dependencies(ciMethod* target) { 971 assert(!failing(), "should not call this when failing"); 972 973 Dependencies::DepType result = dependencies()->validate_dependencies(_task); 974 if (result != Dependencies::end_marker) { 975 if (result == Dependencies::call_site_target_value) { 976 _inc_decompile_count_on_failure = false; 977 record_failure("call site target change"); 978 } else if (Dependencies::is_klass_type(result)) { 979 record_failure("concurrent class loading"); 980 } else { 981 record_failure("invalid non-klass dependency"); 982 } 983 } 984 } 985 986 // is_loading_aot_code = true implies loading compiled code from AOT code cache 987 bool ciEnv::is_compilation_valid(JavaThread* thread, ciMethod* target, bool install_code, bool is_loading_aot_code, bool preload) { 988 methodHandle method(thread, target->get_Method()); 989 990 // We require method counters to store some method state (max compilation levels) required by the compilation policy. 991 if (!preload && method->get_method_counters(thread) == nullptr) { 992 record_failure("can't create method counters"); 993 return false; 994 } 995 996 // Change in Jvmti state may invalidate compilation. 997 if (!failing() && jvmti_state_changed()) { 998 record_failure("Jvmti state change invalidated dependencies"); 999 } 1000 1001 // Change in DTrace flags may invalidate compilation. 1002 if (!failing() && 1003 ( (!dtrace_method_probes() && DTraceMethodProbes) || 1004 (!dtrace_alloc_probes() && DTraceAllocProbes) )) { 1005 record_failure("DTrace flags change invalidated dependencies"); 1006 } 1007 1008 if (!preload && !failing() && target->needs_clinit_barrier() && 1009 target->holder()->is_in_error_state()) { 1010 record_failure("method holder is in error state"); 1011 } 1012 1013 if (!failing() && !is_loading_aot_code) { 1014 if (log() != nullptr) { 1015 // Log the dependencies which this compilation declares. 1016 dependencies()->log_all_dependencies(); 1017 } 1018 1019 // Encode the dependencies now, so we can check them right away. 1020 dependencies()->encode_content_bytes(); 1021 } 1022 // Check for {class loads, evolution, breakpoints, ...} during compilation 1023 if (!failing() && install_code) { 1024 // Check for {class loads, evolution, breakpoints, ...} during compilation 1025 validate_compile_task_dependencies(target); 1026 if (failing() && preload) { 1027 ResourceMark rm; 1028 char *method_name = method->name_and_sig_as_C_string(); 1029 log_info(aot, codecache)("preload code for '%s' failed dependency check", method_name); 1030 } 1031 } 1032 1033 if (failing()) { 1034 // While not a true deoptimization, it is a preemptive decompile. 1035 MethodData* mdo = method()->method_data(); 1036 if (mdo != nullptr && _inc_decompile_count_on_failure) { 1037 mdo->inc_decompile_count(); 1038 } 1039 return false; 1040 } 1041 return true; 1042 } 1043 1044 void ciEnv::make_code_usable(JavaThread* thread, ciMethod* target, bool preload, int entry_bci, AOTCodeEntry* aot_code_entry, nmethod* nm) { 1045 methodHandle method(thread, target->get_Method()); 1046 1047 if (entry_bci == InvocationEntryBci) { 1048 if (TieredCompilation) { 1049 // If there is an old version we're done with it 1050 nmethod* old = method->code(); 1051 if (TraceMethodReplacement && old != nullptr) { 1052 ResourceMark rm; 1053 char *method_name = method->name_and_sig_as_C_string(); 1054 tty->print_cr("Replacing method %s", method_name); 1055 } 1056 if (old != nullptr) { 1057 old->make_not_used(); 1058 } 1059 } 1060 1061 LogTarget(Info, nmethod, install) lt; 1062 if (lt.is_enabled()) { 1063 ResourceMark rm; 1064 char *method_name = method->name_and_sig_as_C_string(); 1065 lt.print("Installing method (L%d) %s id=%d aot=%s%s%u", 1066 task()->comp_level(), method_name, compile_id(), 1067 task()->is_aot_load() ? "A" : "", preload ? "P" : "", 1068 (aot_code_entry != nullptr ? aot_code_entry->offset() : 0)); 1069 } 1070 // Allow the code to be executed 1071 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag); 1072 if (nm->make_in_use()) { 1073 #ifdef ASSERT 1074 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod(); 1075 if (bs_nm != nullptr && bs_nm->supports_entry_barrier(nm)) { 1076 if (!bs_nm->is_armed(nm)) { 1077 log_info(init)("nmethod %d %d not armed", nm->compile_id(), nm->comp_level()); 1078 } 1079 } 1080 #endif // ASSERT 1081 if (preload) { 1082 nm->set_preloaded(true); 1083 method->set_preload_code(nm); 1084 } 1085 if (!preload || target->holder()->is_linked()) { 1086 method->set_code(method, nm); 1087 } 1088 } 1089 } else { 1090 LogTarget(Info, nmethod, install) lt; 1091 if (lt.is_enabled()) { 1092 assert(aot_code_entry == nullptr && !task()->is_aot_load(), "OSR nmethods are not AOT compiled"); 1093 ResourceMark rm; 1094 char *method_name = method->name_and_sig_as_C_string(); 1095 lt.print("Installing osr method (L%d) %s @ %d id=%u", 1096 task()->comp_level(), method_name, entry_bci, compile_id()); 1097 } 1098 MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag); 1099 if (nm->make_in_use()) { 1100 method->method_holder()->add_osr_nmethod(nm); 1101 } 1102 } 1103 } 1104 1105 nmethod* ciEnv::register_aot_method(JavaThread* thread, 1106 ciMethod* target, 1107 AbstractCompiler* compiler, 1108 nmethod* archived_nm, 1109 address reloc_data, 1110 GrowableArray<Handle>& oop_list, 1111 GrowableArray<Metadata*>& metadata_list, 1112 ImmutableOopMapSet* oopmaps, 1113 address immutable_data, 1114 GrowableArray<Handle>& reloc_imm_oop_list, 1115 GrowableArray<Metadata*>& reloc_imm_metadata_list, 1116 AOTCodeReader* aot_code_reader) 1117 { 1118 AOTCodeEntry* aot_code_entry = task()->aot_code_entry(); 1119 assert(aot_code_entry != nullptr, "must be"); 1120 nmethod* nm = nullptr; 1121 { 1122 methodHandle method(thread, target->get_Method()); 1123 bool preload = task()->preload(); // Code is preloaded before Java method execution 1124 1125 // Check if memory should be freed before allocation 1126 CodeCache::gc_on_allocation(); 1127 1128 // To prevent compile queue updates. 1129 MutexLocker locker(thread, task()->compile_queue()->lock()); 1130 1131 // Prevent InstanceKlass::add_to_hierarchy from running 1132 // and invalidating our dependencies until we install this method. 1133 // No safepoints are allowed. Otherwise, class redefinition can occur in between. 1134 MutexLocker ml(Compile_lock); 1135 NoSafepointVerifier nsv; 1136 1137 // AOTCode entry indicates this shared code was marked invalid while it was loaded. 1138 if (aot_code_entry->not_entrant()) { 1139 return nullptr; 1140 } 1141 1142 if (!is_compilation_valid(thread, target, true /*install_code*/, true, preload)) { 1143 return nullptr; 1144 } 1145 1146 nm = nmethod::new_nmethod(archived_nm, 1147 method, 1148 compiler, 1149 compile_id(), 1150 reloc_data, 1151 oop_list, 1152 metadata_list, 1153 oopmaps, 1154 immutable_data, 1155 reloc_imm_oop_list, 1156 reloc_imm_metadata_list, 1157 aot_code_reader); 1158 1159 if (nm != nullptr) { 1160 aot_code_entry->set_loaded(); 1161 make_code_usable(thread, target, preload, InvocationEntryBci, aot_code_entry, nm); 1162 } 1163 } 1164 1165 NoSafepointVerifier nsv; 1166 if (nm != nullptr) { 1167 // Compilation succeeded, post what we know about it 1168 nm->post_compiled_method(task()); 1169 task()->set_num_inlined_bytecodes(num_inlined_bytecodes()); 1170 } else { 1171 // The CodeCache is full. 1172 record_failure("code cache is full"); 1173 } 1174 return nm; 1175 // safepoints are allowed again 1176 } 1177 1178 // ------------------------------------------------------------------ 1179 // ciEnv::register_method 1180 void ciEnv::register_method(ciMethod* target, 1181 int entry_bci, 1182 CodeOffsets* offsets, 1183 int orig_pc_offset, 1184 CodeBuffer* code_buffer, 1185 int frame_words, 1186 OopMapSet* oop_map_set, 1187 ExceptionHandlerTable* handler_table, 1188 ImplicitExceptionTable* inc_table, 1189 AbstractCompiler* compiler, 1190 bool has_clinit_barriers, 1191 bool for_preload, 1192 bool has_unsafe_access, 1193 bool has_wide_vectors, 1194 bool has_monitors, 1195 bool has_scoped_access, 1196 int immediate_oops_patched, 1197 bool install_code) { 1198 VM_ENTRY_MARK; 1199 nmethod* nm = nullptr; 1200 { 1201 methodHandle method(THREAD, target->get_Method()); 1202 1203 // Check if memory should be freed before allocation 1204 CodeCache::gc_on_allocation(); 1205 1206 // To prevent compile queue updates. 1207 MutexLocker locker(THREAD, task()->compile_queue()->lock()); 1208 1209 // Prevent InstanceKlass::add_to_hierarchy from running 1210 // and invalidating our dependencies until we install this method. 1211 // No safepoints are allowed. Otherwise, class redefinition can occur in between. 1212 MutexLocker ml(Compile_lock); 1213 NoSafepointVerifier nsv; 1214 1215 if (!is_compilation_valid(THREAD, target, install_code, /*aot_code_entry*/ false, /*preload*/ false)) { 1216 code_buffer->free_blob(); 1217 return; 1218 } 1219 1220 assert(offsets->value(CodeOffsets::Deopt) != -1, "must have deopt entry"); 1221 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must have exception entry"); 1222 1223 if (install_code) { 1224 nm = nmethod::new_nmethod(method, 1225 compile_id(), 1226 entry_bci, 1227 offsets, 1228 orig_pc_offset, 1229 debug_info(), dependencies(), code_buffer, 1230 frame_words, oop_map_set, 1231 handler_table, inc_table, 1232 compiler, CompLevel(task()->comp_level())); 1233 } 1234 // Free codeBlobs 1235 code_buffer->free_blob(); 1236 1237 if (nm != nullptr) { 1238 nm->set_has_unsafe_access(has_unsafe_access); 1239 nm->set_has_wide_vectors(has_wide_vectors); 1240 nm->set_has_monitors(has_monitors); 1241 nm->set_has_scoped_access(has_scoped_access); 1242 nm->set_preloaded(false); 1243 nm->set_has_clinit_barriers(has_clinit_barriers); 1244 assert(!method->is_synchronized() || nm->has_monitors(), ""); 1245 1246 if (task()->is_precompile()) { 1247 AOTCodeEntry* aot_code_entry = AOTCodeCache::store_nmethod(nm, compiler, for_preload); 1248 if (aot_code_entry != nullptr) { 1249 aot_code_entry->set_inlined_bytecodes(num_inlined_bytecodes()); 1250 } 1251 } 1252 make_code_usable(THREAD, target, /* preload */ false, entry_bci, /* aot_code_entry */ nullptr, nm); 1253 } 1254 } 1255 1256 NoSafepointVerifier nsv; 1257 if (nm != nullptr) { 1258 // Compilation succeeded, post what we know about it 1259 nm->post_compiled_method(task()); 1260 task()->set_num_inlined_bytecodes(num_inlined_bytecodes()); 1261 } else if (install_code) { 1262 // The CodeCache is full. 1263 record_failure("code cache is full"); 1264 } else { 1265 task()->set_num_inlined_bytecodes(num_inlined_bytecodes()); 1266 } 1267 1268 // safepoints are allowed again 1269 } 1270 1271 // ------------------------------------------------------------------ 1272 // ciEnv::find_system_klass 1273 ciKlass* ciEnv::find_system_klass(ciSymbol* klass_name) { 1274 VM_ENTRY_MARK; 1275 return get_klass_by_name_impl(nullptr, constantPoolHandle(), klass_name, false); 1276 } 1277 1278 // ------------------------------------------------------------------ 1279 // ciEnv::comp_level 1280 int ciEnv::comp_level() { 1281 if (task() == nullptr) return CompilationPolicy::highest_compile_level(); 1282 return task()->comp_level(); 1283 } 1284 1285 // ------------------------------------------------------------------ 1286 // ciEnv::compile_id 1287 int ciEnv::compile_id() { 1288 if (task() == nullptr) return 0; 1289 return task()->compile_id(); 1290 } 1291 1292 // ------------------------------------------------------------------ 1293 // ciEnv::notice_inlined_method() 1294 void ciEnv::notice_inlined_method(ciMethod* method) { 1295 _num_inlined_bytecodes += method->code_size_for_inlining(); 1296 CompileTrainingData* ctd = task()->training_data(); 1297 if (ctd != nullptr) { 1298 GUARDED_VM_ENTRY({ 1299 methodHandle mh(Thread::current(), method->get_Method()); 1300 ctd->notice_inlined_method(task(), mh); 1301 }); 1302 } 1303 } 1304 1305 // ------------------------------------------------------------------ 1306 // ciEnv::num_inlined_bytecodes() 1307 int ciEnv::num_inlined_bytecodes() const { 1308 return _num_inlined_bytecodes; 1309 } 1310 1311 // ------------------------------------------------------------------ 1312 // ciEnv::record_failure() 1313 void ciEnv::record_failure(const char* reason) { 1314 // record the bailout for hserr envlog 1315 if (reason != nullptr) { 1316 if (CompilationLog::log() != nullptr) { 1317 CompilerThread* thread = CompilerThread::current(); 1318 CompileTask* task = thread->task(); 1319 CompilationLog::log()->log_failure(thread, task, reason, nullptr); 1320 } 1321 } 1322 1323 if (_failure_reason.get() == nullptr) { 1324 // Record the first failure reason. 1325 _failure_reason.set(reason); 1326 } 1327 } 1328 1329 void ciEnv::report_failure(const char* reason) { 1330 EventCompilationFailure event; 1331 if (event.should_commit()) { 1332 CompilerEvent::CompilationFailureEvent::post(event, compile_id(), reason); 1333 } 1334 } 1335 1336 // ------------------------------------------------------------------ 1337 // ciEnv::record_method_not_compilable() 1338 void ciEnv::record_method_not_compilable(const char* reason, bool all_tiers) { 1339 int new_compilable = 1340 all_tiers ? MethodCompilable_never : MethodCompilable_not_at_tier ; 1341 1342 // Only note transitions to a worse state 1343 if (new_compilable > _compilable) { 1344 if (log() != nullptr) { 1345 if (all_tiers) { 1346 log()->elem("method_not_compilable"); 1347 } else { 1348 log()->elem("method_not_compilable_at_tier level='%d'", 1349 current()->task()->comp_level()); 1350 } 1351 } 1352 _compilable = new_compilable; 1353 1354 // Reset failure reason; this one is more important. 1355 _failure_reason.clear(); 1356 record_failure(reason); 1357 } 1358 } 1359 1360 // ------------------------------------------------------------------ 1361 // ciEnv::record_out_of_memory_failure() 1362 void ciEnv::record_out_of_memory_failure() { 1363 // If memory is low, we stop compiling methods. 1364 record_method_not_compilable("out of memory"); 1365 } 1366 1367 ciInstance* ciEnv::unloaded_ciinstance() { 1368 GUARDED_VM_ENTRY(return _factory->get_unloaded_object_constant();) 1369 } 1370 1371 // ------------------------------------------------------------------ 1372 // Replay support 1373 1374 1375 // Lookup location descriptor for the class, if any. 1376 // Returns false if not found. 1377 bool ciEnv::dyno_loc(const InstanceKlass* ik, const char *&loc) const { 1378 bool found = false; 1379 int pos = _dyno_klasses->find_sorted<const InstanceKlass*, klass_compare>(ik, found); 1380 if (!found) { 1381 return false; 1382 } 1383 loc = _dyno_locs->at(pos); 1384 return found; 1385 } 1386 1387 // Associate the current location descriptor with the given class and record for later lookup. 1388 void ciEnv::set_dyno_loc(const InstanceKlass* ik) { 1389 const char *loc = os::strdup(_dyno_name); 1390 bool found = false; 1391 int pos = _dyno_klasses->find_sorted<const InstanceKlass*, klass_compare>(ik, found); 1392 if (found) { 1393 _dyno_locs->at_put(pos, loc); 1394 } else { 1395 _dyno_klasses->insert_before(pos, ik); 1396 _dyno_locs->insert_before(pos, loc); 1397 } 1398 } 1399 1400 // Associate the current location descriptor with the given class and record for later lookup. 1401 // If it turns out that there are multiple locations for the given class, that conflict should 1402 // be handled here. Currently we choose the first location found. 1403 void ciEnv::record_best_dyno_loc(const InstanceKlass* ik) { 1404 if (!ik->is_hidden()) { 1405 return; 1406 } 1407 const char *loc0; 1408 if (!dyno_loc(ik, loc0)) { 1409 set_dyno_loc(ik); 1410 } 1411 } 1412 1413 // Look up the location descriptor for the given class and print it to the output stream. 1414 bool ciEnv::print_dyno_loc(outputStream* out, const InstanceKlass* ik) const { 1415 const char *loc; 1416 if (dyno_loc(ik, loc)) { 1417 out->print("%s", loc); 1418 return true; 1419 } else { 1420 return false; 1421 } 1422 } 1423 1424 // Look up the location descriptor for the given class and return it as a string. 1425 // Returns null if no location is found. 1426 const char *ciEnv::dyno_name(const InstanceKlass* ik) const { 1427 if (ik->is_hidden()) { 1428 stringStream ss; 1429 if (print_dyno_loc(&ss, ik)) { 1430 ss.print(" ;"); // add terminator 1431 const char* call_site = ss.as_string(); 1432 return call_site; 1433 } 1434 } 1435 return nullptr; 1436 } 1437 1438 // Look up the location descriptor for the given class and return it as a string. 1439 // Returns the class name as a fallback if no location is found. 1440 const char *ciEnv::replay_name(ciKlass* k) const { 1441 if (k->is_instance_klass()) { 1442 return replay_name(k->as_instance_klass()->get_instanceKlass()); 1443 } 1444 return k->name()->as_quoted_ascii(); 1445 } 1446 1447 // Look up the location descriptor for the given class and return it as a string. 1448 // Returns the class name as a fallback if no location is found. 1449 const char *ciEnv::replay_name(const InstanceKlass* ik) const { 1450 const char* name = dyno_name(ik); 1451 if (name != nullptr) { 1452 return name; 1453 } 1454 return ik->name()->as_quoted_ascii(); 1455 } 1456 1457 // Process a java.lang.invoke.MemberName object and record any dynamic locations. 1458 void ciEnv::record_member(Thread* thread, oop member) { 1459 assert(java_lang_invoke_MemberName::is_instance(member), "!"); 1460 // Check MemberName.clazz field 1461 oop clazz = java_lang_invoke_MemberName::clazz(member); 1462 if (clazz->klass()->is_instance_klass()) { 1463 RecordLocation fp(this, "clazz"); 1464 InstanceKlass* ik = InstanceKlass::cast(clazz->klass()); 1465 record_best_dyno_loc(ik); 1466 } 1467 // Check MemberName.method.vmtarget field 1468 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(member); 1469 if (vmtarget != nullptr) { 1470 RecordLocation fp2(this, "<vmtarget>"); 1471 InstanceKlass* ik = vmtarget->method_holder(); 1472 record_best_dyno_loc(ik); 1473 } 1474 } 1475 1476 // Read an object field. Lookup is done by name only. 1477 static inline oop obj_field(oop obj, const char* name) { 1478 return ciReplay::obj_field(obj, name); 1479 } 1480 1481 // Process a java.lang.invoke.LambdaForm object and record any dynamic locations. 1482 void ciEnv::record_lambdaform(Thread* thread, oop form) { 1483 assert(java_lang_invoke_LambdaForm::is_instance(form), "!"); 1484 1485 { 1486 // Check LambdaForm.vmentry field 1487 oop member = java_lang_invoke_LambdaForm::vmentry(form); 1488 RecordLocation fp0(this, "vmentry"); 1489 record_member(thread, member); 1490 } 1491 1492 // Check LambdaForm.names array 1493 objArrayOop names = (objArrayOop)obj_field(form, "names"); 1494 if (names != nullptr) { 1495 RecordLocation lp0(this, "names"); 1496 int len = names->length(); 1497 for (int i = 0; i < len; ++i) { 1498 oop name = names->obj_at(i); 1499 RecordLocation lp1(this, "%d", i); 1500 // Check LambdaForm.names[i].function field 1501 RecordLocation lp2(this, "function"); 1502 oop function = obj_field(name, "function"); 1503 if (function != nullptr) { 1504 // Check LambdaForm.names[i].function.member field 1505 oop member = obj_field(function, "member"); 1506 if (member != nullptr) { 1507 RecordLocation lp3(this, "member"); 1508 record_member(thread, member); 1509 } 1510 // Check LambdaForm.names[i].function.resolvedHandle field 1511 oop mh = obj_field(function, "resolvedHandle"); 1512 if (mh != nullptr) { 1513 RecordLocation lp3(this, "resolvedHandle"); 1514 record_mh(thread, mh); 1515 } 1516 // Check LambdaForm.names[i].function.invoker field 1517 oop invoker = obj_field(function, "invoker"); 1518 if (invoker != nullptr) { 1519 RecordLocation lp3(this, "invoker"); 1520 record_mh(thread, invoker); 1521 } 1522 } 1523 } 1524 } 1525 } 1526 1527 // Process a java.lang.invoke.MethodHandle object and record any dynamic locations. 1528 void ciEnv::record_mh(Thread* thread, oop mh) { 1529 { 1530 // Check MethodHandle.form field 1531 oop form = java_lang_invoke_MethodHandle::form(mh); 1532 RecordLocation fp(this, "form"); 1533 record_lambdaform(thread, form); 1534 } 1535 // Check DirectMethodHandle.member field 1536 if (java_lang_invoke_DirectMethodHandle::is_instance(mh)) { 1537 oop member = java_lang_invoke_DirectMethodHandle::member(mh); 1538 RecordLocation fp(this, "member"); 1539 record_member(thread, member); 1540 } else { 1541 // Check <MethodHandle subclass>.argL<n> fields 1542 // Probably BoundMethodHandle.Species_L*, but we only care if the field exists 1543 char arg_name[] = "argLXX"; 1544 int max_arg = 99; 1545 for (int index = 0; index <= max_arg; ++index) { 1546 jio_snprintf(arg_name, sizeof (arg_name), "argL%d", index); 1547 oop arg = obj_field(mh, arg_name); 1548 if (arg != nullptr) { 1549 RecordLocation fp(this, "%s", arg_name); 1550 if (arg->klass()->is_instance_klass()) { 1551 InstanceKlass* ik2 = InstanceKlass::cast(arg->klass()); 1552 record_best_dyno_loc(ik2); 1553 record_call_site_obj(thread, arg); 1554 } 1555 } else { 1556 break; 1557 } 1558 } 1559 } 1560 } 1561 1562 // Process an object found at an invokedynamic/invokehandle call site and record any dynamic locations. 1563 // Types currently supported are MethodHandle and CallSite. 1564 // The object is typically the "appendix" object, or Bootstrap Method (BSM) object. 1565 void ciEnv::record_call_site_obj(Thread* thread, oop obj) 1566 { 1567 if (obj != nullptr) { 1568 if (java_lang_invoke_MethodHandle::is_instance(obj)) { 1569 record_mh(thread, obj); 1570 } else if (java_lang_invoke_ConstantCallSite::is_instance(obj)) { 1571 oop target = java_lang_invoke_CallSite::target(obj); 1572 if (target->klass()->is_instance_klass()) { 1573 RecordLocation fp(this, "target"); 1574 InstanceKlass* ik = InstanceKlass::cast(target->klass()); 1575 record_best_dyno_loc(ik); 1576 } 1577 } 1578 } 1579 } 1580 1581 // Process an adapter Method* found at an invokedynamic/invokehandle call site and record any dynamic locations. 1582 void ciEnv::record_call_site_method(Thread* thread, Method* adapter) { 1583 InstanceKlass* holder = adapter->method_holder(); 1584 if (!holder->is_hidden()) { 1585 return; 1586 } 1587 RecordLocation fp(this, "<adapter>"); 1588 record_best_dyno_loc(holder); 1589 } 1590 1591 // Process an invokedynamic call site and record any dynamic locations. 1592 void ciEnv::process_invokedynamic(const constantPoolHandle &cp, int indy_index, JavaThread* thread) { 1593 ResolvedIndyEntry* indy_info = cp->resolved_indy_entry_at(indy_index); 1594 if (indy_info->method() != nullptr) { 1595 // process the adapter 1596 Method* adapter = indy_info->method(); 1597 record_call_site_method(thread, adapter); 1598 // process the appendix 1599 oop appendix = cp->resolved_reference_from_indy(indy_index); 1600 { 1601 RecordLocation fp(this, "<appendix>"); 1602 record_call_site_obj(thread, appendix); 1603 } 1604 // process the BSM 1605 int pool_index = indy_info->constant_pool_index(); 1606 BootstrapInfo bootstrap_specifier(cp, pool_index, indy_index); 1607 oop bsm = cp->resolve_possibly_cached_constant_at(bootstrap_specifier.bsm_index(), thread); 1608 { 1609 RecordLocation fp(this, "<bsm>"); 1610 record_call_site_obj(thread, bsm); 1611 } 1612 } 1613 } 1614 1615 // Process an invokehandle call site and record any dynamic locations. 1616 void ciEnv::process_invokehandle(const constantPoolHandle &cp, int index, JavaThread* thread) { 1617 const int holder_index = cp->klass_ref_index_at(index, Bytecodes::_invokehandle); 1618 if (!cp->tag_at(holder_index).is_klass()) { 1619 return; // not resolved 1620 } 1621 Klass* holder = ConstantPool::klass_at_if_loaded(cp, holder_index); 1622 Symbol* name = cp->name_ref_at(index, Bytecodes::_invokehandle); 1623 if (MethodHandles::is_signature_polymorphic_name(holder, name)) { 1624 ResolvedMethodEntry* method_entry = cp->resolved_method_entry_at(index); 1625 if (method_entry->is_resolved(Bytecodes::_invokehandle)) { 1626 // process the adapter 1627 Method* adapter = method_entry->method(); 1628 oop appendix = cp->cache()->appendix_if_resolved(method_entry); 1629 record_call_site_method(thread, adapter); 1630 // process the appendix 1631 { 1632 RecordLocation fp(this, "<appendix>"); 1633 record_call_site_obj(thread, appendix); 1634 } 1635 } 1636 } 1637 } 1638 1639 // Search the class hierarchy for dynamic classes reachable through dynamic call sites or 1640 // constant pool entries and record for future lookup. 1641 void ciEnv::find_dynamic_call_sites() { 1642 _dyno_klasses = new (arena()) GrowableArray<const InstanceKlass*>(arena(), 100, 0, nullptr); 1643 _dyno_locs = new (arena()) GrowableArray<const char *>(arena(), 100, 0, nullptr); 1644 1645 // Iterate over the class hierarchy 1646 for (ClassHierarchyIterator iter(vmClasses::Object_klass()); !iter.done(); iter.next()) { 1647 Klass* sub = iter.klass(); 1648 if (sub->is_instance_klass()) { 1649 InstanceKlass *isub = InstanceKlass::cast(sub); 1650 InstanceKlass* ik = isub; 1651 if (!ik->is_linked()) { 1652 continue; 1653 } 1654 if (ik->is_hidden()) { 1655 continue; 1656 } 1657 JavaThread* thread = JavaThread::current(); 1658 const constantPoolHandle pool(thread, ik->constants()); 1659 1660 // Look for invokedynamic/invokehandle call sites 1661 for (int i = 0; i < ik->methods()->length(); ++i) { 1662 Method* m = ik->methods()->at(i); 1663 1664 BytecodeStream bcs(methodHandle(thread, m)); 1665 while (!bcs.is_last_bytecode()) { 1666 Bytecodes::Code opcode = bcs.next(); 1667 opcode = bcs.raw_code(); 1668 switch (opcode) { 1669 case Bytecodes::_invokedynamic: 1670 case Bytecodes::_invokehandle: { 1671 RecordLocation fp(this, "@bci %s %s %s %d", 1672 ik->name()->as_quoted_ascii(), 1673 m->name()->as_quoted_ascii(), m->signature()->as_quoted_ascii(), 1674 bcs.bci()); 1675 if (opcode == Bytecodes::_invokedynamic) { 1676 int index = bcs.get_index_u4(); 1677 process_invokedynamic(pool, index, thread); 1678 } else { 1679 assert(opcode == Bytecodes::_invokehandle, "new switch label added?"); 1680 int cp_cache_index = bcs.get_index_u2(); 1681 process_invokehandle(pool, cp_cache_index, thread); 1682 } 1683 break; 1684 } 1685 default: 1686 break; 1687 } 1688 } 1689 } 1690 1691 // Look for MethodHandle constant pool entries 1692 RecordLocation fp(this, "@cpi %s", ik->name()->as_quoted_ascii()); 1693 int len = pool->length(); 1694 for (int i = 0; i < len; ++i) { 1695 if (pool->tag_at(i).is_method_handle()) { 1696 bool found_it; 1697 oop mh = pool->find_cached_constant_at(i, found_it, thread); 1698 if (mh != nullptr) { 1699 RecordLocation fp(this, "%d", i); 1700 record_mh(thread, mh); 1701 } 1702 } 1703 } 1704 } 1705 } 1706 } 1707 1708 void ciEnv::dump_compile_data(outputStream* out) { 1709 CompileTask* task = this->task(); 1710 if (task) { 1711 #ifdef COMPILER2 1712 if (ReplayReduce && compiler_data() != nullptr) { 1713 // Dump C2 "reduced" inlining data. 1714 ((Compile*)compiler_data())->dump_inline_data_reduced(out); 1715 } 1716 #endif 1717 Method* method = task->method(); 1718 int entry_bci = task->osr_bci(); 1719 int comp_level = task->comp_level(); 1720 out->print("compile "); 1721 get_method(method)->dump_name_as_ascii(out); 1722 out->print(" %d %d", entry_bci, comp_level); 1723 if (compiler_data() != nullptr) { 1724 if (is_c2_compile(comp_level)) { 1725 #ifdef COMPILER2 1726 // Dump C2 inlining data. 1727 ((Compile*)compiler_data())->dump_inline_data(out); 1728 #endif 1729 } else if (is_c1_compile(comp_level)) { 1730 #ifdef COMPILER1 1731 // Dump C1 inlining data. 1732 ((Compilation*)compiler_data())->dump_inline_data(out); 1733 #endif 1734 } 1735 } 1736 out->cr(); 1737 } 1738 } 1739 1740 // Called from VM error reporter, so be careful. 1741 // Don't safepoint or acquire any locks. 1742 // 1743 void ciEnv::dump_replay_data_helper(outputStream* out) { 1744 NoSafepointVerifier no_safepoint; 1745 ResourceMark rm; 1746 1747 assert(this->task() != nullptr, "task must not be null"); 1748 1749 dump_replay_data_version(out); 1750 #if INCLUDE_JVMTI 1751 out->print_cr("JvmtiExport can_access_local_variables %d", _jvmti_can_access_local_variables); 1752 out->print_cr("JvmtiExport can_hotswap_or_post_breakpoint %d", _jvmti_can_hotswap_or_post_breakpoint); 1753 out->print_cr("JvmtiExport can_post_on_exceptions %d", _jvmti_can_post_on_exceptions); 1754 #endif // INCLUDE_JVMTI 1755 1756 find_dynamic_call_sites(); 1757 1758 GrowableArray<ciMetadata*>* objects = _factory->get_ci_metadata(); 1759 out->print_cr("# %d ciObject found", objects->length()); 1760 1761 // The very first entry is the InstanceKlass of the root method of the current compilation. 1762 ciInstanceKlass::dump_replay_instanceKlass(out, task()->method()->method_holder()); 1763 1764 for (int i = 0; i < objects->length(); i++) { 1765 objects->at(i)->dump_replay_data(out); 1766 } 1767 1768 dump_compile_data(out); 1769 out->flush(); 1770 } 1771 1772 // Called from VM error reporter, so be careful. 1773 // Don't safepoint or acquire any locks. 1774 // 1775 void ciEnv::dump_replay_data_unsafe(outputStream* out) { 1776 GUARDED_VM_ENTRY( 1777 dump_replay_data_helper(out); 1778 ) 1779 } 1780 1781 void ciEnv::dump_replay_data(outputStream* out) { 1782 GUARDED_VM_ENTRY( 1783 MutexLocker ml(Compile_lock); 1784 dump_replay_data_helper(out); 1785 ) 1786 } 1787 1788 void ciEnv::dump_replay_data(int compile_id) { 1789 char buffer[64]; 1790 int ret = jio_snprintf(buffer, sizeof(buffer), "replay_pid%d_compid%d.log", os::current_process_id(), compile_id); 1791 if (ret > 0) { 1792 int fd = os::open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666); 1793 if (fd != -1) { 1794 FILE* replay_data_file = os::fdopen(fd, "w"); 1795 if (replay_data_file != nullptr) { 1796 fileStream replay_data_stream(replay_data_file, /*need_close=*/true); 1797 dump_replay_data(&replay_data_stream); 1798 tty->print_cr("# Compiler replay data is saved as: %s", buffer); 1799 } else { 1800 tty->print_cr("# Can't open file to dump replay data."); 1801 close(fd); 1802 } 1803 } 1804 } 1805 } 1806 1807 void ciEnv::dump_inline_data(int compile_id) { 1808 char buffer[64]; 1809 int ret = jio_snprintf(buffer, sizeof(buffer), "inline_pid%d_compid%d.log", os::current_process_id(), compile_id); 1810 if (ret > 0) { 1811 int fd = os::open(buffer, O_RDWR | O_CREAT | O_TRUNC, 0666); 1812 if (fd != -1) { 1813 FILE* inline_data_file = os::fdopen(fd, "w"); 1814 if (inline_data_file != nullptr) { 1815 fileStream replay_data_stream(inline_data_file, /*need_close=*/true); 1816 GUARDED_VM_ENTRY( 1817 MutexLocker ml(Compile_lock); 1818 dump_replay_data_version(&replay_data_stream); 1819 dump_compile_data(&replay_data_stream); 1820 ) 1821 replay_data_stream.flush(); 1822 tty->print("# Compiler inline data is saved as: "); 1823 tty->print_cr("%s", buffer); 1824 } else { 1825 tty->print_cr("# Can't open file to dump inline data."); 1826 close(fd); 1827 } 1828 } 1829 } 1830 } 1831 1832 void ciEnv::dump_replay_data_version(outputStream* out) { 1833 out->print_cr("version %d", REPLAY_VERSION); 1834 } 1835 1836 bool ciEnv::is_precompile() { 1837 return (task() != nullptr) && task()->is_precompile(); 1838 } 1839 1840 InstanceKlass::ClassState ciEnv::compute_init_state_for_precompiled(InstanceKlass* ik) { 1841 ASSERT_IN_VM; 1842 assert(is_precompile(), "should be called only in assembly phase"); 1843 assert(!ik->is_in_error_state(), "there should not be any probelm with this klass"); 1844 ResourceMark rm; 1845 1846 if (!AOTCacheAccess::can_generate_aot_code_for(ik)) { 1847 log_debug(precompile)("%d: klass (%s) %s is not archived", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name()); 1848 // Skip this class 1849 return InstanceKlass::ClassState::initialization_error; 1850 } 1851 if (task()->method()->method_holder() == ik) { 1852 if (task()->method()->is_static_initializer()) { // Happens with -Xcomp 1853 return InstanceKlass::ClassState::being_initialized; 1854 } else { 1855 return InstanceKlass::ClassState::fully_initialized; 1856 } 1857 } 1858 1859 switch (task()->compile_reason()) { 1860 case CompileTask::Reason_Precompile: { 1861 // check init dependencies 1862 MethodTrainingData* mtd = nullptr; 1863 GUARDED_VM_ENTRY(mtd = MethodTrainingData::find(methodHandle(Thread::current(), task()->method())); ) 1864 if (mtd != nullptr) { 1865 CompileTrainingData* ctd = mtd->last_toplevel_compile(task()->comp_level()); 1866 if (ctd != nullptr) { 1867 for (int i = 0; i < ctd->init_dep_count(); i++) { 1868 KlassTrainingData* ktd = ctd->init_dep(i); 1869 if (ktd->has_holder() && (ktd->holder() == ik)) { 1870 log_trace(precompile)("%d: init_dependency: %s: %s", task()->compile_id(), InstanceKlass::state2name(ik->init_state()), ik->external_name()); 1871 return InstanceKlass::ClassState::fully_initialized;; // init dependency present 1872 } 1873 } 1874 } 1875 } 1876 // Class may be not present during TD creation for this method. 1877 // It could happen when profiled data for inlined method 1878 // was updated after this method was compiled during training. 1879 break; 1880 } 1881 case CompileTask::Reason_PrecompileForPreload: { 1882 // Preload AOT code does not depend on Training Data, 1883 // it has class init barriers to initialize class by 1884 // going into interpreter or directly calling runtime. 1885 return InstanceKlass::ClassState::fully_initialized; 1886 } 1887 default: fatal("%s", CompileTask::reason_name(task()->compile_reason())); 1888 } 1889 // Skip this class 1890 return InstanceKlass::ClassState::initialization_error; 1891 }