0 /*
1  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  *
22  */
23 
24 #include "precompiled.hpp"
25 #include "jvm.h"
26 #include "code/codeCache.hpp"
27 #include "code/compiledIC.hpp"
28 #include "code/compiledMethod.inline.hpp"
29 #include "code/dependencies.hpp"
30 #include "code/nativeInst.hpp"
31 #include "code/nmethod.hpp"
32 #include "code/scopeDesc.hpp"
33 #include "compiler/abstractCompiler.hpp"
34 #include "compiler/compileBroker.hpp"
35 #include "compiler/compileLog.hpp"
36 #include "compiler/compilerDirectives.hpp"
37 #include "compiler/directivesParser.hpp"
38 #include "compiler/disassembler.hpp"
39 #include "interpreter/bytecode.hpp"
40 #include "logging/log.hpp"
41 #include "logging/logStream.hpp"
42 #include "memory/allocation.inline.hpp"
43 #include "memory/resourceArea.hpp"
44 #include "oops/access.inline.hpp"
45 #include "oops/method.inline.hpp"
46 #include "oops/methodData.hpp"
47 #include "oops/oop.inline.hpp"
48 #include "prims/jvmtiImpl.hpp"
49 #include "runtime/atomic.hpp"
50 #include "runtime/flags/flagSetting.hpp"
51 #include "runtime/frame.inline.hpp"
52 #include "runtime/handles.inline.hpp"
53 #include "runtime/jniHandles.inline.hpp"
54 #include "runtime/orderAccess.hpp"
55 #include "runtime/os.hpp"
56 #include "runtime/safepointVerifiers.hpp"
57 #include "runtime/sharedRuntime.hpp"
58 #include "runtime/sweeper.hpp"
59 #include "runtime/vmThread.hpp"
60 #include "utilities/align.hpp"
61 #include "utilities/dtrace.hpp"
62 #include "utilities/events.hpp"
63 #include "utilities/resourceHash.hpp"
64 #include "utilities/xmlstream.hpp"
65 #if INCLUDE_JVMCI
66 #include "jvmci/jvmciJavaClasses.hpp"
67 #endif
68 
69 #ifdef DTRACE_ENABLED
70 
71 // Only bother with this argument setup if dtrace is available
72 
73 #define DTRACE_METHOD_UNLOAD_PROBE(method)                                \
74   {                                                                       \
75     Method* m = (method);                                                 \
76     if (m != NULL) {                                                      \
77       Symbol* klass_name = m->klass_name();                               \
78       Symbol* name = m->name();                                           \
79       Symbol* signature = m->signature();                                 \
80       HOTSPOT_COMPILED_METHOD_UNLOAD(                                     \
81         (char *) klass_name->bytes(), klass_name->utf8_length(),                   \
82         (char *) name->bytes(), name->utf8_length(),                               \
83         (char *) signature->bytes(), signature->utf8_length());                    \
84     }                                                                     \
85   }
86 
87 #else //  ndef DTRACE_ENABLED
88 
89 #define DTRACE_METHOD_UNLOAD_PROBE(method)
90 
91 #endif
92 
93 //---------------------------------------------------------------------------------
94 // NMethod statistics
95 // They are printed under various flags, including:
96 //   PrintC1Statistics, PrintOptoStatistics, LogVMOutput, and LogCompilation.
97 // (In the latter two cases, they like other stats are printed to the log only.)
98 
99 #ifndef PRODUCT
100 // These variables are put into one block to reduce relocations
101 // and make it simpler to print from the debugger.
102 struct java_nmethod_stats_struct {
103   int nmethod_count;
104   int total_size;
105   int relocation_size;
106   int consts_size;
107   int insts_size;
108   int stub_size;
109   int scopes_data_size;
110   int scopes_pcs_size;
111   int dependencies_size;
112   int handler_table_size;
113   int nul_chk_table_size;
114   int oops_size;
115   int metadata_size;
116 
117   void note_nmethod(nmethod* nm) {
118     nmethod_count += 1;
119     total_size          += nm->size();
120     relocation_size     += nm->relocation_size();
121     consts_size         += nm->consts_size();
122     insts_size          += nm->insts_size();
123     stub_size           += nm->stub_size();
124     oops_size           += nm->oops_size();
125     metadata_size       += nm->metadata_size();
126     scopes_data_size    += nm->scopes_data_size();
127     scopes_pcs_size     += nm->scopes_pcs_size();
128     dependencies_size   += nm->dependencies_size();
129     handler_table_size  += nm->handler_table_size();
130     nul_chk_table_size  += nm->nul_chk_table_size();
131   }
132   void print_nmethod_stats(const char* name) {
133     if (nmethod_count == 0)  return;
134     tty->print_cr("Statistics for %d bytecoded nmethods for %s:", nmethod_count, name);
135     if (total_size != 0)          tty->print_cr(" total in heap  = %d", total_size);
136     if (nmethod_count != 0)       tty->print_cr(" header         = " SIZE_FORMAT, nmethod_count * sizeof(nmethod));
137     if (relocation_size != 0)     tty->print_cr(" relocation     = %d", relocation_size);
138     if (consts_size != 0)         tty->print_cr(" constants      = %d", consts_size);
139     if (insts_size != 0)          tty->print_cr(" main code      = %d", insts_size);
140     if (stub_size != 0)           tty->print_cr(" stub code      = %d", stub_size);
141     if (oops_size != 0)           tty->print_cr(" oops           = %d", oops_size);
142     if (metadata_size != 0)       tty->print_cr(" metadata       = %d", metadata_size);
143     if (scopes_data_size != 0)    tty->print_cr(" scopes data    = %d", scopes_data_size);
144     if (scopes_pcs_size != 0)     tty->print_cr(" scopes pcs     = %d", scopes_pcs_size);
145     if (dependencies_size != 0)   tty->print_cr(" dependencies   = %d", dependencies_size);
146     if (handler_table_size != 0)  tty->print_cr(" handler table  = %d", handler_table_size);
147     if (nul_chk_table_size != 0)  tty->print_cr(" nul chk table  = %d", nul_chk_table_size);
148   }
149 };
150 
151 struct native_nmethod_stats_struct {
152   int native_nmethod_count;
153   int native_total_size;
154   int native_relocation_size;
155   int native_insts_size;
156   int native_oops_size;
157   int native_metadata_size;
158   void note_native_nmethod(nmethod* nm) {
159     native_nmethod_count += 1;
160     native_total_size       += nm->size();
161     native_relocation_size  += nm->relocation_size();
162     native_insts_size       += nm->insts_size();
163     native_oops_size        += nm->oops_size();
164     native_metadata_size    += nm->metadata_size();
165   }
166   void print_native_nmethod_stats() {
167     if (native_nmethod_count == 0)  return;
168     tty->print_cr("Statistics for %d native nmethods:", native_nmethod_count);
169     if (native_total_size != 0)       tty->print_cr(" N. total size  = %d", native_total_size);
170     if (native_relocation_size != 0)  tty->print_cr(" N. relocation  = %d", native_relocation_size);
171     if (native_insts_size != 0)       tty->print_cr(" N. main code   = %d", native_insts_size);
172     if (native_oops_size != 0)        tty->print_cr(" N. oops        = %d", native_oops_size);
173     if (native_metadata_size != 0)    tty->print_cr(" N. metadata    = %d", native_metadata_size);
174   }
175 };
176 
177 struct pc_nmethod_stats_struct {
178   int pc_desc_resets;   // number of resets (= number of caches)
179   int pc_desc_queries;  // queries to nmethod::find_pc_desc
180   int pc_desc_approx;   // number of those which have approximate true
181   int pc_desc_repeats;  // number of _pc_descs[0] hits
182   int pc_desc_hits;     // number of LRU cache hits
183   int pc_desc_tests;    // total number of PcDesc examinations
184   int pc_desc_searches; // total number of quasi-binary search steps
185   int pc_desc_adds;     // number of LUR cache insertions
186 
187   void print_pc_stats() {
188     tty->print_cr("PcDesc Statistics:  %d queries, %.2f comparisons per query",
189                   pc_desc_queries,
190                   (double)(pc_desc_tests + pc_desc_searches)
191                   / pc_desc_queries);
192     tty->print_cr("  caches=%d queries=%d/%d, hits=%d+%d, tests=%d+%d, adds=%d",
193                   pc_desc_resets,
194                   pc_desc_queries, pc_desc_approx,
195                   pc_desc_repeats, pc_desc_hits,
196                   pc_desc_tests, pc_desc_searches, pc_desc_adds);
197   }
198 };
199 
200 #ifdef COMPILER1
201 static java_nmethod_stats_struct c1_java_nmethod_stats;
202 #endif
203 #ifdef COMPILER2
204 static java_nmethod_stats_struct c2_java_nmethod_stats;
205 #endif
206 #if INCLUDE_JVMCI
207 static java_nmethod_stats_struct jvmci_java_nmethod_stats;
208 #endif
209 static java_nmethod_stats_struct unknown_java_nmethod_stats;
210 
211 static native_nmethod_stats_struct native_nmethod_stats;
212 static pc_nmethod_stats_struct pc_nmethod_stats;
213 
214 static void note_java_nmethod(nmethod* nm) {
215 #ifdef COMPILER1
216   if (nm->is_compiled_by_c1()) {
217     c1_java_nmethod_stats.note_nmethod(nm);
218   } else
219 #endif
220 #ifdef COMPILER2
221   if (nm->is_compiled_by_c2()) {
222     c2_java_nmethod_stats.note_nmethod(nm);
223   } else
224 #endif
225 #if INCLUDE_JVMCI
226   if (nm->is_compiled_by_jvmci()) {
227     jvmci_java_nmethod_stats.note_nmethod(nm);
228   } else
229 #endif
230   {
231     unknown_java_nmethod_stats.note_nmethod(nm);
232   }
233 }
234 #endif // !PRODUCT
235 
236 //---------------------------------------------------------------------------------
237 
238 
239 ExceptionCache::ExceptionCache(Handle exception, address pc, address handler) {
240   assert(pc != NULL, "Must be non null");
241   assert(exception.not_null(), "Must be non null");
242   assert(handler != NULL, "Must be non null");
243 
244   _count = 0;
245   _exception_type = exception->klass();
246   _next = NULL;
247   _purge_list_next = NULL;
248 
249   add_address_and_handler(pc,handler);
250 }
251 
252 
253 address ExceptionCache::match(Handle exception, address pc) {
254   assert(pc != NULL,"Must be non null");
255   assert(exception.not_null(),"Must be non null");
256   if (exception->klass() == exception_type()) {
257     return (test_address(pc));
258   }
259 
260   return NULL;
261 }
262 
263 
264 bool ExceptionCache::match_exception_with_space(Handle exception) {
265   assert(exception.not_null(),"Must be non null");
266   if (exception->klass() == exception_type() && count() < cache_size) {
267     return true;
268   }
269   return false;
270 }
271 
272 
273 address ExceptionCache::test_address(address addr) {
274   int limit = count();
275   for (int i = 0; i < limit; i++) {
276     if (pc_at(i) == addr) {
277       return handler_at(i);
278     }
279   }
280   return NULL;
281 }
282 
283 
284 bool ExceptionCache::add_address_and_handler(address addr, address handler) {
285   if (test_address(addr) == handler) return true;
286 
287   int index = count();
288   if (index < cache_size) {
289     set_pc_at(index, addr);
290     set_handler_at(index, handler);
291     increment_count();
292     return true;
293   }
294   return false;
295 }
296 
297 ExceptionCache* ExceptionCache::next() {
298   return Atomic::load(&_next);
299 }
300 
301 void ExceptionCache::set_next(ExceptionCache *ec) {
302   Atomic::store(ec, &_next);
303 }
304 
305 //-----------------------------------------------------------------------------
306 
307 
308 // Helper used by both find_pc_desc methods.
309 static inline bool match_desc(PcDesc* pc, int pc_offset, bool approximate) {
310   NOT_PRODUCT(++pc_nmethod_stats.pc_desc_tests);
311   if (!approximate)
312     return pc->pc_offset() == pc_offset;
313   else
314     return (pc-1)->pc_offset() < pc_offset && pc_offset <= pc->pc_offset();
315 }
316 
317 void PcDescCache::reset_to(PcDesc* initial_pc_desc) {
318   if (initial_pc_desc == NULL) {
319     _pc_descs[0] = NULL; // native method; no PcDescs at all
320     return;
321   }
322   NOT_PRODUCT(++pc_nmethod_stats.pc_desc_resets);
323   // reset the cache by filling it with benign (non-null) values
324   assert(initial_pc_desc->pc_offset() < 0, "must be sentinel");
325   for (int i = 0; i < cache_size; i++)
326     _pc_descs[i] = initial_pc_desc;
327 }
328 
329 PcDesc* PcDescCache::find_pc_desc(int pc_offset, bool approximate) {
330   NOT_PRODUCT(++pc_nmethod_stats.pc_desc_queries);
331   NOT_PRODUCT(if (approximate) ++pc_nmethod_stats.pc_desc_approx);
332 
333   // Note: one might think that caching the most recently
334   // read value separately would be a win, but one would be
335   // wrong.  When many threads are updating it, the cache
336   // line it's in would bounce between caches, negating
337   // any benefit.
338 
339   // In order to prevent race conditions do not load cache elements
340   // repeatedly, but use a local copy:
341   PcDesc* res;
342 
343   // Step one:  Check the most recently added value.
344   res = _pc_descs[0];
345   if (res == NULL) return NULL;  // native method; no PcDescs at all
346   if (match_desc(res, pc_offset, approximate)) {
347     NOT_PRODUCT(++pc_nmethod_stats.pc_desc_repeats);
348     return res;
349   }
350 
351   // Step two:  Check the rest of the LRU cache.
352   for (int i = 1; i < cache_size; ++i) {
353     res = _pc_descs[i];
354     if (res->pc_offset() < 0) break;  // optimization: skip empty cache
355     if (match_desc(res, pc_offset, approximate)) {
356       NOT_PRODUCT(++pc_nmethod_stats.pc_desc_hits);
357       return res;
358     }
359   }
360 
361   // Report failure.
362   return NULL;
363 }
364 
365 void PcDescCache::add_pc_desc(PcDesc* pc_desc) {
366   NOT_PRODUCT(++pc_nmethod_stats.pc_desc_adds);
367   // Update the LRU cache by shifting pc_desc forward.
368   for (int i = 0; i < cache_size; i++)  {
369     PcDesc* next = _pc_descs[i];
370     _pc_descs[i] = pc_desc;
371     pc_desc = next;
372   }
373 }
374 
375 // adjust pcs_size so that it is a multiple of both oopSize and
376 // sizeof(PcDesc) (assumes that if sizeof(PcDesc) is not a multiple
377 // of oopSize, then 2*sizeof(PcDesc) is)
378 static int adjust_pcs_size(int pcs_size) {
379   int nsize = align_up(pcs_size,   oopSize);
380   if ((nsize % sizeof(PcDesc)) != 0) {
381     nsize = pcs_size + sizeof(PcDesc);
382   }
383   assert((nsize % oopSize) == 0, "correct alignment");
384   return nsize;
385 }
386 
387 
388 int nmethod::total_size() const {
389   return
390     consts_size()        +
391     insts_size()         +
392     stub_size()          +
393     scopes_data_size()   +
394     scopes_pcs_size()    +
395     handler_table_size() +
396     nul_chk_table_size();
397 }
398 
399 address* nmethod::orig_pc_addr(const frame* fr) {
400   return (address*) ((address)fr->unextended_sp() + _orig_pc_offset);
401 }
402 
403 const char* nmethod::compile_kind() const {
404   if (is_osr_method())     return "osr";
405   if (method() != NULL && is_native_method())  return "c2n";
406   return NULL;
407 }
408 
409 // Fill in default values for various flag fields
410 void nmethod::init_defaults() {
411   _state                      = not_installed;
412   _has_flushed_dependencies   = 0;
413   _lock_count                 = 0;
414   _stack_traversal_mark       = 0;
415   _unload_reported            = false; // jvmti state
416   _is_far_code                = false; // nmethods are located in CodeCache
417 
418 #ifdef ASSERT
419   _oops_are_stale             = false;
420 #endif
421 
422   _oops_do_mark_link       = NULL;
423   _jmethod_id              = NULL;
424   _osr_link                = NULL;
425   _scavenge_root_link      = NULL;
426   _scavenge_root_state     = 0;
427 #if INCLUDE_RTM_OPT
428   _rtm_state               = NoRTM;
429 #endif
430 #if INCLUDE_JVMCI
431   _jvmci_installed_code   = NULL;
432   _speculation_log        = NULL;
433   _jvmci_installed_code_triggers_invalidation = false;
434 #endif
435 }
436 
437 nmethod* nmethod::new_native_nmethod(const methodHandle& method,
438   int compile_id,
439   CodeBuffer *code_buffer,
440   int vep_offset,
441   int frame_complete,
442   int frame_size,
443   ByteSize basic_lock_owner_sp_offset,
444   ByteSize basic_lock_sp_offset,
445   OopMapSet* oop_maps) {
446   code_buffer->finalize_oop_references(method);
447   // create nmethod
448   nmethod* nm = NULL;
449   {
450     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
451     int native_nmethod_size = CodeBlob::allocation_size(code_buffer, sizeof(nmethod));
452     CodeOffsets offsets;
453     offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
454     offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
455     nm = new (native_nmethod_size, CompLevel_none) nmethod(method(), compiler_none, native_nmethod_size,
456                                             compile_id, &offsets,
457                                             code_buffer, frame_size,
458                                             basic_lock_owner_sp_offset,
459                                             basic_lock_sp_offset, oop_maps);
460     NOT_PRODUCT(if (nm != NULL)  native_nmethod_stats.note_native_nmethod(nm));
461   }
462 
463   if (nm != NULL) {
464     // verify nmethod
465     debug_only(nm->verify();) // might block
466 
467     nm->log_new_nmethod();
468     nm->make_in_use();
469   }
470   return nm;
471 }
472 
473 nmethod* nmethod::new_nmethod(const methodHandle& method,
474   int compile_id,
475   int entry_bci,
476   CodeOffsets* offsets,
477   int orig_pc_offset,
478   DebugInformationRecorder* debug_info,
479   Dependencies* dependencies,
480   CodeBuffer* code_buffer, int frame_size,
481   OopMapSet* oop_maps,
482   ExceptionHandlerTable* handler_table,
483   ImplicitExceptionTable* nul_chk_table,
484   AbstractCompiler* compiler,
485   int comp_level
486 #if INCLUDE_JVMCI
487   , jweak installed_code,
488   jweak speculationLog
489 #endif
490 )
491 {
492   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
493   code_buffer->finalize_oop_references(method);
494   // create nmethod
495   nmethod* nm = NULL;
496   { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
497     int nmethod_size =
498       CodeBlob::allocation_size(code_buffer, sizeof(nmethod))
499       + adjust_pcs_size(debug_info->pcs_size())
500       + align_up((int)dependencies->size_in_bytes(), oopSize)
501       + align_up(handler_table->size_in_bytes()    , oopSize)
502       + align_up(nul_chk_table->size_in_bytes()    , oopSize)
503       + align_up(debug_info->data_size()           , oopSize);
504 
505     nm = new (nmethod_size, comp_level)
506     nmethod(method(), compiler->type(), nmethod_size, compile_id, entry_bci, offsets,
507             orig_pc_offset, debug_info, dependencies, code_buffer, frame_size,
508             oop_maps,
509             handler_table,
510             nul_chk_table,
511             compiler,
512             comp_level
513 #if INCLUDE_JVMCI
514             , installed_code,
515             speculationLog
516 #endif
517             );
518 
519     if (nm != NULL) {
520       // To make dependency checking during class loading fast, record
521       // the nmethod dependencies in the classes it is dependent on.
522       // This allows the dependency checking code to simply walk the
523       // class hierarchy above the loaded class, checking only nmethods
524       // which are dependent on those classes.  The slow way is to
525       // check every nmethod for dependencies which makes it linear in
526       // the number of methods compiled.  For applications with a lot
527       // classes the slow way is too slow.
528       for (Dependencies::DepStream deps(nm); deps.next(); ) {
529         if (deps.type() == Dependencies::call_site_target_value) {
530           // CallSite dependencies are managed on per-CallSite instance basis.
531           oop call_site = deps.argument_oop(0);
532           MethodHandles::add_dependent_nmethod(call_site, nm);
533         } else {
534           Klass* klass = deps.context_type();
535           if (klass == NULL) {
536             continue;  // ignore things like evol_method
537           }
538           // record this nmethod as dependent on this klass
539           InstanceKlass::cast(klass)->add_dependent_nmethod(nm);
540         }
541       }
542       NOT_PRODUCT(if (nm != NULL)  note_java_nmethod(nm));
543     }
544   }
545   // Do verification and logging outside CodeCache_lock.
546   if (nm != NULL) {
547     // Safepoints in nmethod::verify aren't allowed because nm hasn't been installed yet.
548     DEBUG_ONLY(nm->verify();)
549     nm->log_new_nmethod();
550   }
551   return nm;
552 }
553 
554 // For native wrappers
555 nmethod::nmethod(
556   Method* method,
557   CompilerType type,
558   int nmethod_size,
559   int compile_id,
560   CodeOffsets* offsets,
561   CodeBuffer* code_buffer,
562   int frame_size,
563   ByteSize basic_lock_owner_sp_offset,
564   ByteSize basic_lock_sp_offset,
565   OopMapSet* oop_maps )
566   : CompiledMethod(method, "native nmethod", type, nmethod_size, sizeof(nmethod), code_buffer, offsets->value(CodeOffsets::Frame_Comp
567   _is_unloading_state(0),
568   _native_receiver_sp_offset(basic_lock_owner_sp_offset),
569   _native_basic_lock_sp_offset(basic_lock_sp_offset)
570 {
571   {
572     int scopes_data_offset = 0;
573     int deoptimize_offset       = 0;
574     int deoptimize_mh_offset    = 0;
575 
576     debug_only(NoSafepointVerifier nsv;)
577     assert_locked_or_safepoint(CodeCache_lock);
578 
579     init_defaults();
580     _entry_bci               = InvocationEntryBci;
581     // We have no exception handler or deopt handler make the
582     // values something that will never match a pc like the nmethod vtable entry
583     _exception_offset        = 0;
584     _orig_pc_offset          = 0;
585 
586     _consts_offset           = data_offset();
587     _stub_offset             = data_offset();
588     _oops_offset             = data_offset();
589     _metadata_offset         = _oops_offset         + align_up(code_buffer->total_oop_size(), oopSize);
590     scopes_data_offset       = _metadata_offset     + align_up(code_buffer->total_metadata_size(), wordSize);
591     _scopes_pcs_offset       = scopes_data_offset;
592     _dependencies_offset     = _scopes_pcs_offset;
593     _handler_table_offset    = _dependencies_offset;
594     _nul_chk_table_offset    = _handler_table_offset;
595     _nmethod_end_offset      = _nul_chk_table_offset;
596     _compile_id              = compile_id;
597     _comp_level              = CompLevel_none;
598     _entry_point             = code_begin()          + offsets->value(CodeOffsets::Entry);
599     _verified_entry_point    = code_begin()          + offsets->value(CodeOffsets::Verified_Entry);
600     _osr_entry_point         = NULL;
601     _exception_cache         = NULL;
602     _pc_desc_container.reset_to(NULL);
603     _hotness_counter         = NMethodSweeper::hotness_counter_reset_val();
604 
605     _scopes_data_begin = (address) this + scopes_data_offset;
606     _deopt_handler_begin = (address) this + deoptimize_offset;
607     _deopt_mh_handler_begin = (address) this + deoptimize_mh_offset;
608 
609     code_buffer->copy_code_and_locs_to(this);
610     code_buffer->copy_values_to(this);
611 
612     clear_unloading_state();
613     if (ScavengeRootsInCode) {
614       Universe::heap()->register_nmethod(this);
615     }
616     debug_only(Universe::heap()->verify_nmethod(this));
617     CodeCache::commit(this);
618   }
619 
620   if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) {
621     ttyLocker ttyl;  // keep the following output all in one block
622     // This output goes directly to the tty, not the compiler log.
623     // To enable tools to match it up with the compilation activity,
624     // be sure to tag this tty output with the compile ID.
625     if (xtty != NULL) {
626       xtty->begin_head("print_native_nmethod");
627       xtty->method(_method);
628       xtty->stamp();
629       xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
630     }
631     // print the header part first
632     print();
633     // then print the requested information
634     if (PrintNativeNMethods) {
635       print_code();
636       if (oop_maps != NULL) {
637         oop_maps->print();
638       }
639     }
640     if (PrintRelocations) {
641       print_relocations();
642     }
643     if (xtty != NULL) {
644       xtty->tail("print_native_nmethod");
645     }
646   }
647 }
648 
649 void* nmethod::operator new(size_t size, int nmethod_size, int comp_level) throw () {
650   return CodeCache::allocate(nmethod_size, CodeCache::get_code_blob_type(comp_level));
651 }
652 
653 nmethod::nmethod(
654   Method* method,
655   CompilerType type,
656   int nmethod_size,
657   int compile_id,
658   int entry_bci,
659   CodeOffsets* offsets,
660   int orig_pc_offset,
661   DebugInformationRecorder* debug_info,
662   Dependencies* dependencies,
663   CodeBuffer *code_buffer,
664   int frame_size,
665   OopMapSet* oop_maps,
666   ExceptionHandlerTable* handler_table,
667   ImplicitExceptionTable* nul_chk_table,
668   AbstractCompiler* compiler,
669   int comp_level
670 #if INCLUDE_JVMCI
671   , jweak installed_code,
672   jweak speculation_log
673 #endif
674   )
675   : CompiledMethod(method, "nmethod", type, nmethod_size, sizeof(nmethod), code_buffer, offsets->value(CodeOffsets::Frame_Complete), 
676   _is_unloading_state(0),
677   _native_receiver_sp_offset(in_ByteSize(-1)),
678   _native_basic_lock_sp_offset(in_ByteSize(-1))
679 {
680   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
681   {
682     debug_only(NoSafepointVerifier nsv;)
683     assert_locked_or_safepoint(CodeCache_lock);
684 
685     _deopt_handler_begin = (address) this;
686     _deopt_mh_handler_begin = (address) this;
687 
688     init_defaults();
689     _entry_bci               = entry_bci;
690     _compile_id              = compile_id;
691     _comp_level              = comp_level;
692     _orig_pc_offset          = orig_pc_offset;
693     _hotness_counter         = NMethodSweeper::hotness_counter_reset_val();
694 
695     // Section offsets
696     _consts_offset           = content_offset()      + code_buffer->total_offset_of(code_buffer->consts());
697     _stub_offset             = content_offset()      + code_buffer->total_offset_of(code_buffer->stubs());
698     set_ctable_begin(header_begin() + _consts_offset);
699 
700 #if INCLUDE_JVMCI
701     _jvmci_installed_code = installed_code;
702     _speculation_log = speculation_log;
703     oop obj = JNIHandles::resolve(installed_code);
704     if (obj == NULL || (obj->is_a(HotSpotNmethod::klass()) && HotSpotNmethod::isDefault(obj))) {
705       _jvmci_installed_code_triggers_invalidation = false;
706     } else {
707       _jvmci_installed_code_triggers_invalidation = true;
708     }
709 
710     if (compiler->is_jvmci()) {
711       // JVMCI might not produce any stub sections
712       if (offsets->value(CodeOffsets::Exceptions) != -1) {
713         _exception_offset        = code_offset()          + offsets->value(CodeOffsets::Exceptions);
714       } else {
715         _exception_offset = -1;
716       }
717       if (offsets->value(CodeOffsets::Deopt) != -1) {
718         _deopt_handler_begin       = (address) this + code_offset()          + offsets->value(CodeOffsets::Deopt);
719       } else {
720         _deopt_handler_begin = NULL;
721       }
722       if (offsets->value(CodeOffsets::DeoptMH) != -1) {
723         _deopt_mh_handler_begin  = (address) this + code_offset()          + offsets->value(CodeOffsets::DeoptMH);
724       } else {
725         _deopt_mh_handler_begin = NULL;
726       }
727     } else {
728 #endif
729     // Exception handler and deopt handler are in the stub section
730     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must be set");
731     assert(offsets->value(CodeOffsets::Deopt     ) != -1, "must be set");
732 
733     _exception_offset       = _stub_offset          + offsets->value(CodeOffsets::Exceptions);
734     _deopt_handler_begin    = (address) this + _stub_offset          + offsets->value(CodeOffsets::Deopt);
735     if (offsets->value(CodeOffsets::DeoptMH) != -1) {
736       _deopt_mh_handler_begin  = (address) this + _stub_offset          + offsets->value(CodeOffsets::DeoptMH);
737     } else {
738       _deopt_mh_handler_begin  = NULL;
739 #if INCLUDE_JVMCI
740     }
741 #endif
742     }
743     if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
744       _unwind_handler_offset = code_offset()         + offsets->value(CodeOffsets::UnwindHandler);
745     } else {
746       _unwind_handler_offset = -1;
747     }
748 
749     _oops_offset             = data_offset();
750     _metadata_offset         = _oops_offset          + align_up(code_buffer->total_oop_size(), oopSize);
751     int scopes_data_offset   = _metadata_offset      + align_up(code_buffer->total_metadata_size(), wordSize);
752 
753     _scopes_pcs_offset       = scopes_data_offset    + align_up(debug_info->data_size       (), oopSize);
754     _dependencies_offset     = _scopes_pcs_offset    + adjust_pcs_size(debug_info->pcs_size());
755     _handler_table_offset    = _dependencies_offset  + align_up((int)dependencies->size_in_bytes (), oopSize);
756     _nul_chk_table_offset    = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
757     _nmethod_end_offset      = _nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize);
758     _entry_point             = code_begin()          + offsets->value(CodeOffsets::Entry);
759     _verified_entry_point    = code_begin()          + offsets->value(CodeOffsets::Verified_Entry);
760     _osr_entry_point         = code_begin()          + offsets->value(CodeOffsets::OSR_Entry);
761     _exception_cache         = NULL;
762 
763     _scopes_data_begin = (address) this + scopes_data_offset;
764 
765     _pc_desc_container.reset_to(scopes_pcs_begin());
766 
767     code_buffer->copy_code_and_locs_to(this);
768     // Copy contents of ScopeDescRecorder to nmethod
769     code_buffer->copy_values_to(this);
770     debug_info->copy_to(this);
771     dependencies->copy_to(this);
772     clear_unloading_state();
773     if (ScavengeRootsInCode) {
774       Universe::heap()->register_nmethod(this);
775     }
776     debug_only(Universe::heap()->verify_nmethod(this));
777 
778     CodeCache::commit(this);
779 
780     // Copy contents of ExceptionHandlerTable to nmethod
781     handler_table->copy_to(this);
782     nul_chk_table->copy_to(this);
783 
784     // we use the information of entry points to find out if a method is
785     // static or non static
786     assert(compiler->is_c2() || compiler->is_jvmci() ||
787            _method->is_static() == (entry_point() == _verified_entry_point),
788            " entry points must be same for static methods and vice versa");
789   }
790 }
791 
792 // Print a short set of xml attributes to identify this nmethod.  The
793 // output should be embedded in some other element.
794 void nmethod::log_identity(xmlStream* log) const {
795   log->print(" compile_id='%d'", compile_id());
796   const char* nm_kind = compile_kind();
797   if (nm_kind != NULL)  log->print(" compile_kind='%s'", nm_kind);
798   log->print(" compiler='%s'", compiler_name());
799   if (TieredCompilation) {
800     log->print(" level='%d'", comp_level());
801   }
802 #if INCLUDE_JVMCI
803     char buffer[O_BUFLEN];
804     char* jvmci_name = jvmci_installed_code_name(buffer, O_BUFLEN);
805     if (jvmci_name != NULL) {
806       log->print(" jvmci_installed_code_name='");
807       log->text("%s", jvmci_name);
808       log->print("'");
809     }
810 #endif
811 }
812 
813 
814 #define LOG_OFFSET(log, name)                    \
815   if (p2i(name##_end()) - p2i(name##_begin())) \
816     log->print(" " XSTR(name) "_offset='" INTX_FORMAT "'"    , \
817                p2i(name##_begin()) - p2i(this))
818 
819 
820 void nmethod::log_new_nmethod() const {
821   if (LogCompilation && xtty != NULL) {
822     ttyLocker ttyl;
823     HandleMark hm;
824     xtty->begin_elem("nmethod");
825     log_identity(xtty);
826     xtty->print(" entry='" INTPTR_FORMAT "' size='%d'", p2i(code_begin()), size());
827     xtty->print(" address='" INTPTR_FORMAT "'", p2i(this));
828 
829     LOG_OFFSET(xtty, relocation);
830     LOG_OFFSET(xtty, consts);
831     LOG_OFFSET(xtty, insts);
832     LOG_OFFSET(xtty, stub);
833     LOG_OFFSET(xtty, scopes_data);
834     LOG_OFFSET(xtty, scopes_pcs);
835     LOG_OFFSET(xtty, dependencies);
836     LOG_OFFSET(xtty, handler_table);
837     LOG_OFFSET(xtty, nul_chk_table);
838     LOG_OFFSET(xtty, oops);
839     LOG_OFFSET(xtty, metadata);
840 
841     xtty->method(method());
842     xtty->stamp();
843     xtty->end_elem();
844   }
845 }
846 
847 #undef LOG_OFFSET
848 
849 
850 // Print out more verbose output usually for a newly created nmethod.
851 void nmethod::print_on(outputStream* st, const char* msg) const {
852   if (st != NULL) {
853     ttyLocker ttyl;
854     if (WizardMode) {
855       CompileTask::print(st, this, msg, /*short_form:*/ true);
856       st->print_cr(" (" INTPTR_FORMAT ")", p2i(this));
857     } else {
858       CompileTask::print(st, this, msg, /*short_form:*/ false);
859     }
860   }
861 }
862 
863 void nmethod::maybe_print_nmethod(DirectiveSet* directive) {
864   bool printnmethods = directive->PrintAssemblyOption || directive->PrintNMethodsOption;
865   if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) {
866     print_nmethod(printnmethods);
867   }
868 }
869 
870 void nmethod::print_nmethod(bool printmethod) {
871   ttyLocker ttyl;  // keep the following output all in one block
872   if (xtty != NULL) {
873     xtty->begin_head("print_nmethod");
874     xtty->stamp();
875     xtty->end_head();
876   }
877   // print the header part first
878   print();
879   // then print the requested information
880   if (printmethod) {
881     print_code();
882     print_pcs();
883     if (oop_maps()) {
884       oop_maps()->print();
885     }
886   }
887   if (printmethod || PrintDebugInfo || CompilerOracle::has_option_string(_method, "PrintDebugInfo")) {
888     print_scopes();
889   }
890   if (printmethod || PrintRelocations || CompilerOracle::has_option_string(_method, "PrintRelocations")) {
891     print_relocations();
892   }
893   if (printmethod || PrintDependencies || CompilerOracle::has_option_string(_method, "PrintDependencies")) {
894     print_dependencies();
895   }
896   if (printmethod || PrintExceptionHandlers) {
897     print_handler_table();
898     print_nul_chk_table();
899   }
900   if (printmethod) {
901     print_recorded_oops();
902     print_recorded_metadata();
903   }
904   if (xtty != NULL) {
905     xtty->tail("print_nmethod");
906   }
907 }
908 
909 
910 // Promote one word from an assembly-time handle to a live embedded oop.
911 inline void nmethod::initialize_immediate_oop(oop* dest, jobject handle) {
912   if (handle == NULL ||
913       // As a special case, IC oops are initialized to 1 or -1.
914       handle == (jobject) Universe::non_oop_word()) {
915     (*dest) = (oop) handle;
916   } else {
917     (*dest) = JNIHandles::resolve_non_null(handle);
918   }
919 }
920 
921 
922 // Have to have the same name because it's called by a template
923 void nmethod::copy_values(GrowableArray<jobject>* array) {
924   int length = array->length();
925   assert((address)(oops_begin() + length) <= (address)oops_end(), "oops big enough");
926   oop* dest = oops_begin();
927   for (int index = 0 ; index < length; index++) {
928     initialize_immediate_oop(&dest[index], array->at(index));
929   }
930 
931   // Now we can fix up all the oops in the code.  We need to do this
932   // in the code because the assembler uses jobjects as placeholders.
933   // The code and relocations have already been initialized by the
934   // CodeBlob constructor, so it is valid even at this early point to
935   // iterate over relocations and patch the code.
936   fix_oop_relocations(NULL, NULL, /*initialize_immediates=*/ true);
937 }
938 
939 void nmethod::copy_values(GrowableArray<Metadata*>* array) {
940   int length = array->length();
941   assert((address)(metadata_begin() + length) <= (address)metadata_end(), "big enough");
942   Metadata** dest = metadata_begin();
943   for (int index = 0 ; index < length; index++) {
944     dest[index] = array->at(index);
945   }
946 }
947 
948 void nmethod::fix_oop_relocations(address begin, address end, bool initialize_immediates) {
949   // re-patch all oop-bearing instructions, just in case some oops moved
950   RelocIterator iter(this, begin, end);
951   while (iter.next()) {
952     if (iter.type() == relocInfo::oop_type) {
953       oop_Relocation* reloc = iter.oop_reloc();
954       if (initialize_immediates && reloc->oop_is_immediate()) {
955         oop* dest = reloc->oop_addr();
956         initialize_immediate_oop(dest, (jobject) *dest);
957       }
958       // Refresh the oop-related bits of this instruction.
959       reloc->fix_oop_relocation();
960     } else if (iter.type() == relocInfo::metadata_type) {
961       metadata_Relocation* reloc = iter.metadata_reloc();
962       reloc->fix_metadata_relocation();
963     }
964   }
965 }
966 
967 
968 void nmethod::verify_clean_inline_caches() {
969   assert(CompiledICLocker::is_safe(this), "mt unsafe call");
970 
971   ResourceMark rm;
972   RelocIterator iter(this, oops_reloc_begin());
973   while(iter.next()) {
974     switch(iter.type()) {
975       case relocInfo::virtual_call_type:
976       case relocInfo::opt_virtual_call_type: {
977         CompiledIC *ic = CompiledIC_at(&iter);
978         // Ok, to lookup references to zombies here
979         CodeBlob *cb = CodeCache::find_blob_unsafe(ic->ic_destination());
980         assert(cb != NULL, "destination not in CodeBlob?");
981         nmethod* nm = cb->as_nmethod_or_null();
982         if( nm != NULL ) {
983           // Verify that inline caches pointing to both zombie and not_entrant methods are clean
984           if (!nm->is_in_use() || (nm->method()->code() != nm)) {
985             assert(ic->is_clean(), "IC should be clean");
986           }
987         }
988         break;
989       }
990       case relocInfo::static_call_type: {
991         CompiledStaticCall *csc = compiledStaticCall_at(iter.reloc());
992         CodeBlob *cb = CodeCache::find_blob_unsafe(csc->destination());
993         assert(cb != NULL, "destination not in CodeBlob?");
994         nmethod* nm = cb->as_nmethod_or_null();
995         if( nm != NULL ) {
996           // Verify that inline caches pointing to both zombie and not_entrant methods are clean
997           if (!nm->is_in_use() || (nm->method()->code() != nm)) {
998             assert(csc->is_clean(), "IC should be clean");
999           }
1000         }
1001         break;
1002       }
1003       default:
1004         break;
1005     }
1006   }
1007 }
1008 
1009 // This is a private interface with the sweeper.
1010 void nmethod::mark_as_seen_on_stack() {
1011   assert(is_alive(), "Must be an alive method");
1012   // Set the traversal mark to ensure that the sweeper does 2
1013   // cleaning passes before moving to zombie.
1014   set_stack_traversal_mark(NMethodSweeper::traversal_count());
1015 }
1016 
1017 // Tell if a non-entrant method can be converted to a zombie (i.e.,
1018 // there are no activations on the stack, not in use by the VM,
1019 // and not in use by the ServiceThread)
1020 bool nmethod::can_convert_to_zombie() {
1021   // Note that this is called when the sweeper has observed the nmethod to be
1022   // not_entrant. However, with concurrent code cache unloading, the state
1023   // might have moved on to unloaded if it is_unloading(), due to racing
1024   // concurrent GC threads.
1025   assert(is_not_entrant() || is_unloading(), "must be a non-entrant method");
1026 
1027   // Since the nmethod sweeper only does partial sweep the sweeper's traversal
1028   // count can be greater than the stack traversal count before it hits the
1029   // nmethod for the second time.
1030   // If an is_unloading() nmethod is still not_entrant, then it is not safe to
1031   // convert it to zombie due to GC unloading interactions. However, if it
1032   // has become unloaded, then it is okay to convert such nmethods to zombie.
1033   return stack_traversal_mark() + 1 < NMethodSweeper::traversal_count() &&
1034          !is_locked_by_vm() && (!is_unloading() || is_unloaded());
1035 }
1036 
1037 void nmethod::inc_decompile_count() {
1038   if (!is_compiled_by_c2() && !is_compiled_by_jvmci()) return;
1039   // Could be gated by ProfileTraps, but do not bother...
1040   Method* m = method();
1041   if (m == NULL)  return;
1042   MethodData* mdo = m->method_data();
1043   if (mdo == NULL)  return;
1044   // There is a benign race here.  See comments in methodData.hpp.
1045   mdo->inc_decompile_count();
1046 }
1047 
1048 void nmethod::make_unloaded() {
1049   post_compiled_method_unload();
1050 
1051   // This nmethod is being unloaded, make sure that dependencies
1052   // recorded in instanceKlasses get flushed.
1053   // Since this work is being done during a GC, defer deleting dependencies from the
1054   // InstanceKlass.
1055   assert(Universe::heap()->is_gc_active() || Thread::current()->is_ConcurrentGC_thread(),
1056          "should only be called during gc");
1057   flush_dependencies(/*delete_immediately*/false);
1058 
1059   // Break cycle between nmethod & method
1060   LogTarget(Trace, class, unload, nmethod) lt;
1061   if (lt.is_enabled()) {
1062     LogStream ls(lt);
1063     ls.print("making nmethod " INTPTR_FORMAT
1064              " unloadable, Method*(" INTPTR_FORMAT
1065              ") ",
1066              p2i(this), p2i(_method));
1067      ls.cr();
1068   }
1069   // Unlink the osr method, so we do not look this up again
1070   if (is_osr_method()) {
1071     // Invalidate the osr nmethod only once
1072     if (is_in_use()) {
1073       invalidate_osr_method();
1074     }
1075 #ifdef ASSERT
1076     if (method() != NULL) {
1077       // Make sure osr nmethod is invalidated, i.e. not on the list
1078       bool found = method()->method_holder()->remove_osr_nmethod(this);
1079       assert(!found, "osr nmethod should have been invalidated");
1080     }
1081 #endif
1082   }
1083 
1084   // If _method is already NULL the Method* is about to be unloaded,
1085   // so we don't have to break the cycle. Note that it is possible to
1086   // have the Method* live here, in case we unload the nmethod because
1087   // it is pointing to some oop (other than the Method*) being unloaded.
1088   if (_method != NULL) {
1089     // OSR methods point to the Method*, but the Method* does not
1090     // point back!
1091     if (_method->code() == this) {
1092       _method->clear_code(); // Break a cycle
1093     }
1094     _method = NULL;            // Clear the method of this dead nmethod
1095   }
1096 
1097   // Make the class unloaded - i.e., change state and notify sweeper
1098   assert(SafepointSynchronize::is_at_safepoint() || Thread::current()->is_ConcurrentGC_thread(),
1099          "must be at safepoint");
1100 
1101   // Unregister must be done before the state change
1102   Universe::heap()->unregister_nmethod(this);
1103 
1104   // Log the unloading.
1105   log_state_change();
1106 
1107 #if INCLUDE_JVMCI
1108   // The method can only be unloaded after the pointer to the installed code
1109   // Java wrapper is no longer alive. Here we need to clear out this weak
1110   // reference to the dead object.
1111   maybe_invalidate_installed_code();
1112 #endif
1113 
1114   // The Method* is gone at this point
1115   assert(_method == NULL, "Tautology");
1116 
1117   set_osr_link(NULL);
1118   NMethodSweeper::report_state_change(this);
1119 
1120   // The release is only needed for compile-time ordering, as accesses
1121   // into the nmethod after the store are not safe due to the sweeper
1122   // being allowed to free it when the store is observed, during
1123   // concurrent nmethod unloading. Therefore, there is no need for
1124   // acquire on the loader side.
1125   OrderAccess::release_store(&_state, (signed char)unloaded);
1126 }
1127 
1128 void nmethod::invalidate_osr_method() {
1129   assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod");
1130   // Remove from list of active nmethods
1131   if (method() != NULL) {
1132     method()->method_holder()->remove_osr_nmethod(this);
1133   }
1134 }
1135 
1136 void nmethod::log_state_change() const {
1137   if (LogCompilation) {
1138     if (xtty != NULL) {
1139       ttyLocker ttyl;  // keep the following output all in one block
1140       if (_state == unloaded) {
1141         xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'",
1142                          os::current_thread_id());
1143       } else {
1144         xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s",
1145                          os::current_thread_id(),
1146                          (_state == zombie ? " zombie='1'" : ""));
1147       }
1148       log_identity(xtty);
1149       xtty->stamp();
1150       xtty->end_elem();
1151     }
1152   }
1153 
1154   const char *state_msg = _state == zombie ? "made zombie" : "made not entrant";
1155   CompileTask::print_ul(this, state_msg);
1156   if (PrintCompilation && _state != unloaded) {
1157     print_on(tty, state_msg);
1158   }
1159 }
1160 
1161 /**
1162  * Common functionality for both make_not_entrant and make_zombie
1163  */
1164 bool nmethod::make_not_entrant_or_zombie(int state) {
1165   assert(state == zombie || state == not_entrant, "must be zombie or not_entrant");
1166   assert(!is_zombie(), "should not already be a zombie");
1167 
1168   if (_state == state) {
1169     // Avoid taking the lock if already in required state.
1170     // This is safe from races because the state is an end-state,
1171     // which the nmethod cannot back out of once entered.
1172     // No need for fencing either.
1173     return false;
1174   }
1175 
1176   // Make sure neither the nmethod nor the method is flushed in case of a safepoint in code below.
1177   nmethodLocker nml(this);
1178   methodHandle the_method(method());
1179   // This can be called while the system is already at a safepoint which is ok
1180   NoSafepointVerifier nsv(true, !SafepointSynchronize::is_at_safepoint());
1181 
1182   // during patching, depending on the nmethod state we must notify the GC that
1183   // code has been unloaded, unregistering it. We cannot do this right while
1184   // holding the Patching_lock because we need to use the CodeCache_lock. This
1185   // would be prone to deadlocks.
1186   // This flag is used to remember whether we need to later lock and unregister.
1187   bool nmethod_needs_unregister = false;
1188 
1189   {
1190     // invalidate osr nmethod before acquiring the patching lock since
1191     // they both acquire leaf locks and we don't want a deadlock.
1192     // This logic is equivalent to the logic below for patching the
1193     // verified entry point of regular methods. We check that the
1194     // nmethod is in use to ensure that it is invalidated only once.
1195     if (is_osr_method() && is_in_use()) {
1196       // this effectively makes the osr nmethod not entrant
1197       invalidate_osr_method();
1198     }
1199 
1200     // Enter critical section.  Does not block for safepoint.
1201     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
1202 
1203     if (_state == state) {
1204       // another thread already performed this transition so nothing
1205       // to do, but return false to indicate this.
1206       return false;
1207     }
1208 
1209     // The caller can be calling the method statically or through an inline
1210     // cache call.
1211     if (!is_osr_method() && !is_not_entrant()) {
1212       NativeJump::patch_verified_entry(entry_point(), verified_entry_point(),
1213                   SharedRuntime::get_handle_wrong_method_stub());
1214     }
1215 
1216     if (is_in_use() && update_recompile_counts()) {
1217       // It's a true state change, so mark the method as decompiled.
1218       // Do it only for transition from alive.
1219       inc_decompile_count();
1220     }
1221 
1222     // If the state is becoming a zombie, signal to unregister the nmethod with
1223     // the heap.
1224     // This nmethod may have already been unloaded during a full GC.
1225     if ((state == zombie) && !is_unloaded()) {
1226       nmethod_needs_unregister = true;
1227     }
1228 
1229     // Must happen before state change. Otherwise we have a race condition in
1230     // nmethod::can_not_entrant_be_converted(). I.e., a method can immediately
1231     // transition its state from 'not_entrant' to 'zombie' without having to wait
1232     // for stack scanning.
1233     if (state == not_entrant) {
1234       mark_as_seen_on_stack();
1235       OrderAccess::storestore(); // _stack_traversal_mark and _state
1236     }
1237 
1238     // Change state
1239     _state = state;
1240 
1241     // Log the transition once
1242     log_state_change();
1243 
1244     // Invalidate while holding the patching lock
1245     JVMCI_ONLY(maybe_invalidate_installed_code());
1246 
1247     // Remove nmethod from method.
1248     // We need to check if both the _code and _from_compiled_code_entry_point
1249     // refer to this nmethod because there is a race in setting these two fields
1250     // in Method* as seen in bugid 4947125.
1251     // If the vep() points to the zombie nmethod, the memory for the nmethod
1252     // could be flushed and the compiler and vtable stubs could still call
1253     // through it.
1254     if (method() != NULL && (method()->code() == this ||
1255                              method()->from_compiled_entry() == verified_entry_point())) {
1256       HandleMark hm;
1257       method()->clear_code(false /* already owns Patching_lock */);
1258     }
1259   } // leave critical region under Patching_lock
1260 
1261 #ifdef ASSERT
1262   if (is_osr_method() && method() != NULL) {
1263     // Make sure osr nmethod is invalidated, i.e. not on the list
1264     bool found = method()->method_holder()->remove_osr_nmethod(this);
1265     assert(!found, "osr nmethod should have been invalidated");
1266   }
1267 #endif
1268 
1269   // When the nmethod becomes zombie it is no longer alive so the
1270   // dependencies must be flushed.  nmethods in the not_entrant
1271   // state will be flushed later when the transition to zombie
1272   // happens or they get unloaded.
1273   if (state == zombie) {
1274     {
1275       // Flushing dependencies must be done before any possible
1276       // safepoint can sneak in, otherwise the oops used by the
1277       // dependency logic could have become stale.
1278       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1279       if (nmethod_needs_unregister) {
1280         Universe::heap()->unregister_nmethod(this);
1281       }
1282       flush_dependencies(/*delete_immediately*/true);
1283     }
1284 
1285     // Clear ICStubs to prevent back patching stubs of zombie or flushed
1286     // nmethods during the next safepoint (see ICStub::finalize).
1287     {
1288       CompiledICLocker ml(this);
1289       clear_ic_stubs();
1290     }
1291 
1292     // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload
1293     // event and it hasn't already been reported for this nmethod then
1294     // report it now. The event may have been reported earlier if the GC
1295     // marked it for unloading). JvmtiDeferredEventQueue support means
1296     // we no longer go to a safepoint here.
1297     post_compiled_method_unload();
1298 
1299 #ifdef ASSERT
1300     // It's no longer safe to access the oops section since zombie
1301     // nmethods aren't scanned for GC.
1302     _oops_are_stale = true;
1303 #endif
1304      // the Method may be reclaimed by class unloading now that the
1305      // nmethod is in zombie state
1306     set_method(NULL);
1307   } else {
1308     assert(state == not_entrant, "other cases may need to be handled differently");
1309   }
1310 
1311   if (TraceCreateZombies) {
1312     ResourceMark m;
1313     tty->print_cr("nmethod <" INTPTR_FORMAT "> %s code made %s", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string
1314   }
1315 
1316   NMethodSweeper::report_state_change(this);
1317   return true;
1318 }
1319 
1320 void nmethod::flush() {
1321   // Note that there are no valid oops in the nmethod anymore.
1322   assert(!is_osr_method() || is_unloaded() || is_zombie(),
1323          "osr nmethod must be unloaded or zombie before flushing");
1324   assert(is_zombie() || is_osr_method(), "must be a zombie method");
1325   assert (!is_locked_by_vm(), "locked methods shouldn't be flushed");
1326   assert_locked_or_safepoint(CodeCache_lock);
1327 
1328   // completely deallocate this method
1329   Events::log(JavaThread::current(), "flushing nmethod " INTPTR_FORMAT, p2i(this));
1330   if (PrintMethodFlushing) {
1331     tty->print_cr("*flushing %s nmethod %3d/" INTPTR_FORMAT ". Live blobs:" UINT32_FORMAT
1332                   "/Free CodeCache:" SIZE_FORMAT "Kb",
1333                   is_osr_method() ? "osr" : "",_compile_id, p2i(this), CodeCache::blob_count(),
1334                   CodeCache::unallocated_capacity(CodeCache::get_code_blob_type(this))/1024);
1335   }
1336 
1337   // We need to deallocate any ExceptionCache data.
1338   // Note that we do not need to grab the nmethod lock for this, it
1339   // better be thread safe if we're disposing of it!
1340   ExceptionCache* ec = exception_cache();
1341   set_exception_cache(NULL);
1342   while(ec != NULL) {
1343     ExceptionCache* next = ec->next();
1344     delete ec;
1345     ec = next;
1346   }
1347 
1348   if (on_scavenge_root_list()) {
1349     CodeCache::drop_scavenge_root_nmethod(this);
1350   }
1351 
1352 #if INCLUDE_JVMCI
1353   assert(_jvmci_installed_code == NULL, "should have been nulled out when transitioned to zombie");
1354   assert(_speculation_log == NULL, "should have been nulled out when transitioned to zombie");
1355 #endif
1356 
1357   CodeBlob::flush();
1358   CodeCache::free(this);
1359 }
1360 
1361 oop nmethod::oop_at(int index) const {
1362   if (index == 0) {
1363     return NULL;
1364   }
1365   return NativeAccess<AS_NO_KEEPALIVE>::oop_load(oop_addr_at(index));
1366 }
1367 
1368 //
1369 // Notify all classes this nmethod is dependent on that it is no
1370 // longer dependent. This should only be called in two situations.
1371 // First, when a nmethod transitions to a zombie all dependents need
1372 // to be clear.  Since zombification happens at a safepoint there's no
1373 // synchronization issues.  The second place is a little more tricky.
1374 // During phase 1 of mark sweep class unloading may happen and as a
1375 // result some nmethods may get unloaded.  In this case the flushing
1376 // of dependencies must happen during phase 1 since after GC any
1377 // dependencies in the unloaded nmethod won't be updated, so
1378 // traversing the dependency information in unsafe.  In that case this
1379 // function is called with a boolean argument and this function only
1380 // notifies instanceKlasses that are reachable
1381 
1382 void nmethod::flush_dependencies(bool delete_immediately) {
1383   DEBUG_ONLY(bool called_by_gc = Universe::heap()->is_gc_active() || Thread::current()->is_ConcurrentGC_thread();)
1384   assert(called_by_gc != delete_immediately,
1385   "delete_immediately is false if and only if we are called during GC");
1386   if (!has_flushed_dependencies()) {
1387     set_has_flushed_dependencies();
1388     for (Dependencies::DepStream deps(this); deps.next(); ) {
1389       if (deps.type() == Dependencies::call_site_target_value) {
1390         // CallSite dependencies are managed on per-CallSite instance basis.
1391         oop call_site = deps.argument_oop(0);
1392         if (delete_immediately) {
1393           assert_locked_or_safepoint(CodeCache_lock);
1394           MethodHandles::remove_dependent_nmethod(call_site, this);
1395         } else {
1396           MethodHandles::clean_dependency_context(call_site);
1397         }
1398       } else {
1399         Klass* klass = deps.context_type();
1400         if (klass == NULL) {
1401           continue;  // ignore things like evol_method
1402         }
1403         // During GC delete_immediately is false, and liveness
1404         // of dependee determines class that needs to be updated.
1405         if (delete_immediately) {
1406           assert_locked_or_safepoint(CodeCache_lock);
1407           InstanceKlass::cast(klass)->remove_dependent_nmethod(this);
1408         } else if (klass->is_loader_alive()) {
1409           // The GC may clean dependency contexts concurrently and in parallel.
1410           InstanceKlass::cast(klass)->clean_dependency_context();
1411         }
1412       }
1413     }
1414   }
1415 }
1416 
1417 // ------------------------------------------------------------------
1418 // post_compiled_method_load_event
1419 // new method for install_code() path
1420 // Transfer information from compilation to jvmti
1421 void nmethod::post_compiled_method_load_event() {
1422 
1423   Method* moop = method();
1424   HOTSPOT_COMPILED_METHOD_LOAD(
1425       (char *) moop->klass_name()->bytes(),
1426       moop->klass_name()->utf8_length(),
1427       (char *) moop->name()->bytes(),
1428       moop->name()->utf8_length(),
1429       (char *) moop->signature()->bytes(),
1430       moop->signature()->utf8_length(),
1431       insts_begin(), insts_size());
1432 
1433   if (JvmtiExport::should_post_compiled_method_load() ||
1434       JvmtiExport::should_post_compiled_method_unload()) {
1435     get_and_cache_jmethod_id();
1436   }
1437 
1438   if (JvmtiExport::should_post_compiled_method_load()) {
1439     // Let the Service thread (which is a real Java thread) post the event
1440     MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
1441     JvmtiDeferredEventQueue::enqueue(
1442       JvmtiDeferredEvent::compiled_method_load_event(this));
1443   }
1444 }
1445 
1446 jmethodID nmethod::get_and_cache_jmethod_id() {
1447   if (_jmethod_id == NULL) {
1448     // Cache the jmethod_id since it can no longer be looked up once the
1449     // method itself has been marked for unloading.
1450     _jmethod_id = method()->jmethod_id();
1451   }
1452   return _jmethod_id;
1453 }
1454 
1455 void nmethod::post_compiled_method_unload() {
1456   if (unload_reported()) {
1457     // During unloading we transition to unloaded and then to zombie
1458     // and the unloading is reported during the first transition.
1459     return;
1460   }
1461 
1462   assert(_method != NULL && !is_unloaded(), "just checking");
1463   DTRACE_METHOD_UNLOAD_PROBE(method());
1464 
1465   // If a JVMTI agent has enabled the CompiledMethodUnload event then
1466   // post the event. Sometime later this nmethod will be made a zombie
1467   // by the sweeper but the Method* will not be valid at that point.
1468   // If the _jmethod_id is null then no load event was ever requested
1469   // so don't bother posting the unload.  The main reason for this is
1470   // that the jmethodID is a weak reference to the Method* so if
1471   // it's being unloaded there's no way to look it up since the weak
1472   // ref will have been cleared.
1473   if (_jmethod_id != NULL && JvmtiExport::should_post_compiled_method_unload()) {
1474     assert(!unload_reported(), "already unloaded");
1475     JvmtiDeferredEvent event =
1476       JvmtiDeferredEvent::compiled_method_unload_event(this,
1477           _jmethod_id, insts_begin());
1478     MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
1479     JvmtiDeferredEventQueue::enqueue(event);
1480   }
1481 
1482   // The JVMTI CompiledMethodUnload event can be enabled or disabled at
1483   // any time. As the nmethod is being unloaded now we mark it has
1484   // having the unload event reported - this will ensure that we don't
1485   // attempt to report the event in the unlikely scenario where the
1486   // event is enabled at the time the nmethod is made a zombie.
1487   set_unload_reported();
1488 }
1489 
1490 // Iterate over metadata calling this function.   Used by RedefineClasses
1491 void nmethod::metadata_do(void f(Metadata*)) {
1492   {
1493     // Visit all immediate references that are embedded in the instruction stream.
1494     RelocIterator iter(this, oops_reloc_begin());
1495     while (iter.next()) {
1496       if (iter.type() == relocInfo::metadata_type ) {
1497         metadata_Relocation* r = iter.metadata_reloc();
1498         // In this metadata, we must only follow those metadatas directly embedded in
1499         // the code.  Other metadatas (oop_index>0) are seen as part of
1500         // the metadata section below.
1501         assert(1 == (r->metadata_is_immediate()) +
1502                (r->metadata_addr() >= metadata_begin() && r->metadata_addr() < metadata_end()),
1503                "metadata must be found in exactly one place");
1504         if (r->metadata_is_immediate() && r->metadata_value() != NULL) {
1505           Metadata* md = r->metadata_value();
1506           if (md != _method) f(md);
1507         }
1508       } else if (iter.type() == relocInfo::virtual_call_type) {
1509         // Check compiledIC holders associated with this nmethod
1510         ResourceMark rm;
1511         CompiledIC *ic = CompiledIC_at(&iter);
1512         if (ic->is_icholder_call()) {
1513           CompiledICHolder* cichk = ic->cached_icholder();
1514           f(cichk->holder_metadata());
1515           f(cichk->holder_klass());
1516         } else {
1517           Metadata* ic_oop = ic->cached_metadata();
1518           if (ic_oop != NULL) {
1519             f(ic_oop);
1520           }
1521         }
1522       }
1523     }
1524   }
1525 
1526   // Visit the metadata section
1527   for (Metadata** p = metadata_begin(); p < metadata_end(); p++) {
1528     if (*p == Universe::non_oop_word() || *p == NULL)  continue;  // skip non-oops
1529     Metadata* md = *p;
1530     f(md);
1531   }
1532 
1533   // Visit metadata not embedded in the other places.
1534   if (_method != NULL) f(_method);
1535 }
1536 
1537 // The _is_unloading_state encodes a tuple comprising the unloading cycle
1538 // and the result of IsUnloadingBehaviour::is_unloading() fpr that cycle.
1539 // This is the bit layout of the _is_unloading_state byte: 00000CCU
1540 // CC refers to the cycle, which has 2 bits, and U refers to the result of
1541 // IsUnloadingBehaviour::is_unloading() for that unloading cycle.
1542 
1543 class IsUnloadingState: public AllStatic {
1544   static const uint8_t _is_unloading_mask = 1;
1545   static const uint8_t _is_unloading_shift = 0;
1546   static const uint8_t _unloading_cycle_mask = 6;
1547   static const uint8_t _unloading_cycle_shift = 1;
1548 
1549   static uint8_t set_is_unloading(uint8_t state, bool value) {
1550     state &= ~_is_unloading_mask;
1551     if (value) {
1552       state |= 1 << _is_unloading_shift;
1553     }
1554     assert(is_unloading(state) == value, "unexpected unloading cycle overflow");
1555     return state;
1556   }
1557 
1558   static uint8_t set_unloading_cycle(uint8_t state, uint8_t value) {
1559     state &= ~_unloading_cycle_mask;
1560     state |= value << _unloading_cycle_shift;
1561     assert(unloading_cycle(state) == value, "unexpected unloading cycle overflow");
1562     return state;
1563   }
1564 
1565 public:
1566   static bool is_unloading(uint8_t state) { return (state & _is_unloading_mask) >> _is_unloading_shift == 1; }
1567   static uint8_t unloading_cycle(uint8_t state) { return (state & _unloading_cycle_mask) >> _unloading_cycle_shift; }
1568 
1569   static uint8_t create(bool is_unloading, uint8_t unloading_cycle) {
1570     uint8_t state = 0;
1571     state = set_is_unloading(state, is_unloading);
1572     state = set_unloading_cycle(state, unloading_cycle);
1573     return state;
1574   }
1575 };
1576 
1577 bool nmethod::is_unloading() {
1578   uint8_t state = RawAccess<MO_RELAXED>::load(&_is_unloading_state);
1579   bool state_is_unloading = IsUnloadingState::is_unloading(state);
1580   uint8_t state_unloading_cycle = IsUnloadingState::unloading_cycle(state);
1581   if (state_is_unloading) {
1582     return true;
1583   }
1584   uint8_t current_cycle = CodeCache::unloading_cycle();
1585   if (state_unloading_cycle == current_cycle) {
1586     return false;
1587   }
1588 
1589   // The IsUnloadingBehaviour is responsible for checking if there are any dead
1590   // oops in the CompiledMethod, by calling oops_do on it.
1591   state_unloading_cycle = current_cycle;
1592 
1593   if (is_zombie()) {
1594     // Zombies without calculated unloading epoch are never unloading due to GC.
1595 
1596     // There are no races where a previously observed is_unloading() nmethod
1597     // suddenly becomes not is_unloading() due to here being observed as zombie.
1598 
1599     // With STW unloading, all is_alive() && is_unloading() nmethods are unlinked
1600     // and unloaded in the safepoint. That makes races where an nmethod is first
1601     // observed as is_alive() && is_unloading() and subsequently observed as
1602     // is_zombie() impossible.
1603 
1604     // With concurrent unloading, all references to is_unloading() nmethods are
1605     // first unlinked (e.g. IC caches and dependency contexts). Then a global
1606     // handshake operation is performed with all JavaThreads before finally
1607     // unloading the nmethods. The sweeper never converts is_alive() && is_unloading()
1608     // nmethods to zombies; it waits for them to become is_unloaded(). So before
1609     // the global handshake, it is impossible for is_unloading() nmethods to
1610     // racingly become is_zombie(). And is_unloading() is calculated for all is_alive()
1611     // nmethods before taking that global handshake, meaning that it will never
1612     // be recalculated after the handshake.
1613 
1614     // After that global handshake, is_unloading() nmethods are only observable
1615     // to the iterators, and they will never trigger recomputation of the cached
1616     // is_unloading_state, and hence may not suffer from such races.
1617 
1618     state_is_unloading = false;
1619   } else {
1620     state_is_unloading = IsUnloadingBehaviour::current()->is_unloading(this);
1621   }
1622 
1623   state = IsUnloadingState::create(state_is_unloading, state_unloading_cycle);
1624 
1625   RawAccess<MO_RELAXED>::store(&_is_unloading_state, state);
1626 
1627   return state_is_unloading;
1628 }
1629 
1630 void nmethod::clear_unloading_state() {
1631   uint8_t state = IsUnloadingState::create(false, CodeCache::unloading_cycle());
1632   RawAccess<MO_RELAXED>::store(&_is_unloading_state, state);
1633 }
1634 
1635 
1636 // This is called at the end of the strong tracing/marking phase of a
1637 // GC to unload an nmethod if it contains otherwise unreachable
1638 // oops.
1639 
1640 void nmethod::do_unloading(bool unloading_occurred) {
1641   // Make sure the oop's ready to receive visitors
1642   assert(!is_zombie() && !is_unloaded(),
1643          "should not call follow on zombie or unloaded nmethod");
1644 
1645   if (is_unloading()) {
1646     make_unloaded();
1647   } else {
1648 #if INCLUDE_JVMCI
1649     if (_jvmci_installed_code != NULL) {
1650       if (JNIHandles::is_global_weak_cleared(_jvmci_installed_code)) {
1651         if (_jvmci_installed_code_triggers_invalidation) {
1652           make_not_entrant();
1653         }
1654         clear_jvmci_installed_code();
1655       }
1656     }
1657 #endif
1658 
1659     guarantee(unload_nmethod_caches(unloading_occurred),
1660               "Should not need transition stubs");
1661   }
1662 }
1663 
1664 void nmethod::oops_do(OopClosure* f, bool allow_zombie) {
1665   // make sure the oops ready to receive visitors
1666   assert(allow_zombie || !is_zombie(), "should not call follow on zombie nmethod");
1667   assert(!is_unloaded(), "should not call follow on unloaded nmethod");
1668 
1669   // Prevent extra code cache walk for platforms that don't have immediate oops.
1670   if (relocInfo::mustIterateImmediateOopsInCode()) {
1671     RelocIterator iter(this, oops_reloc_begin());
1672 
1673     while (iter.next()) {
1674       if (iter.type() == relocInfo::oop_type ) {
1675         oop_Relocation* r = iter.oop_reloc();
1676         // In this loop, we must only follow those oops directly embedded in
1677         // the code.  Other oops (oop_index>0) are seen as part of scopes_oops.
1678         assert(1 == (r->oop_is_immediate()) +
1679                (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()),
1680                "oop must be found in exactly one place");
1681         if (r->oop_is_immediate() && r->oop_value() != NULL) {
1682           f->do_oop(r->oop_addr());
1683         }
1684       }
1685     }
1686   }
1687 
1688   // Scopes
1689   // This includes oop constants not inlined in the code stream.
1690   for (oop* p = oops_begin(); p < oops_end(); p++) {
1691     if (*p == Universe::non_oop_word())  continue;  // skip non-oops
1692     f->do_oop(p);
1693   }
1694 }
1695 
1696 #define NMETHOD_SENTINEL ((nmethod*)badAddress)
1697 
1698 nmethod* volatile nmethod::_oops_do_mark_nmethods;
1699 
1700 // An nmethod is "marked" if its _mark_link is set non-null.
1701 // Even if it is the end of the linked list, it will have a non-null link value,
1702 // as long as it is on the list.
1703 // This code must be MP safe, because it is used from parallel GC passes.
1704 bool nmethod::test_set_oops_do_mark() {
1705   assert(nmethod::oops_do_marking_is_active(), "oops_do_marking_prologue must be called");
1706   if (_oops_do_mark_link == NULL) {
1707     // Claim this nmethod for this thread to mark.
1708     if (Atomic::replace_if_null(NMETHOD_SENTINEL, &_oops_do_mark_link)) {
1709       // Atomically append this nmethod (now claimed) to the head of the list:
1710       nmethod* observed_mark_nmethods = _oops_do_mark_nmethods;
1711       for (;;) {
1712         nmethod* required_mark_nmethods = observed_mark_nmethods;
1713         _oops_do_mark_link = required_mark_nmethods;
1714         observed_mark_nmethods =
1715           Atomic::cmpxchg(this, &_oops_do_mark_nmethods, required_mark_nmethods);
1716         if (observed_mark_nmethods == required_mark_nmethods)
1717           break;
1718       }
1719       // Mark was clear when we first saw this guy.
1720       LogTarget(Trace, gc, nmethod) lt;
1721       if (lt.is_enabled()) {
1722         LogStream ls(lt);
1723         CompileTask::print(&ls, this, "oops_do, mark", /*short_form:*/ true);
1724       }
1725       return false;
1726     }
1727   }
1728   // On fall through, another racing thread marked this nmethod before we did.
1729   return true;
1730 }
1731 
1732 void nmethod::oops_do_marking_prologue() {
1733   log_trace(gc, nmethod)("oops_do_marking_prologue");
1734   assert(_oops_do_mark_nmethods == NULL, "must not call oops_do_marking_prologue twice in a row");
1735   // We use cmpxchg instead of regular assignment here because the user
1736   // may fork a bunch of threads, and we need them all to see the same state.
1737   nmethod* observed = Atomic::cmpxchg(NMETHOD_SENTINEL, &_oops_do_mark_nmethods, (nmethod*)NULL);
1738   guarantee(observed == NULL, "no races in this sequential code");
1739 }
1740 
1741 void nmethod::oops_do_marking_epilogue() {
1742   assert(_oops_do_mark_nmethods != NULL, "must not call oops_do_marking_epilogue twice in a row");
1743   nmethod* cur = _oops_do_mark_nmethods;
1744   while (cur != NMETHOD_SENTINEL) {
1745     assert(cur != NULL, "not NULL-terminated");
1746     nmethod* next = cur->_oops_do_mark_link;
1747     cur->_oops_do_mark_link = NULL;
1748     DEBUG_ONLY(cur->verify_oop_relocations());
1749 
1750     LogTarget(Trace, gc, nmethod) lt;
1751     if (lt.is_enabled()) {
1752       LogStream ls(lt);
1753       CompileTask::print(&ls, cur, "oops_do, unmark", /*short_form:*/ true);
1754     }
1755     cur = next;
1756   }
1757   nmethod* required = _oops_do_mark_nmethods;
1758   nmethod* observed = Atomic::cmpxchg((nmethod*)NULL, &_oops_do_mark_nmethods, required);
1759   guarantee(observed == required, "no races in this sequential code");
1760   log_trace(gc, nmethod)("oops_do_marking_epilogue");
1761 }
1762 
1763 class DetectScavengeRoot: public OopClosure {
1764   bool     _detected_scavenge_root;
1765   nmethod* _print_nm;
1766 public:
1767   DetectScavengeRoot(nmethod* nm) : _detected_scavenge_root(false), _print_nm(nm) {}
1768 
1769   bool detected_scavenge_root() { return _detected_scavenge_root; }
1770   virtual void do_oop(oop* p) {
1771     if ((*p) != NULL && Universe::heap()->is_scavengable(*p)) {
1772       NOT_PRODUCT(maybe_print(p));
1773       _detected_scavenge_root = true;
1774     }
1775   }
1776   virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
1777 
1778 #ifndef PRODUCT
1779   void maybe_print(oop* p) {
1780     LogTarget(Trace, gc, nmethod) lt;
1781     if (lt.is_enabled()) {
1782       LogStream ls(lt);
1783       if (!_detected_scavenge_root) {
1784         CompileTask::print(&ls, _print_nm, "new scavenge root", /*short_form:*/ true);
1785       }
1786       ls.print("" PTR_FORMAT "[offset=%d] detected scavengable oop " PTR_FORMAT " (found at " PTR_FORMAT ") ",
1787                p2i(_print_nm), (int)((intptr_t)p - (intptr_t)_print_nm),
1788                p2i(*p), p2i(p));
1789       ls.cr();
1790     }
1791   }
1792 #endif //PRODUCT
1793 };
1794 
1795 bool nmethod::detect_scavenge_root_oops() {
1796   DetectScavengeRoot detect_scavenge_root(this);
1797   oops_do(&detect_scavenge_root);
1798   return detect_scavenge_root.detected_scavenge_root();
1799 }
1800 
1801 inline bool includes(void* p, void* from, void* to) {
1802   return from <= p && p < to;
1803 }
1804 
1805 
1806 void nmethod::copy_scopes_pcs(PcDesc* pcs, int count) {
1807   assert(count >= 2, "must be sentinel values, at least");
1808 
1809 #ifdef ASSERT
1810   // must be sorted and unique; we do a binary search in find_pc_desc()
1811   int prev_offset = pcs[0].pc_offset();
1812   assert(prev_offset == PcDesc::lower_offset_limit,
1813          "must start with a sentinel");
1814   for (int i = 1; i < count; i++) {
1815     int this_offset = pcs[i].pc_offset();
1816     assert(this_offset > prev_offset, "offsets must be sorted");
1817     prev_offset = this_offset;
1818   }
1819   assert(prev_offset == PcDesc::upper_offset_limit,
1820          "must end with a sentinel");
1821 #endif //ASSERT
1822 
1823   // Search for MethodHandle invokes and tag the nmethod.
1824   for (int i = 0; i < count; i++) {
1825     if (pcs[i].is_method_handle_invoke()) {
1826       set_has_method_handle_invokes(true);
1827       break;
1828     }
1829   }
1830   assert(has_method_handle_invokes() == (_deopt_mh_handler_begin != NULL), "must have deopt mh handler");
1831 
1832   int size = count * sizeof(PcDesc);
1833   assert(scopes_pcs_size() >= size, "oob");
1834   memcpy(scopes_pcs_begin(), pcs, size);
1835 
1836   // Adjust the final sentinel downward.
1837   PcDesc* last_pc = &scopes_pcs_begin()[count-1];
1838   assert(last_pc->pc_offset() == PcDesc::upper_offset_limit, "sanity");
1839   last_pc->set_pc_offset(content_size() + 1);
1840   for (; last_pc + 1 < scopes_pcs_end(); last_pc += 1) {
1841     // Fill any rounding gaps with copies of the last record.
1842     last_pc[1] = last_pc[0];
1843   }
1844   // The following assert could fail if sizeof(PcDesc) is not
1845   // an integral multiple of oopSize (the rounding term).
1846   // If it fails, change the logic to always allocate a multiple
1847   // of sizeof(PcDesc), and fill unused words with copies of *last_pc.
1848   assert(last_pc + 1 == scopes_pcs_end(), "must match exactly");
1849 }
1850 
1851 void nmethod::copy_scopes_data(u_char* buffer, int size) {
1852   assert(scopes_data_size() >= size, "oob");
1853   memcpy(scopes_data_begin(), buffer, size);
1854 }
1855 
1856 #ifdef ASSERT
1857 static PcDesc* linear_search(const PcDescSearch& search, int pc_offset, bool approximate) {
1858   PcDesc* lower = search.scopes_pcs_begin();
1859   PcDesc* upper = search.scopes_pcs_end();
1860   lower += 1; // exclude initial sentinel
1861   PcDesc* res = NULL;
1862   for (PcDesc* p = lower; p < upper; p++) {
1863     NOT_PRODUCT(--pc_nmethod_stats.pc_desc_tests);  // don't count this call to match_desc
1864     if (match_desc(p, pc_offset, approximate)) {
1865       if (res == NULL)
1866         res = p;
1867       else
1868         res = (PcDesc*) badAddress;
1869     }
1870   }
1871   return res;
1872 }
1873 #endif
1874 
1875 
1876 // Finds a PcDesc with real-pc equal to "pc"
1877 PcDesc* PcDescContainer::find_pc_desc_internal(address pc, bool approximate, const PcDescSearch& search) {
1878   address base_address = search.code_begin();
1879   if ((pc < base_address) ||
1880       (pc - base_address) >= (ptrdiff_t) PcDesc::upper_offset_limit) {
1881     return NULL;  // PC is wildly out of range
1882   }
1883   int pc_offset = (int) (pc - base_address);
1884 
1885   // Check the PcDesc cache if it contains the desired PcDesc
1886   // (This as an almost 100% hit rate.)
1887   PcDesc* res = _pc_desc_cache.find_pc_desc(pc_offset, approximate);
1888   if (res != NULL) {
1889     assert(res == linear_search(search, pc_offset, approximate), "cache ok");
1890     return res;
1891   }
1892 
1893   // Fallback algorithm: quasi-linear search for the PcDesc
1894   // Find the last pc_offset less than the given offset.
1895   // The successor must be the required match, if there is a match at all.
1896   // (Use a fixed radix to avoid expensive affine pointer arithmetic.)
1897   PcDesc* lower = search.scopes_pcs_begin();
1898   PcDesc* upper = search.scopes_pcs_end();
1899   upper -= 1; // exclude final sentinel
1900   if (lower >= upper)  return NULL;  // native method; no PcDescs at all
1901 
1902 #define assert_LU_OK \
1903   /* invariant on lower..upper during the following search: */ \
1904   assert(lower->pc_offset() <  pc_offset, "sanity"); \
1905   assert(upper->pc_offset() >= pc_offset, "sanity")
1906   assert_LU_OK;
1907 
1908   // Use the last successful return as a split point.
1909   PcDesc* mid = _pc_desc_cache.last_pc_desc();
1910   NOT_PRODUCT(++pc_nmethod_stats.pc_desc_searches);
1911   if (mid->pc_offset() < pc_offset) {
1912     lower = mid;
1913   } else {
1914     upper = mid;
1915   }
1916 
1917   // Take giant steps at first (4096, then 256, then 16, then 1)
1918   const int LOG2_RADIX = 4 /*smaller steps in debug mode:*/ debug_only(-1);
1919   const int RADIX = (1 << LOG2_RADIX);
1920   for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) {
1921     while ((mid = lower + step) < upper) {
1922       assert_LU_OK;
1923       NOT_PRODUCT(++pc_nmethod_stats.pc_desc_searches);
1924       if (mid->pc_offset() < pc_offset) {
1925         lower = mid;
1926       } else {
1927         upper = mid;
1928         break;
1929       }
1930     }
1931     assert_LU_OK;
1932   }
1933 
1934   // Sneak up on the value with a linear search of length ~16.
1935   while (true) {
1936     assert_LU_OK;
1937     mid = lower + 1;
1938     NOT_PRODUCT(++pc_nmethod_stats.pc_desc_searches);
1939     if (mid->pc_offset() < pc_offset) {
1940       lower = mid;
1941     } else {
1942       upper = mid;
1943       break;
1944     }
1945   }
1946 #undef assert_LU_OK
1947 
1948   if (match_desc(upper, pc_offset, approximate)) {
1949     assert(upper == linear_search(search, pc_offset, approximate), "search ok");
1950     _pc_desc_cache.add_pc_desc(upper);
1951     return upper;
1952   } else {
1953     assert(NULL == linear_search(search, pc_offset, approximate), "search ok");
1954     return NULL;
1955   }
1956 }
1957 
1958 
1959 void nmethod::check_all_dependencies(DepChange& changes) {
1960   // Checked dependencies are allocated into this ResourceMark
1961   ResourceMark rm;
1962 
1963   // Turn off dependency tracing while actually testing dependencies.
1964   NOT_PRODUCT( FlagSetting fs(TraceDependencies, false) );
1965 
1966   typedef ResourceHashtable<DependencySignature, int, &DependencySignature::hash,
1967                             &DependencySignature::equals, 11027> DepTable;
1968 
1969   DepTable* table = new DepTable();
1970 
1971   // Iterate over live nmethods and check dependencies of all nmethods that are not
1972   // marked for deoptimization. A particular dependency is only checked once.
1973   NMethodIterator iter(NMethodIterator::only_alive_and_not_unloading);
1974   while(iter.next()) {
1975     nmethod* nm = iter.method();
1976     // Only notify for live nmethods
1977     if (!nm->is_marked_for_deoptimization()) {
1978       for (Dependencies::DepStream deps(nm); deps.next(); ) {
1979         // Construct abstraction of a dependency.
1980         DependencySignature* current_sig = new DependencySignature(deps);
1981 
1982         // Determine if dependency is already checked. table->put(...) returns
1983         // 'true' if the dependency is added (i.e., was not in the hashtable).
1984         if (table->put(*current_sig, 1)) {
1985           if (deps.check_dependency() != NULL) {
1986             // Dependency checking failed. Print out information about the failed
1987             // dependency and finally fail with an assert. We can fail here, since
1988             // dependency checking is never done in a product build.
1989             tty->print_cr("Failed dependency:");
1990             changes.print();
1991             nm->print();
1992             nm->print_dependencies();
1993             assert(false, "Should have been marked for deoptimization");
1994           }
1995         }
1996       }
1997     }
1998   }
1999 }
2000 
2001 bool nmethod::check_dependency_on(DepChange& changes) {
2002   // What has happened:
2003   // 1) a new class dependee has been added
2004   // 2) dependee and all its super classes have been marked
2005   bool found_check = false;  // set true if we are upset
2006   for (Dependencies::DepStream deps(this); deps.next(); ) {
2007     // Evaluate only relevant dependencies.
2008     if (deps.spot_check_dependency_at(changes) != NULL) {
2009       found_check = true;
2010       NOT_DEBUG(break);
2011     }
2012   }
2013   return found_check;
2014 }
2015 
2016 bool nmethod::is_evol_dependent_on(Klass* dependee) {
2017   InstanceKlass *dependee_ik = InstanceKlass::cast(dependee);
2018   Array<Method*>* dependee_methods = dependee_ik->methods();
2019   for (Dependencies::DepStream deps(this); deps.next(); ) {
2020     if (deps.type() == Dependencies::evol_method) {
2021       Method* method = deps.method_argument(0);
2022       for (int j = 0; j < dependee_methods->length(); j++) {
2023         if (dependee_methods->at(j) == method) {
2024           if (log_is_enabled(Debug, redefine, class, nmethod)) {
2025             ResourceMark rm;
2026             log_debug(redefine, class, nmethod)
2027               ("Found evol dependency of nmethod %s.%s(%s) compile_id=%d on method %s.%s(%s)",
2028                _method->method_holder()->external_name(),
2029                _method->name()->as_C_string(),
2030                _method->signature()->as_C_string(),
2031                compile_id(),
2032                method->method_holder()->external_name(),
2033                method->name()->as_C_string(),
2034                method->signature()->as_C_string());
2035           }
2036           if (TraceDependencies || LogCompilation)
2037             deps.log_dependency(dependee);
2038           return true;
2039         }
2040       }
2041     }
2042   }
2043   return false;
2044 }
2045 
2046 // Called from mark_for_deoptimization, when dependee is invalidated.
2047 bool nmethod::is_dependent_on_method(Method* dependee) {
2048   for (Dependencies::DepStream deps(this); deps.next(); ) {
2049     if (deps.type() != Dependencies::evol_method)
2050       continue;
2051     Method* method = deps.method_argument(0);
2052     if (method == dependee) return true;
2053   }
2054   return false;
2055 }
2056 
2057 
2058 bool nmethod::is_patchable_at(address instr_addr) {
2059   assert(insts_contains(instr_addr), "wrong nmethod used");
2060   if (is_zombie()) {
2061     // a zombie may never be patched
2062     return false;
2063   }
2064   return true;
2065 }
2066 
2067 
2068 address nmethod::continuation_for_implicit_exception(address pc) {
2069   // Exception happened outside inline-cache check code => we are inside
2070   // an active nmethod => use cpc to determine a return address
2071   int exception_offset = pc - code_begin();
2072   int cont_offset = ImplicitExceptionTable(this).at( exception_offset );
2073 #ifdef ASSERT
2074   if (cont_offset == 0) {
2075     Thread* thread = Thread::current();
2076     ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY
2077     HandleMark hm(thread);
2078     ResourceMark rm(thread);
2079     CodeBlob* cb = CodeCache::find_blob(pc);
2080     assert(cb != NULL && cb == this, "");
2081     ttyLocker ttyl;
2082     tty->print_cr("implicit exception happened at " INTPTR_FORMAT, p2i(pc));
2083     print();
2084     method()->print_codes();
2085     print_code();
2086     print_pcs();
2087   }
2088 #endif
2089   if (cont_offset == 0) {
2090     // Let the normal error handling report the exception
2091     return NULL;
2092   }
2093   return code_begin() + cont_offset;
2094 }
2095 
2096 
2097 
2098 void nmethod_init() {
2099   // make sure you didn't forget to adjust the filler fields
2100   assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
2101 }
2102 
2103 
2104 //-------------------------------------------------------------------------------------------
2105 
2106 
2107 // QQQ might we make this work from a frame??
2108 nmethodLocker::nmethodLocker(address pc) {
2109   CodeBlob* cb = CodeCache::find_blob(pc);
2110   guarantee(cb != NULL && cb->is_compiled(), "bad pc for a nmethod found");
2111   _nm = cb->as_compiled_method();
2112   lock_nmethod(_nm);
2113 }
2114 
2115 // Only JvmtiDeferredEvent::compiled_method_unload_event()
2116 // should pass zombie_ok == true.
2117 void nmethodLocker::lock_nmethod(CompiledMethod* cm, bool zombie_ok) {
2118   if (cm == NULL)  return;
2119   if (cm->is_aot()) return;  // FIXME: Revisit once _lock_count is added to aot_method
2120   nmethod* nm = cm->as_nmethod();
2121   Atomic::inc(&nm->_lock_count);
2122   assert(zombie_ok || !nm->is_zombie(), "cannot lock a zombie method");
2123 }
2124 
2125 void nmethodLocker::unlock_nmethod(CompiledMethod* cm) {
2126   if (cm == NULL)  return;
2127   if (cm->is_aot()) return;  // FIXME: Revisit once _lock_count is added to aot_method
2128   nmethod* nm = cm->as_nmethod();
2129   Atomic::dec(&nm->_lock_count);
2130   assert(nm->_lock_count >= 0, "unmatched nmethod lock/unlock");
2131 }
2132 
2133 
2134 // -----------------------------------------------------------------------------
2135 // Verification
2136 
2137 class VerifyOopsClosure: public OopClosure {
2138   nmethod* _nm;
2139   bool     _ok;
2140 public:
2141   VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
2142   bool ok() { return _ok; }
2143   virtual void do_oop(oop* p) {
2144     if (oopDesc::is_oop_or_null(*p)) return;
2145     if (_ok) {
2146       _nm->print_nmethod(true);
2147       _ok = false;
2148     }
2149     tty->print_cr("*** non-oop " PTR_FORMAT " found at " PTR_FORMAT " (offset %d)",
2150                   p2i(*p), p2i(p), (int)((intptr_t)p - (intptr_t)_nm));
2151   }
2152   virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
2153 };
2154 
2155 void nmethod::verify() {
2156 
2157   // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant
2158   // seems odd.
2159 
2160   if (is_zombie() || is_not_entrant() || is_unloaded())
2161     return;
2162 
2163   // Make sure all the entry points are correctly aligned for patching.
2164   NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point());
2165 
2166   // assert(oopDesc::is_oop(method()), "must be valid");
2167 
2168   ResourceMark rm;
2169 
2170   if (!CodeCache::contains(this)) {
2171     fatal("nmethod at " INTPTR_FORMAT " not in zone", p2i(this));
2172   }
2173 
2174   if(is_native_method() )
2175     return;
2176 
2177   nmethod* nm = CodeCache::find_nmethod(verified_entry_point());
2178   if (nm != this) {
2179     fatal("findNMethod did not find this nmethod (" INTPTR_FORMAT ")", p2i(this));
2180   }
2181 
2182   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2183     if (! p->verify(this)) {
2184       tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", p2i(this));
2185     }
2186   }
2187 
2188   VerifyOopsClosure voc(this);
2189   oops_do(&voc);
2190   assert(voc.ok(), "embedded oops must be OK");
2191   Universe::heap()->verify_nmethod(this);
2192 
2193   verify_scopes();
2194 }
2195 
2196 
2197 void nmethod::verify_interrupt_point(address call_site) {
2198   // Verify IC only when nmethod installation is finished.
2199   if (!is_not_installed()) {
2200     if (CompiledICLocker::is_safe(this)) {
2201       CompiledIC_at(this, call_site);
2202       CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
2203     } else {
2204       CompiledICLocker ml_verify(this);
2205       CompiledIC_at(this, call_site);
2206     }
2207   }
2208 
2209   PcDesc* pd = pc_desc_at(nativeCall_at(call_site)->return_address());
2210   assert(pd != NULL, "PcDesc must exist");
2211   for (ScopeDesc* sd = new ScopeDesc(this, pd->scope_decode_offset(),
2212                                      pd->obj_decode_offset(), pd->should_reexecute(), pd->rethrow_exception(),
2213                                      pd->return_oop());
2214        !sd->is_top(); sd = sd->sender()) {
2215     sd->verify();
2216   }
2217 }
2218 
2219 void nmethod::verify_scopes() {
2220   if( !method() ) return;       // Runtime stubs have no scope
2221   if (method()->is_native()) return; // Ignore stub methods.
2222   // iterate through all interrupt point
2223   // and verify the debug information is valid.
2224   RelocIterator iter((nmethod*)this);
2225   while (iter.next()) {
2226     address stub = NULL;
2227     switch (iter.type()) {
2228       case relocInfo::virtual_call_type:
2229         verify_interrupt_point(iter.addr());
2230         break;
2231       case relocInfo::opt_virtual_call_type:
2232         stub = iter.opt_virtual_call_reloc()->static_stub(false);
2233         verify_interrupt_point(iter.addr());
2234         break;
2235       case relocInfo::static_call_type:
2236         stub = iter.static_call_reloc()->static_stub(false);
2237         //verify_interrupt_point(iter.addr());
2238         break;
2239       case relocInfo::runtime_call_type:
2240       case relocInfo::runtime_call_w_cp_type: {
2241         address destination = iter.reloc()->value();
2242         // Right now there is no way to find out which entries support
2243         // an interrupt point.  It would be nice if we had this
2244         // information in a table.
2245         break;
2246       }
2247       default:
2248         break;
2249     }
2250     assert(stub == NULL || stub_contains(stub), "static call stub outside stub section");
2251   }
2252 }
2253 
2254 
2255 // -----------------------------------------------------------------------------
2256 // Non-product code
2257 #ifndef PRODUCT
2258 
2259 class DebugScavengeRoot: public OopClosure {
2260   nmethod* _nm;
2261   bool     _ok;
2262 public:
2263   DebugScavengeRoot(nmethod* nm) : _nm(nm), _ok(true) { }
2264   bool ok() { return _ok; }
2265   virtual void do_oop(oop* p) {
2266     if ((*p) == NULL || !Universe::heap()->is_scavengable(*p))  return;
2267     if (_ok) {
2268       _nm->print_nmethod(true);
2269       _ok = false;
2270     }
2271     tty->print_cr("*** scavengable oop " PTR_FORMAT " found at " PTR_FORMAT " (offset %d)",
2272                   p2i(*p), p2i(p), (int)((intptr_t)p - (intptr_t)_nm));
2273     (*p)->print();
2274   }
2275   virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
2276 };
2277 
2278 void nmethod::verify_scavenge_root_oops() {
2279   if (!on_scavenge_root_list()) {
2280     // Actually look inside, to verify the claim that it's clean.
2281     DebugScavengeRoot debug_scavenge_root(this);
2282     oops_do(&debug_scavenge_root);
2283     if (!debug_scavenge_root.ok())
2284       fatal("found an unadvertised bad scavengable oop in the code cache");
2285   }
2286   assert(scavenge_root_not_marked(), "");
2287 }
2288 
2289 #endif // PRODUCT
2290 
2291 // Printing operations
2292 
2293 void nmethod::print() const {
2294   ResourceMark rm;
2295   ttyLocker ttyl;   // keep the following output all in one block
2296 
2297   tty->print("Compiled method ");
2298 
2299   if (is_compiled_by_c1()) {
2300     tty->print("(c1) ");
2301   } else if (is_compiled_by_c2()) {
2302     tty->print("(c2) ");
2303   } else if (is_compiled_by_jvmci()) {
2304     tty->print("(JVMCI) ");
2305   } else {
2306     tty->print("(nm) ");
2307   }
2308 
2309   print_on(tty, NULL);
2310 
2311   if (WizardMode) {
2312     tty->print("((nmethod*) " INTPTR_FORMAT ") ", p2i(this));
2313     tty->print(" for method " INTPTR_FORMAT , p2i(method()));
2314     tty->print(" { ");
2315     tty->print_cr("%s ", state());
2316     if (on_scavenge_root_list())  tty->print("scavenge_root ");
2317     tty->print_cr("}:");
2318   }
2319   if (size              () > 0) tty->print_cr(" total in heap  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2320                                               p2i(this),
2321                                               p2i(this) + size(),
2322                                               size());
2323   if (relocation_size   () > 0) tty->print_cr(" relocation     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2324                                               p2i(relocation_begin()),
2325                                               p2i(relocation_end()),
2326                                               relocation_size());
2327   if (consts_size       () > 0) tty->print_cr(" constants      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2328                                               p2i(consts_begin()),
2329                                               p2i(consts_end()),
2330                                               consts_size());
2331   if (insts_size        () > 0) tty->print_cr(" main code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2332                                               p2i(insts_begin()),
2333                                               p2i(insts_end()),
2334                                               insts_size());
2335   if (stub_size         () > 0) tty->print_cr(" stub code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2336                                               p2i(stub_begin()),
2337                                               p2i(stub_end()),
2338                                               stub_size());
2339   if (oops_size         () > 0) tty->print_cr(" oops           [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2340                                               p2i(oops_begin()),
2341                                               p2i(oops_end()),
2342                                               oops_size());
2343   if (metadata_size      () > 0) tty->print_cr(" metadata       [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2344                                               p2i(metadata_begin()),
2345                                               p2i(metadata_end()),
2346                                               metadata_size());
2347   if (scopes_data_size  () > 0) tty->print_cr(" scopes data    [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2348                                               p2i(scopes_data_begin()),
2349                                               p2i(scopes_data_end()),
2350                                               scopes_data_size());
2351   if (scopes_pcs_size   () > 0) tty->print_cr(" scopes pcs     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2352                                               p2i(scopes_pcs_begin()),
2353                                               p2i(scopes_pcs_end()),
2354                                               scopes_pcs_size());
2355   if (dependencies_size () > 0) tty->print_cr(" dependencies   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2356                                               p2i(dependencies_begin()),
2357                                               p2i(dependencies_end()),
2358                                               dependencies_size());
2359   if (handler_table_size() > 0) tty->print_cr(" handler table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2360                                               p2i(handler_table_begin()),
2361                                               p2i(handler_table_end()),
2362                                               handler_table_size());
2363   if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2364                                               p2i(nul_chk_table_begin()),
2365                                               p2i(nul_chk_table_end()),
2366                                               nul_chk_table_size());
2367 }
2368 
2369 #ifndef PRODUCT
2370 
2371 void nmethod::print_scopes() {
2372   // Find the first pc desc for all scopes in the code and print it.
2373   ResourceMark rm;
2374   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2375     if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
2376       continue;
2377 
2378     ScopeDesc* sd = scope_desc_at(p->real_pc(this));
2379     while (sd != NULL) {
2380       sd->print_on(tty, p);
2381       sd = sd->sender();
2382     }
2383   }
2384 }
2385 
2386 void nmethod::print_dependencies() {
2387   ResourceMark rm;
2388   ttyLocker ttyl;   // keep the following output all in one block
2389   tty->print_cr("Dependencies:");
2390   for (Dependencies::DepStream deps(this); deps.next(); ) {
2391     deps.print_dependency();
2392     Klass* ctxk = deps.context_type();
2393     if (ctxk != NULL) {
2394       if (ctxk->is_instance_klass() && InstanceKlass::cast(ctxk)->is_dependent_nmethod(this)) {
2395         tty->print_cr("   [nmethod<=klass]%s", ctxk->external_name());
2396       }
2397     }
2398     deps.log_dependency();  // put it into the xml log also
2399   }
2400 }
2401 
2402 
2403 void nmethod::print_relocations() {
2404   ResourceMark m;       // in case methods get printed via the debugger
2405   tty->print_cr("relocations:");
2406   RelocIterator iter(this);
2407   iter.print();
2408 }
2409 
2410 
2411 void nmethod::print_pcs() {
2412   ResourceMark m;       // in case methods get printed via debugger
2413   tty->print_cr("pc-bytecode offsets:");
2414   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2415     p->print(this);
2416   }
2417 }
2418 
2419 void nmethod::print_recorded_oops() {
2420   tty->print_cr("Recorded oops:");
2421   for (int i = 0; i < oops_count(); i++) {
2422     oop o = oop_at(i);
2423     tty->print("#%3d: " INTPTR_FORMAT " ", i, p2i(o));
2424     if (o == Universe::non_oop_word()) {
2425       tty->print("non-oop word");
2426     } else {
2427       if (o != NULL) {
2428         o->print_value();
2429       } else {
2430         tty->print_cr("NULL");
2431       }
2432     }
2433     tty->cr();
2434   }
2435 }
2436 
2437 void nmethod::print_recorded_metadata() {
2438   tty->print_cr("Recorded metadata:");
2439   for (int i = 0; i < metadata_count(); i++) {
2440     Metadata* m = metadata_at(i);
2441     tty->print("#%3d: " INTPTR_FORMAT " ", i, p2i(m));
2442     if (m == (Metadata*)Universe::non_oop_word()) {
2443       tty->print("non-metadata word");
2444     } else {
2445       Metadata::print_value_on_maybe_null(tty, m);
2446     }
2447     tty->cr();
2448   }
2449 }
2450 
2451 #endif // PRODUCT
2452 
2453 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) {
2454   RelocIterator iter(this, begin, end);
2455   bool have_one = false;
2456   while (iter.next()) {
2457     have_one = true;
2458     switch (iter.type()) {
2459         case relocInfo::none:                  return "no_reloc";
2460         case relocInfo::oop_type: {
2461           stringStream st;
2462           oop_Relocation* r = iter.oop_reloc();
2463           oop obj = r->oop_value();
2464           st.print("oop(");
2465           if (obj == NULL) st.print("NULL");
2466           else obj->print_value_on(&st);
2467           st.print(")");
2468           return st.as_string();
2469         }
2470         case relocInfo::metadata_type: {
2471           stringStream st;
2472           metadata_Relocation* r = iter.metadata_reloc();
2473           Metadata* obj = r->metadata_value();
2474           st.print("metadata(");
2475           if (obj == NULL) st.print("NULL");
2476           else obj->print_value_on(&st);
2477           st.print(")");
2478           return st.as_string();
2479         }
2480         case relocInfo::runtime_call_type:
2481         case relocInfo::runtime_call_w_cp_type: {
2482           stringStream st;
2483           st.print("runtime_call");
2484           CallRelocation* r = (CallRelocation*)iter.reloc();
2485           address dest = r->destination();
2486           CodeBlob* cb = CodeCache::find_blob(dest);
2487           if (cb != NULL) {
2488             st.print(" %s", cb->name());
2489           } else {
2490             ResourceMark rm;
2491             const int buflen = 1024;
2492             char* buf = NEW_RESOURCE_ARRAY(char, buflen);
2493             int offset;
2494             if (os::dll_address_to_function_name(dest, buf, buflen, &offset)) {
2495               st.print(" %s", buf);
2496               if (offset != 0) {
2497                 st.print("+%d", offset);
2498               }
2499             }
2500           }
2501           return st.as_string();
2502         }
2503         case relocInfo::virtual_call_type: {
2504           stringStream st;
2505           st.print_raw("virtual_call");
2506           virtual_call_Relocation* r = iter.virtual_call_reloc();
2507           Method* m = r->method_value();
2508           if (m != NULL) {
2509             assert(m->is_method(), "");
2510             m->print_short_name(&st);
2511           }
2512           return st.as_string();
2513         }
2514         case relocInfo::opt_virtual_call_type: {
2515           stringStream st;
2516           st.print_raw("optimized virtual_call");
2517           opt_virtual_call_Relocation* r = iter.opt_virtual_call_reloc();
2518           Method* m = r->method_value();
2519           if (m != NULL) {
2520             assert(m->is_method(), "");
2521             m->print_short_name(&st);
2522           }
2523           return st.as_string();
2524         }
2525         case relocInfo::static_call_type: {
2526           stringStream st;
2527           st.print_raw("static_call");
2528           static_call_Relocation* r = iter.static_call_reloc();
2529           Method* m = r->method_value();
2530           if (m != NULL) {
2531             assert(m->is_method(), "");
2532             m->print_short_name(&st);
2533           }
2534           return st.as_string();
2535         }
2536         case relocInfo::static_stub_type:      return "static_stub";
2537         case relocInfo::external_word_type:    return "external_word";
2538         case relocInfo::internal_word_type:    return "internal_word";
2539         case relocInfo::section_word_type:     return "section_word";
2540         case relocInfo::poll_type:             return "poll";
2541         case relocInfo::poll_return_type:      return "poll_return";
2542         case relocInfo::type_mask:             return "type_bit_mask";
2543 
2544         default:
2545           break;
2546     }
2547   }
2548   return have_one ? "other" : NULL;
2549 }
2550 
2551 // Return a the last scope in (begin..end]
2552 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
2553   PcDesc* p = pc_desc_near(begin+1);
2554   if (p != NULL && p->real_pc(this) <= end) {
2555     return new ScopeDesc(this, p->scope_decode_offset(),
2556                          p->obj_decode_offset(), p->should_reexecute(), p->rethrow_exception(),
2557                          p->return_oop());
2558   }
2559   return NULL;
2560 }
2561 
2562 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin) const {
2563   if (block_begin == entry_point())             stream->print_cr("[Entry Point]");
2564   if (block_begin == verified_entry_point())    stream->print_cr("[Verified Entry Point]");
2565   if (JVMCI_ONLY(_exception_offset >= 0 &&) block_begin == exception_begin())         stream->print_cr("[Exception Handler]");
2566   if (block_begin == stub_begin())              stream->print_cr("[Stub Code]");
2567   if (JVMCI_ONLY(_deopt_handler_begin != NULL &&) block_begin == deopt_handler_begin())     stream->print_cr("[Deopt Handler Code]");
2568 
2569   if (has_method_handle_invokes())
2570     if (block_begin == deopt_mh_handler_begin())  stream->print_cr("[Deopt MH Handler Code]");
2571 
2572   if (block_begin == consts_begin())            stream->print_cr("[Constants]");
2573 
2574   if (block_begin == entry_point()) {
2575     methodHandle m = method();
2576     if (m.not_null()) {
2577       stream->print("  # ");
2578       m->print_value_on(stream);
2579       stream->cr();
2580     }
2581     if (m.not_null() && !is_osr_method()) {
2582       ResourceMark rm;
2583       int sizeargs = m->size_of_parameters();
2584       BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
2585       VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
2586       {
2587         int sig_index = 0;
2588         if (!m->is_static())
2589           sig_bt[sig_index++] = T_OBJECT; // 'this'
2590         for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
2591           BasicType t = ss.type();
2592           sig_bt[sig_index++] = t;
2593           if (type2size[t] == 2) {
2594             sig_bt[sig_index++] = T_VOID;
2595           } else {
2596             assert(type2size[t] == 1, "size is 1 or 2");
2597           }
2598         }
2599         assert(sig_index == sizeargs, "");
2600       }
2601       const char* spname = "sp"; // make arch-specific?
2602       intptr_t out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs, false);
2603       int stack_slot_offset = this->frame_size() * wordSize;
2604       int tab1 = 14, tab2 = 24;
2605       int sig_index = 0;
2606       int arg_index = (m->is_static() ? 0 : -1);
2607       bool did_old_sp = false;
2608       for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
2609         bool at_this = (arg_index == -1);
2610         bool at_old_sp = false;
2611         BasicType t = (at_this ? T_OBJECT : ss.type());
2612         assert(t == sig_bt[sig_index], "sigs in sync");
2613         if (at_this)
2614           stream->print("  # this: ");
2615         else
2616           stream->print("  # parm%d: ", arg_index);
2617         stream->move_to(tab1);
2618         VMReg fst = regs[sig_index].first();
2619         VMReg snd = regs[sig_index].second();
2620         if (fst->is_reg()) {
2621           stream->print("%s", fst->name());
2622           if (snd->is_valid())  {
2623             stream->print(":%s", snd->name());
2624           }
2625         } else if (fst->is_stack()) {
2626           int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
2627           if (offset == stack_slot_offset)  at_old_sp = true;
2628           stream->print("[%s+0x%x]", spname, offset);
2629         } else {
2630           stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
2631         }
2632         stream->print(" ");
2633         stream->move_to(tab2);
2634         stream->print("= ");
2635         if (at_this) {
2636           m->method_holder()->print_value_on(stream);
2637         } else {
2638           bool did_name = false;
2639           if (!at_this && ss.is_object()) {
2640             Symbol* name = ss.as_symbol_or_null();
2641             if (name != NULL) {
2642               name->print_value_on(stream);
2643               did_name = true;
2644             }
2645           }
2646           if (!did_name)
2647             stream->print("%s", type2name(t));
2648         }
2649         if (at_old_sp) {
2650           stream->print("  (%s of caller)", spname);
2651           did_old_sp = true;
2652         }
2653         stream->cr();
2654         sig_index += type2size[t];
2655         arg_index += 1;
2656         if (!at_this)  ss.next();
2657       }
2658       if (!did_old_sp) {
2659         stream->print("  # ");
2660         stream->move_to(tab1);
2661         stream->print("[%s+0x%x]", spname, stack_slot_offset);
2662         stream->print("  (%s of caller)", spname);
2663         stream->cr();
2664       }
2665     }
2666   }
2667 }
2668 
2669 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) {
2670   // First, find an oopmap in (begin, end].
2671   // We use the odd half-closed interval so that oop maps and scope descs
2672   // which are tied to the byte after a call are printed with the call itself.
2673   address base = code_begin();
2674   ImmutableOopMapSet* oms = oop_maps();
2675   if (oms != NULL) {
2676     for (int i = 0, imax = oms->count(); i < imax; i++) {
2677       const ImmutableOopMapPair* pair = oms->pair_at(i);
2678       const ImmutableOopMap* om = pair->get_from(oms);
2679       address pc = base + pair->pc_offset();
2680       if (pc > begin) {
2681         if (pc <= end) {
2682           st->move_to(column);
2683           st->print("; ");
2684           om->print_on(st);
2685         }
2686         break;
2687       }
2688     }
2689   }
2690 
2691   // Print any debug info present at this pc.
2692   ScopeDesc* sd  = scope_desc_in(begin, end);
2693   if (sd != NULL) {
2694     st->move_to(column);
2695     if (sd->bci() == SynchronizationEntryBCI) {
2696       st->print(";*synchronization entry");
2697     } else if (sd->bci() == AfterBci) {
2698       st->print(";* method exit (unlocked if synchronized)");
2699     } else if (sd->bci() == UnwindBci) {
2700       st->print(";* unwind (locked if synchronized)");
2701     } else if (sd->bci() == AfterExceptionBci) {
2702       st->print(";* unwind (unlocked if synchronized)");
2703     } else if (sd->bci() == UnknownBci) {
2704       st->print(";* unknown");
2705     } else if (sd->bci() == InvalidFrameStateBci) {
2706       st->print(";* invalid frame state");
2707     } else {
2708       if (sd->method() == NULL) {
2709         st->print("method is NULL");
2710       } else if (sd->method()->is_native()) {
2711         st->print("method is native");
2712       } else {
2713         Bytecodes::Code bc = sd->method()->java_code_at(sd->bci());
2714         st->print(";*%s", Bytecodes::name(bc));
2715         switch (bc) {
2716         case Bytecodes::_invokevirtual:
2717         case Bytecodes::_invokespecial:
2718         case Bytecodes::_invokestatic:
2719         case Bytecodes::_invokeinterface:
2720           {
2721             Bytecode_invoke invoke(sd->method(), sd->bci());
2722             st->print(" ");
2723             if (invoke.name() != NULL)
2724               invoke.name()->print_symbol_on(st);
2725             else
2726               st->print("<UNKNOWN>");
2727             break;
2728           }
2729         case Bytecodes::_getfield:
2730         case Bytecodes::_putfield:
2731         case Bytecodes::_getstatic:
2732         case Bytecodes::_putstatic:
2733           {
2734             Bytecode_field field(sd->method(), sd->bci());
2735             st->print(" ");
2736             if (field.name() != NULL)
2737               field.name()->print_symbol_on(st);
2738             else
2739               st->print("<UNKNOWN>");
2740           }
2741         default:
2742           break;
2743         }
2744       }
2745       st->print(" {reexecute=%d rethrow=%d return_oop=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop());
2746     }
2747 
2748     // Print all scopes
2749     for (;sd != NULL; sd = sd->sender()) {
2750       st->move_to(column);
2751       st->print("; -");
2752       if (sd->method() == NULL) {
2753         st->print("method is NULL");
2754       } else {
2755         sd->method()->print_short_name(st);
2756       }
2757       int lineno = sd->method()->line_number_from_bci(sd->bci());
2758       if (lineno != -1) {
2759         st->print("@%d (line %d)", sd->bci(), lineno);
2760       } else {
2761         st->print("@%d", sd->bci());
2762       }
2763       st->cr();
2764     }
2765   }
2766 
2767   // Print relocation information
2768   const char* str = reloc_string_for(begin, end);
2769   if (str != NULL) {
2770     if (sd != NULL) st->cr();
2771     st->move_to(column);
2772     st->print(";   {%s}", str);
2773   }
2774   int cont_offset = ImplicitExceptionTable(this).at(begin - code_begin());
2775   if (cont_offset != 0) {
2776     st->move_to(column);
2777     st->print("; implicit exception: dispatches to " INTPTR_FORMAT, p2i(code_begin() + cont_offset));
2778   }
2779 
2780 }
2781 
2782 class DirectNativeCallWrapper: public NativeCallWrapper {
2783 private:
2784   NativeCall* _call;
2785 
2786 public:
2787   DirectNativeCallWrapper(NativeCall* call) : _call(call) {}
2788 
2789   virtual address destination() const { return _call->destination(); }
2790   virtual address instruction_address() const { return _call->instruction_address(); }
2791   virtual address next_instruction_address() const { return _call->next_instruction_address(); }
2792   virtual address return_address() const { return _call->return_address(); }
2793 
2794   virtual address get_resolve_call_stub(bool is_optimized) const {
2795     if (is_optimized) {
2796       return SharedRuntime::get_resolve_opt_virtual_call_stub();
2797     }
2798     return SharedRuntime::get_resolve_virtual_call_stub();
2799   }
2800 
2801   virtual void set_destination_mt_safe(address dest) {
2802 #if INCLUDE_AOT
2803     if (UseAOT) {
2804       CodeBlob* callee = CodeCache::find_blob(dest);
2805       CompiledMethod* cm = callee->as_compiled_method_or_null();
2806       if (cm != NULL && cm->is_far_code()) {
2807         // Temporary fix, see JDK-8143106
2808         CompiledDirectStaticCall* csc = CompiledDirectStaticCall::at(instruction_address());
2809         csc->set_to_far(methodHandle(cm->method()), dest);
2810         return;
2811       }
2812     }
2813 #endif
2814     _call->set_destination_mt_safe(dest);
2815   }
2816 
2817   virtual void set_to_interpreted(const methodHandle& method, CompiledICInfo& info) {
2818     CompiledDirectStaticCall* csc = CompiledDirectStaticCall::at(instruction_address());
2819 #if INCLUDE_AOT
2820     if (info.to_aot()) {
2821       csc->set_to_far(method, info.entry());
2822     } else
2823 #endif
2824     {
2825       csc->set_to_interpreted(method, info.entry());
2826     }
2827   }
2828 
2829   virtual void verify() const {
2830     // make sure code pattern is actually a call imm32 instruction
2831     _call->verify();
2832     _call->verify_alignment();
2833   }
2834 
2835   virtual void verify_resolve_call(address dest) const {
2836     CodeBlob* db = CodeCache::find_blob_unsafe(dest);
2837     assert(db != NULL && !db->is_adapter_blob(), "must use stub!");
2838   }
2839 
2840   virtual bool is_call_to_interpreted(address dest) const {
2841     CodeBlob* cb = CodeCache::find_blob(_call->instruction_address());
2842     return cb->contains(dest);
2843   }
2844 
2845   virtual bool is_safe_for_patching() const { return false; }
2846 
2847   virtual NativeInstruction* get_load_instruction(virtual_call_Relocation* r) const {
2848     return nativeMovConstReg_at(r->cached_value());
2849   }
2850 
2851   virtual void *get_data(NativeInstruction* instruction) const {
2852     return (void*)((NativeMovConstReg*) instruction)->data();
2853   }
2854 
2855   virtual void set_data(NativeInstruction* instruction, intptr_t data) {
2856     ((NativeMovConstReg*) instruction)->set_data(data);
2857   }
2858 };
2859 
2860 NativeCallWrapper* nmethod::call_wrapper_at(address call) const {
2861   return new DirectNativeCallWrapper((NativeCall*) call);
2862 }
2863 
2864 NativeCallWrapper* nmethod::call_wrapper_before(address return_pc) const {
2865   return new DirectNativeCallWrapper(nativeCall_before(return_pc));
2866 }
2867 
2868 address nmethod::call_instruction_address(address pc) const {
2869   if (NativeCall::is_call_before(pc)) {
2870     NativeCall *ncall = nativeCall_before(pc);
2871     return ncall->instruction_address();
2872   }
2873   return NULL;
2874 }
2875 
2876 CompiledStaticCall* nmethod::compiledStaticCall_at(Relocation* call_site) const {
2877   return CompiledDirectStaticCall::at(call_site);
2878 }
2879 
2880 CompiledStaticCall* nmethod::compiledStaticCall_at(address call_site) const {
2881   return CompiledDirectStaticCall::at(call_site);
2882 }
2883 
2884 CompiledStaticCall* nmethod::compiledStaticCall_before(address return_addr) const {
2885   return CompiledDirectStaticCall::before(return_addr);
2886 }
2887 
2888 #ifndef PRODUCT
2889 
2890 void nmethod::print_value_on(outputStream* st) const {
2891   st->print("nmethod");
2892   print_on(st, NULL);
2893 }
2894 
2895 void nmethod::print_calls(outputStream* st) {
2896   RelocIterator iter(this);
2897   while (iter.next()) {
2898     switch (iter.type()) {
2899     case relocInfo::virtual_call_type:
2900     case relocInfo::opt_virtual_call_type: {
2901       CompiledICLocker ml_verify(this);
2902       CompiledIC_at(&iter)->print();
2903       break;
2904     }
2905     case relocInfo::static_call_type:
2906       st->print_cr("Static call at " INTPTR_FORMAT, p2i(iter.reloc()->addr()));
2907       CompiledDirectStaticCall::at(iter.reloc())->print();
2908       break;
2909     default:
2910       break;
2911     }
2912   }
2913 }
2914 
2915 void nmethod::print_handler_table() {
2916   ExceptionHandlerTable(this).print();
2917 }
2918 
2919 void nmethod::print_nul_chk_table() {
2920   ImplicitExceptionTable(this).print(code_begin());
2921 }
2922 
2923 void nmethod::print_statistics() {
2924   ttyLocker ttyl;
2925   if (xtty != NULL)  xtty->head("statistics type='nmethod'");
2926   native_nmethod_stats.print_native_nmethod_stats();
2927 #ifdef COMPILER1
2928   c1_java_nmethod_stats.print_nmethod_stats("C1");
2929 #endif
2930 #ifdef COMPILER2
2931   c2_java_nmethod_stats.print_nmethod_stats("C2");
2932 #endif
2933 #if INCLUDE_JVMCI
2934   jvmci_java_nmethod_stats.print_nmethod_stats("JVMCI");
2935 #endif
2936   unknown_java_nmethod_stats.print_nmethod_stats("Unknown");
2937   DebugInformationRecorder::print_statistics();
2938 #ifndef PRODUCT
2939   pc_nmethod_stats.print_pc_stats();
2940 #endif
2941   Dependencies::print_statistics();
2942   if (xtty != NULL)  xtty->tail("statistics");
2943 }
2944 
2945 #endif // !PRODUCT
2946 
2947 #if INCLUDE_JVMCI
2948 void nmethod::clear_jvmci_installed_code() {
2949   assert_locked_or_safepoint(Patching_lock);
2950   if (_jvmci_installed_code != NULL) {
2951     JNIHandles::destroy_weak_global(_jvmci_installed_code);
2952     _jvmci_installed_code = NULL;
2953   }
2954 }
2955 
2956 void nmethod::clear_speculation_log() {
2957   assert_locked_or_safepoint(Patching_lock);
2958   if (_speculation_log != NULL) {
2959     JNIHandles::destroy_weak_global(_speculation_log);
2960     _speculation_log = NULL;
2961   }
2962 }
2963 
2964 void nmethod::maybe_invalidate_installed_code() {
2965   if (!is_compiled_by_jvmci()) {
2966     return;
2967   }
2968 
2969   assert(Patching_lock->is_locked() ||
2970          SafepointSynchronize::is_at_safepoint(), "should be performed under a lock for consistency");
2971   oop installed_code = JNIHandles::resolve(_jvmci_installed_code);
2972   if (installed_code != NULL) {
2973     // Update the values in the InstalledCode instance if it still refers to this nmethod
2974     nmethod* nm = (nmethod*)InstalledCode::address(installed_code);
2975     if (nm == this) {
2976       if (!is_alive() || is_unloading()) {
2977         // Break the link between nmethod and InstalledCode such that the nmethod
2978         // can subsequently be flushed safely.  The link must be maintained while
2979         // the method could have live activations since invalidateInstalledCode
2980         // might want to invalidate all existing activations.
2981         InstalledCode::set_address(installed_code, 0);
2982         InstalledCode::set_entryPoint(installed_code, 0);
2983       } else if (is_not_entrant()) {
2984         // Remove the entry point so any invocation will fail but keep
2985         // the address link around that so that existing activations can
2986         // be invalidated.
2987         InstalledCode::set_entryPoint(installed_code, 0);
2988       }
2989     }
2990   }
2991   if (!is_alive() || is_unloading()) {
2992     // Clear these out after the nmethod has been unregistered and any
2993     // updates to the InstalledCode instance have been performed.
2994     clear_jvmci_installed_code();
2995     clear_speculation_log();
2996   }
2997 }
2998 
2999 void nmethod::invalidate_installed_code(Handle installedCode, TRAPS) {
3000   if (installedCode() == NULL) {
3001     THROW(vmSymbols::java_lang_NullPointerException());
3002   }
3003   jlong nativeMethod = InstalledCode::address(installedCode);
3004   nmethod* nm = (nmethod*)nativeMethod;
3005   if (nm == NULL) {
3006     // Nothing to do
3007     return;
3008   }
3009 
3010   nmethodLocker nml(nm);
3011 #ifdef ASSERT
3012   {
3013     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
3014     // This relationship can only be checked safely under a lock
3015     assert(!nm->is_alive() || nm->is_unloading() || nm->jvmci_installed_code() == installedCode(), "sanity check");
3016   }
3017 #endif
3018 
3019   if (nm->is_alive()) {
3020     // Invalidating the InstalledCode means we want the nmethod
3021     // to be deoptimized.
3022     nm->mark_for_deoptimization();
3023     VM_Deoptimize op;
3024     VMThread::execute(&op);
3025   }
3026 
3027   // Multiple threads could reach this point so we now need to
3028   // lock and re-check the link to the nmethod so that only one
3029   // thread clears it.
3030   MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
3031   if (InstalledCode::address(installedCode) == nativeMethod) {
3032       InstalledCode::set_address(installedCode, 0);
3033   }
3034 }
3035 
3036 oop nmethod::jvmci_installed_code() {
3037   return JNIHandles::resolve(_jvmci_installed_code);
3038 }
3039 
3040 oop nmethod::speculation_log() {
3041   return JNIHandles::resolve(_speculation_log);
3042 }
3043 
3044 char* nmethod::jvmci_installed_code_name(char* buf, size_t buflen) const {
3045   if (!this->is_compiled_by_jvmci()) {
3046     return NULL;
3047   }
3048   oop installed_code = JNIHandles::resolve(_jvmci_installed_code);
3049   if (installed_code != NULL) {
3050     oop installed_code_name = NULL;
3051     if (installed_code->is_a(InstalledCode::klass())) {
3052       installed_code_name = InstalledCode::name(installed_code);
3053     }
3054     if (installed_code_name != NULL) {
3055       return java_lang_String::as_utf8_string(installed_code_name, buf, (int)buflen);
3056     }
3057   }
3058   return NULL;
3059 }
3060 #endif
--- EOF ---