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