1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "ci/ciValueKlass.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "compiler/compileLog.hpp"
  29 #include "oops/objArrayKlass.hpp"
  30 #include "oops/valueArrayKlass.hpp"
  31 #include "opto/addnode.hpp"
  32 #include "opto/memnode.hpp"
  33 #include "opto/mulnode.hpp"
  34 #include "opto/parse.hpp"
  35 #include "opto/rootnode.hpp"
  36 #include "opto/runtime.hpp"
  37 #include "opto/valuetypenode.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 
  40 //------------------------------make_dtrace_method_entry_exit ----------------
  41 // Dtrace -- record entry or exit of a method if compiled with dtrace support
  42 void GraphKit::make_dtrace_method_entry_exit(ciMethod* method, bool is_entry) {
  43   const TypeFunc *call_type    = OptoRuntime::dtrace_method_entry_exit_Type();
  44   address         call_address = is_entry ? CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_entry) :
  45                                             CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_method_exit);
  46   const char     *call_name    = is_entry ? "dtrace_method_entry" : "dtrace_method_exit";
  47 
  48   // Get base of thread-local storage area
  49   Node* thread = _gvn.transform( new ThreadLocalNode() );
  50 
  51   // Get method
  52   const TypePtr* method_type = TypeMetadataPtr::make(method);
  53   Node *method_node = _gvn.transform(ConNode::make(method_type));
  54 
  55   kill_dead_locals();
  56 
  57   // For some reason, this call reads only raw memory.
  58   const TypePtr* raw_adr_type = TypeRawPtr::BOTTOM;
  59   make_runtime_call(RC_LEAF | RC_NARROW_MEM,
  60                     call_type, call_address,
  61                     call_name, raw_adr_type,
  62                     thread, method_node);
  63 }
  64 
  65 
  66 //=============================================================================
  67 //------------------------------do_checkcast-----------------------------------
  68 void Parse::do_checkcast() {
  69   bool will_link;
  70   ciKlass* klass = iter().get_klass(will_link);
  71 
  72   Node *obj = peek();
  73 
  74   // Throw uncommon trap if class is not loaded or the value we are casting
  75   // _from_ is not loaded, and value is not null.  If the value _is_ NULL,
  76   // then the checkcast does nothing.
  77   const TypeOopPtr *tp = _gvn.type(obj)->isa_oopptr();
  78   if (!will_link || (tp && tp->klass() && !tp->klass()->is_loaded())) {
  79     if (C->log() != NULL) {
  80       if (!will_link) {
  81         C->log()->elem("assert_null reason='checkcast' klass='%d'",
  82                        C->log()->identify(klass));
  83       }
  84       if (tp && tp->klass() && !tp->klass()->is_loaded()) {
  85         // %%% Cannot happen?
  86         C->log()->elem("assert_null reason='checkcast source' klass='%d'",
  87                        C->log()->identify(tp->klass()));
  88       }
  89     }
  90     null_assert(obj);
  91     assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
  92     if (!stopped()) {
  93       profile_null_checkcast();
  94     }
  95     return;
  96   }
  97 
  98   Node *res = gen_checkcast(obj, makecon(TypeKlassPtr::make(klass)) );
  99 
 100   // Pop from stack AFTER gen_checkcast because it can uncommon trap and
 101   // the debug info has to be correct.
 102   pop();
 103   push(res);
 104 }
 105 
 106 
 107 //------------------------------do_instanceof----------------------------------
 108 void Parse::do_instanceof() {
 109   if (stopped())  return;
 110   // We would like to return false if class is not loaded, emitting a
 111   // dependency, but Java requires instanceof to load its operand.
 112 
 113   // Throw uncommon trap if class is not loaded
 114   bool will_link;
 115   ciKlass* klass = iter().get_klass(will_link);
 116 
 117   if (!will_link) {
 118     if (C->log() != NULL) {
 119       C->log()->elem("assert_null reason='instanceof' klass='%d'",
 120                      C->log()->identify(klass));
 121     }
 122     null_assert(peek());
 123     assert( stopped() || _gvn.type(peek())->higher_equal(TypePtr::NULL_PTR), "what's left behind is null" );
 124     if (!stopped()) {
 125       // The object is now known to be null.
 126       // Shortcut the effect of gen_instanceof and return "false" directly.
 127       pop();                   // pop the null
 128       push(_gvn.intcon(0));    // push false answer
 129     }
 130     return;
 131   }
 132 
 133   // Push the bool result back on stack
 134   Node* res = gen_instanceof(peek(), makecon(TypeKlassPtr::make(klass)), true);
 135 
 136   // Pop from stack AFTER gen_instanceof because it can uncommon trap.
 137   pop();
 138   push(res);
 139 }
 140 
 141 //------------------------------array_store_check------------------------------
 142 // pull array from stack and check that the store is valid
 143 void Parse::array_store_check(bool target_is_valuetypearray) {
 144 
 145   // Shorthand access to array store elements without popping them.
 146   Node *obj = peek(0);
 147   Node *idx = peek(1);
 148   Node *ary = peek(2);
 149 
 150   if (_gvn.type(obj) == TypePtr::NULL_PTR) {
 151     // There's never a type check on null values.
 152     // This cutout lets us avoid the uncommon_trap(Reason_array_check)
 153     // below, which turns into a performance liability if the
 154     // gen_checkcast folds up completely.
 155     return;
 156   }
 157 
 158   // Extract the array klass type
 159   int klass_offset = oopDesc::klass_offset_in_bytes();
 160   Node* p = basic_plus_adr( ary, ary, klass_offset );
 161   // p's type is array-of-OOPS plus klass_offset
 162   Node* array_klass = _gvn.transform(LoadKlassNode::make(_gvn, NULL, immutable_memory(), p, TypeInstPtr::KLASS));
 163   // Get the array klass
 164   const TypeKlassPtr *tak = _gvn.type(array_klass)->is_klassptr();
 165 
 166   // The type of array_klass is usually INexact array-of-oop.  Heroically
 167   // cast array_klass to EXACT array and uncommon-trap if the cast fails.
 168   // Make constant out of the inexact array klass, but use it only if the cast
 169   // succeeds.
 170   bool always_see_exact_class = false;
 171   if (MonomorphicArrayCheck
 172       && !too_many_traps(Deoptimization::Reason_array_check)
 173       && !tak->klass_is_exact()
 174       && tak != TypeKlassPtr::OBJECT) {
 175       // Regarding the fourth condition in the if-statement from above:
 176       //
 177       // If the compiler has determined that the type of array 'ary' (represented
 178       // by 'array_klass') is java/lang/Object, the compiler must not assume that
 179       // the array 'ary' is monomorphic.
 180       //
 181       // If 'ary' were of type java/lang/Object, this arraystore would have to fail,
 182       // because it is not possible to perform a arraystore into an object that is not
 183       // a "proper" array.
 184       //
 185       // Therefore, let's obtain at runtime the type of 'ary' and check if we can still
 186       // successfully perform the store.
 187       //
 188       // The implementation reasons for the condition are the following:
 189       //
 190       // java/lang/Object is the superclass of all arrays, but it is represented by the VM
 191       // as an InstanceKlass. The checks generated by gen_checkcast() (see below) expect
 192       // 'array_klass' to be ObjArrayKlass, which can result in invalid memory accesses.
 193       //
 194       // See issue JDK-8057622 for details.
 195 
 196     always_see_exact_class = true;
 197     // (If no MDO at all, hope for the best, until a trap actually occurs.)
 198 
 199     // Make a constant out of the inexact array klass
 200     const TypeKlassPtr *extak = tak->cast_to_exactness(true)->is_klassptr();
 201     Node* con = makecon(extak);
 202     Node* cmp = _gvn.transform(new CmpPNode( array_klass, con ));
 203     Node* bol = _gvn.transform(new BoolNode( cmp, BoolTest::eq ));
 204     Node* ctrl= control();
 205     { BuildCutout unless(this, bol, PROB_MAX);
 206       uncommon_trap(Deoptimization::Reason_array_check,
 207                     Deoptimization::Action_maybe_recompile,
 208                     tak->klass());
 209     }
 210     if (stopped()) {          // MUST uncommon-trap?
 211       set_control(ctrl);      // Then Don't Do It, just fall into the normal checking
 212     } else {                  // Cast array klass to exactness:
 213       // Use the exact constant value we know it is.
 214       replace_in_map(array_klass,con);
 215       CompileLog* log = C->log();
 216       if (log != NULL) {
 217         log->elem("cast_up reason='monomorphic_array' from='%d' to='(exact)'",
 218                   log->identify(tak->klass()));
 219       }
 220       array_klass = con;      // Use cast value moving forward
 221     }
 222   }
 223 
 224   // Come here for polymorphic array klasses
 225 
 226   // Extract the array element class
 227   int element_klass_offset = in_bytes(ArrayKlass::element_klass_offset());
 228 
 229   Node *p2 = basic_plus_adr(array_klass, array_klass, element_klass_offset);
 230   // We are allowed to use the constant type only if cast succeeded. If always_see_exact_class is true,
 231   // we must set a control edge from the IfTrue node created by the uncommon_trap above to the
 232   // LoadKlassNode.
 233   Node* a_e_klass = _gvn.transform(LoadKlassNode::make(_gvn, always_see_exact_class ? control() : NULL,
 234                                                        immutable_memory(), p2, tak));
 235 
 236   if (target_is_valuetypearray) {
 237     ciKlass* target_elem_klass = gvn().type(a_e_klass)->is_klassptr()->klass();
 238     ciKlass* source_klass = gvn().type(obj)->is_valuetype()->value_klass();
 239     if (!target_elem_klass->equals(source_klass)) {
 240       Node* slow_ctl = type_check(a_e_klass, TypeKlassPtr::make(source_klass), 1.0);
 241       {
 242         PreserveJVMState pjvms(this);
 243         set_control(slow_ctl);
 244         builtin_throw(Deoptimization::Reason_class_check);
 245       }
 246     }
 247   } else {
 248     // Check (the hard way) and throw if not a subklass.
 249     // Result is ignored, we just need the CFG effects.
 250     gen_checkcast(obj, a_e_klass);
 251   }
 252 }
 253 
 254 
 255 void Parse::emit_guard_for_new(ciInstanceKlass* klass) {
 256   // Emit guarded new
 257   //   if (klass->_init_thread != current_thread ||
 258   //       klass->_init_state != being_initialized)
 259   //      uncommon_trap
 260   Node* cur_thread = _gvn.transform( new ThreadLocalNode() );
 261   Node* merge = new RegionNode(3);
 262   _gvn.set_type(merge, Type::CONTROL);
 263   Node* kls = makecon(TypeKlassPtr::make(klass));
 264 
 265   Node* init_thread_offset = _gvn.MakeConX(in_bytes(InstanceKlass::init_thread_offset()));
 266   Node* adr_node = basic_plus_adr(kls, kls, init_thread_offset);
 267   Node* init_thread = make_load(NULL, adr_node, TypeRawPtr::BOTTOM, T_ADDRESS, MemNode::unordered);
 268   Node *tst   = Bool( CmpP( init_thread, cur_thread), BoolTest::eq);
 269   IfNode* iff = create_and_map_if(control(), tst, PROB_ALWAYS, COUNT_UNKNOWN);
 270   set_control(IfTrue(iff));
 271   merge->set_req(1, IfFalse(iff));
 272 
 273   Node* init_state_offset = _gvn.MakeConX(in_bytes(InstanceKlass::init_state_offset()));
 274   adr_node = basic_plus_adr(kls, kls, init_state_offset);
 275   // Use T_BOOLEAN for InstanceKlass::_init_state so the compiler
 276   // can generate code to load it as unsigned byte.
 277   Node* init_state = make_load(NULL, adr_node, TypeInt::UBYTE, T_BOOLEAN, MemNode::unordered);
 278   Node* being_init = _gvn.intcon(InstanceKlass::being_initialized);
 279   tst   = Bool( CmpI( init_state, being_init), BoolTest::eq);
 280   iff = create_and_map_if(control(), tst, PROB_ALWAYS, COUNT_UNKNOWN);
 281   set_control(IfTrue(iff));
 282   merge->set_req(2, IfFalse(iff));
 283 
 284   PreserveJVMState pjvms(this);
 285   record_for_igvn(merge);
 286   set_control(merge);
 287 
 288   uncommon_trap(Deoptimization::Reason_uninitialized,
 289                 Deoptimization::Action_reinterpret,
 290                 klass);
 291 }
 292 
 293 
 294 //------------------------------do_new-----------------------------------------
 295 void Parse::do_new() {
 296   kill_dead_locals();
 297 
 298   bool will_link;
 299   ciInstanceKlass* klass = iter().get_klass(will_link)->as_instance_klass();
 300   assert(will_link, "_new: typeflow responsibility");
 301 
 302   // Should initialize, or throw an InstantiationError?
 303   if (!klass->is_initialized() && !klass->is_being_initialized() ||
 304       klass->is_abstract() || klass->is_interface() ||
 305       klass->name() == ciSymbol::java_lang_Class() ||
 306       iter().is_unresolved_klass()) {
 307     uncommon_trap(Deoptimization::Reason_uninitialized,
 308                   Deoptimization::Action_reinterpret,
 309                   klass);
 310     return;
 311   }
 312   if (klass->is_being_initialized()) {
 313     emit_guard_for_new(klass);
 314   }
 315 
 316   Node* kls = makecon(TypeKlassPtr::make(klass));
 317   Node* obj = new_instance(kls);
 318 
 319   // Push resultant oop onto stack
 320   push(obj);
 321 
 322   // Keep track of whether opportunities exist for StringBuilder
 323   // optimizations.
 324   if (OptimizeStringConcat &&
 325       (klass == C->env()->StringBuilder_klass() ||
 326        klass == C->env()->StringBuffer_klass())) {
 327     C->set_has_stringbuilder(true);
 328   }
 329 
 330   // Keep track of boxed values for EliminateAutoBox optimizations.
 331   if (C->eliminate_boxing() && klass->is_box_klass()) {
 332     C->set_has_boxed_value(true);
 333   }
 334 }
 335 
 336 //------------------------------do_vdefault-------------------------------------
 337 void Parse::do_vdefault() {
 338   // Fixme additional checks needed?
 339   bool will_link;
 340   ciValueKlass* vk = iter().get_klass(will_link)->as_value_klass();
 341   assert(will_link, "vdefault: typeflow responsibility");
 342 
 343   // Create a new ValueTypeNode
 344   Node* vt = ValueTypeNode::make_default(_gvn, vk);
 345 
 346   push(_gvn.transform(vt));
 347 }
 348 
 349 //------------------------------do_vwithfield-----------------------------------
 350 void Parse::do_vwithfield() {
 351   // Fixme additional checks needed?
 352   bool will_link;
 353   ciField* field = iter().get_field(will_link);
 354   assert(will_link, "vdefault: typeflow responsibility");
 355   BasicType bt = field->layout_type();
 356   Node* val = type2size[bt] == 1 ? pop() : pop_pair();
 357   Node* vt = pop();
 358   assert(vt->is_ValueType(), "value type expected here");
 359 
 360   ValueTypeNode* new_vt = vt->clone()->as_ValueType();
 361   new_vt->set_oop(_gvn.zerocon(T_VALUETYPE));
 362   int offset = field->offset();
 363   uint i = 0;
 364   for (; i < new_vt->field_count() && new_vt->field_offset(i) != offset; i++);
 365   assert(i < new_vt->field_count(), "where's the field");
 366   new_vt->set_field_value(i, val);
 367 
 368   push(_gvn.transform(new_vt));
 369 }
 370 
 371 #ifndef PRODUCT
 372 //------------------------------dump_map_adr_mem-------------------------------
 373 // Debug dump of the mapping from address types to MergeMemNode indices.
 374 void Parse::dump_map_adr_mem() const {
 375   tty->print_cr("--- Mapping from address types to memory Nodes ---");
 376   MergeMemNode *mem = map() == NULL ? NULL : (map()->memory()->is_MergeMem() ?
 377                                       map()->memory()->as_MergeMem() : NULL);
 378   for (uint i = 0; i < (uint)C->num_alias_types(); i++) {
 379     C->alias_type(i)->print_on(tty);
 380     tty->print("\t");
 381     // Node mapping, if any
 382     if (mem && i < mem->req() && mem->in(i) && mem->in(i) != mem->empty_memory()) {
 383       mem->in(i)->dump();
 384     } else {
 385       tty->cr();
 386     }
 387   }
 388 }
 389 
 390 #endif
 391 
 392 
 393 //=============================================================================
 394 //
 395 // parser methods for profiling
 396 
 397 
 398 //----------------------test_counter_against_threshold ------------------------
 399 void Parse::test_counter_against_threshold(Node* cnt, int limit) {
 400   // Test the counter against the limit and uncommon trap if greater.
 401 
 402   // This code is largely copied from the range check code in
 403   // array_addressing()
 404 
 405   // Test invocation count vs threshold
 406   Node *threshold = makecon(TypeInt::make(limit));
 407   Node *chk   = _gvn.transform( new CmpUNode( cnt, threshold) );
 408   BoolTest::mask btest = BoolTest::lt;
 409   Node *tst   = _gvn.transform( new BoolNode( chk, btest) );
 410   // Branch to failure if threshold exceeded
 411   { BuildCutout unless(this, tst, PROB_ALWAYS);
 412     uncommon_trap(Deoptimization::Reason_age,
 413                   Deoptimization::Action_maybe_recompile);
 414   }
 415 }
 416 
 417 //----------------------increment_and_test_invocation_counter-------------------
 418 void Parse::increment_and_test_invocation_counter(int limit) {
 419   if (!count_invocations()) return;
 420 
 421   // Get the Method* node.
 422   ciMethod* m = method();
 423   MethodCounters* counters_adr = m->ensure_method_counters();
 424   if (counters_adr == NULL) {
 425     C->record_failure("method counters allocation failed");
 426     return;
 427   }
 428 
 429   Node* ctrl = control();
 430   const TypePtr* adr_type = TypeRawPtr::make((address) counters_adr);
 431   Node *counters_node = makecon(adr_type);
 432   Node* adr_iic_node = basic_plus_adr(counters_node, counters_node,
 433     MethodCounters::interpreter_invocation_counter_offset_in_bytes());
 434   Node* cnt = make_load(ctrl, adr_iic_node, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
 435 
 436   test_counter_against_threshold(cnt, limit);
 437 
 438   // Add one to the counter and store
 439   Node* incr = _gvn.transform(new AddINode(cnt, _gvn.intcon(1)));
 440   store_to_memory(ctrl, adr_iic_node, incr, T_INT, adr_type, MemNode::unordered);
 441 }
 442 
 443 //----------------------------method_data_addressing---------------------------
 444 Node* Parse::method_data_addressing(ciMethodData* md, ciProfileData* data, ByteSize counter_offset, Node* idx, uint stride) {
 445   // Get offset within MethodData* of the data array
 446   ByteSize data_offset = MethodData::data_offset();
 447 
 448   // Get cell offset of the ProfileData within data array
 449   int cell_offset = md->dp_to_di(data->dp());
 450 
 451   // Add in counter_offset, the # of bytes into the ProfileData of counter or flag
 452   int offset = in_bytes(data_offset) + cell_offset + in_bytes(counter_offset);
 453 
 454   const TypePtr* adr_type = TypeMetadataPtr::make(md);
 455   Node* mdo = makecon(adr_type);
 456   Node* ptr = basic_plus_adr(mdo, mdo, offset);
 457 
 458   if (stride != 0) {
 459     Node* str = _gvn.MakeConX(stride);
 460     Node* scale = _gvn.transform( new MulXNode( idx, str ) );
 461     ptr   = _gvn.transform( new AddPNode( mdo, ptr, scale ) );
 462   }
 463 
 464   return ptr;
 465 }
 466 
 467 //--------------------------increment_md_counter_at----------------------------
 468 void Parse::increment_md_counter_at(ciMethodData* md, ciProfileData* data, ByteSize counter_offset, Node* idx, uint stride) {
 469   Node* adr_node = method_data_addressing(md, data, counter_offset, idx, stride);
 470 
 471   const TypePtr* adr_type = _gvn.type(adr_node)->is_ptr();
 472   Node* cnt  = make_load(NULL, adr_node, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
 473   Node* incr = _gvn.transform(new AddINode(cnt, _gvn.intcon(DataLayout::counter_increment)));
 474   store_to_memory(NULL, adr_node, incr, T_INT, adr_type, MemNode::unordered);
 475 }
 476 
 477 //--------------------------test_for_osr_md_counter_at-------------------------
 478 void Parse::test_for_osr_md_counter_at(ciMethodData* md, ciProfileData* data, ByteSize counter_offset, int limit) {
 479   Node* adr_node = method_data_addressing(md, data, counter_offset);
 480 
 481   const TypePtr* adr_type = _gvn.type(adr_node)->is_ptr();
 482   Node* cnt  = make_load(NULL, adr_node, TypeInt::INT, T_INT, adr_type, MemNode::unordered);
 483 
 484   test_counter_against_threshold(cnt, limit);
 485 }
 486 
 487 //-------------------------------set_md_flag_at--------------------------------
 488 void Parse::set_md_flag_at(ciMethodData* md, ciProfileData* data, int flag_constant) {
 489   Node* adr_node = method_data_addressing(md, data, DataLayout::flags_offset());
 490 
 491   const TypePtr* adr_type = _gvn.type(adr_node)->is_ptr();
 492   Node* flags = make_load(NULL, adr_node, TypeInt::BYTE, T_BYTE, adr_type, MemNode::unordered);
 493   Node* incr = _gvn.transform(new OrINode(flags, _gvn.intcon(flag_constant)));
 494   store_to_memory(NULL, adr_node, incr, T_BYTE, adr_type, MemNode::unordered);
 495 }
 496 
 497 //----------------------------profile_taken_branch-----------------------------
 498 void Parse::profile_taken_branch(int target_bci, bool force_update) {
 499   // This is a potential osr_site if we have a backedge.
 500   int cur_bci = bci();
 501   bool osr_site =
 502     (target_bci <= cur_bci) && count_invocations() && UseOnStackReplacement;
 503 
 504   // If we are going to OSR, restart at the target bytecode.
 505   set_bci(target_bci);
 506 
 507   // To do: factor out the the limit calculations below. These duplicate
 508   // the similar limit calculations in the interpreter.
 509 
 510   if (method_data_update() || force_update) {
 511     ciMethodData* md = method()->method_data();
 512     assert(md != NULL, "expected valid ciMethodData");
 513     ciProfileData* data = md->bci_to_data(cur_bci);
 514     assert(data->is_JumpData(), "need JumpData for taken branch");
 515     increment_md_counter_at(md, data, JumpData::taken_offset());
 516   }
 517 
 518   // In the new tiered system this is all we need to do. In the old
 519   // (c2 based) tiered sytem we must do the code below.
 520 #ifndef TIERED
 521   if (method_data_update()) {
 522     ciMethodData* md = method()->method_data();
 523     if (osr_site) {
 524       ciProfileData* data = md->bci_to_data(cur_bci);
 525       int limit = (CompileThreshold
 526                    * (OnStackReplacePercentage - InterpreterProfilePercentage)) / 100;
 527       test_for_osr_md_counter_at(md, data, JumpData::taken_offset(), limit);
 528     }
 529   } else {
 530     // With method data update off, use the invocation counter to trigger an
 531     // OSR compilation, as done in the interpreter.
 532     if (osr_site) {
 533       int limit = (CompileThreshold * OnStackReplacePercentage) / 100;
 534       increment_and_test_invocation_counter(limit);
 535     }
 536   }
 537 #endif // TIERED
 538 
 539   // Restore the original bytecode.
 540   set_bci(cur_bci);
 541 }
 542 
 543 //--------------------------profile_not_taken_branch---------------------------
 544 void Parse::profile_not_taken_branch(bool force_update) {
 545 
 546   if (method_data_update() || force_update) {
 547     ciMethodData* md = method()->method_data();
 548     assert(md != NULL, "expected valid ciMethodData");
 549     ciProfileData* data = md->bci_to_data(bci());
 550     assert(data->is_BranchData(), "need BranchData for not taken branch");
 551     increment_md_counter_at(md, data, BranchData::not_taken_offset());
 552   }
 553 
 554 }
 555 
 556 //---------------------------------profile_call--------------------------------
 557 void Parse::profile_call(Node* receiver) {
 558   if (!method_data_update()) return;
 559 
 560   switch (bc()) {
 561   case Bytecodes::_invokevirtual:
 562   case Bytecodes::_invokeinterface:
 563     profile_receiver_type(receiver);
 564     break;
 565   case Bytecodes::_invokestatic:
 566   case Bytecodes::_invokedynamic:
 567   case Bytecodes::_invokespecial:
 568     profile_generic_call();
 569     break;
 570   default: fatal("unexpected call bytecode");
 571   }
 572 }
 573 
 574 //------------------------------profile_generic_call---------------------------
 575 void Parse::profile_generic_call() {
 576   assert(method_data_update(), "must be generating profile code");
 577 
 578   ciMethodData* md = method()->method_data();
 579   assert(md != NULL, "expected valid ciMethodData");
 580   ciProfileData* data = md->bci_to_data(bci());
 581   assert(data->is_CounterData(), "need CounterData for not taken branch");
 582   increment_md_counter_at(md, data, CounterData::count_offset());
 583 }
 584 
 585 //-----------------------------profile_receiver_type---------------------------
 586 void Parse::profile_receiver_type(Node* receiver) {
 587   assert(method_data_update(), "must be generating profile code");
 588 
 589   ciMethodData* md = method()->method_data();
 590   assert(md != NULL, "expected valid ciMethodData");
 591   ciProfileData* data = md->bci_to_data(bci());
 592   assert(data->is_ReceiverTypeData(), "need ReceiverTypeData here");
 593 
 594   // Skip if we aren't tracking receivers
 595   if (TypeProfileWidth < 1) {
 596     increment_md_counter_at(md, data, CounterData::count_offset());
 597     return;
 598   }
 599   ciReceiverTypeData* rdata = (ciReceiverTypeData*)data->as_ReceiverTypeData();
 600 
 601   Node* method_data = method_data_addressing(md, rdata, in_ByteSize(0));
 602 
 603   // Using an adr_type of TypePtr::BOTTOM to work around anti-dep problems.
 604   // A better solution might be to use TypeRawPtr::BOTTOM with RC_NARROW_MEM.
 605   make_runtime_call(RC_LEAF, OptoRuntime::profile_receiver_type_Type(),
 606                     CAST_FROM_FN_PTR(address,
 607                                      OptoRuntime::profile_receiver_type_C),
 608                     "profile_receiver_type_C",
 609                     TypePtr::BOTTOM,
 610                     method_data, receiver);
 611 }
 612 
 613 //---------------------------------profile_ret---------------------------------
 614 void Parse::profile_ret(int target_bci) {
 615   if (!method_data_update()) return;
 616 
 617   // Skip if we aren't tracking ret targets
 618   if (TypeProfileWidth < 1) return;
 619 
 620   ciMethodData* md = method()->method_data();
 621   assert(md != NULL, "expected valid ciMethodData");
 622   ciProfileData* data = md->bci_to_data(bci());
 623   assert(data->is_RetData(), "need RetData for ret");
 624   ciRetData* ret_data = (ciRetData*)data->as_RetData();
 625 
 626   // Look for the target_bci is already in the table
 627   uint row;
 628   bool table_full = true;
 629   for (row = 0; row < ret_data->row_limit(); row++) {
 630     int key = ret_data->bci(row);
 631     table_full &= (key != RetData::no_bci);
 632     if (key == target_bci) break;
 633   }
 634 
 635   if (row >= ret_data->row_limit()) {
 636     // The target_bci was not found in the table.
 637     if (!table_full) {
 638       // XXX: Make slow call to update RetData
 639     }
 640     return;
 641   }
 642 
 643   // the target_bci is already in the table
 644   increment_md_counter_at(md, data, RetData::bci_count_offset(row));
 645 }
 646 
 647 //--------------------------profile_null_checkcast----------------------------
 648 void Parse::profile_null_checkcast() {
 649   // Set the null-seen flag, done in conjunction with the usual null check. We
 650   // never unset the flag, so this is a one-way switch.
 651   if (!method_data_update()) return;
 652 
 653   ciMethodData* md = method()->method_data();
 654   assert(md != NULL, "expected valid ciMethodData");
 655   ciProfileData* data = md->bci_to_data(bci());
 656   assert(data->is_BitData(), "need BitData for checkcast");
 657   set_md_flag_at(md, data, BitData::null_seen_byte_constant());
 658 }
 659 
 660 //-----------------------------profile_switch_case-----------------------------
 661 void Parse::profile_switch_case(int table_index) {
 662   if (!method_data_update()) return;
 663 
 664   ciMethodData* md = method()->method_data();
 665   assert(md != NULL, "expected valid ciMethodData");
 666 
 667   ciProfileData* data = md->bci_to_data(bci());
 668   assert(data->is_MultiBranchData(), "need MultiBranchData for switch case");
 669   if (table_index >= 0) {
 670     increment_md_counter_at(md, data, MultiBranchData::case_count_offset(table_index));
 671   } else {
 672     increment_md_counter_at(md, data, MultiBranchData::default_count_offset());
 673   }
 674 }