124
125 void ProfileData::print_data_on(outputStream* st, const MethodData* md) const {
126 print_data_on(st, print_data_on_helper(md));
127 }
128
129 void ProfileData::print_shared(outputStream* st, const char* name, const char* extra) const {
130 st->print("bci: %d ", bci());
131 st->fill_to(tab_width_one + 1);
132 st->print("%s", name);
133 tab(st);
134 int trap = trap_state();
135 if (trap != 0) {
136 char buf[100];
137 st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
138 }
139 if (extra != nullptr) {
140 st->print("%s", extra);
141 }
142 int flags = data()->flags();
143 if (flags != 0) {
144 st->print("flags(%d) ", flags);
145 }
146 }
147
148 void ProfileData::tab(outputStream* st, bool first) const {
149 st->fill_to(first ? tab_width_one : tab_width_two);
150 }
151
152 // ==================================================================
153 // BitData
154 //
155 // A BitData corresponds to a one-bit flag. This is used to indicate
156 // whether a checkcast bytecode has seen a null value.
157
158
159 void BitData::print_data_on(outputStream* st, const char* extra) const {
160 print_shared(st, "BitData", extra);
161 st->cr();
162 }
163
164 // ==================================================================
194 set_displacement(offset);
195 }
196
197 void JumpData::print_data_on(outputStream* st, const char* extra) const {
198 print_shared(st, "JumpData", extra);
199 st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
200 }
201
202 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) {
203 // Parameter profiling include the receiver
204 int args_count = include_receiver ? 1 : 0;
205 ResourceMark rm;
206 ReferenceArgumentCount rac(signature);
207 args_count += rac.count();
208 args_count = MIN2(args_count, max);
209 return args_count * per_arg_cell_count;
210 }
211
212 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
213 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
214 assert(TypeStackSlotEntries::per_arg_count() > ReturnTypeEntry::static_cell_count(), "code to test for arguments/results broken");
215 const methodHandle m = stream->method();
216 int bci = stream->bci();
217 Bytecode_invoke inv(m, bci);
218 int args_cell = 0;
219 if (MethodData::profile_arguments_for_invoke(m, bci)) {
220 args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit);
221 }
222 int ret_cell = 0;
223 if (MethodData::profile_return_for_invoke(m, bci) && is_reference_type(inv.result_type())) {
224 ret_cell = ReturnTypeEntry::static_cell_count();
225 }
226 int header_cell = 0;
227 if (args_cell + ret_cell > 0) {
228 header_cell = header_cell_count();
229 }
230
231 return header_cell + args_cell + ret_cell;
232 }
233
234 class ArgumentOffsetComputer : public SignatureIterator {
235 private:
236 int _max;
237 int _offset;
238 GrowableArray<int> _offsets;
239
240 friend class SignatureIterator; // so do_parameters_on can call do_type
241 void do_type(BasicType type) {
242 if (is_reference_type(type) && _offsets.length() < _max) {
243 _offsets.push(_offset);
244 }
307 #endif
308 _args.post_initialize(inv.signature(), inv.has_receiver(), false);
309 }
310
311 if (has_return()) {
312 assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
313 _ret.post_initialize();
314 }
315 }
316
317 void TypeStackSlotEntries::clean_weak_klass_links(bool always_clean) {
318 for (int i = 0; i < _number_of_entries; i++) {
319 intptr_t p = type(i);
320 Klass* k = (Klass*)klass_part(p);
321 if (k != nullptr && (always_clean || !k->is_loader_alive())) {
322 set_type(i, with_status((Klass*)nullptr, p));
323 }
324 }
325 }
326
327 void ReturnTypeEntry::clean_weak_klass_links(bool always_clean) {
328 intptr_t p = type();
329 Klass* k = (Klass*)klass_part(p);
330 if (k != nullptr && (always_clean || !k->is_loader_alive())) {
331 set_type(with_status((Klass*)nullptr, p));
332 }
333 }
334
335 bool TypeEntriesAtCall::return_profiling_enabled() {
336 return MethodData::profile_return();
337 }
338
339 bool TypeEntriesAtCall::arguments_profiling_enabled() {
340 return MethodData::profile_arguments();
341 }
342
343 void TypeEntries::print_klass(outputStream* st, intptr_t k) {
344 if (is_type_none(k)) {
345 st->print("none");
346 } else if (is_type_unknown(k)) {
347 st->print("unknown");
348 } else {
349 valid_klass(k)->print_value_on(st);
350 }
351 if (was_null_seen(k)) {
352 st->print(" (null seen)");
353 }
354 }
355
356 void TypeStackSlotEntries::print_data_on(outputStream* st) const {
357 for (int i = 0; i < _number_of_entries; i++) {
358 _pd->tab(st);
359 st->print("%d: stack(%u) ", i, stack_slot(i));
360 print_klass(st, type(i));
361 st->cr();
362 }
363 }
364
365 void ReturnTypeEntry::print_data_on(outputStream* st) const {
366 _pd->tab(st);
367 print_klass(st, type());
368 st->cr();
369 }
370
371 void CallTypeData::print_data_on(outputStream* st, const char* extra) const {
372 CounterData::print_data_on(st, extra);
373 if (has_arguments()) {
374 tab(st, true);
375 st->print("argument types");
376 _args.print_data_on(st);
377 }
378 if (has_return()) {
379 tab(st, true);
380 st->print("return type");
381 _ret.print_data_on(st);
382 }
383 }
384
385 void VirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
506 }
507
508 // ==================================================================
509 // BranchData
510 //
511 // A BranchData is used to access profiling data for a two-way branch.
512 // It consists of taken and not_taken counts as well as a data displacement
513 // for the taken case.
514
515 void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
516 assert(stream->bci() == bci(), "wrong pos");
517 int target = stream->dest();
518 int my_di = mdo->dp_to_di(dp());
519 int target_di = mdo->bci_to_di(target);
520 int offset = target_di - my_di;
521 set_displacement(offset);
522 }
523
524 void BranchData::print_data_on(outputStream* st, const char* extra) const {
525 print_shared(st, "BranchData", extra);
526 st->print_cr("taken(%u) displacement(%d)",
527 taken(), displacement());
528 tab(st);
529 st->print_cr("not taken(%u)", not_taken());
530 }
531
532 // ==================================================================
533 // MultiBranchData
534 //
535 // A MultiBranchData is used to access profiling information for
536 // a multi-way branch (*switch bytecodes). It consists of a series
537 // of (count, displacement) pairs, which count the number of times each
538 // case was taken and specify the data displacement for each branch target.
539
540 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
541 int cell_count = 0;
542 if (stream->code() == Bytecodes::_tableswitch) {
543 Bytecode_tableswitch sw(stream->method()(), stream->bcp());
544 cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
545 } else {
631 }
632
633 bool ParametersTypeData::profiling_enabled() {
634 return MethodData::profile_parameters();
635 }
636
637 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
638 print_shared(st, "ParametersTypeData", extra);
639 tab(st);
640 _parameters.print_data_on(st);
641 st->cr();
642 }
643
644 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
645 print_shared(st, "SpeculativeTrapData", extra);
646 tab(st);
647 method()->print_short_name(st);
648 st->cr();
649 }
650
651 // ==================================================================
652 // MethodData*
653 //
654 // A MethodData* holds information which has been collected about
655 // a method.
656
657 MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) {
658 assert(!THREAD->owns_locks(), "Should not own any locks");
659 int size = MethodData::compute_allocation_size_in_words(method);
660
661 return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
662 MethodData(method);
663 }
664
665 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
666 switch (code) {
667 case Bytecodes::_checkcast:
668 case Bytecodes::_instanceof:
669 case Bytecodes::_aastore:
670 if (TypeProfileCasts) {
671 return ReceiverTypeData::static_cell_count();
672 } else {
673 return BitData::static_cell_count();
674 }
675 case Bytecodes::_invokespecial:
676 case Bytecodes::_invokestatic:
677 if (MethodData::profile_arguments() || MethodData::profile_return()) {
678 return variable_cell_count;
679 } else {
680 return CounterData::static_cell_count();
681 }
682 case Bytecodes::_goto:
683 case Bytecodes::_goto_w:
684 case Bytecodes::_jsr:
685 case Bytecodes::_jsr_w:
686 return JumpData::static_cell_count();
687 case Bytecodes::_invokevirtual:
688 case Bytecodes::_invokeinterface:
689 if (MethodData::profile_arguments() || MethodData::profile_return()) {
690 return variable_cell_count;
691 } else {
692 return VirtualCallData::static_cell_count();
693 }
694 case Bytecodes::_invokedynamic:
695 if (MethodData::profile_arguments() || MethodData::profile_return()) {
696 return variable_cell_count;
697 } else {
698 return CounterData::static_cell_count();
699 }
700 case Bytecodes::_ret:
701 return RetData::static_cell_count();
702 case Bytecodes::_ifeq:
703 case Bytecodes::_ifne:
704 case Bytecodes::_iflt:
705 case Bytecodes::_ifge:
706 case Bytecodes::_ifgt:
707 case Bytecodes::_ifle:
708 case Bytecodes::_if_icmpeq:
709 case Bytecodes::_if_icmpne:
710 case Bytecodes::_if_icmplt:
711 case Bytecodes::_if_icmpge:
712 case Bytecodes::_if_icmpgt:
713 case Bytecodes::_if_icmple:
714 case Bytecodes::_if_acmpeq:
715 case Bytecodes::_if_acmpne:
716 case Bytecodes::_ifnull:
717 case Bytecodes::_ifnonnull:
718 return BranchData::static_cell_count();
719 case Bytecodes::_lookupswitch:
720 case Bytecodes::_tableswitch:
721 return variable_cell_count;
722 default:
723 return no_profile_data;
724 }
725 }
726
727 // Compute the size of the profiling information corresponding to
728 // the current bytecode.
729 int MethodData::compute_data_size(BytecodeStream* stream) {
730 int cell_count = bytecode_cell_count(stream->code());
731 if (cell_count == no_profile_data) {
732 return 0;
733 }
734 if (cell_count == variable_cell_count) {
735 switch (stream->code()) {
736 case Bytecodes::_lookupswitch:
737 case Bytecodes::_tableswitch:
738 cell_count = MultiBranchData::compute_cell_count(stream);
757 } else {
758 cell_count = VirtualCallData::static_cell_count();
759 }
760 break;
761 }
762 default:
763 fatal("unexpected bytecode for var length profile data");
764 }
765 }
766 // Note: cell_count might be zero, meaning that there is just
767 // a DataLayout header, with no extra cells.
768 assert(cell_count >= 0, "sanity");
769 return DataLayout::compute_size_in_bytes(cell_count);
770 }
771
772 bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) {
773 // Bytecodes for which we may use speculation
774 switch (code) {
775 case Bytecodes::_checkcast:
776 case Bytecodes::_instanceof:
777 case Bytecodes::_aastore:
778 case Bytecodes::_invokevirtual:
779 case Bytecodes::_invokeinterface:
780 case Bytecodes::_if_acmpeq:
781 case Bytecodes::_if_acmpne:
782 case Bytecodes::_ifnull:
783 case Bytecodes::_ifnonnull:
784 case Bytecodes::_invokestatic:
785 #ifdef COMPILER2
786 if (CompilerConfig::is_c2_enabled()) {
787 return UseTypeSpeculation;
788 }
789 #endif
790 default:
791 return false;
792 }
793 return false;
794 }
795
796 #if INCLUDE_JVMCI
973
974 // Compute the size of the MethodData* necessary to store
975 // profiling information about a given method. Size is in words
976 int MethodData::compute_allocation_size_in_words(const methodHandle& method) {
977 int byte_size = compute_allocation_size_in_bytes(method);
978 int word_size = align_up(byte_size, BytesPerWord) / BytesPerWord;
979 return align_metadata_size(word_size);
980 }
981
982 // Initialize an individual data segment. Returns the size of
983 // the segment in bytes.
984 int MethodData::initialize_data(BytecodeStream* stream,
985 int data_index) {
986 int cell_count = -1;
987 u1 tag = DataLayout::no_tag;
988 DataLayout* data_layout = data_layout_at(data_index);
989 Bytecodes::Code c = stream->code();
990 switch (c) {
991 case Bytecodes::_checkcast:
992 case Bytecodes::_instanceof:
993 case Bytecodes::_aastore:
994 if (TypeProfileCasts) {
995 cell_count = ReceiverTypeData::static_cell_count();
996 tag = DataLayout::receiver_type_data_tag;
997 } else {
998 cell_count = BitData::static_cell_count();
999 tag = DataLayout::bit_data_tag;
1000 }
1001 break;
1002 case Bytecodes::_invokespecial:
1003 case Bytecodes::_invokestatic: {
1004 int counter_data_cell_count = CounterData::static_cell_count();
1005 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1006 profile_return_for_invoke(stream->method(), stream->bci())) {
1007 cell_count = CallTypeData::compute_cell_count(stream);
1008 } else {
1009 cell_count = counter_data_cell_count;
1010 }
1011 if (cell_count > counter_data_cell_count) {
1012 tag = DataLayout::call_type_data_tag;
1013 } else {
1014 tag = DataLayout::counter_data_tag;
1015 }
1016 break;
1017 }
1018 case Bytecodes::_goto:
1019 case Bytecodes::_goto_w:
1020 case Bytecodes::_jsr:
1021 case Bytecodes::_jsr_w:
1053 tag = DataLayout::counter_data_tag;
1054 }
1055 break;
1056 }
1057 case Bytecodes::_ret:
1058 cell_count = RetData::static_cell_count();
1059 tag = DataLayout::ret_data_tag;
1060 break;
1061 case Bytecodes::_ifeq:
1062 case Bytecodes::_ifne:
1063 case Bytecodes::_iflt:
1064 case Bytecodes::_ifge:
1065 case Bytecodes::_ifgt:
1066 case Bytecodes::_ifle:
1067 case Bytecodes::_if_icmpeq:
1068 case Bytecodes::_if_icmpne:
1069 case Bytecodes::_if_icmplt:
1070 case Bytecodes::_if_icmpge:
1071 case Bytecodes::_if_icmpgt:
1072 case Bytecodes::_if_icmple:
1073 case Bytecodes::_if_acmpeq:
1074 case Bytecodes::_if_acmpne:
1075 case Bytecodes::_ifnull:
1076 case Bytecodes::_ifnonnull:
1077 cell_count = BranchData::static_cell_count();
1078 tag = DataLayout::branch_data_tag;
1079 break;
1080 case Bytecodes::_lookupswitch:
1081 case Bytecodes::_tableswitch:
1082 cell_count = MultiBranchData::compute_cell_count(stream);
1083 tag = DataLayout::multi_branch_data_tag;
1084 break;
1085 default:
1086 break;
1087 }
1088 assert(tag == DataLayout::multi_branch_data_tag ||
1089 ((MethodData::profile_arguments() || MethodData::profile_return()) &&
1090 (tag == DataLayout::call_type_data_tag ||
1091 tag == DataLayout::counter_data_tag ||
1092 tag == DataLayout::virtual_call_type_data_tag ||
1093 tag == DataLayout::virtual_call_data_tag)) ||
1094 cell_count == bytecode_cell_count(c), "cell counts must agree");
1095 if (cell_count >= 0) {
1096 assert(tag != DataLayout::no_tag, "bad tag");
1097 assert(bytecode_has_profile(c), "agree w/ BHP");
1098 data_layout->initialize(tag, checked_cast<u2>(stream->bci()), cell_count);
1099 return DataLayout::compute_size_in_bytes(cell_count);
1127 case DataLayout::receiver_type_data_tag:
1128 return ReceiverTypeData::static_cell_count();
1129 case DataLayout::virtual_call_data_tag:
1130 return VirtualCallData::static_cell_count();
1131 case DataLayout::ret_data_tag:
1132 return RetData::static_cell_count();
1133 case DataLayout::branch_data_tag:
1134 return BranchData::static_cell_count();
1135 case DataLayout::multi_branch_data_tag:
1136 return ((new MultiBranchData(this))->cell_count());
1137 case DataLayout::arg_info_data_tag:
1138 return ((new ArgInfoData(this))->cell_count());
1139 case DataLayout::call_type_data_tag:
1140 return ((new CallTypeData(this))->cell_count());
1141 case DataLayout::virtual_call_type_data_tag:
1142 return ((new VirtualCallTypeData(this))->cell_count());
1143 case DataLayout::parameters_type_data_tag:
1144 return ((new ParametersTypeData(this))->cell_count());
1145 case DataLayout::speculative_trap_data_tag:
1146 return SpeculativeTrapData::static_cell_count();
1147 }
1148 }
1149 ProfileData* DataLayout::data_in() {
1150 switch (tag()) {
1151 case DataLayout::no_tag:
1152 default:
1153 ShouldNotReachHere();
1154 return nullptr;
1155 case DataLayout::bit_data_tag:
1156 return new BitData(this);
1157 case DataLayout::counter_data_tag:
1158 return new CounterData(this);
1159 case DataLayout::jump_data_tag:
1160 return new JumpData(this);
1161 case DataLayout::receiver_type_data_tag:
1162 return new ReceiverTypeData(this);
1163 case DataLayout::virtual_call_data_tag:
1164 return new VirtualCallData(this);
1165 case DataLayout::ret_data_tag:
1166 return new RetData(this);
1167 case DataLayout::branch_data_tag:
1168 return new BranchData(this);
1169 case DataLayout::multi_branch_data_tag:
1170 return new MultiBranchData(this);
1171 case DataLayout::arg_info_data_tag:
1172 return new ArgInfoData(this);
1173 case DataLayout::call_type_data_tag:
1174 return new CallTypeData(this);
1175 case DataLayout::virtual_call_type_data_tag:
1176 return new VirtualCallTypeData(this);
1177 case DataLayout::parameters_type_data_tag:
1178 return new ParametersTypeData(this);
1179 case DataLayout::speculative_trap_data_tag:
1180 return new SpeculativeTrapData(this);
1181 }
1182 }
1183
1184 // Iteration over data.
1185 ProfileData* MethodData::next_data(ProfileData* current) const {
1186 int current_index = dp_to_di(current->dp());
1187 int next_index = current_index + current->size_in_bytes();
1188 ProfileData* next = data_at(next_index);
1189 return next;
1190 }
1191
1192 DataLayout* MethodData::next_data_layout(DataLayout* current) const {
1193 int current_index = dp_to_di((address)current);
1194 int next_index = current_index + current->size_in_bytes();
1195 if (out_of_bounds(next_index)) {
1196 return nullptr;
1197 }
1198 DataLayout* next = data_layout_at(next_index);
1199 return next;
1200 }
|
124
125 void ProfileData::print_data_on(outputStream* st, const MethodData* md) const {
126 print_data_on(st, print_data_on_helper(md));
127 }
128
129 void ProfileData::print_shared(outputStream* st, const char* name, const char* extra) const {
130 st->print("bci: %d ", bci());
131 st->fill_to(tab_width_one + 1);
132 st->print("%s", name);
133 tab(st);
134 int trap = trap_state();
135 if (trap != 0) {
136 char buf[100];
137 st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
138 }
139 if (extra != nullptr) {
140 st->print("%s", extra);
141 }
142 int flags = data()->flags();
143 if (flags != 0) {
144 st->print("flags(%d) %p/%d", flags, data(), in_bytes(DataLayout::flags_offset()));
145 }
146 }
147
148 void ProfileData::tab(outputStream* st, bool first) const {
149 st->fill_to(first ? tab_width_one : tab_width_two);
150 }
151
152 // ==================================================================
153 // BitData
154 //
155 // A BitData corresponds to a one-bit flag. This is used to indicate
156 // whether a checkcast bytecode has seen a null value.
157
158
159 void BitData::print_data_on(outputStream* st, const char* extra) const {
160 print_shared(st, "BitData", extra);
161 st->cr();
162 }
163
164 // ==================================================================
194 set_displacement(offset);
195 }
196
197 void JumpData::print_data_on(outputStream* st, const char* extra) const {
198 print_shared(st, "JumpData", extra);
199 st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
200 }
201
202 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) {
203 // Parameter profiling include the receiver
204 int args_count = include_receiver ? 1 : 0;
205 ResourceMark rm;
206 ReferenceArgumentCount rac(signature);
207 args_count += rac.count();
208 args_count = MIN2(args_count, max);
209 return args_count * per_arg_cell_count;
210 }
211
212 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
213 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
214 assert(TypeStackSlotEntries::per_arg_count() > SingleTypeEntry::static_cell_count(), "code to test for arguments/results broken");
215 const methodHandle m = stream->method();
216 int bci = stream->bci();
217 Bytecode_invoke inv(m, bci);
218 int args_cell = 0;
219 if (MethodData::profile_arguments_for_invoke(m, bci)) {
220 args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit);
221 }
222 int ret_cell = 0;
223 if (MethodData::profile_return_for_invoke(m, bci) && is_reference_type(inv.result_type())) {
224 ret_cell = SingleTypeEntry::static_cell_count();
225 }
226 int header_cell = 0;
227 if (args_cell + ret_cell > 0) {
228 header_cell = header_cell_count();
229 }
230
231 return header_cell + args_cell + ret_cell;
232 }
233
234 class ArgumentOffsetComputer : public SignatureIterator {
235 private:
236 int _max;
237 int _offset;
238 GrowableArray<int> _offsets;
239
240 friend class SignatureIterator; // so do_parameters_on can call do_type
241 void do_type(BasicType type) {
242 if (is_reference_type(type) && _offsets.length() < _max) {
243 _offsets.push(_offset);
244 }
307 #endif
308 _args.post_initialize(inv.signature(), inv.has_receiver(), false);
309 }
310
311 if (has_return()) {
312 assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
313 _ret.post_initialize();
314 }
315 }
316
317 void TypeStackSlotEntries::clean_weak_klass_links(bool always_clean) {
318 for (int i = 0; i < _number_of_entries; i++) {
319 intptr_t p = type(i);
320 Klass* k = (Klass*)klass_part(p);
321 if (k != nullptr && (always_clean || !k->is_loader_alive())) {
322 set_type(i, with_status((Klass*)nullptr, p));
323 }
324 }
325 }
326
327 void SingleTypeEntry::clean_weak_klass_links(bool always_clean) {
328 intptr_t p = type();
329 Klass* k = (Klass*)klass_part(p);
330 if (k != nullptr && (always_clean || !k->is_loader_alive())) {
331 set_type(with_status((Klass*)nullptr, p));
332 }
333 }
334
335 bool TypeEntriesAtCall::return_profiling_enabled() {
336 return MethodData::profile_return();
337 }
338
339 bool TypeEntriesAtCall::arguments_profiling_enabled() {
340 return MethodData::profile_arguments();
341 }
342
343 void TypeEntries::print_klass(outputStream* st, intptr_t k) {
344 if (is_type_none(k)) {
345 st->print("none");
346 } else if (is_type_unknown(k)) {
347 st->print("unknown");
348 } else {
349 valid_klass(k)->print_value_on(st);
350 }
351 if (was_null_seen(k)) {
352 st->print(" (null seen)");
353 }
354 }
355
356 void TypeStackSlotEntries::print_data_on(outputStream* st) const {
357 for (int i = 0; i < _number_of_entries; i++) {
358 _pd->tab(st);
359 st->print("%d: stack(%u) ", i, stack_slot(i));
360 print_klass(st, type(i));
361 st->cr();
362 }
363 }
364
365 void SingleTypeEntry::print_data_on(outputStream* st) const {
366 _pd->tab(st);
367 print_klass(st, type());
368 st->cr();
369 }
370
371 void CallTypeData::print_data_on(outputStream* st, const char* extra) const {
372 CounterData::print_data_on(st, extra);
373 if (has_arguments()) {
374 tab(st, true);
375 st->print("argument types");
376 _args.print_data_on(st);
377 }
378 if (has_return()) {
379 tab(st, true);
380 st->print("return type");
381 _ret.print_data_on(st);
382 }
383 }
384
385 void VirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
506 }
507
508 // ==================================================================
509 // BranchData
510 //
511 // A BranchData is used to access profiling data for a two-way branch.
512 // It consists of taken and not_taken counts as well as a data displacement
513 // for the taken case.
514
515 void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
516 assert(stream->bci() == bci(), "wrong pos");
517 int target = stream->dest();
518 int my_di = mdo->dp_to_di(dp());
519 int target_di = mdo->bci_to_di(target);
520 int offset = target_di - my_di;
521 set_displacement(offset);
522 }
523
524 void BranchData::print_data_on(outputStream* st, const char* extra) const {
525 print_shared(st, "BranchData", extra);
526 if (data()->flags()) {
527 tty->cr();
528 tab(st);
529 }
530 st->print_cr("taken(%u) displacement(%d)",
531 taken(), displacement());
532 tab(st);
533 st->print_cr("not taken(%u)", not_taken());
534 }
535
536 // ==================================================================
537 // MultiBranchData
538 //
539 // A MultiBranchData is used to access profiling information for
540 // a multi-way branch (*switch bytecodes). It consists of a series
541 // of (count, displacement) pairs, which count the number of times each
542 // case was taken and specify the data displacement for each branch target.
543
544 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
545 int cell_count = 0;
546 if (stream->code() == Bytecodes::_tableswitch) {
547 Bytecode_tableswitch sw(stream->method()(), stream->bcp());
548 cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
549 } else {
635 }
636
637 bool ParametersTypeData::profiling_enabled() {
638 return MethodData::profile_parameters();
639 }
640
641 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
642 print_shared(st, "ParametersTypeData", extra);
643 tab(st);
644 _parameters.print_data_on(st);
645 st->cr();
646 }
647
648 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
649 print_shared(st, "SpeculativeTrapData", extra);
650 tab(st);
651 method()->print_short_name(st);
652 st->cr();
653 }
654
655 void ArrayStoreData::print_data_on(outputStream* st, const char* extra) const {
656 print_shared(st, "ArrayStore", extra);
657 st->cr();
658 tab(st, true);
659 st->print("array");
660 _array.print_data_on(st);
661 tab(st, true);
662 st->print("element");
663 if (null_seen()) {
664 st->print(" (null seen)");
665 }
666 tab(st);
667 print_receiver_data_on(st);
668 }
669
670 void ArrayLoadData::print_data_on(outputStream* st, const char* extra) const {
671 print_shared(st, "ArrayLoad", extra);
672 st->cr();
673 tab(st, true);
674 st->print("array");
675 _array.print_data_on(st);
676 tab(st, true);
677 st->print("element");
678 _element.print_data_on(st);
679 }
680
681 void ACmpData::print_data_on(outputStream* st, const char* extra) const {
682 BranchData::print_data_on(st, extra);
683 tab(st, true);
684 st->print("left");
685 _left.print_data_on(st);
686 tab(st, true);
687 st->print("right");
688 _right.print_data_on(st);
689 }
690
691 // ==================================================================
692 // MethodData*
693 //
694 // A MethodData* holds information which has been collected about
695 // a method.
696
697 MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) {
698 assert(!THREAD->owns_locks(), "Should not own any locks");
699 int size = MethodData::compute_allocation_size_in_words(method);
700
701 return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
702 MethodData(method);
703 }
704
705 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
706 switch (code) {
707 case Bytecodes::_checkcast:
708 case Bytecodes::_instanceof:
709 if (TypeProfileCasts) {
710 return ReceiverTypeData::static_cell_count();
711 } else {
712 return BitData::static_cell_count();
713 }
714 case Bytecodes::_aaload:
715 return ArrayLoadData::static_cell_count();
716 case Bytecodes::_aastore:
717 return ArrayStoreData::static_cell_count();
718 case Bytecodes::_invokespecial:
719 case Bytecodes::_invokestatic:
720 if (MethodData::profile_arguments() || MethodData::profile_return()) {
721 return variable_cell_count;
722 } else {
723 return CounterData::static_cell_count();
724 }
725 case Bytecodes::_goto:
726 case Bytecodes::_goto_w:
727 case Bytecodes::_jsr:
728 case Bytecodes::_jsr_w:
729 return JumpData::static_cell_count();
730 case Bytecodes::_invokevirtual:
731 case Bytecodes::_invokeinterface:
732 if (MethodData::profile_arguments() || MethodData::profile_return()) {
733 return variable_cell_count;
734 } else {
735 return VirtualCallData::static_cell_count();
736 }
737 case Bytecodes::_invokedynamic:
738 if (MethodData::profile_arguments() || MethodData::profile_return()) {
739 return variable_cell_count;
740 } else {
741 return CounterData::static_cell_count();
742 }
743 case Bytecodes::_ret:
744 return RetData::static_cell_count();
745 case Bytecodes::_ifeq:
746 case Bytecodes::_ifne:
747 case Bytecodes::_iflt:
748 case Bytecodes::_ifge:
749 case Bytecodes::_ifgt:
750 case Bytecodes::_ifle:
751 case Bytecodes::_if_icmpeq:
752 case Bytecodes::_if_icmpne:
753 case Bytecodes::_if_icmplt:
754 case Bytecodes::_if_icmpge:
755 case Bytecodes::_if_icmpgt:
756 case Bytecodes::_if_icmple:
757 case Bytecodes::_ifnull:
758 case Bytecodes::_ifnonnull:
759 return BranchData::static_cell_count();
760 case Bytecodes::_if_acmpne:
761 case Bytecodes::_if_acmpeq:
762 return ACmpData::static_cell_count();
763 case Bytecodes::_lookupswitch:
764 case Bytecodes::_tableswitch:
765 return variable_cell_count;
766 default:
767 return no_profile_data;
768 }
769 }
770
771 // Compute the size of the profiling information corresponding to
772 // the current bytecode.
773 int MethodData::compute_data_size(BytecodeStream* stream) {
774 int cell_count = bytecode_cell_count(stream->code());
775 if (cell_count == no_profile_data) {
776 return 0;
777 }
778 if (cell_count == variable_cell_count) {
779 switch (stream->code()) {
780 case Bytecodes::_lookupswitch:
781 case Bytecodes::_tableswitch:
782 cell_count = MultiBranchData::compute_cell_count(stream);
801 } else {
802 cell_count = VirtualCallData::static_cell_count();
803 }
804 break;
805 }
806 default:
807 fatal("unexpected bytecode for var length profile data");
808 }
809 }
810 // Note: cell_count might be zero, meaning that there is just
811 // a DataLayout header, with no extra cells.
812 assert(cell_count >= 0, "sanity");
813 return DataLayout::compute_size_in_bytes(cell_count);
814 }
815
816 bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) {
817 // Bytecodes for which we may use speculation
818 switch (code) {
819 case Bytecodes::_checkcast:
820 case Bytecodes::_instanceof:
821 case Bytecodes::_aaload:
822 case Bytecodes::_aastore:
823 case Bytecodes::_invokevirtual:
824 case Bytecodes::_invokeinterface:
825 case Bytecodes::_if_acmpeq:
826 case Bytecodes::_if_acmpne:
827 case Bytecodes::_ifnull:
828 case Bytecodes::_ifnonnull:
829 case Bytecodes::_invokestatic:
830 #ifdef COMPILER2
831 if (CompilerConfig::is_c2_enabled()) {
832 return UseTypeSpeculation;
833 }
834 #endif
835 default:
836 return false;
837 }
838 return false;
839 }
840
841 #if INCLUDE_JVMCI
1018
1019 // Compute the size of the MethodData* necessary to store
1020 // profiling information about a given method. Size is in words
1021 int MethodData::compute_allocation_size_in_words(const methodHandle& method) {
1022 int byte_size = compute_allocation_size_in_bytes(method);
1023 int word_size = align_up(byte_size, BytesPerWord) / BytesPerWord;
1024 return align_metadata_size(word_size);
1025 }
1026
1027 // Initialize an individual data segment. Returns the size of
1028 // the segment in bytes.
1029 int MethodData::initialize_data(BytecodeStream* stream,
1030 int data_index) {
1031 int cell_count = -1;
1032 u1 tag = DataLayout::no_tag;
1033 DataLayout* data_layout = data_layout_at(data_index);
1034 Bytecodes::Code c = stream->code();
1035 switch (c) {
1036 case Bytecodes::_checkcast:
1037 case Bytecodes::_instanceof:
1038 if (TypeProfileCasts) {
1039 cell_count = ReceiverTypeData::static_cell_count();
1040 tag = DataLayout::receiver_type_data_tag;
1041 } else {
1042 cell_count = BitData::static_cell_count();
1043 tag = DataLayout::bit_data_tag;
1044 }
1045 break;
1046 case Bytecodes::_aaload:
1047 cell_count = ArrayLoadData::static_cell_count();
1048 tag = DataLayout::array_load_data_tag;
1049 break;
1050 case Bytecodes::_aastore:
1051 cell_count = ArrayStoreData::static_cell_count();
1052 tag = DataLayout::array_store_data_tag;
1053 break;
1054 case Bytecodes::_invokespecial:
1055 case Bytecodes::_invokestatic: {
1056 int counter_data_cell_count = CounterData::static_cell_count();
1057 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
1058 profile_return_for_invoke(stream->method(), stream->bci())) {
1059 cell_count = CallTypeData::compute_cell_count(stream);
1060 } else {
1061 cell_count = counter_data_cell_count;
1062 }
1063 if (cell_count > counter_data_cell_count) {
1064 tag = DataLayout::call_type_data_tag;
1065 } else {
1066 tag = DataLayout::counter_data_tag;
1067 }
1068 break;
1069 }
1070 case Bytecodes::_goto:
1071 case Bytecodes::_goto_w:
1072 case Bytecodes::_jsr:
1073 case Bytecodes::_jsr_w:
1105 tag = DataLayout::counter_data_tag;
1106 }
1107 break;
1108 }
1109 case Bytecodes::_ret:
1110 cell_count = RetData::static_cell_count();
1111 tag = DataLayout::ret_data_tag;
1112 break;
1113 case Bytecodes::_ifeq:
1114 case Bytecodes::_ifne:
1115 case Bytecodes::_iflt:
1116 case Bytecodes::_ifge:
1117 case Bytecodes::_ifgt:
1118 case Bytecodes::_ifle:
1119 case Bytecodes::_if_icmpeq:
1120 case Bytecodes::_if_icmpne:
1121 case Bytecodes::_if_icmplt:
1122 case Bytecodes::_if_icmpge:
1123 case Bytecodes::_if_icmpgt:
1124 case Bytecodes::_if_icmple:
1125 case Bytecodes::_ifnull:
1126 case Bytecodes::_ifnonnull:
1127 cell_count = BranchData::static_cell_count();
1128 tag = DataLayout::branch_data_tag;
1129 break;
1130 case Bytecodes::_if_acmpeq:
1131 case Bytecodes::_if_acmpne:
1132 cell_count = ACmpData::static_cell_count();
1133 tag = DataLayout::acmp_data_tag;
1134 break;
1135 case Bytecodes::_lookupswitch:
1136 case Bytecodes::_tableswitch:
1137 cell_count = MultiBranchData::compute_cell_count(stream);
1138 tag = DataLayout::multi_branch_data_tag;
1139 break;
1140 default:
1141 break;
1142 }
1143 assert(tag == DataLayout::multi_branch_data_tag ||
1144 ((MethodData::profile_arguments() || MethodData::profile_return()) &&
1145 (tag == DataLayout::call_type_data_tag ||
1146 tag == DataLayout::counter_data_tag ||
1147 tag == DataLayout::virtual_call_type_data_tag ||
1148 tag == DataLayout::virtual_call_data_tag)) ||
1149 cell_count == bytecode_cell_count(c), "cell counts must agree");
1150 if (cell_count >= 0) {
1151 assert(tag != DataLayout::no_tag, "bad tag");
1152 assert(bytecode_has_profile(c), "agree w/ BHP");
1153 data_layout->initialize(tag, checked_cast<u2>(stream->bci()), cell_count);
1154 return DataLayout::compute_size_in_bytes(cell_count);
1182 case DataLayout::receiver_type_data_tag:
1183 return ReceiverTypeData::static_cell_count();
1184 case DataLayout::virtual_call_data_tag:
1185 return VirtualCallData::static_cell_count();
1186 case DataLayout::ret_data_tag:
1187 return RetData::static_cell_count();
1188 case DataLayout::branch_data_tag:
1189 return BranchData::static_cell_count();
1190 case DataLayout::multi_branch_data_tag:
1191 return ((new MultiBranchData(this))->cell_count());
1192 case DataLayout::arg_info_data_tag:
1193 return ((new ArgInfoData(this))->cell_count());
1194 case DataLayout::call_type_data_tag:
1195 return ((new CallTypeData(this))->cell_count());
1196 case DataLayout::virtual_call_type_data_tag:
1197 return ((new VirtualCallTypeData(this))->cell_count());
1198 case DataLayout::parameters_type_data_tag:
1199 return ((new ParametersTypeData(this))->cell_count());
1200 case DataLayout::speculative_trap_data_tag:
1201 return SpeculativeTrapData::static_cell_count();
1202 case DataLayout::array_store_data_tag:
1203 return ((new ArrayStoreData(this))->cell_count());
1204 case DataLayout::array_load_data_tag:
1205 return ((new ArrayLoadData(this))->cell_count());
1206 case DataLayout::acmp_data_tag:
1207 return ((new ACmpData(this))->cell_count());
1208 }
1209 }
1210 ProfileData* DataLayout::data_in() {
1211 switch (tag()) {
1212 case DataLayout::no_tag:
1213 default:
1214 ShouldNotReachHere();
1215 return nullptr;
1216 case DataLayout::bit_data_tag:
1217 return new BitData(this);
1218 case DataLayout::counter_data_tag:
1219 return new CounterData(this);
1220 case DataLayout::jump_data_tag:
1221 return new JumpData(this);
1222 case DataLayout::receiver_type_data_tag:
1223 return new ReceiverTypeData(this);
1224 case DataLayout::virtual_call_data_tag:
1225 return new VirtualCallData(this);
1226 case DataLayout::ret_data_tag:
1227 return new RetData(this);
1228 case DataLayout::branch_data_tag:
1229 return new BranchData(this);
1230 case DataLayout::multi_branch_data_tag:
1231 return new MultiBranchData(this);
1232 case DataLayout::arg_info_data_tag:
1233 return new ArgInfoData(this);
1234 case DataLayout::call_type_data_tag:
1235 return new CallTypeData(this);
1236 case DataLayout::virtual_call_type_data_tag:
1237 return new VirtualCallTypeData(this);
1238 case DataLayout::parameters_type_data_tag:
1239 return new ParametersTypeData(this);
1240 case DataLayout::speculative_trap_data_tag:
1241 return new SpeculativeTrapData(this);
1242 case DataLayout::array_store_data_tag:
1243 return new ArrayStoreData(this);
1244 case DataLayout::array_load_data_tag:
1245 return new ArrayLoadData(this);
1246 case DataLayout::acmp_data_tag:
1247 return new ACmpData(this);
1248 }
1249 }
1250
1251 // Iteration over data.
1252 ProfileData* MethodData::next_data(ProfileData* current) const {
1253 int current_index = dp_to_di(current->dp());
1254 int next_index = current_index + current->size_in_bytes();
1255 ProfileData* next = data_at(next_index);
1256 return next;
1257 }
1258
1259 DataLayout* MethodData::next_data_layout(DataLayout* current) const {
1260 int current_index = dp_to_di((address)current);
1261 int next_index = current_index + current->size_in_bytes();
1262 if (out_of_bounds(next_index)) {
1263 return nullptr;
1264 }
1265 DataLayout* next = data_layout_at(next_index);
1266 return next;
1267 }
|