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