1 /*
   2  * Copyright (c) 1999, 2023, 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/ciCallProfile.hpp"
  27 #include "ci/ciExceptionHandler.hpp"
  28 #include "ci/ciInstanceKlass.hpp"
  29 #include "ci/ciMethod.hpp"
  30 #include "ci/ciMethodBlocks.hpp"
  31 #include "ci/ciMethodData.hpp"
  32 #include "ci/ciStreams.hpp"
  33 #include "ci/ciSymbol.hpp"
  34 #include "ci/ciReplay.hpp"
  35 #include "ci/ciSymbols.hpp"
  36 #include "ci/ciUtilities.inline.hpp"
  37 #include "compiler/abstractCompiler.hpp"
  38 #include "compiler/compilerDefinitions.inline.hpp"
  39 #include "compiler/methodLiveness.hpp"
  40 #include "interpreter/interpreter.hpp"
  41 #include "interpreter/linkResolver.hpp"
  42 #include "interpreter/oopMapCache.hpp"
  43 #include "logging/log.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "memory/allocation.inline.hpp"
  46 #include "memory/resourceArea.hpp"
  47 #include "oops/generateOopMap.hpp"
  48 #include "oops/method.inline.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "prims/methodHandles.hpp"
  51 #include "runtime/deoptimization.hpp"
  52 #include "runtime/handles.inline.hpp"
  53 #include "utilities/bitMap.inline.hpp"
  54 #include "utilities/xmlstream.hpp"
  55 #ifdef COMPILER2
  56 #include "ci/bcEscapeAnalyzer.hpp"
  57 #include "ci/ciTypeFlow.hpp"
  58 #include "oops/method.hpp"
  59 #endif
  60 
  61 // ciMethod
  62 //
  63 // This class represents a Method* in the HotSpot virtual
  64 // machine.
  65 
  66 
  67 // ------------------------------------------------------------------
  68 // ciMethod::ciMethod
  69 //
  70 // Loaded method.
  71 ciMethod::ciMethod(const methodHandle& h_m, ciInstanceKlass* holder) :
  72   ciMetadata(h_m()),
  73   _holder(holder)
  74 {
  75   assert(h_m() != nullptr, "no null method");
  76   assert(_holder->get_instanceKlass() == h_m->method_holder(), "");
  77 
  78   // These fields are always filled in in loaded methods.
  79   _flags = ciFlags(h_m->access_flags());
  80 
  81   // Easy to compute, so fill them in now.
  82   _max_stack          = h_m->max_stack();
  83   _max_locals         = h_m->max_locals();
  84   _code_size          = h_m->code_size();
  85   _handler_count      = h_m->exception_table_length();
  86   _size_of_parameters = h_m->size_of_parameters();
  87   _uses_monitors      = h_m->has_monitor_bytecodes();
  88   _balanced_monitors  = !_uses_monitors || h_m->guaranteed_monitor_matching();
  89   _is_c1_compilable   = !h_m->is_not_c1_compilable();
  90   _is_c2_compilable   = !h_m->is_not_c2_compilable();
  91   _can_be_parsed      = true;
  92   _has_reserved_stack_access = h_m->has_reserved_stack_access();
  93   _is_overpass        = h_m->is_overpass();
  94   // Lazy fields, filled in on demand.  Require allocation.
  95   _code               = nullptr;
  96   _exception_handlers = nullptr;
  97   _liveness           = nullptr;
  98   _method_blocks = nullptr;
  99 #if defined(COMPILER2)
 100   _flow               = nullptr;
 101   _bcea               = nullptr;
 102 #endif // COMPILER2
 103 
 104   // Check for blackhole intrinsic and then populate the intrinsic ID.
 105   CompilerOracle::tag_blackhole_if_possible(h_m);
 106   _intrinsic_id       = h_m->intrinsic_id();
 107 
 108   ciEnv *env = CURRENT_ENV;
 109   if (env->jvmti_can_hotswap_or_post_breakpoint()) {
 110     // 6328518 check hotswap conditions under the right lock.
 111     MutexLocker locker(Compile_lock);
 112     if (Dependencies::check_evol_method(h_m()) != nullptr) {
 113       _is_c1_compilable = false;
 114       _is_c2_compilable = false;
 115       _can_be_parsed = false;
 116     }
 117   } else {
 118     DEBUG_ONLY(CompilerThread::current()->check_possible_safepoint());
 119   }
 120 
 121   if (h_m->method_holder()->is_linked()) {
 122     _can_be_statically_bound = h_m->can_be_statically_bound();
 123     _can_omit_stack_trace = h_m->can_omit_stack_trace();
 124   } else {
 125     // Have to use a conservative value in this case.
 126     _can_be_statically_bound = false;
 127     _can_omit_stack_trace = true;
 128   }
 129 
 130   // Adjust the definition of this condition to be more useful:
 131   // %%% take these conditions into account in vtable generation
 132   if (!_can_be_statically_bound && h_m->is_private())
 133     _can_be_statically_bound = true;
 134   if (_can_be_statically_bound && h_m->is_abstract())
 135     _can_be_statically_bound = false;
 136 
 137   // generating _signature may allow GC and therefore move m.
 138   // These fields are always filled in.
 139   _name = env->get_symbol(h_m->name());
 140   ciSymbol* sig_symbol = env->get_symbol(h_m->signature());
 141   constantPoolHandle cpool(Thread::current(), h_m->constants());
 142   _signature = new (env->arena()) ciSignature(_holder, cpool, sig_symbol);
 143   _method_data = nullptr;
 144   // Take a snapshot of these values, so they will be commensurate with the MDO.
 145   if (ProfileInterpreter || CompilerConfig::is_c1_profiling()) {
 146     int invcnt = h_m->interpreter_invocation_count();
 147     // if the value overflowed report it as max int
 148     _interpreter_invocation_count = invcnt < 0 ? max_jint : invcnt ;
 149     _interpreter_throwout_count   = h_m->interpreter_throwout_count();
 150   } else {
 151     _interpreter_invocation_count = 0;
 152     _interpreter_throwout_count = 0;
 153   }
 154   if (_interpreter_invocation_count == 0)
 155     _interpreter_invocation_count = 1;
 156   _inline_instructions_size = -1;
 157   if (ReplayCompiles) {
 158     ciReplay::initialize(this);
 159   }
 160 }
 161 
 162 
 163 // ------------------------------------------------------------------
 164 // ciMethod::ciMethod
 165 //
 166 // Unloaded method.
 167 ciMethod::ciMethod(ciInstanceKlass* holder,
 168                    ciSymbol*        name,
 169                    ciSymbol*        signature,
 170                    ciInstanceKlass* accessor) :
 171   ciMetadata((Metadata*)nullptr),
 172   _name(                   name),
 173   _holder(                 holder),
 174   _method_data(            nullptr),
 175   _method_blocks(          nullptr),
 176   _intrinsic_id(           vmIntrinsics::_none),
 177   _inline_instructions_size(-1),
 178   _can_be_statically_bound(false),
 179   _can_omit_stack_trace(true),
 180   _liveness(               nullptr)
 181 #if defined(COMPILER2)
 182   ,
 183   _flow(                   nullptr),
 184   _bcea(                   nullptr)
 185 #endif // COMPILER2
 186 {
 187   // Usually holder and accessor are the same type but in some cases
 188   // the holder has the wrong class loader (e.g. invokedynamic call
 189   // sites) so we pass the accessor.
 190   _signature = new (CURRENT_ENV->arena()) ciSignature(accessor, constantPoolHandle(), signature);
 191 }
 192 
 193 
 194 // ------------------------------------------------------------------
 195 // ciMethod::load_code
 196 //
 197 // Load the bytecodes and exception handler table for this method.
 198 void ciMethod::load_code() {
 199   VM_ENTRY_MARK;
 200   assert(is_loaded(), "only loaded methods have code");
 201 
 202   Method* me = get_Method();
 203   Arena* arena = CURRENT_THREAD_ENV->arena();
 204 
 205   // Load the bytecodes.
 206   _code = (address)arena->Amalloc(code_size());
 207   memcpy(_code, me->code_base(), code_size());
 208 
 209 #if INCLUDE_JVMTI
 210   // Revert any breakpoint bytecodes in ci's copy
 211   if (me->number_of_breakpoints() > 0) {
 212     BreakpointInfo* bp = me->method_holder()->breakpoints();
 213     for (; bp != nullptr; bp = bp->next()) {
 214       if (bp->match(me)) {
 215         code_at_put(bp->bci(), bp->orig_bytecode());
 216       }
 217     }
 218   }
 219 #endif
 220 
 221   // And load the exception table.
 222   ExceptionTable exc_table(me);
 223 
 224   // Allocate one extra spot in our list of exceptions.  This
 225   // last entry will be used to represent the possibility that
 226   // an exception escapes the method.  See ciExceptionHandlerStream
 227   // for details.
 228   _exception_handlers =
 229     (ciExceptionHandler**)arena->Amalloc(sizeof(ciExceptionHandler*)
 230                                          * (_handler_count + 1));
 231   if (_handler_count > 0) {
 232     for (int i=0; i<_handler_count; i++) {
 233       _exception_handlers[i] = new (arena) ciExceptionHandler(
 234                                 holder(),
 235             /* start    */      exc_table.start_pc(i),
 236             /* limit    */      exc_table.end_pc(i),
 237             /* goto pc  */      exc_table.handler_pc(i),
 238             /* cp index */      exc_table.catch_type_index(i));
 239     }
 240   }
 241 
 242   // Put an entry at the end of our list to represent the possibility
 243   // of exceptional exit.
 244   _exception_handlers[_handler_count] =
 245     new (arena) ciExceptionHandler(holder(), 0, code_size(), -1, 0);
 246 
 247   if (CIPrintMethodCodes) {
 248     print_codes();
 249   }
 250 }
 251 
 252 
 253 // ------------------------------------------------------------------
 254 // ciMethod::has_linenumber_table
 255 //
 256 // length unknown until decompression
 257 bool    ciMethod::has_linenumber_table() const {
 258   check_is_loaded();
 259   VM_ENTRY_MARK;
 260   return get_Method()->has_linenumber_table();
 261 }
 262 
 263 
 264 // ------------------------------------------------------------------
 265 // ciMethod::line_number_from_bci
 266 int ciMethod::line_number_from_bci(int bci) const {
 267   check_is_loaded();
 268   VM_ENTRY_MARK;
 269   return get_Method()->line_number_from_bci(bci);
 270 }
 271 
 272 
 273 // ------------------------------------------------------------------
 274 // ciMethod::vtable_index
 275 //
 276 // Get the position of this method's entry in the vtable, if any.
 277 int ciMethod::vtable_index() {
 278   check_is_loaded();
 279   assert(holder()->is_linked(), "must be linked");
 280   VM_ENTRY_MARK;
 281   return get_Method()->vtable_index();
 282 }
 283 
 284 // ------------------------------------------------------------------
 285 // ciMethod::uses_balanced_monitors
 286 //
 287 // Does this method use monitors in a strict stack-disciplined manner?
 288 bool ciMethod::has_balanced_monitors() {
 289   check_is_loaded();
 290   if (_balanced_monitors) return true;
 291 
 292   // Analyze the method to see if monitors are used properly.
 293   VM_ENTRY_MARK;
 294   methodHandle method(THREAD, get_Method());
 295   assert(method->has_monitor_bytecodes(), "should have checked this");
 296 
 297   // Check to see if a previous compilation computed the
 298   // monitor-matching analysis.
 299   if (method->guaranteed_monitor_matching()) {
 300     _balanced_monitors = true;
 301     return true;
 302   }
 303 
 304   {
 305     ExceptionMark em(THREAD);
 306     ResourceMark rm(THREAD);
 307     GeneratePairingInfo gpi(method);
 308     if (!gpi.compute_map(THREAD)) {
 309       fatal("Unrecoverable verification or out-of-memory error");
 310     }
 311     if (!gpi.monitor_safe()) {
 312       return false;
 313     }
 314     method->set_guaranteed_monitor_matching();
 315     _balanced_monitors = true;
 316   }
 317   return true;
 318 }
 319 
 320 
 321 // ------------------------------------------------------------------
 322 // ciMethod::get_flow_analysis
 323 ciTypeFlow* ciMethod::get_flow_analysis() {
 324 #if defined(COMPILER2)
 325   if (_flow == nullptr) {
 326     ciEnv* env = CURRENT_ENV;
 327     _flow = new (env->arena()) ciTypeFlow(env, this);
 328     _flow->do_flow();
 329   }
 330   return _flow;
 331 #else // COMPILER2
 332   ShouldNotReachHere();
 333   return nullptr;
 334 #endif // COMPILER2
 335 }
 336 
 337 
 338 // ------------------------------------------------------------------
 339 // ciMethod::get_osr_flow_analysis
 340 ciTypeFlow* ciMethod::get_osr_flow_analysis(int osr_bci) {
 341 #if defined(COMPILER2)
 342   // OSR entry points are always place after a call bytecode of some sort
 343   assert(osr_bci >= 0, "must supply valid OSR entry point");
 344   ciEnv* env = CURRENT_ENV;
 345   ciTypeFlow* flow = new (env->arena()) ciTypeFlow(env, this, osr_bci);
 346   flow->do_flow();
 347   return flow;
 348 #else // COMPILER2
 349   ShouldNotReachHere();
 350   return nullptr;
 351 #endif // COMPILER2
 352 }
 353 
 354 // ------------------------------------------------------------------
 355 // ciMethod::raw_liveness_at_bci
 356 //
 357 // Which local variables are live at a specific bci?
 358 MethodLivenessResult ciMethod::raw_liveness_at_bci(int bci) {
 359   check_is_loaded();
 360   if (_liveness == nullptr) {
 361     // Create the liveness analyzer.
 362     Arena* arena = CURRENT_ENV->arena();
 363     _liveness = new (arena) MethodLiveness(arena, this);
 364     _liveness->compute_liveness();
 365   }
 366   return _liveness->get_liveness_at(bci);
 367 }
 368 
 369 // ------------------------------------------------------------------
 370 // ciMethod::liveness_at_bci
 371 //
 372 // Which local variables are live at a specific bci?  When debugging
 373 // will return true for all locals in some cases to improve debug
 374 // information.
 375 MethodLivenessResult ciMethod::liveness_at_bci(int bci) {
 376   if (CURRENT_ENV->should_retain_local_variables() || DeoptimizeALot) {
 377     // Keep all locals live for the user's edification and amusement.
 378     MethodLivenessResult result(_max_locals);
 379     result.set_range(0, _max_locals);
 380     result.set_is_valid();
 381     return result;
 382   }
 383   return raw_liveness_at_bci(bci);
 384 }
 385 
 386 // ciMethod::live_local_oops_at_bci
 387 //
 388 // find all the live oops in the locals array for a particular bci
 389 // Compute what the interpreter believes by using the interpreter
 390 // oopmap generator. This is used as a double check during osr to
 391 // guard against conservative result from MethodLiveness making us
 392 // think a dead oop is live.  MethodLiveness is conservative in the
 393 // sense that it may consider locals to be live which cannot be live,
 394 // like in the case where a local could contain an oop or  a primitive
 395 // along different paths.  In that case the local must be dead when
 396 // those paths merge. Since the interpreter's viewpoint is used when
 397 // gc'ing an interpreter frame we need to use its viewpoint  during
 398 // OSR when loading the locals.
 399 
 400 ResourceBitMap ciMethod::live_local_oops_at_bci(int bci) {
 401   VM_ENTRY_MARK;
 402   InterpreterOopMap mask;
 403   OopMapCache::compute_one_oop_map(methodHandle(THREAD, get_Method()), bci, &mask);
 404   int mask_size = max_locals();
 405   ResourceBitMap result(mask_size);
 406   int i;
 407   for (i = 0; i < mask_size ; i++ ) {
 408     if (mask.is_oop(i)) result.set_bit(i);
 409   }
 410   return result;
 411 }
 412 
 413 
 414 #ifdef COMPILER1
 415 // ------------------------------------------------------------------
 416 // ciMethod::bci_block_start
 417 //
 418 // Marks all bcis where a new basic block starts
 419 const BitMap& ciMethod::bci_block_start() {
 420   check_is_loaded();
 421   if (_liveness == nullptr) {
 422     // Create the liveness analyzer.
 423     Arena* arena = CURRENT_ENV->arena();
 424     _liveness = new (arena) MethodLiveness(arena, this);
 425     _liveness->compute_liveness();
 426   }
 427 
 428   return _liveness->get_bci_block_start();
 429 }
 430 #endif // COMPILER1
 431 
 432 
 433 // ------------------------------------------------------------------
 434 // ciMethod::check_overflow
 435 //
 436 // Check whether the profile counter is overflowed and adjust if true.
 437 // For invoke* it will turn negative values into max_jint,
 438 // and for checkcast/aastore/instanceof turn positive values into min_jint.
 439 int ciMethod::check_overflow(int c, Bytecodes::Code code) {
 440   switch (code) {
 441     case Bytecodes::_aastore:    // fall-through
 442     case Bytecodes::_checkcast:  // fall-through
 443     case Bytecodes::_instanceof: {
 444       if (VM_Version::profile_all_receivers_at_type_check()) {
 445         return (c < 0 ? max_jint : c); // always non-negative
 446       }
 447       return (c > 0 ? min_jint : c); // always non-positive
 448     }
 449     default: {
 450       assert(Bytecodes::is_invoke(code), "%s", Bytecodes::name(code));
 451       return (c < 0 ? max_jint : c); // always non-negative
 452     }
 453   }
 454 }
 455 
 456 
 457 // ------------------------------------------------------------------
 458 // ciMethod::call_profile_at_bci
 459 //
 460 // Get the ciCallProfile for the invocation of this method.
 461 // Also reports receiver types for non-call type checks (if TypeProfileCasts).
 462 ciCallProfile ciMethod::call_profile_at_bci(int bci) {
 463   ResourceMark rm;
 464   ciCallProfile result;
 465   if (method_data() != nullptr && method_data()->is_mature()) {
 466     ciProfileData* data = method_data()->bci_to_data(bci);
 467     if (data != nullptr && data->is_CounterData()) {
 468       // Every profiled call site has a counter.
 469       int count = check_overflow(data->as_CounterData()->count(), java_code_at_bci(bci));
 470 
 471       if (!data->is_ReceiverTypeData()) {
 472         result._receiver_count[0] = 0;  // that's a definite zero
 473       } else { // ReceiverTypeData is a subclass of CounterData
 474         ciReceiverTypeData* call = (ciReceiverTypeData*)data->as_ReceiverTypeData();
 475         // In addition, virtual call sites have receiver type information
 476         int receivers_count_total = 0;
 477         int morphism = 0;
 478         // Precompute morphism for the possible fixup
 479         for (uint i = 0; i < call->row_limit(); i++) {
 480           ciKlass* receiver = call->receiver(i);
 481           if (receiver == nullptr)  continue;
 482           morphism++;
 483         }
 484         int epsilon = 0;
 485         // For a call, it is assumed that either the type of the receiver(s)
 486         // is recorded or an associated counter is incremented, but not both. With
 487         // tiered compilation, however, both can happen due to the interpreter and
 488         // C1 profiling invocations differently. Address that inconsistency here.
 489         if (morphism == 1 && count > 0) {
 490           epsilon = count;
 491           count = 0;
 492         }
 493         for (uint i = 0; i < call->row_limit(); i++) {
 494           ciKlass* receiver = call->receiver(i);
 495           if (receiver == nullptr)  continue;
 496           int rcount = saturated_add(call->receiver_count(i), epsilon);
 497           if (rcount == 0) rcount = 1; // Should be valid value
 498           receivers_count_total = saturated_add(receivers_count_total, rcount);
 499           // Add the receiver to result data.
 500           result.add_receiver(receiver, rcount);
 501           // If we extend profiling to record methods,
 502           // we will set result._method also.
 503         }
 504         // Determine call site's morphism.
 505         // The call site count is 0 with known morphism (only 1 or 2 receivers)
 506         // or < 0 in the case of a type check failure for checkcast, aastore, instanceof.
 507         // The call site count is > 0 in the case of a polymorphic virtual call.
 508         if (morphism > 0 && morphism == result._limit) {
 509            // The morphism <= MorphismLimit.
 510            if ((morphism <  ciCallProfile::MorphismLimit) ||
 511                (morphism == ciCallProfile::MorphismLimit && count == 0)) {
 512 #ifdef ASSERT
 513              if (count > 0) {
 514                this->print_short_name(tty);
 515                tty->print_cr(" @ bci:%d", bci);
 516                this->print_codes();
 517                assert(false, "this call site should not be polymorphic");
 518              }
 519 #endif
 520              result._morphism = morphism;
 521            }
 522         }
 523         // Make the count consistent if this is a call profile. If count is
 524         // zero or less, presume that this is a typecheck profile and
 525         // do nothing.  Otherwise, increase count to be the sum of all
 526         // receiver's counts.
 527         if (count >= 0) {
 528           count = saturated_add(count, receivers_count_total);
 529         }
 530       }
 531       result._count = count;
 532     }
 533   }
 534   return result;
 535 }
 536 
 537 // ------------------------------------------------------------------
 538 // Add new receiver and sort data by receiver's profile count.
 539 void ciCallProfile::add_receiver(ciKlass* receiver, int receiver_count) {
 540   // Add new receiver and sort data by receiver's counts when we have space
 541   // for it otherwise replace the less called receiver (less called receiver
 542   // is placed to the last array element which is not used).
 543   // First array's element contains most called receiver.
 544   int i = _limit;
 545   for (; i > 0 && receiver_count > _receiver_count[i-1]; i--) {
 546     _receiver[i] = _receiver[i-1];
 547     _receiver_count[i] = _receiver_count[i-1];
 548   }
 549   _receiver[i] = receiver;
 550   _receiver_count[i] = receiver_count;
 551   if (_limit < MorphismLimit) _limit++;
 552 }
 553 
 554 
 555 void ciMethod::assert_virtual_call_type_ok(int bci) {
 556   assert(java_code_at_bci(bci) == Bytecodes::_invokevirtual ||
 557          java_code_at_bci(bci) == Bytecodes::_invokeinterface, "unexpected bytecode %s", Bytecodes::name(java_code_at_bci(bci)));
 558 }
 559 
 560 void ciMethod::assert_call_type_ok(int bci) {
 561   assert(java_code_at_bci(bci) == Bytecodes::_invokestatic ||
 562          java_code_at_bci(bci) == Bytecodes::_invokespecial ||
 563          java_code_at_bci(bci) == Bytecodes::_invokedynamic, "unexpected bytecode %s", Bytecodes::name(java_code_at_bci(bci)));
 564 }
 565 
 566 /**
 567  * Check whether profiling provides a type for the argument i to the
 568  * call at bci bci
 569  *
 570  * @param [in]bci         bci of the call
 571  * @param [in]i           argument number
 572  * @param [out]type       profiled type of argument, null if none
 573  * @param [out]ptr_kind   whether always null, never null or maybe null
 574  * @return                true if profiling exists
 575  *
 576  */
 577 bool ciMethod::argument_profiled_type(int bci, int i, ciKlass*& type, ProfilePtrKind& ptr_kind) {
 578   if (MethodData::profile_parameters() && method_data() != nullptr && method_data()->is_mature()) {
 579     ciProfileData* data = method_data()->bci_to_data(bci);
 580     if (data != nullptr) {
 581       if (data->is_VirtualCallTypeData()) {
 582         assert_virtual_call_type_ok(bci);
 583         ciVirtualCallTypeData* call = (ciVirtualCallTypeData*)data->as_VirtualCallTypeData();
 584         if (i >= call->number_of_arguments()) {
 585           return false;
 586         }
 587         type = call->valid_argument_type(i);
 588         ptr_kind = call->argument_ptr_kind(i);
 589         return true;
 590       } else if (data->is_CallTypeData()) {
 591         assert_call_type_ok(bci);
 592         ciCallTypeData* call = (ciCallTypeData*)data->as_CallTypeData();
 593         if (i >= call->number_of_arguments()) {
 594           return false;
 595         }
 596         type = call->valid_argument_type(i);
 597         ptr_kind = call->argument_ptr_kind(i);
 598         return true;
 599       }
 600     }
 601   }
 602   return false;
 603 }
 604 
 605 /**
 606  * Check whether profiling provides a type for the return value from
 607  * the call at bci bci
 608  *
 609  * @param [in]bci         bci of the call
 610  * @param [out]type       profiled type of argument, null if none
 611  * @param [out]ptr_kind   whether always null, never null or maybe null
 612  * @return                true if profiling exists
 613  *
 614  */
 615 bool ciMethod::return_profiled_type(int bci, ciKlass*& type, ProfilePtrKind& ptr_kind) {
 616   if (MethodData::profile_return() && method_data() != nullptr && method_data()->is_mature()) {
 617     ciProfileData* data = method_data()->bci_to_data(bci);
 618     if (data != nullptr) {
 619       if (data->is_VirtualCallTypeData()) {
 620         assert_virtual_call_type_ok(bci);
 621         ciVirtualCallTypeData* call = (ciVirtualCallTypeData*)data->as_VirtualCallTypeData();
 622         if (call->has_return()) {
 623           type = call->valid_return_type();
 624           ptr_kind = call->return_ptr_kind();
 625           return true;
 626         }
 627       } else if (data->is_CallTypeData()) {
 628         assert_call_type_ok(bci);
 629         ciCallTypeData* call = (ciCallTypeData*)data->as_CallTypeData();
 630         if (call->has_return()) {
 631           type = call->valid_return_type();
 632           ptr_kind = call->return_ptr_kind();
 633         }
 634         return true;
 635       }
 636     }
 637   }
 638   return false;
 639 }
 640 
 641 /**
 642  * Check whether profiling provides a type for the parameter i
 643  *
 644  * @param [in]i           parameter number
 645  * @param [out]type       profiled type of parameter, null if none
 646  * @param [out]ptr_kind   whether always null, never null or maybe null
 647  * @return                true if profiling exists
 648  *
 649  */
 650 bool ciMethod::parameter_profiled_type(int i, ciKlass*& type, ProfilePtrKind& ptr_kind) {
 651   if (MethodData::profile_parameters() && method_data() != nullptr && method_data()->is_mature()) {
 652     ciParametersTypeData* parameters = method_data()->parameters_type_data();
 653     if (parameters != nullptr && i < parameters->number_of_parameters()) {
 654       type = parameters->valid_parameter_type(i);
 655       ptr_kind = parameters->parameter_ptr_kind(i);
 656       return true;
 657     }
 658   }
 659   return false;
 660 }
 661 
 662 
 663 // ------------------------------------------------------------------
 664 // ciMethod::find_monomorphic_target
 665 //
 666 // Given a certain calling environment, find the monomorphic target
 667 // for the call.  Return null if the call is not monomorphic in
 668 // its calling environment, or if there are only abstract methods.
 669 // The returned method is never abstract.
 670 // Note: If caller uses a non-null result, it must inform dependencies
 671 // via assert_unique_concrete_method or assert_leaf_type.
 672 ciMethod* ciMethod::find_monomorphic_target(ciInstanceKlass* caller,
 673                                             ciInstanceKlass* callee_holder,
 674                                             ciInstanceKlass* actual_recv,
 675                                             bool check_access) {
 676   check_is_loaded();
 677 
 678   if (actual_recv->is_interface()) {
 679     // %%% We cannot trust interface types, yet.  See bug 6312651.
 680     return nullptr;
 681   }
 682 
 683   ciMethod* root_m = resolve_invoke(caller, actual_recv, check_access, true /* allow_abstract */);
 684   if (root_m == nullptr) {
 685     // Something went wrong looking up the actual receiver method.
 686     return nullptr;
 687   }
 688 
 689   // Make certain quick checks even if UseCHA is false.
 690 
 691   // Is it private or final?
 692   if (root_m->can_be_statically_bound()) {
 693     assert(!root_m->is_abstract(), "sanity");
 694     return root_m;
 695   }
 696 
 697   if (actual_recv->is_leaf_type() && actual_recv == root_m->holder()) {
 698     // Easy case.  There is no other place to put a method, so don't bother
 699     // to go through the VM_ENTRY_MARK and all the rest.
 700     if (root_m->is_abstract()) {
 701       return nullptr;
 702     }
 703     return root_m;
 704   }
 705 
 706   // Array methods (clone, hashCode, etc.) are always statically bound.
 707   // If we were to see an array type here, we'd return root_m.
 708   // However, this method processes only ciInstanceKlasses.  (See 4962591.)
 709   // The inline_native_clone intrinsic narrows Object to T[] properly,
 710   // so there is no need to do the same job here.
 711 
 712   if (!UseCHA)  return nullptr;
 713 
 714   VM_ENTRY_MARK;
 715 
 716   methodHandle target;
 717   {
 718     MutexLocker locker(Compile_lock);
 719     InstanceKlass* context = actual_recv->get_instanceKlass();
 720     if (UseVtableBasedCHA) {
 721       target = methodHandle(THREAD, Dependencies::find_unique_concrete_method(context,
 722                                                                               root_m->get_Method(),
 723                                                                               callee_holder->get_Klass(),
 724                                                                               this->get_Method()));
 725     } else {
 726       if (root_m->is_abstract()) {
 727         return nullptr; // not supported
 728       }
 729       target = methodHandle(THREAD, Dependencies::find_unique_concrete_method(context, root_m->get_Method()));
 730     }
 731     assert(target() == nullptr || !target()->is_abstract(), "not allowed");
 732     // %%% Should upgrade this ciMethod API to look for 1 or 2 concrete methods.
 733   }
 734 
 735 #ifndef PRODUCT
 736   LogTarget(Debug, dependencies) lt;
 737   if (lt.is_enabled() && target() != nullptr && target() != root_m->get_Method()) {
 738     LogStream ls(&lt);
 739     ls.print("found a non-root unique target method");
 740     ls.print_cr("  context = %s", actual_recv->get_Klass()->external_name());
 741     ls.print("  method  = ");
 742     target->print_short_name(&ls);
 743     ls.cr();
 744   }
 745 #endif //PRODUCT
 746 
 747   if (target() == nullptr) {
 748     return nullptr;
 749   }
 750   if (target() == root_m->get_Method()) {
 751     return root_m;
 752   }
 753   if (!root_m->is_public() &&
 754       !root_m->is_protected()) {
 755     // If we are going to reason about inheritance, it's easiest
 756     // if the method in question is public, protected, or private.
 757     // If the answer is not root_m, it is conservatively correct
 758     // to return null, even if the CHA encountered irrelevant
 759     // methods in other packages.
 760     // %%% TO DO: Work out logic for package-private methods
 761     // with the same name but different vtable indexes.
 762     return nullptr;
 763   }
 764   return CURRENT_THREAD_ENV->get_method(target());
 765 }
 766 
 767 // ------------------------------------------------------------------
 768 // ciMethod::can_be_statically_bound
 769 //
 770 // Tries to determine whether a method can be statically bound in some context.
 771 bool ciMethod::can_be_statically_bound(ciInstanceKlass* context) const {
 772   return (holder() == context) && can_be_statically_bound();
 773 }
 774 
 775 // ------------------------------------------------------------------
 776 // ciMethod::can_omit_stack_trace
 777 //
 778 // Tries to determine whether a method can omit stack trace in throw in compiled code.
 779 bool ciMethod::can_omit_stack_trace() const {
 780   if (!StackTraceInThrowable) {
 781     return true; // stack trace is switched off.
 782   }
 783   if (!OmitStackTraceInFastThrow) {
 784     return false; // Have to provide stack trace.
 785   }
 786   return _can_omit_stack_trace;
 787 }
 788 
 789 // ------------------------------------------------------------------
 790 // ciMethod::resolve_invoke
 791 //
 792 // Given a known receiver klass, find the target for the call.
 793 // Return null if the call has no target or the target is abstract.
 794 ciMethod* ciMethod::resolve_invoke(ciKlass* caller, ciKlass* exact_receiver, bool check_access, bool allow_abstract) {
 795   check_is_loaded();
 796   VM_ENTRY_MARK;
 797 
 798   Klass* caller_klass = caller->get_Klass();
 799   Klass* recv         = exact_receiver->get_Klass();
 800   Klass* resolved     = holder()->get_Klass();
 801   Symbol* h_name      = name()->get_symbol();
 802   Symbol* h_signature = signature()->get_symbol();
 803 
 804   LinkInfo link_info(resolved, h_name, h_signature, caller_klass,
 805                      check_access ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
 806                      check_access ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
 807   Method* m = nullptr;
 808   // Only do exact lookup if receiver klass has been linked.  Otherwise,
 809   // the vtable has not been setup, and the LinkResolver will fail.
 810   if (recv->is_array_klass()
 811        ||
 812       (InstanceKlass::cast(recv)->is_linked() && !exact_receiver->is_interface())) {
 813     if (holder()->is_interface()) {
 814       m = LinkResolver::resolve_interface_call_or_null(recv, link_info);
 815     } else {
 816       m = LinkResolver::resolve_virtual_call_or_null(recv, link_info);
 817     }
 818   }
 819 
 820   if (m == nullptr) {
 821     // Return null only if there was a problem with lookup (uninitialized class, etc.)
 822     return nullptr;
 823   }
 824 
 825   ciMethod* result = this;
 826   if (m != get_Method()) {
 827     result = CURRENT_THREAD_ENV->get_method(m);
 828   }
 829 
 830   if (result->is_abstract() && !allow_abstract) {
 831     // Don't return abstract methods because they aren't optimizable or interesting.
 832     return nullptr;
 833   }
 834   return result;
 835 }
 836 
 837 // ------------------------------------------------------------------
 838 // ciMethod::resolve_vtable_index
 839 //
 840 // Given a known receiver klass, find the vtable index for the call.
 841 // Return Method::invalid_vtable_index if the vtable_index is unknown.
 842 int ciMethod::resolve_vtable_index(ciKlass* caller, ciKlass* receiver) {
 843    check_is_loaded();
 844 
 845    int vtable_index = Method::invalid_vtable_index;
 846    // Only do lookup if receiver klass has been linked.  Otherwise,
 847    // the vtable has not been setup, and the LinkResolver will fail.
 848    if (!receiver->is_interface()
 849        && (!receiver->is_instance_klass() ||
 850            receiver->as_instance_klass()->is_linked())) {
 851      VM_ENTRY_MARK;
 852 
 853      Klass* caller_klass = caller->get_Klass();
 854      Klass* recv         = receiver->get_Klass();
 855      Symbol* h_name = name()->get_symbol();
 856      Symbol* h_signature = signature()->get_symbol();
 857 
 858      LinkInfo link_info(recv, h_name, h_signature, caller_klass);
 859      vtable_index = LinkResolver::resolve_virtual_vtable_index(recv, link_info);
 860      if (vtable_index == Method::nonvirtual_vtable_index) {
 861        // A statically bound method.  Return "no such index".
 862        vtable_index = Method::invalid_vtable_index;
 863      }
 864    }
 865 
 866    return vtable_index;
 867 }
 868 
 869 // ------------------------------------------------------------------
 870 // ciMethod::get_field_at_bci
 871 ciField* ciMethod::get_field_at_bci(int bci, bool &will_link) {
 872   ciBytecodeStream iter(this);
 873   iter.reset_to_bci(bci);
 874   iter.next();
 875   return iter.get_field(will_link);
 876 }
 877 
 878 // ------------------------------------------------------------------
 879 // ciMethod::get_method_at_bci
 880 ciMethod* ciMethod::get_method_at_bci(int bci, bool &will_link, ciSignature* *declared_signature) {
 881   ciBytecodeStream iter(this);
 882   iter.reset_to_bci(bci);
 883   iter.next();
 884   return iter.get_method(will_link, declared_signature);
 885 }
 886 
 887 // ------------------------------------------------------------------
 888 ciKlass* ciMethod::get_declared_method_holder_at_bci(int bci) {
 889   ciBytecodeStream iter(this);
 890   iter.reset_to_bci(bci);
 891   iter.next();
 892   return iter.get_declared_method_holder();
 893 }
 894 
 895 // ------------------------------------------------------------------
 896 // Adjust a CounterData count to be commensurate with
 897 // interpreter_invocation_count.  If the MDO exists for
 898 // only 25% of the time the method exists, then the
 899 // counts in the MDO should be scaled by 4X, so that
 900 // they can be usefully and stably compared against the
 901 // invocation counts in methods.
 902 int ciMethod::scale_count(int count, float prof_factor) {
 903   if (count > 0 && method_data() != nullptr) {
 904     int counter_life = method_data()->invocation_count();
 905     int method_life = interpreter_invocation_count();
 906     if (method_life < counter_life) { // may happen because of the snapshot timing
 907       method_life = counter_life;
 908     }
 909     if (counter_life > 0) {
 910       count = (int)((double)count * prof_factor * method_life / counter_life + 0.5);
 911       count = (count > 0) ? count : 1;
 912     } else {
 913       count = 1;
 914     }
 915   }
 916   return count;
 917 }
 918 
 919 
 920 // ------------------------------------------------------------------
 921 // ciMethod::is_special_get_caller_class_method
 922 //
 923 bool ciMethod::is_ignored_by_security_stack_walk() const {
 924   check_is_loaded();
 925   VM_ENTRY_MARK;
 926   return get_Method()->is_ignored_by_security_stack_walk();
 927 }
 928 
 929 // ------------------------------------------------------------------
 930 // ciMethod::needs_clinit_barrier
 931 //
 932 bool ciMethod::needs_clinit_barrier() const {
 933   check_is_loaded();
 934   return is_static() && !holder()->is_initialized();
 935 }
 936 
 937 // ------------------------------------------------------------------
 938 // invokedynamic support
 939 
 940 // ------------------------------------------------------------------
 941 // ciMethod::is_method_handle_intrinsic
 942 //
 943 // Return true if the method is an instance of the JVM-generated
 944 // signature-polymorphic MethodHandle methods, _invokeBasic, _linkToVirtual, etc.
 945 bool ciMethod::is_method_handle_intrinsic() const {
 946   vmIntrinsics::ID iid = _intrinsic_id;  // do not check if loaded
 947   return (MethodHandles::is_signature_polymorphic(iid) &&
 948           MethodHandles::is_signature_polymorphic_intrinsic(iid));
 949 }
 950 
 951 // ------------------------------------------------------------------
 952 // ciMethod::is_compiled_lambda_form
 953 //
 954 // Return true if the method is a generated MethodHandle adapter.
 955 // These are built by Java code.
 956 bool ciMethod::is_compiled_lambda_form() const {
 957   vmIntrinsics::ID iid = _intrinsic_id;  // do not check if loaded
 958   return iid == vmIntrinsics::_compiledLambdaForm;
 959 }
 960 
 961 // ------------------------------------------------------------------
 962 // ciMethod::is_object_initializer
 963 //
 964 bool ciMethod::is_object_initializer() const {
 965    return name() == ciSymbols::object_initializer_name();
 966 }
 967 
 968 // ------------------------------------------------------------------
 969 // ciMethod::has_member_arg
 970 //
 971 // Return true if the method is a linker intrinsic like _linkToVirtual.
 972 // These are built by the JVM.
 973 bool ciMethod::has_member_arg() const {
 974   vmIntrinsics::ID iid = _intrinsic_id;  // do not check if loaded
 975   return (MethodHandles::is_signature_polymorphic(iid) &&
 976           MethodHandles::has_member_arg(iid));
 977 }
 978 
 979 // ------------------------------------------------------------------
 980 // ciMethod::ensure_method_data
 981 //
 982 // Generate new MethodData* objects at compile time.
 983 // Return true if allocation was successful or no MDO is required.
 984 bool ciMethod::ensure_method_data(const methodHandle& h_m) {
 985   EXCEPTION_CONTEXT;
 986   if (is_native() || is_abstract() || h_m()->is_accessor()) {
 987     return true;
 988   }
 989   if (h_m()->method_data() == nullptr) {
 990     Method::build_profiling_method_data(h_m, THREAD);
 991     if (HAS_PENDING_EXCEPTION) {
 992       CLEAR_PENDING_EXCEPTION;
 993     }
 994   }
 995   if (h_m()->method_data() != nullptr) {
 996     _method_data = CURRENT_ENV->get_method_data(h_m()->method_data());
 997     return _method_data->load_data();
 998   } else {
 999     _method_data = CURRENT_ENV->get_empty_methodData();
1000     return false;
1001   }
1002 }
1003 
1004 // public, retroactive version
1005 bool ciMethod::ensure_method_data() {
1006   bool result = true;
1007   if (_method_data == nullptr || _method_data->is_empty()) {
1008     GUARDED_VM_ENTRY({
1009       methodHandle mh(Thread::current(), get_Method());
1010       result = ensure_method_data(mh);
1011     });
1012   }
1013   return result;
1014 }
1015 
1016 
1017 // ------------------------------------------------------------------
1018 // ciMethod::method_data
1019 //
1020 ciMethodData* ciMethod::method_data() {
1021   if (_method_data != nullptr) {
1022     return _method_data;
1023   }
1024   VM_ENTRY_MARK;
1025   ciEnv* env = CURRENT_ENV;
1026   Thread* my_thread = JavaThread::current();
1027   methodHandle h_m(my_thread, get_Method());
1028 
1029   if (h_m()->method_data() != nullptr) {
1030     _method_data = CURRENT_ENV->get_method_data(h_m()->method_data());
1031     _method_data->load_data();
1032   } else {
1033     _method_data = CURRENT_ENV->get_empty_methodData();
1034   }
1035   return _method_data;
1036 
1037 }
1038 
1039 // ------------------------------------------------------------------
1040 // ciMethod::method_data_or_null
1041 // Returns a pointer to ciMethodData if MDO exists on the VM side,
1042 // null otherwise.
1043 ciMethodData* ciMethod::method_data_or_null() {
1044   ciMethodData *md = method_data();
1045   if (md->is_empty()) {
1046     return nullptr;
1047   }
1048   return md;
1049 }
1050 
1051 // ------------------------------------------------------------------
1052 // ciMethod::ensure_method_counters
1053 //
1054 MethodCounters* ciMethod::ensure_method_counters() {
1055   check_is_loaded();
1056   VM_ENTRY_MARK;
1057   methodHandle mh(THREAD, get_Method());
1058   MethodCounters* method_counters = mh->get_method_counters(CHECK_NULL);
1059   return method_counters;
1060 }
1061 
1062 // ------------------------------------------------------------------
1063 // ciMethod::has_option
1064 //
1065 bool ciMethod::has_option(enum CompileCommand option) {
1066   check_is_loaded();
1067   VM_ENTRY_MARK;
1068   methodHandle mh(THREAD, get_Method());
1069   return CompilerOracle::has_option(mh, option);
1070 }
1071 
1072 // ------------------------------------------------------------------
1073 // ciMethod::has_option_value
1074 //
1075 bool ciMethod::has_option_value(enum CompileCommand option, double& value) {
1076   check_is_loaded();
1077   VM_ENTRY_MARK;
1078   methodHandle mh(THREAD, get_Method());
1079   return CompilerOracle::has_option_value(mh, option, value);
1080 }
1081 // ------------------------------------------------------------------
1082 // ciMethod::can_be_compiled
1083 //
1084 // Have previous compilations of this method succeeded?
1085 bool ciMethod::can_be_compiled() {
1086   check_is_loaded();
1087   ciEnv* env = CURRENT_ENV;
1088   if (is_c1_compile(env->comp_level())) {
1089     return _is_c1_compilable;
1090   }
1091   return _is_c2_compilable;
1092 }
1093 
1094 // ------------------------------------------------------------------
1095 // ciMethod::has_compiled_code
1096 bool ciMethod::has_compiled_code() {
1097   return inline_instructions_size() > 0;
1098 }
1099 
1100 int ciMethod::highest_osr_comp_level() {
1101   check_is_loaded();
1102   VM_ENTRY_MARK;
1103   return get_Method()->highest_osr_comp_level();
1104 }
1105 
1106 // ------------------------------------------------------------------
1107 // ciMethod::code_size_for_inlining
1108 //
1109 // Code size for inlining decisions.  This method returns a code
1110 // size of 1 for methods which has the ForceInline annotation.
1111 int ciMethod::code_size_for_inlining() {
1112   check_is_loaded();
1113   if (get_Method()->force_inline()) {
1114     return 1;
1115   }
1116   return code_size();
1117 }
1118 
1119 // ------------------------------------------------------------------
1120 // ciMethod::inline_instructions_size
1121 //
1122 // This is a rough metric for "fat" methods, compared before inlining
1123 // with InlineSmallCode.  The CodeBlob::code_size accessor includes
1124 // junk like exception handler, stubs, and constant table, which are
1125 // not highly relevant to an inlined method.  So we use the more
1126 // specific accessor nmethod::insts_size.
1127 // Also some instructions inside the code are excluded from inline
1128 // heuristic (e.g. post call nop instructions; see InlineSkippedInstructionsCounter)
1129 int ciMethod::inline_instructions_size() {
1130   if (_inline_instructions_size == -1) {
1131     GUARDED_VM_ENTRY(
1132       CompiledMethod* code = get_Method()->code();
1133       if (code != nullptr && (code->comp_level() == CompLevel_full_optimization)) {
1134         int isize = code->insts_end() - code->verified_entry_point() - code->skipped_instructions_size();
1135         _inline_instructions_size = isize > 0 ? isize : 0;
1136       } else {
1137         _inline_instructions_size = 0;
1138       }
1139     );
1140   }
1141   return _inline_instructions_size;
1142 }
1143 
1144 // ------------------------------------------------------------------
1145 // ciMethod::log_nmethod_identity
1146 void ciMethod::log_nmethod_identity(xmlStream* log) {
1147   GUARDED_VM_ENTRY(
1148     CompiledMethod* code = get_Method()->code();
1149     if (code != nullptr) {
1150       code->log_identity(log);
1151     }
1152   )
1153 }
1154 
1155 // ------------------------------------------------------------------
1156 // ciMethod::is_not_reached
1157 bool ciMethod::is_not_reached(int bci) {
1158   check_is_loaded();
1159   VM_ENTRY_MARK;
1160   return Interpreter::is_not_reached(
1161                methodHandle(THREAD, get_Method()), bci);
1162 }
1163 
1164 // ------------------------------------------------------------------
1165 // ciMethod::was_never_executed
1166 bool ciMethod::was_executed_more_than(int times) {
1167   VM_ENTRY_MARK;
1168   return get_Method()->was_executed_more_than(times);
1169 }
1170 
1171 // ------------------------------------------------------------------
1172 // ciMethod::has_unloaded_classes_in_signature
1173 bool ciMethod::has_unloaded_classes_in_signature() {
1174   // ciSignature is resolved against some accessing class and
1175   // signature classes aren't required to be local. As a benefit,
1176   // it makes signature classes visible through loader constraints.
1177   // So, encountering an unloaded class signals it is absent both in
1178   // the callee (local) and caller contexts.
1179   return signature()->has_unloaded_classes();
1180 }
1181 
1182 // ------------------------------------------------------------------
1183 // ciMethod::is_klass_loaded
1184 bool ciMethod::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
1185   VM_ENTRY_MARK;
1186   return get_Method()->is_klass_loaded(refinfo_index, bc, must_be_resolved);
1187 }
1188 
1189 // ------------------------------------------------------------------
1190 // ciMethod::check_call
1191 bool ciMethod::check_call(int refinfo_index, bool is_static) const {
1192   // This method is used only in C2 from InlineTree::ok_to_inline,
1193   // and is only used under -Xcomp.
1194   // It appears to fail when applied to an invokeinterface call site.
1195   // FIXME: Remove this method and resolve_method_statically; refactor to use the other LinkResolver entry points.
1196   VM_ENTRY_MARK;
1197   {
1198     ExceptionMark em(THREAD);
1199     HandleMark hm(THREAD);
1200     constantPoolHandle pool (THREAD, get_Method()->constants());
1201     Bytecodes::Code code = (is_static ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual);
1202     Method* spec_method = LinkResolver::resolve_method_statically(code, pool, refinfo_index, THREAD);
1203     if (HAS_PENDING_EXCEPTION) {
1204       CLEAR_PENDING_EXCEPTION;
1205       return false;
1206     } else {
1207       return (spec_method->is_static() == is_static);
1208     }
1209   }
1210   return false;
1211 }
1212 // ------------------------------------------------------------------
1213 // ciMethod::print_codes
1214 //
1215 // Print the bytecodes for this method.
1216 void ciMethod::print_codes_on(outputStream* st) {
1217   check_is_loaded();
1218   GUARDED_VM_ENTRY(get_Method()->print_codes_on(st);)
1219 }
1220 
1221 
1222 #define FETCH_FLAG_FROM_VM(flag_accessor) { \
1223   check_is_loaded(); \
1224   VM_ENTRY_MARK; \
1225   return get_Method()->flag_accessor(); \
1226 }
1227 
1228 bool ciMethod::has_loops      () const {         FETCH_FLAG_FROM_VM(has_loops); }
1229 bool ciMethod::has_jsrs       () const {         FETCH_FLAG_FROM_VM(has_jsrs);  }
1230 bool ciMethod::is_getter      () const {         FETCH_FLAG_FROM_VM(is_getter); }
1231 bool ciMethod::is_setter      () const {         FETCH_FLAG_FROM_VM(is_setter); }
1232 bool ciMethod::is_accessor    () const {         FETCH_FLAG_FROM_VM(is_accessor); }
1233 bool ciMethod::is_initializer () const {         FETCH_FLAG_FROM_VM(is_initializer); }
1234 bool ciMethod::is_empty       () const {         FETCH_FLAG_FROM_VM(is_empty_method); }
1235 
1236 bool ciMethod::is_boxing_method() const {
1237   if (intrinsic_id() != vmIntrinsics::_none && holder()->is_box_klass()) {
1238     switch (intrinsic_id()) {
1239       case vmIntrinsics::_Boolean_valueOf:
1240       case vmIntrinsics::_Byte_valueOf:
1241       case vmIntrinsics::_Character_valueOf:
1242       case vmIntrinsics::_Short_valueOf:
1243       case vmIntrinsics::_Integer_valueOf:
1244       case vmIntrinsics::_Long_valueOf:
1245       case vmIntrinsics::_Float_valueOf:
1246       case vmIntrinsics::_Double_valueOf:
1247         return true;
1248       default:
1249         return false;
1250     }
1251   }
1252   return false;
1253 }
1254 
1255 bool ciMethod::is_unboxing_method() const {
1256   if (intrinsic_id() != vmIntrinsics::_none && holder()->is_box_klass()) {
1257     switch (intrinsic_id()) {
1258       case vmIntrinsics::_booleanValue:
1259       case vmIntrinsics::_byteValue:
1260       case vmIntrinsics::_charValue:
1261       case vmIntrinsics::_shortValue:
1262       case vmIntrinsics::_intValue:
1263       case vmIntrinsics::_longValue:
1264       case vmIntrinsics::_floatValue:
1265       case vmIntrinsics::_doubleValue:
1266         return true;
1267       default:
1268         return false;
1269     }
1270   }
1271   return false;
1272 }
1273 
1274 bool ciMethod::is_vector_method() const {
1275   return (holder() == ciEnv::current()->vector_VectorSupport_klass()) &&
1276          (intrinsic_id() != vmIntrinsics::_none);
1277 }
1278 
1279 BCEscapeAnalyzer  *ciMethod::get_bcea() {
1280 #ifdef COMPILER2
1281   if (_bcea == nullptr) {
1282     _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, nullptr);
1283   }
1284   return _bcea;
1285 #else // COMPILER2
1286   ShouldNotReachHere();
1287   return nullptr;
1288 #endif // COMPILER2
1289 }
1290 
1291 ciMethodBlocks  *ciMethod::get_method_blocks() {
1292   if (_method_blocks == nullptr) {
1293     Arena *arena = CURRENT_ENV->arena();
1294     _method_blocks = new (arena) ciMethodBlocks(arena, this);
1295   }
1296   return _method_blocks;
1297 }
1298 
1299 #undef FETCH_FLAG_FROM_VM
1300 
1301 void ciMethod::dump_name_as_ascii(outputStream* st, Method* method) {
1302   st->print("%s %s %s",
1303             CURRENT_ENV->replay_name(method->method_holder()),
1304             method->name()->as_quoted_ascii(),
1305             method->signature()->as_quoted_ascii());
1306 }
1307 
1308 void ciMethod::dump_name_as_ascii(outputStream* st) {
1309   Method* method = get_Method();
1310   dump_name_as_ascii(st, method);
1311 }
1312 
1313 void ciMethod::dump_replay_data(outputStream* st) {
1314   ResourceMark rm;
1315   Method* method = get_Method();
1316   if (MethodHandles::is_signature_polymorphic_method(method)) {
1317     // ignore for now
1318     return;
1319   }
1320   MethodCounters* mcs = method->method_counters();
1321   st->print("ciMethod ");
1322   dump_name_as_ascii(st);
1323   st->print_cr(" %d %d %d %d %d",
1324                mcs == nullptr ? 0 : mcs->invocation_counter()->raw_counter(),
1325                mcs == nullptr ? 0 : mcs->backedge_counter()->raw_counter(),
1326                interpreter_invocation_count(),
1327                interpreter_throwout_count(),
1328                _inline_instructions_size);
1329 }
1330 
1331 // ------------------------------------------------------------------
1332 // ciMethod::print_codes
1333 //
1334 // Print a range of the bytecodes for this method.
1335 void ciMethod::print_codes_on(int from, int to, outputStream* st) {
1336   check_is_loaded();
1337   GUARDED_VM_ENTRY(get_Method()->print_codes_on(from, to, st);)
1338 }
1339 
1340 // ------------------------------------------------------------------
1341 // ciMethod::print_name
1342 //
1343 // Print the name of this method, including signature and some flags.
1344 void ciMethod::print_name(outputStream* st) {
1345   check_is_loaded();
1346   GUARDED_VM_ENTRY(get_Method()->print_name(st);)
1347 }
1348 
1349 // ------------------------------------------------------------------
1350 // ciMethod::print_short_name
1351 //
1352 // Print the name of this method, without signature.
1353 void ciMethod::print_short_name(outputStream* st) {
1354   if (is_loaded()) {
1355     GUARDED_VM_ENTRY(get_Method()->print_short_name(st););
1356   } else {
1357     // Fall back if method is not loaded.
1358     holder()->print_name_on(st);
1359     st->print("::");
1360     name()->print_symbol_on(st);
1361     if (WizardMode)
1362       signature()->as_symbol()->print_symbol_on(st);
1363   }
1364 }
1365 
1366 // ------------------------------------------------------------------
1367 // ciMethod::print_impl
1368 //
1369 // Implementation of the print method.
1370 void ciMethod::print_impl(outputStream* st) {
1371   ciMetadata::print_impl(st);
1372   st->print(" name=");
1373   name()->print_symbol_on(st);
1374   st->print(" holder=");
1375   holder()->print_name_on(st);
1376   st->print(" signature=");
1377   signature()->as_symbol()->print_symbol_on(st);
1378   if (is_loaded()) {
1379     st->print(" loaded=true");
1380     st->print(" arg_size=%d", arg_size());
1381     st->print(" flags=");
1382     flags().print_member_flags(st);
1383   } else {
1384     st->print(" loaded=false");
1385   }
1386 }
1387 
1388 // ------------------------------------------------------------------
1389 
1390 static BasicType erase_to_word_type(BasicType bt) {
1391   if (is_subword_type(bt))   return T_INT;
1392   if (is_reference_type(bt)) return T_OBJECT;
1393   return bt;
1394 }
1395 
1396 static bool basic_types_match(ciType* t1, ciType* t2) {
1397   if (t1 == t2)  return true;
1398   return erase_to_word_type(t1->basic_type()) == erase_to_word_type(t2->basic_type());
1399 }
1400 
1401 bool ciMethod::is_consistent_info(ciMethod* declared_method, ciMethod* resolved_method) {
1402   bool invoke_through_mh_intrinsic = declared_method->is_method_handle_intrinsic() &&
1403                                   !resolved_method->is_method_handle_intrinsic();
1404 
1405   if (!invoke_through_mh_intrinsic) {
1406     // Method name & descriptor should stay the same.
1407     // Signatures may reference unloaded types and thus they may be not strictly equal.
1408     ciSymbol* declared_signature = declared_method->signature()->as_symbol();
1409     ciSymbol* resolved_signature = resolved_method->signature()->as_symbol();
1410 
1411     return (declared_method->name()->equals(resolved_method->name())) &&
1412            (declared_signature->equals(resolved_signature));
1413   }
1414 
1415   ciMethod* linker = declared_method;
1416   ciMethod* target = resolved_method;
1417   // Linkers have appendix argument which is not passed to callee.
1418   int has_appendix = MethodHandles::has_member_arg(linker->intrinsic_id()) ? 1 : 0;
1419   if (linker->arg_size() != (target->arg_size() + has_appendix)) {
1420     return false; // argument slot count mismatch
1421   }
1422 
1423   ciSignature* linker_sig = linker->signature();
1424   ciSignature* target_sig = target->signature();
1425 
1426   if (linker_sig->count() + (linker->is_static() ? 0 : 1) !=
1427       target_sig->count() + (target->is_static() ? 0 : 1) + has_appendix) {
1428     return false; // argument count mismatch
1429   }
1430 
1431   int sbase = 0, rbase = 0;
1432   switch (linker->intrinsic_id()) {
1433     case vmIntrinsics::_linkToVirtual:
1434     case vmIntrinsics::_linkToInterface:
1435     case vmIntrinsics::_linkToSpecial: {
1436       if (target->is_static()) {
1437         return false;
1438       }
1439       if (linker_sig->type_at(0)->is_primitive_type()) {
1440         return false;  // receiver should be an oop
1441       }
1442       sbase = 1; // skip receiver
1443       break;
1444     }
1445     case vmIntrinsics::_linkToStatic: {
1446       if (!target->is_static()) {
1447         return false;
1448       }
1449       break;
1450     }
1451     case vmIntrinsics::_invokeBasic: {
1452       if (target->is_static()) {
1453         if (target_sig->type_at(0)->is_primitive_type()) {
1454           return false; // receiver should be an oop
1455         }
1456         rbase = 1; // skip receiver
1457       }
1458       break;
1459     }
1460     default:
1461       break;
1462   }
1463   assert(target_sig->count() - rbase == linker_sig->count() - sbase - has_appendix, "argument count mismatch");
1464   int arg_count = target_sig->count() - rbase;
1465   for (int i = 0; i < arg_count; i++) {
1466     if (!basic_types_match(linker_sig->type_at(sbase + i), target_sig->type_at(rbase + i))) {
1467       return false;
1468     }
1469   }
1470   // Only check the return type if the symbolic info has non-void return type.
1471   // I.e. the return value of the resolved method can be dropped.
1472   if (!linker->return_type()->is_void() &&
1473       !basic_types_match(linker->return_type(), target->return_type())) {
1474     return false;
1475   }
1476   return true; // no mismatch found
1477 }
1478 
1479 // ------------------------------------------------------------------