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 "asm/assembler.inline.hpp"
26 #include "code/codeCache.hpp"
27 #include "code/compiledIC.hpp"
28 #include "code/dependencies.hpp"
29 #include "code/nativeInst.hpp"
30 #include "code/nmethod.inline.hpp"
31 #include "code/relocInfo.hpp"
32 #include "code/scopeDesc.hpp"
33 #include "compiler/abstractCompiler.hpp"
34 #include "compiler/compilationLog.hpp"
35 #include "compiler/compileBroker.hpp"
36 #include "compiler/compileLog.hpp"
37 #include "compiler/compileTask.hpp"
38 #include "compiler/compilerDirectives.hpp"
39 #include "compiler/compilerOracle.hpp"
40 #include "compiler/directivesParser.hpp"
41 #include "compiler/disassembler.hpp"
42 #include "compiler/oopMap.inline.hpp"
43 #include "gc/shared/barrierSet.hpp"
44 #include "gc/shared/barrierSetNMethod.hpp"
45 #include "gc/shared/classUnloadingContext.hpp"
771
772 void nmethod::clear_inline_caches() {
773 assert(SafepointSynchronize::is_at_safepoint(), "clearing of IC's only allowed at safepoint");
774 RelocIterator iter(this);
775 while (iter.next()) {
776 iter.reloc()->clear_inline_cache();
777 }
778 }
779
780 #ifdef ASSERT
781 // Check class_loader is alive for this bit of metadata.
782 class CheckClass : public MetadataClosure {
783 void do_metadata(Metadata* md) {
784 Klass* klass = nullptr;
785 if (md->is_klass()) {
786 klass = ((Klass*)md);
787 } else if (md->is_method()) {
788 klass = ((Method*)md)->method_holder();
789 } else if (md->is_methodData()) {
790 klass = ((MethodData*)md)->method()->method_holder();
791 } else {
792 md->print();
793 ShouldNotReachHere();
794 }
795 assert(klass->is_loader_alive(), "must be alive");
796 }
797 };
798 #endif // ASSERT
799
800
801 static void clean_ic_if_metadata_is_dead(CompiledIC *ic) {
802 ic->clean_metadata();
803 }
804
805 // Clean references to unloaded nmethods at addr from this one, which is not unloaded.
806 template <typename CallsiteT>
807 static void clean_if_nmethod_is_unloaded(CallsiteT* callsite, nmethod* from,
808 bool clean_all) {
809 CodeBlob* cb = CodeCache::find_blob(callsite->destination());
810 if (!cb->is_nmethod()) {
1115 nm = new (native_nmethod_size, allow_NonNMethod_space)
1116 nmethod(method(), compiler_none, native_nmethod_size,
1117 compile_id, &offsets,
1118 code_buffer, frame_size,
1119 basic_lock_owner_sp_offset,
1120 basic_lock_sp_offset,
1121 oop_maps, mutable_data_size);
1122 DEBUG_ONLY( if (allow_NonNMethod_space) assert_no_oops_or_metadata(nm); )
1123 NOT_PRODUCT(if (nm != nullptr) native_nmethod_stats.note_native_nmethod(nm));
1124 }
1125
1126 if (nm != nullptr) {
1127 // verify nmethod
1128 DEBUG_ONLY(nm->verify();) // might block
1129
1130 nm->log_new_nmethod();
1131 }
1132 return nm;
1133 }
1134
1135 nmethod* nmethod::new_nmethod(const methodHandle& method,
1136 int compile_id,
1137 int entry_bci,
1138 CodeOffsets* offsets,
1139 int orig_pc_offset,
1140 DebugInformationRecorder* debug_info,
1141 Dependencies* dependencies,
1142 CodeBuffer* code_buffer, int frame_size,
1143 OopMapSet* oop_maps,
1144 ExceptionHandlerTable* handler_table,
1145 ImplicitExceptionTable* nul_chk_table,
1146 AbstractCompiler* compiler,
1147 CompLevel comp_level
1148 #if INCLUDE_JVMCI
1149 , char* speculations,
1150 int speculations_len,
1151 JVMCINMethodData* jvmci_data
1152 #endif
1153 )
1154 {
1155 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
1156 code_buffer->finalize_oop_references(method);
1157 // create nmethod
1158 nmethod* nm = nullptr;
1159 int nmethod_size = CodeBlob::allocation_size(code_buffer, sizeof(nmethod));
1160
1161 int immutable_data_size =
1162 adjust_pcs_size(debug_info->pcs_size())
1163 + align_up((int)dependencies->size_in_bytes(), oopSize)
1164 + align_up(handler_table->size_in_bytes() , oopSize)
1165 + align_up(nul_chk_table->size_in_bytes() , oopSize)
1166 #if INCLUDE_JVMCI
1167 + align_up(speculations_len , oopSize)
1171 // First, allocate space for immutable data in C heap.
1172 address immutable_data = nullptr;
1173 if (immutable_data_size > 0) {
1174 immutable_data = (address)os::malloc(immutable_data_size, mtCode);
1175 if (immutable_data == nullptr) {
1176 vm_exit_out_of_memory(immutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for immutable data");
1177 return nullptr;
1178 }
1179 }
1180
1181 int mutable_data_size = required_mutable_data_size(code_buffer
1182 JVMCI_ONLY(COMMA (compiler->is_jvmci() ? jvmci_data->size() : 0)));
1183
1184 {
1185 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1186
1187 nm = new (nmethod_size, comp_level)
1188 nmethod(method(), compiler->type(), nmethod_size, immutable_data_size, mutable_data_size,
1189 compile_id, entry_bci, immutable_data, offsets, orig_pc_offset,
1190 debug_info, dependencies, code_buffer, frame_size, oop_maps,
1191 handler_table, nul_chk_table, compiler, comp_level
1192 #if INCLUDE_JVMCI
1193 , speculations,
1194 speculations_len,
1195 jvmci_data
1196 #endif
1197 );
1198
1199 if (nm != nullptr) {
1200 // To make dependency checking during class loading fast, record
1201 // the nmethod dependencies in the classes it is dependent on.
1202 // This allows the dependency checking code to simply walk the
1203 // class hierarchy above the loaded class, checking only nmethods
1204 // which are dependent on those classes. The slow way is to
1205 // check every nmethod for dependencies which makes it linear in
1206 // the number of methods compiled. For applications with a lot
1207 // classes the slow way is too slow.
1208 for (Dependencies::DepStream deps(nm); deps.next(); ) {
1209 if (deps.type() == Dependencies::call_site_target_value) {
1210 // CallSite dependencies are managed on per-CallSite instance basis.
1211 oop call_site = deps.argument_oop(0);
1212 MethodHandles::add_dependent_nmethod(call_site, nm);
1213 } else {
1214 InstanceKlass* ik = deps.context_type();
1215 if (ik == nullptr) {
1216 continue; // ignore things like evol_method
1217 }
1218 // record this nmethod as dependent on this klass
1219 ik->add_dependent_nmethod(nm);
1220 }
1221 }
1222 NOT_PRODUCT(if (nm != nullptr) note_java_nmethod(nm));
1223 }
1224 }
1225 // Do verification and logging outside CodeCache_lock.
1226 if (nm != nullptr) {
1227 // Safepoints in nmethod::verify aren't allowed because nm hasn't been installed yet.
1228 DEBUG_ONLY(nm->verify();)
1229 nm->log_new_nmethod();
1230 }
1231 return nm;
1232 }
1233
1234 // Fill in default values for various fields
1235 void nmethod::init_defaults(CodeBuffer *code_buffer, CodeOffsets* offsets) {
1236 // avoid uninitialized fields, even for short time periods
1237 _exception_cache = nullptr;
1238 _gc_data = nullptr;
1239 _oops_do_mark_link = nullptr;
1240 _compiled_ic_data = nullptr;
1241
1242 _is_unloading_state = 0;
1243 _state = not_installed;
1244
1245 _has_unsafe_access = 0;
1246 _has_method_handle_invokes = 0;
1247 _has_wide_vectors = 0;
1248 _has_monitors = 0;
1249 _has_scoped_access = 0;
1250 _has_flushed_dependencies = 0;
1251 _is_unlinked = 0;
1252 _load_reported = 0; // jvmti state
1253
1254 _deoptimization_status = not_marked;
1255
1256 // SECT_CONSTS is first in code buffer so the offset should be 0.
1257 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1258 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1259
1260 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1261
1262 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1263 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1264
1265 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1266 }
1267
1268 // Post initialization
1269 void nmethod::post_init() {
1270 clear_unloading_state();
1271
1272 finalize_relocations();
1273
1306
1307 _osr_entry_point = nullptr;
1308 _pc_desc_container = nullptr;
1309 _entry_bci = InvocationEntryBci;
1310 _compile_id = compile_id;
1311 _comp_level = CompLevel_none;
1312 _compiler_type = type;
1313 _orig_pc_offset = 0;
1314 _num_stack_arg_slots = 0;
1315
1316 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1317 // Continuation enter intrinsic
1318 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1319 } else {
1320 _exception_offset = 0;
1321 }
1322 // Native wrappers do not have deopt handlers. Make the values
1323 // something that will never match a pc like the nmethod vtable entry
1324 _deopt_handler_offset = 0;
1325 _deopt_mh_handler_offset = 0;
1326 _unwind_handler_offset = 0;
1327
1328 CHECKED_CAST(_oops_size, uint16_t, align_up(code_buffer->total_oop_size(), oopSize));
1329 uint16_t metadata_size;
1330 CHECKED_CAST(metadata_size, uint16_t, align_up(code_buffer->total_metadata_size(), wordSize));
1331 JVMCI_ONLY( _metadata_size = metadata_size; )
1332 assert(_mutable_data_size == _relocation_size + metadata_size,
1333 "wrong mutable data size: %d != %d + %d",
1334 _mutable_data_size, _relocation_size, metadata_size);
1335
1336 // native wrapper does not have read-only data but we need unique not null address
1337 _immutable_data = blob_end();
1338 _immutable_data_size = 0;
1339 _nul_chk_table_offset = 0;
1340 _handler_table_offset = 0;
1341 _scopes_pcs_offset = 0;
1342 _scopes_data_offset = 0;
1343 #if INCLUDE_JVMCI
1344 _speculations_offset = 0;
1345 #endif
1365 // This is both handled in decode2(), called via print_code() -> decode()
1366 if (PrintNativeNMethods) {
1367 tty->print_cr("-------------------------- Assembly (native nmethod) ---------------------------");
1368 print_code();
1369 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1370 #if defined(SUPPORT_DATA_STRUCTS)
1371 if (AbstractDisassembler::show_structs()) {
1372 if (oop_maps != nullptr) {
1373 tty->print("oop maps:"); // oop_maps->print_on(tty) outputs a cr() at the beginning
1374 oop_maps->print_on(tty);
1375 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1376 }
1377 }
1378 #endif
1379 } else {
1380 print(); // print the header part only.
1381 }
1382 #if defined(SUPPORT_DATA_STRUCTS)
1383 if (AbstractDisassembler::show_structs()) {
1384 if (PrintRelocations) {
1385 print_relocations();
1386 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1387 }
1388 }
1389 #endif
1390 if (xtty != nullptr) {
1391 xtty->tail("print_native_nmethod");
1392 }
1393 }
1394 }
1395
1396 void* nmethod::operator new(size_t size, int nmethod_size, int comp_level) throw () {
1397 return CodeCache::allocate(nmethod_size, CodeCache::get_code_blob_type(comp_level));
1398 }
1399
1400 void* nmethod::operator new(size_t size, int nmethod_size, bool allow_NonNMethod_space) throw () {
1401 // Try MethodNonProfiled and MethodProfiled.
1402 void* return_value = CodeCache::allocate(nmethod_size, CodeBlobType::MethodNonProfiled);
1403 if (return_value != nullptr || !allow_NonNMethod_space) return return_value;
1404 // Try NonNMethod or give up.
1405 return CodeCache::allocate(nmethod_size, CodeBlobType::NonNMethod);
1409 nmethod::nmethod(
1410 Method* method,
1411 CompilerType type,
1412 int nmethod_size,
1413 int immutable_data_size,
1414 int mutable_data_size,
1415 int compile_id,
1416 int entry_bci,
1417 address immutable_data,
1418 CodeOffsets* offsets,
1419 int orig_pc_offset,
1420 DebugInformationRecorder* debug_info,
1421 Dependencies* dependencies,
1422 CodeBuffer *code_buffer,
1423 int frame_size,
1424 OopMapSet* oop_maps,
1425 ExceptionHandlerTable* handler_table,
1426 ImplicitExceptionTable* nul_chk_table,
1427 AbstractCompiler* compiler,
1428 CompLevel comp_level
1429 #if INCLUDE_JVMCI
1430 , char* speculations,
1431 int speculations_len,
1432 JVMCINMethodData* jvmci_data
1433 #endif
1434 )
1435 : CodeBlob("nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1436 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1437 _deoptimization_generation(0),
1438 _gc_epoch(CodeCache::gc_epoch()),
1439 _method(method),
1440 _osr_link(nullptr)
1441 {
1442 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
1443 {
1444 DEBUG_ONLY(NoSafepointVerifier nsv;)
1445 assert_locked_or_safepoint(CodeCache_lock);
1446
1447 init_defaults(code_buffer, offsets);
1448
1449 _osr_entry_point = code_begin() + offsets->value(CodeOffsets::OSR_Entry);
1450 _entry_bci = entry_bci;
1451 _compile_id = compile_id;
1452 _comp_level = comp_level;
1453 _compiler_type = type;
1454 _orig_pc_offset = orig_pc_offset;
1455
1456 _num_stack_arg_slots = entry_bci != InvocationEntryBci ? 0 : _method->constMethod()->num_stack_arg_slots();
1457
1458 set_ctable_begin(header_begin() + content_offset());
1459
1460 #if INCLUDE_JVMCI
1461 if (compiler->is_jvmci()) {
1462 // JVMCI might not produce any stub sections
1463 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1464 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1465 } else {
1466 _exception_offset = -1;
1467 }
1558 #if INCLUDE_JVMCI
1559 // Copy speculations to nmethod
1560 if (speculations_size() != 0) {
1561 memcpy(speculations_begin(), speculations, speculations_len);
1562 }
1563 #endif
1564
1565 post_init();
1566
1567 // we use the information of entry points to find out if a method is
1568 // static or non static
1569 assert(compiler->is_c2() || compiler->is_jvmci() ||
1570 _method->is_static() == (entry_point() == verified_entry_point()),
1571 " entry points must be same for static methods and vice versa");
1572 }
1573 }
1574
1575 // Print a short set of xml attributes to identify this nmethod. The
1576 // output should be embedded in some other element.
1577 void nmethod::log_identity(xmlStream* log) const {
1578 log->print(" compile_id='%d'", compile_id());
1579 const char* nm_kind = compile_kind();
1580 if (nm_kind != nullptr) log->print(" compile_kind='%s'", nm_kind);
1581 log->print(" compiler='%s'", compiler_name());
1582 if (TieredCompilation) {
1583 log->print(" level='%d'", comp_level());
1584 }
1585 #if INCLUDE_JVMCI
1586 if (jvmci_nmethod_data() != nullptr) {
1587 const char* jvmci_name = jvmci_nmethod_data()->name();
1588 if (jvmci_name != nullptr) {
1589 log->print(" jvmci_mirror_name='");
1590 log->text("%s", jvmci_name);
1591 log->print("'");
1592 }
1593 }
1594 #endif
1595 }
1596
1597
1598 #define LOG_OFFSET(log, name) \
1599 if (p2i(name##_end()) - p2i(name##_begin())) \
1600 log->print(" " XSTR(name) "_offset='%zd'" , \
1601 p2i(name##_begin()) - p2i(this))
1602
1603
1688 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1689 if (oop_maps() != nullptr) {
1690 tty->print("oop maps:"); // oop_maps()->print_on(tty) outputs a cr() at the beginning
1691 oop_maps()->print_on(tty);
1692 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1693 }
1694 }
1695 #endif
1696 } else {
1697 print(); // print the header part only.
1698 }
1699
1700 #if defined(SUPPORT_DATA_STRUCTS)
1701 if (AbstractDisassembler::show_structs()) {
1702 methodHandle mh(Thread::current(), _method);
1703 if (printmethod || PrintDebugInfo || CompilerOracle::has_option(mh, CompileCommandEnum::PrintDebugInfo)) {
1704 print_scopes();
1705 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1706 }
1707 if (printmethod || PrintRelocations || CompilerOracle::has_option(mh, CompileCommandEnum::PrintRelocations)) {
1708 print_relocations();
1709 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1710 }
1711 if (printmethod || PrintDependencies || CompilerOracle::has_option(mh, CompileCommandEnum::PrintDependencies)) {
1712 print_dependencies_on(tty);
1713 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1714 }
1715 if (printmethod || PrintExceptionHandlers) {
1716 print_handler_table();
1717 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1718 print_nul_chk_table();
1719 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1720 }
1721
1722 if (printmethod) {
1723 print_recorded_oops();
1724 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1725 print_recorded_metadata();
1726 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1727 }
1728 }
1729 #endif
1730
1731 if (xtty != nullptr) {
1732 xtty->tail("print_nmethod");
1733 }
1734 }
1735
1736
1737 // Promote one word from an assembly-time handle to a live embedded oop.
1738 inline void nmethod::initialize_immediate_oop(oop* dest, jobject handle) {
1739 if (handle == nullptr ||
1740 // As a special case, IC oops are initialized to 1 or -1.
1741 handle == (jobject) Universe::non_oop_word()) {
1742 *(void**)dest = handle;
1743 } else {
1744 *dest = JNIHandles::resolve_non_null(handle);
1745 }
1746 }
1747
1748
1749 // Have to have the same name because it's called by a template
1750 void nmethod::copy_values(GrowableArray<jobject>* array) {
1751 int length = array->length();
1752 assert((address)(oops_begin() + length) <= (address)oops_end(), "oops big enough");
1753 oop* dest = oops_begin();
1754 for (int index = 0 ; index < length; index++) {
1755 initialize_immediate_oop(&dest[index], array->at(index));
1756 }
1757
1758 // Now we can fix up all the oops in the code. We need to do this
1759 // in the code because the assembler uses jobjects as placeholders.
1760 // The code and relocations have already been initialized by the
1761 // CodeBlob constructor, so it is valid even at this early point to
1762 // iterate over relocations and patch the code.
1763 fix_oop_relocations(nullptr, nullptr, /*initialize_immediates=*/ true);
1764 }
1765
1766 void nmethod::copy_values(GrowableArray<Metadata*>* array) {
1767 int length = array->length();
1775 void nmethod::fix_oop_relocations(address begin, address end, bool initialize_immediates) {
1776 // re-patch all oop-bearing instructions, just in case some oops moved
1777 RelocIterator iter(this, begin, end);
1778 while (iter.next()) {
1779 if (iter.type() == relocInfo::oop_type) {
1780 oop_Relocation* reloc = iter.oop_reloc();
1781 if (initialize_immediates && reloc->oop_is_immediate()) {
1782 oop* dest = reloc->oop_addr();
1783 jobject obj = *reinterpret_cast<jobject*>(dest);
1784 initialize_immediate_oop(dest, obj);
1785 }
1786 // Refresh the oop-related bits of this instruction.
1787 reloc->fix_oop_relocation();
1788 } else if (iter.type() == relocInfo::metadata_type) {
1789 metadata_Relocation* reloc = iter.metadata_reloc();
1790 reloc->fix_metadata_relocation();
1791 }
1792 }
1793 }
1794
1795 static void install_post_call_nop_displacement(nmethod* nm, address pc) {
1796 NativePostCallNop* nop = nativePostCallNop_at((address) pc);
1797 intptr_t cbaddr = (intptr_t) nm;
1798 intptr_t offset = ((intptr_t) pc) - cbaddr;
1799
1800 int oopmap_slot = nm->oop_maps()->find_slot_for_offset(int((intptr_t) pc - (intptr_t) nm->code_begin()));
1801 if (oopmap_slot < 0) { // this can happen at asynchronous (non-safepoint) stackwalks
1802 log_debug(codecache)("failed to find oopmap for cb: " INTPTR_FORMAT " offset: %d", cbaddr, (int) offset);
1803 } else if (!nop->patch(oopmap_slot, offset)) {
1804 log_debug(codecache)("failed to encode %d %d", oopmap_slot, (int) offset);
1805 }
1806 }
1807
1808 void nmethod::finalize_relocations() {
1809 NoSafepointVerifier nsv;
1810
1811 GrowableArray<NativeMovConstReg*> virtual_call_data;
1812
1813 // Make sure that post call nops fill in nmethod offsets eagerly so
1814 // we don't have to race with deoptimization
1936 Atomic::store(&_gc_epoch, CodeCache::gc_epoch());
1937 }
1938
1939 bool nmethod::is_maybe_on_stack() {
1940 // If the condition below is true, it means that the nmethod was found to
1941 // be alive the previous completed marking cycle.
1942 return Atomic::load(&_gc_epoch) >= CodeCache::previous_completed_gc_marking_cycle();
1943 }
1944
1945 void nmethod::inc_decompile_count() {
1946 if (!is_compiled_by_c2() && !is_compiled_by_jvmci()) return;
1947 // Could be gated by ProfileTraps, but do not bother...
1948 Method* m = method();
1949 if (m == nullptr) return;
1950 MethodData* mdo = m->method_data();
1951 if (mdo == nullptr) return;
1952 // There is a benign race here. See comments in methodData.hpp.
1953 mdo->inc_decompile_count();
1954 }
1955
1956 bool nmethod::try_transition(signed char new_state_int) {
1957 signed char new_state = new_state_int;
1958 assert_lock_strong(NMethodState_lock);
1959 signed char old_state = _state;
1960 if (old_state >= new_state) {
1961 // Ensure monotonicity of transitions.
1962 return false;
1963 }
1964 Atomic::store(&_state, new_state);
1965 return true;
1966 }
1967
1968 void nmethod::invalidate_osr_method() {
1969 assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod");
1970 // Remove from list of active nmethods
1971 if (method() != nullptr) {
1972 method()->method_holder()->remove_osr_nmethod(this);
1973 }
1974 }
1975
1987 }
1988 }
1989
1990 ResourceMark rm;
1991 stringStream ss(NEW_RESOURCE_ARRAY(char, 256), 256);
1992 ss.print("made not entrant: %s", reason);
1993
1994 CompileTask::print_ul(this, ss.freeze());
1995 if (PrintCompilation) {
1996 print_on_with_msg(tty, ss.freeze());
1997 }
1998 }
1999
2000 void nmethod::unlink_from_method() {
2001 if (method() != nullptr) {
2002 method()->unlink_code(this);
2003 }
2004 }
2005
2006 // Invalidate code
2007 bool nmethod::make_not_entrant(const char* reason) {
2008 assert(reason != nullptr, "Must provide a reason");
2009
2010 // This can be called while the system is already at a safepoint which is ok
2011 NoSafepointVerifier nsv;
2012
2013 if (is_unloading()) {
2014 // If the nmethod is unloading, then it is already not entrant through
2015 // the nmethod entry barriers. No need to do anything; GC will unload it.
2016 return false;
2017 }
2018
2019 if (Atomic::load(&_state) == not_entrant) {
2020 // Avoid taking the lock if already in required state.
2021 // This is safe from races because the state is an end-state,
2022 // which the nmethod cannot back out of once entered.
2023 // No need for fencing either.
2024 return false;
2025 }
2026
2027 {
2063 }
2064
2065 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
2066 if (bs_nm == nullptr || !bs_nm->supports_entry_barrier(this)) {
2067 // If nmethod entry barriers are not supported, we won't mark
2068 // nmethods as on-stack when they become on-stack. So we
2069 // degrade to a less accurate flushing strategy, for now.
2070 mark_as_maybe_on_stack();
2071 }
2072
2073 // Change state
2074 bool success = try_transition(not_entrant);
2075 assert(success, "Transition can't fail");
2076
2077 // Log the transition once
2078 log_state_change(reason);
2079
2080 // Remove nmethod from method.
2081 unlink_from_method();
2082
2083 } // leave critical region under NMethodState_lock
2084
2085 #if INCLUDE_JVMCI
2086 // Invalidate can't occur while holding the NMethodState_lock
2087 JVMCINMethodData* nmethod_data = jvmci_nmethod_data();
2088 if (nmethod_data != nullptr) {
2089 nmethod_data->invalidate_nmethod_mirror(this);
2090 }
2091 #endif
2092
2093 #ifdef ASSERT
2094 if (is_osr_method() && method() != nullptr) {
2095 // Make sure osr nmethod is invalidated, i.e. not on the list
2096 bool found = method()->method_holder()->remove_osr_nmethod(this);
2097 assert(!found, "osr nmethod should have been invalidated");
2098 }
2099 #endif
2100
2101 return true;
2102 }
2143
2144 // completely deallocate this method
2145 Events::log_nmethod_flush(Thread::current(), "flushing %s nmethod " INTPTR_FORMAT, is_osr_method() ? "osr" : "", p2i(this));
2146 log_debug(codecache)("*flushing %s nmethod %3d/" INTPTR_FORMAT ". Live blobs:" UINT32_FORMAT
2147 "/Free CodeCache:%zuKb",
2148 is_osr_method() ? "osr" : "",_compile_id, p2i(this), CodeCache::blob_count(),
2149 CodeCache::unallocated_capacity(CodeCache::get_code_blob_type(this))/1024);
2150
2151 // We need to deallocate any ExceptionCache data.
2152 // Note that we do not need to grab the nmethod lock for this, it
2153 // better be thread safe if we're disposing of it!
2154 ExceptionCache* ec = exception_cache();
2155 while(ec != nullptr) {
2156 ExceptionCache* next = ec->next();
2157 delete ec;
2158 ec = next;
2159 }
2160 if (_pc_desc_container != nullptr) {
2161 delete _pc_desc_container;
2162 }
2163 delete[] _compiled_ic_data;
2164
2165 if (_immutable_data != blob_end()) {
2166 os::free(_immutable_data);
2167 _immutable_data = blob_end(); // Valid not null address
2168 }
2169 if (unregister_nmethod) {
2170 Universe::heap()->unregister_nmethod(this);
2171 }
2172 CodeCache::unregister_old_nmethod(this);
2173
2174 CodeBlob::purge();
2175 }
2176
2177 oop nmethod::oop_at(int index) const {
2178 if (index == 0) {
2179 return nullptr;
2180 }
2181
2182 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
2183 return bs_nm->oop_load_no_keepalive(this, index);
2184 }
2185
2206 MethodHandles::clean_dependency_context(call_site);
2207 } else {
2208 InstanceKlass* ik = deps.context_type();
2209 if (ik == nullptr) {
2210 continue; // ignore things like evol_method
2211 }
2212 // During GC liveness of dependee determines class that needs to be updated.
2213 // The GC may clean dependency contexts concurrently and in parallel.
2214 ik->clean_dependency_context();
2215 }
2216 }
2217 }
2218 }
2219
2220 void nmethod::post_compiled_method(CompileTask* task) {
2221 task->mark_success();
2222 task->set_nm_content_size(content_size());
2223 task->set_nm_insts_size(insts_size());
2224 task->set_nm_total_size(total_size());
2225
2226 // JVMTI -- compiled method notification (must be done outside lock)
2227 post_compiled_method_load_event();
2228
2229 if (CompilationLog::log() != nullptr) {
2230 CompilationLog::log()->log_nmethod(JavaThread::current(), this);
2231 }
2232
2233 const DirectiveSet* directive = task->directive();
2234 maybe_print_nmethod(directive);
2235 }
2236
2237 // ------------------------------------------------------------------
2238 // post_compiled_method_load_event
2239 // new method for install_code() path
2240 // Transfer information from compilation to jvmti
2241 void nmethod::post_compiled_method_load_event(JvmtiThreadState* state) {
2242 // This is a bad time for a safepoint. We don't want
2243 // this nmethod to get unloaded while we're queueing the event.
2244 NoSafepointVerifier nsv;
2245
2937
2938 // Make sure all the entry points are correctly aligned for patching.
2939 NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point());
2940
2941 // assert(oopDesc::is_oop(method()), "must be valid");
2942
2943 ResourceMark rm;
2944
2945 if (!CodeCache::contains(this)) {
2946 fatal("nmethod at " INTPTR_FORMAT " not in zone", p2i(this));
2947 }
2948
2949 if(is_native_method() )
2950 return;
2951
2952 nmethod* nm = CodeCache::find_nmethod(verified_entry_point());
2953 if (nm != this) {
2954 fatal("find_nmethod did not find this nmethod (" INTPTR_FORMAT ")", p2i(this));
2955 }
2956
2957 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2958 if (! p->verify(this)) {
2959 tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", p2i(this));
2960 }
2961 }
2962
2963 #ifdef ASSERT
2964 #if INCLUDE_JVMCI
2965 {
2966 // Verify that implicit exceptions that deoptimize have a PcDesc and OopMap
2967 ImmutableOopMapSet* oms = oop_maps();
2968 ImplicitExceptionTable implicit_table(this);
2969 for (uint i = 0; i < implicit_table.len(); i++) {
2970 int exec_offset = (int) implicit_table.get_exec_offset(i);
2971 if (implicit_table.get_exec_offset(i) == implicit_table.get_cont_offset(i)) {
2972 assert(pc_desc_at(code_begin() + exec_offset) != nullptr, "missing PcDesc");
2973 bool found = false;
2974 for (int i = 0, imax = oms->count(); i < imax; i++) {
2975 if (oms->pair_at(i)->pc_offset() == exec_offset) {
2976 found = true;
2977 break;
2978 }
2979 }
2980 assert(found, "missing oopmap");
2981 }
2982 }
2983 }
2984 #endif
2985 #endif
2986
2987 VerifyOopsClosure voc(this);
2988 oops_do(&voc);
2989 assert(voc.ok(), "embedded oops must be OK");
2990 Universe::heap()->verify_nmethod(this);
2991
2992 assert(_oops_do_mark_link == nullptr, "_oops_do_mark_link for %s should be nullptr but is " PTR_FORMAT,
2993 nm->method()->external_name(), p2i(_oops_do_mark_link));
2994 verify_scopes();
2995
2996 CompiledICLocker nm_verify(this);
2997 VerifyMetadataClosure vmc;
2998 metadata_do(&vmc);
2999 }
3000
3001
3002 void nmethod::verify_interrupt_point(address call_site, bool is_inline_cache) {
3003
3004 // Verify IC only when nmethod installation is finished.
3005 if (!is_not_installed()) {
3006 if (CompiledICLocker::is_safe(this)) {
3007 if (is_inline_cache) {
3008 CompiledIC_at(this, call_site);
3009 } else {
3010 CompiledDirectCall::at(call_site);
3011 }
3012 } else {
3013 CompiledICLocker ml_verify(this);
3014 if (is_inline_cache) {
3143 p2i(nul_chk_table_end()),
3144 nul_chk_table_size());
3145 if (handler_table_size() > 0) st->print_cr(" handler table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3146 p2i(handler_table_begin()),
3147 p2i(handler_table_end()),
3148 handler_table_size());
3149 if (scopes_pcs_size () > 0) st->print_cr(" scopes pcs [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3150 p2i(scopes_pcs_begin()),
3151 p2i(scopes_pcs_end()),
3152 scopes_pcs_size());
3153 if (scopes_data_size () > 0) st->print_cr(" scopes data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3154 p2i(scopes_data_begin()),
3155 p2i(scopes_data_end()),
3156 scopes_data_size());
3157 #if INCLUDE_JVMCI
3158 if (speculations_size () > 0) st->print_cr(" speculations [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3159 p2i(speculations_begin()),
3160 p2i(speculations_end()),
3161 speculations_size());
3162 #endif
3163 }
3164
3165 void nmethod::print_code() {
3166 ResourceMark m;
3167 ttyLocker ttyl;
3168 // Call the specialized decode method of this class.
3169 decode(tty);
3170 }
3171
3172 #ifndef PRODUCT // called InstanceKlass methods are available only then. Declared as PRODUCT_RETURN
3173
3174 void nmethod::print_dependencies_on(outputStream* out) {
3175 ResourceMark rm;
3176 stringStream st;
3177 st.print_cr("Dependencies:");
3178 for (Dependencies::DepStream deps(this); deps.next(); ) {
3179 deps.print_dependency(&st);
3180 InstanceKlass* ctxk = deps.context_type();
3181 if (ctxk != nullptr) {
3182 if (ctxk->is_dependent_nmethod(this)) {
3242 st->print("scopes:");
3243 if (scopes_pcs_begin() < scopes_pcs_end()) {
3244 st->cr();
3245 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
3246 if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
3247 continue;
3248
3249 ScopeDesc* sd = scope_desc_at(p->real_pc(this));
3250 while (sd != nullptr) {
3251 sd->print_on(st, p); // print output ends with a newline
3252 sd = sd->sender();
3253 }
3254 }
3255 } else {
3256 st->print_cr(" <list empty>");
3257 }
3258 }
3259 #endif
3260
3261 #ifndef PRODUCT // RelocIterator does support printing only then.
3262 void nmethod::print_relocations() {
3263 ResourceMark m; // in case methods get printed via the debugger
3264 tty->print_cr("relocations:");
3265 RelocIterator iter(this);
3266 iter.print_on(tty);
3267 }
3268 #endif
3269
3270 void nmethod::print_pcs_on(outputStream* st) {
3271 ResourceMark m; // in case methods get printed via debugger
3272 st->print("pc-bytecode offsets:");
3273 if (scopes_pcs_begin() < scopes_pcs_end()) {
3274 st->cr();
3275 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
3276 p->print_on(st, this); // print output ends with a newline
3277 }
3278 } else {
3279 st->print_cr(" <list empty>");
3280 }
3281 }
3282
3283 void nmethod::print_handler_table() {
3284 ExceptionHandlerTable(this).print(code_begin());
3285 }
3286
4052
4053 #endif // !PRODUCT
4054
4055 #if INCLUDE_JVMCI
4056 void nmethod::update_speculation(JavaThread* thread) {
4057 jlong speculation = thread->pending_failed_speculation();
4058 if (speculation != 0) {
4059 guarantee(jvmci_nmethod_data() != nullptr, "failed speculation in nmethod without failed speculation list");
4060 jvmci_nmethod_data()->add_failed_speculation(this, speculation);
4061 thread->set_pending_failed_speculation(0);
4062 }
4063 }
4064
4065 const char* nmethod::jvmci_name() {
4066 if (jvmci_nmethod_data() != nullptr) {
4067 return jvmci_nmethod_data()->name();
4068 }
4069 return nullptr;
4070 }
4071 #endif
|
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 "asm/assembler.inline.hpp"
26 #include "code/aotCodeCache.hpp"
27 #include "code/codeCache.hpp"
28 #include "code/compiledIC.hpp"
29 #include "code/dependencies.hpp"
30 #include "code/nativeInst.hpp"
31 #include "code/nmethod.inline.hpp"
32 #include "code/relocInfo.hpp"
33 #include "code/scopeDesc.hpp"
34 #include "compiler/abstractCompiler.hpp"
35 #include "compiler/compilationLog.hpp"
36 #include "compiler/compileBroker.hpp"
37 #include "compiler/compileLog.hpp"
38 #include "compiler/compileTask.hpp"
39 #include "compiler/compilerDirectives.hpp"
40 #include "compiler/compilerOracle.hpp"
41 #include "compiler/directivesParser.hpp"
42 #include "compiler/disassembler.hpp"
43 #include "compiler/oopMap.inline.hpp"
44 #include "gc/shared/barrierSet.hpp"
45 #include "gc/shared/barrierSetNMethod.hpp"
46 #include "gc/shared/classUnloadingContext.hpp"
772
773 void nmethod::clear_inline_caches() {
774 assert(SafepointSynchronize::is_at_safepoint(), "clearing of IC's only allowed at safepoint");
775 RelocIterator iter(this);
776 while (iter.next()) {
777 iter.reloc()->clear_inline_cache();
778 }
779 }
780
781 #ifdef ASSERT
782 // Check class_loader is alive for this bit of metadata.
783 class CheckClass : public MetadataClosure {
784 void do_metadata(Metadata* md) {
785 Klass* klass = nullptr;
786 if (md->is_klass()) {
787 klass = ((Klass*)md);
788 } else if (md->is_method()) {
789 klass = ((Method*)md)->method_holder();
790 } else if (md->is_methodData()) {
791 klass = ((MethodData*)md)->method()->method_holder();
792 } else if (md->is_methodCounters()) {
793 klass = ((MethodCounters*)md)->method()->method_holder();
794 } else {
795 md->print();
796 ShouldNotReachHere();
797 }
798 assert(klass->is_loader_alive(), "must be alive");
799 }
800 };
801 #endif // ASSERT
802
803
804 static void clean_ic_if_metadata_is_dead(CompiledIC *ic) {
805 ic->clean_metadata();
806 }
807
808 // Clean references to unloaded nmethods at addr from this one, which is not unloaded.
809 template <typename CallsiteT>
810 static void clean_if_nmethod_is_unloaded(CallsiteT* callsite, nmethod* from,
811 bool clean_all) {
812 CodeBlob* cb = CodeCache::find_blob(callsite->destination());
813 if (!cb->is_nmethod()) {
1118 nm = new (native_nmethod_size, allow_NonNMethod_space)
1119 nmethod(method(), compiler_none, native_nmethod_size,
1120 compile_id, &offsets,
1121 code_buffer, frame_size,
1122 basic_lock_owner_sp_offset,
1123 basic_lock_sp_offset,
1124 oop_maps, mutable_data_size);
1125 DEBUG_ONLY( if (allow_NonNMethod_space) assert_no_oops_or_metadata(nm); )
1126 NOT_PRODUCT(if (nm != nullptr) native_nmethod_stats.note_native_nmethod(nm));
1127 }
1128
1129 if (nm != nullptr) {
1130 // verify nmethod
1131 DEBUG_ONLY(nm->verify();) // might block
1132
1133 nm->log_new_nmethod();
1134 }
1135 return nm;
1136 }
1137
1138 void nmethod::record_nmethod_dependency() {
1139 // To make dependency checking during class loading fast, record
1140 // the nmethod dependencies in the classes it is dependent on.
1141 // This allows the dependency checking code to simply walk the
1142 // class hierarchy above the loaded class, checking only nmethods
1143 // which are dependent on those classes. The slow way is to
1144 // check every nmethod for dependencies which makes it linear in
1145 // the number of methods compiled. For applications with a lot
1146 // classes the slow way is too slow.
1147 for (Dependencies::DepStream deps(this); deps.next(); ) {
1148 if (deps.type() == Dependencies::call_site_target_value) {
1149 // CallSite dependencies are managed on per-CallSite instance basis.
1150 oop call_site = deps.argument_oop(0);
1151 MethodHandles::add_dependent_nmethod(call_site, this);
1152 } else {
1153 InstanceKlass* ik = deps.context_type();
1154 if (ik == nullptr) {
1155 continue; // ignore things like evol_method
1156 }
1157 // record this nmethod as dependent on this klass
1158 ik->add_dependent_nmethod(this);
1159 }
1160 }
1161 }
1162
1163 nmethod* nmethod::new_nmethod(const methodHandle& method,
1164 int compile_id,
1165 int entry_bci,
1166 CodeOffsets* offsets,
1167 int orig_pc_offset,
1168 DebugInformationRecorder* debug_info,
1169 Dependencies* dependencies,
1170 CodeBuffer* code_buffer, int frame_size,
1171 OopMapSet* oop_maps,
1172 ExceptionHandlerTable* handler_table,
1173 ImplicitExceptionTable* nul_chk_table,
1174 AbstractCompiler* compiler,
1175 CompLevel comp_level
1176 , AOTCodeEntry* aot_code_entry
1177 #if INCLUDE_JVMCI
1178 , char* speculations,
1179 int speculations_len,
1180 JVMCINMethodData* jvmci_data
1181 #endif
1182 )
1183 {
1184 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
1185 code_buffer->finalize_oop_references(method);
1186 // create nmethod
1187 nmethod* nm = nullptr;
1188 int nmethod_size = CodeBlob::allocation_size(code_buffer, sizeof(nmethod));
1189
1190 int immutable_data_size =
1191 adjust_pcs_size(debug_info->pcs_size())
1192 + align_up((int)dependencies->size_in_bytes(), oopSize)
1193 + align_up(handler_table->size_in_bytes() , oopSize)
1194 + align_up(nul_chk_table->size_in_bytes() , oopSize)
1195 #if INCLUDE_JVMCI
1196 + align_up(speculations_len , oopSize)
1200 // First, allocate space for immutable data in C heap.
1201 address immutable_data = nullptr;
1202 if (immutable_data_size > 0) {
1203 immutable_data = (address)os::malloc(immutable_data_size, mtCode);
1204 if (immutable_data == nullptr) {
1205 vm_exit_out_of_memory(immutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for immutable data");
1206 return nullptr;
1207 }
1208 }
1209
1210 int mutable_data_size = required_mutable_data_size(code_buffer
1211 JVMCI_ONLY(COMMA (compiler->is_jvmci() ? jvmci_data->size() : 0)));
1212
1213 {
1214 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1215
1216 nm = new (nmethod_size, comp_level)
1217 nmethod(method(), compiler->type(), nmethod_size, immutable_data_size, mutable_data_size,
1218 compile_id, entry_bci, immutable_data, offsets, orig_pc_offset,
1219 debug_info, dependencies, code_buffer, frame_size, oop_maps,
1220 handler_table, nul_chk_table, compiler, comp_level, aot_code_entry
1221 #if INCLUDE_JVMCI
1222 , speculations,
1223 speculations_len,
1224 jvmci_data
1225 #endif
1226 );
1227
1228 if (nm != nullptr) {
1229 nm->record_nmethod_dependency();
1230 NOT_PRODUCT(note_java_nmethod(nm));
1231 }
1232 }
1233 // Do verification and logging outside CodeCache_lock.
1234 if (nm != nullptr) {
1235
1236 #ifdef ASSERT
1237 LogTarget(Debug, aot, codecache, nmethod) log;
1238 if (log.is_enabled()) {
1239 LogStream out(log);
1240 out.print_cr("== new_nmethod 2");
1241 FlagSetting fs(PrintRelocations, true);
1242 nm->print_on_impl(&out);
1243 nm->decode(&out);
1244 }
1245 #endif
1246
1247 // Safepoints in nmethod::verify aren't allowed because nm hasn't been installed yet.
1248 DEBUG_ONLY(nm->verify();)
1249 nm->log_new_nmethod();
1250 }
1251 return nm;
1252 }
1253
1254 void nmethod::restore_from_archive(nmethod* archived_nm,
1255 const methodHandle& method,
1256 int compile_id,
1257 address reloc_data,
1258 GrowableArray<Handle>& oop_list,
1259 GrowableArray<Metadata*>& metadata_list,
1260 ImmutableOopMapSet* oop_maps,
1261 address immutable_data,
1262 GrowableArray<Handle>& reloc_imm_oop_list,
1263 GrowableArray<Metadata*>& reloc_imm_metadata_list,
1264 #ifndef PRODUCT
1265 AsmRemarks& archived_asm_remarks,
1266 DbgStrings& archived_dbg_strings,
1267 #endif /* PRODUCT */
1268 AOTCodeReader* aot_code_reader)
1269 {
1270 archived_nm->copy_to((address)this);
1271 set_name("nmethod");
1272 set_method(method());
1273
1274 _compile_id = compile_id;
1275 // allocate _mutable_data before copying relocation data because relocation data is now stored as part of mutable data area
1276 if (archived_nm->mutable_data_size() > 0) {
1277 _mutable_data = (address)os::malloc(archived_nm->mutable_data_size(), mtCode);
1278 if (_mutable_data == nullptr) {
1279 vm_exit_out_of_memory(archived_nm->mutable_data_size(), OOM_MALLOC_ERROR, "codebuffer: no space for mutable data");
1280 }
1281 }
1282 memcpy((address)relocation_begin(), reloc_data, archived_nm->relocation_size());
1283 set_oop_maps(oop_maps);
1284 set_immutable_data(immutable_data);
1285 copy_values(&oop_list);
1286 copy_values(&metadata_list);
1287
1288 aot_code_reader->apply_relocations(this, reloc_imm_oop_list, reloc_imm_metadata_list);
1289
1290 #ifndef PRODUCT
1291 AsmRemarks::init(asm_remarks());
1292 use_remarks(archived_asm_remarks);
1293 archived_asm_remarks.clear();
1294 DbgStrings::init(dbg_strings());
1295 use_strings(archived_dbg_strings);
1296 archived_dbg_strings.clear();
1297 #endif /* PRODUCT */
1298
1299 // Flush the code block
1300 ICache::invalidate_range(code_begin(), code_size());
1301
1302 // Create cache after PcDesc data is copied - it will be used to initialize cache
1303 _pc_desc_container = new PcDescContainer(scopes_pcs_begin());
1304
1305 set_aot_code_entry(aot_code_reader->aot_code_entry());
1306
1307 post_init();
1308 }
1309
1310 nmethod* nmethod::new_nmethod(nmethod* archived_nm,
1311 const methodHandle& method,
1312 AbstractCompiler* compiler,
1313 int compile_id,
1314 address reloc_data,
1315 GrowableArray<Handle>& oop_list,
1316 GrowableArray<Metadata*>& metadata_list,
1317 ImmutableOopMapSet* oop_maps,
1318 address immutable_data,
1319 GrowableArray<Handle>& reloc_imm_oop_list,
1320 GrowableArray<Metadata*>& reloc_imm_metadata_list,
1321 #ifndef PRODUCT
1322 AsmRemarks& asm_remarks,
1323 DbgStrings& dbg_strings,
1324 #endif /* PRODUCT */
1325 AOTCodeReader* aot_code_reader)
1326 {
1327 nmethod* nm = nullptr;
1328 int nmethod_size = archived_nm->size();
1329 // create nmethod
1330 {
1331 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1332 nm = (nmethod *)CodeCache::allocate(nmethod_size, CodeCache::get_code_blob_type(archived_nm->comp_level()));
1333 if (nm != nullptr) {
1334 nm->restore_from_archive(archived_nm,
1335 method,
1336 compile_id,
1337 reloc_data,
1338 oop_list,
1339 metadata_list,
1340 oop_maps,
1341 immutable_data,
1342 reloc_imm_oop_list,
1343 reloc_imm_metadata_list,
1344 NOT_PRODUCT_ARG(asm_remarks)
1345 NOT_PRODUCT_ARG(dbg_strings)
1346 aot_code_reader);
1347 nm->record_nmethod_dependency();
1348 NOT_PRODUCT(note_java_nmethod(nm));
1349 }
1350 }
1351 // Do verification and logging outside CodeCache_lock.
1352 if (nm != nullptr) {
1353 #ifdef ASSERT
1354 LogTarget(Debug, aot, codecache, nmethod) log;
1355 if (log.is_enabled()) {
1356 LogStream out(log);
1357 out.print_cr("== new_nmethod 2");
1358 FlagSetting fs(PrintRelocations, true);
1359 nm->print_on_impl(&out);
1360 nm->decode(&out);
1361 }
1362 #endif
1363 // Safepoints in nmethod::verify aren't allowed because nm hasn't been installed yet.
1364 DEBUG_ONLY(nm->verify();)
1365 nm->log_new_nmethod();
1366 }
1367 return nm;
1368 }
1369
1370 // Fill in default values for various fields
1371 void nmethod::init_defaults(CodeBuffer *code_buffer, CodeOffsets* offsets) {
1372 // avoid uninitialized fields, even for short time periods
1373 _exception_cache = nullptr;
1374 _gc_data = nullptr;
1375 _oops_do_mark_link = nullptr;
1376 _compiled_ic_data = nullptr;
1377
1378 _is_unloading_state = 0;
1379 _state = not_installed;
1380
1381 _has_unsafe_access = 0;
1382 _has_method_handle_invokes = 0;
1383 _has_wide_vectors = 0;
1384 _has_monitors = 0;
1385 _has_scoped_access = 0;
1386 _has_flushed_dependencies = 0;
1387 _is_unlinked = 0;
1388 _load_reported = 0; // jvmti state
1389 _preloaded = 0;
1390 _has_clinit_barriers = 0;
1391
1392 _used = false;
1393 _deoptimization_status = not_marked;
1394
1395 // SECT_CONSTS is first in code buffer so the offset should be 0.
1396 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1397 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1398
1399 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1400
1401 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1402 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1403
1404 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1405 }
1406
1407 // Post initialization
1408 void nmethod::post_init() {
1409 clear_unloading_state();
1410
1411 finalize_relocations();
1412
1445
1446 _osr_entry_point = nullptr;
1447 _pc_desc_container = nullptr;
1448 _entry_bci = InvocationEntryBci;
1449 _compile_id = compile_id;
1450 _comp_level = CompLevel_none;
1451 _compiler_type = type;
1452 _orig_pc_offset = 0;
1453 _num_stack_arg_slots = 0;
1454
1455 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1456 // Continuation enter intrinsic
1457 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1458 } else {
1459 _exception_offset = 0;
1460 }
1461 // Native wrappers do not have deopt handlers. Make the values
1462 // something that will never match a pc like the nmethod vtable entry
1463 _deopt_handler_offset = 0;
1464 _deopt_mh_handler_offset = 0;
1465 _aot_code_entry = nullptr;
1466 _method_profiling_count = 0;
1467 _unwind_handler_offset = 0;
1468
1469 CHECKED_CAST(_oops_size, uint16_t, align_up(code_buffer->total_oop_size(), oopSize));
1470 uint16_t metadata_size;
1471 CHECKED_CAST(metadata_size, uint16_t, align_up(code_buffer->total_metadata_size(), wordSize));
1472 JVMCI_ONLY( _metadata_size = metadata_size; )
1473 assert(_mutable_data_size == _relocation_size + metadata_size,
1474 "wrong mutable data size: %d != %d + %d",
1475 _mutable_data_size, _relocation_size, metadata_size);
1476
1477 // native wrapper does not have read-only data but we need unique not null address
1478 _immutable_data = blob_end();
1479 _immutable_data_size = 0;
1480 _nul_chk_table_offset = 0;
1481 _handler_table_offset = 0;
1482 _scopes_pcs_offset = 0;
1483 _scopes_data_offset = 0;
1484 #if INCLUDE_JVMCI
1485 _speculations_offset = 0;
1486 #endif
1506 // This is both handled in decode2(), called via print_code() -> decode()
1507 if (PrintNativeNMethods) {
1508 tty->print_cr("-------------------------- Assembly (native nmethod) ---------------------------");
1509 print_code();
1510 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1511 #if defined(SUPPORT_DATA_STRUCTS)
1512 if (AbstractDisassembler::show_structs()) {
1513 if (oop_maps != nullptr) {
1514 tty->print("oop maps:"); // oop_maps->print_on(tty) outputs a cr() at the beginning
1515 oop_maps->print_on(tty);
1516 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1517 }
1518 }
1519 #endif
1520 } else {
1521 print(); // print the header part only.
1522 }
1523 #if defined(SUPPORT_DATA_STRUCTS)
1524 if (AbstractDisassembler::show_structs()) {
1525 if (PrintRelocations) {
1526 print_relocations_on(tty);
1527 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1528 }
1529 }
1530 #endif
1531 if (xtty != nullptr) {
1532 xtty->tail("print_native_nmethod");
1533 }
1534 }
1535 }
1536
1537 void* nmethod::operator new(size_t size, int nmethod_size, int comp_level) throw () {
1538 return CodeCache::allocate(nmethod_size, CodeCache::get_code_blob_type(comp_level));
1539 }
1540
1541 void* nmethod::operator new(size_t size, int nmethod_size, bool allow_NonNMethod_space) throw () {
1542 // Try MethodNonProfiled and MethodProfiled.
1543 void* return_value = CodeCache::allocate(nmethod_size, CodeBlobType::MethodNonProfiled);
1544 if (return_value != nullptr || !allow_NonNMethod_space) return return_value;
1545 // Try NonNMethod or give up.
1546 return CodeCache::allocate(nmethod_size, CodeBlobType::NonNMethod);
1550 nmethod::nmethod(
1551 Method* method,
1552 CompilerType type,
1553 int nmethod_size,
1554 int immutable_data_size,
1555 int mutable_data_size,
1556 int compile_id,
1557 int entry_bci,
1558 address immutable_data,
1559 CodeOffsets* offsets,
1560 int orig_pc_offset,
1561 DebugInformationRecorder* debug_info,
1562 Dependencies* dependencies,
1563 CodeBuffer *code_buffer,
1564 int frame_size,
1565 OopMapSet* oop_maps,
1566 ExceptionHandlerTable* handler_table,
1567 ImplicitExceptionTable* nul_chk_table,
1568 AbstractCompiler* compiler,
1569 CompLevel comp_level
1570 , AOTCodeEntry* aot_code_entry
1571 #if INCLUDE_JVMCI
1572 , char* speculations,
1573 int speculations_len,
1574 JVMCINMethodData* jvmci_data
1575 #endif
1576 )
1577 : CodeBlob("nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1578 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1579 _deoptimization_generation(0),
1580 _gc_epoch(CodeCache::gc_epoch()),
1581 _method(method),
1582 _osr_link(nullptr)
1583 {
1584 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
1585 {
1586 DEBUG_ONLY(NoSafepointVerifier nsv;)
1587 assert_locked_or_safepoint(CodeCache_lock);
1588
1589 init_defaults(code_buffer, offsets);
1590 _aot_code_entry = aot_code_entry;
1591 _method_profiling_count = 0;
1592
1593 _osr_entry_point = code_begin() + offsets->value(CodeOffsets::OSR_Entry);
1594 _entry_bci = entry_bci;
1595 _compile_id = compile_id;
1596 _comp_level = comp_level;
1597 _compiler_type = type;
1598 _orig_pc_offset = orig_pc_offset;
1599
1600 _num_stack_arg_slots = entry_bci != InvocationEntryBci ? 0 : _method->constMethod()->num_stack_arg_slots();
1601
1602 set_ctable_begin(header_begin() + content_offset());
1603
1604 #if INCLUDE_JVMCI
1605 if (compiler->is_jvmci()) {
1606 // JVMCI might not produce any stub sections
1607 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1608 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1609 } else {
1610 _exception_offset = -1;
1611 }
1702 #if INCLUDE_JVMCI
1703 // Copy speculations to nmethod
1704 if (speculations_size() != 0) {
1705 memcpy(speculations_begin(), speculations, speculations_len);
1706 }
1707 #endif
1708
1709 post_init();
1710
1711 // we use the information of entry points to find out if a method is
1712 // static or non static
1713 assert(compiler->is_c2() || compiler->is_jvmci() ||
1714 _method->is_static() == (entry_point() == verified_entry_point()),
1715 " entry points must be same for static methods and vice versa");
1716 }
1717 }
1718
1719 // Print a short set of xml attributes to identify this nmethod. The
1720 // output should be embedded in some other element.
1721 void nmethod::log_identity(xmlStream* log) const {
1722 assert(log->inside_attrs_or_error(), "printing attributes");
1723 log->print(" code_compile_id='%d'", compile_id());
1724 const char* nm_kind = compile_kind();
1725 if (nm_kind != nullptr) log->print(" code_compile_kind='%s'", nm_kind);
1726 log->print(" code_compiler='%s'", compiler_name());
1727 if (TieredCompilation) {
1728 log->print(" code_compile_level='%d'", comp_level());
1729 }
1730 #if INCLUDE_JVMCI
1731 if (jvmci_nmethod_data() != nullptr) {
1732 const char* jvmci_name = jvmci_nmethod_data()->name();
1733 if (jvmci_name != nullptr) {
1734 log->print(" jvmci_mirror_name='");
1735 log->text("%s", jvmci_name);
1736 log->print("'");
1737 }
1738 }
1739 #endif
1740 }
1741
1742
1743 #define LOG_OFFSET(log, name) \
1744 if (p2i(name##_end()) - p2i(name##_begin())) \
1745 log->print(" " XSTR(name) "_offset='%zd'" , \
1746 p2i(name##_begin()) - p2i(this))
1747
1748
1833 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1834 if (oop_maps() != nullptr) {
1835 tty->print("oop maps:"); // oop_maps()->print_on(tty) outputs a cr() at the beginning
1836 oop_maps()->print_on(tty);
1837 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1838 }
1839 }
1840 #endif
1841 } else {
1842 print(); // print the header part only.
1843 }
1844
1845 #if defined(SUPPORT_DATA_STRUCTS)
1846 if (AbstractDisassembler::show_structs()) {
1847 methodHandle mh(Thread::current(), _method);
1848 if (printmethod || PrintDebugInfo || CompilerOracle::has_option(mh, CompileCommandEnum::PrintDebugInfo)) {
1849 print_scopes();
1850 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1851 }
1852 if (printmethod || PrintRelocations || CompilerOracle::has_option(mh, CompileCommandEnum::PrintRelocations)) {
1853 print_relocations_on(tty);
1854 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1855 }
1856 if (printmethod || PrintDependencies || CompilerOracle::has_option(mh, CompileCommandEnum::PrintDependencies)) {
1857 print_dependencies_on(tty);
1858 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1859 }
1860 if (printmethod || PrintExceptionHandlers) {
1861 print_handler_table();
1862 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1863 print_nul_chk_table();
1864 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1865 }
1866
1867 if (printmethod) {
1868 print_recorded_oops();
1869 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1870 print_recorded_metadata();
1871 tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
1872 }
1873 }
1874 #endif
1875
1876 if (xtty != nullptr) {
1877 xtty->tail("print_nmethod");
1878 }
1879 }
1880
1881
1882 // Promote one word from an assembly-time handle to a live embedded oop.
1883 inline void nmethod::initialize_immediate_oop(oop* dest, jobject handle) {
1884 if (handle == nullptr ||
1885 // As a special case, IC oops are initialized to 1 or -1.
1886 handle == (jobject) Universe::non_oop_word()) {
1887 *(void**)dest = handle;
1888 } else {
1889 *dest = JNIHandles::resolve_non_null(handle);
1890 }
1891 }
1892
1893 void nmethod::copy_values(GrowableArray<Handle>* array) {
1894 int length = array->length();
1895 assert((address)(oops_begin() + length) <= (address)oops_end(), "oops big enough");
1896 oop* dest = oops_begin();
1897 for (int index = 0 ; index < length; index++) {
1898 dest[index] = array->at(index)();
1899 }
1900 }
1901
1902 // Have to have the same name because it's called by a template
1903 void nmethod::copy_values(GrowableArray<jobject>* array) {
1904 int length = array->length();
1905 assert((address)(oops_begin() + length) <= (address)oops_end(), "oops big enough");
1906 oop* dest = oops_begin();
1907 for (int index = 0 ; index < length; index++) {
1908 initialize_immediate_oop(&dest[index], array->at(index));
1909 }
1910
1911 // Now we can fix up all the oops in the code. We need to do this
1912 // in the code because the assembler uses jobjects as placeholders.
1913 // The code and relocations have already been initialized by the
1914 // CodeBlob constructor, so it is valid even at this early point to
1915 // iterate over relocations and patch the code.
1916 fix_oop_relocations(nullptr, nullptr, /*initialize_immediates=*/ true);
1917 }
1918
1919 void nmethod::copy_values(GrowableArray<Metadata*>* array) {
1920 int length = array->length();
1928 void nmethod::fix_oop_relocations(address begin, address end, bool initialize_immediates) {
1929 // re-patch all oop-bearing instructions, just in case some oops moved
1930 RelocIterator iter(this, begin, end);
1931 while (iter.next()) {
1932 if (iter.type() == relocInfo::oop_type) {
1933 oop_Relocation* reloc = iter.oop_reloc();
1934 if (initialize_immediates && reloc->oop_is_immediate()) {
1935 oop* dest = reloc->oop_addr();
1936 jobject obj = *reinterpret_cast<jobject*>(dest);
1937 initialize_immediate_oop(dest, obj);
1938 }
1939 // Refresh the oop-related bits of this instruction.
1940 reloc->fix_oop_relocation();
1941 } else if (iter.type() == relocInfo::metadata_type) {
1942 metadata_Relocation* reloc = iter.metadata_reloc();
1943 reloc->fix_metadata_relocation();
1944 }
1945 }
1946 }
1947
1948 void nmethod::create_reloc_immediates_list(JavaThread* thread, GrowableArray<Handle>& oop_list, GrowableArray<Metadata*>& metadata_list) {
1949 RelocIterator iter(this);
1950 while (iter.next()) {
1951 if (iter.type() == relocInfo::oop_type) {
1952 oop_Relocation* reloc = iter.oop_reloc();
1953 if (reloc->oop_is_immediate()) {
1954 oop dest = reloc->oop_value();
1955 Handle h(thread, dest);
1956 oop_list.append(h);
1957 }
1958 } else if (iter.type() == relocInfo::metadata_type) {
1959 metadata_Relocation* reloc = iter.metadata_reloc();
1960 if (reloc->metadata_is_immediate()) {
1961 Metadata* m = reloc->metadata_value();
1962 metadata_list.append(m);
1963 }
1964 }
1965 }
1966 }
1967
1968 static void install_post_call_nop_displacement(nmethod* nm, address pc) {
1969 NativePostCallNop* nop = nativePostCallNop_at((address) pc);
1970 intptr_t cbaddr = (intptr_t) nm;
1971 intptr_t offset = ((intptr_t) pc) - cbaddr;
1972
1973 int oopmap_slot = nm->oop_maps()->find_slot_for_offset(int((intptr_t) pc - (intptr_t) nm->code_begin()));
1974 if (oopmap_slot < 0) { // this can happen at asynchronous (non-safepoint) stackwalks
1975 log_debug(codecache)("failed to find oopmap for cb: " INTPTR_FORMAT " offset: %d", cbaddr, (int) offset);
1976 } else if (!nop->patch(oopmap_slot, offset)) {
1977 log_debug(codecache)("failed to encode %d %d", oopmap_slot, (int) offset);
1978 }
1979 }
1980
1981 void nmethod::finalize_relocations() {
1982 NoSafepointVerifier nsv;
1983
1984 GrowableArray<NativeMovConstReg*> virtual_call_data;
1985
1986 // Make sure that post call nops fill in nmethod offsets eagerly so
1987 // we don't have to race with deoptimization
2109 Atomic::store(&_gc_epoch, CodeCache::gc_epoch());
2110 }
2111
2112 bool nmethod::is_maybe_on_stack() {
2113 // If the condition below is true, it means that the nmethod was found to
2114 // be alive the previous completed marking cycle.
2115 return Atomic::load(&_gc_epoch) >= CodeCache::previous_completed_gc_marking_cycle();
2116 }
2117
2118 void nmethod::inc_decompile_count() {
2119 if (!is_compiled_by_c2() && !is_compiled_by_jvmci()) return;
2120 // Could be gated by ProfileTraps, but do not bother...
2121 Method* m = method();
2122 if (m == nullptr) return;
2123 MethodData* mdo = m->method_data();
2124 if (mdo == nullptr) return;
2125 // There is a benign race here. See comments in methodData.hpp.
2126 mdo->inc_decompile_count();
2127 }
2128
2129 void nmethod::inc_method_profiling_count() {
2130 Atomic::inc(&_method_profiling_count);
2131 }
2132
2133 uint64_t nmethod::method_profiling_count() {
2134 return _method_profiling_count;
2135 }
2136
2137 bool nmethod::try_transition(signed char new_state_int) {
2138 signed char new_state = new_state_int;
2139 assert_lock_strong(NMethodState_lock);
2140 signed char old_state = _state;
2141 if (old_state >= new_state) {
2142 // Ensure monotonicity of transitions.
2143 return false;
2144 }
2145 Atomic::store(&_state, new_state);
2146 return true;
2147 }
2148
2149 void nmethod::invalidate_osr_method() {
2150 assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod");
2151 // Remove from list of active nmethods
2152 if (method() != nullptr) {
2153 method()->method_holder()->remove_osr_nmethod(this);
2154 }
2155 }
2156
2168 }
2169 }
2170
2171 ResourceMark rm;
2172 stringStream ss(NEW_RESOURCE_ARRAY(char, 256), 256);
2173 ss.print("made not entrant: %s", reason);
2174
2175 CompileTask::print_ul(this, ss.freeze());
2176 if (PrintCompilation) {
2177 print_on_with_msg(tty, ss.freeze());
2178 }
2179 }
2180
2181 void nmethod::unlink_from_method() {
2182 if (method() != nullptr) {
2183 method()->unlink_code(this);
2184 }
2185 }
2186
2187 // Invalidate code
2188 bool nmethod::make_not_entrant(const char* reason, bool make_not_entrant) {
2189 assert(reason != nullptr, "Must provide a reason");
2190
2191 // This can be called while the system is already at a safepoint which is ok
2192 NoSafepointVerifier nsv;
2193
2194 if (is_unloading()) {
2195 // If the nmethod is unloading, then it is already not entrant through
2196 // the nmethod entry barriers. No need to do anything; GC will unload it.
2197 return false;
2198 }
2199
2200 if (Atomic::load(&_state) == not_entrant) {
2201 // Avoid taking the lock if already in required state.
2202 // This is safe from races because the state is an end-state,
2203 // which the nmethod cannot back out of once entered.
2204 // No need for fencing either.
2205 return false;
2206 }
2207
2208 {
2244 }
2245
2246 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
2247 if (bs_nm == nullptr || !bs_nm->supports_entry_barrier(this)) {
2248 // If nmethod entry barriers are not supported, we won't mark
2249 // nmethods as on-stack when they become on-stack. So we
2250 // degrade to a less accurate flushing strategy, for now.
2251 mark_as_maybe_on_stack();
2252 }
2253
2254 // Change state
2255 bool success = try_transition(not_entrant);
2256 assert(success, "Transition can't fail");
2257
2258 // Log the transition once
2259 log_state_change(reason);
2260
2261 // Remove nmethod from method.
2262 unlink_from_method();
2263
2264 if (make_not_entrant) {
2265 // Keep cached code if it was simply replaced
2266 // otherwise make it not entrant too.
2267 AOTCodeCache::invalidate(_aot_code_entry);
2268 }
2269
2270 CompileBroker::log_not_entrant(this);
2271 } // leave critical region under NMethodState_lock
2272
2273 #if INCLUDE_JVMCI
2274 // Invalidate can't occur while holding the NMethodState_lock
2275 JVMCINMethodData* nmethod_data = jvmci_nmethod_data();
2276 if (nmethod_data != nullptr) {
2277 nmethod_data->invalidate_nmethod_mirror(this);
2278 }
2279 #endif
2280
2281 #ifdef ASSERT
2282 if (is_osr_method() && method() != nullptr) {
2283 // Make sure osr nmethod is invalidated, i.e. not on the list
2284 bool found = method()->method_holder()->remove_osr_nmethod(this);
2285 assert(!found, "osr nmethod should have been invalidated");
2286 }
2287 #endif
2288
2289 return true;
2290 }
2331
2332 // completely deallocate this method
2333 Events::log_nmethod_flush(Thread::current(), "flushing %s nmethod " INTPTR_FORMAT, is_osr_method() ? "osr" : "", p2i(this));
2334 log_debug(codecache)("*flushing %s nmethod %3d/" INTPTR_FORMAT ". Live blobs:" UINT32_FORMAT
2335 "/Free CodeCache:%zuKb",
2336 is_osr_method() ? "osr" : "",_compile_id, p2i(this), CodeCache::blob_count(),
2337 CodeCache::unallocated_capacity(CodeCache::get_code_blob_type(this))/1024);
2338
2339 // We need to deallocate any ExceptionCache data.
2340 // Note that we do not need to grab the nmethod lock for this, it
2341 // better be thread safe if we're disposing of it!
2342 ExceptionCache* ec = exception_cache();
2343 while(ec != nullptr) {
2344 ExceptionCache* next = ec->next();
2345 delete ec;
2346 ec = next;
2347 }
2348 if (_pc_desc_container != nullptr) {
2349 delete _pc_desc_container;
2350 }
2351 if (_compiled_ic_data != nullptr) {
2352 delete[] _compiled_ic_data;
2353 }
2354
2355 if (_immutable_data != data_end() && !AOTCodeCache::is_address_in_aot_cache((address)_oop_maps)) {
2356 os::free(_immutable_data);
2357 _immutable_data = blob_end(); // Valid not null address
2358 }
2359 if (unregister_nmethod) {
2360 Universe::heap()->unregister_nmethod(this);
2361 }
2362 CodeCache::unregister_old_nmethod(this);
2363
2364 CodeBlob::purge();
2365 }
2366
2367 oop nmethod::oop_at(int index) const {
2368 if (index == 0) {
2369 return nullptr;
2370 }
2371
2372 BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
2373 return bs_nm->oop_load_no_keepalive(this, index);
2374 }
2375
2396 MethodHandles::clean_dependency_context(call_site);
2397 } else {
2398 InstanceKlass* ik = deps.context_type();
2399 if (ik == nullptr) {
2400 continue; // ignore things like evol_method
2401 }
2402 // During GC liveness of dependee determines class that needs to be updated.
2403 // The GC may clean dependency contexts concurrently and in parallel.
2404 ik->clean_dependency_context();
2405 }
2406 }
2407 }
2408 }
2409
2410 void nmethod::post_compiled_method(CompileTask* task) {
2411 task->mark_success();
2412 task->set_nm_content_size(content_size());
2413 task->set_nm_insts_size(insts_size());
2414 task->set_nm_total_size(total_size());
2415
2416 // task->is_aot() is true only for loaded cached code.
2417 // nmethod::_aot_code_entry is set for loaded and stored cached code
2418 // to invalidate the entry when nmethod is deoptimized.
2419 // There is option to not store in archive cached code.
2420 guarantee((_aot_code_entry != nullptr) || !task->is_aot() || VerifyCachedCode, "sanity");
2421
2422 // JVMTI -- compiled method notification (must be done outside lock)
2423 post_compiled_method_load_event();
2424
2425 if (CompilationLog::log() != nullptr) {
2426 CompilationLog::log()->log_nmethod(JavaThread::current(), this);
2427 }
2428
2429 const DirectiveSet* directive = task->directive();
2430 maybe_print_nmethod(directive);
2431 }
2432
2433 // ------------------------------------------------------------------
2434 // post_compiled_method_load_event
2435 // new method for install_code() path
2436 // Transfer information from compilation to jvmti
2437 void nmethod::post_compiled_method_load_event(JvmtiThreadState* state) {
2438 // This is a bad time for a safepoint. We don't want
2439 // this nmethod to get unloaded while we're queueing the event.
2440 NoSafepointVerifier nsv;
2441
3133
3134 // Make sure all the entry points are correctly aligned for patching.
3135 NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point());
3136
3137 // assert(oopDesc::is_oop(method()), "must be valid");
3138
3139 ResourceMark rm;
3140
3141 if (!CodeCache::contains(this)) {
3142 fatal("nmethod at " INTPTR_FORMAT " not in zone", p2i(this));
3143 }
3144
3145 if(is_native_method() )
3146 return;
3147
3148 nmethod* nm = CodeCache::find_nmethod(verified_entry_point());
3149 if (nm != this) {
3150 fatal("find_nmethod did not find this nmethod (" INTPTR_FORMAT ")", p2i(this));
3151 }
3152
3153 // Verification can triggered during shutdown after AOTCodeCache is closed.
3154 // If the Scopes data is in the AOT code cache, then we should avoid verification during shutdown.
3155 if (!is_aot() || AOTCodeCache::is_on()) {
3156 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
3157 if (! p->verify(this)) {
3158 tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", p2i(this));
3159 }
3160 }
3161
3162 #ifdef ASSERT
3163 #if INCLUDE_JVMCI
3164 {
3165 // Verify that implicit exceptions that deoptimize have a PcDesc and OopMap
3166 ImmutableOopMapSet* oms = oop_maps();
3167 ImplicitExceptionTable implicit_table(this);
3168 for (uint i = 0; i < implicit_table.len(); i++) {
3169 int exec_offset = (int) implicit_table.get_exec_offset(i);
3170 if (implicit_table.get_exec_offset(i) == implicit_table.get_cont_offset(i)) {
3171 assert(pc_desc_at(code_begin() + exec_offset) != nullptr, "missing PcDesc");
3172 bool found = false;
3173 for (int i = 0, imax = oms->count(); i < imax; i++) {
3174 if (oms->pair_at(i)->pc_offset() == exec_offset) {
3175 found = true;
3176 break;
3177 }
3178 }
3179 assert(found, "missing oopmap");
3180 }
3181 }
3182 }
3183 #endif
3184 #endif
3185 }
3186
3187 VerifyOopsClosure voc(this);
3188 oops_do(&voc);
3189 assert(voc.ok(), "embedded oops must be OK");
3190 Universe::heap()->verify_nmethod(this);
3191
3192 assert(_oops_do_mark_link == nullptr, "_oops_do_mark_link for %s should be nullptr but is " PTR_FORMAT,
3193 nm->method()->external_name(), p2i(_oops_do_mark_link));
3194 if (!is_aot() || AOTCodeCache::is_on()) {
3195 verify_scopes();
3196 }
3197
3198 CompiledICLocker nm_verify(this);
3199 VerifyMetadataClosure vmc;
3200 metadata_do(&vmc);
3201 }
3202
3203
3204 void nmethod::verify_interrupt_point(address call_site, bool is_inline_cache) {
3205
3206 // Verify IC only when nmethod installation is finished.
3207 if (!is_not_installed()) {
3208 if (CompiledICLocker::is_safe(this)) {
3209 if (is_inline_cache) {
3210 CompiledIC_at(this, call_site);
3211 } else {
3212 CompiledDirectCall::at(call_site);
3213 }
3214 } else {
3215 CompiledICLocker ml_verify(this);
3216 if (is_inline_cache) {
3345 p2i(nul_chk_table_end()),
3346 nul_chk_table_size());
3347 if (handler_table_size() > 0) st->print_cr(" handler table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3348 p2i(handler_table_begin()),
3349 p2i(handler_table_end()),
3350 handler_table_size());
3351 if (scopes_pcs_size () > 0) st->print_cr(" scopes pcs [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3352 p2i(scopes_pcs_begin()),
3353 p2i(scopes_pcs_end()),
3354 scopes_pcs_size());
3355 if (scopes_data_size () > 0) st->print_cr(" scopes data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3356 p2i(scopes_data_begin()),
3357 p2i(scopes_data_end()),
3358 scopes_data_size());
3359 #if INCLUDE_JVMCI
3360 if (speculations_size () > 0) st->print_cr(" speculations [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
3361 p2i(speculations_begin()),
3362 p2i(speculations_end()),
3363 speculations_size());
3364 #endif
3365 if (AOTCodeCache::is_on() && _aot_code_entry != nullptr) {
3366 _aot_code_entry->print(st);
3367 }
3368 }
3369
3370 void nmethod::print_code() {
3371 ResourceMark m;
3372 ttyLocker ttyl;
3373 // Call the specialized decode method of this class.
3374 decode(tty);
3375 }
3376
3377 #ifndef PRODUCT // called InstanceKlass methods are available only then. Declared as PRODUCT_RETURN
3378
3379 void nmethod::print_dependencies_on(outputStream* out) {
3380 ResourceMark rm;
3381 stringStream st;
3382 st.print_cr("Dependencies:");
3383 for (Dependencies::DepStream deps(this); deps.next(); ) {
3384 deps.print_dependency(&st);
3385 InstanceKlass* ctxk = deps.context_type();
3386 if (ctxk != nullptr) {
3387 if (ctxk->is_dependent_nmethod(this)) {
3447 st->print("scopes:");
3448 if (scopes_pcs_begin() < scopes_pcs_end()) {
3449 st->cr();
3450 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
3451 if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
3452 continue;
3453
3454 ScopeDesc* sd = scope_desc_at(p->real_pc(this));
3455 while (sd != nullptr) {
3456 sd->print_on(st, p); // print output ends with a newline
3457 sd = sd->sender();
3458 }
3459 }
3460 } else {
3461 st->print_cr(" <list empty>");
3462 }
3463 }
3464 #endif
3465
3466 #ifndef PRODUCT // RelocIterator does support printing only then.
3467 void nmethod::print_relocations_on(outputStream* st) {
3468 ResourceMark m; // in case methods get printed via the debugger
3469 st->print_cr("relocations:");
3470 RelocIterator iter(this);
3471 iter.print_on(st);
3472 }
3473 #endif
3474
3475 void nmethod::print_pcs_on(outputStream* st) {
3476 ResourceMark m; // in case methods get printed via debugger
3477 st->print("pc-bytecode offsets:");
3478 if (scopes_pcs_begin() < scopes_pcs_end()) {
3479 st->cr();
3480 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
3481 p->print_on(st, this); // print output ends with a newline
3482 }
3483 } else {
3484 st->print_cr(" <list empty>");
3485 }
3486 }
3487
3488 void nmethod::print_handler_table() {
3489 ExceptionHandlerTable(this).print(code_begin());
3490 }
3491
4257
4258 #endif // !PRODUCT
4259
4260 #if INCLUDE_JVMCI
4261 void nmethod::update_speculation(JavaThread* thread) {
4262 jlong speculation = thread->pending_failed_speculation();
4263 if (speculation != 0) {
4264 guarantee(jvmci_nmethod_data() != nullptr, "failed speculation in nmethod without failed speculation list");
4265 jvmci_nmethod_data()->add_failed_speculation(this, speculation);
4266 thread->set_pending_failed_speculation(0);
4267 }
4268 }
4269
4270 const char* nmethod::jvmci_name() {
4271 if (jvmci_nmethod_data() != nullptr) {
4272 return jvmci_nmethod_data()->name();
4273 }
4274 return nullptr;
4275 }
4276 #endif
4277
4278 void nmethod::prepare_for_archiving() {
4279 CodeBlob::prepare_for_archiving();
4280 _deoptimization_generation = 0;
4281 _gc_epoch = 0;
4282 _method_profiling_count = 0;
4283 _osr_link = nullptr;
4284 _method = nullptr;
4285 _immutable_data = nullptr;
4286 _pc_desc_container = nullptr;
4287 _exception_cache = nullptr;
4288 _gc_data = nullptr;
4289 _oops_do_mark_link = nullptr;
4290 _compiled_ic_data = nullptr;
4291 _osr_entry_point = nullptr;
4292 _compile_id = -1;
4293 _deoptimization_status = not_marked;
4294 _is_unloading_state = 0;
4295 _state = not_installed;
4296 }
|