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