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