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