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