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