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