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