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