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