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