29 #include "code/pcDesc.hpp"
30 #include "oops/metadata.hpp"
31 #include "oops/method.hpp"
32 #include "runtime/mutexLocker.hpp"
33
34 class AbstractCompiler;
35 class CompiledDirectCall;
36 class CompiledIC;
37 class CompiledICData;
38 class CompileTask;
39 class DepChange;
40 class Dependencies;
41 class DirectiveSet;
42 class DebugInformationRecorder;
43 class ExceptionHandlerTable;
44 class ImplicitExceptionTable;
45 class JvmtiThreadState;
46 class MetadataClosure;
47 class NativeCallWrapper;
48 class OopIterateClosure;
49 class ScopeDesc;
50 class xmlStream;
51
52 // This class is used internally by nmethods, to cache
53 // exception/pc/handler information.
54
55 class ExceptionCache : public CHeapObj<mtCode> {
56 friend class VMStructs;
57 private:
58 enum { cache_size = 16 };
59 Klass* _exception_type;
60 address _pc[cache_size];
61 address _handler[cache_size];
62 volatile int _count;
63 ExceptionCache* volatile _next;
64 ExceptionCache* _purge_list_next;
65
66 inline address pc_at(int index);
67 void set_pc_at(int index, address a) { assert(index >= 0 && index < cache_size,""); _pc[index] = a; }
68
161 class FailedSpeculation;
162 class JVMCINMethodData;
163 #endif
164
165 class nmethod : public CodeBlob {
166 friend class VMStructs;
167 friend class JVMCIVMStructs;
168 friend class CodeCache; // scavengable oops
169 friend class JVMCINMethodData;
170 friend class DeoptimizationScope;
171
172 #define ImmutableDataRefCountSize ((int)sizeof(int))
173
174 private:
175
176 // Used to track in which deoptimize handshake this method will be deoptimized.
177 uint64_t _deoptimization_generation;
178
179 uint64_t _gc_epoch;
180
181 Method* _method;
182
183 // To reduce header size union fields which usages do not overlap.
184 union {
185 // To support simple linked-list chaining of nmethods:
186 nmethod* _osr_link; // from InstanceKlass::osr_nmethods_head
187 struct {
188 // These are used for compiled synchronized native methods to
189 // locate the owner and stack slot for the BasicLock. They are
190 // needed because there is no debug information for compiled native
191 // wrappers and the oop maps are insufficient to allow
192 // frame::retrieve_receiver() to work. Currently they are expected
193 // to be byte offsets from the Java stack pointer for maximum code
194 // sharing between platforms. JVMTI's GetLocalInstance() uses these
195 // offsets to find the receiver for non-static native wrapper frames.
196 ByteSize _native_receiver_sp_offset;
197 ByteSize _native_basic_lock_sp_offset;
198 };
199 };
200
244
245 // Offset in immutable data section
246 // _dependencies_offset == 0
247 uint16_t _nul_chk_table_offset;
248 uint16_t _handler_table_offset; // This table could be big in C1 code
249 int _scopes_pcs_offset;
250 int _scopes_data_offset;
251 #if INCLUDE_JVMCI
252 int _speculations_offset;
253 #endif
254 int _immutable_data_ref_count_offset;
255
256 // location in frame (offset for sp) that deopt can store the original
257 // pc during a deopt.
258 int _orig_pc_offset;
259
260 int _compile_id; // which compilation made this nmethod
261 CompLevel _comp_level; // compilation level (s1)
262 CompilerType _compiler_type; // which compiler made this nmethod (u1)
263
264 // Local state used to keep track of whether unloading is happening or not
265 volatile uint8_t _is_unloading_state;
266
267 // Protected by NMethodState_lock
268 volatile signed char _state; // {not_installed, in_use, not_entrant}
269
270 // set during construction
271 uint8_t _has_unsafe_access:1, // May fault due to unsafe access.
272 _has_wide_vectors:1, // Preserve wide vectors at safepoints
273 _has_monitors:1, // Fastpath monitor detection for continuations
274 _has_scoped_access:1, // used by for shared scope closure (scopedMemoryAccess.cpp)
275 _has_flushed_dependencies:1, // Used for maintenance of dependencies (under CodeCache_lock)
276 _is_unlinked:1, // mark during class unloading
277 _load_reported:1; // used by jvmti to track if an event has been posted for this nmethod
278
279 enum DeoptimizationStatus : u1 {
280 not_marked,
281 deoptimize,
282 deoptimize_noupdate,
283 deoptimize_done
284 };
285
286 volatile DeoptimizationStatus _deoptimization_status; // Used for stack deoptimization
287
288 DeoptimizationStatus deoptimization_status() const {
289 return AtomicAccess::load(&_deoptimization_status);
290 }
291
292 // Initialize fields to their default values
293 void init_defaults(CodeBuffer *code_buffer, CodeOffsets* offsets);
294
295 // Post initialization
296 void post_init();
297
456 // Attempt Unclaimed -> N|WR transition. Returns true if successful.
457 bool oops_do_try_claim_weak_request();
458
459 // Attempt Unclaimed -> N|SD transition. Returns the current link.
460 oops_do_mark_link* oops_do_try_claim_strong_done();
461 // Attempt N|WR -> X|WD transition. Returns nullptr if successful, X otherwise.
462 nmethod* oops_do_try_add_to_list_as_weak_done();
463
464 // Attempt X|WD -> N|SR transition. Returns the current link.
465 oops_do_mark_link* oops_do_try_add_strong_request(oops_do_mark_link* next);
466 // Attempt X|WD -> X|SD transition. Returns true if successful.
467 bool oops_do_try_claim_weak_done_as_strong_done(oops_do_mark_link* next);
468
469 // Do the N|SD -> X|SD transition.
470 void oops_do_add_to_list_as_strong_done();
471
472 // Sets this nmethod as strongly claimed (as part of N|SD -> X|SD and N|SR -> X|SD
473 // transitions).
474 void oops_do_set_strong_done(nmethod* old_head);
475
476 public:
477 // If you change anything in this enum please patch
478 // vmStructs_jvmci.cpp accordingly.
479 enum class InvalidationReason : s1 {
480 NOT_INVALIDATED = -1,
481 C1_CODEPATCH,
482 C1_DEOPTIMIZE,
483 C1_DEOPTIMIZE_FOR_PATCHING,
484 C1_PREDICATE_FAILED_TRAP,
485 CI_REPLAY,
486 UNLOADING,
487 UNLOADING_COLD,
488 JVMCI_INVALIDATE,
489 JVMCI_MATERIALIZE_VIRTUAL_OBJECT,
490 JVMCI_REPLACED_WITH_NEW_CODE,
491 JVMCI_REPROFILE,
492 MARKED_FOR_DEOPTIMIZATION,
493 MISSING_EXCEPTION_HANDLER,
494 NOT_USED,
495 OSR_INVALIDATION_BACK_BRANCH,
496 OSR_INVALIDATION_FOR_COMPILING_WITH_C1,
573 );
574
575 // Relocate the nmethod to the code heap identified by code_blob_type.
576 // Returns nullptr if the code heap does not have enough space, the
577 // nmethod is unrelocatable, or the nmethod is invalidated during relocation,
578 // otherwise the relocated nmethod. The original nmethod will be marked not entrant.
579 nmethod* relocate(CodeBlobType code_blob_type);
580
581 static nmethod* new_native_nmethod(const methodHandle& method,
582 int compile_id,
583 CodeBuffer *code_buffer,
584 int vep_offset,
585 int frame_complete,
586 int frame_size,
587 ByteSize receiver_sp_offset,
588 ByteSize basic_lock_sp_offset,
589 OopMapSet* oop_maps,
590 int exception_handler = -1);
591
592 Method* method () const { return _method; }
593 bool is_native_method() const { return _method != nullptr && _method->is_native(); }
594 bool is_java_method () const { return _method != nullptr && !_method->is_native(); }
595 bool is_osr_method () const { return _entry_bci != InvocationEntryBci; }
596
597 bool is_relocatable();
598
599 // Compiler task identification. Note that all OSR methods
600 // are numbered in an independent sequence if CICountOSR is true,
601 // and native method wrappers are also numbered independently if
602 // CICountNative is true.
603 int compile_id() const { return _compile_id; }
604 const char* compile_kind() const;
605
606 inline bool is_compiled_by_c1 () const { return _compiler_type == compiler_c1; }
607 inline bool is_compiled_by_c2 () const { return _compiler_type == compiler_c2; }
608 inline bool is_compiled_by_jvmci() const { return _compiler_type == compiler_jvmci; }
609 CompilerType compiler_type () const { return _compiler_type; }
610 const char* compiler_name () const;
611
612 // boundaries for different parts
613 address consts_begin () const { return content_begin(); }
614 address consts_end () const { return code_begin() ; }
615 address insts_begin () const { return code_begin() ; }
616 address insts_end () const { return header_begin() + _stub_offset ; }
617 address stub_begin () const { return header_begin() + _stub_offset ; }
618 address stub_end () const { return code_end() ; }
619 address exception_begin () const { return header_begin() + _exception_offset ; }
620 address deopt_handler_begin () const { return header_begin() + _deopt_handler_offset ; }
621 address unwind_handler_begin () const { return _unwind_handler_offset != -1 ? (insts_end() - _unwind_handler_offset) : nullptr; }
622 oop* oops_begin () const { return (oop*) data_begin(); }
623 oop* oops_end () const { return (oop*) data_end(); }
624
625 // mutable data
626 Metadata** metadata_begin () const { return (Metadata**) (mutable_data_begin() + _relocation_size); }
627 #if INCLUDE_JVMCI
628 Metadata** metadata_end () const { return (Metadata**) (mutable_data_begin() + _relocation_size + _metadata_size); }
629 address jvmci_data_begin () const { return mutable_data_begin() + _relocation_size + _metadata_size; }
630 address jvmci_data_end () const { return mutable_data_end(); }
631 #else
632 Metadata** metadata_end () const { return (Metadata**) mutable_data_end(); }
633 #endif
634
635 // immutable data
636 address immutable_data_begin () const { return _immutable_data; }
637 address immutable_data_end () const { return _immutable_data + _immutable_data_size ; }
638 address dependencies_begin () const { return _immutable_data; }
639 address dependencies_end () const { return _immutable_data + _nul_chk_table_offset; }
640 address nul_chk_table_begin () const { return _immutable_data + _nul_chk_table_offset; }
641 address nul_chk_table_end () const { return _immutable_data + _handler_table_offset; }
642 address handler_table_begin () const { return _immutable_data + _handler_table_offset; }
643 address handler_table_end () const { return _immutable_data + _scopes_pcs_offset ; }
644 PcDesc* scopes_pcs_begin () const { return (PcDesc*)(_immutable_data + _scopes_pcs_offset) ; }
645 PcDesc* scopes_pcs_end () const { return (PcDesc*)(_immutable_data + _scopes_data_offset) ; }
646 address scopes_data_begin () const { return _immutable_data + _scopes_data_offset ; }
647
648 #if INCLUDE_JVMCI
649 address scopes_data_end () const { return _immutable_data + _speculations_offset ; }
650 address speculations_begin () const { return _immutable_data + _speculations_offset ; }
651 address speculations_end () const { return _immutable_data + _immutable_data_ref_count_offset ; }
652 #else
653 address scopes_data_end () const { return _immutable_data + _immutable_data_ref_count_offset ; }
654 #endif
655 address immutable_data_ref_count_begin () const { return _immutable_data + _immutable_data_ref_count_offset ; }
696 address verified_entry_point() const { return code_begin() + _verified_entry_offset; } // if klass is correct
697
698 enum : signed char { not_installed = -1, // in construction, only the owner doing the construction is
699 // allowed to advance state
700 in_use = 0, // executable nmethod
701 not_entrant = 1 // marked for deoptimization but activations may still exist
702 };
703
704 // flag accessing and manipulation
705 bool is_not_installed() const { return _state == not_installed; }
706 bool is_in_use() const { return _state <= in_use; }
707 bool is_not_entrant() const { return _state == not_entrant; }
708 int get_state() const { return _state; }
709
710 void clear_unloading_state();
711 // Heuristically deduce an nmethod isn't worth keeping around
712 bool is_cold();
713 bool is_unloading();
714 void do_unloading(bool unloading_occurred);
715
716 bool make_in_use() {
717 return try_transition(in_use);
718 }
719 // Make the nmethod non entrant. The nmethod will continue to be
720 // alive. It is used when an uncommon trap happens. Returns true
721 // if this thread changed the state of the nmethod or false if
722 // another thread performed the transition.
723 bool make_not_entrant(InvalidationReason invalidation_reason);
724 bool make_not_used() { return make_not_entrant(InvalidationReason::NOT_USED); }
725
726 bool is_marked_for_deoptimization() const { return deoptimization_status() != not_marked; }
727 bool has_been_deoptimized() const { return deoptimization_status() == deoptimize_done; }
728 void set_deoptimized_done();
729
730 bool update_recompile_counts() const {
731 // Update recompile counts when either the update is explicitly requested (deoptimize)
732 // or the nmethod is not marked for deoptimization at all (not_marked).
733 // The latter happens during uncommon traps when deoptimized nmethod is made not entrant.
734 DeoptimizationStatus status = deoptimization_status();
735 return status != deoptimize_noupdate && status != deoptimize_done;
736 }
737
738 // tells whether frames described by this nmethod can be deoptimized
739 // note: native wrappers cannot be deoptimized.
740 bool can_be_deoptimized() const { return is_java_method(); }
741
742 bool has_dependencies() { return dependencies_size() != 0; }
743 void print_dependencies_on(outputStream* out) PRODUCT_RETURN;
744 void flush_dependencies();
745
746 template<typename T>
747 T* gc_data() const { return reinterpret_cast<T*>(_gc_data); }
748 template<typename T>
749 void set_gc_data(T* gc_data) { _gc_data = reinterpret_cast<void*>(gc_data); }
750
751 bool has_unsafe_access() const { return _has_unsafe_access; }
752 void set_has_unsafe_access(bool z) { _has_unsafe_access = z; }
753
754 bool has_monitors() const { return _has_monitors; }
755 void set_has_monitors(bool z) { _has_monitors = z; }
756
757 bool has_scoped_access() const { return _has_scoped_access; }
758 void set_has_scoped_access(bool z) { _has_scoped_access = z; }
759
760 bool has_wide_vectors() const { return _has_wide_vectors; }
761 void set_has_wide_vectors(bool z) { _has_wide_vectors = z; }
762
763 bool has_flushed_dependencies() const { return _has_flushed_dependencies; }
764 void set_has_flushed_dependencies(bool z) {
765 assert(!has_flushed_dependencies(), "should only happen once");
766 _has_flushed_dependencies = z;
767 }
768
769 bool is_unlinked() const { return _is_unlinked; }
770 void set_is_unlinked() {
771 assert(!_is_unlinked, "already unlinked");
772 _is_unlinked = true;
773 }
774
775 int comp_level() const { return _comp_level; }
776
777 // Support for oops in scopes and relocs:
778 // Note: index 0 is reserved for null.
779 oop oop_at(int index) const;
780 oop oop_at_phantom(int index) const; // phantom reference
781 oop* oop_addr_at(int index) const { // for GC
782 // relocation indexes are biased by 1 (because 0 is reserved)
783 assert(index > 0 && index <= oops_count(), "must be a valid non-zero index");
784 return &oops_begin()[index - 1];
785 }
786
787 // Support for meta data in scopes and relocs:
788 // Note: index 0 is reserved for null.
789 Metadata* metadata_at(int index) const { return index == 0 ? nullptr: *metadata_addr_at(index); }
790 Metadata** metadata_addr_at(int index) const { // for GC
791 // relocation indexes are biased by 1 (because 0 is reserved)
792 assert(index > 0 && index <= metadata_count(), "must be a valid non-zero index");
793 return &metadata_begin()[index - 1];
794 }
795
796 void copy_values(GrowableArray<jobject>* oops);
797 void copy_values(GrowableArray<Metadata*>* metadata);
798 void copy_values(GrowableArray<address>* metadata) {} // Nothing to do
799
800 // Relocation support
801 private:
802 void fix_oop_relocations(address begin, address end, bool initialize_immediates);
803 inline void initialize_immediate_oop(oop* dest, jobject handle);
804
805 protected:
806 address oops_reloc_begin() const;
807
808 public:
809 void fix_oop_relocations(address begin, address end) { fix_oop_relocations(begin, end, false); }
810 void fix_oop_relocations() { fix_oop_relocations(nullptr, nullptr, false); }
811
812 bool is_at_poll_return(address pc);
813 bool is_at_poll_or_poll_return(address pc);
814
815 protected:
816 // Exception cache support
817 // Note: _exception_cache may be read and cleaned concurrently.
818 ExceptionCache* exception_cache() const { return _exception_cache; }
819 ExceptionCache* exception_cache_acquire() const;
820
821 public:
822 address handler_for_exception_and_pc(Handle exception, address pc);
823 void add_handler_for_exception_and_pc(Handle exception, address pc, address handler);
824 void clean_exception_cache();
825
826 void add_exception_cache_entry(ExceptionCache* new_entry);
827 ExceptionCache* exception_cache_entry_for_exception(Handle exception);
828
829
830 // Deopt
831 // Return true is the PC is one would expect if the frame is being deopted.
981 assert(*ref_count > 0, "Must be positive");
982 return --(*ref_count);
983 }
984
985 static void add_delayed_compiled_method_load_event(nmethod* nm) NOT_CDS_RETURN;
986
987 public:
988 // ScopeDesc retrieval operation
989 PcDesc* pc_desc_at(address pc) { return find_pc_desc(pc, false); }
990 // pc_desc_near returns the first PcDesc at or after the given pc.
991 PcDesc* pc_desc_near(address pc) { return find_pc_desc(pc, true); }
992
993 // ScopeDesc for an instruction
994 ScopeDesc* scope_desc_at(address pc);
995 ScopeDesc* scope_desc_near(address pc);
996
997 // copying of debugging information
998 void copy_scopes_pcs(PcDesc* pcs, int count);
999 void copy_scopes_data(address buffer, int size);
1000
1001 int orig_pc_offset() { return _orig_pc_offset; }
1002
1003 // Post successful compilation
1004 void post_compiled_method(CompileTask* task);
1005
1006 // jvmti support:
1007 void post_compiled_method_load_event(JvmtiThreadState* state = nullptr);
1008
1009 // verify operations
1010 void verify();
1011 void verify_scopes();
1012 void verify_interrupt_point(address interrupt_point, bool is_inline_cache);
1013
1014 // Disassemble this nmethod with additional debug information, e.g. information about blocks.
1015 void decode2(outputStream* st) const;
1016 void print_constant_pool(outputStream* st);
1017
1018 // Avoid hiding of parent's 'decode(outputStream*)' method.
1019 void decode(outputStream* st) const { decode2(st); } // just delegate here.
1020
1021 // AOT cache support
1022 static void post_delayed_compiled_method_load_events() NOT_CDS_RETURN;
1023
1024 // printing support
1025 void print_on_impl(outputStream* st) const;
1026 void print_code();
1027 void print_value_on_impl(outputStream* st) const;
1028 void print_code_snippet(outputStream* st, address addr) const;
1029
1030 #if defined(SUPPORT_DATA_STRUCTS)
1031 // print output in opt build for disassembler library
1032 void print_relocations() PRODUCT_RETURN;
1033 void print_pcs_on(outputStream* st);
1034 void print_scopes() { print_scopes_on(tty); }
1035 void print_scopes_on(outputStream* st) PRODUCT_RETURN;
1036 void print_handler_table();
1037 void print_nul_chk_table();
1038 void print_recorded_oop(int log_n, int index);
1039 void print_recorded_oops();
1040 void print_recorded_metadata();
1041
1042 void print_oops(outputStream* st); // oops from the underlying CodeBlob.
1043 void print_metadata(outputStream* st); // metadata in metadata pool.
1044 #else
1045 void print_pcs_on(outputStream* st) { return; }
1046 #endif
1047
1048 void print_calls(outputStream* st) PRODUCT_RETURN;
1049 static void print_statistics() PRODUCT_RETURN;
1050
1051 void maybe_print_nmethod(const DirectiveSet* directive);
1052 void print_nmethod(bool print_code);
1081 ByteSize native_receiver_sp_offset() {
1082 assert(is_native_method(), "sanity");
1083 return _native_receiver_sp_offset;
1084 }
1085 ByteSize native_basic_lock_sp_offset() {
1086 assert(is_native_method(), "sanity");
1087 return _native_basic_lock_sp_offset;
1088 }
1089
1090 // support for code generation
1091 static ByteSize osr_entry_point_offset() { return byte_offset_of(nmethod, _osr_entry_point); }
1092 static ByteSize state_offset() { return byte_offset_of(nmethod, _state); }
1093
1094 void metadata_do(MetadataClosure* f);
1095
1096 address call_instruction_address(address pc) const;
1097
1098 void make_deoptimized();
1099 void finalize_relocations();
1100
1101 class Vptr : public CodeBlob::Vptr {
1102 void print_on(const CodeBlob* instance, outputStream* st) const override {
1103 ttyLocker ttyl;
1104 instance->as_nmethod()->print_on_impl(st);
1105 }
1106 void print_value_on(const CodeBlob* instance, outputStream* st) const override {
1107 instance->as_nmethod()->print_value_on_impl(st);
1108 }
1109 };
1110
1111 static const Vptr _vpntr;
1112 };
1113
1114 struct NMethodMarkingScope : StackObj {
1115 NMethodMarkingScope() {
1116 nmethod::oops_do_marking_prologue();
1117 }
1118 ~NMethodMarkingScope() {
1119 nmethod::oops_do_marking_epilogue();
1120 }
1121 };
1122
1123 #endif // SHARE_CODE_NMETHOD_HPP
|
29 #include "code/pcDesc.hpp"
30 #include "oops/metadata.hpp"
31 #include "oops/method.hpp"
32 #include "runtime/mutexLocker.hpp"
33
34 class AbstractCompiler;
35 class CompiledDirectCall;
36 class CompiledIC;
37 class CompiledICData;
38 class CompileTask;
39 class DepChange;
40 class Dependencies;
41 class DirectiveSet;
42 class DebugInformationRecorder;
43 class ExceptionHandlerTable;
44 class ImplicitExceptionTable;
45 class JvmtiThreadState;
46 class MetadataClosure;
47 class NativeCallWrapper;
48 class OopIterateClosure;
49 class AOTCodeReader;
50 class AOTCodeEntry;
51 class ScopeDesc;
52 class xmlStream;
53
54 // This class is used internally by nmethods, to cache
55 // exception/pc/handler information.
56
57 class ExceptionCache : public CHeapObj<mtCode> {
58 friend class VMStructs;
59 private:
60 enum { cache_size = 16 };
61 Klass* _exception_type;
62 address _pc[cache_size];
63 address _handler[cache_size];
64 volatile int _count;
65 ExceptionCache* volatile _next;
66 ExceptionCache* _purge_list_next;
67
68 inline address pc_at(int index);
69 void set_pc_at(int index, address a) { assert(index >= 0 && index < cache_size,""); _pc[index] = a; }
70
163 class FailedSpeculation;
164 class JVMCINMethodData;
165 #endif
166
167 class nmethod : public CodeBlob {
168 friend class VMStructs;
169 friend class JVMCIVMStructs;
170 friend class CodeCache; // scavengable oops
171 friend class JVMCINMethodData;
172 friend class DeoptimizationScope;
173
174 #define ImmutableDataRefCountSize ((int)sizeof(int))
175
176 private:
177
178 // Used to track in which deoptimize handshake this method will be deoptimized.
179 uint64_t _deoptimization_generation;
180
181 uint64_t _gc_epoch;
182
183 // Profiling counter used to figure out the hottest nmethods to record into CDS
184 volatile uint64_t _method_profiling_count;
185
186 Method* _method;
187
188 // To reduce header size union fields which usages do not overlap.
189 union {
190 // To support simple linked-list chaining of nmethods:
191 nmethod* _osr_link; // from InstanceKlass::osr_nmethods_head
192 struct {
193 // These are used for compiled synchronized native methods to
194 // locate the owner and stack slot for the BasicLock. They are
195 // needed because there is no debug information for compiled native
196 // wrappers and the oop maps are insufficient to allow
197 // frame::retrieve_receiver() to work. Currently they are expected
198 // to be byte offsets from the Java stack pointer for maximum code
199 // sharing between platforms. JVMTI's GetLocalInstance() uses these
200 // offsets to find the receiver for non-static native wrapper frames.
201 ByteSize _native_receiver_sp_offset;
202 ByteSize _native_basic_lock_sp_offset;
203 };
204 };
205
249
250 // Offset in immutable data section
251 // _dependencies_offset == 0
252 uint16_t _nul_chk_table_offset;
253 uint16_t _handler_table_offset; // This table could be big in C1 code
254 int _scopes_pcs_offset;
255 int _scopes_data_offset;
256 #if INCLUDE_JVMCI
257 int _speculations_offset;
258 #endif
259 int _immutable_data_ref_count_offset;
260
261 // location in frame (offset for sp) that deopt can store the original
262 // pc during a deopt.
263 int _orig_pc_offset;
264
265 int _compile_id; // which compilation made this nmethod
266 CompLevel _comp_level; // compilation level (s1)
267 CompilerType _compiler_type; // which compiler made this nmethod (u1)
268
269 AOTCodeEntry* _aot_code_entry;
270
271 bool _used; // has this nmethod ever been invoked?
272
273 // Local state used to keep track of whether unloading is happening or not
274 volatile uint8_t _is_unloading_state;
275
276 // Protected by NMethodState_lock
277 volatile signed char _state; // {not_installed, in_use, not_entrant}
278
279 // set during construction
280 uint8_t _has_unsafe_access:1, // May fault due to unsafe access.
281 _has_wide_vectors:1, // Preserve wide vectors at safepoints
282 _has_monitors:1, // Fastpath monitor detection for continuations
283 _has_scoped_access:1, // used by for shared scope closure (scopedMemoryAccess.cpp)
284 _has_flushed_dependencies:1, // Used for maintenance of dependencies (under CodeCache_lock)
285 _is_unlinked:1, // mark during class unloading
286 _load_reported:1, // used by jvmti to track if an event has been posted for this nmethod
287 _preloaded:1,
288 _has_clinit_barriers:1;
289
290 enum DeoptimizationStatus : u1 {
291 not_marked,
292 deoptimize,
293 deoptimize_noupdate,
294 deoptimize_done
295 };
296
297 volatile DeoptimizationStatus _deoptimization_status; // Used for stack deoptimization
298
299 DeoptimizationStatus deoptimization_status() const {
300 return AtomicAccess::load(&_deoptimization_status);
301 }
302
303 // Initialize fields to their default values
304 void init_defaults(CodeBuffer *code_buffer, CodeOffsets* offsets);
305
306 // Post initialization
307 void post_init();
308
467 // Attempt Unclaimed -> N|WR transition. Returns true if successful.
468 bool oops_do_try_claim_weak_request();
469
470 // Attempt Unclaimed -> N|SD transition. Returns the current link.
471 oops_do_mark_link* oops_do_try_claim_strong_done();
472 // Attempt N|WR -> X|WD transition. Returns nullptr if successful, X otherwise.
473 nmethod* oops_do_try_add_to_list_as_weak_done();
474
475 // Attempt X|WD -> N|SR transition. Returns the current link.
476 oops_do_mark_link* oops_do_try_add_strong_request(oops_do_mark_link* next);
477 // Attempt X|WD -> X|SD transition. Returns true if successful.
478 bool oops_do_try_claim_weak_done_as_strong_done(oops_do_mark_link* next);
479
480 // Do the N|SD -> X|SD transition.
481 void oops_do_add_to_list_as_strong_done();
482
483 // Sets this nmethod as strongly claimed (as part of N|SD -> X|SD and N|SR -> X|SD
484 // transitions).
485 void oops_do_set_strong_done(nmethod* old_head);
486
487 void record_nmethod_dependency();
488
489 nmethod* restore(address code_cache_buffer,
490 const methodHandle& method,
491 int compile_id,
492 address reloc_data,
493 GrowableArray<Handle>& oop_list,
494 GrowableArray<Metadata*>& metadata_list,
495 ImmutableOopMapSet* oop_maps,
496 address immutable_data,
497 GrowableArray<Handle>& reloc_imm_oop_list,
498 GrowableArray<Metadata*>& reloc_imm_metadata_list,
499 AOTCodeReader* aot_code_reader);
500
501 public:
502 // create nmethod using archived nmethod from AOT code cache
503 static nmethod* new_nmethod(nmethod* archived_nm,
504 const methodHandle& method,
505 AbstractCompiler* compiler,
506 int compile_id,
507 address reloc_data,
508 GrowableArray<Handle>& oop_list,
509 GrowableArray<Metadata*>& metadata_list,
510 ImmutableOopMapSet* oop_maps,
511 address immutable_data,
512 GrowableArray<Handle>& reloc_imm_oop_list,
513 GrowableArray<Metadata*>& reloc_imm_metadata_list,
514 AOTCodeReader* aot_code_reader);
515
516 // If you change anything in this enum please patch
517 // vmStructs_jvmci.cpp accordingly.
518 enum class InvalidationReason : s1 {
519 NOT_INVALIDATED = -1,
520 C1_CODEPATCH,
521 C1_DEOPTIMIZE,
522 C1_DEOPTIMIZE_FOR_PATCHING,
523 C1_PREDICATE_FAILED_TRAP,
524 CI_REPLAY,
525 UNLOADING,
526 UNLOADING_COLD,
527 JVMCI_INVALIDATE,
528 JVMCI_MATERIALIZE_VIRTUAL_OBJECT,
529 JVMCI_REPLACED_WITH_NEW_CODE,
530 JVMCI_REPROFILE,
531 MARKED_FOR_DEOPTIMIZATION,
532 MISSING_EXCEPTION_HANDLER,
533 NOT_USED,
534 OSR_INVALIDATION_BACK_BRANCH,
535 OSR_INVALIDATION_FOR_COMPILING_WITH_C1,
612 );
613
614 // Relocate the nmethod to the code heap identified by code_blob_type.
615 // Returns nullptr if the code heap does not have enough space, the
616 // nmethod is unrelocatable, or the nmethod is invalidated during relocation,
617 // otherwise the relocated nmethod. The original nmethod will be marked not entrant.
618 nmethod* relocate(CodeBlobType code_blob_type);
619
620 static nmethod* new_native_nmethod(const methodHandle& method,
621 int compile_id,
622 CodeBuffer *code_buffer,
623 int vep_offset,
624 int frame_complete,
625 int frame_size,
626 ByteSize receiver_sp_offset,
627 ByteSize basic_lock_sp_offset,
628 OopMapSet* oop_maps,
629 int exception_handler = -1);
630
631 Method* method () const { return _method; }
632 uint16_t entry_bci () const { return _entry_bci; }
633 bool is_native_method() const { return _method != nullptr && _method->is_native(); }
634 bool is_java_method () const { return _method != nullptr && !_method->is_native(); }
635 bool is_osr_method () const { return _entry_bci != InvocationEntryBci; }
636
637 int orig_pc_offset() { return _orig_pc_offset; }
638 bool is_relocatable();
639
640 // Compiler task identification. Note that all OSR methods
641 // are numbered in an independent sequence if CICountOSR is true,
642 // and native method wrappers are also numbered independently if
643 // CICountNative is true.
644 int compile_id() const { return _compile_id; }
645 int comp_level() const { return _comp_level; }
646 const char* compile_kind() const;
647
648 inline bool is_compiled_by_c1 () const { return _compiler_type == compiler_c1; }
649 inline bool is_compiled_by_c2 () const { return _compiler_type == compiler_c2; }
650 inline bool is_compiled_by_jvmci() const { return _compiler_type == compiler_jvmci; }
651 CompilerType compiler_type () const { return _compiler_type; }
652 const char* compiler_name () const;
653
654 // boundaries for different parts
655 address consts_begin () const { return content_begin(); }
656 address consts_end () const { return code_begin() ; }
657 address insts_begin () const { return code_begin() ; }
658 address insts_end () const { return header_begin() + _stub_offset ; }
659 address stub_begin () const { return header_begin() + _stub_offset ; }
660 address stub_end () const { return code_end() ; }
661 address exception_begin () const { return header_begin() + _exception_offset ; }
662 address deopt_handler_begin () const { return header_begin() + _deopt_handler_offset ; }
663 address unwind_handler_begin () const { return _unwind_handler_offset != -1 ? (insts_end() - _unwind_handler_offset) : nullptr; }
664 oop* oops_begin () const { return (oop*) data_begin(); }
665 oop* oops_end () const { return (oop*) data_end(); }
666
667 // mutable data
668 Metadata** metadata_begin () const { return (Metadata**) (mutable_data_begin() + _relocation_size); }
669 #if INCLUDE_JVMCI
670 Metadata** metadata_end () const { return (Metadata**) (mutable_data_begin() + _relocation_size + _metadata_size); }
671 address jvmci_data_begin () const { return mutable_data_begin() + _relocation_size + _metadata_size; }
672 address jvmci_data_end () const { return mutable_data_end(); }
673 #else
674 Metadata** metadata_end () const { return (Metadata**) mutable_data_end(); }
675 #endif
676
677 // immutable data
678 void set_immutable_data(address data) { _immutable_data = data; }
679 address immutable_data_begin () const { return _immutable_data; }
680 address immutable_data_end () const { return _immutable_data + _immutable_data_size ; }
681 address dependencies_begin () const { return _immutable_data; }
682 address dependencies_end () const { return _immutable_data + _nul_chk_table_offset; }
683 address nul_chk_table_begin () const { return _immutable_data + _nul_chk_table_offset; }
684 address nul_chk_table_end () const { return _immutable_data + _handler_table_offset; }
685 address handler_table_begin () const { return _immutable_data + _handler_table_offset; }
686 address handler_table_end () const { return _immutable_data + _scopes_pcs_offset ; }
687 PcDesc* scopes_pcs_begin () const { return (PcDesc*)(_immutable_data + _scopes_pcs_offset) ; }
688 PcDesc* scopes_pcs_end () const { return (PcDesc*)(_immutable_data + _scopes_data_offset) ; }
689 address scopes_data_begin () const { return _immutable_data + _scopes_data_offset ; }
690
691 #if INCLUDE_JVMCI
692 address scopes_data_end () const { return _immutable_data + _speculations_offset ; }
693 address speculations_begin () const { return _immutable_data + _speculations_offset ; }
694 address speculations_end () const { return _immutable_data + _immutable_data_ref_count_offset ; }
695 #else
696 address scopes_data_end () const { return _immutable_data + _immutable_data_ref_count_offset ; }
697 #endif
698 address immutable_data_ref_count_begin () const { return _immutable_data + _immutable_data_ref_count_offset ; }
739 address verified_entry_point() const { return code_begin() + _verified_entry_offset; } // if klass is correct
740
741 enum : signed char { not_installed = -1, // in construction, only the owner doing the construction is
742 // allowed to advance state
743 in_use = 0, // executable nmethod
744 not_entrant = 1 // marked for deoptimization but activations may still exist
745 };
746
747 // flag accessing and manipulation
748 bool is_not_installed() const { return _state == not_installed; }
749 bool is_in_use() const { return _state <= in_use; }
750 bool is_not_entrant() const { return _state == not_entrant; }
751 int get_state() const { return _state; }
752
753 void clear_unloading_state();
754 // Heuristically deduce an nmethod isn't worth keeping around
755 bool is_cold();
756 bool is_unloading();
757 void do_unloading(bool unloading_occurred);
758
759 void inc_method_profiling_count();
760 uint64_t method_profiling_count();
761
762 bool make_in_use() {
763 return try_transition(in_use);
764 }
765 // Make the nmethod non entrant. The nmethod will continue to be
766 // alive. It is used when an uncommon trap happens. Returns true
767 // if this thread changed the state of the nmethod or false if
768 // another thread performed the transition.
769 bool make_not_entrant(InvalidationReason invalidation_reason, bool keep_aot_entry = false);
770 bool make_not_used() { return make_not_entrant(InvalidationReason::NOT_USED, true /* keep AOT entry */); }
771
772 bool is_marked_for_deoptimization() const { return deoptimization_status() != not_marked; }
773 bool has_been_deoptimized() const { return deoptimization_status() == deoptimize_done; }
774 void set_deoptimized_done();
775
776 bool update_recompile_counts() const {
777 // Update recompile counts when either the update is explicitly requested (deoptimize)
778 // or the nmethod is not marked for deoptimization at all (not_marked).
779 // The latter happens during uncommon traps when deoptimized nmethod is made not entrant.
780 DeoptimizationStatus status = deoptimization_status();
781 return status != deoptimize_noupdate && status != deoptimize_done;
782 }
783
784 // tells whether frames described by this nmethod can be deoptimized
785 // note: native wrappers cannot be deoptimized.
786 bool can_be_deoptimized() const { return is_java_method(); }
787
788 bool has_dependencies() { return dependencies_size() != 0; }
789 void print_dependencies_on(outputStream* out) PRODUCT_RETURN;
790 void flush_dependencies();
791
792 template<typename T>
793 T* gc_data() const { return reinterpret_cast<T*>(_gc_data); }
794 template<typename T>
795 void set_gc_data(T* gc_data) { _gc_data = reinterpret_cast<void*>(gc_data); }
796
797 bool has_unsafe_access() const { return _has_unsafe_access; }
798 void set_has_unsafe_access(bool z) { _has_unsafe_access = z; }
799
800 bool has_monitors() const { return _has_monitors; }
801 void set_has_monitors(bool z) { _has_monitors = z; }
802
803 bool has_scoped_access() const { return _has_scoped_access; }
804 void set_has_scoped_access(bool z) { _has_scoped_access = z; }
805
806 bool has_wide_vectors() const { return _has_wide_vectors; }
807 void set_has_wide_vectors(bool z) { _has_wide_vectors = z; }
808
809 bool has_clinit_barriers() const { return _has_clinit_barriers; }
810 void set_has_clinit_barriers(bool z) { _has_clinit_barriers = z; }
811
812 bool preloaded() const { return _preloaded; }
813 void set_preloaded(bool z) { _preloaded = z; }
814
815 bool has_flushed_dependencies() const { return _has_flushed_dependencies; }
816 void set_has_flushed_dependencies(bool z) {
817 assert(!has_flushed_dependencies(), "should only happen once");
818 _has_flushed_dependencies = z;
819 }
820
821 bool is_unlinked() const { return _is_unlinked; }
822 void set_is_unlinked() {
823 assert(!_is_unlinked, "already unlinked");
824 _is_unlinked = true;
825 }
826
827 bool used() const { return _used; }
828 void set_used() { _used = true; }
829
830 bool is_aot() const { return _aot_code_entry != nullptr; }
831 void set_aot_code_entry(AOTCodeEntry* entry) { _aot_code_entry = entry; }
832 AOTCodeEntry* aot_code_entry() const { return _aot_code_entry; }
833
834 // Support for oops in scopes and relocs:
835 // Note: index 0 is reserved for null.
836 oop oop_at(int index) const;
837 oop oop_at_phantom(int index) const; // phantom reference
838 oop* oop_addr_at(int index) const { // for GC
839 // relocation indexes are biased by 1 (because 0 is reserved)
840 assert(index > 0 && index <= oops_count(), "must be a valid non-zero index");
841 return &oops_begin()[index - 1];
842 }
843
844 // Support for meta data in scopes and relocs:
845 // Note: index 0 is reserved for null.
846 Metadata* metadata_at(int index) const { return index == 0 ? nullptr: *metadata_addr_at(index); }
847 Metadata** metadata_addr_at(int index) const { // for GC
848 // relocation indexes are biased by 1 (because 0 is reserved)
849 assert(index > 0 && index <= metadata_count(), "must be a valid non-zero index");
850 return &metadata_begin()[index - 1];
851 }
852
853 void copy_values(GrowableArray<Handle>* array);
854 void copy_values(GrowableArray<jobject>* oops);
855 void copy_values(GrowableArray<Metadata*>* metadata);
856 void copy_values(GrowableArray<address>* metadata) {} // Nothing to do
857
858 // Relocation support
859 private:
860 void fix_oop_relocations(address begin, address end, bool initialize_immediates);
861 inline void initialize_immediate_oop(oop* dest, jobject handle);
862
863 protected:
864 address oops_reloc_begin() const;
865
866 public:
867 void fix_oop_relocations(address begin, address end) { fix_oop_relocations(begin, end, false); }
868 void fix_oop_relocations() { fix_oop_relocations(nullptr, nullptr, false); }
869
870 void create_reloc_immediates_list(JavaThread* thread, GrowableArray<Handle>& oop_list, GrowableArray<Metadata*>& metadata_list);
871
872 bool is_at_poll_return(address pc);
873 bool is_at_poll_or_poll_return(address pc);
874
875 protected:
876 // Exception cache support
877 // Note: _exception_cache may be read and cleaned concurrently.
878 ExceptionCache* exception_cache() const { return _exception_cache; }
879 ExceptionCache* exception_cache_acquire() const;
880
881 public:
882 address handler_for_exception_and_pc(Handle exception, address pc);
883 void add_handler_for_exception_and_pc(Handle exception, address pc, address handler);
884 void clean_exception_cache();
885
886 void add_exception_cache_entry(ExceptionCache* new_entry);
887 ExceptionCache* exception_cache_entry_for_exception(Handle exception);
888
889
890 // Deopt
891 // Return true is the PC is one would expect if the frame is being deopted.
1041 assert(*ref_count > 0, "Must be positive");
1042 return --(*ref_count);
1043 }
1044
1045 static void add_delayed_compiled_method_load_event(nmethod* nm) NOT_CDS_RETURN;
1046
1047 public:
1048 // ScopeDesc retrieval operation
1049 PcDesc* pc_desc_at(address pc) { return find_pc_desc(pc, false); }
1050 // pc_desc_near returns the first PcDesc at or after the given pc.
1051 PcDesc* pc_desc_near(address pc) { return find_pc_desc(pc, true); }
1052
1053 // ScopeDesc for an instruction
1054 ScopeDesc* scope_desc_at(address pc);
1055 ScopeDesc* scope_desc_near(address pc);
1056
1057 // copying of debugging information
1058 void copy_scopes_pcs(PcDesc* pcs, int count);
1059 void copy_scopes_data(address buffer, int size);
1060
1061 // Post successful compilation
1062 void post_compiled_method(CompileTask* task);
1063
1064 // jvmti support:
1065 void post_compiled_method_load_event(JvmtiThreadState* state = nullptr);
1066
1067 // verify operations
1068 void verify();
1069 void verify_scopes();
1070 void verify_interrupt_point(address interrupt_point, bool is_inline_cache);
1071
1072 // Disassemble this nmethod with additional debug information, e.g. information about blocks.
1073 void decode2(outputStream* st) const;
1074 void print_constant_pool(outputStream* st);
1075
1076 // Avoid hiding of parent's 'decode(outputStream*)' method.
1077 void decode(outputStream* st) const { decode2(st); } // just delegate here.
1078
1079 // AOT cache support
1080 static void post_delayed_compiled_method_load_events() NOT_CDS_RETURN;
1081
1082 // printing support
1083 void print_on_impl(outputStream* st) const;
1084 void print_code();
1085 void print_value_on_impl(outputStream* st) const;
1086 void print_code_snippet(outputStream* st, address addr) const;
1087
1088 #if defined(SUPPORT_DATA_STRUCTS)
1089 // print output in opt build for disassembler library
1090 void print_relocations_on(outputStream* st) PRODUCT_RETURN;
1091 void print_pcs_on(outputStream* st);
1092 void print_scopes() { print_scopes_on(tty); }
1093 void print_scopes_on(outputStream* st) PRODUCT_RETURN;
1094 void print_handler_table();
1095 void print_nul_chk_table();
1096 void print_recorded_oop(int log_n, int index);
1097 void print_recorded_oops();
1098 void print_recorded_metadata();
1099
1100 void print_oops(outputStream* st); // oops from the underlying CodeBlob.
1101 void print_metadata(outputStream* st); // metadata in metadata pool.
1102 #else
1103 void print_pcs_on(outputStream* st) { return; }
1104 #endif
1105
1106 void print_calls(outputStream* st) PRODUCT_RETURN;
1107 static void print_statistics() PRODUCT_RETURN;
1108
1109 void maybe_print_nmethod(const DirectiveSet* directive);
1110 void print_nmethod(bool print_code);
1139 ByteSize native_receiver_sp_offset() {
1140 assert(is_native_method(), "sanity");
1141 return _native_receiver_sp_offset;
1142 }
1143 ByteSize native_basic_lock_sp_offset() {
1144 assert(is_native_method(), "sanity");
1145 return _native_basic_lock_sp_offset;
1146 }
1147
1148 // support for code generation
1149 static ByteSize osr_entry_point_offset() { return byte_offset_of(nmethod, _osr_entry_point); }
1150 static ByteSize state_offset() { return byte_offset_of(nmethod, _state); }
1151
1152 void metadata_do(MetadataClosure* f);
1153
1154 address call_instruction_address(address pc) const;
1155
1156 void make_deoptimized();
1157 void finalize_relocations();
1158
1159 void prepare_for_archiving_impl();
1160
1161 class Vptr : public CodeBlob::Vptr {
1162 void print_on(const CodeBlob* instance, outputStream* st) const override {
1163 ttyLocker ttyl;
1164 instance->as_nmethod()->print_on_impl(st);
1165 }
1166 void print_value_on(const CodeBlob* instance, outputStream* st) const override {
1167 instance->as_nmethod()->print_value_on_impl(st);
1168 }
1169 void prepare_for_archiving(CodeBlob* instance) const override {
1170 ((nmethod*)instance)->prepare_for_archiving_impl();
1171 };
1172 };
1173
1174 static const Vptr _vpntr;
1175 };
1176
1177 struct NMethodMarkingScope : StackObj {
1178 NMethodMarkingScope() {
1179 nmethod::oops_do_marking_prologue();
1180 }
1181 ~NMethodMarkingScope() {
1182 nmethod::oops_do_marking_epilogue();
1183 }
1184 };
1185
1186 #endif // SHARE_CODE_NMETHOD_HPP
|