1 /*
2 * Copyright (c) 2000, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/cdsConfig.hpp"
26 #include "ci/ciMethodData.hpp"
27 #include "classfile/systemDictionaryShared.hpp"
28 #include "classfile/vmSymbols.hpp"
29 #include "compiler/compilationPolicy.hpp"
30 #include "compiler/compilerDefinitions.inline.hpp"
31 #include "compiler/compilerOracle.hpp"
32 #include "interpreter/bytecode.hpp"
33 #include "interpreter/bytecodeStream.hpp"
34 #include "interpreter/linkResolver.hpp"
35 #include "memory/metaspaceClosure.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "oops/klass.inline.hpp"
38 #include "oops/method.inline.hpp"
39 #include "oops/methodData.inline.hpp"
40 #include "prims/jvmtiRedefineClasses.hpp"
41 #include "runtime/atomicAccess.hpp"
42 #include "runtime/deoptimization.hpp"
43 #include "runtime/handles.inline.hpp"
44 #include "runtime/orderAccess.hpp"
45 #include "runtime/safepointVerifiers.hpp"
46 #include "runtime/signature.hpp"
47 #include "utilities/align.hpp"
48 #include "utilities/checkedCast.hpp"
49 #include "utilities/copy.hpp"
50
51 // ==================================================================
52 // DataLayout
53 //
54 // Overlay for generic profiling data.
55
56 // Some types of data layouts need a length field.
57 bool DataLayout::needs_array_len(u1 tag) {
58 return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag) || (tag == parameters_type_data_tag);
59 }
60
61 // Perform generic initialization of the data. More specific
62 // initialization occurs in overrides of ProfileData::post_initialize.
63 void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
64 _header._bits = (intptr_t)0;
65 _header._struct._tag = tag;
66 _header._struct._bci = bci;
67 for (int i = 0; i < cell_count; i++) {
68 set_cell_at(i, (intptr_t)0);
69 }
70 if (needs_array_len(tag)) {
71 set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
72 }
73 if (tag == call_type_data_tag) {
74 CallTypeData::initialize(this, cell_count);
75 } else if (tag == virtual_call_type_data_tag) {
76 VirtualCallTypeData::initialize(this, cell_count);
77 }
78 }
79
80 void DataLayout::clean_weak_klass_links(bool always_clean) {
81 ResourceMark m;
82 data_in()->clean_weak_klass_links(always_clean);
83 }
84
85
86 // ==================================================================
87 // ProfileData
88 //
89 // A ProfileData object is created to refer to a section of profiling
90 // data in a structured way.
91
92 // Constructor for invalid ProfileData.
93 ProfileData::ProfileData() {
94 _data = nullptr;
95 }
96
97 char* ProfileData::print_data_on_helper(const MethodData* md) const {
98 DataLayout* dp = md->extra_data_base();
99 DataLayout* end = md->args_data_limit();
100 stringStream ss;
101 for (;; dp = MethodData::next_extra(dp)) {
102 assert(dp < end, "moved past end of extra data");
103 switch(dp->tag()) {
104 case DataLayout::speculative_trap_data_tag:
105 if (dp->bci() == bci()) {
106 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
107 int trap = data->trap_state();
108 char buf[100];
109 ss.print("trap/");
110 data->method()->print_short_name(&ss);
111 ss.print("(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
112 }
113 break;
114 case DataLayout::bit_data_tag:
115 break;
116 case DataLayout::no_tag:
117 case DataLayout::arg_info_data_tag:
118 return ss.as_string();
119 break;
120 default:
121 fatal("unexpected tag %d", dp->tag());
122 }
123 }
124 return nullptr;
125 }
126
127 void ProfileData::print_data_on(outputStream* st, const MethodData* md) const {
128 print_data_on(st, print_data_on_helper(md));
129 }
130
131 void ProfileData::print_shared(outputStream* st, const char* name, const char* extra) const {
132 st->print("bci: %d ", bci());
133 st->fill_to(tab_width_one + 1);
134 st->print("%s", name);
135 tab(st);
136 int trap = trap_state();
137 if (trap != 0) {
138 char buf[100];
139 st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
140 }
141 if (extra != nullptr) {
142 st->print("%s", extra);
143 }
144 int flags = data()->flags();
145 if (flags != 0) {
146 st->print("flags(%d) ", flags);
147 }
148 }
149
150 void ProfileData::tab(outputStream* st, bool first) const {
151 st->fill_to(first ? tab_width_one : tab_width_two);
152 }
153
154 // ==================================================================
155 // BitData
156 //
157 // A BitData corresponds to a one-bit flag. This is used to indicate
158 // whether a checkcast bytecode has seen a null value.
159
160
161 void BitData::print_data_on(outputStream* st, const char* extra) const {
162 print_shared(st, "BitData", extra);
163 st->cr();
164 }
165
166 // ==================================================================
167 // CounterData
168 //
169 // A CounterData corresponds to a simple counter.
170
171 void CounterData::print_data_on(outputStream* st, const char* extra) const {
172 print_shared(st, "CounterData", extra);
173 st->print_cr("count(%u)", count());
174 }
175
176 // ==================================================================
177 // JumpData
178 //
179 // A JumpData is used to access profiling information for a direct
180 // branch. It is a counter, used for counting the number of branches,
181 // plus a data displacement, used for realigning the data pointer to
182 // the corresponding target bci.
183
184 void JumpData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
185 assert(stream->bci() == bci(), "wrong pos");
186 int target;
187 Bytecodes::Code c = stream->code();
188 if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
189 target = stream->dest_w();
190 } else {
191 target = stream->dest();
192 }
193 int my_di = mdo->dp_to_di(dp());
194 int target_di = mdo->bci_to_di(target);
195 int offset = target_di - my_di;
196 set_displacement(offset);
197 }
198
199 void JumpData::print_data_on(outputStream* st, const char* extra) const {
200 print_shared(st, "JumpData", extra);
201 st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
202 }
203
204 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) {
205 // Parameter profiling include the receiver
206 int args_count = include_receiver ? 1 : 0;
207 ResourceMark rm;
208 ReferenceArgumentCount rac(signature);
209 args_count += rac.count();
210 args_count = MIN2(args_count, max);
211 return args_count * per_arg_cell_count;
212 }
213
214 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
215 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
216 assert(TypeStackSlotEntries::per_arg_count() > ReturnTypeEntry::static_cell_count(), "code to test for arguments/results broken");
217 const methodHandle m = stream->method();
218 int bci = stream->bci();
219 Bytecode_invoke inv(m, bci);
220 int args_cell = 0;
221 if (MethodData::profile_arguments_for_invoke(m, bci)) {
222 args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit);
223 }
224 int ret_cell = 0;
225 if (MethodData::profile_return_for_invoke(m, bci) && is_reference_type(inv.result_type())) {
226 ret_cell = ReturnTypeEntry::static_cell_count();
227 }
228 int header_cell = 0;
229 if (args_cell + ret_cell > 0) {
230 header_cell = header_cell_count();
231 }
232
233 return header_cell + args_cell + ret_cell;
234 }
235
236 class ArgumentOffsetComputer : public SignatureIterator {
237 private:
238 int _max;
239 int _offset;
240 GrowableArray<int> _offsets;
241
242 friend class SignatureIterator; // so do_parameters_on can call do_type
243 void do_type(BasicType type) {
244 if (is_reference_type(type) && _offsets.length() < _max) {
245 _offsets.push(_offset);
246 }
247 _offset += parameter_type_word_count(type);
248 }
249
250 public:
251 ArgumentOffsetComputer(Symbol* signature, int max)
252 : SignatureIterator(signature),
253 _max(max), _offset(0),
254 _offsets(max) {
255 do_parameters_on(this); // non-virtual template execution
256 }
257
258 int off_at(int i) const { return _offsets.at(i); }
259 };
260
261 void TypeStackSlotEntries::post_initialize(Symbol* signature, bool has_receiver, bool include_receiver) {
262 ResourceMark rm;
263 int start = 0;
264 // Parameter profiling include the receiver
265 if (include_receiver && has_receiver) {
266 set_stack_slot(0, 0);
267 set_type(0, type_none());
268 start += 1;
269 }
270 ArgumentOffsetComputer aos(signature, _number_of_entries-start);
271 for (int i = start; i < _number_of_entries; i++) {
272 set_stack_slot(i, aos.off_at(i-start) + (has_receiver ? 1 : 0));
273 set_type(i, type_none());
274 }
275 }
276
277 void CallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
278 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
279 Bytecode_invoke inv(stream->method(), stream->bci());
280
281 if (has_arguments()) {
282 #ifdef ASSERT
283 ResourceMark rm;
284 ReferenceArgumentCount rac(inv.signature());
285 int count = MIN2(rac.count(), (int)TypeProfileArgsLimit);
286 assert(count > 0, "room for args type but none found?");
287 check_number_of_arguments(count);
288 #endif
289 _args.post_initialize(inv.signature(), inv.has_receiver(), false);
290 }
291
292 if (has_return()) {
293 assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
294 _ret.post_initialize();
295 }
296 }
297
298 void VirtualCallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
299 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
300 Bytecode_invoke inv(stream->method(), stream->bci());
301
302 if (has_arguments()) {
303 #ifdef ASSERT
304 ResourceMark rm;
305 ReferenceArgumentCount rac(inv.signature());
306 int count = MIN2(rac.count(), (int)TypeProfileArgsLimit);
307 assert(count > 0, "room for args type but none found?");
308 check_number_of_arguments(count);
309 #endif
310 _args.post_initialize(inv.signature(), inv.has_receiver(), false);
311 }
312
313 if (has_return()) {
314 assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?");
315 _ret.post_initialize();
316 }
317 }
318
319 static bool is_excluded(Klass* k) {
320 #if INCLUDE_CDS
321 if (CDSConfig::is_at_aot_safepoint()) {
322 // Check for CDS exclusion only at CDS safe point.
323 if (k->is_instance_klass() && !InstanceKlass::cast(k)->is_loaded()) {
324 log_debug(aot, training)("Purged %s from MDO: unloaded class", k->name()->as_C_string());
325 return true;
326 } else {
327 bool excluded = SystemDictionaryShared::should_be_excluded(k) || !SystemDictionaryShared::is_builtin_loader(k->class_loader_data());
328 if (excluded) {
329 log_debug(aot, training)("Purged %s from MDO: excluded class", k->name()->as_C_string());
330 }
331 return excluded;
332 }
333 }
334 #endif
335 return false;
336 }
337
338 void TypeStackSlotEntries::clean_weak_klass_links(bool always_clean) {
339 for (int i = 0; i < _number_of_entries; i++) {
340 intptr_t p = type(i);
341 Klass* k = (Klass*)klass_part(p);
342 if (k != nullptr) {
343 if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
344 continue; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
345 }
346 if (always_clean || !k->is_loader_present_and_alive() || is_excluded(k)) {
347 set_type(i, with_status((Klass*)nullptr, p));
348 }
349 }
350 }
351 }
352
353 void TypeStackSlotEntries::metaspace_pointers_do(MetaspaceClosure* it) {
354 for (int i = 0; i < _number_of_entries; i++) {
355 Klass** k = (Klass**)type_adr(i); // tagged
356 it->push(k);
357 }
358 }
359
360 void ReturnTypeEntry::clean_weak_klass_links(bool always_clean) {
361 intptr_t p = type();
362 Klass* k = (Klass*)klass_part(p);
363 if (k != nullptr) {
364 if (!always_clean && k->is_instance_klass() && InstanceKlass::cast(k)->is_not_initialized()) {
365 return; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
366 }
367 if (always_clean || !k->is_loader_present_and_alive() || is_excluded(k)) {
368 set_type(with_status((Klass*)nullptr, p));
369 }
370 }
371 }
372
373 void ReturnTypeEntry::metaspace_pointers_do(MetaspaceClosure* it) {
374 Klass** k = (Klass**)type_adr(); // tagged
375 it->push(k);
376 }
377
378 bool TypeEntriesAtCall::return_profiling_enabled() {
379 return MethodData::profile_return();
380 }
381
382 bool TypeEntriesAtCall::arguments_profiling_enabled() {
383 return MethodData::profile_arguments();
384 }
385
386 void TypeEntries::print_klass(outputStream* st, intptr_t k) {
387 if (is_type_none(k)) {
388 st->print("none");
389 } else if (is_type_unknown(k)) {
390 st->print("unknown");
391 } else {
392 valid_klass(k)->print_value_on(st);
393 }
394 if (was_null_seen(k)) {
395 st->print(" (null seen)");
396 }
397 }
398
399 void TypeStackSlotEntries::print_data_on(outputStream* st) const {
400 for (int i = 0; i < _number_of_entries; i++) {
401 _pd->tab(st);
402 st->print("%d: stack(%u) ", i, stack_slot(i));
403 print_klass(st, type(i));
404 st->cr();
405 }
406 }
407
408 void ReturnTypeEntry::print_data_on(outputStream* st) const {
409 _pd->tab(st);
410 print_klass(st, type());
411 st->cr();
412 }
413
414 void CallTypeData::print_data_on(outputStream* st, const char* extra) const {
415 CounterData::print_data_on(st, extra);
416 if (has_arguments()) {
417 tab(st, true);
418 st->print("argument types");
419 _args.print_data_on(st);
420 }
421 if (has_return()) {
422 tab(st, true);
423 st->print("return type");
424 _ret.print_data_on(st);
425 }
426 }
427
428 void VirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
429 VirtualCallData::print_data_on(st, extra);
430 if (has_arguments()) {
431 tab(st, true);
432 st->print("argument types");
433 _args.print_data_on(st);
434 }
435 if (has_return()) {
436 tab(st, true);
437 st->print("return type");
438 _ret.print_data_on(st);
439 }
440 }
441
442 // ==================================================================
443 // ReceiverTypeData
444 //
445 // A ReceiverTypeData is used to access profiling information about a
446 // dynamic type check. It consists of a counter which counts the total times
447 // that the check is reached, and a series of (Klass*, count) pairs
448 // which are used to store a type profile for the receiver of the check.
449
450 void ReceiverTypeData::clean_weak_klass_links(bool always_clean) {
451 for (uint row = 0; row < row_limit(); row++) {
452 Klass* p = receiver(row);
453 if (p != nullptr) {
454 if (!always_clean && p->is_instance_klass() && InstanceKlass::cast(p)->is_not_initialized()) {
455 continue; // skip not-yet-initialized classes // TODO: maybe clear the slot instead?
456 }
457 if (always_clean || !p->is_loader_present_and_alive() || is_excluded(p)) {
458 clear_row(row);
459 }
460 }
461 }
462 }
463
464 void ReceiverTypeData::metaspace_pointers_do(MetaspaceClosure *it) {
465 for (uint row = 0; row < row_limit(); row++) {
466 Klass** recv = (Klass**)intptr_at_adr(receiver_cell_index(row));
467 it->push(recv);
468 }
469 }
470
471 void ReceiverTypeData::print_receiver_data_on(outputStream* st) const {
472 uint row;
473 int entries = 0;
474 for (row = 0; row < row_limit(); row++) {
475 if (receiver(row) != nullptr) entries++;
476 }
477 st->print_cr("count(%u) entries(%u)", count(), entries);
478 int total = count();
479 for (row = 0; row < row_limit(); row++) {
480 if (receiver(row) != nullptr) {
481 total += receiver_count(row);
482 }
483 }
484 for (row = 0; row < row_limit(); row++) {
485 if (receiver(row) != nullptr) {
486 tab(st);
487 receiver(row)->print_value_on(st);
488 st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
489 }
490 }
491 }
492 void ReceiverTypeData::print_data_on(outputStream* st, const char* extra) const {
493 print_shared(st, "ReceiverTypeData", extra);
494 print_receiver_data_on(st);
495 }
496
497 void VirtualCallData::print_data_on(outputStream* st, const char* extra) const {
498 print_shared(st, "VirtualCallData", extra);
499 print_receiver_data_on(st);
500 }
501
502 // ==================================================================
503 // RetData
504 //
505 // A RetData is used to access profiling information for a ret bytecode.
506 // It is composed of a count of the number of times that the ret has
507 // been executed, followed by a series of triples of the form
508 // (bci, count, di) which count the number of times that some bci was the
509 // target of the ret and cache a corresponding displacement.
510
511 void RetData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
512 for (uint row = 0; row < row_limit(); row++) {
513 set_bci_displacement(row, -1);
514 set_bci(row, no_bci);
515 }
516 // release so other threads see a consistent state. bci is used as
517 // a valid flag for bci_displacement.
518 OrderAccess::release();
519 }
520
521 // This routine needs to atomically update the RetData structure, so the
522 // caller needs to hold the RetData_lock before it gets here. Since taking
523 // the lock can block (and allow GC) and since RetData is a ProfileData is a
524 // wrapper around a derived oop, taking the lock in _this_ method will
525 // basically cause the 'this' pointer's _data field to contain junk after the
526 // lock. We require the caller to take the lock before making the ProfileData
527 // structure. Currently the only caller is InterpreterRuntime::update_mdp_for_ret
528 address RetData::fixup_ret(int return_bci, MethodData* h_mdo) {
529 // First find the mdp which corresponds to the return bci.
530 address mdp = h_mdo->bci_to_dp(return_bci);
531
532 // Now check to see if any of the cache slots are open.
533 for (uint row = 0; row < row_limit(); row++) {
534 if (bci(row) == no_bci) {
535 set_bci_displacement(row, checked_cast<int>(mdp - dp()));
536 set_bci_count(row, DataLayout::counter_increment);
537 // Barrier to ensure displacement is written before the bci; allows
538 // the interpreter to read displacement without fear of race condition.
539 release_set_bci(row, return_bci);
540 break;
541 }
542 }
543 return mdp;
544 }
545
546 void RetData::print_data_on(outputStream* st, const char* extra) const {
547 print_shared(st, "RetData", extra);
548 uint row;
549 int entries = 0;
550 for (row = 0; row < row_limit(); row++) {
551 if (bci(row) != no_bci) entries++;
552 }
553 st->print_cr("count(%u) entries(%u)", count(), entries);
554 for (row = 0; row < row_limit(); row++) {
555 if (bci(row) != no_bci) {
556 tab(st);
557 st->print_cr("bci(%d: count(%u) displacement(%d))",
558 bci(row), bci_count(row), bci_displacement(row));
559 }
560 }
561 }
562
563 // ==================================================================
564 // BranchData
565 //
566 // A BranchData is used to access profiling data for a two-way branch.
567 // It consists of taken and not_taken counts as well as a data displacement
568 // for the taken case.
569
570 void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
571 assert(stream->bci() == bci(), "wrong pos");
572 int target = stream->dest();
573 int my_di = mdo->dp_to_di(dp());
574 int target_di = mdo->bci_to_di(target);
575 int offset = target_di - my_di;
576 set_displacement(offset);
577 }
578
579 void BranchData::print_data_on(outputStream* st, const char* extra) const {
580 print_shared(st, "BranchData", extra);
581 st->print_cr("taken(%u) displacement(%d)",
582 taken(), displacement());
583 tab(st);
584 st->print_cr("not taken(%u)", not_taken());
585 }
586
587 // ==================================================================
588 // MultiBranchData
589 //
590 // A MultiBranchData is used to access profiling information for
591 // a multi-way branch (*switch bytecodes). It consists of a series
592 // of (count, displacement) pairs, which count the number of times each
593 // case was taken and specify the data displacement for each branch target.
594
595 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
596 int cell_count = 0;
597 if (stream->code() == Bytecodes::_tableswitch) {
598 Bytecode_tableswitch sw(stream->method()(), stream->bcp());
599 cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
600 } else {
601 Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
602 cell_count = 1 + per_case_cell_count * (sw.number_of_pairs() + 1); // 1 for default
603 }
604 return cell_count;
605 }
606
607 void MultiBranchData::post_initialize(BytecodeStream* stream,
608 MethodData* mdo) {
609 assert(stream->bci() == bci(), "wrong pos");
610 int target;
611 int my_di;
612 int target_di;
613 int offset;
614 if (stream->code() == Bytecodes::_tableswitch) {
615 Bytecode_tableswitch sw(stream->method()(), stream->bcp());
616 int len = sw.length();
617 assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
618 for (int count = 0; count < len; count++) {
619 target = sw.dest_offset_at(count) + bci();
620 my_di = mdo->dp_to_di(dp());
621 target_di = mdo->bci_to_di(target);
622 offset = target_di - my_di;
623 set_displacement_at(count, offset);
624 }
625 target = sw.default_offset() + bci();
626 my_di = mdo->dp_to_di(dp());
627 target_di = mdo->bci_to_di(target);
628 offset = target_di - my_di;
629 set_default_displacement(offset);
630
631 } else {
632 Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
633 int npairs = sw.number_of_pairs();
634 assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
635 for (int count = 0; count < npairs; count++) {
636 LookupswitchPair pair = sw.pair_at(count);
637 target = pair.offset() + bci();
638 my_di = mdo->dp_to_di(dp());
639 target_di = mdo->bci_to_di(target);
640 offset = target_di - my_di;
641 set_displacement_at(count, offset);
642 }
643 target = sw.default_offset() + bci();
644 my_di = mdo->dp_to_di(dp());
645 target_di = mdo->bci_to_di(target);
646 offset = target_di - my_di;
647 set_default_displacement(offset);
648 }
649 }
650
651 void MultiBranchData::print_data_on(outputStream* st, const char* extra) const {
652 print_shared(st, "MultiBranchData", extra);
653 st->print_cr("default_count(%u) displacement(%d)",
654 default_count(), default_displacement());
655 int cases = number_of_cases();
656 for (int i = 0; i < cases; i++) {
657 tab(st);
658 st->print_cr("count(%u) displacement(%d)",
659 count_at(i), displacement_at(i));
660 }
661 }
662
663 void ArgInfoData::print_data_on(outputStream* st, const char* extra) const {
664 print_shared(st, "ArgInfoData", extra);
665 int args_size = size_of_args();
666 for (int i = 0; i < args_size; i++) {
667 st->print(" 0x%x", arg_modified(i));
668 }
669 st->cr();
670 }
671
672 int ParametersTypeData::compute_cell_count(Method* m) {
673 if (!MethodData::profile_parameters_for_method(methodHandle(Thread::current(), m))) {
674 return 0;
675 }
676 int max = TypeProfileParmsLimit == -1 ? INT_MAX : TypeProfileParmsLimit;
677 int obj_args = TypeStackSlotEntries::compute_cell_count(m->signature(), !m->is_static(), max);
678 if (obj_args > 0) {
679 return obj_args + 1; // 1 cell for array len
680 }
681 return 0;
682 }
683
684 void ParametersTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
685 _parameters.post_initialize(mdo->method()->signature(), !mdo->method()->is_static(), true);
686 }
687
688 bool ParametersTypeData::profiling_enabled() {
689 return MethodData::profile_parameters();
690 }
691
692 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
693 print_shared(st, "ParametersTypeData", extra);
694 tab(st);
695 _parameters.print_data_on(st);
696 st->cr();
697 }
698
699 void SpeculativeTrapData::metaspace_pointers_do(MetaspaceClosure* it) {
700 Method** m = (Method**)intptr_at_adr(speculative_trap_method);
701 it->push(m);
702 }
703
704 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
705 print_shared(st, "SpeculativeTrapData", extra);
706 tab(st);
707 method()->print_short_name(st);
708 st->cr();
709 }
710
711 // ==================================================================
712 // MethodData*
713 //
714 // A MethodData* holds information which has been collected about
715 // a method.
716
717 MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) {
718 assert(!THREAD->owns_locks(), "Should not own any locks");
719 int size = MethodData::compute_allocation_size_in_words(method);
720
721 return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD)
722 MethodData(method);
723 }
724
725 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
726 switch (code) {
727 case Bytecodes::_checkcast:
728 case Bytecodes::_instanceof:
729 case Bytecodes::_aastore:
730 if (TypeProfileCasts) {
731 return ReceiverTypeData::static_cell_count();
732 } else {
733 return BitData::static_cell_count();
734 }
735 case Bytecodes::_invokespecial:
736 case Bytecodes::_invokestatic:
737 if (MethodData::profile_arguments() || MethodData::profile_return()) {
738 return variable_cell_count;
739 } else {
740 return CounterData::static_cell_count();
741 }
742 case Bytecodes::_goto:
743 case Bytecodes::_goto_w:
744 case Bytecodes::_jsr:
745 case Bytecodes::_jsr_w:
746 return JumpData::static_cell_count();
747 case Bytecodes::_invokevirtual:
748 case Bytecodes::_invokeinterface:
749 if (MethodData::profile_arguments() || MethodData::profile_return()) {
750 return variable_cell_count;
751 } else {
752 return VirtualCallData::static_cell_count();
753 }
754 case Bytecodes::_invokedynamic:
755 if (MethodData::profile_arguments() || MethodData::profile_return()) {
756 return variable_cell_count;
757 } else {
758 return CounterData::static_cell_count();
759 }
760 case Bytecodes::_ret:
761 return RetData::static_cell_count();
762 case Bytecodes::_ifeq:
763 case Bytecodes::_ifne:
764 case Bytecodes::_iflt:
765 case Bytecodes::_ifge:
766 case Bytecodes::_ifgt:
767 case Bytecodes::_ifle:
768 case Bytecodes::_if_icmpeq:
769 case Bytecodes::_if_icmpne:
770 case Bytecodes::_if_icmplt:
771 case Bytecodes::_if_icmpge:
772 case Bytecodes::_if_icmpgt:
773 case Bytecodes::_if_icmple:
774 case Bytecodes::_if_acmpeq:
775 case Bytecodes::_if_acmpne:
776 case Bytecodes::_ifnull:
777 case Bytecodes::_ifnonnull:
778 return BranchData::static_cell_count();
779 case Bytecodes::_lookupswitch:
780 case Bytecodes::_tableswitch:
781 return variable_cell_count;
782 default:
783 return no_profile_data;
784 }
785 }
786
787 // Compute the size of the profiling information corresponding to
788 // the current bytecode.
789 int MethodData::compute_data_size(BytecodeStream* stream) {
790 int cell_count = bytecode_cell_count(stream->code());
791 if (cell_count == no_profile_data) {
792 return 0;
793 }
794 if (cell_count == variable_cell_count) {
795 switch (stream->code()) {
796 case Bytecodes::_lookupswitch:
797 case Bytecodes::_tableswitch:
798 cell_count = MultiBranchData::compute_cell_count(stream);
799 break;
800 case Bytecodes::_invokespecial:
801 case Bytecodes::_invokestatic:
802 case Bytecodes::_invokedynamic:
803 assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
804 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
805 profile_return_for_invoke(stream->method(), stream->bci())) {
806 cell_count = CallTypeData::compute_cell_count(stream);
807 } else {
808 cell_count = CounterData::static_cell_count();
809 }
810 break;
811 case Bytecodes::_invokevirtual:
812 case Bytecodes::_invokeinterface: {
813 assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
814 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
815 profile_return_for_invoke(stream->method(), stream->bci())) {
816 cell_count = VirtualCallTypeData::compute_cell_count(stream);
817 } else {
818 cell_count = VirtualCallData::static_cell_count();
819 }
820 break;
821 }
822 default:
823 fatal("unexpected bytecode for var length profile data");
824 }
825 }
826 // Note: cell_count might be zero, meaning that there is just
827 // a DataLayout header, with no extra cells.
828 assert(cell_count >= 0, "sanity");
829 return DataLayout::compute_size_in_bytes(cell_count);
830 }
831
832 bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) {
833 // Bytecodes for which we may use speculation
834 switch (code) {
835 case Bytecodes::_checkcast:
836 case Bytecodes::_instanceof:
837 case Bytecodes::_aastore:
838 case Bytecodes::_invokevirtual:
839 case Bytecodes::_invokeinterface:
840 case Bytecodes::_if_acmpeq:
841 case Bytecodes::_if_acmpne:
842 case Bytecodes::_ifnull:
843 case Bytecodes::_ifnonnull:
844 case Bytecodes::_invokestatic:
845 #ifdef COMPILER2
846 if (CompilerConfig::is_c2_enabled()) {
847 return UseTypeSpeculation;
848 }
849 #endif
850 default:
851 return false;
852 }
853 return false;
854 }
855
856 int MethodData::compute_extra_data_count(int data_size, int empty_bc_count, bool needs_speculative_traps) {
857 if (ProfileTraps) {
858 // Assume that up to 3% of BCIs with no MDP will need to allocate one.
859 int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
860 // If the method is large, let the extra BCIs grow numerous (to ~1%).
861 int one_percent_of_data
862 = (uint)data_size / (DataLayout::header_size_in_bytes()*128);
863 if (extra_data_count < one_percent_of_data)
864 extra_data_count = one_percent_of_data;
865 if (extra_data_count > empty_bc_count)
866 extra_data_count = empty_bc_count; // no need for more
867
868 // Make sure we have a minimum number of extra data slots to
869 // allocate SpeculativeTrapData entries. We would want to have one
870 // entry per compilation that inlines this method and for which
871 // some type speculation assumption fails. So the room we need for
872 // the SpeculativeTrapData entries doesn't directly depend on the
873 // size of the method. Because it's hard to estimate, we reserve
874 // space for an arbitrary number of entries.
875 int spec_data_count = (needs_speculative_traps ? SpecTrapLimitExtraEntries : 0) *
876 (SpeculativeTrapData::static_cell_count() + DataLayout::header_size_in_cells());
877
878 return MAX2(extra_data_count, spec_data_count);
879 } else {
880 return 0;
881 }
882 }
883
884 // Compute the size of the MethodData* necessary to store
885 // profiling information about a given method. Size is in bytes.
886 int MethodData::compute_allocation_size_in_bytes(const methodHandle& method) {
887 int data_size = 0;
888 BytecodeStream stream(method);
889 Bytecodes::Code c;
890 int empty_bc_count = 0; // number of bytecodes lacking data
891 bool needs_speculative_traps = false;
892 while ((c = stream.next()) >= 0) {
893 int size_in_bytes = compute_data_size(&stream);
894 data_size += size_in_bytes;
895 if (size_in_bytes == 0) {
896 empty_bc_count += 1;
897 }
898 needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c);
899 }
900 int object_size = in_bytes(data_offset()) + data_size;
901
902 // Add some extra DataLayout cells (at least one) to track stray traps.
903 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps);
904 object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
905
906 // Add a cell to record information about modified arguments.
907 int arg_size = method->size_of_parameters();
908 object_size += DataLayout::compute_size_in_bytes(arg_size+1);
909
910 // Reserve room for an area of the MDO dedicated to profiling of
911 // parameters
912 int args_cell = ParametersTypeData::compute_cell_count(method());
913 if (args_cell > 0) {
914 object_size += DataLayout::compute_size_in_bytes(args_cell);
915 }
916
917 if (ProfileExceptionHandlers && method()->has_exception_handler()) {
918 int num_exception_handlers = method()->exception_table_length();
919 object_size += num_exception_handlers * single_exception_handler_data_size();
920 }
921
922 return object_size;
923 }
924
925 // Compute the size of the MethodData* necessary to store
926 // profiling information about a given method. Size is in words
927 int MethodData::compute_allocation_size_in_words(const methodHandle& method) {
928 int byte_size = compute_allocation_size_in_bytes(method);
929 int word_size = align_up(byte_size, BytesPerWord) / BytesPerWord;
930 return align_metadata_size(word_size);
931 }
932
933 // Initialize an individual data segment. Returns the size of
934 // the segment in bytes.
935 int MethodData::initialize_data(BytecodeStream* stream,
936 int data_index) {
937 int cell_count = -1;
938 u1 tag = DataLayout::no_tag;
939 DataLayout* data_layout = data_layout_at(data_index);
940 Bytecodes::Code c = stream->code();
941 switch (c) {
942 case Bytecodes::_checkcast:
943 case Bytecodes::_instanceof:
944 case Bytecodes::_aastore:
945 if (TypeProfileCasts) {
946 cell_count = ReceiverTypeData::static_cell_count();
947 tag = DataLayout::receiver_type_data_tag;
948 } else {
949 cell_count = BitData::static_cell_count();
950 tag = DataLayout::bit_data_tag;
951 }
952 break;
953 case Bytecodes::_invokespecial:
954 case Bytecodes::_invokestatic: {
955 int counter_data_cell_count = CounterData::static_cell_count();
956 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
957 profile_return_for_invoke(stream->method(), stream->bci())) {
958 cell_count = CallTypeData::compute_cell_count(stream);
959 } else {
960 cell_count = counter_data_cell_count;
961 }
962 if (cell_count > counter_data_cell_count) {
963 tag = DataLayout::call_type_data_tag;
964 } else {
965 tag = DataLayout::counter_data_tag;
966 }
967 break;
968 }
969 case Bytecodes::_goto:
970 case Bytecodes::_goto_w:
971 case Bytecodes::_jsr:
972 case Bytecodes::_jsr_w:
973 cell_count = JumpData::static_cell_count();
974 tag = DataLayout::jump_data_tag;
975 break;
976 case Bytecodes::_invokevirtual:
977 case Bytecodes::_invokeinterface: {
978 int virtual_call_data_cell_count = VirtualCallData::static_cell_count();
979 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
980 profile_return_for_invoke(stream->method(), stream->bci())) {
981 cell_count = VirtualCallTypeData::compute_cell_count(stream);
982 } else {
983 cell_count = virtual_call_data_cell_count;
984 }
985 if (cell_count > virtual_call_data_cell_count) {
986 tag = DataLayout::virtual_call_type_data_tag;
987 } else {
988 tag = DataLayout::virtual_call_data_tag;
989 }
990 break;
991 }
992 case Bytecodes::_invokedynamic: {
993 // %%% should make a type profile for any invokedynamic that takes a ref argument
994 int counter_data_cell_count = CounterData::static_cell_count();
995 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
996 profile_return_for_invoke(stream->method(), stream->bci())) {
997 cell_count = CallTypeData::compute_cell_count(stream);
998 } else {
999 cell_count = counter_data_cell_count;
1000 }
1001 if (cell_count > counter_data_cell_count) {
1002 tag = DataLayout::call_type_data_tag;
1003 } else {
1004 tag = DataLayout::counter_data_tag;
1005 }
1006 break;
1007 }
1008 case Bytecodes::_ret:
1009 cell_count = RetData::static_cell_count();
1010 tag = DataLayout::ret_data_tag;
1011 break;
1012 case Bytecodes::_ifeq:
1013 case Bytecodes::_ifne:
1014 case Bytecodes::_iflt:
1015 case Bytecodes::_ifge:
1016 case Bytecodes::_ifgt:
1017 case Bytecodes::_ifle:
1018 case Bytecodes::_if_icmpeq:
1019 case Bytecodes::_if_icmpne:
1020 case Bytecodes::_if_icmplt:
1021 case Bytecodes::_if_icmpge:
1022 case Bytecodes::_if_icmpgt:
1023 case Bytecodes::_if_icmple:
1024 case Bytecodes::_if_acmpeq:
1025 case Bytecodes::_if_acmpne:
1026 case Bytecodes::_ifnull:
1027 case Bytecodes::_ifnonnull:
1028 cell_count = BranchData::static_cell_count();
1029 tag = DataLayout::branch_data_tag;
1030 break;
1031 case Bytecodes::_lookupswitch:
1032 case Bytecodes::_tableswitch:
1033 cell_count = MultiBranchData::compute_cell_count(stream);
1034 tag = DataLayout::multi_branch_data_tag;
1035 break;
1036 default:
1037 break;
1038 }
1039 assert(tag == DataLayout::multi_branch_data_tag ||
1040 ((MethodData::profile_arguments() || MethodData::profile_return()) &&
1041 (tag == DataLayout::call_type_data_tag ||
1042 tag == DataLayout::counter_data_tag ||
1043 tag == DataLayout::virtual_call_type_data_tag ||
1044 tag == DataLayout::virtual_call_data_tag)) ||
1045 cell_count == bytecode_cell_count(c), "cell counts must agree");
1046 if (cell_count >= 0) {
1047 assert(tag != DataLayout::no_tag, "bad tag");
1048 assert(bytecode_has_profile(c), "agree w/ BHP");
1049 data_layout->initialize(tag, checked_cast<u2>(stream->bci()), cell_count);
1050 return DataLayout::compute_size_in_bytes(cell_count);
1051 } else {
1052 assert(!bytecode_has_profile(c), "agree w/ !BHP");
1053 return 0;
1054 }
1055 }
1056
1057 // Get the data at an arbitrary (sort of) data index.
1058 ProfileData* MethodData::data_at(int data_index) const {
1059 if (out_of_bounds(data_index)) {
1060 return nullptr;
1061 }
1062 DataLayout* data_layout = data_layout_at(data_index);
1063 return data_layout->data_in();
1064 }
1065
1066 int DataLayout::cell_count() {
1067 switch (tag()) {
1068 case DataLayout::no_tag:
1069 default:
1070 ShouldNotReachHere();
1071 return 0;
1072 case DataLayout::bit_data_tag:
1073 return BitData::static_cell_count();
1074 case DataLayout::counter_data_tag:
1075 return CounterData::static_cell_count();
1076 case DataLayout::jump_data_tag:
1077 return JumpData::static_cell_count();
1078 case DataLayout::receiver_type_data_tag:
1079 return ReceiverTypeData::static_cell_count();
1080 case DataLayout::virtual_call_data_tag:
1081 return VirtualCallData::static_cell_count();
1082 case DataLayout::ret_data_tag:
1083 return RetData::static_cell_count();
1084 case DataLayout::branch_data_tag:
1085 return BranchData::static_cell_count();
1086 case DataLayout::multi_branch_data_tag:
1087 return ((new MultiBranchData(this))->cell_count());
1088 case DataLayout::arg_info_data_tag:
1089 return ((new ArgInfoData(this))->cell_count());
1090 case DataLayout::call_type_data_tag:
1091 return ((new CallTypeData(this))->cell_count());
1092 case DataLayout::virtual_call_type_data_tag:
1093 return ((new VirtualCallTypeData(this))->cell_count());
1094 case DataLayout::parameters_type_data_tag:
1095 return ((new ParametersTypeData(this))->cell_count());
1096 case DataLayout::speculative_trap_data_tag:
1097 return SpeculativeTrapData::static_cell_count();
1098 }
1099 }
1100 ProfileData* DataLayout::data_in() {
1101 switch (tag()) {
1102 case DataLayout::no_tag:
1103 default:
1104 ShouldNotReachHere();
1105 return nullptr;
1106 case DataLayout::bit_data_tag:
1107 return new BitData(this);
1108 case DataLayout::counter_data_tag:
1109 return new CounterData(this);
1110 case DataLayout::jump_data_tag:
1111 return new JumpData(this);
1112 case DataLayout::receiver_type_data_tag:
1113 return new ReceiverTypeData(this);
1114 case DataLayout::virtual_call_data_tag:
1115 return new VirtualCallData(this);
1116 case DataLayout::ret_data_tag:
1117 return new RetData(this);
1118 case DataLayout::branch_data_tag:
1119 return new BranchData(this);
1120 case DataLayout::multi_branch_data_tag:
1121 return new MultiBranchData(this);
1122 case DataLayout::arg_info_data_tag:
1123 return new ArgInfoData(this);
1124 case DataLayout::call_type_data_tag:
1125 return new CallTypeData(this);
1126 case DataLayout::virtual_call_type_data_tag:
1127 return new VirtualCallTypeData(this);
1128 case DataLayout::parameters_type_data_tag:
1129 return new ParametersTypeData(this);
1130 case DataLayout::speculative_trap_data_tag:
1131 return new SpeculativeTrapData(this);
1132 }
1133 }
1134
1135 // Iteration over data.
1136 ProfileData* MethodData::next_data(ProfileData* current) const {
1137 int current_index = dp_to_di(current->dp());
1138 int next_index = current_index + current->size_in_bytes();
1139 ProfileData* next = data_at(next_index);
1140 return next;
1141 }
1142
1143 DataLayout* MethodData::next_data_layout(DataLayout* current) const {
1144 int current_index = dp_to_di((address)current);
1145 int next_index = current_index + current->size_in_bytes();
1146 if (out_of_bounds(next_index)) {
1147 return nullptr;
1148 }
1149 DataLayout* next = data_layout_at(next_index);
1150 return next;
1151 }
1152
1153 // Give each of the data entries a chance to perform specific
1154 // data initialization.
1155 void MethodData::post_initialize(BytecodeStream* stream) {
1156 ResourceMark rm;
1157 ProfileData* data;
1158 for (data = first_data(); is_valid(data); data = next_data(data)) {
1159 stream->set_start(data->bci());
1160 stream->next();
1161 data->post_initialize(stream, this);
1162 }
1163 if (_parameters_type_data_di != no_parameters) {
1164 parameters_type_data()->post_initialize(nullptr, this);
1165 }
1166 }
1167
1168 // Initialize the MethodData* corresponding to a given method.
1169 MethodData::MethodData(const methodHandle& method)
1170 : _method(method()),
1171 // Holds Compile_lock
1172 _compiler_counters(),
1173 _parameters_type_data_di(parameters_uninitialized) {
1174 _extra_data_lock = nullptr;
1175 initialize();
1176 }
1177
1178 #if INCLUDE_CDS
1179 MethodData::MethodData() {
1180 // Used by cppVtables.cpp only
1181 assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
1182 }
1183 #endif
1184
1185 void MethodData::initialize() {
1186 Thread* thread = Thread::current();
1187 NoSafepointVerifier no_safepoint; // init function atomic wrt GC
1188 ResourceMark rm(thread);
1189
1190 init();
1191
1192 // Go through the bytecodes and allocate and initialize the
1193 // corresponding data cells.
1194 int data_size = 0;
1195 int empty_bc_count = 0; // number of bytecodes lacking data
1196 _data[0] = 0; // apparently not set below.
1197 BytecodeStream stream(methodHandle(thread, method()));
1198 Bytecodes::Code c;
1199 bool needs_speculative_traps = false;
1200 while ((c = stream.next()) >= 0) {
1201 int size_in_bytes = initialize_data(&stream, data_size);
1202 data_size += size_in_bytes;
1203 if (size_in_bytes == 0) {
1204 empty_bc_count += 1;
1205 }
1206 needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c);
1207 }
1208 _data_size = data_size;
1209 int object_size = in_bytes(data_offset()) + data_size;
1210
1211 // Add some extra DataLayout cells (at least one) to track stray traps.
1212 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps);
1213 int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
1214
1215 // Let's zero the space for the extra data
1216 if (extra_size > 0) {
1217 Copy::zero_to_bytes(((address)_data) + data_size, extra_size);
1218 }
1219
1220 // Add a cell to record information about modified arguments.
1221 // Set up _args_modified array after traps cells so that
1222 // the code for traps cells works.
1223 DataLayout *dp = data_layout_at(data_size + extra_size);
1224
1225 int arg_size = method()->size_of_parameters();
1226 dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
1227
1228 int arg_data_size = DataLayout::compute_size_in_bytes(arg_size+1);
1229 object_size += extra_size + arg_data_size;
1230
1231 int parms_cell = ParametersTypeData::compute_cell_count(method());
1232 // If we are profiling parameters, we reserved an area near the end
1233 // of the MDO after the slots for bytecodes (because there's no bci
1234 // for method entry so they don't fit with the framework for the
1235 // profiling of bytecodes). We store the offset within the MDO of
1236 // this area (or -1 if no parameter is profiled)
1237 int parm_data_size = 0;
1238 if (parms_cell > 0) {
1239 parm_data_size = DataLayout::compute_size_in_bytes(parms_cell);
1240 object_size += parm_data_size;
1241 _parameters_type_data_di = data_size + extra_size + arg_data_size;
1242 DataLayout *dp = data_layout_at(data_size + extra_size + arg_data_size);
1243 dp->initialize(DataLayout::parameters_type_data_tag, 0, parms_cell);
1244 } else {
1245 _parameters_type_data_di = no_parameters;
1246 }
1247
1248 _exception_handler_data_di = data_size + extra_size + arg_data_size + parm_data_size;
1249 if (ProfileExceptionHandlers && method()->has_exception_handler()) {
1250 int num_exception_handlers = method()->exception_table_length();
1251 object_size += num_exception_handlers * single_exception_handler_data_size();
1252 ExceptionTableElement* exception_handlers = method()->exception_table_start();
1253 for (int i = 0; i < num_exception_handlers; i++) {
1254 DataLayout *dp = exception_handler_data_at(i);
1255 dp->initialize(DataLayout::bit_data_tag, exception_handlers[i].handler_pc, single_exception_handler_data_cell_count());
1256 }
1257 }
1258
1259 // Set an initial hint. Don't use set_hint_di() because
1260 // first_di() may be out of bounds if data_size is 0.
1261 // In that situation, _hint_di is never used, but at
1262 // least well-defined.
1263 _hint_di = first_di();
1264
1265 post_initialize(&stream);
1266
1267 assert(object_size == compute_allocation_size_in_bytes(methodHandle(thread, _method)), "MethodData: computed size != initialized size");
1268 set_size(object_size);
1269 }
1270
1271 void MethodData::init() {
1272 _compiler_counters = CompilerCounters(); // reset compiler counters
1273 _invocation_counter.init();
1274 _backedge_counter.init();
1275 _invocation_counter_start = 0;
1276 _backedge_counter_start = 0;
1277
1278 // Set per-method invoke- and backedge mask.
1279 double scale = 1.0;
1280 methodHandle mh(Thread::current(), _method);
1281 CompilerOracle::has_option_value(mh, CompileCommandEnum::CompileThresholdScaling, scale);
1282 _invoke_mask = (int)right_n_bits(CompilerConfig::scaled_freq_log(Tier0InvokeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
1283 _backedge_mask = (int)right_n_bits(CompilerConfig::scaled_freq_log(Tier0BackedgeNotifyFreqLog, scale)) << InvocationCounter::count_shift;
1284
1285 _tenure_traps = 0;
1286 _num_loops = 0;
1287 _num_blocks = 0;
1288 _would_profile = unknown;
1289
1290 // Initialize escape flags.
1291 clear_escape_info();
1292 }
1293
1294 bool MethodData::is_mature() const {
1295 return CompilationPolicy::is_mature(const_cast<MethodData*>(this));
1296 }
1297
1298 // Translate a bci to its corresponding data index (di).
1299 address MethodData::bci_to_dp(int bci) {
1300 ResourceMark rm;
1301 DataLayout* data = data_layout_before(bci);
1302 DataLayout* prev = nullptr;
1303 for ( ; is_valid(data); data = next_data_layout(data)) {
1304 if (data->bci() >= bci) {
1305 if (data->bci() == bci) set_hint_di(dp_to_di((address)data));
1306 else if (prev != nullptr) set_hint_di(dp_to_di((address)prev));
1307 return (address)data;
1308 }
1309 prev = data;
1310 }
1311 return (address)limit_data_position();
1312 }
1313
1314 // Translate a bci to its corresponding data, or null.
1315 ProfileData* MethodData::bci_to_data(int bci) {
1316 check_extra_data_locked();
1317
1318 DataLayout* data = data_layout_before(bci);
1319 for ( ; is_valid(data); data = next_data_layout(data)) {
1320 if (data->bci() == bci) {
1321 set_hint_di(dp_to_di((address)data));
1322 return data->data_in();
1323 } else if (data->bci() > bci) {
1324 break;
1325 }
1326 }
1327 return bci_to_extra_data(bci, nullptr, false);
1328 }
1329
1330 DataLayout* MethodData::exception_handler_bci_to_data_helper(int bci) {
1331 assert(ProfileExceptionHandlers, "not profiling");
1332 for (int i = 0; i < num_exception_handler_data(); i++) {
1333 DataLayout* exception_handler_data = exception_handler_data_at(i);
1334 if (exception_handler_data->bci() == bci) {
1335 return exception_handler_data;
1336 }
1337 }
1338 return nullptr;
1339 }
1340
1341 BitData* MethodData::exception_handler_bci_to_data_or_null(int bci) {
1342 DataLayout* data = exception_handler_bci_to_data_helper(bci);
1343 return data != nullptr ? new BitData(data) : nullptr;
1344 }
1345
1346 BitData MethodData::exception_handler_bci_to_data(int bci) {
1347 DataLayout* data = exception_handler_bci_to_data_helper(bci);
1348 assert(data != nullptr, "invalid bci");
1349 return BitData(data);
1350 }
1351
1352 DataLayout* MethodData::next_extra(DataLayout* dp) {
1353 int nb_cells = 0;
1354 switch(dp->tag()) {
1355 case DataLayout::bit_data_tag:
1356 case DataLayout::no_tag:
1357 nb_cells = BitData::static_cell_count();
1358 break;
1359 case DataLayout::speculative_trap_data_tag:
1360 nb_cells = SpeculativeTrapData::static_cell_count();
1361 break;
1362 default:
1363 fatal("unexpected tag %d", dp->tag());
1364 }
1365 return (DataLayout*)((address)dp + DataLayout::compute_size_in_bytes(nb_cells));
1366 }
1367
1368 ProfileData* MethodData::bci_to_extra_data_find(int bci, Method* m, DataLayout*& dp) {
1369 check_extra_data_locked();
1370
1371 DataLayout* end = args_data_limit();
1372
1373 for (;; dp = next_extra(dp)) {
1374 assert(dp < end, "moved past end of extra data");
1375 // No need for "AtomicAccess::load_acquire" ops,
1376 // since the data structure is monotonic.
1377 switch(dp->tag()) {
1378 case DataLayout::no_tag:
1379 return nullptr;
1380 case DataLayout::arg_info_data_tag:
1381 dp = end;
1382 return nullptr; // ArgInfoData is at the end of extra data section.
1383 case DataLayout::bit_data_tag:
1384 if (m == nullptr && dp->bci() == bci) {
1385 return new BitData(dp);
1386 }
1387 break;
1388 case DataLayout::speculative_trap_data_tag:
1389 if (m != nullptr) {
1390 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1391 if (dp->bci() == bci) {
1392 assert(data->method() != nullptr, "method must be set");
1393 if (data->method() == m) {
1394 return data;
1395 }
1396 }
1397 }
1398 break;
1399 default:
1400 fatal("unexpected tag %d", dp->tag());
1401 }
1402 }
1403 return nullptr;
1404 }
1405
1406
1407 // Translate a bci to its corresponding extra data, or null.
1408 ProfileData* MethodData::bci_to_extra_data(int bci, Method* m, bool create_if_missing) {
1409 check_extra_data_locked();
1410
1411 // This code assumes an entry for a SpeculativeTrapData is 2 cells
1412 assert(2*DataLayout::compute_size_in_bytes(BitData::static_cell_count()) ==
1413 DataLayout::compute_size_in_bytes(SpeculativeTrapData::static_cell_count()),
1414 "code needs to be adjusted");
1415
1416 // Do not create one of these if method has been redefined.
1417 if (m != nullptr && m->is_old()) {
1418 return nullptr;
1419 }
1420
1421 DataLayout* dp = extra_data_base();
1422 DataLayout* end = args_data_limit();
1423
1424 // Find if already exists
1425 ProfileData* result = bci_to_extra_data_find(bci, m, dp);
1426 if (result != nullptr || dp >= end) {
1427 return result;
1428 }
1429
1430 if (create_if_missing) {
1431 // Not found -> Allocate
1432 assert(dp->tag() == DataLayout::no_tag || (dp->tag() == DataLayout::speculative_trap_data_tag && m != nullptr), "should be free");
1433 assert(next_extra(dp)->tag() == DataLayout::no_tag || next_extra(dp)->tag() == DataLayout::arg_info_data_tag, "should be free or arg info");
1434 u1 tag = m == nullptr ? DataLayout::bit_data_tag : DataLayout::speculative_trap_data_tag;
1435 // SpeculativeTrapData is 2 slots. Make sure we have room.
1436 if (m != nullptr && next_extra(dp)->tag() != DataLayout::no_tag) {
1437 return nullptr;
1438 }
1439 DataLayout temp;
1440 temp.initialize(tag, checked_cast<u2>(bci), 0);
1441
1442 dp->set_header(temp.header());
1443 assert(dp->tag() == tag, "sane");
1444 assert(dp->bci() == bci, "no concurrent allocation");
1445 if (tag == DataLayout::bit_data_tag) {
1446 return new BitData(dp);
1447 } else {
1448 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1449 data->set_method(m);
1450 return data;
1451 }
1452 }
1453 return nullptr;
1454 }
1455
1456 ArgInfoData *MethodData::arg_info() {
1457 DataLayout* dp = extra_data_base();
1458 DataLayout* end = args_data_limit();
1459 for (; dp < end; dp = next_extra(dp)) {
1460 if (dp->tag() == DataLayout::arg_info_data_tag)
1461 return new ArgInfoData(dp);
1462 }
1463 return nullptr;
1464 }
1465
1466 // Printing
1467
1468 void MethodData::print_on(outputStream* st) const {
1469 assert(is_methodData(), "should be method data");
1470 st->print("method data for ");
1471 method()->print_value_on(st);
1472 st->cr();
1473 print_data_on(st);
1474 }
1475
1476 void MethodData::print_value_on(outputStream* st) const {
1477 assert(is_methodData(), "should be method data");
1478 st->print("method data for ");
1479 method()->print_value_on(st);
1480 }
1481
1482 void MethodData::print_data_on(outputStream* st) const {
1483 Mutex* lock = const_cast<MethodData*>(this)->extra_data_lock();
1484 ConditionalMutexLocker ml(lock, !lock->owned_by_self(),
1485 Mutex::_no_safepoint_check_flag);
1486 ResourceMark rm;
1487 ProfileData* data = first_data();
1488 if (_parameters_type_data_di != no_parameters) {
1489 parameters_type_data()->print_data_on(st);
1490 }
1491 for ( ; is_valid(data); data = next_data(data)) {
1492 st->print("%d", dp_to_di(data->dp()));
1493 st->fill_to(6);
1494 data->print_data_on(st, this);
1495 }
1496
1497 st->print_cr("--- Extra data:");
1498 DataLayout* dp = extra_data_base();
1499 DataLayout* end = args_data_limit();
1500 for (;; dp = next_extra(dp)) {
1501 assert(dp < end, "moved past end of extra data");
1502 // No need for "AtomicAccess::load_acquire" ops,
1503 // since the data structure is monotonic.
1504 switch(dp->tag()) {
1505 case DataLayout::no_tag:
1506 continue;
1507 case DataLayout::bit_data_tag:
1508 data = new BitData(dp);
1509 break;
1510 case DataLayout::speculative_trap_data_tag:
1511 data = new SpeculativeTrapData(dp);
1512 break;
1513 case DataLayout::arg_info_data_tag:
1514 data = new ArgInfoData(dp);
1515 dp = end; // ArgInfoData is at the end of extra data section.
1516 break;
1517 default:
1518 fatal("unexpected tag %d", dp->tag());
1519 }
1520 st->print("%d", dp_to_di(data->dp()));
1521 st->fill_to(6);
1522 data->print_data_on(st);
1523 if (dp >= end) return;
1524 }
1525 }
1526
1527 // Verification
1528
1529 void MethodData::verify_on(outputStream* st) {
1530 guarantee(is_methodData(), "object must be method data");
1531 // guarantee(m->is_perm(), "should be in permspace");
1532 this->verify_data_on(st);
1533 }
1534
1535 void MethodData::verify_data_on(outputStream* st) {
1536 NEEDS_CLEANUP;
1537 // not yet implemented.
1538 }
1539
1540 bool MethodData::profile_jsr292(const methodHandle& m, int bci) {
1541 if (m->is_compiled_lambda_form()) {
1542 return true;
1543 }
1544
1545 Bytecode_invoke inv(m , bci);
1546 return inv.is_invokedynamic() || inv.is_invokehandle();
1547 }
1548
1549 bool MethodData::profile_unsafe(const methodHandle& m, int bci) {
1550 Bytecode_invoke inv(m , bci);
1551 if (inv.is_invokevirtual()) {
1552 Symbol* klass = inv.klass();
1553 if (klass == vmSymbols::jdk_internal_misc_Unsafe() ||
1554 klass == vmSymbols::sun_misc_Unsafe() ||
1555 klass == vmSymbols::jdk_internal_misc_ScopedMemoryAccess()) {
1556 Symbol* name = inv.name();
1557 if (name->starts_with("get") || name->starts_with("put")) {
1558 return true;
1559 }
1560 }
1561 }
1562 return false;
1563 }
1564
1565 int MethodData::profile_arguments_flag() {
1566 return TypeProfileLevel % 10;
1567 }
1568
1569 bool MethodData::profile_arguments() {
1570 return profile_arguments_flag() > no_type_profile && profile_arguments_flag() <= type_profile_all && TypeProfileArgsLimit > 0;
1571 }
1572
1573 bool MethodData::profile_arguments_jsr292_only() {
1574 return profile_arguments_flag() == type_profile_jsr292;
1575 }
1576
1577 bool MethodData::profile_all_arguments() {
1578 return profile_arguments_flag() == type_profile_all;
1579 }
1580
1581 bool MethodData::profile_arguments_for_invoke(const methodHandle& m, int bci) {
1582 if (!profile_arguments()) {
1583 return false;
1584 }
1585
1586 if (profile_all_arguments()) {
1587 return true;
1588 }
1589
1590 if (profile_unsafe(m, bci)) {
1591 return true;
1592 }
1593
1594 assert(profile_arguments_jsr292_only(), "inconsistent");
1595 return profile_jsr292(m, bci);
1596 }
1597
1598 int MethodData::profile_return_flag() {
1599 return (TypeProfileLevel % 100) / 10;
1600 }
1601
1602 bool MethodData::profile_return() {
1603 return profile_return_flag() > no_type_profile && profile_return_flag() <= type_profile_all;
1604 }
1605
1606 bool MethodData::profile_return_jsr292_only() {
1607 return profile_return_flag() == type_profile_jsr292;
1608 }
1609
1610 bool MethodData::profile_all_return() {
1611 return profile_return_flag() == type_profile_all;
1612 }
1613
1614 bool MethodData::profile_return_for_invoke(const methodHandle& m, int bci) {
1615 if (!profile_return()) {
1616 return false;
1617 }
1618
1619 if (profile_all_return()) {
1620 return true;
1621 }
1622
1623 assert(profile_return_jsr292_only(), "inconsistent");
1624 return profile_jsr292(m, bci);
1625 }
1626
1627 int MethodData::profile_parameters_flag() {
1628 return TypeProfileLevel / 100;
1629 }
1630
1631 bool MethodData::profile_parameters() {
1632 return profile_parameters_flag() > no_type_profile && profile_parameters_flag() <= type_profile_all;
1633 }
1634
1635 bool MethodData::profile_parameters_jsr292_only() {
1636 return profile_parameters_flag() == type_profile_jsr292;
1637 }
1638
1639 bool MethodData::profile_all_parameters() {
1640 return profile_parameters_flag() == type_profile_all;
1641 }
1642
1643 bool MethodData::profile_parameters_for_method(const methodHandle& m) {
1644 if (!profile_parameters()) {
1645 return false;
1646 }
1647
1648 if (profile_all_parameters()) {
1649 return true;
1650 }
1651
1652 assert(profile_parameters_jsr292_only(), "inconsistent");
1653 return m->is_compiled_lambda_form();
1654 }
1655
1656 void MethodData::metaspace_pointers_do(MetaspaceClosure* it) {
1657 log_trace(aot, training)("Iter(MethodData): %p for %p %s", this, _method, _method->name_and_sig_as_C_string());
1658 it->push(&_method);
1659 if (_parameters_type_data_di != no_parameters) {
1660 parameters_type_data()->metaspace_pointers_do(it);
1661 }
1662 for (ProfileData* data = first_data(); is_valid(data); data = next_data(data)) {
1663 data->metaspace_pointers_do(it);
1664 }
1665 for (DataLayout* dp = extra_data_base();
1666 dp < extra_data_limit();
1667 dp = MethodData::next_extra(dp)) {
1668 if (dp->tag() == DataLayout::speculative_trap_data_tag) {
1669 ResourceMark rm;
1670 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1671 data->metaspace_pointers_do(it);
1672 } else if (dp->tag() == DataLayout::no_tag ||
1673 dp->tag() == DataLayout::arg_info_data_tag) {
1674 break;
1675 }
1676 }
1677 }
1678
1679 void MethodData::clean_extra_data_helper(DataLayout* dp, int shift, bool reset) {
1680 check_extra_data_locked();
1681
1682 if (shift == 0) {
1683 return;
1684 }
1685 if (!reset) {
1686 // Move all cells of trap entry at dp left by "shift" cells
1687 intptr_t* start = (intptr_t*)dp;
1688 intptr_t* end = (intptr_t*)next_extra(dp);
1689 for (intptr_t* ptr = start; ptr < end; ptr++) {
1690 *(ptr-shift) = *ptr;
1691 }
1692 } else {
1693 // Reset "shift" cells stopping at dp
1694 intptr_t* start = ((intptr_t*)dp) - shift;
1695 intptr_t* end = (intptr_t*)dp;
1696 for (intptr_t* ptr = start; ptr < end; ptr++) {
1697 *ptr = 0;
1698 }
1699 }
1700 }
1701
1702 // Check for entries that reference an unloaded method
1703 class CleanExtraDataKlassClosure : public CleanExtraDataClosure {
1704 bool _always_clean;
1705 public:
1706 CleanExtraDataKlassClosure(bool always_clean) : _always_clean(always_clean) {}
1707 bool is_live(Method* m) {
1708 if (!_always_clean && m->method_holder()->is_instance_klass() && InstanceKlass::cast(m->method_holder())->is_not_initialized()) {
1709 return true; // TODO: treat as unloaded instead?
1710 }
1711 return !(_always_clean) && m->method_holder()->is_loader_alive();
1712 }
1713 };
1714
1715 // Check for entries that reference a redefined method
1716 class CleanExtraDataMethodClosure : public CleanExtraDataClosure {
1717 public:
1718 CleanExtraDataMethodClosure() {}
1719 bool is_live(Method* m) { return !m->is_old(); }
1720 };
1721
1722 Mutex* MethodData::extra_data_lock() {
1723 Mutex* lock = AtomicAccess::load_acquire(&_extra_data_lock);
1724 if (lock == nullptr) {
1725 // This lock could be acquired while we are holding DumpTimeTable_lock/nosafepoint
1726 lock = new Mutex(Mutex::nosafepoint-1, "MDOExtraData_lock");
1727 Mutex* old = AtomicAccess::cmpxchg(&_extra_data_lock, (Mutex*)nullptr, lock);
1728 if (old != nullptr) {
1729 // Another thread created the lock before us. Use that lock instead.
1730 delete lock;
1731 return old;
1732 }
1733 }
1734 return lock;
1735 }
1736
1737 // Remove SpeculativeTrapData entries that reference an unloaded or
1738 // redefined method
1739 void MethodData::clean_extra_data(CleanExtraDataClosure* cl) {
1740 check_extra_data_locked();
1741
1742 DataLayout* dp = extra_data_base();
1743 DataLayout* end = args_data_limit();
1744
1745 int shift = 0;
1746 for (; dp < end; dp = next_extra(dp)) {
1747 switch(dp->tag()) {
1748 case DataLayout::speculative_trap_data_tag: {
1749 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1750 Method* m = data->method();
1751 assert(m != nullptr, "should have a method");
1752 if (is_excluded(m->method_holder()) || !cl->is_live(m)) {
1753 // "shift" accumulates the number of cells for dead
1754 // SpeculativeTrapData entries that have been seen so
1755 // far. Following entries must be shifted left by that many
1756 // cells to remove the dead SpeculativeTrapData entries.
1757 shift += (int)((intptr_t*)next_extra(dp) - (intptr_t*)dp);
1758 } else {
1759 // Shift this entry left if it follows dead
1760 // SpeculativeTrapData entries
1761 clean_extra_data_helper(dp, shift);
1762 }
1763 break;
1764 }
1765 case DataLayout::bit_data_tag:
1766 // Shift this entry left if it follows dead SpeculativeTrapData
1767 // entries
1768 clean_extra_data_helper(dp, shift);
1769 continue;
1770 case DataLayout::no_tag:
1771 case DataLayout::arg_info_data_tag:
1772 // We are at end of the live trap entries. The previous "shift"
1773 // cells contain entries that are either dead or were shifted
1774 // left. They need to be reset to no_tag
1775 clean_extra_data_helper(dp, shift, true);
1776 return;
1777 default:
1778 fatal("unexpected tag %d", dp->tag());
1779 }
1780 }
1781 }
1782
1783 // Verify there's no unloaded or redefined method referenced by a
1784 // SpeculativeTrapData entry
1785 void MethodData::verify_extra_data_clean(CleanExtraDataClosure* cl) {
1786 check_extra_data_locked();
1787
1788 #ifdef ASSERT
1789 DataLayout* dp = extra_data_base();
1790 DataLayout* end = args_data_limit();
1791
1792 for (; dp < end; dp = next_extra(dp)) {
1793 switch(dp->tag()) {
1794 case DataLayout::speculative_trap_data_tag: {
1795 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1796 Method* m = data->method();
1797 assert(m != nullptr && cl->is_live(m), "Method should exist");
1798 break;
1799 }
1800 case DataLayout::bit_data_tag:
1801 continue;
1802 case DataLayout::no_tag:
1803 case DataLayout::arg_info_data_tag:
1804 return;
1805 default:
1806 fatal("unexpected tag %d", dp->tag());
1807 }
1808 }
1809 #endif
1810 }
1811
1812 void MethodData::clean_method_data(bool always_clean) {
1813 ResourceMark rm;
1814 for (ProfileData* data = first_data();
1815 is_valid(data);
1816 data = next_data(data)) {
1817 data->clean_weak_klass_links(always_clean);
1818 }
1819 ParametersTypeData* parameters = parameters_type_data();
1820 if (parameters != nullptr) {
1821 parameters->clean_weak_klass_links(always_clean);
1822 }
1823
1824 CleanExtraDataKlassClosure cl(always_clean);
1825
1826 // Lock to modify extra data, and prevent Safepoint from breaking the lock
1827 MutexLocker ml(extra_data_lock(), Mutex::_no_safepoint_check_flag);
1828
1829 clean_extra_data(&cl);
1830 verify_extra_data_clean(&cl);
1831 }
1832
1833 // This is called during redefinition to clean all "old" redefined
1834 // methods out of MethodData for all methods.
1835 void MethodData::clean_weak_method_links() {
1836 ResourceMark rm;
1837 CleanExtraDataMethodClosure cl;
1838
1839 // Lock to modify extra data, and prevent Safepoint from breaking the lock
1840 MutexLocker ml(extra_data_lock(), Mutex::_no_safepoint_check_flag);
1841
1842 clean_extra_data(&cl);
1843 verify_extra_data_clean(&cl);
1844 }
1845
1846 void MethodData::deallocate_contents(ClassLoaderData* loader_data) {
1847 release_C_heap_structures();
1848 }
1849
1850 #if INCLUDE_CDS
1851 void MethodData::remove_unshareable_info() {
1852 _extra_data_lock = nullptr;
1853 }
1854
1855 void MethodData::restore_unshareable_info(TRAPS) {
1856 //_extra_data_lock = new Mutex(Mutex::nosafepoint, "MDOExtraData_lock");
1857 }
1858 #endif // INCLUDE_CDS
1859
1860 #ifdef ASSERT
1861 void MethodData::check_extra_data_locked() const {
1862 // Cast const away, just to be able to verify the lock
1863 // Usually we only want non-const accesses on the lock,
1864 // so this here is an exception.
1865 MethodData* self = (MethodData*)this;
1866 assert(self->extra_data_lock()->owned_by_self() || CDSConfig::is_dumping_archive(), "must have lock");
1867 assert(!Thread::current()->is_Java_thread() ||
1868 JavaThread::current()->is_in_no_safepoint_scope(),
1869 "JavaThread must have NoSafepointVerifier inside lock scope");
1870 }
1871 #endif