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