131
132 void ProfileData::print_data_on(outputStream* st, const MethodData* md) const {
133 print_data_on(st, print_data_on_helper(md));
134 }
135
136 void ProfileData::print_shared(outputStream* st, const char* name, const char* extra) const {
137 st->print("bci: %d ", bci());
138 st->fill_to(tab_width_one + 1);
139 st->print("%s", name);
140 tab(st);
141 int trap = trap_state();
142 if (trap != 0) {
143 char buf[100];
144 st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
145 }
146 if (extra != nullptr) {
147 st->print("%s", extra);
148 }
149 int flags = data()->flags();
150 if (flags != 0) {
151 st->print("flags(%d) ", flags);
152 }
153 }
154
155 void ProfileData::tab(outputStream* st, bool first) const {
156 st->fill_to(first ? tab_width_one : tab_width_two);
157 }
158
159 // ==================================================================
160 // BitData
161 //
162 // A BitData corresponds to a one-bit flag. This is used to indicate
163 // whether a checkcast bytecode has seen a null value.
164
165
166 void BitData::print_data_on(outputStream* st, const char* extra) const {
167 print_shared(st, "BitData", extra);
168 st->cr();
169 }
170
171 // ==================================================================
201 set_displacement(offset);
202 }
203
204 void JumpData::print_data_on(outputStream* st, const char* extra) const {
205 print_shared(st, "JumpData", extra);
206 st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
207 }
208
209 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) {
210 // Parameter profiling include the receiver
211 int args_count = include_receiver ? 1 : 0;
212 ResourceMark rm;
213 ReferenceArgumentCount rac(signature);
214 args_count += rac.count();
215 args_count = MIN2(args_count, max);
216 return args_count * per_arg_cell_count;
217 }
218
219 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
220 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
221 assert(TypeStackSlotEntries::per_arg_count() > ReturnTypeEntry::static_cell_count(), "code to test for arguments/results broken");
222 const methodHandle m = stream->method();
223 int bci = stream->bci();
224 Bytecode_invoke inv(m, bci);
225 int args_cell = 0;
226 if (MethodData::profile_arguments_for_invoke(m, bci)) {
227 args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit);
228 }
229 int ret_cell = 0;
230 if (MethodData::profile_return_for_invoke(m, bci) && is_reference_type(inv.result_type())) {
231 ret_cell = ReturnTypeEntry::static_cell_count();
232 }
233 int header_cell = 0;
234 if (args_cell + ret_cell > 0) {
235 header_cell = header_cell_count();
236 }
237
238 return header_cell + args_cell + ret_cell;
239 }
240
241 class ArgumentOffsetComputer : public SignatureIterator {
242 private:
243 int _max;
244 int _offset;
245 GrowableArray<int> _offsets;
246
247 friend class SignatureIterator; // so do_parameters_on can call do_type
248 void do_type(BasicType type) {
249 if (is_reference_type(type) && _offsets.length() < _max) {
250 _offsets.push(_offset);
251 }
346 intptr_t p = type(i);
347 Klass* k = (Klass*)klass_part(p);
348 if (k != nullptr) {
349 if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
350 continue; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
351 }
352 if (always_clean || !k->is_loader_present_and_alive() || is_excluded(k)) {
353 set_type(i, with_status((Klass*)nullptr, p));
354 }
355 }
356 }
357 }
358
359 void TypeStackSlotEntries::metaspace_pointers_do(MetaspaceClosure* it) {
360 for (int i = 0; i < _number_of_entries; i++) {
361 Klass** k = (Klass**)type_adr(i); // tagged
362 it->push(k);
363 }
364 }
365
366 void ReturnTypeEntry::clean_weak_klass_links(bool always_clean) {
367 intptr_t p = type();
368 Klass* k = (Klass*)klass_part(p);
369 if (k != nullptr) {
370 if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
371 return; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
372 }
373 if (always_clean || !k->is_loader_present_and_alive() || is_excluded(k)) {
374 set_type(with_status((Klass*)nullptr, p));
375 }
376 }
377 }
378
379 void ReturnTypeEntry::metaspace_pointers_do(MetaspaceClosure* it) {
380 Klass** k = (Klass**)type_adr(); // tagged
381 it->push(k);
382 }
383
384 bool TypeEntriesAtCall::return_profiling_enabled() {
385 return MethodData::profile_return();
386 }
387
388 bool TypeEntriesAtCall::arguments_profiling_enabled() {
389 return MethodData::profile_arguments();
390 }
391
392 void TypeEntries::print_klass(outputStream* st, intptr_t k) {
393 if (is_type_none(k)) {
394 st->print("none");
395 } else if (is_type_unknown(k)) {
396 st->print("unknown");
397 } else {
398 valid_klass(k)->print_value_on(st);
399 }
400 if (was_null_seen(k)) {
401 st->print(" (null seen)");
402 }
403 }
404
405 void TypeStackSlotEntries::print_data_on(outputStream* st) const {
406 for (int i = 0; i < _number_of_entries; i++) {
407 _pd->tab(st);
408 st->print("%d: stack(%u) ", i, stack_slot(i));
409 print_klass(st, type(i));
410 st->cr();
411 }
412 }
413
414 void ReturnTypeEntry::print_data_on(outputStream* st) const {
415 _pd->tab(st);
416 print_klass(st, type());
417 st->cr();
418 }
419
420 void CallTypeData::print_data_on(outputStream* st, const char* extra) const {
421 CounterData::print_data_on(st, extra);
422 if (has_arguments()) {
423 tab(st, true);
424 st->print("argument types");
425 _args.print_data_on(st);
426 }
427 if (has_return()) {
428 tab(st, true);
429 st->print("return type");
430 _ret.print_data_on(st);
431 }
432 }
433
434 void VirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
567 }
568
569 // ==================================================================
570 // BranchData
571 //
572 // A BranchData is used to access profiling data for a two-way branch.
573 // It consists of taken and not_taken counts as well as a data displacement
574 // for the taken case.
575
576 void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
577 assert(stream->bci() == bci(), "wrong pos");
578 int target = stream->dest();
579 int my_di = mdo->dp_to_di(dp());
580 int target_di = mdo->bci_to_di(target);
581 int offset = target_di - my_di;
582 set_displacement(offset);
583 }
584
585 void BranchData::print_data_on(outputStream* st, const char* extra) const {
586 print_shared(st, "BranchData", extra);
587 st->print_cr("taken(%u) displacement(%d)",
588 taken(), displacement());
589 tab(st);
590 st->print_cr("not taken(%u)", not_taken());
591 }
592
593 // ==================================================================
594 // MultiBranchData
595 //
596 // A MultiBranchData is used to access profiling information for
597 // a multi-way branch (*switch bytecodes). It consists of a series
598 // of (count, displacement) pairs, which count the number of times each
599 // case was taken and specify the data displacement for each branch target.
600
601 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
602 int cell_count = 0;
603 if (stream->code() == Bytecodes::_tableswitch) {
604 Bytecode_tableswitch sw(stream->method()(), stream->bcp());
605 cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
606 } else {
697
698 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
699 print_shared(st, "ParametersTypeData", extra);
700 tab(st);
701 _parameters.print_data_on(st);
702 st->cr();
703 }
704
705 void SpeculativeTrapData::metaspace_pointers_do(MetaspaceClosure* it) {
706 Method** m = (Method**)intptr_at_adr(speculative_trap_method);
707 it->push(m);
708 }
709
710 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
711 print_shared(st, "SpeculativeTrapData", extra);
712 tab(st);
713 method()->print_short_name(st);
714 st->cr();
715 }
716
717 // ==================================================================
718 // MethodData*
719 //
720 // A MethodData* holds information which has been collected about
721 // a method.
722
723 MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) {
724 assert(!THREAD->owns_locks(), "Should not own any locks");
725 int size = MethodData::compute_allocation_size_in_words(method);
726
727 return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
728 MethodData(method);
729 }
730
731 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
732 switch (code) {
733 case Bytecodes::_checkcast:
734 case Bytecodes::_instanceof:
735 case Bytecodes::_aastore:
736 if (TypeProfileCasts) {
737 return ReceiverTypeData::static_cell_count();
738 } else {
739 return BitData::static_cell_count();
740 }
741 case Bytecodes::_invokespecial:
742 case Bytecodes::_invokestatic:
743 if (MethodData::profile_arguments() || MethodData::profile_return()) {
744 return variable_cell_count;
745 } else {
746 return CounterData::static_cell_count();
747 }
748 case Bytecodes::_goto:
749 case Bytecodes::_goto_w:
750 case Bytecodes::_jsr:
751 case Bytecodes::_jsr_w:
752 return JumpData::static_cell_count();
753 case Bytecodes::_invokevirtual:
754 case Bytecodes::_invokeinterface:
755 if (MethodData::profile_arguments() || MethodData::profile_return()) {
756 return variable_cell_count;
757 } else {
758 return VirtualCallData::static_cell_count();
759 }
760 case Bytecodes::_invokedynamic:
761 if (MethodData::profile_arguments() || MethodData::profile_return()) {
762 return variable_cell_count;
763 } else {
764 return CounterData::static_cell_count();
765 }
766 case Bytecodes::_ret:
767 return RetData::static_cell_count();
768 case Bytecodes::_ifeq:
769 case Bytecodes::_ifne:
770 case Bytecodes::_iflt:
771 case Bytecodes::_ifge:
772 case Bytecodes::_ifgt:
773 case Bytecodes::_ifle:
774 case Bytecodes::_if_icmpeq:
775 case Bytecodes::_if_icmpne:
776 case Bytecodes::_if_icmplt:
777 case Bytecodes::_if_icmpge:
778 case Bytecodes::_if_icmpgt:
779 case Bytecodes::_if_icmple:
780 case Bytecodes::_if_acmpeq:
781 case Bytecodes::_if_acmpne:
782 case Bytecodes::_ifnull:
783 case Bytecodes::_ifnonnull:
784 return BranchData::static_cell_count();
785 case Bytecodes::_lookupswitch:
786 case Bytecodes::_tableswitch:
787 return variable_cell_count;
788 default:
789 return no_profile_data;
790 }
791 }
792
793 // Compute the size of the profiling information corresponding to
794 // the current bytecode.
795 int MethodData::compute_data_size(BytecodeStream* stream) {
796 int cell_count = bytecode_cell_count(stream->code());
797 if (cell_count == no_profile_data) {
798 return 0;
799 }
800 if (cell_count == variable_cell_count) {
801 switch (stream->code()) {
802 case Bytecodes::_lookupswitch:
803 case Bytecodes::_tableswitch:
804 cell_count = MultiBranchData::compute_cell_count(stream);
823 } else {
824 cell_count = VirtualCallData::static_cell_count();
825 }
826 break;
827 }
828 default:
829 fatal("unexpected bytecode for var length profile data");
830 }
831 }
832 // Note: cell_count might be zero, meaning that there is just
833 // a DataLayout header, with no extra cells.
834 assert(cell_count >= 0, "sanity");
835 return DataLayout::compute_size_in_bytes(cell_count);
836 }
837
838 bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) {
839 // Bytecodes for which we may use speculation
840 switch (code) {
841 case Bytecodes::_checkcast:
842 case Bytecodes::_instanceof:
843 case Bytecodes::_aastore:
844 case Bytecodes::_invokevirtual:
845 case Bytecodes::_invokeinterface:
846 case Bytecodes::_if_acmpeq:
847 case Bytecodes::_if_acmpne:
848 case Bytecodes::_ifnull:
849 case Bytecodes::_ifnonnull:
850 case Bytecodes::_invokestatic:
851 #ifdef COMPILER2
852 if (CompilerConfig::is_c2_enabled()) {
853 return UseTypeSpeculation;
854 }
855 #endif
856 default:
857 return false;
858 }
859 return false;
860 }
861
862 #if INCLUDE_JVMCI
1039
1040 // Compute the size of the MethodData* necessary to store
1041 // profiling information about a given method. Size is in words
1042 int MethodData::compute_allocation_size_in_words(const methodHandle& method) {
1043 int byte_size = compute_allocation_size_in_bytes(method);
1044 int word_size = align_up(byte_size, BytesPerWord) / BytesPerWord;
1045 return align_metadata_size(word_size);
1046 }
1047
1048 // Initialize an individual data segment. Returns the size of
1049 // the segment in bytes.
1050 int MethodData::initialize_data(BytecodeStream* stream,
1051 int data_index) {
1052 int cell_count = -1;
1053 u1 tag = DataLayout::no_tag;
1054 DataLayout* data_layout = data_layout_at(data_index);
1055 Bytecodes::Code c = stream->code();
1056 switch (c) {
1057 case Bytecodes::_checkcast:
1058 case Bytecodes::_instanceof:
1059 case Bytecodes::_aastore:
1060 if (TypeProfileCasts) {
1061 cell_count = ReceiverTypeData::static_cell_count();
1062 tag = DataLayout::receiver_type_data_tag;
1063 } else {
1064 cell_count = BitData::static_cell_count();
1065 tag = DataLayout::bit_data_tag;
1066 }
1067 break;
1068 case Bytecodes::_invokespecial:
1069 case Bytecodes::_invokestatic: {
1070 int counter_data_cell_count = CounterData::static_cell_count();
1071 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1072 profile_return_for_invoke(stream->method(), stream->bci())) {
1073 cell_count = CallTypeData::compute_cell_count(stream);
1074 } else {
1075 cell_count = counter_data_cell_count;
1076 }
1077 if (cell_count > counter_data_cell_count) {
1078 tag = DataLayout::call_type_data_tag;
1079 } else {
1080 tag = DataLayout::counter_data_tag;
1081 }
1082 break;
1083 }
1084 case Bytecodes::_goto:
1085 case Bytecodes::_goto_w:
1086 case Bytecodes::_jsr:
1087 case Bytecodes::_jsr_w:
1119 tag = DataLayout::counter_data_tag;
1120 }
1121 break;
1122 }
1123 case Bytecodes::_ret:
1124 cell_count = RetData::static_cell_count();
1125 tag = DataLayout::ret_data_tag;
1126 break;
1127 case Bytecodes::_ifeq:
1128 case Bytecodes::_ifne:
1129 case Bytecodes::_iflt:
1130 case Bytecodes::_ifge:
1131 case Bytecodes::_ifgt:
1132 case Bytecodes::_ifle:
1133 case Bytecodes::_if_icmpeq:
1134 case Bytecodes::_if_icmpne:
1135 case Bytecodes::_if_icmplt:
1136 case Bytecodes::_if_icmpge:
1137 case Bytecodes::_if_icmpgt:
1138 case Bytecodes::_if_icmple:
1139 case Bytecodes::_if_acmpeq:
1140 case Bytecodes::_if_acmpne:
1141 case Bytecodes::_ifnull:
1142 case Bytecodes::_ifnonnull:
1143 cell_count = BranchData::static_cell_count();
1144 tag = DataLayout::branch_data_tag;
1145 break;
1146 case Bytecodes::_lookupswitch:
1147 case Bytecodes::_tableswitch:
1148 cell_count = MultiBranchData::compute_cell_count(stream);
1149 tag = DataLayout::multi_branch_data_tag;
1150 break;
1151 default:
1152 break;
1153 }
1154 assert(tag == DataLayout::multi_branch_data_tag ||
1155 ((MethodData::profile_arguments() || MethodData::profile_return()) &&
1156 (tag == DataLayout::call_type_data_tag ||
1157 tag == DataLayout::counter_data_tag ||
1158 tag == DataLayout::virtual_call_type_data_tag ||
1159 tag == DataLayout::virtual_call_data_tag)) ||
1160 cell_count == bytecode_cell_count(c), "cell counts must agree");
1161 if (cell_count >= 0) {
1162 assert(tag != DataLayout::no_tag, "bad tag");
1163 assert(bytecode_has_profile(c), "agree w/ BHP");
1164 data_layout->initialize(tag, checked_cast<u2>(stream->bci()), cell_count);
1165 return DataLayout::compute_size_in_bytes(cell_count);
1193 case DataLayout::receiver_type_data_tag:
1194 return ReceiverTypeData::static_cell_count();
1195 case DataLayout::virtual_call_data_tag:
1196 return VirtualCallData::static_cell_count();
1197 case DataLayout::ret_data_tag:
1198 return RetData::static_cell_count();
1199 case DataLayout::branch_data_tag:
1200 return BranchData::static_cell_count();
1201 case DataLayout::multi_branch_data_tag:
1202 return ((new MultiBranchData(this))->cell_count());
1203 case DataLayout::arg_info_data_tag:
1204 return ((new ArgInfoData(this))->cell_count());
1205 case DataLayout::call_type_data_tag:
1206 return ((new CallTypeData(this))->cell_count());
1207 case DataLayout::virtual_call_type_data_tag:
1208 return ((new VirtualCallTypeData(this))->cell_count());
1209 case DataLayout::parameters_type_data_tag:
1210 return ((new ParametersTypeData(this))->cell_count());
1211 case DataLayout::speculative_trap_data_tag:
1212 return SpeculativeTrapData::static_cell_count();
1213 }
1214 }
1215 ProfileData* DataLayout::data_in() {
1216 switch (tag()) {
1217 case DataLayout::no_tag:
1218 default:
1219 ShouldNotReachHere();
1220 return nullptr;
1221 case DataLayout::bit_data_tag:
1222 return new BitData(this);
1223 case DataLayout::counter_data_tag:
1224 return new CounterData(this);
1225 case DataLayout::jump_data_tag:
1226 return new JumpData(this);
1227 case DataLayout::receiver_type_data_tag:
1228 return new ReceiverTypeData(this);
1229 case DataLayout::virtual_call_data_tag:
1230 return new VirtualCallData(this);
1231 case DataLayout::ret_data_tag:
1232 return new RetData(this);
1233 case DataLayout::branch_data_tag:
1234 return new BranchData(this);
1235 case DataLayout::multi_branch_data_tag:
1236 return new MultiBranchData(this);
1237 case DataLayout::arg_info_data_tag:
1238 return new ArgInfoData(this);
1239 case DataLayout::call_type_data_tag:
1240 return new CallTypeData(this);
1241 case DataLayout::virtual_call_type_data_tag:
1242 return new VirtualCallTypeData(this);
1243 case DataLayout::parameters_type_data_tag:
1244 return new ParametersTypeData(this);
1245 case DataLayout::speculative_trap_data_tag:
1246 return new SpeculativeTrapData(this);
1247 }
1248 }
1249
1250 // Iteration over data.
1251 ProfileData* MethodData::next_data(ProfileData* current) const {
1252 int current_index = dp_to_di(current->dp());
1253 int next_index = current_index + current->size_in_bytes();
1254 ProfileData* next = data_at(next_index);
1255 return next;
1256 }
1257
1258 DataLayout* MethodData::next_data_layout(DataLayout* current) const {
1259 int current_index = dp_to_di((address)current);
1260 int next_index = current_index + current->size_in_bytes();
1261 if (out_of_bounds(next_index)) {
1262 return nullptr;
1263 }
1264 DataLayout* next = data_layout_at(next_index);
1265 return next;
1266 }
|
131
132 void ProfileData::print_data_on(outputStream* st, const MethodData* md) const {
133 print_data_on(st, print_data_on_helper(md));
134 }
135
136 void ProfileData::print_shared(outputStream* st, const char* name, const char* extra) const {
137 st->print("bci: %d ", bci());
138 st->fill_to(tab_width_one + 1);
139 st->print("%s", name);
140 tab(st);
141 int trap = trap_state();
142 if (trap != 0) {
143 char buf[100];
144 st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
145 }
146 if (extra != nullptr) {
147 st->print("%s", extra);
148 }
149 int flags = data()->flags();
150 if (flags != 0) {
151 st->print("flags(%d) %p/%d", flags, data(), in_bytes(DataLayout::flags_offset()));
152 }
153 }
154
155 void ProfileData::tab(outputStream* st, bool first) const {
156 st->fill_to(first ? tab_width_one : tab_width_two);
157 }
158
159 // ==================================================================
160 // BitData
161 //
162 // A BitData corresponds to a one-bit flag. This is used to indicate
163 // whether a checkcast bytecode has seen a null value.
164
165
166 void BitData::print_data_on(outputStream* st, const char* extra) const {
167 print_shared(st, "BitData", extra);
168 st->cr();
169 }
170
171 // ==================================================================
201 set_displacement(offset);
202 }
203
204 void JumpData::print_data_on(outputStream* st, const char* extra) const {
205 print_shared(st, "JumpData", extra);
206 st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
207 }
208
209 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) {
210 // Parameter profiling include the receiver
211 int args_count = include_receiver ? 1 : 0;
212 ResourceMark rm;
213 ReferenceArgumentCount rac(signature);
214 args_count += rac.count();
215 args_count = MIN2(args_count, max);
216 return args_count * per_arg_cell_count;
217 }
218
219 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
220 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
221 assert(TypeStackSlotEntries::per_arg_count() > SingleTypeEntry::static_cell_count(), "code to test for arguments/results broken");
222 const methodHandle m = stream->method();
223 int bci = stream->bci();
224 Bytecode_invoke inv(m, bci);
225 int args_cell = 0;
226 if (MethodData::profile_arguments_for_invoke(m, bci)) {
227 args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit);
228 }
229 int ret_cell = 0;
230 if (MethodData::profile_return_for_invoke(m, bci) && is_reference_type(inv.result_type())) {
231 ret_cell = SingleTypeEntry::static_cell_count();
232 }
233 int header_cell = 0;
234 if (args_cell + ret_cell > 0) {
235 header_cell = header_cell_count();
236 }
237
238 return header_cell + args_cell + ret_cell;
239 }
240
241 class ArgumentOffsetComputer : public SignatureIterator {
242 private:
243 int _max;
244 int _offset;
245 GrowableArray<int> _offsets;
246
247 friend class SignatureIterator; // so do_parameters_on can call do_type
248 void do_type(BasicType type) {
249 if (is_reference_type(type) && _offsets.length() < _max) {
250 _offsets.push(_offset);
251 }
346 intptr_t p = type(i);
347 Klass* k = (Klass*)klass_part(p);
348 if (k != nullptr) {
349 if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
350 continue; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
351 }
352 if (always_clean || !k->is_loader_present_and_alive() || is_excluded(k)) {
353 set_type(i, with_status((Klass*)nullptr, p));
354 }
355 }
356 }
357 }
358
359 void TypeStackSlotEntries::metaspace_pointers_do(MetaspaceClosure* it) {
360 for (int i = 0; i < _number_of_entries; i++) {
361 Klass** k = (Klass**)type_adr(i); // tagged
362 it->push(k);
363 }
364 }
365
366 void SingleTypeEntry::clean_weak_klass_links(bool always_clean) {
367 intptr_t p = type();
368 Klass* k = (Klass*)klass_part(p);
369 if (k != nullptr) {
370 if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
371 return; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
372 }
373 if (always_clean || !k->is_loader_present_and_alive() || is_excluded(k)) {
374 set_type(with_status((Klass*)nullptr, p));
375 }
376 }
377 }
378
379 void SingleTypeEntry::metaspace_pointers_do(MetaspaceClosure* it) {
380 Klass** k = (Klass**)type_adr(); // tagged
381 it->push(k);
382 }
383
384 bool TypeEntriesAtCall::return_profiling_enabled() {
385 return MethodData::profile_return();
386 }
387
388 bool TypeEntriesAtCall::arguments_profiling_enabled() {
389 return MethodData::profile_arguments();
390 }
391
392 void TypeEntries::print_klass(outputStream* st, intptr_t k) {
393 if (is_type_none(k)) {
394 st->print("none");
395 } else if (is_type_unknown(k)) {
396 st->print("unknown");
397 } else {
398 valid_klass(k)->print_value_on(st);
399 }
400 if (was_null_seen(k)) {
401 st->print(" (null seen)");
402 }
403 }
404
405 void TypeStackSlotEntries::print_data_on(outputStream* st) const {
406 for (int i = 0; i < _number_of_entries; i++) {
407 _pd->tab(st);
408 st->print("%d: stack(%u) ", i, stack_slot(i));
409 print_klass(st, type(i));
410 st->cr();
411 }
412 }
413
414 void SingleTypeEntry::print_data_on(outputStream* st) const {
415 _pd->tab(st);
416 print_klass(st, type());
417 st->cr();
418 }
419
420 void CallTypeData::print_data_on(outputStream* st, const char* extra) const {
421 CounterData::print_data_on(st, extra);
422 if (has_arguments()) {
423 tab(st, true);
424 st->print("argument types");
425 _args.print_data_on(st);
426 }
427 if (has_return()) {
428 tab(st, true);
429 st->print("return type");
430 _ret.print_data_on(st);
431 }
432 }
433
434 void VirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
567 }
568
569 // ==================================================================
570 // BranchData
571 //
572 // A BranchData is used to access profiling data for a two-way branch.
573 // It consists of taken and not_taken counts as well as a data displacement
574 // for the taken case.
575
576 void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
577 assert(stream->bci() == bci(), "wrong pos");
578 int target = stream->dest();
579 int my_di = mdo->dp_to_di(dp());
580 int target_di = mdo->bci_to_di(target);
581 int offset = target_di - my_di;
582 set_displacement(offset);
583 }
584
585 void BranchData::print_data_on(outputStream* st, const char* extra) const {
586 print_shared(st, "BranchData", extra);
587 if (data()->flags()) {
588 st->cr();
589 tab(st);
590 }
591 st->print_cr("taken(%u) displacement(%d)",
592 taken(), displacement());
593 tab(st);
594 st->print_cr("not taken(%u)", not_taken());
595 }
596
597 // ==================================================================
598 // MultiBranchData
599 //
600 // A MultiBranchData is used to access profiling information for
601 // a multi-way branch (*switch bytecodes). It consists of a series
602 // of (count, displacement) pairs, which count the number of times each
603 // case was taken and specify the data displacement for each branch target.
604
605 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
606 int cell_count = 0;
607 if (stream->code() == Bytecodes::_tableswitch) {
608 Bytecode_tableswitch sw(stream->method()(), stream->bcp());
609 cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
610 } else {
701
702 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
703 print_shared(st, "ParametersTypeData", extra);
704 tab(st);
705 _parameters.print_data_on(st);
706 st->cr();
707 }
708
709 void SpeculativeTrapData::metaspace_pointers_do(MetaspaceClosure* it) {
710 Method** m = (Method**)intptr_at_adr(speculative_trap_method);
711 it->push(m);
712 }
713
714 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
715 print_shared(st, "SpeculativeTrapData", extra);
716 tab(st);
717 method()->print_short_name(st);
718 st->cr();
719 }
720
721 void ArrayStoreData::print_data_on(outputStream* st, const char* extra) const {
722 print_shared(st, "ArrayStore", extra);
723 st->cr();
724 tab(st, true);
725 st->print("array");
726 _array.print_data_on(st);
727 tab(st, true);
728 st->print("element");
729 if (null_seen()) {
730 st->print(" (null seen)");
731 }
732 tab(st);
733 print_receiver_data_on(st);
734 }
735
736 void ArrayLoadData::print_data_on(outputStream* st, const char* extra) const {
737 print_shared(st, "ArrayLoad", extra);
738 st->cr();
739 tab(st, true);
740 st->print("array");
741 _array.print_data_on(st);
742 tab(st, true);
743 st->print("element");
744 _element.print_data_on(st);
745 }
746
747 void ACmpData::print_data_on(outputStream* st, const char* extra) const {
748 BranchData::print_data_on(st, extra);
749 tab(st, true);
750 st->print("left");
751 _left.print_data_on(st);
752 tab(st, true);
753 st->print("right");
754 _right.print_data_on(st);
755 }
756
757 // ==================================================================
758 // MethodData*
759 //
760 // A MethodData* holds information which has been collected about
761 // a method.
762
763 MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) {
764 assert(!THREAD->owns_locks(), "Should not own any locks");
765 int size = MethodData::compute_allocation_size_in_words(method);
766
767 return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
768 MethodData(method);
769 }
770
771 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
772 switch (code) {
773 case Bytecodes::_checkcast:
774 case Bytecodes::_instanceof:
775 if (TypeProfileCasts) {
776 return ReceiverTypeData::static_cell_count();
777 } else {
778 return BitData::static_cell_count();
779 }
780 case Bytecodes::_aaload:
781 return ArrayLoadData::static_cell_count();
782 case Bytecodes::_aastore:
783 return ArrayStoreData::static_cell_count();
784 case Bytecodes::_invokespecial:
785 case Bytecodes::_invokestatic:
786 if (MethodData::profile_arguments() || MethodData::profile_return()) {
787 return variable_cell_count;
788 } else {
789 return CounterData::static_cell_count();
790 }
791 case Bytecodes::_goto:
792 case Bytecodes::_goto_w:
793 case Bytecodes::_jsr:
794 case Bytecodes::_jsr_w:
795 return JumpData::static_cell_count();
796 case Bytecodes::_invokevirtual:
797 case Bytecodes::_invokeinterface:
798 if (MethodData::profile_arguments() || MethodData::profile_return()) {
799 return variable_cell_count;
800 } else {
801 return VirtualCallData::static_cell_count();
802 }
803 case Bytecodes::_invokedynamic:
804 if (MethodData::profile_arguments() || MethodData::profile_return()) {
805 return variable_cell_count;
806 } else {
807 return CounterData::static_cell_count();
808 }
809 case Bytecodes::_ret:
810 return RetData::static_cell_count();
811 case Bytecodes::_ifeq:
812 case Bytecodes::_ifne:
813 case Bytecodes::_iflt:
814 case Bytecodes::_ifge:
815 case Bytecodes::_ifgt:
816 case Bytecodes::_ifle:
817 case Bytecodes::_if_icmpeq:
818 case Bytecodes::_if_icmpne:
819 case Bytecodes::_if_icmplt:
820 case Bytecodes::_if_icmpge:
821 case Bytecodes::_if_icmpgt:
822 case Bytecodes::_if_icmple:
823 case Bytecodes::_ifnull:
824 case Bytecodes::_ifnonnull:
825 return BranchData::static_cell_count();
826 case Bytecodes::_if_acmpne:
827 case Bytecodes::_if_acmpeq:
828 return ACmpData::static_cell_count();
829 case Bytecodes::_lookupswitch:
830 case Bytecodes::_tableswitch:
831 return variable_cell_count;
832 default:
833 return no_profile_data;
834 }
835 }
836
837 // Compute the size of the profiling information corresponding to
838 // the current bytecode.
839 int MethodData::compute_data_size(BytecodeStream* stream) {
840 int cell_count = bytecode_cell_count(stream->code());
841 if (cell_count == no_profile_data) {
842 return 0;
843 }
844 if (cell_count == variable_cell_count) {
845 switch (stream->code()) {
846 case Bytecodes::_lookupswitch:
847 case Bytecodes::_tableswitch:
848 cell_count = MultiBranchData::compute_cell_count(stream);
867 } else {
868 cell_count = VirtualCallData::static_cell_count();
869 }
870 break;
871 }
872 default:
873 fatal("unexpected bytecode for var length profile data");
874 }
875 }
876 // Note: cell_count might be zero, meaning that there is just
877 // a DataLayout header, with no extra cells.
878 assert(cell_count >= 0, "sanity");
879 return DataLayout::compute_size_in_bytes(cell_count);
880 }
881
882 bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) {
883 // Bytecodes for which we may use speculation
884 switch (code) {
885 case Bytecodes::_checkcast:
886 case Bytecodes::_instanceof:
887 case Bytecodes::_aaload:
888 case Bytecodes::_aastore:
889 case Bytecodes::_invokevirtual:
890 case Bytecodes::_invokeinterface:
891 case Bytecodes::_if_acmpeq:
892 case Bytecodes::_if_acmpne:
893 case Bytecodes::_ifnull:
894 case Bytecodes::_ifnonnull:
895 case Bytecodes::_invokestatic:
896 #ifdef COMPILER2
897 if (CompilerConfig::is_c2_enabled()) {
898 return UseTypeSpeculation;
899 }
900 #endif
901 default:
902 return false;
903 }
904 return false;
905 }
906
907 #if INCLUDE_JVMCI
1084
1085 // Compute the size of the MethodData* necessary to store
1086 // profiling information about a given method. Size is in words
1087 int MethodData::compute_allocation_size_in_words(const methodHandle& method) {
1088 int byte_size = compute_allocation_size_in_bytes(method);
1089 int word_size = align_up(byte_size, BytesPerWord) / BytesPerWord;
1090 return align_metadata_size(word_size);
1091 }
1092
1093 // Initialize an individual data segment. Returns the size of
1094 // the segment in bytes.
1095 int MethodData::initialize_data(BytecodeStream* stream,
1096 int data_index) {
1097 int cell_count = -1;
1098 u1 tag = DataLayout::no_tag;
1099 DataLayout* data_layout = data_layout_at(data_index);
1100 Bytecodes::Code c = stream->code();
1101 switch (c) {
1102 case Bytecodes::_checkcast:
1103 case Bytecodes::_instanceof:
1104 if (TypeProfileCasts) {
1105 cell_count = ReceiverTypeData::static_cell_count();
1106 tag = DataLayout::receiver_type_data_tag;
1107 } else {
1108 cell_count = BitData::static_cell_count();
1109 tag = DataLayout::bit_data_tag;
1110 }
1111 break;
1112 case Bytecodes::_aaload:
1113 cell_count = ArrayLoadData::static_cell_count();
1114 tag = DataLayout::array_load_data_tag;
1115 break;
1116 case Bytecodes::_aastore:
1117 cell_count = ArrayStoreData::static_cell_count();
1118 tag = DataLayout::array_store_data_tag;
1119 break;
1120 case Bytecodes::_invokespecial:
1121 case Bytecodes::_invokestatic: {
1122 int counter_data_cell_count = CounterData::static_cell_count();
1123 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1124 profile_return_for_invoke(stream->method(), stream->bci())) {
1125 cell_count = CallTypeData::compute_cell_count(stream);
1126 } else {
1127 cell_count = counter_data_cell_count;
1128 }
1129 if (cell_count > counter_data_cell_count) {
1130 tag = DataLayout::call_type_data_tag;
1131 } else {
1132 tag = DataLayout::counter_data_tag;
1133 }
1134 break;
1135 }
1136 case Bytecodes::_goto:
1137 case Bytecodes::_goto_w:
1138 case Bytecodes::_jsr:
1139 case Bytecodes::_jsr_w:
1171 tag = DataLayout::counter_data_tag;
1172 }
1173 break;
1174 }
1175 case Bytecodes::_ret:
1176 cell_count = RetData::static_cell_count();
1177 tag = DataLayout::ret_data_tag;
1178 break;
1179 case Bytecodes::_ifeq:
1180 case Bytecodes::_ifne:
1181 case Bytecodes::_iflt:
1182 case Bytecodes::_ifge:
1183 case Bytecodes::_ifgt:
1184 case Bytecodes::_ifle:
1185 case Bytecodes::_if_icmpeq:
1186 case Bytecodes::_if_icmpne:
1187 case Bytecodes::_if_icmplt:
1188 case Bytecodes::_if_icmpge:
1189 case Bytecodes::_if_icmpgt:
1190 case Bytecodes::_if_icmple:
1191 case Bytecodes::_ifnull:
1192 case Bytecodes::_ifnonnull:
1193 cell_count = BranchData::static_cell_count();
1194 tag = DataLayout::branch_data_tag;
1195 break;
1196 case Bytecodes::_if_acmpeq:
1197 case Bytecodes::_if_acmpne:
1198 cell_count = ACmpData::static_cell_count();
1199 tag = DataLayout::acmp_data_tag;
1200 break;
1201 case Bytecodes::_lookupswitch:
1202 case Bytecodes::_tableswitch:
1203 cell_count = MultiBranchData::compute_cell_count(stream);
1204 tag = DataLayout::multi_branch_data_tag;
1205 break;
1206 default:
1207 break;
1208 }
1209 assert(tag == DataLayout::multi_branch_data_tag ||
1210 ((MethodData::profile_arguments() || MethodData::profile_return()) &&
1211 (tag == DataLayout::call_type_data_tag ||
1212 tag == DataLayout::counter_data_tag ||
1213 tag == DataLayout::virtual_call_type_data_tag ||
1214 tag == DataLayout::virtual_call_data_tag)) ||
1215 cell_count == bytecode_cell_count(c), "cell counts must agree");
1216 if (cell_count >= 0) {
1217 assert(tag != DataLayout::no_tag, "bad tag");
1218 assert(bytecode_has_profile(c), "agree w/ BHP");
1219 data_layout->initialize(tag, checked_cast<u2>(stream->bci()), cell_count);
1220 return DataLayout::compute_size_in_bytes(cell_count);
1248 case DataLayout::receiver_type_data_tag:
1249 return ReceiverTypeData::static_cell_count();
1250 case DataLayout::virtual_call_data_tag:
1251 return VirtualCallData::static_cell_count();
1252 case DataLayout::ret_data_tag:
1253 return RetData::static_cell_count();
1254 case DataLayout::branch_data_tag:
1255 return BranchData::static_cell_count();
1256 case DataLayout::multi_branch_data_tag:
1257 return ((new MultiBranchData(this))->cell_count());
1258 case DataLayout::arg_info_data_tag:
1259 return ((new ArgInfoData(this))->cell_count());
1260 case DataLayout::call_type_data_tag:
1261 return ((new CallTypeData(this))->cell_count());
1262 case DataLayout::virtual_call_type_data_tag:
1263 return ((new VirtualCallTypeData(this))->cell_count());
1264 case DataLayout::parameters_type_data_tag:
1265 return ((new ParametersTypeData(this))->cell_count());
1266 case DataLayout::speculative_trap_data_tag:
1267 return SpeculativeTrapData::static_cell_count();
1268 case DataLayout::array_store_data_tag:
1269 return ((new ArrayStoreData(this))->cell_count());
1270 case DataLayout::array_load_data_tag:
1271 return ((new ArrayLoadData(this))->cell_count());
1272 case DataLayout::acmp_data_tag:
1273 return ((new ACmpData(this))->cell_count());
1274 }
1275 }
1276 ProfileData* DataLayout::data_in() {
1277 switch (tag()) {
1278 case DataLayout::no_tag:
1279 default:
1280 ShouldNotReachHere();
1281 return nullptr;
1282 case DataLayout::bit_data_tag:
1283 return new BitData(this);
1284 case DataLayout::counter_data_tag:
1285 return new CounterData(this);
1286 case DataLayout::jump_data_tag:
1287 return new JumpData(this);
1288 case DataLayout::receiver_type_data_tag:
1289 return new ReceiverTypeData(this);
1290 case DataLayout::virtual_call_data_tag:
1291 return new VirtualCallData(this);
1292 case DataLayout::ret_data_tag:
1293 return new RetData(this);
1294 case DataLayout::branch_data_tag:
1295 return new BranchData(this);
1296 case DataLayout::multi_branch_data_tag:
1297 return new MultiBranchData(this);
1298 case DataLayout::arg_info_data_tag:
1299 return new ArgInfoData(this);
1300 case DataLayout::call_type_data_tag:
1301 return new CallTypeData(this);
1302 case DataLayout::virtual_call_type_data_tag:
1303 return new VirtualCallTypeData(this);
1304 case DataLayout::parameters_type_data_tag:
1305 return new ParametersTypeData(this);
1306 case DataLayout::speculative_trap_data_tag:
1307 return new SpeculativeTrapData(this);
1308 case DataLayout::array_store_data_tag:
1309 return new ArrayStoreData(this);
1310 case DataLayout::array_load_data_tag:
1311 return new ArrayLoadData(this);
1312 case DataLayout::acmp_data_tag:
1313 return new ACmpData(this);
1314 }
1315 }
1316
1317 // Iteration over data.
1318 ProfileData* MethodData::next_data(ProfileData* current) const {
1319 int current_index = dp_to_di(current->dp());
1320 int next_index = current_index + current->size_in_bytes();
1321 ProfileData* next = data_at(next_index);
1322 return next;
1323 }
1324
1325 DataLayout* MethodData::next_data_layout(DataLayout* current) const {
1326 int current_index = dp_to_di((address)current);
1327 int next_index = current_index + current->size_in_bytes();
1328 if (out_of_bounds(next_index)) {
1329 return nullptr;
1330 }
1331 DataLayout* next = data_layout_at(next_index);
1332 return next;
1333 }
|