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