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