Fri, 20 Mar 2009 23:19:36 -0700
6814659: separable cleanups and subroutines for 6655638
Summary: preparatory but separable changes for method handles
Reviewed-by: kvn, never
1 /*
2 * Copyright 2000-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 class BytecodeStream;
27 // The MethodData object collects counts and other profile information
28 // during zeroth-tier (interpretive) and first-tier execution.
29 // The profile is used later by compilation heuristics. Some heuristics
30 // enable use of aggressive (or "heroic") optimizations. An aggressive
31 // optimization often has a down-side, a corner case that it handles
32 // poorly, but which is thought to be rare. The profile provides
33 // evidence of this rarity for a given method or even BCI. It allows
34 // the compiler to back out of the optimization at places where it
35 // has historically been a poor choice. Other heuristics try to use
36 // specific information gathered about types observed at a given site.
37 //
38 // All data in the profile is approximate. It is expected to be accurate
39 // on the whole, but the system expects occasional inaccuraces, due to
40 // counter overflow, multiprocessor races during data collection, space
41 // limitations, missing MDO blocks, etc. Bad or missing data will degrade
42 // optimization quality but will not affect correctness. Also, each MDO
43 // is marked with its birth-date ("creation_mileage") which can be used
44 // to assess the quality ("maturity") of its data.
45 //
46 // Short (<32-bit) counters are designed to overflow to a known "saturated"
47 // state. Also, certain recorded per-BCI events are given one-bit counters
48 // which overflow to a saturated state which applied to all counters at
49 // that BCI. In other words, there is a small lattice which approximates
50 // the ideal of an infinite-precision counter for each event at each BCI,
51 // and the lattice quickly "bottoms out" in a state where all counters
52 // are taken to be indefinitely large.
53 //
54 // The reader will find many data races in profile gathering code, starting
55 // with invocation counter incrementation. None of these races harm correct
56 // execution of the compiled code.
58 // DataLayout
59 //
60 // Overlay for generic profiling data.
61 class DataLayout VALUE_OBJ_CLASS_SPEC {
62 private:
63 // Every data layout begins with a header. This header
64 // contains a tag, which is used to indicate the size/layout
65 // of the data, 4 bits of flags, which can be used in any way,
66 // 4 bits of trap history (none/one reason/many reasons),
67 // and a bci, which is used to tie this piece of data to a
68 // specific bci in the bytecodes.
69 union {
70 intptr_t _bits;
71 struct {
72 u1 _tag;
73 u1 _flags;
74 u2 _bci;
75 } _struct;
76 } _header;
78 // The data layout has an arbitrary number of cells, each sized
79 // to accomodate a pointer or an integer.
80 intptr_t _cells[1];
82 // Some types of data layouts need a length field.
83 static bool needs_array_len(u1 tag);
85 public:
86 enum {
87 counter_increment = 1
88 };
90 enum {
91 cell_size = sizeof(intptr_t)
92 };
94 // Tag values
95 enum {
96 no_tag,
97 bit_data_tag,
98 counter_data_tag,
99 jump_data_tag,
100 receiver_type_data_tag,
101 virtual_call_data_tag,
102 ret_data_tag,
103 branch_data_tag,
104 multi_branch_data_tag,
105 arg_info_data_tag
106 };
108 enum {
109 // The _struct._flags word is formatted as [trap_state:4 | flags:4].
110 // The trap state breaks down further as [recompile:1 | reason:3].
111 // This further breakdown is defined in deoptimization.cpp.
112 // See Deoptimization::trap_state_reason for an assert that
113 // trap_bits is big enough to hold reasons < Reason_RECORDED_LIMIT.
114 //
115 // The trap_state is collected only if ProfileTraps is true.
116 trap_bits = 1+3, // 3: enough to distinguish [0..Reason_RECORDED_LIMIT].
117 trap_shift = BitsPerByte - trap_bits,
118 trap_mask = right_n_bits(trap_bits),
119 trap_mask_in_place = (trap_mask << trap_shift),
120 flag_limit = trap_shift,
121 flag_mask = right_n_bits(flag_limit),
122 first_flag = 0
123 };
125 // Size computation
126 static int header_size_in_bytes() {
127 return cell_size;
128 }
129 static int header_size_in_cells() {
130 return 1;
131 }
133 static int compute_size_in_bytes(int cell_count) {
134 return header_size_in_bytes() + cell_count * cell_size;
135 }
137 // Initialization
138 void initialize(u1 tag, u2 bci, int cell_count);
140 // Accessors
141 u1 tag() {
142 return _header._struct._tag;
143 }
145 // Return a few bits of trap state. Range is [0..trap_mask].
146 // The state tells if traps with zero, one, or many reasons have occurred.
147 // It also tells whether zero or many recompilations have occurred.
148 // The associated trap histogram in the MDO itself tells whether
149 // traps are common or not. If a BCI shows that a trap X has
150 // occurred, and the MDO shows N occurrences of X, we make the
151 // simplifying assumption that all N occurrences can be blamed
152 // on that BCI.
153 int trap_state() {
154 return ((_header._struct._flags >> trap_shift) & trap_mask);
155 }
157 void set_trap_state(int new_state) {
158 assert(ProfileTraps, "used only under +ProfileTraps");
159 uint old_flags = (_header._struct._flags & flag_mask);
160 _header._struct._flags = (new_state << trap_shift) | old_flags;
161 }
163 u1 flags() {
164 return _header._struct._flags;
165 }
167 u2 bci() {
168 return _header._struct._bci;
169 }
171 void set_header(intptr_t value) {
172 _header._bits = value;
173 }
174 void release_set_header(intptr_t value) {
175 OrderAccess::release_store_ptr(&_header._bits, value);
176 }
177 intptr_t header() {
178 return _header._bits;
179 }
180 void set_cell_at(int index, intptr_t value) {
181 _cells[index] = value;
182 }
183 void release_set_cell_at(int index, intptr_t value) {
184 OrderAccess::release_store_ptr(&_cells[index], value);
185 }
186 intptr_t cell_at(int index) {
187 return _cells[index];
188 }
189 intptr_t* adr_cell_at(int index) {
190 return &_cells[index];
191 }
192 oop* adr_oop_at(int index) {
193 return (oop*)&(_cells[index]);
194 }
196 void set_flag_at(int flag_number) {
197 assert(flag_number < flag_limit, "oob");
198 _header._struct._flags |= (0x1 << flag_number);
199 }
200 bool flag_at(int flag_number) {
201 assert(flag_number < flag_limit, "oob");
202 return (_header._struct._flags & (0x1 << flag_number)) != 0;
203 }
205 // Low-level support for code generation.
206 static ByteSize header_offset() {
207 return byte_offset_of(DataLayout, _header);
208 }
209 static ByteSize tag_offset() {
210 return byte_offset_of(DataLayout, _header._struct._tag);
211 }
212 static ByteSize flags_offset() {
213 return byte_offset_of(DataLayout, _header._struct._flags);
214 }
215 static ByteSize bci_offset() {
216 return byte_offset_of(DataLayout, _header._struct._bci);
217 }
218 static ByteSize cell_offset(int index) {
219 return byte_offset_of(DataLayout, _cells[index]);
220 }
221 // Return a value which, when or-ed as a byte into _flags, sets the flag.
222 static int flag_number_to_byte_constant(int flag_number) {
223 assert(0 <= flag_number && flag_number < flag_limit, "oob");
224 DataLayout temp; temp.set_header(0);
225 temp.set_flag_at(flag_number);
226 return temp._header._struct._flags;
227 }
228 // Return a value which, when or-ed as a word into _header, sets the flag.
229 static intptr_t flag_mask_to_header_mask(int byte_constant) {
230 DataLayout temp; temp.set_header(0);
231 temp._header._struct._flags = byte_constant;
232 return temp._header._bits;
233 }
234 };
237 // ProfileData class hierarchy
238 class ProfileData;
239 class BitData;
240 class CounterData;
241 class ReceiverTypeData;
242 class VirtualCallData;
243 class RetData;
244 class JumpData;
245 class BranchData;
246 class ArrayData;
247 class MultiBranchData;
248 class ArgInfoData;
251 // ProfileData
252 //
253 // A ProfileData object is created to refer to a section of profiling
254 // data in a structured way.
255 class ProfileData : public ResourceObj {
256 private:
257 #ifndef PRODUCT
258 enum {
259 tab_width_one = 16,
260 tab_width_two = 36
261 };
262 #endif // !PRODUCT
264 // This is a pointer to a section of profiling data.
265 DataLayout* _data;
267 protected:
268 DataLayout* data() { return _data; }
270 enum {
271 cell_size = DataLayout::cell_size
272 };
274 public:
275 // How many cells are in this?
276 virtual int cell_count() {
277 ShouldNotReachHere();
278 return -1;
279 }
281 // Return the size of this data.
282 int size_in_bytes() {
283 return DataLayout::compute_size_in_bytes(cell_count());
284 }
286 protected:
287 // Low-level accessors for underlying data
288 void set_intptr_at(int index, intptr_t value) {
289 assert(0 <= index && index < cell_count(), "oob");
290 data()->set_cell_at(index, value);
291 }
292 void release_set_intptr_at(int index, intptr_t value) {
293 assert(0 <= index && index < cell_count(), "oob");
294 data()->release_set_cell_at(index, value);
295 }
296 intptr_t intptr_at(int index) {
297 assert(0 <= index && index < cell_count(), "oob");
298 return data()->cell_at(index);
299 }
300 void set_uint_at(int index, uint value) {
301 set_intptr_at(index, (intptr_t) value);
302 }
303 void release_set_uint_at(int index, uint value) {
304 release_set_intptr_at(index, (intptr_t) value);
305 }
306 uint uint_at(int index) {
307 return (uint)intptr_at(index);
308 }
309 void set_int_at(int index, int value) {
310 set_intptr_at(index, (intptr_t) value);
311 }
312 void release_set_int_at(int index, int value) {
313 release_set_intptr_at(index, (intptr_t) value);
314 }
315 int int_at(int index) {
316 return (int)intptr_at(index);
317 }
318 int int_at_unchecked(int index) {
319 return (int)data()->cell_at(index);
320 }
321 void set_oop_at(int index, oop value) {
322 set_intptr_at(index, (intptr_t) value);
323 }
324 oop oop_at(int index) {
325 return (oop)intptr_at(index);
326 }
327 oop* adr_oop_at(int index) {
328 assert(0 <= index && index < cell_count(), "oob");
329 return data()->adr_oop_at(index);
330 }
332 void set_flag_at(int flag_number) {
333 data()->set_flag_at(flag_number);
334 }
335 bool flag_at(int flag_number) {
336 return data()->flag_at(flag_number);
337 }
339 // two convenient imports for use by subclasses:
340 static ByteSize cell_offset(int index) {
341 return DataLayout::cell_offset(index);
342 }
343 static int flag_number_to_byte_constant(int flag_number) {
344 return DataLayout::flag_number_to_byte_constant(flag_number);
345 }
347 ProfileData(DataLayout* data) {
348 _data = data;
349 }
351 public:
352 // Constructor for invalid ProfileData.
353 ProfileData();
355 u2 bci() {
356 return data()->bci();
357 }
359 address dp() {
360 return (address)_data;
361 }
363 int trap_state() {
364 return data()->trap_state();
365 }
366 void set_trap_state(int new_state) {
367 data()->set_trap_state(new_state);
368 }
370 // Type checking
371 virtual bool is_BitData() { return false; }
372 virtual bool is_CounterData() { return false; }
373 virtual bool is_JumpData() { return false; }
374 virtual bool is_ReceiverTypeData(){ return false; }
375 virtual bool is_VirtualCallData() { return false; }
376 virtual bool is_RetData() { return false; }
377 virtual bool is_BranchData() { return false; }
378 virtual bool is_ArrayData() { return false; }
379 virtual bool is_MultiBranchData() { return false; }
380 virtual bool is_ArgInfoData() { return false; }
383 BitData* as_BitData() {
384 assert(is_BitData(), "wrong type");
385 return is_BitData() ? (BitData*) this : NULL;
386 }
387 CounterData* as_CounterData() {
388 assert(is_CounterData(), "wrong type");
389 return is_CounterData() ? (CounterData*) this : NULL;
390 }
391 JumpData* as_JumpData() {
392 assert(is_JumpData(), "wrong type");
393 return is_JumpData() ? (JumpData*) this : NULL;
394 }
395 ReceiverTypeData* as_ReceiverTypeData() {
396 assert(is_ReceiverTypeData(), "wrong type");
397 return is_ReceiverTypeData() ? (ReceiverTypeData*)this : NULL;
398 }
399 VirtualCallData* as_VirtualCallData() {
400 assert(is_VirtualCallData(), "wrong type");
401 return is_VirtualCallData() ? (VirtualCallData*)this : NULL;
402 }
403 RetData* as_RetData() {
404 assert(is_RetData(), "wrong type");
405 return is_RetData() ? (RetData*) this : NULL;
406 }
407 BranchData* as_BranchData() {
408 assert(is_BranchData(), "wrong type");
409 return is_BranchData() ? (BranchData*) this : NULL;
410 }
411 ArrayData* as_ArrayData() {
412 assert(is_ArrayData(), "wrong type");
413 return is_ArrayData() ? (ArrayData*) this : NULL;
414 }
415 MultiBranchData* as_MultiBranchData() {
416 assert(is_MultiBranchData(), "wrong type");
417 return is_MultiBranchData() ? (MultiBranchData*)this : NULL;
418 }
419 ArgInfoData* as_ArgInfoData() {
420 assert(is_ArgInfoData(), "wrong type");
421 return is_ArgInfoData() ? (ArgInfoData*)this : NULL;
422 }
425 // Subclass specific initialization
426 virtual void post_initialize(BytecodeStream* stream, methodDataOop mdo) {}
428 // GC support
429 virtual void follow_contents() {}
430 virtual void oop_iterate(OopClosure* blk) {}
431 virtual void oop_iterate_m(OopClosure* blk, MemRegion mr) {}
432 virtual void adjust_pointers() {}
434 #ifndef SERIALGC
435 // Parallel old support
436 virtual void follow_contents(ParCompactionManager* cm) {}
437 virtual void update_pointers() {}
438 virtual void update_pointers(HeapWord* beg_addr, HeapWord* end_addr) {}
439 #endif // SERIALGC
441 // CI translation: ProfileData can represent both MethodDataOop data
442 // as well as CIMethodData data. This function is provided for translating
443 // an oop in a ProfileData to the ci equivalent. Generally speaking,
444 // most ProfileData don't require any translation, so we provide the null
445 // translation here, and the required translators are in the ci subclasses.
446 virtual void translate_from(ProfileData* data) {}
448 virtual void print_data_on(outputStream* st) {
449 ShouldNotReachHere();
450 }
452 #ifndef PRODUCT
453 void print_shared(outputStream* st, const char* name);
454 void tab(outputStream* st);
455 #endif
456 };
458 // BitData
459 //
460 // A BitData holds a flag or two in its header.
461 class BitData : public ProfileData {
462 protected:
463 enum {
464 // null_seen:
465 // saw a null operand (cast/aastore/instanceof)
466 null_seen_flag = DataLayout::first_flag + 0
467 };
468 enum { bit_cell_count = 0 }; // no additional data fields needed.
469 public:
470 BitData(DataLayout* layout) : ProfileData(layout) {
471 }
473 virtual bool is_BitData() { return true; }
475 static int static_cell_count() {
476 return bit_cell_count;
477 }
479 virtual int cell_count() {
480 return static_cell_count();
481 }
483 // Accessor
485 // The null_seen flag bit is specially known to the interpreter.
486 // Consulting it allows the compiler to avoid setting up null_check traps.
487 bool null_seen() { return flag_at(null_seen_flag); }
488 void set_null_seen() { set_flag_at(null_seen_flag); }
491 // Code generation support
492 static int null_seen_byte_constant() {
493 return flag_number_to_byte_constant(null_seen_flag);
494 }
496 static ByteSize bit_data_size() {
497 return cell_offset(bit_cell_count);
498 }
500 #ifndef PRODUCT
501 void print_data_on(outputStream* st);
502 #endif
503 };
505 // CounterData
506 //
507 // A CounterData corresponds to a simple counter.
508 class CounterData : public BitData {
509 protected:
510 enum {
511 count_off,
512 counter_cell_count
513 };
514 public:
515 CounterData(DataLayout* layout) : BitData(layout) {}
517 virtual bool is_CounterData() { return true; }
519 static int static_cell_count() {
520 return counter_cell_count;
521 }
523 virtual int cell_count() {
524 return static_cell_count();
525 }
527 // Direct accessor
528 uint count() {
529 return uint_at(count_off);
530 }
532 // Code generation support
533 static ByteSize count_offset() {
534 return cell_offset(count_off);
535 }
536 static ByteSize counter_data_size() {
537 return cell_offset(counter_cell_count);
538 }
540 #ifndef PRODUCT
541 void print_data_on(outputStream* st);
542 #endif
543 };
545 // JumpData
546 //
547 // A JumpData is used to access profiling information for a direct
548 // branch. It is a counter, used for counting the number of branches,
549 // plus a data displacement, used for realigning the data pointer to
550 // the corresponding target bci.
551 class JumpData : public ProfileData {
552 protected:
553 enum {
554 taken_off_set,
555 displacement_off_set,
556 jump_cell_count
557 };
559 void set_displacement(int displacement) {
560 set_int_at(displacement_off_set, displacement);
561 }
563 public:
564 JumpData(DataLayout* layout) : ProfileData(layout) {
565 assert(layout->tag() == DataLayout::jump_data_tag ||
566 layout->tag() == DataLayout::branch_data_tag, "wrong type");
567 }
569 virtual bool is_JumpData() { return true; }
571 static int static_cell_count() {
572 return jump_cell_count;
573 }
575 virtual int cell_count() {
576 return static_cell_count();
577 }
579 // Direct accessor
580 uint taken() {
581 return uint_at(taken_off_set);
582 }
583 // Saturating counter
584 uint inc_taken() {
585 uint cnt = taken() + 1;
586 // Did we wrap? Will compiler screw us??
587 if (cnt == 0) cnt--;
588 set_uint_at(taken_off_set, cnt);
589 return cnt;
590 }
592 int displacement() {
593 return int_at(displacement_off_set);
594 }
596 // Code generation support
597 static ByteSize taken_offset() {
598 return cell_offset(taken_off_set);
599 }
601 static ByteSize displacement_offset() {
602 return cell_offset(displacement_off_set);
603 }
605 // Specific initialization.
606 void post_initialize(BytecodeStream* stream, methodDataOop mdo);
608 #ifndef PRODUCT
609 void print_data_on(outputStream* st);
610 #endif
611 };
613 // ReceiverTypeData
614 //
615 // A ReceiverTypeData is used to access profiling information about a
616 // dynamic type check. It consists of a counter which counts the total times
617 // that the check is reached, and a series of (klassOop, count) pairs
618 // which are used to store a type profile for the receiver of the check.
619 class ReceiverTypeData : public CounterData {
620 protected:
621 enum {
622 receiver0_offset = counter_cell_count,
623 count0_offset,
624 receiver_type_row_cell_count = (count0_offset + 1) - receiver0_offset
625 };
627 public:
628 ReceiverTypeData(DataLayout* layout) : CounterData(layout) {
629 assert(layout->tag() == DataLayout::receiver_type_data_tag ||
630 layout->tag() == DataLayout::virtual_call_data_tag, "wrong type");
631 }
633 virtual bool is_ReceiverTypeData() { return true; }
635 static int static_cell_count() {
636 return counter_cell_count + (uint) TypeProfileWidth * receiver_type_row_cell_count;
637 }
639 virtual int cell_count() {
640 return static_cell_count();
641 }
643 // Direct accessors
644 static uint row_limit() {
645 return TypeProfileWidth;
646 }
647 static int receiver_cell_index(uint row) {
648 return receiver0_offset + row * receiver_type_row_cell_count;
649 }
650 static int receiver_count_cell_index(uint row) {
651 return count0_offset + row * receiver_type_row_cell_count;
652 }
654 // Get the receiver at row. The 'unchecked' version is needed by parallel old
655 // gc; it does not assert the receiver is a klass. During compaction of the
656 // perm gen, the klass may already have moved, so the is_klass() predicate
657 // would fail. The 'normal' version should be used whenever possible.
658 klassOop receiver_unchecked(uint row) {
659 assert(row < row_limit(), "oob");
660 oop recv = oop_at(receiver_cell_index(row));
661 return (klassOop)recv;
662 }
664 klassOop receiver(uint row) {
665 klassOop recv = receiver_unchecked(row);
666 assert(recv == NULL || ((oop)recv)->is_klass(), "wrong type");
667 return recv;
668 }
670 uint receiver_count(uint row) {
671 assert(row < row_limit(), "oob");
672 return uint_at(receiver_count_cell_index(row));
673 }
675 // Code generation support
676 static ByteSize receiver_offset(uint row) {
677 return cell_offset(receiver_cell_index(row));
678 }
679 static ByteSize receiver_count_offset(uint row) {
680 return cell_offset(receiver_count_cell_index(row));
681 }
682 static ByteSize receiver_type_data_size() {
683 return cell_offset(static_cell_count());
684 }
686 // GC support
687 virtual void follow_contents();
688 virtual void oop_iterate(OopClosure* blk);
689 virtual void oop_iterate_m(OopClosure* blk, MemRegion mr);
690 virtual void adjust_pointers();
692 #ifndef SERIALGC
693 // Parallel old support
694 virtual void follow_contents(ParCompactionManager* cm);
695 virtual void update_pointers();
696 virtual void update_pointers(HeapWord* beg_addr, HeapWord* end_addr);
697 #endif // SERIALGC
699 oop* adr_receiver(uint row) {
700 return adr_oop_at(receiver_cell_index(row));
701 }
703 #ifndef PRODUCT
704 void print_receiver_data_on(outputStream* st);
705 void print_data_on(outputStream* st);
706 #endif
707 };
709 // VirtualCallData
710 //
711 // A VirtualCallData is used to access profiling information about a
712 // virtual call. For now, it has nothing more than a ReceiverTypeData.
713 class VirtualCallData : public ReceiverTypeData {
714 public:
715 VirtualCallData(DataLayout* layout) : ReceiverTypeData(layout) {
716 assert(layout->tag() == DataLayout::virtual_call_data_tag, "wrong type");
717 }
719 virtual bool is_VirtualCallData() { return true; }
721 static int static_cell_count() {
722 // At this point we could add more profile state, e.g., for arguments.
723 // But for now it's the same size as the base record type.
724 return ReceiverTypeData::static_cell_count();
725 }
727 virtual int cell_count() {
728 return static_cell_count();
729 }
731 // Direct accessors
732 static ByteSize virtual_call_data_size() {
733 return cell_offset(static_cell_count());
734 }
736 #ifndef PRODUCT
737 void print_data_on(outputStream* st);
738 #endif
739 };
741 // RetData
742 //
743 // A RetData is used to access profiling information for a ret bytecode.
744 // It is composed of a count of the number of times that the ret has
745 // been executed, followed by a series of triples of the form
746 // (bci, count, di) which count the number of times that some bci was the
747 // target of the ret and cache a corresponding data displacement.
748 class RetData : public CounterData {
749 protected:
750 enum {
751 bci0_offset = counter_cell_count,
752 count0_offset,
753 displacement0_offset,
754 ret_row_cell_count = (displacement0_offset + 1) - bci0_offset
755 };
757 void set_bci(uint row, int bci) {
758 assert((uint)row < row_limit(), "oob");
759 set_int_at(bci0_offset + row * ret_row_cell_count, bci);
760 }
761 void release_set_bci(uint row, int bci) {
762 assert((uint)row < row_limit(), "oob");
763 // 'release' when setting the bci acts as a valid flag for other
764 // threads wrt bci_count and bci_displacement.
765 release_set_int_at(bci0_offset + row * ret_row_cell_count, bci);
766 }
767 void set_bci_count(uint row, uint count) {
768 assert((uint)row < row_limit(), "oob");
769 set_uint_at(count0_offset + row * ret_row_cell_count, count);
770 }
771 void set_bci_displacement(uint row, int disp) {
772 set_int_at(displacement0_offset + row * ret_row_cell_count, disp);
773 }
775 public:
776 RetData(DataLayout* layout) : CounterData(layout) {
777 assert(layout->tag() == DataLayout::ret_data_tag, "wrong type");
778 }
780 virtual bool is_RetData() { return true; }
782 enum {
783 no_bci = -1 // value of bci when bci1/2 are not in use.
784 };
786 static int static_cell_count() {
787 return counter_cell_count + (uint) BciProfileWidth * ret_row_cell_count;
788 }
790 virtual int cell_count() {
791 return static_cell_count();
792 }
794 static uint row_limit() {
795 return BciProfileWidth;
796 }
797 static int bci_cell_index(uint row) {
798 return bci0_offset + row * ret_row_cell_count;
799 }
800 static int bci_count_cell_index(uint row) {
801 return count0_offset + row * ret_row_cell_count;
802 }
803 static int bci_displacement_cell_index(uint row) {
804 return displacement0_offset + row * ret_row_cell_count;
805 }
807 // Direct accessors
808 int bci(uint row) {
809 return int_at(bci_cell_index(row));
810 }
811 uint bci_count(uint row) {
812 return uint_at(bci_count_cell_index(row));
813 }
814 int bci_displacement(uint row) {
815 return int_at(bci_displacement_cell_index(row));
816 }
818 // Interpreter Runtime support
819 address fixup_ret(int return_bci, methodDataHandle mdo);
821 // Code generation support
822 static ByteSize bci_offset(uint row) {
823 return cell_offset(bci_cell_index(row));
824 }
825 static ByteSize bci_count_offset(uint row) {
826 return cell_offset(bci_count_cell_index(row));
827 }
828 static ByteSize bci_displacement_offset(uint row) {
829 return cell_offset(bci_displacement_cell_index(row));
830 }
832 // Specific initialization.
833 void post_initialize(BytecodeStream* stream, methodDataOop mdo);
835 #ifndef PRODUCT
836 void print_data_on(outputStream* st);
837 #endif
838 };
840 // BranchData
841 //
842 // A BranchData is used to access profiling data for a two-way branch.
843 // It consists of taken and not_taken counts as well as a data displacement
844 // for the taken case.
845 class BranchData : public JumpData {
846 protected:
847 enum {
848 not_taken_off_set = jump_cell_count,
849 branch_cell_count
850 };
852 void set_displacement(int displacement) {
853 set_int_at(displacement_off_set, displacement);
854 }
856 public:
857 BranchData(DataLayout* layout) : JumpData(layout) {
858 assert(layout->tag() == DataLayout::branch_data_tag, "wrong type");
859 }
861 virtual bool is_BranchData() { return true; }
863 static int static_cell_count() {
864 return branch_cell_count;
865 }
867 virtual int cell_count() {
868 return static_cell_count();
869 }
871 // Direct accessor
872 uint not_taken() {
873 return uint_at(not_taken_off_set);
874 }
876 uint inc_not_taken() {
877 uint cnt = not_taken() + 1;
878 // Did we wrap? Will compiler screw us??
879 if (cnt == 0) cnt--;
880 set_uint_at(not_taken_off_set, cnt);
881 return cnt;
882 }
884 // Code generation support
885 static ByteSize not_taken_offset() {
886 return cell_offset(not_taken_off_set);
887 }
888 static ByteSize branch_data_size() {
889 return cell_offset(branch_cell_count);
890 }
892 // Specific initialization.
893 void post_initialize(BytecodeStream* stream, methodDataOop mdo);
895 #ifndef PRODUCT
896 void print_data_on(outputStream* st);
897 #endif
898 };
900 // ArrayData
901 //
902 // A ArrayData is a base class for accessing profiling data which does
903 // not have a statically known size. It consists of an array length
904 // and an array start.
905 class ArrayData : public ProfileData {
906 protected:
907 friend class DataLayout;
909 enum {
910 array_len_off_set,
911 array_start_off_set
912 };
914 uint array_uint_at(int index) {
915 int aindex = index + array_start_off_set;
916 return uint_at(aindex);
917 }
918 int array_int_at(int index) {
919 int aindex = index + array_start_off_set;
920 return int_at(aindex);
921 }
922 oop array_oop_at(int index) {
923 int aindex = index + array_start_off_set;
924 return oop_at(aindex);
925 }
926 void array_set_int_at(int index, int value) {
927 int aindex = index + array_start_off_set;
928 set_int_at(aindex, value);
929 }
931 // Code generation support for subclasses.
932 static ByteSize array_element_offset(int index) {
933 return cell_offset(array_start_off_set + index);
934 }
936 public:
937 ArrayData(DataLayout* layout) : ProfileData(layout) {}
939 virtual bool is_ArrayData() { return true; }
941 static int static_cell_count() {
942 return -1;
943 }
945 int array_len() {
946 return int_at_unchecked(array_len_off_set);
947 }
949 virtual int cell_count() {
950 return array_len() + 1;
951 }
953 // Code generation support
954 static ByteSize array_len_offset() {
955 return cell_offset(array_len_off_set);
956 }
957 static ByteSize array_start_offset() {
958 return cell_offset(array_start_off_set);
959 }
960 };
962 // MultiBranchData
963 //
964 // A MultiBranchData is used to access profiling information for
965 // a multi-way branch (*switch bytecodes). It consists of a series
966 // of (count, displacement) pairs, which count the number of times each
967 // case was taken and specify the data displacment for each branch target.
968 class MultiBranchData : public ArrayData {
969 protected:
970 enum {
971 default_count_off_set,
972 default_disaplacement_off_set,
973 case_array_start
974 };
975 enum {
976 relative_count_off_set,
977 relative_displacement_off_set,
978 per_case_cell_count
979 };
981 void set_default_displacement(int displacement) {
982 array_set_int_at(default_disaplacement_off_set, displacement);
983 }
984 void set_displacement_at(int index, int displacement) {
985 array_set_int_at(case_array_start +
986 index * per_case_cell_count +
987 relative_displacement_off_set,
988 displacement);
989 }
991 public:
992 MultiBranchData(DataLayout* layout) : ArrayData(layout) {
993 assert(layout->tag() == DataLayout::multi_branch_data_tag, "wrong type");
994 }
996 virtual bool is_MultiBranchData() { return true; }
998 static int compute_cell_count(BytecodeStream* stream);
1000 int number_of_cases() {
1001 int alen = array_len() - 2; // get rid of default case here.
1002 assert(alen % per_case_cell_count == 0, "must be even");
1003 return (alen / per_case_cell_count);
1004 }
1006 uint default_count() {
1007 return array_uint_at(default_count_off_set);
1008 }
1009 int default_displacement() {
1010 return array_int_at(default_disaplacement_off_set);
1011 }
1013 uint count_at(int index) {
1014 return array_uint_at(case_array_start +
1015 index * per_case_cell_count +
1016 relative_count_off_set);
1017 }
1018 int displacement_at(int index) {
1019 return array_int_at(case_array_start +
1020 index * per_case_cell_count +
1021 relative_displacement_off_set);
1022 }
1024 // Code generation support
1025 static ByteSize default_count_offset() {
1026 return array_element_offset(default_count_off_set);
1027 }
1028 static ByteSize default_displacement_offset() {
1029 return array_element_offset(default_disaplacement_off_set);
1030 }
1031 static ByteSize case_count_offset(int index) {
1032 return case_array_offset() +
1033 (per_case_size() * index) +
1034 relative_count_offset();
1035 }
1036 static ByteSize case_array_offset() {
1037 return array_element_offset(case_array_start);
1038 }
1039 static ByteSize per_case_size() {
1040 return in_ByteSize(per_case_cell_count) * cell_size;
1041 }
1042 static ByteSize relative_count_offset() {
1043 return in_ByteSize(relative_count_off_set) * cell_size;
1044 }
1045 static ByteSize relative_displacement_offset() {
1046 return in_ByteSize(relative_displacement_off_set) * cell_size;
1047 }
1049 // Specific initialization.
1050 void post_initialize(BytecodeStream* stream, methodDataOop mdo);
1052 #ifndef PRODUCT
1053 void print_data_on(outputStream* st);
1054 #endif
1055 };
1057 class ArgInfoData : public ArrayData {
1059 public:
1060 ArgInfoData(DataLayout* layout) : ArrayData(layout) {
1061 assert(layout->tag() == DataLayout::arg_info_data_tag, "wrong type");
1062 }
1064 virtual bool is_ArgInfoData() { return true; }
1067 int number_of_args() {
1068 return array_len();
1069 }
1071 uint arg_modified(int arg) {
1072 return array_uint_at(arg);
1073 }
1075 void set_arg_modified(int arg, uint val) {
1076 array_set_int_at(arg, val);
1077 }
1079 #ifndef PRODUCT
1080 void print_data_on(outputStream* st);
1081 #endif
1082 };
1084 // methodDataOop
1085 //
1086 // A methodDataOop holds information which has been collected about
1087 // a method. Its layout looks like this:
1088 //
1089 // -----------------------------
1090 // | header |
1091 // | klass |
1092 // -----------------------------
1093 // | method |
1094 // | size of the methodDataOop |
1095 // -----------------------------
1096 // | Data entries... |
1097 // | (variable size) |
1098 // | |
1099 // . .
1100 // . .
1101 // . .
1102 // | |
1103 // -----------------------------
1104 //
1105 // The data entry area is a heterogeneous array of DataLayouts. Each
1106 // DataLayout in the array corresponds to a specific bytecode in the
1107 // method. The entries in the array are sorted by the corresponding
1108 // bytecode. Access to the data is via resource-allocated ProfileData,
1109 // which point to the underlying blocks of DataLayout structures.
1110 //
1111 // During interpretation, if profiling in enabled, the interpreter
1112 // maintains a method data pointer (mdp), which points at the entry
1113 // in the array corresponding to the current bci. In the course of
1114 // intepretation, when a bytecode is encountered that has profile data
1115 // associated with it, the entry pointed to by mdp is updated, then the
1116 // mdp is adjusted to point to the next appropriate DataLayout. If mdp
1117 // is NULL to begin with, the interpreter assumes that the current method
1118 // is not (yet) being profiled.
1119 //
1120 // In methodDataOop parlance, "dp" is a "data pointer", the actual address
1121 // of a DataLayout element. A "di" is a "data index", the offset in bytes
1122 // from the base of the data entry array. A "displacement" is the byte offset
1123 // in certain ProfileData objects that indicate the amount the mdp must be
1124 // adjusted in the event of a change in control flow.
1125 //
1127 class methodDataOopDesc : public oopDesc {
1128 friend class VMStructs;
1129 private:
1130 friend class ProfileData;
1132 // Back pointer to the methodOop
1133 methodOop _method;
1135 // Size of this oop in bytes
1136 int _size;
1138 // Cached hint for bci_to_dp and bci_to_data
1139 int _hint_di;
1141 // Whole-method sticky bits and flags
1142 public:
1143 enum {
1144 _trap_hist_limit = 16, // decoupled from Deoptimization::Reason_LIMIT
1145 _trap_hist_mask = max_jubyte,
1146 _extra_data_count = 4 // extra DataLayout headers, for trap history
1147 }; // Public flag values
1148 private:
1149 uint _nof_decompiles; // count of all nmethod removals
1150 uint _nof_overflow_recompiles; // recompile count, excluding recomp. bits
1151 uint _nof_overflow_traps; // trap count, excluding _trap_hist
1152 union {
1153 intptr_t _align;
1154 u1 _array[_trap_hist_limit];
1155 } _trap_hist;
1157 // Support for interprocedural escape analysis, from Thomas Kotzmann.
1158 intx _eflags; // flags on escape information
1159 intx _arg_local; // bit set of non-escaping arguments
1160 intx _arg_stack; // bit set of stack-allocatable arguments
1161 intx _arg_returned; // bit set of returned arguments
1163 int _creation_mileage; // method mileage at MDO creation
1165 // Size of _data array in bytes. (Excludes header and extra_data fields.)
1166 int _data_size;
1168 // Beginning of the data entries
1169 intptr_t _data[1];
1171 // Helper for size computation
1172 static int compute_data_size(BytecodeStream* stream);
1173 static int bytecode_cell_count(Bytecodes::Code code);
1174 enum { no_profile_data = -1, variable_cell_count = -2 };
1176 // Helper for initialization
1177 DataLayout* data_layout_at(int data_index) {
1178 assert(data_index % sizeof(intptr_t) == 0, "unaligned");
1179 return (DataLayout*) (((address)_data) + data_index);
1180 }
1182 // Initialize an individual data segment. Returns the size of
1183 // the segment in bytes.
1184 int initialize_data(BytecodeStream* stream, int data_index);
1186 // Helper for data_at
1187 DataLayout* limit_data_position() {
1188 return (DataLayout*)((address)data_base() + _data_size);
1189 }
1190 bool out_of_bounds(int data_index) {
1191 return data_index >= data_size();
1192 }
1194 // Give each of the data entries a chance to perform specific
1195 // data initialization.
1196 void post_initialize(BytecodeStream* stream);
1198 // hint accessors
1199 int hint_di() const { return _hint_di; }
1200 void set_hint_di(int di) {
1201 assert(!out_of_bounds(di), "hint_di out of bounds");
1202 _hint_di = di;
1203 }
1204 ProfileData* data_before(int bci) {
1205 // avoid SEGV on this edge case
1206 if (data_size() == 0)
1207 return NULL;
1208 int hint = hint_di();
1209 if (data_layout_at(hint)->bci() <= bci)
1210 return data_at(hint);
1211 return first_data();
1212 }
1214 // What is the index of the first data entry?
1215 int first_di() { return 0; }
1217 // Find or create an extra ProfileData:
1218 ProfileData* bci_to_extra_data(int bci, bool create_if_missing);
1220 // return the argument info cell
1221 ArgInfoData *arg_info();
1223 public:
1224 static int header_size() {
1225 return sizeof(methodDataOopDesc)/wordSize;
1226 }
1228 // Compute the size of a methodDataOop before it is created.
1229 static int compute_allocation_size_in_bytes(methodHandle method);
1230 static int compute_allocation_size_in_words(methodHandle method);
1231 static int compute_extra_data_count(int data_size, int empty_bc_count);
1233 // Determine if a given bytecode can have profile information.
1234 static bool bytecode_has_profile(Bytecodes::Code code) {
1235 return bytecode_cell_count(code) != no_profile_data;
1236 }
1238 // Perform initialization of a new methodDataOop
1239 void initialize(methodHandle method);
1241 // My size
1242 int object_size_in_bytes() { return _size; }
1243 int object_size() {
1244 return align_object_size(align_size_up(_size, BytesPerWord)/BytesPerWord);
1245 }
1247 int creation_mileage() const { return _creation_mileage; }
1248 void set_creation_mileage(int x) { _creation_mileage = x; }
1249 bool is_mature() const; // consult mileage and ProfileMaturityPercentage
1250 static int mileage_of(methodOop m);
1252 // Support for interprocedural escape analysis, from Thomas Kotzmann.
1253 enum EscapeFlag {
1254 estimated = 1 << 0,
1255 return_local = 1 << 1,
1256 return_allocated = 1 << 2,
1257 allocated_escapes = 1 << 3,
1258 unknown_modified = 1 << 4
1259 };
1261 intx eflags() { return _eflags; }
1262 intx arg_local() { return _arg_local; }
1263 intx arg_stack() { return _arg_stack; }
1264 intx arg_returned() { return _arg_returned; }
1265 uint arg_modified(int a) { ArgInfoData *aid = arg_info();
1266 assert(a >= 0 && a < aid->number_of_args(), "valid argument number");
1267 return aid->arg_modified(a); }
1269 void set_eflags(intx v) { _eflags = v; }
1270 void set_arg_local(intx v) { _arg_local = v; }
1271 void set_arg_stack(intx v) { _arg_stack = v; }
1272 void set_arg_returned(intx v) { _arg_returned = v; }
1273 void set_arg_modified(int a, uint v) { ArgInfoData *aid = arg_info();
1274 assert(a >= 0 && a < aid->number_of_args(), "valid argument number");
1276 aid->set_arg_modified(a, v); }
1278 void clear_escape_info() { _eflags = _arg_local = _arg_stack = _arg_returned = 0; }
1280 // Location and size of data area
1281 address data_base() const {
1282 return (address) _data;
1283 }
1284 int data_size() {
1285 return _data_size;
1286 }
1288 // Accessors
1289 methodOop method() { return _method; }
1291 // Get the data at an arbitrary (sort of) data index.
1292 ProfileData* data_at(int data_index);
1294 // Walk through the data in order.
1295 ProfileData* first_data() { return data_at(first_di()); }
1296 ProfileData* next_data(ProfileData* current);
1297 bool is_valid(ProfileData* current) { return current != NULL; }
1299 // Convert a dp (data pointer) to a di (data index).
1300 int dp_to_di(address dp) {
1301 return dp - ((address)_data);
1302 }
1304 address di_to_dp(int di) {
1305 return (address)data_layout_at(di);
1306 }
1308 // bci to di/dp conversion.
1309 address bci_to_dp(int bci);
1310 int bci_to_di(int bci) {
1311 return dp_to_di(bci_to_dp(bci));
1312 }
1314 // Get the data at an arbitrary bci, or NULL if there is none.
1315 ProfileData* bci_to_data(int bci);
1317 // Same, but try to create an extra_data record if one is needed:
1318 ProfileData* allocate_bci_to_data(int bci) {
1319 ProfileData* data = bci_to_data(bci);
1320 return (data != NULL) ? data : bci_to_extra_data(bci, true);
1321 }
1323 // Add a handful of extra data records, for trap tracking.
1324 DataLayout* extra_data_base() { return limit_data_position(); }
1325 DataLayout* extra_data_limit() { return (DataLayout*)((address)this + object_size_in_bytes()); }
1326 int extra_data_size() { return (address)extra_data_limit()
1327 - (address)extra_data_base(); }
1328 static DataLayout* next_extra(DataLayout* dp) { return (DataLayout*)((address)dp + in_bytes(DataLayout::cell_offset(0))); }
1330 // Return (uint)-1 for overflow.
1331 uint trap_count(int reason) const {
1332 assert((uint)reason < _trap_hist_limit, "oob");
1333 return (int)((_trap_hist._array[reason]+1) & _trap_hist_mask) - 1;
1334 }
1335 // For loops:
1336 static uint trap_reason_limit() { return _trap_hist_limit; }
1337 static uint trap_count_limit() { return _trap_hist_mask; }
1338 uint inc_trap_count(int reason) {
1339 // Count another trap, anywhere in this method.
1340 assert(reason >= 0, "must be single trap");
1341 if ((uint)reason < _trap_hist_limit) {
1342 uint cnt1 = 1 + _trap_hist._array[reason];
1343 if ((cnt1 & _trap_hist_mask) != 0) { // if no counter overflow...
1344 _trap_hist._array[reason] = cnt1;
1345 return cnt1;
1346 } else {
1347 return _trap_hist_mask + (++_nof_overflow_traps);
1348 }
1349 } else {
1350 // Could not represent the count in the histogram.
1351 return (++_nof_overflow_traps);
1352 }
1353 }
1355 uint overflow_trap_count() const {
1356 return _nof_overflow_traps;
1357 }
1358 uint overflow_recompile_count() const {
1359 return _nof_overflow_recompiles;
1360 }
1361 void inc_overflow_recompile_count() {
1362 _nof_overflow_recompiles += 1;
1363 }
1364 uint decompile_count() const {
1365 return _nof_decompiles;
1366 }
1367 void inc_decompile_count() {
1368 _nof_decompiles += 1;
1369 }
1371 // Support for code generation
1372 static ByteSize data_offset() {
1373 return byte_offset_of(methodDataOopDesc, _data[0]);
1374 }
1376 // GC support
1377 oop* adr_method() const { return (oop*)&_method; }
1378 bool object_is_parsable() const { return _size != 0; }
1379 void set_object_is_parsable(int object_size_in_bytes) { _size = object_size_in_bytes; }
1381 #ifndef PRODUCT
1382 // printing support for method data
1383 void print_data_on(outputStream* st);
1384 #endif
1386 // verification
1387 void verify_data_on(outputStream* st);
1388 };