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