1 /*
   2  * Copyright (c) 2000, 2013, 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   };
 124 
 125   enum {
 126     // The _struct._flags word is formatted as [trap_state:4 | flags:4].
 127     // The trap state breaks down further as [recompile:1 | reason:3].
 128     // This further breakdown is defined in deoptimization.cpp.
 129     // See Deoptimization::trap_state_reason for an assert that
 130     // trap_bits is big enough to hold reasons < Reason_RECORDED_LIMIT.
 131     //
 132     // The trap_state is collected only if ProfileTraps is true.
 133     trap_bits = 1+3,  // 3: enough to distinguish [0..Reason_RECORDED_LIMIT].
 134     trap_shift = BitsPerByte - trap_bits,
 135     trap_mask = right_n_bits(trap_bits),
 136     trap_mask_in_place = (trap_mask << trap_shift),
 137     flag_limit = trap_shift,
 138     flag_mask = right_n_bits(flag_limit),
 139     first_flag = 0
 140   };
 141 
 142   // Size computation
 143   static int header_size_in_bytes() {
 144     return cell_size;
 145   }
 146   static int header_size_in_cells() {
 147     return 1;
 148   }
 149 
 150   static int compute_size_in_bytes(int cell_count) {
 151     return header_size_in_bytes() + cell_count * cell_size;
 152   }
 153 
 154   // Initialization
 155   void initialize(u1 tag, u2 bci, int cell_count);
 156 
 157   // Accessors
 158   u1 tag() {
 159     return _header._struct._tag;
 160   }
 161 
 162   // Return a few bits of trap state.  Range is [0..trap_mask].
 163   // The state tells if traps with zero, one, or many reasons have occurred.
 164   // It also tells whether zero or many recompilations have occurred.
 165   // The associated trap histogram in the MDO itself tells whether
 166   // traps are common or not.  If a BCI shows that a trap X has
 167   // occurred, and the MDO shows N occurrences of X, we make the
 168   // simplifying assumption that all N occurrences can be blamed
 169   // on that BCI.
 170   int trap_state() const {
 171     return ((_header._struct._flags >> trap_shift) & trap_mask);
 172   }
 173 
 174   void set_trap_state(int new_state) {
 175     assert(ProfileTraps, "used only under +ProfileTraps");
 176     uint old_flags = (_header._struct._flags & flag_mask);
 177     _header._struct._flags = (new_state << trap_shift) | old_flags;
 178   }
 179 
 180   u1 flags() const {
 181     return _header._struct._flags;
 182   }
 183 
 184   u2 bci() const {
 185     return _header._struct._bci;
 186   }
 187 
 188   void set_header(intptr_t value) {
 189     _header._bits = value;
 190   }
 191   void release_set_header(intptr_t value) {
 192     OrderAccess::release_store_ptr(&_header._bits, value);
 193   }
 194   intptr_t header() {
 195     return _header._bits;
 196   }
 197   void set_cell_at(int index, intptr_t value) {
 198     _cells[index] = value;
 199   }
 200   void release_set_cell_at(int index, intptr_t value) {
 201     OrderAccess::release_store_ptr(&_cells[index], value);
 202   }
 203   intptr_t cell_at(int index) const {
 204     return _cells[index];
 205   }
 206 
 207   void set_flag_at(int flag_number) {
 208     assert(flag_number < flag_limit, "oob");
 209     _header._struct._flags |= (0x1 << flag_number);
 210   }
 211   bool flag_at(int flag_number) const {
 212     assert(flag_number < flag_limit, "oob");
 213     return (_header._struct._flags & (0x1 << flag_number)) != 0;
 214   }
 215 
 216   // Low-level support for code generation.
 217   static ByteSize header_offset() {
 218     return byte_offset_of(DataLayout, _header);
 219   }
 220   static ByteSize tag_offset() {
 221     return byte_offset_of(DataLayout, _header._struct._tag);
 222   }
 223   static ByteSize flags_offset() {
 224     return byte_offset_of(DataLayout, _header._struct._flags);
 225   }
 226   static ByteSize bci_offset() {
 227     return byte_offset_of(DataLayout, _header._struct._bci);
 228   }
 229   static ByteSize cell_offset(int index) {
 230     return byte_offset_of(DataLayout, _cells) + in_ByteSize(index * cell_size);
 231   }
 232   // Return a value which, when or-ed as a byte into _flags, sets the flag.
 233   static int flag_number_to_byte_constant(int flag_number) {
 234     assert(0 <= flag_number && flag_number < flag_limit, "oob");
 235     DataLayout temp; temp.set_header(0);
 236     temp.set_flag_at(flag_number);
 237     return temp._header._struct._flags;
 238   }
 239   // Return a value which, when or-ed as a word into _header, sets the flag.
 240   static intptr_t flag_mask_to_header_mask(int byte_constant) {
 241     DataLayout temp; temp.set_header(0);
 242     temp._header._struct._flags = byte_constant;
 243     return temp._header._bits;
 244   }
 245 
 246   ProfileData* data_in();
 247 
 248   // GC support
 249   void clean_weak_klass_links(BoolObjectClosure* cl);
 250 };
 251 
 252 
 253 // ProfileData class hierarchy
 254 class ProfileData;
 255 class   BitData;
 256 class     CounterData;
 257 class       ReceiverTypeData;
 258 class         VirtualCallData;
 259 class           VirtualCallTypeData;
 260 class       RetData;
 261 class       CallTypeData;
 262 class   JumpData;
 263 class     BranchData;
 264 class   ArrayData;
 265 class     MultiBranchData;
 266 class     ArgInfoData;
 267 
 268 // ProfileData
 269 //
 270 // A ProfileData object is created to refer to a section of profiling
 271 // data in a structured way.
 272 class ProfileData : public ResourceObj {
 273   friend class TypeEntries;
 274   friend class TypeStackSlotEntries;
 275 private:
 276 #ifndef PRODUCT
 277   enum {
 278     tab_width_one = 16,
 279     tab_width_two = 36
 280   };
 281 #endif // !PRODUCT
 282 
 283   // This is a pointer to a section of profiling data.
 284   DataLayout* _data;
 285 
 286 protected:
 287   DataLayout* data() { return _data; }
 288   const DataLayout* data() const { return _data; }
 289 
 290   enum {
 291     cell_size = DataLayout::cell_size
 292   };
 293 
 294 public:
 295   // How many cells are in this?
 296   virtual int cell_count() const {
 297     ShouldNotReachHere();
 298     return -1;
 299   }
 300 
 301   // Return the size of this data.
 302   int size_in_bytes() {
 303     return DataLayout::compute_size_in_bytes(cell_count());
 304   }
 305 
 306 protected:
 307   // Low-level accessors for underlying data
 308   void set_intptr_at(int index, intptr_t value) {
 309     assert(0 <= index && index < cell_count(), "oob");
 310     data()->set_cell_at(index, value);
 311   }
 312   void release_set_intptr_at(int index, intptr_t value) {
 313     assert(0 <= index && index < cell_count(), "oob");
 314     data()->release_set_cell_at(index, value);
 315   }
 316   intptr_t intptr_at(int index) const {
 317     assert(0 <= index && index < cell_count(), "oob");
 318     return data()->cell_at(index);
 319   }
 320   void set_uint_at(int index, uint value) {
 321     set_intptr_at(index, (intptr_t) value);
 322   }
 323   void release_set_uint_at(int index, uint value) {
 324     release_set_intptr_at(index, (intptr_t) value);
 325   }
 326   uint uint_at(int index) const {
 327     return (uint)intptr_at(index);
 328   }
 329   void set_int_at(int index, int value) {
 330     set_intptr_at(index, (intptr_t) value);
 331   }
 332   void release_set_int_at(int index, int value) {
 333     release_set_intptr_at(index, (intptr_t) value);
 334   }
 335   int int_at(int index) const {
 336     return (int)intptr_at(index);
 337   }
 338   int int_at_unchecked(int index) const {
 339     return (int)data()->cell_at(index);
 340   }
 341   void set_oop_at(int index, oop value) {
 342     set_intptr_at(index, (intptr_t) value);
 343   }
 344   oop oop_at(int index) const {
 345     return (oop)intptr_at(index);
 346   }
 347 
 348   void set_flag_at(int flag_number) {
 349     data()->set_flag_at(flag_number);
 350   }
 351   bool flag_at(int flag_number) const {
 352     return data()->flag_at(flag_number);
 353   }
 354 
 355   // two convenient imports for use by subclasses:
 356   static ByteSize cell_offset(int index) {
 357     return DataLayout::cell_offset(index);
 358   }
 359   static int flag_number_to_byte_constant(int flag_number) {
 360     return DataLayout::flag_number_to_byte_constant(flag_number);
 361   }
 362 
 363   ProfileData(DataLayout* data) {
 364     _data = data;
 365   }
 366 
 367 public:
 368   // Constructor for invalid ProfileData.
 369   ProfileData();
 370 
 371   u2 bci() const {
 372     return data()->bci();
 373   }
 374 
 375   address dp() {
 376     return (address)_data;
 377   }
 378 
 379   int trap_state() const {
 380     return data()->trap_state();
 381   }
 382   void set_trap_state(int new_state) {
 383     data()->set_trap_state(new_state);
 384   }
 385 
 386   // Type checking
 387   virtual bool is_BitData()         const { return false; }
 388   virtual bool is_CounterData()     const { return false; }
 389   virtual bool is_JumpData()        const { return false; }
 390   virtual bool is_ReceiverTypeData()const { return false; }
 391   virtual bool is_VirtualCallData() const { return false; }
 392   virtual bool is_RetData()         const { return false; }
 393   virtual bool is_BranchData()      const { return false; }
 394   virtual bool is_ArrayData()       const { return false; }
 395   virtual bool is_MultiBranchData() const { return false; }
 396   virtual bool is_ArgInfoData()     const { return false; }
 397   virtual bool is_CallTypeData()    const { return false; }
 398   virtual bool is_VirtualCallTypeData()const { return false; }
 399 
 400 
 401   BitData* as_BitData() const {
 402     assert(is_BitData(), "wrong type");
 403     return is_BitData()         ? (BitData*)        this : NULL;
 404   }
 405   CounterData* as_CounterData() const {
 406     assert(is_CounterData(), "wrong type");
 407     return is_CounterData()     ? (CounterData*)    this : NULL;
 408   }
 409   JumpData* as_JumpData() const {
 410     assert(is_JumpData(), "wrong type");
 411     return is_JumpData()        ? (JumpData*)       this : NULL;
 412   }
 413   ReceiverTypeData* as_ReceiverTypeData() const {
 414     assert(is_ReceiverTypeData(), "wrong type");
 415     return is_ReceiverTypeData() ? (ReceiverTypeData*)this : NULL;
 416   }
 417   VirtualCallData* as_VirtualCallData() const {
 418     assert(is_VirtualCallData(), "wrong type");
 419     return is_VirtualCallData() ? (VirtualCallData*)this : NULL;
 420   }
 421   RetData* as_RetData() const {
 422     assert(is_RetData(), "wrong type");
 423     return is_RetData()         ? (RetData*)        this : NULL;
 424   }
 425   BranchData* as_BranchData() const {
 426     assert(is_BranchData(), "wrong type");
 427     return is_BranchData()      ? (BranchData*)     this : NULL;
 428   }
 429   ArrayData* as_ArrayData() const {
 430     assert(is_ArrayData(), "wrong type");
 431     return is_ArrayData()       ? (ArrayData*)      this : NULL;
 432   }
 433   MultiBranchData* as_MultiBranchData() const {
 434     assert(is_MultiBranchData(), "wrong type");
 435     return is_MultiBranchData() ? (MultiBranchData*)this : NULL;
 436   }
 437   ArgInfoData* as_ArgInfoData() const {
 438     assert(is_ArgInfoData(), "wrong type");
 439     return is_ArgInfoData() ? (ArgInfoData*)this : NULL;
 440   }
 441   CallTypeData* as_CallTypeData() const {
 442     assert(is_CallTypeData(), "wrong type");
 443     return is_CallTypeData() ? (CallTypeData*)this : NULL;
 444   }
 445   VirtualCallTypeData* as_VirtualCallTypeData() const {
 446     assert(is_VirtualCallTypeData(), "wrong type");
 447     return is_VirtualCallTypeData() ? (VirtualCallTypeData*)this : NULL;
 448   }
 449 
 450 
 451   // Subclass specific initialization
 452   virtual void post_initialize(BytecodeStream* stream, MethodData* mdo) {}
 453 
 454   // GC support
 455   virtual void clean_weak_klass_links(BoolObjectClosure* is_alive_closure) {}
 456 
 457   // CI translation: ProfileData can represent both MethodDataOop data
 458   // as well as CIMethodData data. This function is provided for translating
 459   // an oop in a ProfileData to the ci equivalent. Generally speaking,
 460   // most ProfileData don't require any translation, so we provide the null
 461   // translation here, and the required translators are in the ci subclasses.
 462   virtual void translate_from(const ProfileData* data) {}
 463 
 464   virtual void print_data_on(outputStream* st) const {
 465     ShouldNotReachHere();
 466   }
 467 
 468 #ifndef PRODUCT
 469   void print_shared(outputStream* st, const char* name) const;
 470   void tab(outputStream* st, bool first = false) const;
 471 #endif
 472 };
 473 
 474 // BitData
 475 //
 476 // A BitData holds a flag or two in its header.
 477 class BitData : public ProfileData {
 478 protected:
 479   enum {
 480     // null_seen:
 481     //  saw a null operand (cast/aastore/instanceof)
 482     null_seen_flag              = DataLayout::first_flag + 0
 483   };
 484   enum { bit_cell_count = 0 };  // no additional data fields needed.
 485 public:
 486   BitData(DataLayout* layout) : ProfileData(layout) {
 487   }
 488 
 489   virtual bool is_BitData() const { return true; }
 490 
 491   static int static_cell_count() {
 492     return bit_cell_count;
 493   }
 494 
 495   virtual int cell_count() const {
 496     return static_cell_count();
 497   }
 498 
 499   // Accessor
 500 
 501   // The null_seen flag bit is specially known to the interpreter.
 502   // Consulting it allows the compiler to avoid setting up null_check traps.
 503   bool null_seen()     { return flag_at(null_seen_flag); }
 504   void set_null_seen()    { set_flag_at(null_seen_flag); }
 505 
 506 
 507   // Code generation support
 508   static int null_seen_byte_constant() {
 509     return flag_number_to_byte_constant(null_seen_flag);
 510   }
 511 
 512   static ByteSize bit_data_size() {
 513     return cell_offset(bit_cell_count);
 514   }
 515 
 516 #ifndef PRODUCT
 517   void print_data_on(outputStream* st) const;
 518 #endif
 519 };
 520 
 521 // CounterData
 522 //
 523 // A CounterData corresponds to a simple counter.
 524 class CounterData : public BitData {
 525 protected:
 526   enum {
 527     count_off,
 528     counter_cell_count
 529   };
 530 public:
 531   CounterData(DataLayout* layout) : BitData(layout) {}
 532 
 533   virtual bool is_CounterData() const { return true; }
 534 
 535   static int static_cell_count() {
 536     return counter_cell_count;
 537   }
 538 
 539   virtual int cell_count() const {
 540     return static_cell_count();
 541   }
 542 
 543   // Direct accessor
 544   uint count() const {
 545     return uint_at(count_off);
 546   }
 547 
 548   // Code generation support
 549   static ByteSize count_offset() {
 550     return cell_offset(count_off);
 551   }
 552   static ByteSize counter_data_size() {
 553     return cell_offset(counter_cell_count);
 554   }
 555 
 556   void set_count(uint count) {
 557     set_uint_at(count_off, count);
 558   }
 559 
 560 #ifndef PRODUCT
 561   void print_data_on(outputStream* st) const;
 562 #endif
 563 };
 564 
 565 // JumpData
 566 //
 567 // A JumpData is used to access profiling information for a direct
 568 // branch.  It is a counter, used for counting the number of branches,
 569 // plus a data displacement, used for realigning the data pointer to
 570 // the corresponding target bci.
 571 class JumpData : public ProfileData {
 572 protected:
 573   enum {
 574     taken_off_set,
 575     displacement_off_set,
 576     jump_cell_count
 577   };
 578 
 579   void set_displacement(int displacement) {
 580     set_int_at(displacement_off_set, displacement);
 581   }
 582 
 583 public:
 584   JumpData(DataLayout* layout) : ProfileData(layout) {
 585     assert(layout->tag() == DataLayout::jump_data_tag ||
 586       layout->tag() == DataLayout::branch_data_tag, "wrong type");
 587   }
 588 
 589   virtual bool is_JumpData() const { return true; }
 590 
 591   static int static_cell_count() {
 592     return jump_cell_count;
 593   }
 594 
 595   virtual int cell_count() const {
 596     return static_cell_count();
 597   }
 598 
 599   // Direct accessor
 600   uint taken() const {
 601     return uint_at(taken_off_set);
 602   }
 603 
 604   void set_taken(uint cnt) {
 605     set_uint_at(taken_off_set, cnt);
 606   }
 607 
 608   // Saturating counter
 609   uint inc_taken() {
 610     uint cnt = taken() + 1;
 611     // Did we wrap? Will compiler screw us??
 612     if (cnt == 0) cnt--;
 613     set_uint_at(taken_off_set, cnt);
 614     return cnt;
 615   }
 616 
 617   int displacement() const {
 618     return int_at(displacement_off_set);
 619   }
 620 
 621   // Code generation support
 622   static ByteSize taken_offset() {
 623     return cell_offset(taken_off_set);
 624   }
 625 
 626   static ByteSize displacement_offset() {
 627     return cell_offset(displacement_off_set);
 628   }
 629 
 630   // Specific initialization.
 631   void post_initialize(BytecodeStream* stream, MethodData* mdo);
 632 
 633 #ifndef PRODUCT
 634   void print_data_on(outputStream* st) const;
 635 #endif
 636 };
 637 
 638 // Entries in a ProfileData object to record types: it can either be
 639 // none (no profile), unknown (conflicting profile data) or a klass if
 640 // a single one is seen. Whether a null reference was seen is also
 641 // recorded. No counter is associated with the type and a single type
 642 // is tracked (unlike VirtualCallData).
 643 class TypeEntries {
 644 
 645 public:
 646 
 647   // A single cell is used to record information for a type:
 648   // - the cell is initialized to 0
 649   // - when a type is discovered it is stored in the cell
 650   // - bit zero of the cell is used to record whether a null reference
 651   // was encountered or not
 652   // - bit 1 is set to record a conflict in the type information
 653 
 654   enum {
 655     null_seen = 1,
 656     type_mask = ~null_seen,
 657     type_unknown = 2,
 658     status_bits = null_seen | type_unknown,
 659     type_klass_mask = ~status_bits
 660   };
 661 
 662   // what to initialize a cell to
 663   static intptr_t type_none() {
 664     return NULL;
 665   }
 666 
 667   // null seen = bit 0 set?
 668   static bool was_null_seen(intptr_t v) {
 669     return v & null_seen;
 670   }
 671 
 672   // conflicting type information = bit 1 set?
 673   static bool is_type_unknown(intptr_t v) {
 674     return v & type_unknown;
 675   }
 676 
 677   // not type information yet = all bits cleared, ignoring bit 0?
 678   static bool is_type_none(intptr_t v) {
 679     return (v & type_mask) == 0;
 680   }
 681 
 682   // recorded type: cell without bit 0 and 1
 683   static intptr_t klass_part(intptr_t v) {
 684     intptr_t r = v & type_klass_mask;
 685     assert (r != NULL, "invalid");
 686     return r;
 687   }
 688 
 689   // type recorded
 690   static Klass* valid_klass(intptr_t k) {
 691     if (!is_type_none(k) &&
 692         !is_type_unknown(k)) {
 693       return (Klass*)klass_part(k);
 694     } else {
 695       return NULL;
 696     }
 697   }
 698 
 699   static intptr_t with_status(intptr_t k, intptr_t in) {
 700     return k | (in & status_bits);
 701   }
 702 
 703   static intptr_t with_status(Klass* k, intptr_t in) {
 704     return with_status((intptr_t)k, in);
 705   }
 706 
 707 #ifndef PRODUCT
 708   static void print_klass(outputStream* st, intptr_t k);
 709 #endif
 710 
 711   // GC support
 712   static bool is_loader_alive(BoolObjectClosure* is_alive_cl, intptr_t p);
 713 
 714 protected:
 715   // ProfileData object these entries are part of
 716   ProfileData* _pd;
 717   // offset within the ProfileData object where the entries start
 718   const int _base_off;
 719 
 720   TypeEntries(int base_off, ProfileData* pd)
 721     : _base_off(base_off), _pd(pd) {}
 722 
 723   void set_intptr_at(int index, intptr_t value) {
 724     _pd->set_intptr_at(index, value);
 725   }
 726 
 727   intptr_t intptr_at(int index) const {
 728     return _pd->intptr_at(index);
 729   }
 730 };
 731 
 732 // Type entries used for arguments passed at a call and parameters on
 733 // method entry. 2 cells per entry: one for the type encoded as in
 734 // TypeEntries and one initialized with the stack slot where the
 735 // profiled object is to be found so that the interpreter can locate
 736 // it quickly.
 737 class TypeStackSlotEntries : public TypeEntries {
 738 
 739 private:
 740   enum {
 741     stack_slot_entry,
 742     type_entry,
 743     per_arg_cell_count
 744   };
 745 
 746   // Start with a header if needed. It stores the number of cells used
 747   // for this call type information. Unless we collect only profiling
 748   // for a single argument the number of cells is unknown statically.
 749   static int header_cell_count() {
 750     return (TypeProfileArgsLimit > 1) ? 1 : 0;
 751   }
 752 
 753   static int cell_count_local_offset() {
 754      assert(arguments_profiling_enabled() && TypeProfileArgsLimit > 1, "no cell count");
 755      return 0;
 756    }
 757 
 758   int cell_count_global_offset() const {
 759     return _base_off + cell_count_local_offset();
 760   }
 761   
 762   // offset of cell for stack slot for entry i within ProfileData object
 763   int stack_slot_global_offset(int i) const {
 764     return _base_off + stack_slot_local_offset(i);
 765   }
 766 
 767   void check_number_of_arguments(uint total) {
 768     assert(number_of_arguments() == total, "should be set in DataLayout::initialize");
 769   }
 770 
 771   // number of cells not counting the header
 772   int cell_count_no_header() const {
 773     return _pd->uint_at(cell_count_global_offset());
 774   }
 775 
 776   static bool arguments_profiling_enabled();
 777   static void assert_arguments_profiling_enabled() {
 778     assert(arguments_profiling_enabled(), "args profiling should be on");
 779   }
 780 
 781 protected:
 782 
 783   // offset of cell for type for entry i within ProfileData object
 784   int type_global_offset(int i) const {
 785     return _base_off + type_local_offset(i);
 786   }
 787 
 788 public:
 789 
 790   TypeStackSlotEntries(int base_off, ProfileData* pd)
 791     : TypeEntries(base_off, pd) {}
 792 
 793   static int compute_cell_count(BytecodeStream* stream);
 794 
 795   static void initialize(DataLayout* dl, int base, int cell_count) {
 796     if (TypeProfileArgsLimit > 1) {
 797       int off = base + cell_count_local_offset();
 798       dl->set_cell_at(off, cell_count - base - header_cell_count());
 799     }
 800   }
 801 
 802   void post_initialize(BytecodeStream* stream);
 803 
 804   uint number_of_arguments() const {
 805     assert_arguments_profiling_enabled();
 806     if (TypeProfileArgsLimit > 1) {
 807       int cell_count = cell_count_no_header();
 808       int nb = cell_count / TypeStackSlotEntries::per_arg_count();
 809       assert(nb > 0 && nb <= TypeProfileArgsLimit , "only when we profile args");
 810       return nb;
 811     } else {
 812       assert(TypeProfileArgsLimit == 1, "at least one arg");
 813       return 1;
 814     }
 815   }
 816   
 817   int cell_count() const {
 818     assert_arguments_profiling_enabled();
 819     if (TypeProfileArgsLimit > 1) {
 820       return _base_off + header_cell_count() + _pd->int_at_unchecked(cell_count_global_offset());
 821     } else {
 822       return _base_off + TypeStackSlotEntries::per_arg_count();
 823     }
 824   }
 825 
 826   // offset of cell for stack slot for entry i within this block of cells for a TypeStackSlotEntries
 827   static int stack_slot_local_offset(int i) {
 828     assert_arguments_profiling_enabled();
 829     return header_cell_count() + i * per_arg_cell_count + stack_slot_entry;
 830   }
 831 
 832   // offset of cell for type for entry i within this block of cells for a TypeStackSlotEntries
 833   static int type_local_offset(int i) {
 834     return header_cell_count() + i * per_arg_cell_count + type_entry;
 835   }
 836 
 837   // stack slot for entry i
 838   uint stack_slot(int i) const {
 839     assert(i >= 0 && i < number_of_arguments(), "oob");
 840     return _pd->uint_at(stack_slot_global_offset(i));
 841   }
 842 
 843   // set stack slot for entry i
 844   void set_stack_slot(int i, uint num) {
 845     assert(i >= 0 && i < number_of_arguments(), "oob");
 846     _pd->set_uint_at(stack_slot_global_offset(i), num);
 847   }
 848   
 849   // type for entry i
 850   intptr_t type(int i) const {
 851     assert(i >= 0 && i < number_of_arguments(), "oob");
 852     return _pd->intptr_at(type_global_offset(i));
 853   }
 854 
 855   // set type for entry i
 856   void set_type(int i, intptr_t k) {
 857     assert(i >= 0 && i < number_of_arguments(), "oob");
 858     _pd->set_intptr_at(type_global_offset(i), k);
 859   }
 860 
 861   static ByteSize per_arg_size() {
 862     return in_ByteSize(per_arg_cell_count * DataLayout::cell_size);
 863   }
 864 
 865   static int per_arg_count() {
 866     return per_arg_cell_count ;
 867   }
 868 
 869   // Code generation support
 870    static ByteSize cell_count_offset() {
 871      return in_ByteSize(cell_count_local_offset() * DataLayout::cell_size);
 872    }
 873  
 874    static ByteSize args_data_offset() {
 875      return in_ByteSize(header_cell_count() * DataLayout::cell_size);
 876    }
 877  
 878    static ByteSize stack_slot_offset(int i) {
 879      return in_ByteSize(stack_slot_local_offset(i) * DataLayout::cell_size);
 880    }
 881  
 882    static ByteSize type_offset(int i) {
 883      return in_ByteSize(type_local_offset(i) * DataLayout::cell_size);
 884    }
 885 
 886   // GC support
 887   void clean_weak_klass_links(BoolObjectClosure* is_alive_closure);
 888 
 889 #ifndef PRODUCT
 890   void print_data_on(outputStream* st) const;
 891 #endif
 892 };
 893 
 894 // CallTypeData
 895 //
 896 // A CallTypeData is used to access profiling information about a non
 897 // virtual call for which we collect type information about arguments.
 898 class CallTypeData : public CounterData {
 899 private:
 900   TypeStackSlotEntries _args;
 901 
 902 public:
 903   CallTypeData(DataLayout* layout) :
 904     CounterData(layout), _args(CounterData::static_cell_count(), this)  {
 905     assert(layout->tag() == DataLayout::call_type_data_tag, "wrong type");
 906   }
 907 
 908   const TypeStackSlotEntries* args() const { return &_args; }
 909 
 910   virtual bool is_CallTypeData() const { return true; }
 911 
 912   static int static_cell_count() {
 913     return -1;
 914   }
 915 
 916   static int compute_cell_count(BytecodeStream* stream) {
 917     return CounterData::static_cell_count() + TypeStackSlotEntries::compute_cell_count(stream);
 918   }
 919   
 920   static void initialize(DataLayout* dl, int cell_count) {
 921     TypeStackSlotEntries::initialize(dl, CounterData::static_cell_count(), cell_count);
 922   }
 923 
 924   virtual void post_initialize(BytecodeStream* stream, MethodData* mdo) {
 925     _args.post_initialize(stream);
 926   }
 927 
 928   virtual int cell_count() const {
 929     return _args.cell_count();
 930   }
 931 
 932   uint number_of_arguments() const {
 933     return args()->number_of_arguments();
 934   }
 935 
 936   void set_argument_type(int i, Klass* k) {
 937     intptr_t current = _args.type(i);
 938     _args.set_type(i, TypeEntries::with_status(k, current));
 939   }
 940 
 941   // Code generation support
 942   static ByteSize args_data_offset() {
 943     return cell_offset(CounterData::static_cell_count()) + TypeStackSlotEntries::args_data_offset();
 944   }
 945 
 946   // GC support
 947   virtual void clean_weak_klass_links(BoolObjectClosure* is_alive_closure) {
 948     _args.clean_weak_klass_links(is_alive_closure);
 949   }
 950 
 951 #ifndef PRODUCT
 952   virtual void print_data_on(outputStream* st) const;
 953 #endif
 954 };
 955 
 956 // ReceiverTypeData
 957 //
 958 // A ReceiverTypeData is used to access profiling information about a
 959 // dynamic type check.  It consists of a counter which counts the total times
 960 // that the check is reached, and a series of (Klass*, count) pairs
 961 // which are used to store a type profile for the receiver of the check.
 962 class ReceiverTypeData : public CounterData {
 963 protected:
 964   enum {
 965     receiver0_offset = counter_cell_count,
 966     count0_offset,
 967     receiver_type_row_cell_count = (count0_offset + 1) - receiver0_offset
 968   };
 969 
 970 public:
 971   ReceiverTypeData(DataLayout* layout) : CounterData(layout) {
 972     assert(layout->tag() == DataLayout::receiver_type_data_tag ||
 973            layout->tag() == DataLayout::virtual_call_data_tag ||
 974            layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type");
 975   }
 976 
 977   virtual bool is_ReceiverTypeData() const { return true; }
 978 
 979   static int static_cell_count() {
 980     return counter_cell_count + (uint) TypeProfileWidth * receiver_type_row_cell_count;
 981   }
 982 
 983   virtual int cell_count() const {
 984     return static_cell_count();
 985   }
 986 
 987   // Direct accessors
 988   static uint row_limit() {
 989     return TypeProfileWidth;
 990   }
 991   static int receiver_cell_index(uint row) {
 992     return receiver0_offset + row * receiver_type_row_cell_count;
 993   }
 994   static int receiver_count_cell_index(uint row) {
 995     return count0_offset + row * receiver_type_row_cell_count;
 996   }
 997 
 998   Klass* receiver(uint row) const {
 999     assert(row < row_limit(), "oob");
1000 
1001     Klass* recv = (Klass*)intptr_at(receiver_cell_index(row));
1002     assert(recv == NULL || recv->is_klass(), "wrong type");
1003     return recv;
1004   }
1005 
1006   void set_receiver(uint row, Klass* k) {
1007     assert((uint)row < row_limit(), "oob");
1008     set_intptr_at(receiver_cell_index(row), (uintptr_t)k);
1009   }
1010 
1011   uint receiver_count(uint row) const {
1012     assert(row < row_limit(), "oob");
1013     return uint_at(receiver_count_cell_index(row));
1014   }
1015 
1016   void set_receiver_count(uint row, uint count) {
1017     assert(row < row_limit(), "oob");
1018     set_uint_at(receiver_count_cell_index(row), count);
1019   }
1020 
1021   void clear_row(uint row) {
1022     assert(row < row_limit(), "oob");
1023     // Clear total count - indicator of polymorphic call site.
1024     // The site may look like as monomorphic after that but
1025     // it allow to have more accurate profiling information because
1026     // there was execution phase change since klasses were unloaded.
1027     // If the site is still polymorphic then MDO will be updated
1028     // to reflect it. But it could be the case that the site becomes
1029     // only bimorphic. Then keeping total count not 0 will be wrong.
1030     // Even if we use monomorphic (when it is not) for compilation
1031     // we will only have trap, deoptimization and recompile again
1032     // with updated MDO after executing method in Interpreter.
1033     // An additional receiver will be recorded in the cleaned row
1034     // during next call execution.
1035     //
1036     // Note: our profiling logic works with empty rows in any slot.
1037     // We do sorting a profiling info (ciCallProfile) for compilation.
1038     //
1039     set_count(0);
1040     set_receiver(row, NULL);
1041     set_receiver_count(row, 0);
1042   }
1043 
1044   // Code generation support
1045   static ByteSize receiver_offset(uint row) {
1046     return cell_offset(receiver_cell_index(row));
1047   }
1048   static ByteSize receiver_count_offset(uint row) {
1049     return cell_offset(receiver_count_cell_index(row));
1050   }
1051   static ByteSize receiver_type_data_size() {
1052     return cell_offset(static_cell_count());
1053   }
1054 
1055   // GC support
1056   virtual void clean_weak_klass_links(BoolObjectClosure* is_alive_closure);
1057 
1058 #ifndef PRODUCT
1059   void print_receiver_data_on(outputStream* st) const;
1060   void print_data_on(outputStream* st) const;
1061 #endif
1062 };
1063 
1064 // VirtualCallData
1065 //
1066 // A VirtualCallData is used to access profiling information about a
1067 // virtual call.  For now, it has nothing more than a ReceiverTypeData.
1068 class VirtualCallData : public ReceiverTypeData {
1069 public:
1070   VirtualCallData(DataLayout* layout) : ReceiverTypeData(layout) {
1071     assert(layout->tag() == DataLayout::virtual_call_data_tag ||
1072            layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type");
1073   }
1074 
1075   virtual bool is_VirtualCallData() const { return true; }
1076 
1077   static int static_cell_count() {
1078     // At this point we could add more profile state, e.g., for arguments.
1079     // But for now it's the same size as the base record type.
1080     return ReceiverTypeData::static_cell_count();
1081   }
1082 
1083   virtual int cell_count() const {
1084     return static_cell_count();
1085   }
1086 
1087   // Direct accessors
1088   static ByteSize virtual_call_data_size() {
1089     return cell_offset(static_cell_count());
1090   }
1091 
1092 #ifndef PRODUCT
1093   void print_data_on(outputStream* st) const;
1094 #endif
1095 };
1096 
1097 // VirtualCallTypeData
1098 //
1099 // A VirtualCallTypeData is used to access profiling information about
1100 // a virtual call for which we collect type information about
1101 // arguments.
1102 class VirtualCallTypeData : public VirtualCallData {
1103 private:
1104   TypeStackSlotEntries _args;
1105 
1106 public:
1107   VirtualCallTypeData(DataLayout* layout) :
1108     VirtualCallData(layout), _args(VirtualCallData::static_cell_count(), this)  {
1109     assert(layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type");
1110   }
1111 
1112   const TypeStackSlotEntries* args() const { return &_args; }
1113 
1114   virtual bool is_VirtualCallTypeData() const { return true; }
1115 
1116   static int static_cell_count() {
1117     return -1;
1118   }
1119 
1120   static int compute_cell_count(BytecodeStream* stream) {
1121     return VirtualCallData::static_cell_count() + TypeStackSlotEntries::compute_cell_count(stream);
1122   }
1123   
1124   static void initialize(DataLayout* dl, int cell_count) {
1125     TypeStackSlotEntries::initialize(dl, VirtualCallData::static_cell_count(), cell_count);
1126   }
1127 
1128   virtual void post_initialize(BytecodeStream* stream, MethodData* mdo) {
1129     _args.post_initialize(stream);
1130   }
1131 
1132   virtual int cell_count() const {
1133     return _args.cell_count();
1134   }
1135 
1136   uint number_of_arguments() const {
1137     return args()->number_of_arguments();
1138   }
1139 
1140   void set_argument_type(int i, Klass* k) {
1141     intptr_t current = _args.type(i);
1142     _args.set_type(i, TypeEntries::with_status(k, current));
1143   }
1144 
1145   // Code generation support
1146   static ByteSize args_data_offset() {
1147     return cell_offset(VirtualCallData::static_cell_count()) + TypeStackSlotEntries::args_data_offset();
1148   }
1149 
1150   // GC support
1151   virtual void clean_weak_klass_links(BoolObjectClosure* is_alive_closure) {
1152     ReceiverTypeData::clean_weak_klass_links(is_alive_closure);
1153     _args.clean_weak_klass_links(is_alive_closure);
1154   }
1155 
1156 #ifndef PRODUCT
1157   virtual void print_data_on(outputStream* st) const;
1158 #endif
1159 };
1160 
1161 // RetData
1162 //
1163 // A RetData is used to access profiling information for a ret bytecode.
1164 // It is composed of a count of the number of times that the ret has
1165 // been executed, followed by a series of triples of the form
1166 // (bci, count, di) which count the number of times that some bci was the
1167 // target of the ret and cache a corresponding data displacement.
1168 class RetData : public CounterData {
1169 protected:
1170   enum {
1171     bci0_offset = counter_cell_count,
1172     count0_offset,
1173     displacement0_offset,
1174     ret_row_cell_count = (displacement0_offset + 1) - bci0_offset
1175   };
1176 
1177   void set_bci(uint row, int bci) {
1178     assert((uint)row < row_limit(), "oob");
1179     set_int_at(bci0_offset + row * ret_row_cell_count, bci);
1180   }
1181   void release_set_bci(uint row, int bci) {
1182     assert((uint)row < row_limit(), "oob");
1183     // 'release' when setting the bci acts as a valid flag for other
1184     // threads wrt bci_count and bci_displacement.
1185     release_set_int_at(bci0_offset + row * ret_row_cell_count, bci);
1186   }
1187   void set_bci_count(uint row, uint count) {
1188     assert((uint)row < row_limit(), "oob");
1189     set_uint_at(count0_offset + row * ret_row_cell_count, count);
1190   }
1191   void set_bci_displacement(uint row, int disp) {
1192     set_int_at(displacement0_offset + row * ret_row_cell_count, disp);
1193   }
1194 
1195 public:
1196   RetData(DataLayout* layout) : CounterData(layout) {
1197     assert(layout->tag() == DataLayout::ret_data_tag, "wrong type");
1198   }
1199 
1200   virtual bool is_RetData() const { return true; }
1201 
1202   enum {
1203     no_bci = -1 // value of bci when bci1/2 are not in use.
1204   };
1205 
1206   static int static_cell_count() {
1207     return counter_cell_count + (uint) BciProfileWidth * ret_row_cell_count;
1208   }
1209 
1210   virtual int cell_count() const {
1211     return static_cell_count();
1212   }
1213 
1214   static uint row_limit() {
1215     return BciProfileWidth;
1216   }
1217   static int bci_cell_index(uint row) {
1218     return bci0_offset + row * ret_row_cell_count;
1219   }
1220   static int bci_count_cell_index(uint row) {
1221     return count0_offset + row * ret_row_cell_count;
1222   }
1223   static int bci_displacement_cell_index(uint row) {
1224     return displacement0_offset + row * ret_row_cell_count;
1225   }
1226 
1227   // Direct accessors
1228   int bci(uint row) const {
1229     return int_at(bci_cell_index(row));
1230   }
1231   uint bci_count(uint row) const {
1232     return uint_at(bci_count_cell_index(row));
1233   }
1234   int bci_displacement(uint row) const {
1235     return int_at(bci_displacement_cell_index(row));
1236   }
1237 
1238   // Interpreter Runtime support
1239   address fixup_ret(int return_bci, MethodData* mdo);
1240 
1241   // Code generation support
1242   static ByteSize bci_offset(uint row) {
1243     return cell_offset(bci_cell_index(row));
1244   }
1245   static ByteSize bci_count_offset(uint row) {
1246     return cell_offset(bci_count_cell_index(row));
1247   }
1248   static ByteSize bci_displacement_offset(uint row) {
1249     return cell_offset(bci_displacement_cell_index(row));
1250   }
1251 
1252   // Specific initialization.
1253   void post_initialize(BytecodeStream* stream, MethodData* mdo);
1254 
1255 #ifndef PRODUCT
1256   void print_data_on(outputStream* st) const;
1257 #endif
1258 };
1259 
1260 // BranchData
1261 //
1262 // A BranchData is used to access profiling data for a two-way branch.
1263 // It consists of taken and not_taken counts as well as a data displacement
1264 // for the taken case.
1265 class BranchData : public JumpData {
1266 protected:
1267   enum {
1268     not_taken_off_set = jump_cell_count,
1269     branch_cell_count
1270   };
1271 
1272   void set_displacement(int displacement) {
1273     set_int_at(displacement_off_set, displacement);
1274   }
1275 
1276 public:
1277   BranchData(DataLayout* layout) : JumpData(layout) {
1278     assert(layout->tag() == DataLayout::branch_data_tag, "wrong type");
1279   }
1280 
1281   virtual bool is_BranchData() const { return true; }
1282 
1283   static int static_cell_count() {
1284     return branch_cell_count;
1285   }
1286 
1287   virtual int cell_count() const {
1288     return static_cell_count();
1289   }
1290 
1291   // Direct accessor
1292   uint not_taken() const {
1293     return uint_at(not_taken_off_set);
1294   }
1295 
1296   void set_not_taken(uint cnt) {
1297     set_uint_at(not_taken_off_set, cnt);
1298   }
1299 
1300   uint inc_not_taken() {
1301     uint cnt = not_taken() + 1;
1302     // Did we wrap? Will compiler screw us??
1303     if (cnt == 0) cnt--;
1304     set_uint_at(not_taken_off_set, cnt);
1305     return cnt;
1306   }
1307 
1308   // Code generation support
1309   static ByteSize not_taken_offset() {
1310     return cell_offset(not_taken_off_set);
1311   }
1312   static ByteSize branch_data_size() {
1313     return cell_offset(branch_cell_count);
1314   }
1315 
1316   // Specific initialization.
1317   void post_initialize(BytecodeStream* stream, MethodData* mdo);
1318 
1319 #ifndef PRODUCT
1320   void print_data_on(outputStream* st) const;
1321 #endif
1322 };
1323 
1324 // ArrayData
1325 //
1326 // A ArrayData is a base class for accessing profiling data which does
1327 // not have a statically known size.  It consists of an array length
1328 // and an array start.
1329 class ArrayData : public ProfileData {
1330 protected:
1331   friend class DataLayout;
1332 
1333   enum {
1334     array_len_off_set,
1335     array_start_off_set
1336   };
1337 
1338   uint array_uint_at(int index) const {
1339     int aindex = index + array_start_off_set;
1340     return uint_at(aindex);
1341   }
1342   int array_int_at(int index) const {
1343     int aindex = index + array_start_off_set;
1344     return int_at(aindex);
1345   }
1346   oop array_oop_at(int index) const {
1347     int aindex = index + array_start_off_set;
1348     return oop_at(aindex);
1349   }
1350   void array_set_int_at(int index, int value) {
1351     int aindex = index + array_start_off_set;
1352     set_int_at(aindex, value);
1353   }
1354 
1355   // Code generation support for subclasses.
1356   static ByteSize array_element_offset(int index) {
1357     return cell_offset(array_start_off_set + index);
1358   }
1359 
1360 public:
1361   ArrayData(DataLayout* layout) : ProfileData(layout) {}
1362 
1363   virtual bool is_ArrayData() const { return true; }
1364 
1365   static int static_cell_count() {
1366     return -1;
1367   }
1368 
1369   int array_len() const {
1370     return int_at_unchecked(array_len_off_set);
1371   }
1372 
1373   virtual int cell_count() const {
1374     return array_len() + 1;
1375   }
1376 
1377   // Code generation support
1378   static ByteSize array_len_offset() {
1379     return cell_offset(array_len_off_set);
1380   }
1381   static ByteSize array_start_offset() {
1382     return cell_offset(array_start_off_set);
1383   }
1384 };
1385 
1386 // MultiBranchData
1387 //
1388 // A MultiBranchData is used to access profiling information for
1389 // a multi-way branch (*switch bytecodes).  It consists of a series
1390 // of (count, displacement) pairs, which count the number of times each
1391 // case was taken and specify the data displacment for each branch target.
1392 class MultiBranchData : public ArrayData {
1393 protected:
1394   enum {
1395     default_count_off_set,
1396     default_disaplacement_off_set,
1397     case_array_start
1398   };
1399   enum {
1400     relative_count_off_set,
1401     relative_displacement_off_set,
1402     per_case_cell_count
1403   };
1404 
1405   void set_default_displacement(int displacement) {
1406     array_set_int_at(default_disaplacement_off_set, displacement);
1407   }
1408   void set_displacement_at(int index, int displacement) {
1409     array_set_int_at(case_array_start +
1410                      index * per_case_cell_count +
1411                      relative_displacement_off_set,
1412                      displacement);
1413   }
1414 
1415 public:
1416   MultiBranchData(DataLayout* layout) : ArrayData(layout) {
1417     assert(layout->tag() == DataLayout::multi_branch_data_tag, "wrong type");
1418   }
1419 
1420   virtual bool is_MultiBranchData() const { return true; }
1421 
1422   static int compute_cell_count(BytecodeStream* stream);
1423 
1424   int number_of_cases() const {
1425     int alen = array_len() - 2; // get rid of default case here.
1426     assert(alen % per_case_cell_count == 0, "must be even");
1427     return (alen / per_case_cell_count);
1428   }
1429 
1430   uint default_count() const {
1431     return array_uint_at(default_count_off_set);
1432   }
1433   int default_displacement() const {
1434     return array_int_at(default_disaplacement_off_set);
1435   }
1436 
1437   uint count_at(int index) const {
1438     return array_uint_at(case_array_start +
1439                          index * per_case_cell_count +
1440                          relative_count_off_set);
1441   }
1442   int displacement_at(int index) const {
1443     return array_int_at(case_array_start +
1444                         index * per_case_cell_count +
1445                         relative_displacement_off_set);
1446   }
1447 
1448   // Code generation support
1449   static ByteSize default_count_offset() {
1450     return array_element_offset(default_count_off_set);
1451   }
1452   static ByteSize default_displacement_offset() {
1453     return array_element_offset(default_disaplacement_off_set);
1454   }
1455   static ByteSize case_count_offset(int index) {
1456     return case_array_offset() +
1457            (per_case_size() * index) +
1458            relative_count_offset();
1459   }
1460   static ByteSize case_array_offset() {
1461     return array_element_offset(case_array_start);
1462   }
1463   static ByteSize per_case_size() {
1464     return in_ByteSize(per_case_cell_count) * cell_size;
1465   }
1466   static ByteSize relative_count_offset() {
1467     return in_ByteSize(relative_count_off_set) * cell_size;
1468   }
1469   static ByteSize relative_displacement_offset() {
1470     return in_ByteSize(relative_displacement_off_set) * cell_size;
1471   }
1472 
1473   // Specific initialization.
1474   void post_initialize(BytecodeStream* stream, MethodData* mdo);
1475 
1476 #ifndef PRODUCT
1477   void print_data_on(outputStream* st) const;
1478 #endif
1479 };
1480 
1481 class ArgInfoData : public ArrayData {
1482 
1483 public:
1484   ArgInfoData(DataLayout* layout) : ArrayData(layout) {
1485     assert(layout->tag() == DataLayout::arg_info_data_tag, "wrong type");
1486   }
1487 
1488   virtual bool is_ArgInfoData() const { return true; }
1489 
1490 
1491   int number_of_args() const {
1492     return array_len();
1493   }
1494 
1495   uint arg_modified(int arg) const {
1496     return array_uint_at(arg);
1497   }
1498 
1499   void set_arg_modified(int arg, uint val) {
1500     array_set_int_at(arg, val);
1501   }
1502 
1503 #ifndef PRODUCT
1504   void print_data_on(outputStream* st) const;
1505 #endif
1506 };
1507 
1508 // MethodData*
1509 //
1510 // A MethodData* holds information which has been collected about
1511 // a method.  Its layout looks like this:
1512 //
1513 // -----------------------------
1514 // | header                    |
1515 // | klass                     |
1516 // -----------------------------
1517 // | method                    |
1518 // | size of the MethodData* |
1519 // -----------------------------
1520 // | Data entries...           |
1521 // |   (variable size)         |
1522 // |                           |
1523 // .                           .
1524 // .                           .
1525 // .                           .
1526 // |                           |
1527 // -----------------------------
1528 //
1529 // The data entry area is a heterogeneous array of DataLayouts. Each
1530 // DataLayout in the array corresponds to a specific bytecode in the
1531 // method.  The entries in the array are sorted by the corresponding
1532 // bytecode.  Access to the data is via resource-allocated ProfileData,
1533 // which point to the underlying blocks of DataLayout structures.
1534 //
1535 // During interpretation, if profiling in enabled, the interpreter
1536 // maintains a method data pointer (mdp), which points at the entry
1537 // in the array corresponding to the current bci.  In the course of
1538 // intepretation, when a bytecode is encountered that has profile data
1539 // associated with it, the entry pointed to by mdp is updated, then the
1540 // mdp is adjusted to point to the next appropriate DataLayout.  If mdp
1541 // is NULL to begin with, the interpreter assumes that the current method
1542 // is not (yet) being profiled.
1543 //
1544 // In MethodData* parlance, "dp" is a "data pointer", the actual address
1545 // of a DataLayout element.  A "di" is a "data index", the offset in bytes
1546 // from the base of the data entry array.  A "displacement" is the byte offset
1547 // in certain ProfileData objects that indicate the amount the mdp must be
1548 // adjusted in the event of a change in control flow.
1549 //
1550 
1551 class MethodData : public Metadata {
1552   friend class VMStructs;
1553 private:
1554   friend class ProfileData;
1555 
1556   // Back pointer to the Method*
1557   Method* _method;
1558 
1559   // Size of this oop in bytes
1560   int _size;
1561 
1562   // Cached hint for bci_to_dp and bci_to_data
1563   int _hint_di;
1564 
1565   MethodData(methodHandle method, int size, TRAPS);
1566 public:
1567   static MethodData* allocate(ClassLoaderData* loader_data, methodHandle method, TRAPS);
1568   MethodData() {}; // For ciMethodData
1569 
1570   bool is_methodData() const volatile { return true; }
1571 
1572   // Whole-method sticky bits and flags
1573   enum {
1574     _trap_hist_limit    = 17,   // decoupled from Deoptimization::Reason_LIMIT
1575     _trap_hist_mask     = max_jubyte,
1576     _extra_data_count   = 4     // extra DataLayout headers, for trap history
1577   }; // Public flag values
1578 private:
1579   uint _nof_decompiles;             // count of all nmethod removals
1580   uint _nof_overflow_recompiles;    // recompile count, excluding recomp. bits
1581   uint _nof_overflow_traps;         // trap count, excluding _trap_hist
1582   union {
1583     intptr_t _align;
1584     u1 _array[_trap_hist_limit];
1585   } _trap_hist;
1586 
1587   // Support for interprocedural escape analysis, from Thomas Kotzmann.
1588   intx              _eflags;          // flags on escape information
1589   intx              _arg_local;       // bit set of non-escaping arguments
1590   intx              _arg_stack;       // bit set of stack-allocatable arguments
1591   intx              _arg_returned;    // bit set of returned arguments
1592 
1593   int _creation_mileage;              // method mileage at MDO creation
1594 
1595   // How many invocations has this MDO seen?
1596   // These counters are used to determine the exact age of MDO.
1597   // We need those because in tiered a method can be concurrently
1598   // executed at different levels.
1599   InvocationCounter _invocation_counter;
1600   // Same for backedges.
1601   InvocationCounter _backedge_counter;
1602   // Counter values at the time profiling started.
1603   int               _invocation_counter_start;
1604   int               _backedge_counter_start;
1605   // Number of loops and blocks is computed when compiling the first
1606   // time with C1. It is used to determine if method is trivial.
1607   short             _num_loops;
1608   short             _num_blocks;
1609   // Highest compile level this method has ever seen.
1610   u1                _highest_comp_level;
1611   // Same for OSR level
1612   u1                _highest_osr_comp_level;
1613   // Does this method contain anything worth profiling?
1614   bool              _would_profile;
1615 
1616   // Size of _data array in bytes.  (Excludes header and extra_data fields.)
1617   int _data_size;
1618 
1619   // Beginning of the data entries
1620   intptr_t _data[1];
1621 
1622   // Helper for size computation
1623   static int compute_data_size(BytecodeStream* stream);
1624   static int bytecode_cell_count(Bytecodes::Code code);
1625   enum { no_profile_data = -1, variable_cell_count = -2 };
1626 
1627   // Helper for initialization
1628   DataLayout* data_layout_at(int data_index) const {
1629     assert(data_index % sizeof(intptr_t) == 0, "unaligned");
1630     return (DataLayout*) (((address)_data) + data_index);
1631   }
1632 
1633   // Initialize an individual data segment.  Returns the size of
1634   // the segment in bytes.
1635   int initialize_data(BytecodeStream* stream, int data_index);
1636 
1637   // Helper for data_at
1638   DataLayout* limit_data_position() const {
1639     return (DataLayout*)((address)data_base() + _data_size);
1640   }
1641   bool out_of_bounds(int data_index) const {
1642     return data_index >= data_size();
1643   }
1644 
1645   // Give each of the data entries a chance to perform specific
1646   // data initialization.
1647   void post_initialize(BytecodeStream* stream);
1648 
1649   // hint accessors
1650   int      hint_di() const  { return _hint_di; }
1651   void set_hint_di(int di)  {
1652     assert(!out_of_bounds(di), "hint_di out of bounds");
1653     _hint_di = di;
1654   }
1655   ProfileData* data_before(int bci) {
1656     // avoid SEGV on this edge case
1657     if (data_size() == 0)
1658       return NULL;
1659     int hint = hint_di();
1660     if (data_layout_at(hint)->bci() <= bci)
1661       return data_at(hint);
1662     return first_data();
1663   }
1664 
1665   // What is the index of the first data entry?
1666   int first_di() const { return 0; }
1667 
1668   // Find or create an extra ProfileData:
1669   ProfileData* bci_to_extra_data(int bci, bool create_if_missing);
1670 
1671   // return the argument info cell
1672   ArgInfoData *arg_info();
1673 
1674   enum {
1675     no_type_profile = 0,
1676     type_profile_jsr292 = 1,
1677     type_profile_all = 2
1678   };
1679 
1680   static bool profile_jsr292(methodHandle m, int bci);
1681   static int profile_arguments_flag();
1682   static bool profile_arguments_jsr292_only();
1683   static bool profile_all_arguments();
1684   static bool profile_arguments_for_invoke(methodHandle m, int bci);
1685 
1686 public:
1687   static int header_size() {
1688     return sizeof(MethodData)/wordSize;
1689   }
1690 
1691   // Compute the size of a MethodData* before it is created.
1692   static int compute_allocation_size_in_bytes(methodHandle method);
1693   static int compute_allocation_size_in_words(methodHandle method);
1694   static int compute_extra_data_count(int data_size, int empty_bc_count);
1695 
1696   // Determine if a given bytecode can have profile information.
1697   static bool bytecode_has_profile(Bytecodes::Code code) {
1698     return bytecode_cell_count(code) != no_profile_data;
1699   }
1700 
1701   // reset into original state
1702   void init();
1703 
1704   // My size
1705   int size_in_bytes() const { return _size; }
1706   int size() const    { return align_object_size(align_size_up(_size, BytesPerWord)/BytesPerWord); }
1707 #if INCLUDE_SERVICES
1708   void collect_statistics(KlassSizeStats *sz) const;
1709 #endif
1710 
1711   int      creation_mileage() const  { return _creation_mileage; }
1712   void set_creation_mileage(int x)   { _creation_mileage = x; }
1713 
1714   int invocation_count() {
1715     if (invocation_counter()->carry()) {
1716       return InvocationCounter::count_limit;
1717     }
1718     return invocation_counter()->count();
1719   }
1720   int backedge_count() {
1721     if (backedge_counter()->carry()) {
1722       return InvocationCounter::count_limit;
1723     }
1724     return backedge_counter()->count();
1725   }
1726 
1727   int invocation_count_start() {
1728     if (invocation_counter()->carry()) {
1729       return 0;
1730     }
1731     return _invocation_counter_start;
1732   }
1733 
1734   int backedge_count_start() {
1735     if (backedge_counter()->carry()) {
1736       return 0;
1737     }
1738     return _backedge_counter_start;
1739   }
1740 
1741   int invocation_count_delta() { return invocation_count() - invocation_count_start(); }
1742   int backedge_count_delta()   { return backedge_count()   - backedge_count_start();   }
1743 
1744   void reset_start_counters() {
1745     _invocation_counter_start = invocation_count();
1746     _backedge_counter_start = backedge_count();
1747   }
1748 
1749   InvocationCounter* invocation_counter()     { return &_invocation_counter; }
1750   InvocationCounter* backedge_counter()       { return &_backedge_counter;   }
1751 
1752   void set_would_profile(bool p)              { _would_profile = p;    }
1753   bool would_profile() const                  { return _would_profile; }
1754 
1755   int highest_comp_level() const              { return _highest_comp_level;      }
1756   void set_highest_comp_level(int level)      { _highest_comp_level = level;     }
1757   int highest_osr_comp_level() const          { return _highest_osr_comp_level;  }
1758   void set_highest_osr_comp_level(int level)  { _highest_osr_comp_level = level; }
1759 
1760   int num_loops() const                       { return _num_loops;  }
1761   void set_num_loops(int n)                   { _num_loops = n;     }
1762   int num_blocks() const                      { return _num_blocks; }
1763   void set_num_blocks(int n)                  { _num_blocks = n;    }
1764 
1765   bool is_mature() const;  // consult mileage and ProfileMaturityPercentage
1766   static int mileage_of(Method* m);
1767 
1768   // Support for interprocedural escape analysis, from Thomas Kotzmann.
1769   enum EscapeFlag {
1770     estimated    = 1 << 0,
1771     return_local = 1 << 1,
1772     return_allocated = 1 << 2,
1773     allocated_escapes = 1 << 3,
1774     unknown_modified = 1 << 4
1775   };
1776 
1777   intx eflags()                                  { return _eflags; }
1778   intx arg_local()                               { return _arg_local; }
1779   intx arg_stack()                               { return _arg_stack; }
1780   intx arg_returned()                            { return _arg_returned; }
1781   uint arg_modified(int a)                       { ArgInfoData *aid = arg_info();
1782                                                    assert(aid != NULL, "arg_info must be not null");
1783                                                    assert(a >= 0 && a < aid->number_of_args(), "valid argument number");
1784                                                    return aid->arg_modified(a); }
1785 
1786   void set_eflags(intx v)                        { _eflags = v; }
1787   void set_arg_local(intx v)                     { _arg_local = v; }
1788   void set_arg_stack(intx v)                     { _arg_stack = v; }
1789   void set_arg_returned(intx v)                  { _arg_returned = v; }
1790   void set_arg_modified(int a, uint v)           { ArgInfoData *aid = arg_info();
1791                                                    assert(aid != NULL, "arg_info must be not null");
1792                                                    assert(a >= 0 && a < aid->number_of_args(), "valid argument number");
1793                                                    aid->set_arg_modified(a, v); }
1794 
1795   void clear_escape_info()                       { _eflags = _arg_local = _arg_stack = _arg_returned = 0; }
1796 
1797   // Location and size of data area
1798   address data_base() const {
1799     return (address) _data;
1800   }
1801   int data_size() const {
1802     return _data_size;
1803   }
1804 
1805   // Accessors
1806   Method* method() const { return _method; }
1807 
1808   // Get the data at an arbitrary (sort of) data index.
1809   ProfileData* data_at(int data_index) const;
1810 
1811   // Walk through the data in order.
1812   ProfileData* first_data() const { return data_at(first_di()); }
1813   ProfileData* next_data(ProfileData* current) const;
1814   bool is_valid(ProfileData* current) const { return current != NULL; }
1815 
1816   // Convert a dp (data pointer) to a di (data index).
1817   int dp_to_di(address dp) const {
1818     return dp - ((address)_data);
1819   }
1820 
1821   address di_to_dp(int di) {
1822     return (address)data_layout_at(di);
1823   }
1824 
1825   // bci to di/dp conversion.
1826   address bci_to_dp(int bci);
1827   int bci_to_di(int bci) {
1828     return dp_to_di(bci_to_dp(bci));
1829   }
1830 
1831   // Get the data at an arbitrary bci, or NULL if there is none.
1832   ProfileData* bci_to_data(int bci);
1833 
1834   // Same, but try to create an extra_data record if one is needed:
1835   ProfileData* allocate_bci_to_data(int bci) {
1836     ProfileData* data = bci_to_data(bci);
1837     return (data != NULL) ? data : bci_to_extra_data(bci, true);
1838   }
1839 
1840   // Add a handful of extra data records, for trap tracking.
1841   DataLayout* extra_data_base() const { return limit_data_position(); }
1842   DataLayout* extra_data_limit() const { return (DataLayout*)((address)this + size_in_bytes()); }
1843   int extra_data_size() const { return (address)extra_data_limit()
1844                                - (address)extra_data_base(); }
1845   static DataLayout* next_extra(DataLayout* dp) { return (DataLayout*)((address)dp + in_bytes(DataLayout::cell_offset(0))); }
1846 
1847   // Return (uint)-1 for overflow.
1848   uint trap_count(int reason) const {
1849     assert((uint)reason < _trap_hist_limit, "oob");
1850     return (int)((_trap_hist._array[reason]+1) & _trap_hist_mask) - 1;
1851   }
1852   // For loops:
1853   static uint trap_reason_limit() { return _trap_hist_limit; }
1854   static uint trap_count_limit()  { return _trap_hist_mask; }
1855   uint inc_trap_count(int reason) {
1856     // Count another trap, anywhere in this method.
1857     assert(reason >= 0, "must be single trap");
1858     if ((uint)reason < _trap_hist_limit) {
1859       uint cnt1 = 1 + _trap_hist._array[reason];
1860       if ((cnt1 & _trap_hist_mask) != 0) {  // if no counter overflow...
1861         _trap_hist._array[reason] = cnt1;
1862         return cnt1;
1863       } else {
1864         return _trap_hist_mask + (++_nof_overflow_traps);
1865       }
1866     } else {
1867       // Could not represent the count in the histogram.
1868       return (++_nof_overflow_traps);
1869     }
1870   }
1871 
1872   uint overflow_trap_count() const {
1873     return _nof_overflow_traps;
1874   }
1875   uint overflow_recompile_count() const {
1876     return _nof_overflow_recompiles;
1877   }
1878   void inc_overflow_recompile_count() {
1879     _nof_overflow_recompiles += 1;
1880   }
1881   uint decompile_count() const {
1882     return _nof_decompiles;
1883   }
1884   void inc_decompile_count() {
1885     _nof_decompiles += 1;
1886     if (decompile_count() > (uint)PerMethodRecompilationCutoff) {
1887       method()->set_not_compilable(CompLevel_full_optimization, true, "decompile_count > PerMethodRecompilationCutoff");
1888     }
1889   }
1890 
1891   // Support for code generation
1892   static ByteSize data_offset() {
1893     return byte_offset_of(MethodData, _data[0]);
1894   }
1895 
1896   static ByteSize invocation_counter_offset() {
1897     return byte_offset_of(MethodData, _invocation_counter);
1898   }
1899   static ByteSize backedge_counter_offset() {
1900     return byte_offset_of(MethodData, _backedge_counter);
1901   }
1902 
1903   // Deallocation support - no pointer fields to deallocate
1904   void deallocate_contents(ClassLoaderData* loader_data) {}
1905 
1906   // GC support
1907   void set_size(int object_size_in_bytes) { _size = object_size_in_bytes; }
1908 
1909   // Printing
1910 #ifndef PRODUCT
1911   void print_on      (outputStream* st) const;
1912 #endif
1913   void print_value_on(outputStream* st) const;
1914 
1915 #ifndef PRODUCT
1916   // printing support for method data
1917   void print_data_on(outputStream* st) const;
1918 #endif
1919 
1920   const char* internal_name() const { return "{method data}"; }
1921 
1922   // verification
1923   void verify_on(outputStream* st);
1924   void verify_data_on(outputStream* st);
1925 
1926   static bool profile_arguments();
1927 };
1928 
1929 #endif // SHARE_VM_OOPS_METHODDATAOOP_HPP