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