1 /*
   2  * Copyright (c) 1999, 2024, 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/compilerOracle.hpp"
  40 #include "compiler/methodLiveness.hpp"
  41 #include "interpreter/interpreter.hpp"
  42 #include "interpreter/linkResolver.hpp"
  43 #include "interpreter/oopMapCache.hpp"
  44 #include "logging/log.hpp"
  45 #include "logging/logStream.hpp"
  46 #include "memory/allocation.inline.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "oops/generateOopMap.hpp"
  49 #include "oops/method.inline.hpp"
  50 #include "oops/oop.inline.hpp"
  51 #include "prims/methodHandles.hpp"
  52 #include "runtime/deoptimization.hpp"
  53 #include "runtime/handles.inline.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       if (VM_Version::profile_all_receivers_at_type_check()) {
 446         return (c < 0 ? max_jint : c); // always non-negative
 447       }
 448       return (c > 0 ? min_jint : c); // always non-positive
 449     }
 450     default: {
 451       assert(Bytecodes::is_invoke(code), "%s", Bytecodes::name(code));
 452       return (c < 0 ? max_jint : c); // always non-negative
 453     }
 454   }
 455 }
 456 
 457 
 458 // ------------------------------------------------------------------
 459 // ciMethod::call_profile_at_bci
 460 //
 461 // Get the ciCallProfile for the invocation of this method.
 462 // Also reports receiver types for non-call type checks (if TypeProfileCasts).
 463 ciCallProfile ciMethod::call_profile_at_bci(int bci) {
 464   ResourceMark rm;
 465   ciCallProfile result;
 466   if (method_data() != nullptr && method_data()->is_mature()) {
 467     ciProfileData* data = method_data()->bci_to_data(bci);
 468     if (data != nullptr && data->is_CounterData()) {
 469       // Every profiled call site has a counter.
 470       int count = check_overflow(data->as_CounterData()->count(), java_code_at_bci(bci));
 471 
 472       if (!data->is_ReceiverTypeData()) {
 473         result._receiver_count[0] = 0;  // that's a definite zero
 474       } else { // ReceiverTypeData is a subclass of CounterData
 475         ciReceiverTypeData* call = (ciReceiverTypeData*)data->as_ReceiverTypeData();
 476         // In addition, virtual call sites have receiver type information
 477         int receivers_count_total = 0;
 478         int morphism = 0;
 479         // Precompute morphism for the possible fixup
 480         for (uint i = 0; i < call->row_limit(); i++) {
 481           ciKlass* receiver = call->receiver(i);
 482           if (receiver == nullptr)  continue;
 483           morphism++;
 484         }
 485         int epsilon = 0;
 486         // For a call, it is assumed that either the type of the receiver(s)
 487         // is recorded or an associated counter is incremented, but not both. With
 488         // tiered compilation, however, both can happen due to the interpreter and
 489         // C1 profiling invocations differently. Address that inconsistency here.
 490         if (morphism == 1 && count > 0) {
 491           epsilon = count;
 492           count = 0;
 493         }
 494         for (uint i = 0; i < call->row_limit(); i++) {
 495           ciKlass* receiver = call->receiver(i);
 496           if (receiver == nullptr)  continue;
 497           int rcount = saturated_add(call->receiver_count(i), epsilon);
 498           if (rcount == 0) rcount = 1; // Should be valid value
 499           receivers_count_total = saturated_add(receivers_count_total, rcount);
 500           // Add the receiver to result data.
 501           result.add_receiver(receiver, rcount);
 502           // If we extend profiling to record methods,
 503           // we will set result._method also.
 504         }
 505         // Determine call site's morphism.
 506         // The call site count is 0 with known morphism (only 1 or 2 receivers)
 507         // or < 0 in the case of a type check failure for checkcast, aastore, instanceof.
 508         // The call site count is > 0 in the case of a polymorphic virtual call.
 509         if (morphism > 0 && morphism == result._limit) {
 510            // The morphism <= MorphismLimit.
 511            if ((morphism <  ciCallProfile::MorphismLimit) ||
 512                (morphism == ciCallProfile::MorphismLimit && count == 0)) {
 513 #ifdef ASSERT
 514              if (count > 0) {
 515                this->print_short_name(tty);
 516                tty->print_cr(" @ bci:%d", bci);
 517                this->print_codes();
 518                assert(false, "this call site should not be polymorphic");
 519              }
 520 #endif
 521              result._morphism = morphism;
 522            }
 523         }
 524         // Make the count consistent if this is a call profile. If count is
 525         // zero or less, presume that this is a typecheck profile and
 526         // do nothing.  Otherwise, increase count to be the sum of all
 527         // receiver's counts.
 528         if (count >= 0) {
 529           count = saturated_add(count, receivers_count_total);
 530         }
 531       }
 532       result._count = count;
 533     }
 534   }
 535   return result;
 536 }
 537 
 538 // ------------------------------------------------------------------
 539 // Add new receiver and sort data by receiver's profile count.
 540 void ciCallProfile::add_receiver(ciKlass* receiver, int receiver_count) {
 541   // Add new receiver and sort data by receiver's counts when we have space
 542   // for it otherwise replace the less called receiver (less called receiver
 543   // is placed to the last array element which is not used).
 544   // First array's element contains most called receiver.
 545   int i = _limit;
 546   for (; i > 0 && receiver_count > _receiver_count[i-1]; i--) {
 547     _receiver[i] = _receiver[i-1];
 548     _receiver_count[i] = _receiver_count[i-1];
 549   }
 550   _receiver[i] = receiver;
 551   _receiver_count[i] = receiver_count;
 552   if (_limit < MorphismLimit) _limit++;
 553 }
 554 
 555 
 556 void ciMethod::assert_virtual_call_type_ok(int bci) {
 557   assert(java_code_at_bci(bci) == Bytecodes::_invokevirtual ||
 558          java_code_at_bci(bci) == Bytecodes::_invokeinterface, "unexpected bytecode %s", Bytecodes::name(java_code_at_bci(bci)));
 559 }
 560 
 561 void ciMethod::assert_call_type_ok(int bci) {
 562   assert(java_code_at_bci(bci) == Bytecodes::_invokestatic ||
 563          java_code_at_bci(bci) == Bytecodes::_invokespecial ||
 564          java_code_at_bci(bci) == Bytecodes::_invokedynamic, "unexpected bytecode %s", Bytecodes::name(java_code_at_bci(bci)));
 565 }
 566 
 567 /**
 568  * Check whether profiling provides a type for the argument i to the
 569  * call at bci bci
 570  *
 571  * @param [in]bci         bci of the call
 572  * @param [in]i           argument number
 573  * @param [out]type       profiled type of argument, null if none
 574  * @param [out]ptr_kind   whether always null, never null or maybe null
 575  * @return                true if profiling exists
 576  *
 577  */
 578 bool ciMethod::argument_profiled_type(int bci, int i, ciKlass*& type, ProfilePtrKind& ptr_kind) {
 579   if (MethodData::profile_parameters() && method_data() != nullptr && method_data()->is_mature()) {
 580     ciProfileData* data = method_data()->bci_to_data(bci);
 581     if (data != nullptr) {
 582       if (data->is_VirtualCallTypeData()) {
 583         assert_virtual_call_type_ok(bci);
 584         ciVirtualCallTypeData* call = (ciVirtualCallTypeData*)data->as_VirtualCallTypeData();
 585         if (i >= call->number_of_arguments()) {
 586           return false;
 587         }
 588         type = call->valid_argument_type(i);
 589         ptr_kind = call->argument_ptr_kind(i);
 590         return true;
 591       } else if (data->is_CallTypeData()) {
 592         assert_call_type_ok(bci);
 593         ciCallTypeData* call = (ciCallTypeData*)data->as_CallTypeData();
 594         if (i >= call->number_of_arguments()) {
 595           return false;
 596         }
 597         type = call->valid_argument_type(i);
 598         ptr_kind = call->argument_ptr_kind(i);
 599         return true;
 600       }
 601     }
 602   }
 603   return false;
 604 }
 605 
 606 /**
 607  * Check whether profiling provides a type for the return value from
 608  * the call at bci bci
 609  *
 610  * @param [in]bci         bci of the call
 611  * @param [out]type       profiled type of argument, null if none
 612  * @param [out]ptr_kind   whether always null, never null or maybe null
 613  * @return                true if profiling exists
 614  *
 615  */
 616 bool ciMethod::return_profiled_type(int bci, ciKlass*& type, ProfilePtrKind& ptr_kind) {
 617   if (MethodData::profile_return() && method_data() != nullptr && method_data()->is_mature()) {
 618     ciProfileData* data = method_data()->bci_to_data(bci);
 619     if (data != nullptr) {
 620       if (data->is_VirtualCallTypeData()) {
 621         assert_virtual_call_type_ok(bci);
 622         ciVirtualCallTypeData* call = (ciVirtualCallTypeData*)data->as_VirtualCallTypeData();
 623         if (call->has_return()) {
 624           type = call->valid_return_type();
 625           ptr_kind = call->return_ptr_kind();
 626           return true;
 627         }
 628       } else if (data->is_CallTypeData()) {
 629         assert_call_type_ok(bci);
 630         ciCallTypeData* call = (ciCallTypeData*)data->as_CallTypeData();
 631         if (call->has_return()) {
 632           type = call->valid_return_type();
 633           ptr_kind = call->return_ptr_kind();
 634         }
 635         return true;
 636       }
 637     }
 638   }
 639   return false;
 640 }
 641 
 642 /**
 643  * Check whether profiling provides a type for the parameter i
 644  *
 645  * @param [in]i           parameter number
 646  * @param [out]type       profiled type of parameter, null if none
 647  * @param [out]ptr_kind   whether always null, never null or maybe null
 648  * @return                true if profiling exists
 649  *
 650  */
 651 bool ciMethod::parameter_profiled_type(int i, ciKlass*& type, ProfilePtrKind& ptr_kind) {
 652   if (MethodData::profile_parameters() && method_data() != nullptr && method_data()->is_mature()) {
 653     ciParametersTypeData* parameters = method_data()->parameters_type_data();
 654     if (parameters != nullptr && i < parameters->number_of_parameters()) {
 655       type = parameters->valid_parameter_type(i);
 656       ptr_kind = parameters->parameter_ptr_kind(i);
 657       return true;
 658     }
 659   }
 660   return false;
 661 }
 662 
 663 
 664 // ------------------------------------------------------------------
 665 // ciMethod::find_monomorphic_target
 666 //
 667 // Given a certain calling environment, find the monomorphic target
 668 // for the call.  Return null if the call is not monomorphic in
 669 // its calling environment, or if there are only abstract methods.
 670 // The returned method is never abstract.
 671 // Note: If caller uses a non-null result, it must inform dependencies
 672 // via assert_unique_concrete_method or assert_leaf_type.
 673 ciMethod* ciMethod::find_monomorphic_target(ciInstanceKlass* caller,
 674                                             ciInstanceKlass* callee_holder,
 675                                             ciInstanceKlass* actual_recv,
 676                                             bool check_access) {
 677   check_is_loaded();
 678 
 679   if (actual_recv->is_interface()) {
 680     // %%% We cannot trust interface types, yet.  See bug 6312651.
 681     return nullptr;
 682   }
 683 
 684   ciMethod* root_m = resolve_invoke(caller, actual_recv, check_access, true /* allow_abstract */);
 685   if (root_m == nullptr) {
 686     // Something went wrong looking up the actual receiver method.
 687     return nullptr;
 688   }
 689 
 690   // Make certain quick checks even if UseCHA is false.
 691 
 692   // Is it private or final?
 693   if (root_m->can_be_statically_bound()) {
 694     assert(!root_m->is_abstract(), "sanity");
 695     return root_m;
 696   }
 697 
 698   if (actual_recv->is_leaf_type() && actual_recv == root_m->holder()) {
 699     // Easy case.  There is no other place to put a method, so don't bother
 700     // to go through the VM_ENTRY_MARK and all the rest.
 701     if (root_m->is_abstract()) {
 702       return nullptr;
 703     }
 704     return root_m;
 705   }
 706 
 707   // Array methods (clone, hashCode, etc.) are always statically bound.
 708   // If we were to see an array type here, we'd return root_m.
 709   // However, this method processes only ciInstanceKlasses.  (See 4962591.)
 710   // The inline_native_clone intrinsic narrows Object to T[] properly,
 711   // so there is no need to do the same job here.
 712 
 713   if (!UseCHA)  return nullptr;
 714 
 715   VM_ENTRY_MARK;
 716 
 717   methodHandle target;
 718   {
 719     MutexLocker locker(Compile_lock);
 720     InstanceKlass* context = actual_recv->get_instanceKlass();
 721     if (UseVtableBasedCHA) {
 722       target = methodHandle(THREAD, Dependencies::find_unique_concrete_method(context,
 723                                                                               root_m->get_Method(),
 724                                                                               callee_holder->get_Klass(),
 725                                                                               this->get_Method()));
 726     } else {
 727       if (root_m->is_abstract()) {
 728         return nullptr; // not supported
 729       }
 730       target = methodHandle(THREAD, Dependencies::find_unique_concrete_method(context, root_m->get_Method()));
 731     }
 732     assert(target() == nullptr || !target()->is_abstract(), "not allowed");
 733     // %%% Should upgrade this ciMethod API to look for 1 or 2 concrete methods.
 734   }
 735 
 736 #ifndef PRODUCT
 737   LogTarget(Debug, dependencies) lt;
 738   if (lt.is_enabled() && target() != nullptr && target() != root_m->get_Method()) {
 739     LogStream ls(&lt);
 740     ls.print("found a non-root unique target method");
 741     ls.print_cr("  context = %s", actual_recv->get_Klass()->external_name());
 742     ls.print("  method  = ");
 743     target->print_short_name(&ls);
 744     ls.cr();
 745   }
 746 #endif //PRODUCT
 747 
 748   if (target() == nullptr) {
 749     return nullptr;
 750   }
 751   if (target() == root_m->get_Method()) {
 752     return root_m;
 753   }
 754   if (!root_m->is_public() &&
 755       !root_m->is_protected()) {
 756     // If we are going to reason about inheritance, it's easiest
 757     // if the method in question is public, protected, or private.
 758     // If the answer is not root_m, it is conservatively correct
 759     // to return null, even if the CHA encountered irrelevant
 760     // methods in other packages.
 761     // %%% TO DO: Work out logic for package-private methods
 762     // with the same name but different vtable indexes.
 763     return nullptr;
 764   }
 765   return CURRENT_THREAD_ENV->get_method(target());
 766 }
 767 
 768 // ------------------------------------------------------------------
 769 // ciMethod::can_be_statically_bound
 770 //
 771 // Tries to determine whether a method can be statically bound in some context.
 772 bool ciMethod::can_be_statically_bound(ciInstanceKlass* context) const {
 773   return (holder() == context) && can_be_statically_bound();
 774 }
 775 
 776 // ------------------------------------------------------------------
 777 // ciMethod::can_omit_stack_trace
 778 //
 779 // Tries to determine whether a method can omit stack trace in throw in compiled code.
 780 bool ciMethod::can_omit_stack_trace() const {
 781   if (!StackTraceInThrowable) {
 782     return true; // stack trace is switched off.
 783   }
 784   if (!OmitStackTraceInFastThrow) {
 785     return false; // Have to provide stack trace.
 786   }
 787   return _can_omit_stack_trace;
 788 }
 789 
 790 // ------------------------------------------------------------------
 791 // ciMethod::resolve_invoke
 792 //
 793 // Given a known receiver klass, find the target for the call.
 794 // Return null if the call has no target or the target is abstract.
 795 ciMethod* ciMethod::resolve_invoke(ciKlass* caller, ciKlass* exact_receiver, bool check_access, bool allow_abstract) {
 796   check_is_loaded();
 797   VM_ENTRY_MARK;
 798 
 799   Klass* caller_klass = caller->get_Klass();
 800   Klass* recv         = exact_receiver->get_Klass();
 801   Klass* resolved     = holder()->get_Klass();
 802   Symbol* h_name      = name()->get_symbol();
 803   Symbol* h_signature = signature()->get_symbol();
 804 
 805   LinkInfo link_info(resolved, h_name, h_signature, caller_klass,
 806                      check_access ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
 807                      check_access ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
 808   Method* m = nullptr;
 809   // Only do exact lookup if receiver klass has been linked.  Otherwise,
 810   // the vtable has not been setup, and the LinkResolver will fail.
 811   if (recv->is_array_klass()
 812        ||
 813       (InstanceKlass::cast(recv)->is_linked() && !exact_receiver->is_interface())) {
 814     if (holder()->is_interface()) {
 815       m = LinkResolver::resolve_interface_call_or_null(recv, link_info);
 816     } else {
 817       m = LinkResolver::resolve_virtual_call_or_null(recv, link_info);
 818     }
 819   }
 820 
 821   if (m == nullptr) {
 822     // Return null only if there was a problem with lookup (uninitialized class, etc.)
 823     return nullptr;
 824   }
 825 
 826   ciMethod* result = this;
 827   if (m != get_Method()) {
 828     result = CURRENT_THREAD_ENV->get_method(m);
 829   }
 830 
 831   if (result->is_abstract() && !allow_abstract) {
 832     // Don't return abstract methods because they aren't optimizable or interesting.
 833     return nullptr;
 834   }
 835   return result;
 836 }
 837 
 838 // ------------------------------------------------------------------
 839 // ciMethod::resolve_vtable_index
 840 //
 841 // Given a known receiver klass, find the vtable index for the call.
 842 // Return Method::invalid_vtable_index if the vtable_index is unknown.
 843 int ciMethod::resolve_vtable_index(ciKlass* caller, ciKlass* receiver) {
 844    check_is_loaded();
 845 
 846    int vtable_index = Method::invalid_vtable_index;
 847    // Only do lookup if receiver klass has been linked.  Otherwise,
 848    // the vtable has not been setup, and the LinkResolver will fail.
 849    if (!receiver->is_interface()
 850        && (!receiver->is_instance_klass() ||
 851            receiver->as_instance_klass()->is_linked())) {
 852      VM_ENTRY_MARK;
 853 
 854      Klass* caller_klass = caller->get_Klass();
 855      Klass* recv         = receiver->get_Klass();
 856      Symbol* h_name = name()->get_symbol();
 857      Symbol* h_signature = signature()->get_symbol();
 858 
 859      LinkInfo link_info(recv, h_name, h_signature, caller_klass);
 860      vtable_index = LinkResolver::resolve_virtual_vtable_index(recv, link_info);
 861      if (vtable_index == Method::nonvirtual_vtable_index) {
 862        // A statically bound method.  Return "no such index".
 863        vtable_index = Method::invalid_vtable_index;
 864      }
 865    }
 866 
 867    return vtable_index;
 868 }
 869 
 870 // ------------------------------------------------------------------
 871 // ciMethod::get_field_at_bci
 872 ciField* ciMethod::get_field_at_bci(int bci, bool &will_link) {
 873   ciBytecodeStream iter(this);
 874   iter.reset_to_bci(bci);
 875   iter.next();
 876   return iter.get_field(will_link);
 877 }
 878 
 879 // ------------------------------------------------------------------
 880 // ciMethod::get_method_at_bci
 881 ciMethod* ciMethod::get_method_at_bci(int bci, bool &will_link, ciSignature* *declared_signature) {
 882   ciBytecodeStream iter(this);
 883   iter.reset_to_bci(bci);
 884   iter.next();
 885   return iter.get_method(will_link, declared_signature);
 886 }
 887 
 888 // ------------------------------------------------------------------
 889 ciKlass* ciMethod::get_declared_method_holder_at_bci(int bci) {
 890   ciBytecodeStream iter(this);
 891   iter.reset_to_bci(bci);
 892   iter.next();
 893   return iter.get_declared_method_holder();
 894 }
 895 
 896 // ------------------------------------------------------------------
 897 // Adjust a CounterData count to be commensurate with
 898 // interpreter_invocation_count.  If the MDO exists for
 899 // only 25% of the time the method exists, then the
 900 // counts in the MDO should be scaled by 4X, so that
 901 // they can be usefully and stably compared against the
 902 // invocation counts in methods.
 903 int ciMethod::scale_count(int count, float prof_factor) {
 904   if (count > 0 && method_data() != nullptr) {
 905     int counter_life = method_data()->invocation_count();
 906     int method_life = interpreter_invocation_count();
 907     if (method_life < counter_life) { // may happen because of the snapshot timing
 908       method_life = counter_life;
 909     }
 910     if (counter_life > 0) {
 911       count = (int)((double)count * prof_factor * method_life / counter_life + 0.5);
 912       count = (count > 0) ? count : 1;
 913     } else {
 914       count = 1;
 915     }
 916   }
 917   return count;
 918 }
 919 
 920 
 921 // ------------------------------------------------------------------
 922 // ciMethod::is_special_get_caller_class_method
 923 //
 924 bool ciMethod::is_ignored_by_security_stack_walk() const {
 925   check_is_loaded();
 926   VM_ENTRY_MARK;
 927   return get_Method()->is_ignored_by_security_stack_walk();
 928 }
 929 
 930 // ------------------------------------------------------------------
 931 // ciMethod::needs_clinit_barrier
 932 //
 933 bool ciMethod::needs_clinit_barrier() const {
 934   check_is_loaded();
 935   return is_static() && !holder()->is_initialized();
 936 }
 937 
 938 // ------------------------------------------------------------------
 939 // invokedynamic support
 940 
 941 // ------------------------------------------------------------------
 942 // ciMethod::is_method_handle_intrinsic
 943 //
 944 // Return true if the method is an instance of the JVM-generated
 945 // signature-polymorphic MethodHandle methods, _invokeBasic, _linkToVirtual, etc.
 946 bool ciMethod::is_method_handle_intrinsic() const {
 947   vmIntrinsics::ID iid = _intrinsic_id;  // do not check if loaded
 948   return (MethodHandles::is_signature_polymorphic(iid) &&
 949           MethodHandles::is_signature_polymorphic_intrinsic(iid));
 950 }
 951 
 952 // ------------------------------------------------------------------
 953 // ciMethod::is_compiled_lambda_form
 954 //
 955 // Return true if the method is a generated MethodHandle adapter.
 956 // These are built by Java code.
 957 bool ciMethod::is_compiled_lambda_form() const {
 958   vmIntrinsics::ID iid = _intrinsic_id;  // do not check if loaded
 959   return iid == vmIntrinsics::_compiledLambdaForm;
 960 }
 961 
 962 // ------------------------------------------------------------------
 963 // ciMethod::is_object_initializer
 964 //
 965 bool ciMethod::is_object_initializer() const {
 966    return name() == ciSymbols::object_initializer_name();
 967 }
 968 
 969 // ------------------------------------------------------------------
 970 // ciMethod::has_member_arg
 971 //
 972 // Return true if the method is a linker intrinsic like _linkToVirtual.
 973 // These are built by the JVM.
 974 bool ciMethod::has_member_arg() const {
 975   vmIntrinsics::ID iid = _intrinsic_id;  // do not check if loaded
 976   return (MethodHandles::is_signature_polymorphic(iid) &&
 977           MethodHandles::has_member_arg(iid));
 978 }
 979 
 980 // ------------------------------------------------------------------
 981 // ciMethod::ensure_method_data
 982 //
 983 // Generate new MethodData* objects at compile time.
 984 // Return true if allocation was successful or no MDO is required.
 985 bool ciMethod::ensure_method_data(const methodHandle& h_m) {
 986   EXCEPTION_CONTEXT;
 987   if (is_native() || is_abstract() || h_m()->is_accessor()) {
 988     return true;
 989   }
 990   if (h_m()->method_data() == nullptr) {
 991     Method::build_profiling_method_data(h_m, THREAD);
 992     if (HAS_PENDING_EXCEPTION) {
 993       CLEAR_PENDING_EXCEPTION;
 994     }
 995   }
 996   if (h_m()->method_data() != nullptr) {
 997     _method_data = CURRENT_ENV->get_method_data(h_m()->method_data());
 998     return _method_data->load_data();
 999   } else {
1000     _method_data = CURRENT_ENV->get_empty_methodData();
1001     return false;
1002   }
1003 }
1004 
1005 // public, retroactive version
1006 bool ciMethod::ensure_method_data() {
1007   bool result = true;
1008   if (_method_data == nullptr || _method_data->is_empty()) {
1009     GUARDED_VM_ENTRY({
1010       methodHandle mh(Thread::current(), get_Method());
1011       result = ensure_method_data(mh);
1012     });
1013   }
1014   return result;
1015 }
1016 
1017 
1018 // ------------------------------------------------------------------
1019 // ciMethod::method_data
1020 //
1021 ciMethodData* ciMethod::method_data() {
1022   if (_method_data != nullptr) {
1023     return _method_data;
1024   }
1025   VM_ENTRY_MARK;
1026   ciEnv* env = CURRENT_ENV;
1027   Thread* my_thread = JavaThread::current();
1028   methodHandle h_m(my_thread, get_Method());
1029 
1030   if (h_m()->method_data() != nullptr) {
1031     _method_data = CURRENT_ENV->get_method_data(h_m()->method_data());
1032     _method_data->load_data();
1033   } else {
1034     _method_data = CURRENT_ENV->get_empty_methodData();
1035   }
1036   return _method_data;
1037 
1038 }
1039 
1040 // ------------------------------------------------------------------
1041 // ciMethod::method_data_or_null
1042 // Returns a pointer to ciMethodData if MDO exists on the VM side,
1043 // null otherwise.
1044 ciMethodData* ciMethod::method_data_or_null() {
1045   ciMethodData *md = method_data();
1046   if (md->is_empty()) {
1047     return nullptr;
1048   }
1049   return md;
1050 }
1051 
1052 // ------------------------------------------------------------------
1053 // ciMethod::ensure_method_counters
1054 //
1055 MethodCounters* ciMethod::ensure_method_counters() {
1056   check_is_loaded();
1057   VM_ENTRY_MARK;
1058   methodHandle mh(THREAD, get_Method());
1059   MethodCounters* method_counters = mh->get_method_counters(CHECK_NULL);
1060   return method_counters;
1061 }
1062 
1063 // ------------------------------------------------------------------
1064 // ciMethod::has_option
1065 //
1066 bool ciMethod::has_option(CompileCommandEnum option) {
1067   check_is_loaded();
1068   VM_ENTRY_MARK;
1069   methodHandle mh(THREAD, get_Method());
1070   return CompilerOracle::has_option(mh, option);
1071 }
1072 
1073 // ------------------------------------------------------------------
1074 // ciMethod::has_option_value
1075 //
1076 bool ciMethod::has_option_value(CompileCommandEnum option, double& value) {
1077   check_is_loaded();
1078   VM_ENTRY_MARK;
1079   methodHandle mh(THREAD, get_Method());
1080   return CompilerOracle::has_option_value(mh, option, value);
1081 }
1082 // ------------------------------------------------------------------
1083 // ciMethod::can_be_compiled
1084 //
1085 // Have previous compilations of this method succeeded?
1086 bool ciMethod::can_be_compiled() {
1087   check_is_loaded();
1088   ciEnv* env = CURRENT_ENV;
1089   if (is_c1_compile(env->comp_level())) {
1090     return _is_c1_compilable;
1091   }
1092   return _is_c2_compilable;
1093 }
1094 
1095 // ------------------------------------------------------------------
1096 // ciMethod::has_compiled_code
1097 bool ciMethod::has_compiled_code() {
1098   return inline_instructions_size() > 0;
1099 }
1100 
1101 int ciMethod::highest_osr_comp_level() {
1102   check_is_loaded();
1103   VM_ENTRY_MARK;
1104   return get_Method()->highest_osr_comp_level();
1105 }
1106 
1107 // ------------------------------------------------------------------
1108 // ciMethod::code_size_for_inlining
1109 //
1110 // Code size for inlining decisions.  This method returns a code
1111 // size of 1 for methods which has the ForceInline annotation.
1112 int ciMethod::code_size_for_inlining() {
1113   check_is_loaded();
1114   if (get_Method()->force_inline()) {
1115     return 1;
1116   }
1117   return code_size();
1118 }
1119 
1120 // ------------------------------------------------------------------
1121 // ciMethod::inline_instructions_size
1122 //
1123 // This is a rough metric for "fat" methods, compared before inlining
1124 // with InlineSmallCode.  The CodeBlob::code_size accessor includes
1125 // junk like exception handler, stubs, and constant table, which are
1126 // not highly relevant to an inlined method.  So we use the more
1127 // specific accessor nmethod::insts_size.
1128 // Also some instructions inside the code are excluded from inline
1129 // heuristic (e.g. post call nop instructions; see InlineSkippedInstructionsCounter)
1130 int ciMethod::inline_instructions_size() {
1131   if (_inline_instructions_size == -1) {
1132     GUARDED_VM_ENTRY(
1133       nmethod* code = get_Method()->code();
1134       if (code != nullptr && (code->comp_level() == CompLevel_full_optimization)) {
1135         int isize = code->insts_end() - code->verified_entry_point() - code->skipped_instructions_size();
1136         _inline_instructions_size = isize > 0 ? isize : 0;
1137       } else {
1138         _inline_instructions_size = 0;
1139       }
1140     );
1141   }
1142   return _inline_instructions_size;
1143 }
1144 
1145 // ------------------------------------------------------------------
1146 // ciMethod::log_nmethod_identity
1147 void ciMethod::log_nmethod_identity(xmlStream* log) {
1148   GUARDED_VM_ENTRY(
1149     nmethod* code = get_Method()->code();
1150     if (code != nullptr) {
1151       code->log_identity(log);
1152     }
1153   )
1154 }
1155 
1156 // ------------------------------------------------------------------
1157 // ciMethod::is_not_reached
1158 bool ciMethod::is_not_reached(int bci) {
1159   check_is_loaded();
1160   VM_ENTRY_MARK;
1161   return Interpreter::is_not_reached(
1162                methodHandle(THREAD, get_Method()), bci);
1163 }
1164 
1165 // ------------------------------------------------------------------
1166 // ciMethod::was_never_executed
1167 bool ciMethod::was_executed_more_than(int times) {
1168   VM_ENTRY_MARK;
1169   return get_Method()->was_executed_more_than(times);
1170 }
1171 
1172 // ------------------------------------------------------------------
1173 // ciMethod::has_unloaded_classes_in_signature
1174 bool ciMethod::has_unloaded_classes_in_signature() {
1175   // ciSignature is resolved against some accessing class and
1176   // signature classes aren't required to be local. As a benefit,
1177   // it makes signature classes visible through loader constraints.
1178   // So, encountering an unloaded class signals it is absent both in
1179   // the callee (local) and caller contexts.
1180   return signature()->has_unloaded_classes();
1181 }
1182 
1183 // ------------------------------------------------------------------
1184 // ciMethod::is_klass_loaded
1185 bool ciMethod::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
1186   VM_ENTRY_MARK;
1187   return get_Method()->is_klass_loaded(refinfo_index, bc, must_be_resolved);
1188 }
1189 
1190 // ------------------------------------------------------------------
1191 // ciMethod::check_call
1192 bool ciMethod::check_call(int refinfo_index, bool is_static) const {
1193   // This method is used only in C2 from InlineTree::ok_to_inline,
1194   // and is only used under -Xcomp.
1195   // It appears to fail when applied to an invokeinterface call site.
1196   // FIXME: Remove this method and resolve_method_statically; refactor to use the other LinkResolver entry points.
1197   VM_ENTRY_MARK;
1198   {
1199     ExceptionMark em(THREAD);
1200     HandleMark hm(THREAD);
1201     constantPoolHandle pool (THREAD, get_Method()->constants());
1202     Bytecodes::Code code = (is_static ? Bytecodes::_invokestatic : Bytecodes::_invokevirtual);
1203     Method* spec_method = LinkResolver::resolve_method_statically(code, pool, refinfo_index, THREAD);
1204     if (HAS_PENDING_EXCEPTION) {
1205       CLEAR_PENDING_EXCEPTION;
1206       return false;
1207     } else {
1208       return (spec_method->is_static() == is_static);
1209     }
1210   }
1211   return false;
1212 }
1213 // ------------------------------------------------------------------
1214 // ciMethod::print_codes
1215 //
1216 // Print the bytecodes for this method.
1217 void ciMethod::print_codes_on(outputStream* st) {
1218   check_is_loaded();
1219   GUARDED_VM_ENTRY(get_Method()->print_codes_on(st);)
1220 }
1221 
1222 
1223 #define FETCH_FLAG_FROM_VM(flag_accessor) { \
1224   check_is_loaded(); \
1225   VM_ENTRY_MARK; \
1226   return get_Method()->flag_accessor(); \
1227 }
1228 
1229 bool ciMethod::has_loops      () const {         FETCH_FLAG_FROM_VM(has_loops); }
1230 bool ciMethod::has_jsrs       () const {         FETCH_FLAG_FROM_VM(has_jsrs);  }
1231 bool ciMethod::is_getter      () const {         FETCH_FLAG_FROM_VM(is_getter); }
1232 bool ciMethod::is_setter      () const {         FETCH_FLAG_FROM_VM(is_setter); }
1233 bool ciMethod::is_accessor    () const {         FETCH_FLAG_FROM_VM(is_accessor); }
1234 bool ciMethod::is_initializer () const {         FETCH_FLAG_FROM_VM(is_initializer); }
1235 bool ciMethod::is_empty       () const {         FETCH_FLAG_FROM_VM(is_empty_method); }
1236 
1237 bool ciMethod::is_boxing_method() const {
1238   if (intrinsic_id() != vmIntrinsics::_none && holder()->is_box_klass()) {
1239     switch (intrinsic_id()) {
1240       case vmIntrinsics::_Boolean_valueOf:
1241       case vmIntrinsics::_Byte_valueOf:
1242       case vmIntrinsics::_Character_valueOf:
1243       case vmIntrinsics::_Short_valueOf:
1244       case vmIntrinsics::_Integer_valueOf:
1245       case vmIntrinsics::_Long_valueOf:
1246       case vmIntrinsics::_Float_valueOf:
1247       case vmIntrinsics::_Double_valueOf:
1248         return true;
1249       default:
1250         return false;
1251     }
1252   }
1253   return false;
1254 }
1255 
1256 bool ciMethod::is_unboxing_method() const {
1257   if (intrinsic_id() != vmIntrinsics::_none && holder()->is_box_klass()) {
1258     switch (intrinsic_id()) {
1259       case vmIntrinsics::_booleanValue:
1260       case vmIntrinsics::_byteValue:
1261       case vmIntrinsics::_charValue:
1262       case vmIntrinsics::_shortValue:
1263       case vmIntrinsics::_intValue:
1264       case vmIntrinsics::_longValue:
1265       case vmIntrinsics::_floatValue:
1266       case vmIntrinsics::_doubleValue:
1267         return true;
1268       default:
1269         return false;
1270     }
1271   }
1272   return false;
1273 }
1274 
1275 bool ciMethod::is_vector_method() const {
1276   return (holder() == ciEnv::current()->vector_VectorSupport_klass()) &&
1277          (intrinsic_id() != vmIntrinsics::_none);
1278 }
1279 
1280 BCEscapeAnalyzer  *ciMethod::get_bcea() {
1281 #ifdef COMPILER2
1282   if (_bcea == nullptr) {
1283     _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, nullptr);
1284   }
1285   return _bcea;
1286 #else // COMPILER2
1287   ShouldNotReachHere();
1288   return nullptr;
1289 #endif // COMPILER2
1290 }
1291 
1292 ciMethodBlocks  *ciMethod::get_method_blocks() {
1293   if (_method_blocks == nullptr) {
1294     Arena *arena = CURRENT_ENV->arena();
1295     _method_blocks = new (arena) ciMethodBlocks(arena, this);
1296   }
1297   return _method_blocks;
1298 }
1299 
1300 #undef FETCH_FLAG_FROM_VM
1301 
1302 void ciMethod::dump_name_as_ascii(outputStream* st, Method* method) {
1303   st->print("%s %s %s",
1304             CURRENT_ENV->replay_name(method->method_holder()),
1305             method->name()->as_quoted_ascii(),
1306             method->signature()->as_quoted_ascii());
1307 }
1308 
1309 void ciMethod::dump_name_as_ascii(outputStream* st) {
1310   Method* method = get_Method();
1311   dump_name_as_ascii(st, method);
1312 }
1313 
1314 void ciMethod::dump_replay_data(outputStream* st) {
1315   ResourceMark rm;
1316   Method* method = get_Method();
1317   if (MethodHandles::is_signature_polymorphic_method(method)) {
1318     // ignore for now
1319     return;
1320   }
1321   MethodCounters* mcs = method->method_counters();
1322   st->print("ciMethod ");
1323   dump_name_as_ascii(st);
1324   st->print_cr(" %d %d %d %d %d",
1325                mcs == nullptr ? 0 : mcs->invocation_counter()->raw_counter(),
1326                mcs == nullptr ? 0 : mcs->backedge_counter()->raw_counter(),
1327                interpreter_invocation_count(),
1328                interpreter_throwout_count(),
1329                _inline_instructions_size);
1330 }
1331 
1332 // ------------------------------------------------------------------
1333 // ciMethod::print_codes
1334 //
1335 // Print a range of the bytecodes for this method.
1336 void ciMethod::print_codes_on(int from, int to, outputStream* st) {
1337   check_is_loaded();
1338   GUARDED_VM_ENTRY(get_Method()->print_codes_on(from, to, st);)
1339 }
1340 
1341 // ------------------------------------------------------------------
1342 // ciMethod::print_name
1343 //
1344 // Print the name of this method, including signature and some flags.
1345 void ciMethod::print_name(outputStream* st) {
1346   check_is_loaded();
1347   GUARDED_VM_ENTRY(get_Method()->print_name(st);)
1348 }
1349 
1350 // ------------------------------------------------------------------
1351 // ciMethod::print_short_name
1352 //
1353 // Print the name of this method, without signature.
1354 void ciMethod::print_short_name(outputStream* st) {
1355   if (is_loaded()) {
1356     GUARDED_VM_ENTRY(get_Method()->print_short_name(st););
1357   } else {
1358     // Fall back if method is not loaded.
1359     holder()->print_name_on(st);
1360     st->print("::");
1361     name()->print_symbol_on(st);
1362     if (WizardMode)
1363       signature()->as_symbol()->print_symbol_on(st);
1364   }
1365 }
1366 
1367 // ------------------------------------------------------------------
1368 // ciMethod::print_impl
1369 //
1370 // Implementation of the print method.
1371 void ciMethod::print_impl(outputStream* st) {
1372   ciMetadata::print_impl(st);
1373   st->print(" name=");
1374   name()->print_symbol_on(st);
1375   st->print(" holder=");
1376   holder()->print_name_on(st);
1377   st->print(" signature=");
1378   signature()->as_symbol()->print_symbol_on(st);
1379   if (is_loaded()) {
1380     st->print(" loaded=true");
1381     st->print(" arg_size=%d", arg_size());
1382     st->print(" flags=");
1383     flags().print_member_flags(st);
1384   } else {
1385     st->print(" loaded=false");
1386   }
1387 }
1388 
1389 // ------------------------------------------------------------------
1390 
1391 static BasicType erase_to_word_type(BasicType bt) {
1392   if (is_subword_type(bt))   return T_INT;
1393   if (is_reference_type(bt)) return T_OBJECT;
1394   return bt;
1395 }
1396 
1397 static bool basic_types_match(ciType* t1, ciType* t2) {
1398   if (t1 == t2)  return true;
1399   return erase_to_word_type(t1->basic_type()) == erase_to_word_type(t2->basic_type());
1400 }
1401 
1402 bool ciMethod::is_consistent_info(ciMethod* declared_method, ciMethod* resolved_method) {
1403   bool invoke_through_mh_intrinsic = declared_method->is_method_handle_intrinsic() &&
1404                                   !resolved_method->is_method_handle_intrinsic();
1405 
1406   if (!invoke_through_mh_intrinsic) {
1407     // Method name & descriptor should stay the same.
1408     // Signatures may reference unloaded types and thus they may be not strictly equal.
1409     ciSymbol* declared_signature = declared_method->signature()->as_symbol();
1410     ciSymbol* resolved_signature = resolved_method->signature()->as_symbol();
1411 
1412     return (declared_method->name()->equals(resolved_method->name())) &&
1413            (declared_signature->equals(resolved_signature));
1414   }
1415 
1416   ciMethod* linker = declared_method;
1417   ciMethod* target = resolved_method;
1418   // Linkers have appendix argument which is not passed to callee.
1419   int has_appendix = MethodHandles::has_member_arg(linker->intrinsic_id()) ? 1 : 0;
1420   if (linker->arg_size() != (target->arg_size() + has_appendix)) {
1421     return false; // argument slot count mismatch
1422   }
1423 
1424   ciSignature* linker_sig = linker->signature();
1425   ciSignature* target_sig = target->signature();
1426 
1427   if (linker_sig->count() + (linker->is_static() ? 0 : 1) !=
1428       target_sig->count() + (target->is_static() ? 0 : 1) + has_appendix) {
1429     return false; // argument count mismatch
1430   }
1431 
1432   int sbase = 0, rbase = 0;
1433   switch (linker->intrinsic_id()) {
1434     case vmIntrinsics::_linkToVirtual:
1435     case vmIntrinsics::_linkToInterface:
1436     case vmIntrinsics::_linkToSpecial: {
1437       if (target->is_static()) {
1438         return false;
1439       }
1440       if (linker_sig->type_at(0)->is_primitive_type()) {
1441         return false;  // receiver should be an oop
1442       }
1443       sbase = 1; // skip receiver
1444       break;
1445     }
1446     case vmIntrinsics::_linkToStatic: {
1447       if (!target->is_static()) {
1448         return false;
1449       }
1450       break;
1451     }
1452     case vmIntrinsics::_invokeBasic: {
1453       if (target->is_static()) {
1454         if (target_sig->type_at(0)->is_primitive_type()) {
1455           return false; // receiver should be an oop
1456         }
1457         rbase = 1; // skip receiver
1458       }
1459       break;
1460     }
1461     default:
1462       break;
1463   }
1464   assert(target_sig->count() - rbase == linker_sig->count() - sbase - has_appendix, "argument count mismatch");
1465   int arg_count = target_sig->count() - rbase;
1466   for (int i = 0; i < arg_count; i++) {
1467     if (!basic_types_match(linker_sig->type_at(sbase + i), target_sig->type_at(rbase + i))) {
1468       return false;
1469     }
1470   }
1471   // Only check the return type if the symbolic info has non-void return type.
1472   // I.e. the return value of the resolved method can be dropped.
1473   if (!linker->return_type()->is_void() &&
1474       !basic_types_match(linker->return_type(), target->return_type())) {
1475     return false;
1476   }
1477   return true; // no mismatch found
1478 }
1479 
1480 // ------------------------------------------------------------------