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