1 /*
2 * Copyright (c) 2000, 2025, 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 #ifndef SHARE_OOPS_METHODDATA_HPP
26 #define SHARE_OOPS_METHODDATA_HPP
27
28 #include "interpreter/bytecodes.hpp"
29 #include "interpreter/invocationCounter.hpp"
30 #include "oops/metadata.hpp"
31 #include "oops/method.hpp"
32 #include "runtime/atomicAccess.hpp"
33 #include "runtime/deoptimization.hpp"
34 #include "runtime/mutex.hpp"
35 #include "utilities/align.hpp"
36 #include "utilities/copy.hpp"
37 #include "utilities/integerCast.hpp"
38
39 class BytecodeStream;
40
41 // The MethodData object collects counts and other profile information
42 // during zeroth-tier (interpreter) and third-tier (C1 with full profiling)
43 // execution.
44 //
45 // The profile is used later by compilation heuristics. Some heuristics
46 // enable use of aggressive (or "heroic") optimizations. An aggressive
47 // optimization often has a down-side, a corner case that it handles
48 // poorly, but which is thought to be rare. The profile provides
49 // evidence of this rarity for a given method or even BCI. It allows
50 // the compiler to back out of the optimization at places where it
51 // has historically been a poor choice. Other heuristics try to use
52 // specific information gathered about types observed at a given site.
53 //
54 // All data in the profile is approximate. It is expected to be accurate
55 // on the whole, but the system expects occasional inaccuraces, due to
56 // counter overflow, multiprocessor races during data collection, space
57 // limitations, missing MDO blocks, etc. Bad or missing data will degrade
58 // optimization quality but will not affect correctness. Also, each MDO
59 // can be checked for its "maturity" by calling is_mature().
60 //
61 // Short (<32-bit) counters are designed to overflow to a known "saturated"
62 // state. Also, certain recorded per-BCI events are given one-bit counters
63 // which overflow to a saturated state which applied to all counters at
64 // that BCI. In other words, there is a small lattice which approximates
65 // the ideal of an infinite-precision counter for each event at each BCI,
66 // and the lattice quickly "bottoms out" in a state where all counters
67 // are taken to be indefinitely large.
68 //
69 // The reader will find many data races in profile gathering code, starting
70 // with invocation counter incrementation. None of these races harm correct
71 // execution of the compiled code.
72
73 // forward decl
74 class ProfileData;
75
76 // DataLayout
77 //
78 // Overlay for generic profiling data.
79 class DataLayout {
80 friend class VMStructs;
81 friend class JVMCIVMStructs;
82
83 private:
84 // Every data layout begins with a header. This header
85 // contains a tag, which is used to indicate the size/layout
86 // of the data, 8 bits of flags, which can be used in any way,
87 // 32 bits of trap history (none/one reason/many reasons),
88 // and a bci, which is used to tie this piece of data to a
89 // specific bci in the bytecodes.
90 union {
91 u8 _bits;
92 struct {
93 u1 _tag;
94 u1 _flags;
95 u2 _bci;
96 u4 _traps;
97 } _struct;
98 } _header;
99
100 // The data layout has an arbitrary number of cells, each sized
101 // to accommodate a pointer or an integer.
102 intptr_t _cells[1];
103
104 // Some types of data layouts need a length field.
105 static bool needs_array_len(u1 tag);
106
107 public:
108 enum {
109 counter_increment = 1
110 };
111
112 enum {
113 cell_size = sizeof(intptr_t)
114 };
115
116 // Tag values
117 enum : u1 {
118 no_tag,
119 bit_data_tag,
120 counter_data_tag,
121 jump_data_tag,
122 receiver_type_data_tag,
123 virtual_call_data_tag,
124 ret_data_tag,
125 branch_data_tag,
126 multi_branch_data_tag,
127 arg_info_data_tag,
128 call_type_data_tag,
129 virtual_call_type_data_tag,
130 parameters_type_data_tag,
131 speculative_trap_data_tag,
132 array_store_data_tag,
133 array_load_data_tag,
134 acmp_data_tag
135 };
136
137 enum {
138 // The trap state breaks down as [recompile:1 | reason:31].
139 // This further breakdown is defined in deoptimization.cpp.
140 // See Deoptimization::trap_state_reason for an assert that
141 // trap_bits is big enough to hold reasons < Reason_RECORDED_LIMIT.
142 //
143 // The trap_state is collected only if ProfileTraps is true.
144 trap_bits = 1+31, // 31: enough to distinguish [0..Reason_RECORDED_LIMIT].
145 trap_mask = -1,
146 first_flag = 0
147 };
148
149 // Size computation
150 static int header_size_in_bytes() {
151 return header_size_in_cells() * cell_size;
152 }
153 static int header_size_in_cells() {
154 return LP64_ONLY(1) NOT_LP64(2);
155 }
156
157 static int compute_size_in_bytes(int cell_count) {
158 return header_size_in_bytes() + cell_count * cell_size;
159 }
160
161 // Initialization
162 void initialize(u1 tag, u2 bci, int cell_count);
163
164 // Accessors
165 u1 tag() {
166 return _header._struct._tag;
167 }
168
169 // Return 32 bits of trap state.
170 // The state tells if traps with zero, one, or many reasons have occurred.
171 // It also tells whether zero or many recompilations have occurred.
172 // The associated trap histogram in the MDO itself tells whether
173 // traps are common or not. If a BCI shows that a trap X has
174 // occurred, and the MDO shows N occurrences of X, we make the
175 // simplifying assumption that all N occurrences can be blamed
176 // on that BCI.
177 uint trap_state() const {
178 return _header._struct._traps;
179 }
180
181 void set_trap_state(uint new_state) {
182 assert(ProfileTraps, "used only under +ProfileTraps");
183 uint old_flags = _header._struct._traps;
184 _header._struct._traps = new_state | old_flags;
185 }
186
187 u1 flags() const {
188 return AtomicAccess::load_acquire(&_header._struct._flags);
189 }
190
191 u2 bci() const {
192 return _header._struct._bci;
193 }
194
195 void set_header(u8 value) {
196 _header._bits = value;
197 }
198 u8 header() {
199 return _header._bits;
200 }
201 void set_cell_at(int index, intptr_t value) {
202 _cells[index] = value;
203 }
204 void release_set_cell_at(int index, intptr_t value);
205 intptr_t cell_at(int index) const {
206 return _cells[index];
207 }
208 intptr_t* cell_at_adr(int index) const {
209 return const_cast<intptr_t*>(&_cells[index]);
210 }
211
212 bool set_flag_at(u1 flag_number) {
213 const u1 bit = integer_cast<u1>(1 << flag_number);
214 u1 compare_value;
215 do {
216 compare_value = _header._struct._flags;
217 if ((compare_value & bit) == bit) {
218 // already set.
219 return false;
220 }
221 } while (compare_value != AtomicAccess::cmpxchg(&_header._struct._flags, compare_value, static_cast<u1>(compare_value | bit)));
222 return true;
223 }
224
225 bool clear_flag_at(u1 flag_number) {
226 const u1 bit = integer_cast<u1>(1 << flag_number);
227 u1 compare_value;
228 u1 exchange_value;
229 do {
230 compare_value = _header._struct._flags;
231 if ((compare_value & bit) == 0) {
232 // already cleaed.
233 return false;
234 }
235 exchange_value = compare_value & ~bit;
236 } while (compare_value != AtomicAccess::cmpxchg(&_header._struct._flags, compare_value, exchange_value));
237 return true;
238 }
239
240 bool flag_at(u1 flag_number) const {
241 return (flags() & (1 << flag_number)) != 0;
242 }
243
244 // Low-level support for code generation.
245 static ByteSize header_offset() {
246 return byte_offset_of(DataLayout, _header);
247 }
248 static ByteSize tag_offset() {
249 return byte_offset_of(DataLayout, _header._struct._tag);
250 }
251 static ByteSize flags_offset() {
252 return byte_offset_of(DataLayout, _header._struct._flags);
253 }
254 static ByteSize bci_offset() {
255 return byte_offset_of(DataLayout, _header._struct._bci);
256 }
257 static ByteSize cell_offset(int index) {
258 return byte_offset_of(DataLayout, _cells) + in_ByteSize(index * cell_size);
259 }
260 // Return a value which, when or-ed as a byte into _flags, sets the flag.
261 static u1 flag_number_to_constant(u1 flag_number) {
262 DataLayout temp; temp.set_header(0);
263 temp.set_flag_at(flag_number);
264 return temp._header._struct._flags;
265 }
266 // Return a value which, when or-ed as a word into _header, sets the flag.
267 static u8 flag_mask_to_header_mask(u1 byte_constant) {
268 DataLayout temp; temp.set_header(0);
269 temp._header._struct._flags = byte_constant;
270 return temp._header._bits;
271 }
272
273 ProfileData* data_in();
274
275 int size_in_bytes() {
276 int cells = cell_count();
277 assert(cells >= 0, "invalid number of cells");
278 return DataLayout::compute_size_in_bytes(cells);
279 }
280 int cell_count();
281
282 // GC support
283 void clean_weak_klass_links(bool always_clean);
284 };
285
286
287 // ProfileData class hierarchy
288 class ProfileData;
289 class BitData;
290 class CounterData;
291 class ReceiverTypeData;
292 class VirtualCallData;
293 class VirtualCallTypeData;
294 class ArrayStoreData;
295 class RetData;
296 class CallTypeData;
297 class JumpData;
298 class BranchData;
299 class ACmpData;
300 class ArrayData;
301 class MultiBranchData;
302 class ArgInfoData;
303 class ParametersTypeData;
304 class SpeculativeTrapData;
305 class ArrayLoadData;
306
307 // ProfileData
308 //
309 // A ProfileData object is created to refer to a section of profiling
310 // data in a structured way.
311 class ProfileData : public ResourceObj {
312 friend class TypeEntries;
313 friend class SingleTypeEntry;
314 friend class TypeStackSlotEntries;
315 private:
316 enum {
317 tab_width_one = 16,
318 tab_width_two = 36
319 };
320
321 // This is a pointer to a section of profiling data.
322 DataLayout* _data;
323
324 char* print_data_on_helper(const MethodData* md) const;
325
326 protected:
327 DataLayout* data() { return _data; }
328 const DataLayout* data() const { return _data; }
329
330 enum {
331 cell_size = DataLayout::cell_size
332 };
333
334 public:
335 // How many cells are in this?
336 virtual int cell_count() const {
337 ShouldNotReachHere();
338 return -1;
339 }
340
341 // Return the size of this data.
342 int size_in_bytes() {
343 return DataLayout::compute_size_in_bytes(cell_count());
344 }
345
346 protected:
347 // Low-level accessors for underlying data
348 void set_intptr_at(int index, intptr_t value) {
349 assert(0 <= index && index < cell_count(), "oob");
350 data()->set_cell_at(index, value);
351 }
352 void release_set_intptr_at(int index, intptr_t value);
353 intptr_t intptr_at(int index) const {
354 assert(0 <= index && index < cell_count(), "oob");
355 return data()->cell_at(index);
356 }
357 intptr_t* intptr_at_adr(int index) const {
358 assert(0 <= index && index < cell_count(), "oob");
359 return data()->cell_at_adr(index);
360 }
361 void set_uint_at(int index, uint value) {
362 set_intptr_at(index, (intptr_t) value);
363 }
364 void release_set_uint_at(int index, uint value);
365 uint uint_at(int index) const {
366 return (uint)intptr_at(index);
367 }
368 void set_int_at(int index, int value) {
369 set_intptr_at(index, (intptr_t) value);
370 }
371 void release_set_int_at(int index, int value);
372 int int_at(int index) const {
373 return (int)intptr_at(index);
374 }
375 int int_at_unchecked(int index) const {
376 return (int)data()->cell_at(index);
377 }
378
379 void set_flag_at(u1 flag_number) {
380 data()->set_flag_at(flag_number);
381 }
382 bool flag_at(u1 flag_number) const {
383 return data()->flag_at(flag_number);
384 }
385
386 // two convenient imports for use by subclasses:
387 static ByteSize cell_offset(int index) {
388 return DataLayout::cell_offset(index);
389 }
390 static u1 flag_number_to_constant(u1 flag_number) {
391 return DataLayout::flag_number_to_constant(flag_number);
392 }
393
394 ProfileData(DataLayout* data) {
395 _data = data;
396 }
397
398 public:
399 // Constructor for invalid ProfileData.
400 ProfileData();
401
402 u2 bci() const {
403 return data()->bci();
404 }
405
406 address dp() {
407 return (address)_data;
408 }
409
410 int trap_state() const {
411 return data()->trap_state();
412 }
413 void set_trap_state(int new_state) {
414 data()->set_trap_state(new_state);
415 }
416
417 // Type checking
418 virtual bool is_BitData() const { return false; }
419 virtual bool is_CounterData() const { return false; }
420 virtual bool is_JumpData() const { return false; }
421 virtual bool is_ReceiverTypeData()const { return false; }
422 virtual bool is_VirtualCallData() const { return false; }
423 virtual bool is_RetData() const { return false; }
424 virtual bool is_BranchData() const { return false; }
425 virtual bool is_ArrayData() const { return false; }
426 virtual bool is_MultiBranchData() const { return false; }
427 virtual bool is_ArgInfoData() const { return false; }
428 virtual bool is_CallTypeData() const { return false; }
429 virtual bool is_VirtualCallTypeData()const { return false; }
430 virtual bool is_ParametersTypeData() const { return false; }
431 virtual bool is_SpeculativeTrapData()const { return false; }
432 virtual bool is_ArrayStoreData() const { return false; }
433 virtual bool is_ArrayLoadData() const { return false; }
434 virtual bool is_ACmpData() const { return false; }
435
436
437 BitData* as_BitData() const {
438 assert(is_BitData(), "wrong type");
439 return is_BitData() ? (BitData*) this : nullptr;
440 }
441 CounterData* as_CounterData() const {
442 assert(is_CounterData(), "wrong type");
443 return is_CounterData() ? (CounterData*) this : nullptr;
444 }
445 JumpData* as_JumpData() const {
446 assert(is_JumpData(), "wrong type");
447 return is_JumpData() ? (JumpData*) this : nullptr;
448 }
449 ReceiverTypeData* as_ReceiverTypeData() const {
450 assert(is_ReceiverTypeData(), "wrong type");
451 return is_ReceiverTypeData() ? (ReceiverTypeData*)this : nullptr;
452 }
453 VirtualCallData* as_VirtualCallData() const {
454 assert(is_VirtualCallData(), "wrong type");
455 return is_VirtualCallData() ? (VirtualCallData*)this : nullptr;
456 }
457 RetData* as_RetData() const {
458 assert(is_RetData(), "wrong type");
459 return is_RetData() ? (RetData*) this : nullptr;
460 }
461 BranchData* as_BranchData() const {
462 assert(is_BranchData(), "wrong type");
463 return is_BranchData() ? (BranchData*) this : nullptr;
464 }
465 ArrayData* as_ArrayData() const {
466 assert(is_ArrayData(), "wrong type");
467 return is_ArrayData() ? (ArrayData*) this : nullptr;
468 }
469 MultiBranchData* as_MultiBranchData() const {
470 assert(is_MultiBranchData(), "wrong type");
471 return is_MultiBranchData() ? (MultiBranchData*)this : nullptr;
472 }
473 ArgInfoData* as_ArgInfoData() const {
474 assert(is_ArgInfoData(), "wrong type");
475 return is_ArgInfoData() ? (ArgInfoData*)this : nullptr;
476 }
477 CallTypeData* as_CallTypeData() const {
478 assert(is_CallTypeData(), "wrong type");
479 return is_CallTypeData() ? (CallTypeData*)this : nullptr;
480 }
481 VirtualCallTypeData* as_VirtualCallTypeData() const {
482 assert(is_VirtualCallTypeData(), "wrong type");
483 return is_VirtualCallTypeData() ? (VirtualCallTypeData*)this : nullptr;
484 }
485 ParametersTypeData* as_ParametersTypeData() const {
486 assert(is_ParametersTypeData(), "wrong type");
487 return is_ParametersTypeData() ? (ParametersTypeData*)this : nullptr;
488 }
489 SpeculativeTrapData* as_SpeculativeTrapData() const {
490 assert(is_SpeculativeTrapData(), "wrong type");
491 return is_SpeculativeTrapData() ? (SpeculativeTrapData*)this : nullptr;
492 }
493 ArrayStoreData* as_ArrayStoreData() const {
494 assert(is_ArrayStoreData(), "wrong type");
495 return is_ArrayStoreData() ? (ArrayStoreData*)this : nullptr;
496 }
497 ArrayLoadData* as_ArrayLoadData() const {
498 assert(is_ArrayLoadData(), "wrong type");
499 return is_ArrayLoadData() ? (ArrayLoadData*)this : nullptr;
500 }
501 ACmpData* as_ACmpData() const {
502 assert(is_ACmpData(), "wrong type");
503 return is_ACmpData() ? (ACmpData*)this : nullptr;
504 }
505
506
507 // Subclass specific initialization
508 virtual void post_initialize(BytecodeStream* stream, MethodData* mdo) {}
509
510 // GC support
511 virtual void clean_weak_klass_links(bool always_clean) {}
512
513 // CDS support
514 virtual void metaspace_pointers_do(MetaspaceClosure* it) {}
515
516 // CI translation: ProfileData can represent both MethodDataOop data
517 // as well as CIMethodData data. This function is provided for translating
518 // an oop in a ProfileData to the ci equivalent. Generally speaking,
519 // most ProfileData don't require any translation, so we provide the null
520 // translation here, and the required translators are in the ci subclasses.
521 virtual void translate_from(const ProfileData* data) {}
522
523 virtual void print_data_on(outputStream* st, const char* extra = nullptr) const {
524 ShouldNotReachHere();
525 }
526
527 void print_data_on(outputStream* st, const MethodData* md) const;
528
529 void print_shared(outputStream* st, const char* name, const char* extra) const;
530 void tab(outputStream* st, bool first = false) const;
531 };
532
533 // BitData
534 //
535 // A BitData holds a flag or two in its header.
536 class BitData : public ProfileData {
537 friend class VMStructs;
538 friend class JVMCIVMStructs;
539 protected:
540 enum : u1 {
541 // null_seen:
542 // saw a null operand (cast/aastore/instanceof)
543 null_seen_flag = DataLayout::first_flag + 0,
544 exception_handler_entered_flag = null_seen_flag + 1,
545 deprecated_method_callsite_flag = exception_handler_entered_flag + 1
546 #if INCLUDE_JVMCI
547 // bytecode threw any exception
548 , exception_seen_flag = deprecated_method_callsite_flag + 1
549 #endif
550 , last_bit_data_flag
551 };
552 enum { bit_cell_count = 0 }; // no additional data fields needed.
553 public:
554 BitData(DataLayout* layout) : ProfileData(layout) {
555 }
556
557 virtual bool is_BitData() const { return true; }
558
559 static int static_cell_count() {
560 return bit_cell_count;
561 }
562
563 virtual int cell_count() const {
564 return static_cell_count();
565 }
566
567 // Accessor
568
569 // The null_seen flag bit is specially known to the interpreter.
570 // Consulting it allows the compiler to avoid setting up null_check traps.
571 bool null_seen() const { return flag_at(null_seen_flag); }
572 void set_null_seen() { set_flag_at(null_seen_flag); }
573 bool deprecated_method_call_site() const { return flag_at(deprecated_method_callsite_flag); }
574 bool set_deprecated_method_call_site() { return data()->set_flag_at(deprecated_method_callsite_flag); }
575 bool clear_deprecated_method_call_site() { return data()->clear_flag_at(deprecated_method_callsite_flag); }
576
577 #if INCLUDE_JVMCI
578 // true if an exception was thrown at the specific BCI
579 bool exception_seen() { return flag_at(exception_seen_flag); }
580 void set_exception_seen() { set_flag_at(exception_seen_flag); }
581 #endif
582
583 // true if a ex handler block at this bci was entered
584 bool exception_handler_entered() { return flag_at(exception_handler_entered_flag); }
585 void set_exception_handler_entered() { set_flag_at(exception_handler_entered_flag); }
586
587 // Code generation support
588 static u1 null_seen_byte_constant() {
589 return flag_number_to_constant(null_seen_flag);
590 }
591
592 static ByteSize bit_data_size() {
593 return cell_offset(bit_cell_count);
594 }
595
596 void print_data_on(outputStream* st, const char* extra = nullptr) const;
597 };
598
599 // CounterData
600 //
601 // A CounterData corresponds to a simple counter.
602 class CounterData : public BitData {
603 friend class VMStructs;
604 friend class JVMCIVMStructs;
605 protected:
606 enum {
607 count_off,
608 counter_cell_count
609 };
610 public:
611 CounterData(DataLayout* layout) : BitData(layout) {}
612
613 virtual bool is_CounterData() const { return true; }
614
615 static int static_cell_count() {
616 return counter_cell_count;
617 }
618
619 virtual int cell_count() const {
620 return static_cell_count();
621 }
622
623 // Direct accessor
624 int count() const {
625 intptr_t raw_data = intptr_at(count_off);
626 if (raw_data > max_jint) {
627 raw_data = max_jint;
628 } else if (raw_data < min_jint) {
629 raw_data = min_jint;
630 }
631 return int(raw_data);
632 }
633
634 // Code generation support
635 static ByteSize count_offset() {
636 return cell_offset(count_off);
637 }
638 static ByteSize counter_data_size() {
639 return cell_offset(counter_cell_count);
640 }
641
642 void set_count(int count) {
643 set_int_at(count_off, count);
644 }
645
646 void print_data_on(outputStream* st, const char* extra = nullptr) const;
647 };
648
649 // JumpData
650 //
651 // A JumpData is used to access profiling information for a direct
652 // branch. It is a counter, used for counting the number of branches,
653 // plus a data displacement, used for realigning the data pointer to
654 // the corresponding target bci.
655 class JumpData : public ProfileData {
656 friend class VMStructs;
657 friend class JVMCIVMStructs;
658 protected:
659 enum {
660 taken_off_set,
661 displacement_off_set,
662 jump_cell_count
663 };
664
665 void set_displacement(int displacement) {
666 set_int_at(displacement_off_set, displacement);
667 }
668
669 public:
670 JumpData(DataLayout* layout) : ProfileData(layout) {
671 assert(layout->tag() == DataLayout::jump_data_tag ||
672 layout->tag() == DataLayout::branch_data_tag ||
673 layout->tag() == DataLayout::acmp_data_tag, "wrong type");
674 }
675
676 virtual bool is_JumpData() const { return true; }
677
678 static int static_cell_count() {
679 return jump_cell_count;
680 }
681
682 virtual int cell_count() const {
683 return static_cell_count();
684 }
685
686 // Direct accessor
687 uint taken() const {
688 return uint_at(taken_off_set);
689 }
690
691 void set_taken(uint cnt) {
692 set_uint_at(taken_off_set, cnt);
693 }
694
695 // Saturating counter
696 uint inc_taken() {
697 uint cnt = taken() + 1;
698 // Did we wrap? Will compiler screw us??
699 if (cnt == 0) cnt--;
700 set_uint_at(taken_off_set, cnt);
701 return cnt;
702 }
703
704 int displacement() const {
705 return int_at(displacement_off_set);
706 }
707
708 // Code generation support
709 static ByteSize taken_offset() {
710 return cell_offset(taken_off_set);
711 }
712
713 static ByteSize displacement_offset() {
714 return cell_offset(displacement_off_set);
715 }
716
717 // Specific initialization.
718 void post_initialize(BytecodeStream* stream, MethodData* mdo);
719
720 void print_data_on(outputStream* st, const char* extra = nullptr) const;
721 };
722
723 // Entries in a ProfileData object to record types: it can either be
724 // none (no profile), unknown (conflicting profile data) or a klass if
725 // a single one is seen. Whether a null reference was seen is also
726 // recorded. No counter is associated with the type and a single type
727 // is tracked (unlike VirtualCallData).
728 class TypeEntries {
729
730 public:
731
732 // A single cell is used to record information for a type:
733 // - the cell is initialized to 0
734 // - when a type is discovered it is stored in the cell
735 // - bit zero of the cell is used to record whether a null reference
736 // was encountered or not
737 // - bit 1 is set to record a conflict in the type information
738
739 enum {
740 null_seen = 1,
741 type_mask = ~null_seen,
742 type_unknown = 2,
743 status_bits = null_seen | type_unknown,
744 type_klass_mask = ~status_bits
745 };
746
747 // what to initialize a cell to
748 static intptr_t type_none() {
749 return 0;
750 }
751
752 // null seen = bit 0 set?
753 static bool was_null_seen(intptr_t v) {
754 return (v & null_seen) != 0;
755 }
756
757 // conflicting type information = bit 1 set?
758 static bool is_type_unknown(intptr_t v) {
759 return (v & type_unknown) != 0;
760 }
761
762 // not type information yet = all bits cleared, ignoring bit 0?
763 static bool is_type_none(intptr_t v) {
764 return (v & type_mask) == 0;
765 }
766
767 // recorded type: cell without bit 0 and 1
768 static intptr_t klass_part(intptr_t v) {
769 intptr_t r = v & type_klass_mask;
770 return r;
771 }
772
773 // type recorded
774 static Klass* valid_klass(intptr_t k) {
775 if (!is_type_none(k) &&
776 !is_type_unknown(k)) {
777 Klass* res = (Klass*)klass_part(k);
778 assert(res != nullptr, "invalid");
779 return res;
780 } else {
781 return nullptr;
782 }
783 }
784
785 static intptr_t with_status(intptr_t k, intptr_t in) {
786 return k | (in & status_bits);
787 }
788
789 static intptr_t with_status(Klass* k, intptr_t in) {
790 return with_status((intptr_t)k, in);
791 }
792
793 static void print_klass(outputStream* st, intptr_t k);
794
795 protected:
796 // ProfileData object these entries are part of
797 ProfileData* _pd;
798 // offset within the ProfileData object where the entries start
799 const int _base_off;
800
801 TypeEntries(int base_off)
802 : _pd(nullptr), _base_off(base_off) {}
803
804 void set_intptr_at(int index, intptr_t value) {
805 _pd->set_intptr_at(index, value);
806 }
807
808 intptr_t intptr_at(int index) const {
809 return _pd->intptr_at(index);
810 }
811
812 public:
813 void set_profile_data(ProfileData* pd) {
814 _pd = pd;
815 }
816 };
817
818 // Type entries used for arguments passed at a call and parameters on
819 // method entry. 2 cells per entry: one for the type encoded as in
820 // TypeEntries and one initialized with the stack slot where the
821 // profiled object is to be found so that the interpreter can locate
822 // it quickly.
823 class TypeStackSlotEntries : public TypeEntries {
824
825 private:
826 enum {
827 stack_slot_entry,
828 type_entry,
829 per_arg_cell_count
830 };
831
832 // offset of cell for stack slot for entry i within ProfileData object
833 int stack_slot_offset(int i) const {
834 return _base_off + stack_slot_local_offset(i);
835 }
836
837 const int _number_of_entries;
838
839 // offset of cell for type for entry i within ProfileData object
840 int type_offset_in_cells(int i) const {
841 return _base_off + type_local_offset(i);
842 }
843
844 public:
845
846 TypeStackSlotEntries(int base_off, int nb_entries)
847 : TypeEntries(base_off), _number_of_entries(nb_entries) {}
848
849 static int compute_cell_count(Symbol* signature, bool include_receiver, int max);
850
851 void post_initialize(Symbol* signature, bool has_receiver, bool include_receiver);
852
853 int number_of_entries() const { return _number_of_entries; }
854
855 // offset of cell for stack slot for entry i within this block of cells for a TypeStackSlotEntries
856 static int stack_slot_local_offset(int i) {
857 return i * per_arg_cell_count + stack_slot_entry;
858 }
859
860 // offset of cell for type for entry i within this block of cells for a TypeStackSlotEntries
861 static int type_local_offset(int i) {
862 return i * per_arg_cell_count + type_entry;
863 }
864
865 // stack slot for entry i
866 uint stack_slot(int i) const {
867 assert(i >= 0 && i < _number_of_entries, "oob");
868 return _pd->uint_at(stack_slot_offset(i));
869 }
870
871 // set stack slot for entry i
872 void set_stack_slot(int i, uint num) {
873 assert(i >= 0 && i < _number_of_entries, "oob");
874 _pd->set_uint_at(stack_slot_offset(i), num);
875 }
876
877 // type for entry i
878 intptr_t type(int i) const {
879 assert(i >= 0 && i < _number_of_entries, "oob");
880 return _pd->intptr_at(type_offset_in_cells(i));
881 }
882
883 intptr_t* type_adr(int i) const {
884 assert(i >= 0 && i < _number_of_entries, "oob");
885 return _pd->intptr_at_adr(type_offset_in_cells(i));
886 }
887
888 // set type for entry i
889 void set_type(int i, intptr_t k) {
890 assert(i >= 0 && i < _number_of_entries, "oob");
891 _pd->set_intptr_at(type_offset_in_cells(i), k);
892 }
893
894 static ByteSize per_arg_size() {
895 return in_ByteSize(per_arg_cell_count * DataLayout::cell_size);
896 }
897
898 static int per_arg_count() {
899 return per_arg_cell_count;
900 }
901
902 ByteSize type_offset(int i) const {
903 return DataLayout::cell_offset(type_offset_in_cells(i));
904 }
905
906 // GC support
907 void clean_weak_klass_links(bool always_clean);
908
909 // CDS support
910 virtual void metaspace_pointers_do(MetaspaceClosure* it);
911
912 void print_data_on(outputStream* st) const;
913 };
914
915 // Type entry used for return from a call. A single cell to record the
916 // type.
917 class SingleTypeEntry : public TypeEntries {
918
919 private:
920 enum {
921 cell_count = 1
922 };
923
924 public:
925 SingleTypeEntry(int base_off)
926 : TypeEntries(base_off) {}
927
928 void post_initialize() {
929 set_type(type_none());
930 }
931
932 intptr_t type() const {
933 return _pd->intptr_at(_base_off);
934 }
935
936 intptr_t* type_adr() const {
937 return _pd->intptr_at_adr(_base_off);
938 }
939
940 void set_type(intptr_t k) {
941 _pd->set_intptr_at(_base_off, k);
942 }
943
944 static int static_cell_count() {
945 return cell_count;
946 }
947
948 static ByteSize size() {
949 return in_ByteSize(cell_count * DataLayout::cell_size);
950 }
951
952 ByteSize type_offset() {
953 return DataLayout::cell_offset(_base_off);
954 }
955
956 // GC support
957 void clean_weak_klass_links(bool always_clean);
958
959 // CDS support
960 virtual void metaspace_pointers_do(MetaspaceClosure* it);
961
962 void print_data_on(outputStream* st) const;
963 };
964
965 // Entries to collect type information at a call: contains arguments
966 // (TypeStackSlotEntries), a return type (SingleTypeEntry) and a
967 // number of cells. Because the number of cells for the return type is
968 // smaller than the number of cells for the type of an arguments, the
969 // number of cells is used to tell how many arguments are profiled and
970 // whether a return value is profiled. See has_arguments() and
971 // has_return().
972 class TypeEntriesAtCall {
973 private:
974 static int stack_slot_local_offset(int i) {
975 return header_cell_count() + TypeStackSlotEntries::stack_slot_local_offset(i);
976 }
977
978 static int argument_type_local_offset(int i) {
979 return header_cell_count() + TypeStackSlotEntries::type_local_offset(i);
980 }
981
982 public:
983
984 static int header_cell_count() {
985 return 1;
986 }
987
988 static int cell_count_local_offset() {
989 return 0;
990 }
991
992 static int compute_cell_count(BytecodeStream* stream);
993
994 static void initialize(DataLayout* dl, int base, int cell_count) {
995 int off = base + cell_count_local_offset();
996 dl->set_cell_at(off, cell_count - base - header_cell_count());
997 }
998
999 static bool arguments_profiling_enabled();
1000 static bool return_profiling_enabled();
1001
1002 // Code generation support
1003 static ByteSize cell_count_offset() {
1004 return in_ByteSize(cell_count_local_offset() * DataLayout::cell_size);
1005 }
1006
1007 static ByteSize args_data_offset() {
1008 return in_ByteSize(header_cell_count() * DataLayout::cell_size);
1009 }
1010
1011 static ByteSize stack_slot_offset(int i) {
1012 return in_ByteSize(stack_slot_local_offset(i) * DataLayout::cell_size);
1013 }
1014
1015 static ByteSize argument_type_offset(int i) {
1016 return in_ByteSize(argument_type_local_offset(i) * DataLayout::cell_size);
1017 }
1018
1019 static ByteSize return_only_size() {
1020 return SingleTypeEntry::size() + in_ByteSize(header_cell_count() * DataLayout::cell_size);
1021 }
1022
1023 };
1024
1025 // CallTypeData
1026 //
1027 // A CallTypeData is used to access profiling information about a non
1028 // virtual call for which we collect type information about arguments
1029 // and return value.
1030 class CallTypeData : public CounterData {
1031 private:
1032 // entries for arguments if any
1033 TypeStackSlotEntries _args;
1034 // entry for return type if any
1035 SingleTypeEntry _ret;
1036
1037 int cell_count_global_offset() const {
1038 return CounterData::static_cell_count() + TypeEntriesAtCall::cell_count_local_offset();
1039 }
1040
1041 // number of cells not counting the header
1042 int cell_count_no_header() const {
1043 return uint_at(cell_count_global_offset());
1044 }
1045
1046 void check_number_of_arguments(int total) {
1047 assert(number_of_arguments() == total, "should be set in DataLayout::initialize");
1048 }
1049
1050 public:
1051 CallTypeData(DataLayout* layout) :
1052 CounterData(layout),
1053 _args(CounterData::static_cell_count()+TypeEntriesAtCall::header_cell_count(), number_of_arguments()),
1054 _ret(cell_count() - SingleTypeEntry::static_cell_count())
1055 {
1056 assert(layout->tag() == DataLayout::call_type_data_tag, "wrong type");
1057 // Some compilers (VC++) don't want this passed in member initialization list
1058 _args.set_profile_data(this);
1059 _ret.set_profile_data(this);
1060 }
1061
1062 const TypeStackSlotEntries* args() const {
1063 assert(has_arguments(), "no profiling of arguments");
1064 return &_args;
1065 }
1066
1067 const SingleTypeEntry* ret() const {
1068 assert(has_return(), "no profiling of return value");
1069 return &_ret;
1070 }
1071
1072 virtual bool is_CallTypeData() const { return true; }
1073
1074 static int static_cell_count() {
1075 return -1;
1076 }
1077
1078 static int compute_cell_count(BytecodeStream* stream) {
1079 return CounterData::static_cell_count() + TypeEntriesAtCall::compute_cell_count(stream);
1080 }
1081
1082 static void initialize(DataLayout* dl, int cell_count) {
1083 TypeEntriesAtCall::initialize(dl, CounterData::static_cell_count(), cell_count);
1084 }
1085
1086 virtual void post_initialize(BytecodeStream* stream, MethodData* mdo);
1087
1088 virtual int cell_count() const {
1089 return CounterData::static_cell_count() +
1090 TypeEntriesAtCall::header_cell_count() +
1091 int_at_unchecked(cell_count_global_offset());
1092 }
1093
1094 int number_of_arguments() const {
1095 return cell_count_no_header() / TypeStackSlotEntries::per_arg_count();
1096 }
1097
1098 void set_argument_type(int i, Klass* k) {
1099 assert(has_arguments(), "no arguments!");
1100 intptr_t current = _args.type(i);
1101 _args.set_type(i, TypeEntries::with_status(k, current));
1102 }
1103
1104 void set_return_type(Klass* k) {
1105 assert(has_return(), "no return!");
1106 intptr_t current = _ret.type();
1107 _ret.set_type(TypeEntries::with_status(k, current));
1108 }
1109
1110 // An entry for a return value takes less space than an entry for an
1111 // argument so if the number of cells exceeds the number of cells
1112 // needed for an argument, this object contains type information for
1113 // at least one argument.
1114 bool has_arguments() const {
1115 bool res = cell_count_no_header() >= TypeStackSlotEntries::per_arg_count();
1116 assert (!res || TypeEntriesAtCall::arguments_profiling_enabled(), "no profiling of arguments");
1117 return res;
1118 }
1119
1120 // An entry for a return value takes less space than an entry for an
1121 // argument, so if the remainder of the number of cells divided by
1122 // the number of cells for an argument is not null, a return value
1123 // is profiled in this object.
1124 bool has_return() const {
1125 bool res = (cell_count_no_header() % TypeStackSlotEntries::per_arg_count()) != 0;
1126 assert (!res || TypeEntriesAtCall::return_profiling_enabled(), "no profiling of return values");
1127 return res;
1128 }
1129
1130 // Code generation support
1131 static ByteSize args_data_offset() {
1132 return cell_offset(CounterData::static_cell_count()) + TypeEntriesAtCall::args_data_offset();
1133 }
1134
1135 ByteSize argument_type_offset(int i) {
1136 return _args.type_offset(i);
1137 }
1138
1139 ByteSize return_type_offset() {
1140 return _ret.type_offset();
1141 }
1142
1143 // GC support
1144 virtual void clean_weak_klass_links(bool always_clean) {
1145 if (has_arguments()) {
1146 _args.clean_weak_klass_links(always_clean);
1147 }
1148 if (has_return()) {
1149 _ret.clean_weak_klass_links(always_clean);
1150 }
1151 }
1152
1153 // CDS support
1154 virtual void metaspace_pointers_do(MetaspaceClosure* it) {
1155 if (has_arguments()) {
1156 _args.metaspace_pointers_do(it);
1157 }
1158 if (has_return()) {
1159 _ret.metaspace_pointers_do(it);
1160 }
1161 }
1162
1163 virtual void print_data_on(outputStream* st, const char* extra = nullptr) const;
1164 };
1165
1166 // ReceiverTypeData
1167 //
1168 // A ReceiverTypeData is used to access profiling information about a
1169 // dynamic type check. It consists of a series of (Klass*, count)
1170 // pairs which are used to store a type profile for the receiver of
1171 // the check, the associated count is incremented every time the type
1172 // is seen. A per ReceiverTypeData counter is incremented on type
1173 // overflow (when there's no more room for a not yet profiled Klass*).
1174 //
1175 // Updated by platform-specific code, for example MacroAssembler::profile_receiver_type.
1176 //
1177 class ReceiverTypeData : public CounterData {
1178 friend class VMStructs;
1179 friend class JVMCIVMStructs;
1180 protected:
1181 enum {
1182 receiver0_offset = counter_cell_count,
1183 count0_offset,
1184 receiver_type_row_cell_count = (count0_offset + 1) - receiver0_offset
1185 };
1186
1187 public:
1188 ReceiverTypeData(DataLayout* layout) : CounterData(layout) {
1189 assert(layout->tag() == DataLayout::receiver_type_data_tag ||
1190 layout->tag() == DataLayout::virtual_call_data_tag ||
1191 layout->tag() == DataLayout::virtual_call_type_data_tag ||
1192 layout->tag() == DataLayout::array_store_data_tag, "wrong type");
1193 }
1194
1195 virtual bool is_ReceiverTypeData() const { return true; }
1196
1197 static int static_cell_count() {
1198 return counter_cell_count + (uint) TypeProfileWidth * receiver_type_row_cell_count;
1199 }
1200
1201 virtual int cell_count() const {
1202 return static_cell_count();
1203 }
1204
1205 // Direct accessors
1206 static uint row_limit() {
1207 return (uint) TypeProfileWidth;
1208 }
1209 static int receiver_cell_index(uint row) {
1210 return receiver0_offset + row * receiver_type_row_cell_count;
1211 }
1212 static int receiver_count_cell_index(uint row) {
1213 return count0_offset + row * receiver_type_row_cell_count;
1214 }
1215
1216 Klass* receiver(uint row) const {
1217 assert(row < row_limit(), "oob");
1218
1219 Klass* recv = (Klass*)intptr_at(receiver_cell_index(row));
1220 assert(recv == nullptr || recv->is_klass(), "wrong type");
1221 return recv;
1222 }
1223
1224 void set_receiver(uint row, Klass* k) {
1225 assert((uint)row < row_limit(), "oob");
1226 set_intptr_at(receiver_cell_index(row), (uintptr_t)k);
1227 }
1228
1229 uint receiver_count(uint row) const {
1230 assert(row < row_limit(), "oob");
1231 return uint_at(receiver_count_cell_index(row));
1232 }
1233
1234 void set_receiver_count(uint row, uint count) {
1235 assert(row < row_limit(), "oob");
1236 set_uint_at(receiver_count_cell_index(row), count);
1237 }
1238
1239 void clear_row(uint row) {
1240 assert(row < row_limit(), "oob");
1241 // Clear total count - indicator of polymorphic call site.
1242 // The site may look like as monomorphic after that but
1243 // it allow to have more accurate profiling information because
1244 // there was execution phase change since klasses were unloaded.
1245 // If the site is still polymorphic then MDO will be updated
1246 // to reflect it. But it could be the case that the site becomes
1247 // only bimorphic. Then keeping total count not 0 will be wrong.
1248 // Even if we use monomorphic (when it is not) for compilation
1249 // we will only have trap, deoptimization and recompile again
1250 // with updated MDO after executing method in Interpreter.
1251 // An additional receiver will be recorded in the cleaned row
1252 // during next call execution.
1253 //
1254 // Note: our profiling logic works with empty rows in any slot.
1255 // We do sorting a profiling info (ciCallProfile) for compilation.
1256 //
1257 set_count(0);
1258 set_receiver(row, nullptr);
1259 set_receiver_count(row, 0);
1260 }
1261
1262 // Code generation support
1263 static ByteSize receiver_offset(uint row) {
1264 return cell_offset(receiver_cell_index(row));
1265 }
1266 static ByteSize receiver_count_offset(uint row) {
1267 return cell_offset(receiver_count_cell_index(row));
1268 }
1269 static ByteSize receiver_type_data_size() {
1270 return cell_offset(static_cell_count());
1271 }
1272
1273 // GC support
1274 virtual void clean_weak_klass_links(bool always_clean);
1275
1276 // CDS support
1277 virtual void metaspace_pointers_do(MetaspaceClosure* it);
1278
1279 void print_receiver_data_on(outputStream* st) const;
1280 void print_data_on(outputStream* st, const char* extra = nullptr) const;
1281 };
1282
1283 // VirtualCallData
1284 //
1285 // A VirtualCallData is used to access profiling information about a
1286 // virtual call. For now, it has nothing more than a ReceiverTypeData.
1287 class VirtualCallData : public ReceiverTypeData {
1288 public:
1289 VirtualCallData(DataLayout* layout) : ReceiverTypeData(layout) {
1290 assert(layout->tag() == DataLayout::virtual_call_data_tag ||
1291 layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type");
1292 }
1293
1294 virtual bool is_VirtualCallData() const { return true; }
1295
1296 static int static_cell_count() {
1297 // At this point we could add more profile state, e.g., for arguments.
1298 // But for now it's the same size as the base record type.
1299 return ReceiverTypeData::static_cell_count();
1300 }
1301
1302 virtual int cell_count() const {
1303 return static_cell_count();
1304 }
1305
1306 // Direct accessors
1307 static ByteSize virtual_call_data_size() {
1308 return cell_offset(static_cell_count());
1309 }
1310
1311 void print_method_data_on(outputStream* st) const NOT_JVMCI_RETURN;
1312 void print_data_on(outputStream* st, const char* extra = nullptr) const;
1313 };
1314
1315 // VirtualCallTypeData
1316 //
1317 // A VirtualCallTypeData is used to access profiling information about
1318 // a virtual call for which we collect type information about
1319 // arguments and return value.
1320 class VirtualCallTypeData : public VirtualCallData {
1321 private:
1322 // entries for arguments if any
1323 TypeStackSlotEntries _args;
1324 // entry for return type if any
1325 SingleTypeEntry _ret;
1326
1327 int cell_count_global_offset() const {
1328 return VirtualCallData::static_cell_count() + TypeEntriesAtCall::cell_count_local_offset();
1329 }
1330
1331 // number of cells not counting the header
1332 int cell_count_no_header() const {
1333 return uint_at(cell_count_global_offset());
1334 }
1335
1336 void check_number_of_arguments(int total) {
1337 assert(number_of_arguments() == total, "should be set in DataLayout::initialize");
1338 }
1339
1340 public:
1341 VirtualCallTypeData(DataLayout* layout) :
1342 VirtualCallData(layout),
1343 _args(VirtualCallData::static_cell_count()+TypeEntriesAtCall::header_cell_count(), number_of_arguments()),
1344 _ret(cell_count() - SingleTypeEntry::static_cell_count())
1345 {
1346 assert(layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type");
1347 // Some compilers (VC++) don't want this passed in member initialization list
1348 _args.set_profile_data(this);
1349 _ret.set_profile_data(this);
1350 }
1351
1352 const TypeStackSlotEntries* args() const {
1353 assert(has_arguments(), "no profiling of arguments");
1354 return &_args;
1355 }
1356
1357 const SingleTypeEntry* ret() const {
1358 assert(has_return(), "no profiling of return value");
1359 return &_ret;
1360 }
1361
1362 virtual bool is_VirtualCallTypeData() const { return true; }
1363
1364 static int static_cell_count() {
1365 return -1;
1366 }
1367
1368 static int compute_cell_count(BytecodeStream* stream) {
1369 return VirtualCallData::static_cell_count() + TypeEntriesAtCall::compute_cell_count(stream);
1370 }
1371
1372 static void initialize(DataLayout* dl, int cell_count) {
1373 TypeEntriesAtCall::initialize(dl, VirtualCallData::static_cell_count(), cell_count);
1374 }
1375
1376 virtual void post_initialize(BytecodeStream* stream, MethodData* mdo);
1377
1378 virtual int cell_count() const {
1379 return VirtualCallData::static_cell_count() +
1380 TypeEntriesAtCall::header_cell_count() +
1381 int_at_unchecked(cell_count_global_offset());
1382 }
1383
1384 int number_of_arguments() const {
1385 return cell_count_no_header() / TypeStackSlotEntries::per_arg_count();
1386 }
1387
1388 void set_argument_type(int i, Klass* k) {
1389 assert(has_arguments(), "no arguments!");
1390 intptr_t current = _args.type(i);
1391 _args.set_type(i, TypeEntries::with_status(k, current));
1392 }
1393
1394 void set_return_type(Klass* k) {
1395 assert(has_return(), "no return!");
1396 intptr_t current = _ret.type();
1397 _ret.set_type(TypeEntries::with_status(k, current));
1398 }
1399
1400 // An entry for a return value takes less space than an entry for an
1401 // argument, so if the remainder of the number of cells divided by
1402 // the number of cells for an argument is not null, a return value
1403 // is profiled in this object.
1404 bool has_return() const {
1405 bool res = (cell_count_no_header() % TypeStackSlotEntries::per_arg_count()) != 0;
1406 assert (!res || TypeEntriesAtCall::return_profiling_enabled(), "no profiling of return values");
1407 return res;
1408 }
1409
1410 // An entry for a return value takes less space than an entry for an
1411 // argument so if the number of cells exceeds the number of cells
1412 // needed for an argument, this object contains type information for
1413 // at least one argument.
1414 bool has_arguments() const {
1415 bool res = cell_count_no_header() >= TypeStackSlotEntries::per_arg_count();
1416 assert (!res || TypeEntriesAtCall::arguments_profiling_enabled(), "no profiling of arguments");
1417 return res;
1418 }
1419
1420 // Code generation support
1421 static ByteSize args_data_offset() {
1422 return cell_offset(VirtualCallData::static_cell_count()) + TypeEntriesAtCall::args_data_offset();
1423 }
1424
1425 ByteSize argument_type_offset(int i) {
1426 return _args.type_offset(i);
1427 }
1428
1429 ByteSize return_type_offset() {
1430 return _ret.type_offset();
1431 }
1432
1433 // GC support
1434 virtual void clean_weak_klass_links(bool always_clean) {
1435 ReceiverTypeData::clean_weak_klass_links(always_clean);
1436 if (has_arguments()) {
1437 _args.clean_weak_klass_links(always_clean);
1438 }
1439 if (has_return()) {
1440 _ret.clean_weak_klass_links(always_clean);
1441 }
1442 }
1443
1444 // CDS support
1445 virtual void metaspace_pointers_do(MetaspaceClosure* it) {
1446 ReceiverTypeData::metaspace_pointers_do(it);
1447 if (has_arguments()) {
1448 _args.metaspace_pointers_do(it);
1449 }
1450 if (has_return()) {
1451 _ret.metaspace_pointers_do(it);
1452 }
1453 }
1454
1455 virtual void print_data_on(outputStream* st, const char* extra = nullptr) const;
1456 };
1457
1458 // RetData
1459 //
1460 // A RetData is used to access profiling information for a ret bytecode.
1461 // It is composed of a count of the number of times that the ret has
1462 // been executed, followed by a series of triples of the form
1463 // (bci, count, di) which count the number of times that some bci was the
1464 // target of the ret and cache a corresponding data displacement.
1465 class RetData : public CounterData {
1466 protected:
1467 enum {
1468 bci0_offset = counter_cell_count,
1469 count0_offset,
1470 displacement0_offset,
1471 ret_row_cell_count = (displacement0_offset + 1) - bci0_offset
1472 };
1473
1474 void set_bci(uint row, int bci) {
1475 assert((uint)row < row_limit(), "oob");
1476 set_int_at(bci0_offset + row * ret_row_cell_count, bci);
1477 }
1478 void release_set_bci(uint row, int bci);
1479 void set_bci_count(uint row, uint count) {
1480 assert((uint)row < row_limit(), "oob");
1481 set_uint_at(count0_offset + row * ret_row_cell_count, count);
1482 }
1483 void set_bci_displacement(uint row, int disp) {
1484 set_int_at(displacement0_offset + row * ret_row_cell_count, disp);
1485 }
1486
1487 public:
1488 RetData(DataLayout* layout) : CounterData(layout) {
1489 assert(layout->tag() == DataLayout::ret_data_tag, "wrong type");
1490 }
1491
1492 virtual bool is_RetData() const { return true; }
1493
1494 enum {
1495 no_bci = -1 // value of bci when bci1/2 are not in use.
1496 };
1497
1498 static int static_cell_count() {
1499 return counter_cell_count + (uint) BciProfileWidth * ret_row_cell_count;
1500 }
1501
1502 virtual int cell_count() const {
1503 return static_cell_count();
1504 }
1505
1506 static uint row_limit() {
1507 return (uint) BciProfileWidth;
1508 }
1509 static int bci_cell_index(uint row) {
1510 return bci0_offset + row * ret_row_cell_count;
1511 }
1512 static int bci_count_cell_index(uint row) {
1513 return count0_offset + row * ret_row_cell_count;
1514 }
1515 static int bci_displacement_cell_index(uint row) {
1516 return displacement0_offset + row * ret_row_cell_count;
1517 }
1518
1519 // Direct accessors
1520 int bci(uint row) const {
1521 return int_at(bci_cell_index(row));
1522 }
1523 uint bci_count(uint row) const {
1524 return uint_at(bci_count_cell_index(row));
1525 }
1526 int bci_displacement(uint row) const {
1527 return int_at(bci_displacement_cell_index(row));
1528 }
1529
1530 // Interpreter Runtime support
1531 address fixup_ret(int return_bci, MethodData* mdo);
1532
1533 // Code generation support
1534 static ByteSize bci_offset(uint row) {
1535 return cell_offset(bci_cell_index(row));
1536 }
1537 static ByteSize bci_count_offset(uint row) {
1538 return cell_offset(bci_count_cell_index(row));
1539 }
1540 static ByteSize bci_displacement_offset(uint row) {
1541 return cell_offset(bci_displacement_cell_index(row));
1542 }
1543
1544 // Specific initialization.
1545 void post_initialize(BytecodeStream* stream, MethodData* mdo);
1546
1547 void print_data_on(outputStream* st, const char* extra = nullptr) const;
1548 };
1549
1550 // BranchData
1551 //
1552 // A BranchData is used to access profiling data for a two-way branch.
1553 // It consists of taken and not_taken counts as well as a data displacement
1554 // for the taken case.
1555 class BranchData : public JumpData {
1556 friend class VMStructs;
1557 friend class JVMCIVMStructs;
1558 protected:
1559 enum {
1560 not_taken_off_set = jump_cell_count,
1561 branch_cell_count
1562 };
1563
1564 void set_displacement(int displacement) {
1565 set_int_at(displacement_off_set, displacement);
1566 }
1567
1568 public:
1569 BranchData(DataLayout* layout) : JumpData(layout) {
1570 assert(layout->tag() == DataLayout::branch_data_tag || layout->tag() == DataLayout::acmp_data_tag, "wrong type");
1571 }
1572
1573 virtual bool is_BranchData() const { return true; }
1574
1575 static int static_cell_count() {
1576 return branch_cell_count;
1577 }
1578
1579 virtual int cell_count() const {
1580 return static_cell_count();
1581 }
1582
1583 // Direct accessor
1584 uint not_taken() const {
1585 return uint_at(not_taken_off_set);
1586 }
1587
1588 void set_not_taken(uint cnt) {
1589 set_uint_at(not_taken_off_set, cnt);
1590 }
1591
1592 uint inc_not_taken() {
1593 uint cnt = not_taken() + 1;
1594 // Did we wrap? Will compiler screw us??
1595 if (cnt == 0) cnt--;
1596 set_uint_at(not_taken_off_set, cnt);
1597 return cnt;
1598 }
1599
1600 // Code generation support
1601 static ByteSize not_taken_offset() {
1602 return cell_offset(not_taken_off_set);
1603 }
1604 static ByteSize branch_data_size() {
1605 return cell_offset(branch_cell_count);
1606 }
1607
1608 // Specific initialization.
1609 void post_initialize(BytecodeStream* stream, MethodData* mdo);
1610
1611 void print_data_on(outputStream* st, const char* extra = nullptr) const;
1612 };
1613
1614 // ArrayData
1615 //
1616 // A ArrayData is a base class for accessing profiling data which does
1617 // not have a statically known size. It consists of an array length
1618 // and an array start.
1619 class ArrayData : public ProfileData {
1620 friend class VMStructs;
1621 friend class JVMCIVMStructs;
1622 protected:
1623 friend class DataLayout;
1624
1625 enum {
1626 array_len_off_set,
1627 array_start_off_set
1628 };
1629
1630 uint array_uint_at(int index) const {
1631 int aindex = index + array_start_off_set;
1632 return uint_at(aindex);
1633 }
1634 int array_int_at(int index) const {
1635 int aindex = index + array_start_off_set;
1636 return int_at(aindex);
1637 }
1638 void array_set_int_at(int index, int value) {
1639 int aindex = index + array_start_off_set;
1640 set_int_at(aindex, value);
1641 }
1642
1643 // Code generation support for subclasses.
1644 static ByteSize array_element_offset(int index) {
1645 return cell_offset(array_start_off_set + index);
1646 }
1647
1648 public:
1649 ArrayData(DataLayout* layout) : ProfileData(layout) {}
1650
1651 virtual bool is_ArrayData() const { return true; }
1652
1653 static int static_cell_count() {
1654 return -1;
1655 }
1656
1657 int array_len() const {
1658 return int_at_unchecked(array_len_off_set);
1659 }
1660
1661 virtual int cell_count() const {
1662 return array_len() + 1;
1663 }
1664
1665 // Code generation support
1666 static ByteSize array_len_offset() {
1667 return cell_offset(array_len_off_set);
1668 }
1669 static ByteSize array_start_offset() {
1670 return cell_offset(array_start_off_set);
1671 }
1672 };
1673
1674 // MultiBranchData
1675 //
1676 // A MultiBranchData is used to access profiling information for
1677 // a multi-way branch (*switch bytecodes). It consists of a series
1678 // of (count, displacement) pairs, which count the number of times each
1679 // case was taken and specify the data displacement for each branch target.
1680 class MultiBranchData : public ArrayData {
1681 friend class VMStructs;
1682 friend class JVMCIVMStructs;
1683 protected:
1684 enum {
1685 default_count_off_set,
1686 default_disaplacement_off_set,
1687 case_array_start
1688 };
1689 enum {
1690 relative_count_off_set,
1691 relative_displacement_off_set,
1692 per_case_cell_count
1693 };
1694
1695 void set_default_displacement(int displacement) {
1696 array_set_int_at(default_disaplacement_off_set, displacement);
1697 }
1698 void set_displacement_at(int index, int displacement) {
1699 array_set_int_at(case_array_start +
1700 index * per_case_cell_count +
1701 relative_displacement_off_set,
1702 displacement);
1703 }
1704
1705 public:
1706 MultiBranchData(DataLayout* layout) : ArrayData(layout) {
1707 assert(layout->tag() == DataLayout::multi_branch_data_tag, "wrong type");
1708 }
1709
1710 virtual bool is_MultiBranchData() const { return true; }
1711
1712 static int compute_cell_count(BytecodeStream* stream);
1713
1714 int number_of_cases() const {
1715 int alen = array_len() - 2; // get rid of default case here.
1716 assert(alen % per_case_cell_count == 0, "must be even");
1717 return (alen / per_case_cell_count);
1718 }
1719
1720 uint default_count() const {
1721 return array_uint_at(default_count_off_set);
1722 }
1723 int default_displacement() const {
1724 return array_int_at(default_disaplacement_off_set);
1725 }
1726
1727 uint count_at(int index) const {
1728 return array_uint_at(case_array_start +
1729 index * per_case_cell_count +
1730 relative_count_off_set);
1731 }
1732 int displacement_at(int index) const {
1733 return array_int_at(case_array_start +
1734 index * per_case_cell_count +
1735 relative_displacement_off_set);
1736 }
1737
1738 // Code generation support
1739 static ByteSize default_count_offset() {
1740 return array_element_offset(default_count_off_set);
1741 }
1742 static ByteSize default_displacement_offset() {
1743 return array_element_offset(default_disaplacement_off_set);
1744 }
1745 static ByteSize case_count_offset(int index) {
1746 return case_array_offset() +
1747 (per_case_size() * index) +
1748 relative_count_offset();
1749 }
1750 static ByteSize case_array_offset() {
1751 return array_element_offset(case_array_start);
1752 }
1753 static ByteSize per_case_size() {
1754 return in_ByteSize(per_case_cell_count) * cell_size;
1755 }
1756 static ByteSize relative_count_offset() {
1757 return in_ByteSize(relative_count_off_set) * cell_size;
1758 }
1759 static ByteSize relative_displacement_offset() {
1760 return in_ByteSize(relative_displacement_off_set) * cell_size;
1761 }
1762
1763 // Specific initialization.
1764 void post_initialize(BytecodeStream* stream, MethodData* mdo);
1765
1766 void print_data_on(outputStream* st, const char* extra = nullptr) const;
1767 };
1768
1769 class ArgInfoData : public ArrayData {
1770
1771 public:
1772 ArgInfoData(DataLayout* layout) : ArrayData(layout) {
1773 assert(layout->tag() == DataLayout::arg_info_data_tag, "wrong type");
1774 }
1775
1776 virtual bool is_ArgInfoData() const { return true; }
1777
1778
1779 int size_of_args() const {
1780 return array_len();
1781 }
1782
1783 uint arg_modified(int arg) const {
1784 return array_uint_at(arg);
1785 }
1786
1787 void set_arg_modified(int arg, uint val) {
1788 array_set_int_at(arg, val);
1789 }
1790
1791 void print_data_on(outputStream* st, const char* extra = nullptr) const;
1792 };
1793
1794 // ParametersTypeData
1795 //
1796 // A ParametersTypeData is used to access profiling information about
1797 // types of parameters to a method
1798 class ParametersTypeData : public ArrayData {
1799
1800 private:
1801 TypeStackSlotEntries _parameters;
1802
1803 static int stack_slot_local_offset(int i) {
1804 assert_profiling_enabled();
1805 return array_start_off_set + TypeStackSlotEntries::stack_slot_local_offset(i);
1806 }
1807
1808 static int type_local_offset(int i) {
1809 assert_profiling_enabled();
1810 return array_start_off_set + TypeStackSlotEntries::type_local_offset(i);
1811 }
1812
1813 static bool profiling_enabled();
1814 static void assert_profiling_enabled() {
1815 assert(profiling_enabled(), "method parameters profiling should be on");
1816 }
1817
1818 public:
1819 ParametersTypeData(DataLayout* layout) : ArrayData(layout), _parameters(1, number_of_parameters()) {
1820 assert(layout->tag() == DataLayout::parameters_type_data_tag, "wrong type");
1821 // Some compilers (VC++) don't want this passed in member initialization list
1822 _parameters.set_profile_data(this);
1823 }
1824
1825 static int compute_cell_count(Method* m);
1826
1827 virtual bool is_ParametersTypeData() const { return true; }
1828
1829 virtual void post_initialize(BytecodeStream* stream, MethodData* mdo);
1830
1831 int number_of_parameters() const {
1832 return array_len() / TypeStackSlotEntries::per_arg_count();
1833 }
1834
1835 const TypeStackSlotEntries* parameters() const { return &_parameters; }
1836
1837 uint stack_slot(int i) const {
1838 return _parameters.stack_slot(i);
1839 }
1840
1841 void set_type(int i, Klass* k) {
1842 intptr_t current = _parameters.type(i);
1843 _parameters.set_type(i, TypeEntries::with_status((intptr_t)k, current));
1844 }
1845
1846 virtual void clean_weak_klass_links(bool always_clean) {
1847 _parameters.clean_weak_klass_links(always_clean);
1848 }
1849
1850 // CDS support
1851 virtual void metaspace_pointers_do(MetaspaceClosure* it) {
1852 _parameters.metaspace_pointers_do(it);
1853 }
1854
1855 virtual void print_data_on(outputStream* st, const char* extra = nullptr) const;
1856
1857 static ByteSize stack_slot_offset(int i) {
1858 return cell_offset(stack_slot_local_offset(i));
1859 }
1860
1861 static ByteSize type_offset(int i) {
1862 return cell_offset(type_local_offset(i));
1863 }
1864 };
1865
1866 // SpeculativeTrapData
1867 //
1868 // A SpeculativeTrapData is used to record traps due to type
1869 // speculation. It records the root of the compilation: that type
1870 // speculation is wrong in the context of one compilation (for
1871 // method1) doesn't mean it's wrong in the context of another one (for
1872 // method2). Type speculation could have more/different data in the
1873 // context of the compilation of method2 and it's worthwhile to try an
1874 // optimization that failed for compilation of method1 in the context
1875 // of compilation of method2.
1876 // Space for SpeculativeTrapData entries is allocated from the extra
1877 // data space in the MDO. If we run out of space, the trap data for
1878 // the ProfileData at that bci is updated.
1879 class SpeculativeTrapData : public ProfileData {
1880 protected:
1881 enum {
1882 speculative_trap_method,
1883 #ifndef _LP64
1884 // The size of the area for traps is a multiple of the header
1885 // size, 2 cells on 32 bits. Packed at the end of this area are
1886 // argument info entries (with tag
1887 // DataLayout::arg_info_data_tag). The logic in
1888 // MethodData::bci_to_extra_data() that guarantees traps don't
1889 // overflow over argument info entries assumes the size of a
1890 // SpeculativeTrapData is twice the header size. On 32 bits, a
1891 // SpeculativeTrapData must be 4 cells.
1892 padding,
1893 #endif
1894 speculative_trap_cell_count
1895 };
1896 public:
1897 SpeculativeTrapData(DataLayout* layout) : ProfileData(layout) {
1898 assert(layout->tag() == DataLayout::speculative_trap_data_tag, "wrong type");
1899 }
1900
1901 virtual bool is_SpeculativeTrapData() const { return true; }
1902
1903 static int static_cell_count() {
1904 return speculative_trap_cell_count;
1905 }
1906
1907 virtual int cell_count() const {
1908 return static_cell_count();
1909 }
1910
1911 // Direct accessor
1912 Method* method() const {
1913 return (Method*)intptr_at(speculative_trap_method);
1914 }
1915
1916 void set_method(Method* m) {
1917 assert(!m->is_old(), "cannot add old methods");
1918 set_intptr_at(speculative_trap_method, (intptr_t)m);
1919 }
1920
1921 static ByteSize method_offset() {
1922 return cell_offset(speculative_trap_method);
1923 }
1924
1925 // CDS support
1926 virtual void metaspace_pointers_do(MetaspaceClosure* it);
1927
1928 virtual void print_data_on(outputStream* st, const char* extra = nullptr) const;
1929 };
1930
1931 class ArrayStoreData : public ReceiverTypeData {
1932 private:
1933 enum {
1934 flat_array_flag = BitData::last_bit_data_flag,
1935 null_free_array_flag = flat_array_flag + 1,
1936 };
1937
1938 SingleTypeEntry _array;
1939
1940 public:
1941 ArrayStoreData(DataLayout* layout) :
1942 ReceiverTypeData(layout),
1943 _array(ReceiverTypeData::static_cell_count()) {
1944 assert(layout->tag() == DataLayout::array_store_data_tag, "wrong type");
1945 _array.set_profile_data(this);
1946 }
1947
1948 const SingleTypeEntry* array() const {
1949 return &_array;
1950 }
1951
1952 virtual bool is_ArrayStoreData() const { return true; }
1953
1954 static int static_cell_count() {
1955 return ReceiverTypeData::static_cell_count() + SingleTypeEntry::static_cell_count();
1956 }
1957
1958 virtual int cell_count() const {
1959 return static_cell_count();
1960 }
1961
1962 void set_flat_array() { set_flag_at(flat_array_flag); }
1963 bool flat_array() const { return flag_at(flat_array_flag); }
1964
1965 void set_null_free_array() { set_flag_at(null_free_array_flag); }
1966 bool null_free_array() const { return flag_at(null_free_array_flag); }
1967
1968 // Code generation support
1969 static int flat_array_byte_constant() {
1970 return flag_number_to_constant(flat_array_flag);
1971 }
1972
1973 static int null_free_array_byte_constant() {
1974 return flag_number_to_constant(null_free_array_flag);
1975 }
1976
1977 static ByteSize array_offset() {
1978 return cell_offset(ReceiverTypeData::static_cell_count());
1979 }
1980
1981 virtual void clean_weak_klass_links(bool always_clean) {
1982 ReceiverTypeData::clean_weak_klass_links(always_clean);
1983 _array.clean_weak_klass_links(always_clean);
1984 }
1985
1986 virtual void metaspace_pointers_do(MetaspaceClosure* it) {
1987 ReceiverTypeData::metaspace_pointers_do(it);
1988 _array.metaspace_pointers_do(it);
1989 }
1990
1991 static ByteSize array_store_data_size() {
1992 return cell_offset(static_cell_count());
1993 }
1994
1995 virtual void print_data_on(outputStream* st, const char* extra = nullptr) const;
1996 };
1997
1998 class ArrayLoadData : public BitData {
1999 private:
2000 enum {
2001 flat_array_flag = BitData::last_bit_data_flag,
2002 null_free_array_flag = flat_array_flag + 1,
2003 };
2004
2005 SingleTypeEntry _array;
2006 SingleTypeEntry _element;
2007
2008 public:
2009 ArrayLoadData(DataLayout* layout) :
2010 BitData(layout),
2011 _array(0),
2012 _element(SingleTypeEntry::static_cell_count()) {
2013 assert(layout->tag() == DataLayout::array_load_data_tag, "wrong type");
2014 _array.set_profile_data(this);
2015 _element.set_profile_data(this);
2016 }
2017
2018 const SingleTypeEntry* array() const {
2019 return &_array;
2020 }
2021
2022 const SingleTypeEntry* element() const {
2023 return &_element;
2024 }
2025
2026 virtual bool is_ArrayLoadData() const { return true; }
2027
2028 static int static_cell_count() {
2029 return SingleTypeEntry::static_cell_count() * 2;
2030 }
2031
2032 virtual int cell_count() const {
2033 return static_cell_count();
2034 }
2035
2036 void set_flat_array() { set_flag_at(flat_array_flag); }
2037 bool flat_array() const { return flag_at(flat_array_flag); }
2038
2039 void set_null_free_array() { set_flag_at(null_free_array_flag); }
2040 bool null_free_array() const { return flag_at(null_free_array_flag); }
2041
2042 // Code generation support
2043 static int flat_array_byte_constant() {
2044 return flag_number_to_constant(flat_array_flag);
2045 }
2046
2047 static int null_free_array_byte_constant() {
2048 return flag_number_to_constant(null_free_array_flag);
2049 }
2050
2051 static ByteSize array_offset() {
2052 return cell_offset(0);
2053 }
2054
2055 static ByteSize element_offset() {
2056 return cell_offset(SingleTypeEntry::static_cell_count());
2057 }
2058
2059 virtual void clean_weak_klass_links(bool always_clean) {
2060 _array.clean_weak_klass_links(always_clean);
2061 _element.clean_weak_klass_links(always_clean);
2062 }
2063
2064 virtual void metaspace_pointers_do(MetaspaceClosure* it) {
2065 _array.metaspace_pointers_do(it);
2066 _element.metaspace_pointers_do(it);
2067 }
2068
2069 static ByteSize array_load_data_size() {
2070 return cell_offset(static_cell_count());
2071 }
2072
2073 virtual void print_data_on(outputStream* st, const char* extra = nullptr) const;
2074 };
2075
2076 class ACmpData : public BranchData {
2077 private:
2078 enum {
2079 left_inline_type_flag = DataLayout::first_flag,
2080 right_inline_type_flag
2081 };
2082
2083 SingleTypeEntry _left;
2084 SingleTypeEntry _right;
2085
2086 public:
2087 ACmpData(DataLayout* layout) :
2088 BranchData(layout),
2089 _left(BranchData::static_cell_count()),
2090 _right(BranchData::static_cell_count() + SingleTypeEntry::static_cell_count()) {
2091 assert(layout->tag() == DataLayout::acmp_data_tag, "wrong type");
2092 _left.set_profile_data(this);
2093 _right.set_profile_data(this);
2094 }
2095
2096 const SingleTypeEntry* left() const {
2097 return &_left;
2098 }
2099
2100 const SingleTypeEntry* right() const {
2101 return &_right;
2102 }
2103
2104 virtual bool is_ACmpData() const { return true; }
2105
2106 static int static_cell_count() {
2107 return BranchData::static_cell_count() + SingleTypeEntry::static_cell_count() * 2;
2108 }
2109
2110 virtual int cell_count() const {
2111 return static_cell_count();
2112 }
2113
2114 void set_left_inline_type() { set_flag_at(left_inline_type_flag); }
2115 bool left_inline_type() const { return flag_at(left_inline_type_flag); }
2116
2117 void set_right_inline_type() { set_flag_at(right_inline_type_flag); }
2118 bool right_inline_type() const { return flag_at(right_inline_type_flag); }
2119
2120 // Code generation support
2121 static int left_inline_type_byte_constant() {
2122 return flag_number_to_constant(left_inline_type_flag);
2123 }
2124
2125 static int right_inline_type_byte_constant() {
2126 return flag_number_to_constant(right_inline_type_flag);
2127 }
2128
2129 static ByteSize left_offset() {
2130 return cell_offset(BranchData::static_cell_count());
2131 }
2132
2133 static ByteSize right_offset() {
2134 return cell_offset(BranchData::static_cell_count() + SingleTypeEntry::static_cell_count());
2135 }
2136
2137 virtual void clean_weak_klass_links(bool always_clean) {
2138 _left.clean_weak_klass_links(always_clean);
2139 _right.clean_weak_klass_links(always_clean);
2140 }
2141
2142 virtual void metaspace_pointers_do(MetaspaceClosure* it) {
2143 _left.metaspace_pointers_do(it);
2144 _right.metaspace_pointers_do(it);
2145 }
2146
2147 static ByteSize acmp_data_size() {
2148 return cell_offset(static_cell_count());
2149 }
2150
2151 virtual void print_data_on(outputStream* st, const char* extra = nullptr) const;
2152 };
2153
2154 // MethodData*
2155 //
2156 // A MethodData* holds information which has been collected about
2157 // a method. Its layout looks like this:
2158 //
2159 // -----------------------------
2160 // | header |
2161 // | klass |
2162 // -----------------------------
2163 // | method |
2164 // | size of the MethodData* |
2165 // -----------------------------
2166 // | Data entries... |
2167 // | (variable size) |
2168 // | |
2169 // . .
2170 // . .
2171 // . .
2172 // | |
2173 // -----------------------------
2174 //
2175 // The data entry area is a heterogeneous array of DataLayouts. Each
2176 // DataLayout in the array corresponds to a specific bytecode in the
2177 // method. The entries in the array are sorted by the corresponding
2178 // bytecode. Access to the data is via resource-allocated ProfileData,
2179 // which point to the underlying blocks of DataLayout structures.
2180 //
2181 // During interpretation, if profiling in enabled, the interpreter
2182 // maintains a method data pointer (mdp), which points at the entry
2183 // in the array corresponding to the current bci. In the course of
2184 // interpretation, when a bytecode is encountered that has profile data
2185 // associated with it, the entry pointed to by mdp is updated, then the
2186 // mdp is adjusted to point to the next appropriate DataLayout. If mdp
2187 // is null to begin with, the interpreter assumes that the current method
2188 // is not (yet) being profiled.
2189 //
2190 // In MethodData* parlance, "dp" is a "data pointer", the actual address
2191 // of a DataLayout element. A "di" is a "data index", the offset in bytes
2192 // from the base of the data entry array. A "displacement" is the byte offset
2193 // in certain ProfileData objects that indicate the amount the mdp must be
2194 // adjusted in the event of a change in control flow.
2195 //
2196
2197 class CleanExtraDataClosure : public StackObj {
2198 public:
2199 virtual bool is_live(Method* m) = 0;
2200 };
2201
2202
2203 #if INCLUDE_JVMCI
2204 // Encapsulates an encoded speculation reason. These are linked together in
2205 // a list that is atomically appended to during deoptimization. Entries are
2206 // never removed from the list.
2207 // @see jdk.vm.ci.hotspot.HotSpotSpeculationLog.HotSpotSpeculationEncoding
2208 class FailedSpeculation: public CHeapObj<mtCompiler> {
2209 private:
2210 // The length of HotSpotSpeculationEncoding.toByteArray(). The data itself
2211 // is an array embedded at the end of this object.
2212 int _data_len;
2213
2214 // Next entry in a linked list.
2215 FailedSpeculation* _next;
2216
2217 FailedSpeculation(address data, int data_len);
2218
2219 FailedSpeculation** next_adr() { return &_next; }
2220
2221 // Placement new operator for inlining the speculation data into
2222 // the FailedSpeculation object.
2223 void* operator new(size_t size, size_t fs_size) throw();
2224
2225 public:
2226 char* data() { return (char*)(((address) this) + sizeof(FailedSpeculation)); }
2227 int data_len() const { return _data_len; }
2228 FailedSpeculation* next() const { return _next; }
2229
2230 // Atomically appends a speculation from nm to the list whose head is at (*failed_speculations_address).
2231 // Returns false if the FailedSpeculation object could not be allocated.
2232 static bool add_failed_speculation(nmethod* nm, FailedSpeculation** failed_speculations_address, address speculation, int speculation_len);
2233
2234 // Frees all entries in the linked list whose head is at (*failed_speculations_address).
2235 static void free_failed_speculations(FailedSpeculation** failed_speculations_address);
2236 };
2237 #endif
2238
2239 class ciMethodData;
2240
2241 class MethodData : public Metadata {
2242 friend class VMStructs;
2243 friend class JVMCIVMStructs;
2244 friend class ProfileData;
2245 friend class TypeEntriesAtCall;
2246 friend class ciMethodData;
2247 friend class VM_ReinitializeMDO;
2248
2249 // If you add a new field that points to any metaspace object, you
2250 // must add this field to MethodData::metaspace_pointers_do().
2251
2252 // Back pointer to the Method*
2253 Method* _method;
2254
2255 // Size of this oop in bytes
2256 int _size;
2257
2258 // Cached hint for bci_to_dp and bci_to_data
2259 int _hint_di;
2260
2261 Mutex* volatile _extra_data_lock;
2262
2263 MethodData(const methodHandle& method);
2264
2265 void initialize();
2266
2267 public:
2268 MethodData();
2269
2270 static MethodData* allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS);
2271
2272 virtual bool is_methodData() const { return true; }
2273
2274 // Safely reinitialize the data in the MDO. This is intended as a testing facility as the
2275 // reinitialization is performed at a safepoint so it's isn't cheap and it doesn't ensure that all
2276 // readers will see consistent profile data.
2277 void reinitialize();
2278
2279 // Whole-method sticky bits and flags
2280 enum {
2281 _trap_hist_limit = Deoptimization::Reason_TRAP_HISTORY_LENGTH,
2282 _trap_hist_mask = max_jubyte,
2283 _extra_data_count = 4 // extra DataLayout headers, for trap history
2284 }; // Public flag values
2285
2286 // Compiler-related counters.
2287 class CompilerCounters {
2288 friend class VMStructs;
2289 friend class JVMCIVMStructs;
2290
2291 uint _nof_decompiles; // count of all nmethod removals
2292 uint _nof_overflow_recompiles; // recompile count, excluding recomp. bits
2293 uint _nof_overflow_traps; // trap count, excluding _trap_hist
2294 union {
2295 intptr_t _align;
2296 // JVMCI separates trap history for OSR compilations from normal compilations
2297 u1 _array[JVMCI_ONLY(2 *) MethodData::_trap_hist_limit];
2298 } _trap_hist;
2299
2300 public:
2301 CompilerCounters() : _nof_decompiles(0), _nof_overflow_recompiles(0), _nof_overflow_traps(0) {
2302 #ifndef ZERO
2303 // Some Zero platforms do not have expected alignment, and do not use
2304 // this code. static_assert would still fire and fail for them.
2305 static_assert(sizeof(_trap_hist) % HeapWordSize == 0, "align");
2306 #endif
2307 uint size_in_words = sizeof(_trap_hist) / HeapWordSize;
2308 Copy::zero_to_words((HeapWord*) &_trap_hist, size_in_words);
2309 }
2310
2311 // Return (uint)-1 for overflow.
2312 uint trap_count(int reason) const {
2313 assert((uint)reason < ARRAY_SIZE(_trap_hist._array), "oob");
2314 return (int)((_trap_hist._array[reason]+1) & _trap_hist_mask) - 1;
2315 }
2316
2317 uint inc_trap_count(int reason) {
2318 // Count another trap, anywhere in this method.
2319 assert(reason >= 0, "must be single trap");
2320 assert((uint)reason < ARRAY_SIZE(_trap_hist._array), "oob");
2321 uint cnt1 = 1 + _trap_hist._array[reason];
2322 if ((cnt1 & _trap_hist_mask) != 0) { // if no counter overflow...
2323 _trap_hist._array[reason] = (u1)cnt1;
2324 return cnt1;
2325 } else {
2326 return _trap_hist_mask + (++_nof_overflow_traps);
2327 }
2328 }
2329
2330 uint overflow_trap_count() const {
2331 return _nof_overflow_traps;
2332 }
2333 uint overflow_recompile_count() const {
2334 return _nof_overflow_recompiles;
2335 }
2336 uint inc_overflow_recompile_count() {
2337 return ++_nof_overflow_recompiles;
2338 }
2339 uint decompile_count() const {
2340 return _nof_decompiles;
2341 }
2342 uint inc_decompile_count() {
2343 return ++_nof_decompiles;
2344 }
2345
2346 // Support for code generation
2347 static ByteSize trap_history_offset() {
2348 return byte_offset_of(CompilerCounters, _trap_hist._array);
2349 }
2350 };
2351
2352 private:
2353 CompilerCounters _compiler_counters;
2354
2355 // Support for interprocedural escape analysis, from Thomas Kotzmann.
2356 intx _eflags; // flags on escape information
2357 intx _arg_local; // bit set of non-escaping arguments
2358 intx _arg_stack; // bit set of stack-allocatable arguments
2359 intx _arg_returned; // bit set of returned arguments
2360
2361 // How many invocations has this MDO seen?
2362 // These counters are used to determine the exact age of MDO.
2363 // We need those because in tiered a method can be concurrently
2364 // executed at different levels.
2365 InvocationCounter _invocation_counter;
2366 // Same for backedges.
2367 InvocationCounter _backedge_counter;
2368 // Counter values at the time profiling started.
2369 int _invocation_counter_start;
2370 int _backedge_counter_start;
2371 uint _tenure_traps;
2372 int _invoke_mask; // per-method Tier0InvokeNotifyFreqLog
2373 int _backedge_mask; // per-method Tier0BackedgeNotifyFreqLog
2374
2375 // Number of loops and blocks is computed when compiling the first
2376 // time with C1. It is used to determine if method is trivial.
2377 short _num_loops;
2378 short _num_blocks;
2379 // Does this method contain anything worth profiling?
2380 enum WouldProfile {unknown, no_profile, profile};
2381 WouldProfile _would_profile;
2382
2383 #if INCLUDE_JVMCI
2384 // Support for HotSpotMethodData.setCompiledIRSize(int)
2385 FailedSpeculation* _failed_speculations;
2386 int _jvmci_ir_size;
2387 #endif
2388
2389 // Size of _data array in bytes. (Excludes header and extra_data fields.)
2390 int _data_size;
2391
2392 // data index for the area dedicated to parameters. -1 if no
2393 // parameter profiling.
2394 enum { no_parameters = -2, parameters_uninitialized = -1 };
2395 int _parameters_type_data_di;
2396
2397 // data index of exception handler profiling data
2398 int _exception_handler_data_di;
2399
2400 // Beginning of the data entries
2401 // See comment in ciMethodData::load_data
2402 intptr_t _data[1];
2403
2404 // Helper for size computation
2405 static int compute_data_size(BytecodeStream* stream);
2406 static int bytecode_cell_count(Bytecodes::Code code);
2407 static bool is_speculative_trap_bytecode(Bytecodes::Code code);
2408 enum { no_profile_data = -1, variable_cell_count = -2 };
2409
2410 // Helper for initialization
2411 DataLayout* data_layout_at(int data_index) const {
2412 assert(data_index % sizeof(intptr_t) == 0, "unaligned");
2413 return (DataLayout*) (((address)_data) + data_index);
2414 }
2415
2416 static int single_exception_handler_data_cell_count() {
2417 return BitData::static_cell_count();
2418 }
2419
2420 static int single_exception_handler_data_size() {
2421 return DataLayout::compute_size_in_bytes(single_exception_handler_data_cell_count());
2422 }
2423
2424 DataLayout* exception_handler_data_at(int exception_handler_index) const {
2425 return data_layout_at(_exception_handler_data_di + (exception_handler_index * single_exception_handler_data_size()));
2426 }
2427
2428 int num_exception_handler_data() const {
2429 return exception_handlers_data_size() / single_exception_handler_data_size();
2430 }
2431
2432 // Initialize an individual data segment. Returns the size of
2433 // the segment in bytes.
2434 int initialize_data(BytecodeStream* stream, int data_index);
2435
2436 // Helper for data_at
2437 DataLayout* limit_data_position() const {
2438 return data_layout_at(_data_size);
2439 }
2440 bool out_of_bounds(int data_index) const {
2441 return data_index >= data_size();
2442 }
2443
2444 // Give each of the data entries a chance to perform specific
2445 // data initialization.
2446 void post_initialize(BytecodeStream* stream);
2447
2448 // hint accessors
2449 int hint_di() const { return _hint_di; }
2450 void set_hint_di(int di) {
2451 assert(!out_of_bounds(di), "hint_di out of bounds");
2452 _hint_di = di;
2453 }
2454
2455 DataLayout* data_layout_before(int bci) {
2456 // avoid SEGV on this edge case
2457 if (data_size() == 0)
2458 return nullptr;
2459 DataLayout* layout = data_layout_at(hint_di());
2460 if (layout->bci() <= bci)
2461 return layout;
2462 return data_layout_at(first_di());
2463 }
2464
2465 // What is the index of the first data entry?
2466 int first_di() const { return 0; }
2467
2468 ProfileData* bci_to_extra_data_find(int bci, Method* m, DataLayout*& dp);
2469 // Find or create an extra ProfileData:
2470 ProfileData* bci_to_extra_data(int bci, Method* m, bool create_if_missing);
2471
2472 // return the argument info cell
2473 ArgInfoData *arg_info();
2474
2475 enum {
2476 no_type_profile = 0,
2477 type_profile_jsr292 = 1,
2478 type_profile_all = 2
2479 };
2480
2481 static bool profile_jsr292(const methodHandle& m, int bci);
2482 static bool profile_unsafe(const methodHandle& m, int bci);
2483 static bool profile_memory_access(const methodHandle& m, int bci);
2484 static int profile_arguments_flag();
2485 static bool profile_all_arguments();
2486 static bool profile_arguments_for_invoke(const methodHandle& m, int bci);
2487 static int profile_return_flag();
2488 static bool profile_all_return();
2489 static bool profile_return_for_invoke(const methodHandle& m, int bci);
2490 static int profile_parameters_flag();
2491 static bool profile_parameters_jsr292_only();
2492 static bool profile_all_parameters();
2493
2494 void clean_extra_data_helper(DataLayout* dp, int shift, bool reset = false);
2495 void verify_extra_data_clean(CleanExtraDataClosure* cl);
2496
2497 DataLayout* exception_handler_bci_to_data_helper(int bci);
2498
2499 public:
2500 void clean_extra_data(CleanExtraDataClosure* cl);
2501
2502 static int header_size() {
2503 return sizeof(MethodData)/wordSize;
2504 }
2505
2506 // Compute the size of a MethodData* before it is created.
2507 static int compute_allocation_size_in_bytes(const methodHandle& method);
2508 static int compute_allocation_size_in_words(const methodHandle& method);
2509 static int compute_extra_data_count(int data_size, int empty_bc_count, bool needs_speculative_traps);
2510
2511 // Determine if a given bytecode can have profile information.
2512 static bool bytecode_has_profile(Bytecodes::Code code) {
2513 return bytecode_cell_count(code) != no_profile_data;
2514 }
2515
2516 // reset into original state
2517 void init();
2518
2519 // My size
2520 int size_in_bytes() const { return _size; }
2521 int size() const { return align_metadata_size(align_up(_size, BytesPerWord)/BytesPerWord); }
2522
2523 int invocation_count() {
2524 if (invocation_counter()->carry()) {
2525 return InvocationCounter::count_limit;
2526 }
2527 return invocation_counter()->count();
2528 }
2529 int backedge_count() {
2530 if (backedge_counter()->carry()) {
2531 return InvocationCounter::count_limit;
2532 }
2533 return backedge_counter()->count();
2534 }
2535
2536 int invocation_count_start() {
2537 if (invocation_counter()->carry()) {
2538 return 0;
2539 }
2540 return _invocation_counter_start;
2541 }
2542
2543 int backedge_count_start() {
2544 if (backedge_counter()->carry()) {
2545 return 0;
2546 }
2547 return _backedge_counter_start;
2548 }
2549
2550 int invocation_count_delta() { return invocation_count() - invocation_count_start(); }
2551 int backedge_count_delta() { return backedge_count() - backedge_count_start(); }
2552
2553 void reset_start_counters() {
2554 _invocation_counter_start = invocation_count();
2555 _backedge_counter_start = backedge_count();
2556 }
2557
2558 InvocationCounter* invocation_counter() { return &_invocation_counter; }
2559 InvocationCounter* backedge_counter() { return &_backedge_counter; }
2560
2561 #if INCLUDE_JVMCI
2562 FailedSpeculation** get_failed_speculations_address() {
2563 return &_failed_speculations;
2564 }
2565 #endif
2566
2567 #if INCLUDE_CDS
2568 void remove_unshareable_info();
2569 void restore_unshareable_info(TRAPS);
2570 #endif
2571
2572 void set_would_profile(bool p) { _would_profile = p ? profile : no_profile; }
2573 bool would_profile() const { return _would_profile != no_profile; }
2574
2575 int num_loops() const { return _num_loops; }
2576 void set_num_loops(short n) { _num_loops = n; }
2577 int num_blocks() const { return _num_blocks; }
2578 void set_num_blocks(short n) { _num_blocks = n; }
2579
2580 bool is_mature() const;
2581
2582 // Support for interprocedural escape analysis, from Thomas Kotzmann.
2583 enum EscapeFlag {
2584 estimated = 1 << 0,
2585 return_local = 1 << 1,
2586 return_allocated = 1 << 2,
2587 allocated_escapes = 1 << 3,
2588 unknown_modified = 1 << 4
2589 };
2590
2591 intx eflags() { return _eflags; }
2592 intx arg_local() { return _arg_local; }
2593 intx arg_stack() { return _arg_stack; }
2594 intx arg_returned() { return _arg_returned; }
2595 uint arg_modified(int a);
2596 void set_eflags(intx v) { _eflags = v; }
2597 void set_arg_local(intx v) { _arg_local = v; }
2598 void set_arg_stack(intx v) { _arg_stack = v; }
2599 void set_arg_returned(intx v) { _arg_returned = v; }
2600 void set_arg_modified(int a, uint v);
2601 void clear_escape_info() { _eflags = _arg_local = _arg_stack = _arg_returned = 0; }
2602
2603 // Location and size of data area
2604 address data_base() const {
2605 return (address) _data;
2606 }
2607 int data_size() const {
2608 return _data_size;
2609 }
2610
2611 int parameters_size_in_bytes() const {
2612 return pointer_delta_as_int((address) parameters_data_limit(), (address) parameters_data_base());
2613 }
2614
2615 int exception_handlers_data_size() const {
2616 return pointer_delta_as_int((address) exception_handler_data_limit(), (address) exception_handler_data_base());
2617 }
2618
2619 // Accessors
2620 Method* method() const { return _method; }
2621
2622 // Get the data at an arbitrary (sort of) data index.
2623 ProfileData* data_at(int data_index) const;
2624
2625 // Walk through the data in order.
2626 ProfileData* first_data() const { return data_at(first_di()); }
2627 ProfileData* next_data(ProfileData* current) const;
2628 DataLayout* next_data_layout(DataLayout* current) const;
2629 bool is_valid(ProfileData* current) const { return current != nullptr; }
2630 bool is_valid(DataLayout* current) const { return current != nullptr; }
2631
2632 // Convert a dp (data pointer) to a di (data index).
2633 int dp_to_di(address dp) const {
2634 return (int)(dp - ((address)_data));
2635 }
2636
2637 // bci to di/dp conversion.
2638 address bci_to_dp(int bci);
2639 int bci_to_di(int bci) {
2640 return dp_to_di(bci_to_dp(bci));
2641 }
2642
2643 // Get the data at an arbitrary bci, or null if there is none.
2644 ProfileData* bci_to_data(int bci);
2645
2646 // Same, but try to create an extra_data record if one is needed:
2647 ProfileData* allocate_bci_to_data(int bci, Method* m) {
2648 check_extra_data_locked();
2649
2650 ProfileData* data = nullptr;
2651 // If m not null, try to allocate a SpeculativeTrapData entry
2652 if (m == nullptr) {
2653 data = bci_to_data(bci);
2654 }
2655 if (data != nullptr) {
2656 return data;
2657 }
2658 data = bci_to_extra_data(bci, m, true);
2659 if (data != nullptr) {
2660 return data;
2661 }
2662 // If SpeculativeTrapData allocation fails try to allocate a
2663 // regular entry
2664 data = bci_to_data(bci);
2665 if (data != nullptr) {
2666 return data;
2667 }
2668 return bci_to_extra_data(bci, nullptr, true);
2669 }
2670
2671 BitData* exception_handler_bci_to_data_or_null(int bci);
2672 BitData exception_handler_bci_to_data(int bci);
2673
2674 // Add a handful of extra data records, for trap tracking.
2675 // Only valid after 'set_size' is called at the end of MethodData::initialize
2676 DataLayout* extra_data_base() const {
2677 check_extra_data_locked();
2678 return limit_data_position();
2679 }
2680 DataLayout* extra_data_limit() const { return (DataLayout*)((address)this + size_in_bytes()); }
2681 // pointers to sections in extra data
2682 DataLayout* args_data_limit() const { return parameters_data_base(); }
2683 DataLayout* parameters_data_base() const {
2684 assert(_parameters_type_data_di != parameters_uninitialized, "called too early");
2685 return _parameters_type_data_di != no_parameters ? data_layout_at(_parameters_type_data_di) : parameters_data_limit();
2686 }
2687 DataLayout* parameters_data_limit() const {
2688 assert(_parameters_type_data_di != parameters_uninitialized, "called too early");
2689 return exception_handler_data_base();
2690 }
2691 DataLayout* exception_handler_data_base() const { return data_layout_at(_exception_handler_data_di); }
2692 DataLayout* exception_handler_data_limit() const { return extra_data_limit(); }
2693
2694 int extra_data_size() const { return (int)((address)extra_data_limit() - (address)limit_data_position()); }
2695 static DataLayout* next_extra(DataLayout* dp);
2696
2697 // Return (uint)-1 for overflow.
2698 uint trap_count(int reason) const {
2699 return _compiler_counters.trap_count(reason);
2700 }
2701 // For loops:
2702 static uint trap_reason_limit() { return _trap_hist_limit; }
2703 static uint trap_count_limit() { return _trap_hist_mask; }
2704 uint inc_trap_count(int reason) {
2705 return _compiler_counters.inc_trap_count(reason);
2706 }
2707
2708 uint overflow_trap_count() const {
2709 return _compiler_counters.overflow_trap_count();
2710 }
2711 uint overflow_recompile_count() const {
2712 return _compiler_counters.overflow_recompile_count();
2713 }
2714 uint inc_overflow_recompile_count() {
2715 return _compiler_counters.inc_overflow_recompile_count();
2716 }
2717 uint decompile_count() const {
2718 return _compiler_counters.decompile_count();
2719 }
2720 uint inc_decompile_count() {
2721 uint dec_count = _compiler_counters.inc_decompile_count();
2722 if (dec_count > (uint)PerMethodRecompilationCutoff) {
2723 method()->set_not_compilable("decompile_count > PerMethodRecompilationCutoff", CompLevel_full_optimization);
2724 }
2725 return dec_count;
2726 }
2727 uint tenure_traps() const {
2728 return _tenure_traps;
2729 }
2730 void inc_tenure_traps() {
2731 _tenure_traps += 1;
2732 }
2733
2734 // Return pointer to area dedicated to parameters in MDO
2735 ParametersTypeData* parameters_type_data() const {
2736 assert(_parameters_type_data_di != parameters_uninitialized, "called too early");
2737 return _parameters_type_data_di != no_parameters ? data_layout_at(_parameters_type_data_di)->data_in()->as_ParametersTypeData() : nullptr;
2738 }
2739
2740 int parameters_type_data_di() const {
2741 assert(_parameters_type_data_di != parameters_uninitialized, "called too early");
2742 return _parameters_type_data_di != no_parameters ? _parameters_type_data_di : exception_handlers_data_di();
2743 }
2744
2745 int exception_handlers_data_di() const {
2746 return _exception_handler_data_di;
2747 }
2748
2749 // Support for code generation
2750 static ByteSize data_offset() {
2751 return byte_offset_of(MethodData, _data[0]);
2752 }
2753
2754 static ByteSize trap_history_offset() {
2755 return byte_offset_of(MethodData, _compiler_counters) + CompilerCounters::trap_history_offset();
2756 }
2757
2758 static ByteSize invocation_counter_offset() {
2759 return byte_offset_of(MethodData, _invocation_counter);
2760 }
2761
2762 static ByteSize backedge_counter_offset() {
2763 return byte_offset_of(MethodData, _backedge_counter);
2764 }
2765
2766 static ByteSize invoke_mask_offset() {
2767 return byte_offset_of(MethodData, _invoke_mask);
2768 }
2769
2770 static ByteSize backedge_mask_offset() {
2771 return byte_offset_of(MethodData, _backedge_mask);
2772 }
2773
2774 static ByteSize parameters_type_data_di_offset() {
2775 return byte_offset_of(MethodData, _parameters_type_data_di);
2776 }
2777
2778 virtual void metaspace_pointers_do(MetaspaceClosure* iter);
2779 virtual MetaspaceObj::Type type() const { return MethodDataType; }
2780
2781 // Deallocation support
2782 void deallocate_contents(ClassLoaderData* loader_data);
2783 void release_C_heap_structures();
2784
2785 // GC support
2786 void set_size(int object_size_in_bytes) { _size = object_size_in_bytes; }
2787
2788 // Printing
2789 void print_on (outputStream* st) const;
2790 void print_value_on(outputStream* st) const;
2791
2792 // printing support for method data
2793 void print_data_on(outputStream* st) const;
2794
2795 const char* internal_name() const { return "{method data}"; }
2796
2797 // verification
2798 void verify_on(outputStream* st);
2799 void verify_data_on(outputStream* st);
2800
2801 static bool profile_parameters_for_method(const methodHandle& m);
2802 static bool profile_arguments();
2803 static bool profile_arguments_jsr292_only();
2804 static bool profile_return();
2805 static bool profile_parameters();
2806 static bool profile_return_jsr292_only();
2807
2808 void clean_method_data(bool always_clean);
2809 void clean_weak_method_links();
2810 Mutex* extra_data_lock();
2811 void check_extra_data_locked() const NOT_DEBUG_RETURN;
2812 };
2813
2814 #endif // SHARE_OOPS_METHODDATA_HPP