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