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