1 /*
   2  * Copyright (c) 2000, 2010, 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 class BytecodeStream;
  26 
  27 // The MethodData object collects counts and other profile information
  28 // during zeroth-tier (interpretive) and first-tier execution.
  29 // The profile is used later by compilation heuristics.  Some heuristics
  30 // enable use of aggressive (or "heroic") optimizations.  An aggressive
  31 // optimization often has a down-side, a corner case that it handles
  32 // poorly, but which is thought to be rare.  The profile provides
  33 // evidence of this rarity for a given method or even BCI.  It allows
  34 // the compiler to back out of the optimization at places where it
  35 // has historically been a poor choice.  Other heuristics try to use
  36 // specific information gathered about types observed at a given site.
  37 //
  38 // All data in the profile is approximate.  It is expected to be accurate
  39 // on the whole, but the system expects occasional inaccuraces, due to
  40 // counter overflow, multiprocessor races during data collection, space
  41 // limitations, missing MDO blocks, etc.  Bad or missing data will degrade
  42 // optimization quality but will not affect correctness.  Also, each MDO
  43 // is marked with its birth-date ("creation_mileage") which can be used
  44 // to assess the quality ("maturity") of its data.
  45 //
  46 // Short (<32-bit) counters are designed to overflow to a known "saturated"
  47 // state.  Also, certain recorded per-BCI events are given one-bit counters
  48 // which overflow to a saturated state which applied to all counters at
  49 // that BCI.  In other words, there is a small lattice which approximates
  50 // the ideal of an infinite-precision counter for each event at each BCI,
  51 // and the lattice quickly "bottoms out" in a state where all counters
  52 // are taken to be indefinitely large.
  53 //
  54 // The reader will find many data races in profile gathering code, starting
  55 // with invocation counter incrementation.  None of these races harm correct
  56 // execution of the compiled code.
  57 
  58 // forward decl
  59 class ProfileData;
  60 
  61 // DataLayout
  62 //
  63 // Overlay for generic profiling data.
  64 class DataLayout VALUE_OBJ_CLASS_SPEC {
  65 private:
  66   // Every data layout begins with a header.  This header
  67   // contains a tag, which is used to indicate the size/layout
  68   // of the data, 4 bits of flags, which can be used in any way,
  69   // 4 bits of trap history (none/one reason/many reasons),
  70   // and a bci, which is used to tie this piece of data to a
  71   // specific bci in the bytecodes.
  72   union {
  73     intptr_t _bits;
  74     struct {
  75       u1 _tag;
  76       u1 _flags;
  77       u2 _bci;
  78     } _struct;
  79   } _header;
  80 
  81   // The data layout has an arbitrary number of cells, each sized
  82   // to accomodate a pointer or an integer.
  83   intptr_t _cells[1];
  84 
  85   // Some types of data layouts need a length field.
  86   static bool needs_array_len(u1 tag);
  87 
  88 public:
  89   enum {
  90     counter_increment = 1
  91   };
  92 
  93   enum {
  94     cell_size = sizeof(intptr_t)
  95   };
  96 
  97   // Tag values
  98   enum {
  99     no_tag,
 100     bit_data_tag,
 101     counter_data_tag,
 102     jump_data_tag,
 103     receiver_type_data_tag,
 104     virtual_call_data_tag,
 105     ret_data_tag,
 106     branch_data_tag,
 107     multi_branch_data_tag,
 108     arg_info_data_tag
 109   };
 110 
 111   enum {
 112     // The _struct._flags word is formatted as [trap_state:4 | flags:4].
 113     // The trap state breaks down further as [recompile:1 | reason:3].
 114     // This further breakdown is defined in deoptimization.cpp.
 115     // See Deoptimization::trap_state_reason for an assert that
 116     // trap_bits is big enough to hold reasons < Reason_RECORDED_LIMIT.
 117     //
 118     // The trap_state is collected only if ProfileTraps is true.
 119     trap_bits = 1+3,  // 3: enough to distinguish [0..Reason_RECORDED_LIMIT].
 120     trap_shift = BitsPerByte - trap_bits,
 121     trap_mask = right_n_bits(trap_bits),
 122     trap_mask_in_place = (trap_mask << trap_shift),
 123     flag_limit = trap_shift,
 124     flag_mask = right_n_bits(flag_limit),
 125     first_flag = 0
 126   };
 127 
 128   // Size computation
 129   static int header_size_in_bytes() {
 130     return cell_size;
 131   }
 132   static int header_size_in_cells() {
 133     return 1;
 134   }
 135 
 136   static int compute_size_in_bytes(int cell_count) {
 137     return header_size_in_bytes() + cell_count * cell_size;
 138   }
 139 
 140   // Initialization
 141   void initialize(u1 tag, u2 bci, int cell_count);
 142 
 143   // Accessors
 144   u1 tag() {
 145     return _header._struct._tag;
 146   }
 147 
 148   // Return a few bits of trap state.  Range is [0..trap_mask].
 149   // The state tells if traps with zero, one, or many reasons have occurred.
 150   // It also tells whether zero or many recompilations have occurred.
 151   // The associated trap histogram in the MDO itself tells whether
 152   // traps are common or not.  If a BCI shows that a trap X has
 153   // occurred, and the MDO shows N occurrences of X, we make the
 154   // simplifying assumption that all N occurrences can be blamed
 155   // on that BCI.
 156   int trap_state() {
 157     return ((_header._struct._flags >> trap_shift) & trap_mask);
 158   }
 159 
 160   void set_trap_state(int new_state) {
 161     assert(ProfileTraps, "used only under +ProfileTraps");
 162     uint old_flags = (_header._struct._flags & flag_mask);
 163     _header._struct._flags = (new_state << trap_shift) | old_flags;
 164   }
 165 
 166   u1 flags() {
 167     return _header._struct._flags;
 168   }
 169 
 170   u2 bci() {
 171     return _header._struct._bci;
 172   }
 173 
 174   void set_header(intptr_t value) {
 175     _header._bits = value;
 176   }
 177   void release_set_header(intptr_t value) {
 178     OrderAccess::release_store_ptr(&_header._bits, value);
 179   }
 180   intptr_t header() {
 181     return _header._bits;
 182   }
 183   void set_cell_at(int index, intptr_t value) {
 184     _cells[index] = value;
 185   }
 186   void release_set_cell_at(int index, intptr_t value) {
 187     OrderAccess::release_store_ptr(&_cells[index], value);
 188   }
 189   intptr_t cell_at(int index) {
 190     return _cells[index];
 191   }
 192   intptr_t* adr_cell_at(int index) {
 193     return &_cells[index];
 194   }
 195   oop* adr_oop_at(int index) {
 196     return (oop*)&(_cells[index]);
 197   }
 198 
 199   void set_flag_at(int flag_number) {
 200     assert(flag_number < flag_limit, "oob");
 201     _header._struct._flags |= (0x1 << flag_number);
 202   }
 203   bool flag_at(int flag_number) {
 204     assert(flag_number < flag_limit, "oob");
 205     return (_header._struct._flags & (0x1 << flag_number)) != 0;
 206   }
 207 
 208   // Low-level support for code generation.
 209   static ByteSize header_offset() {
 210     return byte_offset_of(DataLayout, _header);
 211   }
 212   static ByteSize tag_offset() {
 213     return byte_offset_of(DataLayout, _header._struct._tag);
 214   }
 215   static ByteSize flags_offset() {
 216     return byte_offset_of(DataLayout, _header._struct._flags);
 217   }
 218   static ByteSize bci_offset() {
 219     return byte_offset_of(DataLayout, _header._struct._bci);
 220   }
 221   static ByteSize cell_offset(int index) {
 222     return byte_offset_of(DataLayout, _cells[index]);
 223   }
 224   // Return a value which, when or-ed as a byte into _flags, sets the flag.
 225   static int flag_number_to_byte_constant(int flag_number) {
 226     assert(0 <= flag_number && flag_number < flag_limit, "oob");
 227     DataLayout temp; temp.set_header(0);
 228     temp.set_flag_at(flag_number);
 229     return temp._header._struct._flags;
 230   }
 231   // Return a value which, when or-ed as a word into _header, sets the flag.
 232   static intptr_t flag_mask_to_header_mask(int byte_constant) {
 233     DataLayout temp; temp.set_header(0);
 234     temp._header._struct._flags = byte_constant;
 235     return temp._header._bits;
 236   }
 237 
 238   // GC support
 239   ProfileData* data_in();
 240   void follow_weak_refs(BoolObjectClosure* cl);
 241 };
 242 
 243 
 244 // ProfileData class hierarchy
 245 class ProfileData;
 246 class   BitData;
 247 class     CounterData;
 248 class       ReceiverTypeData;
 249 class         VirtualCallData;
 250 class       RetData;
 251 class   JumpData;
 252 class     BranchData;
 253 class   ArrayData;
 254 class     MultiBranchData;
 255 class     ArgInfoData;
 256 
 257 
 258 // ProfileData
 259 //
 260 // A ProfileData object is created to refer to a section of profiling
 261 // data in a structured way.
 262 class ProfileData : public ResourceObj {
 263 private:
 264 #ifndef PRODUCT
 265   enum {
 266     tab_width_one = 16,
 267     tab_width_two = 36
 268   };
 269 #endif // !PRODUCT
 270 
 271   // This is a pointer to a section of profiling data.
 272   DataLayout* _data;
 273 
 274 protected:
 275   DataLayout* data() { return _data; }
 276 
 277   enum {
 278     cell_size = DataLayout::cell_size
 279   };
 280 
 281 public:
 282   // How many cells are in this?
 283   virtual int cell_count() {
 284     ShouldNotReachHere();
 285     return -1;
 286   }
 287 
 288   // Return the size of this data.
 289   int size_in_bytes() {
 290     return DataLayout::compute_size_in_bytes(cell_count());
 291   }
 292 
 293 protected:
 294   // Low-level accessors for underlying data
 295   void set_intptr_at(int index, intptr_t value) {
 296     assert(0 <= index && index < cell_count(), "oob");
 297     data()->set_cell_at(index, value);
 298   }
 299   void release_set_intptr_at(int index, intptr_t value) {
 300     assert(0 <= index && index < cell_count(), "oob");
 301     data()->release_set_cell_at(index, value);
 302   }
 303   intptr_t intptr_at(int index) {
 304     assert(0 <= index && index < cell_count(), "oob");
 305     return data()->cell_at(index);
 306   }
 307   void set_uint_at(int index, uint value) {
 308     set_intptr_at(index, (intptr_t) value);
 309   }
 310   void release_set_uint_at(int index, uint value) {
 311     release_set_intptr_at(index, (intptr_t) value);
 312   }
 313   uint uint_at(int index) {
 314     return (uint)intptr_at(index);
 315   }
 316   void set_int_at(int index, int value) {
 317     set_intptr_at(index, (intptr_t) value);
 318   }
 319   void release_set_int_at(int index, int value) {
 320     release_set_intptr_at(index, (intptr_t) value);
 321   }
 322   int int_at(int index) {
 323     return (int)intptr_at(index);
 324   }
 325   int int_at_unchecked(int index) {
 326     return (int)data()->cell_at(index);
 327   }
 328   void set_oop_at(int index, oop value) {
 329     set_intptr_at(index, (intptr_t) value);
 330   }
 331   oop oop_at(int index) {
 332     return (oop)intptr_at(index);
 333   }
 334   oop* adr_oop_at(int index) {
 335     assert(0 <= index && index < cell_count(), "oob");
 336     return data()->adr_oop_at(index);
 337   }
 338 
 339   void set_flag_at(int flag_number) {
 340     data()->set_flag_at(flag_number);
 341   }
 342   bool flag_at(int flag_number) {
 343     return data()->flag_at(flag_number);
 344   }
 345 
 346   // two convenient imports for use by subclasses:
 347   static ByteSize cell_offset(int index) {
 348     return DataLayout::cell_offset(index);
 349   }
 350   static int flag_number_to_byte_constant(int flag_number) {
 351     return DataLayout::flag_number_to_byte_constant(flag_number);
 352   }
 353 
 354   ProfileData(DataLayout* data) {
 355     _data = data;
 356   }
 357 
 358 public:
 359   // Constructor for invalid ProfileData.
 360   ProfileData();
 361 
 362   u2 bci() {
 363     return data()->bci();
 364   }
 365 
 366   address dp() {
 367     return (address)_data;
 368   }
 369 
 370   int trap_state() {
 371     return data()->trap_state();
 372   }
 373   void set_trap_state(int new_state) {
 374     data()->set_trap_state(new_state);
 375   }
 376 
 377   // Type checking
 378   virtual bool is_BitData()         { return false; }
 379   virtual bool is_CounterData()     { return false; }
 380   virtual bool is_JumpData()        { return false; }
 381   virtual bool is_ReceiverTypeData(){ return false; }
 382   virtual bool is_VirtualCallData() { return false; }
 383   virtual bool is_RetData()         { return false; }
 384   virtual bool is_BranchData()      { return false; }
 385   virtual bool is_ArrayData()       { return false; }
 386   virtual bool is_MultiBranchData() { return false; }
 387   virtual bool is_ArgInfoData()     { return false; }
 388 
 389 
 390   BitData* as_BitData() {
 391     assert(is_BitData(), "wrong type");
 392     return is_BitData()         ? (BitData*)        this : NULL;
 393   }
 394   CounterData* as_CounterData() {
 395     assert(is_CounterData(), "wrong type");
 396     return is_CounterData()     ? (CounterData*)    this : NULL;
 397   }
 398   JumpData* as_JumpData() {
 399     assert(is_JumpData(), "wrong type");
 400     return is_JumpData()        ? (JumpData*)       this : NULL;
 401   }
 402   ReceiverTypeData* as_ReceiverTypeData() {
 403     assert(is_ReceiverTypeData(), "wrong type");
 404     return is_ReceiverTypeData() ? (ReceiverTypeData*)this : NULL;
 405   }
 406   VirtualCallData* as_VirtualCallData() {
 407     assert(is_VirtualCallData(), "wrong type");
 408     return is_VirtualCallData() ? (VirtualCallData*)this : NULL;
 409   }
 410   RetData* as_RetData() {
 411     assert(is_RetData(), "wrong type");
 412     return is_RetData()         ? (RetData*)        this : NULL;
 413   }
 414   BranchData* as_BranchData() {
 415     assert(is_BranchData(), "wrong type");
 416     return is_BranchData()      ? (BranchData*)     this : NULL;
 417   }
 418   ArrayData* as_ArrayData() {
 419     assert(is_ArrayData(), "wrong type");
 420     return is_ArrayData()       ? (ArrayData*)      this : NULL;
 421   }
 422   MultiBranchData* as_MultiBranchData() {
 423     assert(is_MultiBranchData(), "wrong type");
 424     return is_MultiBranchData() ? (MultiBranchData*)this : NULL;
 425   }
 426   ArgInfoData* as_ArgInfoData() {
 427     assert(is_ArgInfoData(), "wrong type");
 428     return is_ArgInfoData() ? (ArgInfoData*)this : NULL;
 429   }
 430 
 431 
 432   // Subclass specific initialization
 433   virtual void post_initialize(BytecodeStream* stream, methodDataOop mdo) {}
 434 
 435   // GC support
 436   virtual void follow_contents() {}
 437   virtual void oop_iterate(OopClosure* blk) {}
 438   virtual void oop_iterate_m(OopClosure* blk, MemRegion mr) {}
 439   virtual void adjust_pointers() {}
 440   virtual void follow_weak_refs(BoolObjectClosure* is_alive_closure) {}
 441 
 442 #ifndef SERIALGC
 443   // Parallel old support
 444   virtual void follow_contents(ParCompactionManager* cm) {}
 445   virtual void update_pointers() {}
 446   virtual void update_pointers(HeapWord* beg_addr, HeapWord* end_addr) {}
 447 #endif // SERIALGC
 448 
 449   // CI translation: ProfileData can represent both MethodDataOop data
 450   // as well as CIMethodData data. This function is provided for translating
 451   // an oop in a ProfileData to the ci equivalent. Generally speaking,
 452   // most ProfileData don't require any translation, so we provide the null
 453   // translation here, and the required translators are in the ci subclasses.
 454   virtual void translate_from(ProfileData* data) {}
 455 
 456   virtual void print_data_on(outputStream* st) {
 457     ShouldNotReachHere();
 458   }
 459 
 460 #ifndef PRODUCT
 461   void print_shared(outputStream* st, const char* name);
 462   void tab(outputStream* st);
 463 #endif
 464 };
 465 
 466 // BitData
 467 //
 468 // A BitData holds a flag or two in its header.
 469 class BitData : public ProfileData {
 470 protected:
 471   enum {
 472     // null_seen:
 473     //  saw a null operand (cast/aastore/instanceof)
 474     null_seen_flag              = DataLayout::first_flag + 0
 475   };
 476   enum { bit_cell_count = 0 };  // no additional data fields needed.
 477 public:
 478   BitData(DataLayout* layout) : ProfileData(layout) {
 479   }
 480 
 481   virtual bool is_BitData() { return true; }
 482 
 483   static int static_cell_count() {
 484     return bit_cell_count;
 485   }
 486 
 487   virtual int cell_count() {
 488     return static_cell_count();
 489   }
 490 
 491   // Accessor
 492 
 493   // The null_seen flag bit is specially known to the interpreter.
 494   // Consulting it allows the compiler to avoid setting up null_check traps.
 495   bool null_seen()     { return flag_at(null_seen_flag); }
 496   void set_null_seen()    { set_flag_at(null_seen_flag); }
 497 
 498 
 499   // Code generation support
 500   static int null_seen_byte_constant() {
 501     return flag_number_to_byte_constant(null_seen_flag);
 502   }
 503 
 504   static ByteSize bit_data_size() {
 505     return cell_offset(bit_cell_count);
 506   }
 507 
 508 #ifndef PRODUCT
 509   void print_data_on(outputStream* st);
 510 #endif
 511 };
 512 
 513 // CounterData
 514 //
 515 // A CounterData corresponds to a simple counter.
 516 class CounterData : public BitData {
 517 protected:
 518   enum {
 519     count_off,
 520     counter_cell_count
 521   };
 522 public:
 523   CounterData(DataLayout* layout) : BitData(layout) {}
 524 
 525   virtual bool is_CounterData() { return true; }
 526 
 527   static int static_cell_count() {
 528     return counter_cell_count;
 529   }
 530 
 531   virtual int cell_count() {
 532     return static_cell_count();
 533   }
 534 
 535   // Direct accessor
 536   uint count() {
 537     return uint_at(count_off);
 538   }
 539 
 540   // Code generation support
 541   static ByteSize count_offset() {
 542     return cell_offset(count_off);
 543   }
 544   static ByteSize counter_data_size() {
 545     return cell_offset(counter_cell_count);
 546   }
 547 
 548   void set_count(uint count) {
 549     set_uint_at(count_off, count);
 550   }
 551 
 552 #ifndef PRODUCT
 553   void print_data_on(outputStream* st);
 554 #endif
 555 };
 556 
 557 // JumpData
 558 //
 559 // A JumpData is used to access profiling information for a direct
 560 // branch.  It is a counter, used for counting the number of branches,
 561 // plus a data displacement, used for realigning the data pointer to
 562 // the corresponding target bci.
 563 class JumpData : public ProfileData {
 564 protected:
 565   enum {
 566     taken_off_set,
 567     displacement_off_set,
 568     jump_cell_count
 569   };
 570 
 571   void set_displacement(int displacement) {
 572     set_int_at(displacement_off_set, displacement);
 573   }
 574 
 575 public:
 576   JumpData(DataLayout* layout) : ProfileData(layout) {
 577     assert(layout->tag() == DataLayout::jump_data_tag ||
 578       layout->tag() == DataLayout::branch_data_tag, "wrong type");
 579   }
 580 
 581   virtual bool is_JumpData() { return true; }
 582 
 583   static int static_cell_count() {
 584     return jump_cell_count;
 585   }
 586 
 587   virtual int cell_count() {
 588     return static_cell_count();
 589   }
 590 
 591   // Direct accessor
 592   uint taken() {
 593     return uint_at(taken_off_set);
 594   }
 595   // Saturating counter
 596   uint inc_taken() {
 597     uint cnt = taken() + 1;
 598     // Did we wrap? Will compiler screw us??
 599     if (cnt == 0) cnt--;
 600     set_uint_at(taken_off_set, cnt);
 601     return cnt;
 602   }
 603 
 604   int displacement() {
 605     return int_at(displacement_off_set);
 606   }
 607 
 608   // Code generation support
 609   static ByteSize taken_offset() {
 610     return cell_offset(taken_off_set);
 611   }
 612 
 613   static ByteSize displacement_offset() {
 614     return cell_offset(displacement_off_set);
 615   }
 616 
 617   // Specific initialization.
 618   void post_initialize(BytecodeStream* stream, methodDataOop mdo);
 619 
 620 #ifndef PRODUCT
 621   void print_data_on(outputStream* st);
 622 #endif
 623 };
 624 
 625 // ReceiverTypeData
 626 //
 627 // A ReceiverTypeData is used to access profiling information about a
 628 // dynamic type check.  It consists of a counter which counts the total times
 629 // that the check is reached, and a series of (klassOop, count) pairs
 630 // which are used to store a type profile for the receiver of the check.
 631 class ReceiverTypeData : public CounterData {
 632 protected:
 633   enum {
 634     receiver0_offset = counter_cell_count,
 635     count0_offset,
 636     receiver_type_row_cell_count = (count0_offset + 1) - receiver0_offset
 637   };
 638 
 639 public:
 640   ReceiverTypeData(DataLayout* layout) : CounterData(layout) {
 641     assert(layout->tag() == DataLayout::receiver_type_data_tag ||
 642            layout->tag() == DataLayout::virtual_call_data_tag, "wrong type");
 643   }
 644 
 645   virtual bool is_ReceiverTypeData() { return true; }
 646 
 647   static int static_cell_count() {
 648     return counter_cell_count + (uint) TypeProfileWidth * receiver_type_row_cell_count;
 649   }
 650 
 651   virtual int cell_count() {
 652     return static_cell_count();
 653   }
 654 
 655   // Direct accessors
 656   static uint row_limit() {
 657     return TypeProfileWidth;
 658   }
 659   static int receiver_cell_index(uint row) {
 660     return receiver0_offset + row * receiver_type_row_cell_count;
 661   }
 662   static int receiver_count_cell_index(uint row) {
 663     return count0_offset + row * receiver_type_row_cell_count;
 664   }
 665 
 666   // Get the receiver at row.  The 'unchecked' version is needed by parallel old
 667   // gc; it does not assert the receiver is a klass.  During compaction of the
 668   // perm gen, the klass may already have moved, so the is_klass() predicate
 669   // would fail.  The 'normal' version should be used whenever possible.
 670   klassOop receiver_unchecked(uint row) {
 671     assert(row < row_limit(), "oob");
 672     oop recv = oop_at(receiver_cell_index(row));
 673     return (klassOop)recv;
 674   }
 675 
 676   klassOop receiver(uint row) {
 677     klassOop recv = receiver_unchecked(row);
 678     assert(recv == NULL || ((oop)recv)->is_klass(), "wrong type");
 679     return recv;
 680   }
 681 
 682   void set_receiver(uint row, oop p) {
 683     assert((uint)row < row_limit(), "oob");
 684     set_oop_at(receiver_cell_index(row), p);
 685   }
 686 
 687   uint receiver_count(uint row) {
 688     assert(row < row_limit(), "oob");
 689     return uint_at(receiver_count_cell_index(row));
 690   }
 691 
 692   void set_receiver_count(uint row, uint count) {
 693     assert(row < row_limit(), "oob");
 694     set_uint_at(receiver_count_cell_index(row), count);
 695   }
 696 
 697   void clear_row(uint row) {
 698     assert(row < row_limit(), "oob");
 699     // Clear total count - indicator of polymorphic call site.
 700     // The site may look like as monomorphic after that but
 701     // it allow to have more accurate profiling information because
 702     // there was execution phase change since klasses were unloaded.
 703     // If the site is still polymorphic then MDO will be updated
 704     // to reflect it. But it could be the case that the site becomes
 705     // only bimorphic. Then keeping total count not 0 will be wrong.
 706     // Even if we use monomorphic (when it is not) for compilation
 707     // we will only have trap, deoptimization and recompile again
 708     // with updated MDO after executing method in Interpreter.
 709     // An additional receiver will be recorded in the cleaned row
 710     // during next call execution.
 711     //
 712     // Note: our profiling logic works with empty rows in any slot.
 713     // We do sorting a profiling info (ciCallProfile) for compilation.
 714     //
 715     set_count(0);
 716     set_receiver(row, NULL);
 717     set_receiver_count(row, 0);
 718   }
 719 
 720   // Code generation support
 721   static ByteSize receiver_offset(uint row) {
 722     return cell_offset(receiver_cell_index(row));
 723   }
 724   static ByteSize receiver_count_offset(uint row) {
 725     return cell_offset(receiver_count_cell_index(row));
 726   }
 727   static ByteSize receiver_type_data_size() {
 728     return cell_offset(static_cell_count());
 729   }
 730 
 731   // GC support
 732   virtual void follow_contents();
 733   virtual void oop_iterate(OopClosure* blk);
 734   virtual void oop_iterate_m(OopClosure* blk, MemRegion mr);
 735   virtual void adjust_pointers();
 736   virtual void follow_weak_refs(BoolObjectClosure* is_alive_closure);
 737 
 738 #ifndef SERIALGC
 739   // Parallel old support
 740   virtual void follow_contents(ParCompactionManager* cm);
 741   virtual void update_pointers();
 742   virtual void update_pointers(HeapWord* beg_addr, HeapWord* end_addr);
 743 #endif // SERIALGC
 744 
 745   oop* adr_receiver(uint row) {
 746     return adr_oop_at(receiver_cell_index(row));
 747   }
 748 
 749 #ifndef PRODUCT
 750   void print_receiver_data_on(outputStream* st);
 751   void print_data_on(outputStream* st);
 752 #endif
 753 };
 754 
 755 // VirtualCallData
 756 //
 757 // A VirtualCallData is used to access profiling information about a
 758 // virtual call.  For now, it has nothing more than a ReceiverTypeData.
 759 class VirtualCallData : public ReceiverTypeData {
 760 public:
 761   VirtualCallData(DataLayout* layout) : ReceiverTypeData(layout) {
 762     assert(layout->tag() == DataLayout::virtual_call_data_tag, "wrong type");
 763   }
 764 
 765   virtual bool is_VirtualCallData() { return true; }
 766 
 767   static int static_cell_count() {
 768     // At this point we could add more profile state, e.g., for arguments.
 769     // But for now it's the same size as the base record type.
 770     return ReceiverTypeData::static_cell_count();
 771   }
 772 
 773   virtual int cell_count() {
 774     return static_cell_count();
 775   }
 776 
 777   // Direct accessors
 778   static ByteSize virtual_call_data_size() {
 779     return cell_offset(static_cell_count());
 780   }
 781 
 782 #ifndef PRODUCT
 783   void print_data_on(outputStream* st);
 784 #endif
 785 };
 786 
 787 // RetData
 788 //
 789 // A RetData is used to access profiling information for a ret bytecode.
 790 // It is composed of a count of the number of times that the ret has
 791 // been executed, followed by a series of triples of the form
 792 // (bci, count, di) which count the number of times that some bci was the
 793 // target of the ret and cache a corresponding data displacement.
 794 class RetData : public CounterData {
 795 protected:
 796   enum {
 797     bci0_offset = counter_cell_count,
 798     count0_offset,
 799     displacement0_offset,
 800     ret_row_cell_count = (displacement0_offset + 1) - bci0_offset
 801   };
 802 
 803   void set_bci(uint row, int bci) {
 804     assert((uint)row < row_limit(), "oob");
 805     set_int_at(bci0_offset + row * ret_row_cell_count, bci);
 806   }
 807   void release_set_bci(uint row, int bci) {
 808     assert((uint)row < row_limit(), "oob");
 809     // 'release' when setting the bci acts as a valid flag for other
 810     // threads wrt bci_count and bci_displacement.
 811     release_set_int_at(bci0_offset + row * ret_row_cell_count, bci);
 812   }
 813   void set_bci_count(uint row, uint count) {
 814     assert((uint)row < row_limit(), "oob");
 815     set_uint_at(count0_offset + row * ret_row_cell_count, count);
 816   }
 817   void set_bci_displacement(uint row, int disp) {
 818     set_int_at(displacement0_offset + row * ret_row_cell_count, disp);
 819   }
 820 
 821 public:
 822   RetData(DataLayout* layout) : CounterData(layout) {
 823     assert(layout->tag() == DataLayout::ret_data_tag, "wrong type");
 824   }
 825 
 826   virtual bool is_RetData() { return true; }
 827 
 828   enum {
 829     no_bci = -1 // value of bci when bci1/2 are not in use.
 830   };
 831 
 832   static int static_cell_count() {
 833     return counter_cell_count + (uint) BciProfileWidth * ret_row_cell_count;
 834   }
 835 
 836   virtual int cell_count() {
 837     return static_cell_count();
 838   }
 839 
 840   static uint row_limit() {
 841     return BciProfileWidth;
 842   }
 843   static int bci_cell_index(uint row) {
 844     return bci0_offset + row * ret_row_cell_count;
 845   }
 846   static int bci_count_cell_index(uint row) {
 847     return count0_offset + row * ret_row_cell_count;
 848   }
 849   static int bci_displacement_cell_index(uint row) {
 850     return displacement0_offset + row * ret_row_cell_count;
 851   }
 852 
 853   // Direct accessors
 854   int bci(uint row) {
 855     return int_at(bci_cell_index(row));
 856   }
 857   uint bci_count(uint row) {
 858     return uint_at(bci_count_cell_index(row));
 859   }
 860   int bci_displacement(uint row) {
 861     return int_at(bci_displacement_cell_index(row));
 862   }
 863 
 864   // Interpreter Runtime support
 865   address fixup_ret(int return_bci, methodDataHandle mdo);
 866 
 867   // Code generation support
 868   static ByteSize bci_offset(uint row) {
 869     return cell_offset(bci_cell_index(row));
 870   }
 871   static ByteSize bci_count_offset(uint row) {
 872     return cell_offset(bci_count_cell_index(row));
 873   }
 874   static ByteSize bci_displacement_offset(uint row) {
 875     return cell_offset(bci_displacement_cell_index(row));
 876   }
 877 
 878   // Specific initialization.
 879   void post_initialize(BytecodeStream* stream, methodDataOop mdo);
 880 
 881 #ifndef PRODUCT
 882   void print_data_on(outputStream* st);
 883 #endif
 884 };
 885 
 886 // BranchData
 887 //
 888 // A BranchData is used to access profiling data for a two-way branch.
 889 // It consists of taken and not_taken counts as well as a data displacement
 890 // for the taken case.
 891 class BranchData : public JumpData {
 892 protected:
 893   enum {
 894     not_taken_off_set = jump_cell_count,
 895     branch_cell_count
 896   };
 897 
 898   void set_displacement(int displacement) {
 899     set_int_at(displacement_off_set, displacement);
 900   }
 901 
 902 public:
 903   BranchData(DataLayout* layout) : JumpData(layout) {
 904     assert(layout->tag() == DataLayout::branch_data_tag, "wrong type");
 905   }
 906 
 907   virtual bool is_BranchData() { return true; }
 908 
 909   static int static_cell_count() {
 910     return branch_cell_count;
 911   }
 912 
 913   virtual int cell_count() {
 914     return static_cell_count();
 915   }
 916 
 917   // Direct accessor
 918   uint not_taken() {
 919     return uint_at(not_taken_off_set);
 920   }
 921 
 922   uint inc_not_taken() {
 923     uint cnt = not_taken() + 1;
 924     // Did we wrap? Will compiler screw us??
 925     if (cnt == 0) cnt--;
 926     set_uint_at(not_taken_off_set, cnt);
 927     return cnt;
 928   }
 929 
 930   // Code generation support
 931   static ByteSize not_taken_offset() {
 932     return cell_offset(not_taken_off_set);
 933   }
 934   static ByteSize branch_data_size() {
 935     return cell_offset(branch_cell_count);
 936   }
 937 
 938   // Specific initialization.
 939   void post_initialize(BytecodeStream* stream, methodDataOop mdo);
 940 
 941 #ifndef PRODUCT
 942   void print_data_on(outputStream* st);
 943 #endif
 944 };
 945 
 946 // ArrayData
 947 //
 948 // A ArrayData is a base class for accessing profiling data which does
 949 // not have a statically known size.  It consists of an array length
 950 // and an array start.
 951 class ArrayData : public ProfileData {
 952 protected:
 953   friend class DataLayout;
 954 
 955   enum {
 956     array_len_off_set,
 957     array_start_off_set
 958   };
 959 
 960   uint array_uint_at(int index) {
 961     int aindex = index + array_start_off_set;
 962     return uint_at(aindex);
 963   }
 964   int array_int_at(int index) {
 965     int aindex = index + array_start_off_set;
 966     return int_at(aindex);
 967   }
 968   oop array_oop_at(int index) {
 969     int aindex = index + array_start_off_set;
 970     return oop_at(aindex);
 971   }
 972   void array_set_int_at(int index, int value) {
 973     int aindex = index + array_start_off_set;
 974     set_int_at(aindex, value);
 975   }
 976 
 977   // Code generation support for subclasses.
 978   static ByteSize array_element_offset(int index) {
 979     return cell_offset(array_start_off_set + index);
 980   }
 981 
 982 public:
 983   ArrayData(DataLayout* layout) : ProfileData(layout) {}
 984 
 985   virtual bool is_ArrayData() { return true; }
 986 
 987   static int static_cell_count() {
 988     return -1;
 989   }
 990 
 991   int array_len() {
 992     return int_at_unchecked(array_len_off_set);
 993   }
 994 
 995   virtual int cell_count() {
 996     return array_len() + 1;
 997   }
 998 
 999   // Code generation support
1000   static ByteSize array_len_offset() {
1001     return cell_offset(array_len_off_set);
1002   }
1003   static ByteSize array_start_offset() {
1004     return cell_offset(array_start_off_set);
1005   }
1006 };
1007 
1008 // MultiBranchData
1009 //
1010 // A MultiBranchData is used to access profiling information for
1011 // a multi-way branch (*switch bytecodes).  It consists of a series
1012 // of (count, displacement) pairs, which count the number of times each
1013 // case was taken and specify the data displacment for each branch target.
1014 class MultiBranchData : public ArrayData {
1015 protected:
1016   enum {
1017     default_count_off_set,
1018     default_disaplacement_off_set,
1019     case_array_start
1020   };
1021   enum {
1022     relative_count_off_set,
1023     relative_displacement_off_set,
1024     per_case_cell_count
1025   };
1026 
1027   void set_default_displacement(int displacement) {
1028     array_set_int_at(default_disaplacement_off_set, displacement);
1029   }
1030   void set_displacement_at(int index, int displacement) {
1031     array_set_int_at(case_array_start +
1032                      index * per_case_cell_count +
1033                      relative_displacement_off_set,
1034                      displacement);
1035   }
1036 
1037 public:
1038   MultiBranchData(DataLayout* layout) : ArrayData(layout) {
1039     assert(layout->tag() == DataLayout::multi_branch_data_tag, "wrong type");
1040   }
1041 
1042   virtual bool is_MultiBranchData() { return true; }
1043 
1044   static int compute_cell_count(BytecodeStream* stream);
1045 
1046   int number_of_cases() {
1047     int alen = array_len() - 2; // get rid of default case here.
1048     assert(alen % per_case_cell_count == 0, "must be even");
1049     return (alen / per_case_cell_count);
1050   }
1051 
1052   uint default_count() {
1053     return array_uint_at(default_count_off_set);
1054   }
1055   int default_displacement() {
1056     return array_int_at(default_disaplacement_off_set);
1057   }
1058 
1059   uint count_at(int index) {
1060     return array_uint_at(case_array_start +
1061                          index * per_case_cell_count +
1062                          relative_count_off_set);
1063   }
1064   int displacement_at(int index) {
1065     return array_int_at(case_array_start +
1066                         index * per_case_cell_count +
1067                         relative_displacement_off_set);
1068   }
1069 
1070   // Code generation support
1071   static ByteSize default_count_offset() {
1072     return array_element_offset(default_count_off_set);
1073   }
1074   static ByteSize default_displacement_offset() {
1075     return array_element_offset(default_disaplacement_off_set);
1076   }
1077   static ByteSize case_count_offset(int index) {
1078     return case_array_offset() +
1079            (per_case_size() * index) +
1080            relative_count_offset();
1081   }
1082   static ByteSize case_array_offset() {
1083     return array_element_offset(case_array_start);
1084   }
1085   static ByteSize per_case_size() {
1086     return in_ByteSize(per_case_cell_count) * cell_size;
1087   }
1088   static ByteSize relative_count_offset() {
1089     return in_ByteSize(relative_count_off_set) * cell_size;
1090   }
1091   static ByteSize relative_displacement_offset() {
1092     return in_ByteSize(relative_displacement_off_set) * cell_size;
1093   }
1094 
1095   // Specific initialization.
1096   void post_initialize(BytecodeStream* stream, methodDataOop mdo);
1097 
1098 #ifndef PRODUCT
1099   void print_data_on(outputStream* st);
1100 #endif
1101 };
1102 
1103 class ArgInfoData : public ArrayData {
1104 
1105 public:
1106   ArgInfoData(DataLayout* layout) : ArrayData(layout) {
1107     assert(layout->tag() == DataLayout::arg_info_data_tag, "wrong type");
1108   }
1109 
1110   virtual bool is_ArgInfoData() { return true; }
1111 
1112 
1113   int number_of_args() {
1114     return array_len();
1115   }
1116 
1117   uint arg_modified(int arg) {
1118     return array_uint_at(arg);
1119   }
1120 
1121   void set_arg_modified(int arg, uint val) {
1122     array_set_int_at(arg, val);
1123   }
1124 
1125 #ifndef PRODUCT
1126   void print_data_on(outputStream* st);
1127 #endif
1128 };
1129 
1130 // methodDataOop
1131 //
1132 // A methodDataOop holds information which has been collected about
1133 // a method.  Its layout looks like this:
1134 //
1135 // -----------------------------
1136 // | header                    |
1137 // | klass                     |
1138 // -----------------------------
1139 // | method                    |
1140 // | size of the methodDataOop |
1141 // -----------------------------
1142 // | Data entries...           |
1143 // |   (variable size)         |
1144 // |                           |
1145 // .                           .
1146 // .                           .
1147 // .                           .
1148 // |                           |
1149 // -----------------------------
1150 //
1151 // The data entry area is a heterogeneous array of DataLayouts. Each
1152 // DataLayout in the array corresponds to a specific bytecode in the
1153 // method.  The entries in the array are sorted by the corresponding
1154 // bytecode.  Access to the data is via resource-allocated ProfileData,
1155 // which point to the underlying blocks of DataLayout structures.
1156 //
1157 // During interpretation, if profiling in enabled, the interpreter
1158 // maintains a method data pointer (mdp), which points at the entry
1159 // in the array corresponding to the current bci.  In the course of
1160 // intepretation, when a bytecode is encountered that has profile data
1161 // associated with it, the entry pointed to by mdp is updated, then the
1162 // mdp is adjusted to point to the next appropriate DataLayout.  If mdp
1163 // is NULL to begin with, the interpreter assumes that the current method
1164 // is not (yet) being profiled.
1165 //
1166 // In methodDataOop parlance, "dp" is a "data pointer", the actual address
1167 // of a DataLayout element.  A "di" is a "data index", the offset in bytes
1168 // from the base of the data entry array.  A "displacement" is the byte offset
1169 // in certain ProfileData objects that indicate the amount the mdp must be
1170 // adjusted in the event of a change in control flow.
1171 //
1172 
1173 class methodDataOopDesc : public oopDesc {
1174   friend class VMStructs;
1175 private:
1176   friend class ProfileData;
1177 
1178   // Back pointer to the methodOop
1179   methodOop _method;
1180 
1181   // Size of this oop in bytes
1182   int _size;
1183 
1184   // Cached hint for bci_to_dp and bci_to_data
1185   int _hint_di;
1186 
1187   // Whole-method sticky bits and flags
1188 public:
1189   enum {
1190     _trap_hist_limit    = 16,   // decoupled from Deoptimization::Reason_LIMIT
1191     _trap_hist_mask     = max_jubyte,
1192     _extra_data_count   = 4     // extra DataLayout headers, for trap history
1193   }; // Public flag values
1194 private:
1195   uint _nof_decompiles;             // count of all nmethod removals
1196   uint _nof_overflow_recompiles;    // recompile count, excluding recomp. bits
1197   uint _nof_overflow_traps;         // trap count, excluding _trap_hist
1198   union {
1199     intptr_t _align;
1200     u1 _array[_trap_hist_limit];
1201   } _trap_hist;
1202 
1203   // Support for interprocedural escape analysis, from Thomas Kotzmann.
1204   intx              _eflags;          // flags on escape information
1205   intx              _arg_local;       // bit set of non-escaping arguments
1206   intx              _arg_stack;       // bit set of stack-allocatable arguments
1207   intx              _arg_returned;    // bit set of returned arguments
1208 
1209   int _creation_mileage;              // method mileage at MDO creation
1210 
1211   // How many invocations has this MDO seen?
1212   // These counters are used to determine the exact age of MDO.
1213   // We need those because in tiered a method can be concurrently
1214   // executed at different levels.
1215   InvocationCounter _invocation_counter;
1216   // Same for backedges.
1217   InvocationCounter _backedge_counter;
1218   // Number of loops and blocks is computed when compiling the first
1219   // time with C1. It is used to determine if method is trivial.
1220   short             _num_loops;
1221   short             _num_blocks;
1222   // Highest compile level this method has ever seen.
1223   u1                _highest_comp_level;
1224   // Same for OSR level
1225   u1                _highest_osr_comp_level;
1226   // Does this method contain anything worth profiling?
1227   bool              _would_profile;
1228 
1229   // Size of _data array in bytes.  (Excludes header and extra_data fields.)
1230   int _data_size;
1231 
1232   // Beginning of the data entries
1233   intptr_t _data[1];
1234 
1235   // Helper for size computation
1236   static int compute_data_size(BytecodeStream* stream);
1237   static int bytecode_cell_count(Bytecodes::Code code);
1238   enum { no_profile_data = -1, variable_cell_count = -2 };
1239 
1240   // Helper for initialization
1241   DataLayout* data_layout_at(int data_index) {
1242     assert(data_index % sizeof(intptr_t) == 0, "unaligned");
1243     return (DataLayout*) (((address)_data) + data_index);
1244   }
1245 
1246   // Initialize an individual data segment.  Returns the size of
1247   // the segment in bytes.
1248   int initialize_data(BytecodeStream* stream, int data_index);
1249 
1250   // Helper for data_at
1251   DataLayout* limit_data_position() {
1252     return (DataLayout*)((address)data_base() + _data_size);
1253   }
1254   bool out_of_bounds(int data_index) {
1255     return data_index >= data_size();
1256   }
1257 
1258   // Give each of the data entries a chance to perform specific
1259   // data initialization.
1260   void post_initialize(BytecodeStream* stream);
1261 
1262   // hint accessors
1263   int      hint_di() const  { return _hint_di; }
1264   void set_hint_di(int di)  {
1265     assert(!out_of_bounds(di), "hint_di out of bounds");
1266     _hint_di = di;
1267   }
1268   ProfileData* data_before(int bci) {
1269     // avoid SEGV on this edge case
1270     if (data_size() == 0)
1271       return NULL;
1272     int hint = hint_di();
1273     if (data_layout_at(hint)->bci() <= bci)
1274       return data_at(hint);
1275     return first_data();
1276   }
1277 
1278   // What is the index of the first data entry?
1279   int first_di() { return 0; }
1280 
1281   // Find or create an extra ProfileData:
1282   ProfileData* bci_to_extra_data(int bci, bool create_if_missing);
1283 
1284   // return the argument info cell
1285   ArgInfoData *arg_info();
1286 
1287 public:
1288   static int header_size() {
1289     return sizeof(methodDataOopDesc)/wordSize;
1290   }
1291 
1292   // Compute the size of a methodDataOop before it is created.
1293   static int compute_allocation_size_in_bytes(methodHandle method);
1294   static int compute_allocation_size_in_words(methodHandle method);
1295   static int compute_extra_data_count(int data_size, int empty_bc_count);
1296 
1297   // Determine if a given bytecode can have profile information.
1298   static bool bytecode_has_profile(Bytecodes::Code code) {
1299     return bytecode_cell_count(code) != no_profile_data;
1300   }
1301 
1302   // Perform initialization of a new methodDataOop
1303   void initialize(methodHandle method);
1304 
1305   // My size
1306   int object_size_in_bytes() { return _size; }
1307   int object_size() {
1308     return align_object_size(align_size_up(_size, BytesPerWord)/BytesPerWord);
1309   }
1310 
1311   int      creation_mileage() const  { return _creation_mileage; }
1312   void set_creation_mileage(int x)   { _creation_mileage = x; }
1313 
1314   int invocation_count() {
1315     if (invocation_counter()->carry()) {
1316       return InvocationCounter::count_limit;
1317     }
1318     return invocation_counter()->count();
1319   }
1320   int backedge_count() {
1321     if (backedge_counter()->carry()) {
1322       return InvocationCounter::count_limit;
1323     }
1324     return backedge_counter()->count();
1325   }
1326 
1327   InvocationCounter* invocation_counter()     { return &_invocation_counter; }
1328   InvocationCounter* backedge_counter()       { return &_backedge_counter;   }
1329 
1330   void set_would_profile(bool p)              { _would_profile = p;    }
1331   bool would_profile() const                  { return _would_profile; }
1332 
1333   int highest_comp_level()                    { return _highest_comp_level;      }
1334   void set_highest_comp_level(int level)      { _highest_comp_level = level;     }
1335   int highest_osr_comp_level()                { return _highest_osr_comp_level;  }
1336   void set_highest_osr_comp_level(int level)  { _highest_osr_comp_level = level; }
1337 
1338   int num_loops() const                       { return _num_loops;  }
1339   void set_num_loops(int n)                   { _num_loops = n;     }
1340   int num_blocks() const                      { return _num_blocks; }
1341   void set_num_blocks(int n)                  { _num_blocks = n;    }
1342 
1343   bool is_mature() const;  // consult mileage and ProfileMaturityPercentage
1344   static int mileage_of(methodOop m);
1345 
1346   // Support for interprocedural escape analysis, from Thomas Kotzmann.
1347   enum EscapeFlag {
1348     estimated    = 1 << 0,
1349     return_local = 1 << 1,
1350     return_allocated = 1 << 2,
1351     allocated_escapes = 1 << 3,
1352     unknown_modified = 1 << 4
1353   };
1354 
1355   intx eflags()                                  { return _eflags; }
1356   intx arg_local()                               { return _arg_local; }
1357   intx arg_stack()                               { return _arg_stack; }
1358   intx arg_returned()                            { return _arg_returned; }
1359   uint arg_modified(int a)                       { ArgInfoData *aid = arg_info();
1360                                                    assert(a >= 0 && a < aid->number_of_args(), "valid argument number");
1361                                                    return aid->arg_modified(a); }
1362 
1363   void set_eflags(intx v)                        { _eflags = v; }
1364   void set_arg_local(intx v)                     { _arg_local = v; }
1365   void set_arg_stack(intx v)                     { _arg_stack = v; }
1366   void set_arg_returned(intx v)                  { _arg_returned = v; }
1367   void set_arg_modified(int a, uint v)           { ArgInfoData *aid = arg_info();
1368                                                    assert(a >= 0 && a < aid->number_of_args(), "valid argument number");
1369 
1370                                                    aid->set_arg_modified(a, v); }
1371 
1372   void clear_escape_info()                       { _eflags = _arg_local = _arg_stack = _arg_returned = 0; }
1373 
1374   // Location and size of data area
1375   address data_base() const {
1376     return (address) _data;
1377   }
1378   int data_size() {
1379     return _data_size;
1380   }
1381 
1382   // Accessors
1383   methodOop method() { return _method; }
1384 
1385   // Get the data at an arbitrary (sort of) data index.
1386   ProfileData* data_at(int data_index);
1387 
1388   // Walk through the data in order.
1389   ProfileData* first_data() { return data_at(first_di()); }
1390   ProfileData* next_data(ProfileData* current);
1391   bool is_valid(ProfileData* current) { return current != NULL; }
1392 
1393   // Convert a dp (data pointer) to a di (data index).
1394   int dp_to_di(address dp) {
1395     return dp - ((address)_data);
1396   }
1397 
1398   address di_to_dp(int di) {
1399     return (address)data_layout_at(di);
1400   }
1401 
1402   // bci to di/dp conversion.
1403   address bci_to_dp(int bci);
1404   int bci_to_di(int bci) {
1405     return dp_to_di(bci_to_dp(bci));
1406   }
1407 
1408   // Get the data at an arbitrary bci, or NULL if there is none.
1409   ProfileData* bci_to_data(int bci);
1410 
1411   // Same, but try to create an extra_data record if one is needed:
1412   ProfileData* allocate_bci_to_data(int bci) {
1413     ProfileData* data = bci_to_data(bci);
1414     return (data != NULL) ? data : bci_to_extra_data(bci, true);
1415   }
1416 
1417   // Add a handful of extra data records, for trap tracking.
1418   DataLayout* extra_data_base() { return limit_data_position(); }
1419   DataLayout* extra_data_limit() { return (DataLayout*)((address)this + object_size_in_bytes()); }
1420   int extra_data_size() { return (address)extra_data_limit()
1421                                - (address)extra_data_base(); }
1422   static DataLayout* next_extra(DataLayout* dp) { return (DataLayout*)((address)dp + in_bytes(DataLayout::cell_offset(0))); }
1423 
1424   // Return (uint)-1 for overflow.
1425   uint trap_count(int reason) const {
1426     assert((uint)reason < _trap_hist_limit, "oob");
1427     return (int)((_trap_hist._array[reason]+1) & _trap_hist_mask) - 1;
1428   }
1429   // For loops:
1430   static uint trap_reason_limit() { return _trap_hist_limit; }
1431   static uint trap_count_limit()  { return _trap_hist_mask; }
1432   uint inc_trap_count(int reason) {
1433     // Count another trap, anywhere in this method.
1434     assert(reason >= 0, "must be single trap");
1435     if ((uint)reason < _trap_hist_limit) {
1436       uint cnt1 = 1 + _trap_hist._array[reason];
1437       if ((cnt1 & _trap_hist_mask) != 0) {  // if no counter overflow...
1438         _trap_hist._array[reason] = cnt1;
1439         return cnt1;
1440       } else {
1441         return _trap_hist_mask + (++_nof_overflow_traps);
1442       }
1443     } else {
1444       // Could not represent the count in the histogram.
1445       return (++_nof_overflow_traps);
1446     }
1447   }
1448 
1449   uint overflow_trap_count() const {
1450     return _nof_overflow_traps;
1451   }
1452   uint overflow_recompile_count() const {
1453     return _nof_overflow_recompiles;
1454   }
1455   void inc_overflow_recompile_count() {
1456     _nof_overflow_recompiles += 1;
1457   }
1458   uint decompile_count() const {
1459     return _nof_decompiles;
1460   }
1461   void inc_decompile_count() {
1462     _nof_decompiles += 1;
1463     if (decompile_count() > (uint)PerMethodRecompilationCutoff) {
1464       method()->set_not_compilable(CompLevel_full_optimization);
1465     }
1466   }
1467 
1468   // Support for code generation
1469   static ByteSize data_offset() {
1470     return byte_offset_of(methodDataOopDesc, _data[0]);
1471   }
1472 
1473   static ByteSize invocation_counter_offset() {
1474     return byte_offset_of(methodDataOopDesc, _invocation_counter);
1475   }
1476   static ByteSize backedge_counter_offset() {
1477     return byte_offset_of(methodDataOopDesc, _backedge_counter);
1478   }
1479 
1480   // GC support
1481   oop* adr_method() const { return (oop*)&_method; }
1482   bool object_is_parsable() const { return _size != 0; }
1483   void set_object_is_parsable(int object_size_in_bytes) { _size = object_size_in_bytes; }
1484 
1485 #ifndef PRODUCT
1486   // printing support for method data
1487   void print_data_on(outputStream* st);
1488 #endif
1489 
1490   // verification
1491   void verify_data_on(outputStream* st);
1492 };