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