1 /*
   2  * Copyright (c) 1999, 2010, 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 "incls/_precompiled.incl"
  26 #include "incls/_ciMethod.cpp.incl"
  27 
  28 // ciMethod
  29 //
  30 // This class represents a methodOop in the HotSpot virtual
  31 // machine.
  32 
  33 
  34 // ------------------------------------------------------------------
  35 // ciMethod::ciMethod
  36 //
  37 // Loaded method.
  38 ciMethod::ciMethod(methodHandle h_m) : ciObject(h_m) {
  39   assert(h_m() != NULL, "no null method");
  40 
  41   // These fields are always filled in in loaded methods.
  42   _flags = ciFlags(h_m()->access_flags());
  43 
  44   // Easy to compute, so fill them in now.
  45   _max_stack          = h_m()->max_stack();
  46   _max_locals         = h_m()->max_locals();
  47   _code_size          = h_m()->code_size();
  48   _intrinsic_id       = h_m()->intrinsic_id();
  49   _handler_count      = h_m()->exception_table()->length() / 4;
  50   _uses_monitors      = h_m()->access_flags().has_monitor_bytecodes();
  51   _balanced_monitors  = !_uses_monitors || h_m()->access_flags().is_monitor_matching();
  52   _is_compilable      = !h_m()->is_not_compilable();
  53   // Lazy fields, filled in on demand.  Require allocation.
  54   _code               = NULL;
  55   _exception_handlers = NULL;
  56   _liveness           = NULL;
  57   _bcea = NULL;
  58   _method_blocks = NULL;
  59 #if defined(COMPILER2) || defined(SHARK)
  60   _flow               = NULL;
  61 #endif // COMPILER2 || SHARK
  62 
  63   ciEnv *env = CURRENT_ENV;
  64   if (env->jvmti_can_hotswap_or_post_breakpoint() && _is_compilable) {
  65     // 6328518 check hotswap conditions under the right lock.
  66     MutexLocker locker(Compile_lock);
  67     if (Dependencies::check_evol_method(h_m()) != NULL) {
  68       _is_compilable = false;
  69     }
  70   } else {
  71     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
  72   }
  73 
  74   if (instanceKlass::cast(h_m()->method_holder())->is_linked()) {
  75     _can_be_statically_bound = h_m()->can_be_statically_bound();
  76   } else {
  77     // Have to use a conservative value in this case.
  78     _can_be_statically_bound = false;
  79   }
  80 
  81   // Adjust the definition of this condition to be more useful:
  82   // %%% take these conditions into account in vtable generation
  83   if (!_can_be_statically_bound && h_m()->is_private())
  84     _can_be_statically_bound = true;
  85   if (_can_be_statically_bound && h_m()->is_abstract())
  86     _can_be_statically_bound = false;
  87 
  88   // generating _signature may allow GC and therefore move m.
  89   // These fields are always filled in.
  90   _name = env->get_object(h_m()->name())->as_symbol();
  91   _holder = env->get_object(h_m()->method_holder())->as_instance_klass();
  92   ciSymbol* sig_symbol = env->get_object(h_m()->signature())->as_symbol();
  93   _signature = new (env->arena()) ciSignature(_holder, sig_symbol);
  94   _method_data = NULL;
  95   // Take a snapshot of these values, so they will be commensurate with the MDO.
  96   if (ProfileInterpreter) {
  97     int invcnt = h_m()->interpreter_invocation_count();
  98     // if the value overflowed report it as max int
  99     _interpreter_invocation_count = invcnt < 0 ? max_jint : invcnt ;
 100     _interpreter_throwout_count   = h_m()->interpreter_throwout_count();
 101   } else {
 102     _interpreter_invocation_count = 0;
 103     _interpreter_throwout_count = 0;
 104   }
 105   if (_interpreter_invocation_count == 0)
 106     _interpreter_invocation_count = 1;
 107 }
 108 
 109 
 110 // ------------------------------------------------------------------
 111 // ciMethod::ciMethod
 112 //
 113 // Unloaded method.
 114 ciMethod::ciMethod(ciInstanceKlass* holder,
 115                    ciSymbol* name,
 116                    ciSymbol* signature) : ciObject(ciMethodKlass::make()) {
 117   // These fields are always filled in.
 118   _name = name;
 119   _holder = holder;
 120   _signature = new (CURRENT_ENV->arena()) ciSignature(_holder, signature);
 121   _intrinsic_id = vmIntrinsics::_none;
 122   _liveness = NULL;
 123   _can_be_statically_bound = false;
 124   _bcea = NULL;
 125   _method_blocks = NULL;
 126   _method_data = NULL;
 127 #if defined(COMPILER2) || defined(SHARK)
 128   _flow = NULL;
 129 #endif // COMPILER2 || SHARK
 130 }
 131 
 132 
 133 // ------------------------------------------------------------------
 134 // ciMethod::load_code
 135 //
 136 // Load the bytecodes and exception handler table for this method.
 137 void ciMethod::load_code() {
 138   VM_ENTRY_MARK;
 139   assert(is_loaded(), "only loaded methods have code");
 140 
 141   methodOop me = get_methodOop();
 142   Arena* arena = CURRENT_THREAD_ENV->arena();
 143 
 144   // Load the bytecodes.
 145   _code = (address)arena->Amalloc(code_size());
 146   memcpy(_code, me->code_base(), code_size());
 147 
 148   // Revert any breakpoint bytecodes in ci's copy
 149   if (me->number_of_breakpoints() > 0) {
 150     BreakpointInfo* bp = instanceKlass::cast(me->method_holder())->breakpoints();
 151     for (; bp != NULL; bp = bp->next()) {
 152       if (bp->match(me)) {
 153         code_at_put(bp->bci(), bp->orig_bytecode());
 154       }
 155     }
 156   }
 157 
 158   // And load the exception table.
 159   typeArrayOop exc_table = me->exception_table();
 160 
 161   // Allocate one extra spot in our list of exceptions.  This
 162   // last entry will be used to represent the possibility that
 163   // an exception escapes the method.  See ciExceptionHandlerStream
 164   // for details.
 165   _exception_handlers =
 166     (ciExceptionHandler**)arena->Amalloc(sizeof(ciExceptionHandler*)
 167                                          * (_handler_count + 1));
 168   if (_handler_count > 0) {
 169     for (int i=0; i<_handler_count; i++) {
 170       int base = i*4;
 171       _exception_handlers[i] = new (arena) ciExceptionHandler(
 172                                 holder(),
 173             /* start    */      exc_table->int_at(base),
 174             /* limit    */      exc_table->int_at(base+1),
 175             /* goto pc  */      exc_table->int_at(base+2),
 176             /* cp index */      exc_table->int_at(base+3));
 177     }
 178   }
 179 
 180   // Put an entry at the end of our list to represent the possibility
 181   // of exceptional exit.
 182   _exception_handlers[_handler_count] =
 183     new (arena) ciExceptionHandler(holder(), 0, code_size(), -1, 0);
 184 
 185   if (CIPrintMethodCodes) {
 186     print_codes();
 187   }
 188 }
 189 
 190 
 191 // ------------------------------------------------------------------
 192 // ciMethod::has_linenumber_table
 193 //
 194 // length unknown until decompression
 195 bool    ciMethod::has_linenumber_table() const {
 196   check_is_loaded();
 197   VM_ENTRY_MARK;
 198   return get_methodOop()->has_linenumber_table();
 199 }
 200 
 201 
 202 // ------------------------------------------------------------------
 203 // ciMethod::compressed_linenumber_table
 204 u_char* ciMethod::compressed_linenumber_table() const {
 205   check_is_loaded();
 206   VM_ENTRY_MARK;
 207   return get_methodOop()->compressed_linenumber_table();
 208 }
 209 
 210 
 211 // ------------------------------------------------------------------
 212 // ciMethod::line_number_from_bci
 213 int ciMethod::line_number_from_bci(int bci) const {
 214   check_is_loaded();
 215   VM_ENTRY_MARK;
 216   return get_methodOop()->line_number_from_bci(bci);
 217 }
 218 
 219 
 220 // ------------------------------------------------------------------
 221 // ciMethod::vtable_index
 222 //
 223 // Get the position of this method's entry in the vtable, if any.
 224 int ciMethod::vtable_index() {
 225   check_is_loaded();
 226   assert(holder()->is_linked(), "must be linked");
 227   VM_ENTRY_MARK;
 228   return get_methodOop()->vtable_index();
 229 }
 230 
 231 
 232 #ifdef SHARK
 233 // ------------------------------------------------------------------
 234 // ciMethod::itable_index
 235 //
 236 // Get the position of this method's entry in the itable, if any.
 237 int ciMethod::itable_index() {
 238   check_is_loaded();
 239   assert(holder()->is_linked(), "must be linked");
 240   VM_ENTRY_MARK;
 241   return klassItable::compute_itable_index(get_methodOop());
 242 }
 243 #endif // SHARK
 244 
 245 
 246 // ------------------------------------------------------------------
 247 // ciMethod::native_entry
 248 //
 249 // Get the address of this method's native code, if any.
 250 address ciMethod::native_entry() {
 251   check_is_loaded();
 252   assert(flags().is_native(), "must be native method");
 253   VM_ENTRY_MARK;
 254   methodOop method = get_methodOop();
 255   address entry = method->native_function();
 256   assert(entry != NULL, "must be valid entry point");
 257   return entry;
 258 }
 259 
 260 
 261 // ------------------------------------------------------------------
 262 // ciMethod::interpreter_entry
 263 //
 264 // Get the entry point for running this method in the interpreter.
 265 address ciMethod::interpreter_entry() {
 266   check_is_loaded();
 267   VM_ENTRY_MARK;
 268   methodHandle mh(THREAD, get_methodOop());
 269   return Interpreter::entry_for_method(mh);
 270 }
 271 
 272 
 273 // ------------------------------------------------------------------
 274 // ciMethod::uses_balanced_monitors
 275 //
 276 // Does this method use monitors in a strict stack-disciplined manner?
 277 bool ciMethod::has_balanced_monitors() {
 278   check_is_loaded();
 279   if (_balanced_monitors) return true;
 280 
 281   // Analyze the method to see if monitors are used properly.
 282   VM_ENTRY_MARK;
 283   methodHandle method(THREAD, get_methodOop());
 284   assert(method->has_monitor_bytecodes(), "should have checked this");
 285 
 286   // Check to see if a previous compilation computed the
 287   // monitor-matching analysis.
 288   if (method->guaranteed_monitor_matching()) {
 289     _balanced_monitors = true;
 290     return true;
 291   }
 292 
 293   {
 294     EXCEPTION_MARK;
 295     ResourceMark rm(THREAD);
 296     GeneratePairingInfo gpi(method);
 297     gpi.compute_map(CATCH);
 298     if (!gpi.monitor_safe()) {
 299       return false;
 300     }
 301     method->set_guaranteed_monitor_matching();
 302     _balanced_monitors = true;
 303   }
 304   return true;
 305 }
 306 
 307 
 308 // ------------------------------------------------------------------
 309 // ciMethod::get_flow_analysis
 310 ciTypeFlow* ciMethod::get_flow_analysis() {
 311 #if defined(COMPILER2) || defined(SHARK)
 312   if (_flow == NULL) {
 313     ciEnv* env = CURRENT_ENV;
 314     _flow = new (env->arena()) ciTypeFlow(env, this);
 315     _flow->do_flow();
 316   }
 317   return _flow;
 318 #else // COMPILER2 || SHARK
 319   ShouldNotReachHere();
 320   return NULL;
 321 #endif // COMPILER2 || SHARK
 322 }
 323 
 324 
 325 // ------------------------------------------------------------------
 326 // ciMethod::get_osr_flow_analysis
 327 ciTypeFlow* ciMethod::get_osr_flow_analysis(int osr_bci) {
 328 #if defined(COMPILER2) || defined(SHARK)
 329   // OSR entry points are always place after a call bytecode of some sort
 330   assert(osr_bci >= 0, "must supply valid OSR entry point");
 331   ciEnv* env = CURRENT_ENV;
 332   ciTypeFlow* flow = new (env->arena()) ciTypeFlow(env, this, osr_bci);
 333   flow->do_flow();
 334   return flow;
 335 #else // COMPILER2 || SHARK
 336   ShouldNotReachHere();
 337   return NULL;
 338 #endif // COMPILER2 || SHARK
 339 }
 340 
 341 // ------------------------------------------------------------------
 342 // ciMethod::raw_liveness_at_bci
 343 //
 344 // Which local variables are live at a specific bci?
 345 MethodLivenessResult ciMethod::raw_liveness_at_bci(int bci) {
 346   check_is_loaded();
 347   if (_liveness == NULL) {
 348     // Create the liveness analyzer.
 349     Arena* arena = CURRENT_ENV->arena();
 350     _liveness = new (arena) MethodLiveness(arena, this);
 351     _liveness->compute_liveness();
 352   }
 353   return _liveness->get_liveness_at(bci);
 354 }
 355 
 356 // ------------------------------------------------------------------
 357 // ciMethod::liveness_at_bci
 358 //
 359 // Which local variables are live at a specific bci?  When debugging
 360 // will return true for all locals in some cases to improve debug
 361 // information.
 362 MethodLivenessResult ciMethod::liveness_at_bci(int bci) {
 363   MethodLivenessResult result = raw_liveness_at_bci(bci);
 364   if (CURRENT_ENV->jvmti_can_access_local_variables() || DeoptimizeALot || CompileTheWorld) {
 365     // Keep all locals live for the user's edification and amusement.
 366     result.at_put_range(0, result.size(), true);
 367   }
 368   return result;
 369 }
 370 
 371 // ciMethod::live_local_oops_at_bci
 372 //
 373 // find all the live oops in the locals array for a particular bci
 374 // Compute what the interpreter believes by using the interpreter
 375 // oopmap generator. This is used as a double check during osr to
 376 // guard against conservative result from MethodLiveness making us
 377 // think a dead oop is live.  MethodLiveness is conservative in the
 378 // sense that it may consider locals to be live which cannot be live,
 379 // like in the case where a local could contain an oop or  a primitive
 380 // along different paths.  In that case the local must be dead when
 381 // those paths merge. Since the interpreter's viewpoint is used when
 382 // gc'ing an interpreter frame we need to use its viewpoint  during
 383 // OSR when loading the locals.
 384 
 385 BitMap ciMethod::live_local_oops_at_bci(int bci) {
 386   VM_ENTRY_MARK;
 387   InterpreterOopMap mask;
 388   OopMapCache::compute_one_oop_map(get_methodOop(), bci, &mask);
 389   int mask_size = max_locals();
 390   BitMap result(mask_size);
 391   result.clear();
 392   int i;
 393   for (i = 0; i < mask_size ; i++ ) {
 394     if (mask.is_oop(i)) result.set_bit(i);
 395   }
 396   return result;
 397 }
 398 
 399 
 400 #ifdef COMPILER1
 401 // ------------------------------------------------------------------
 402 // ciMethod::bci_block_start
 403 //
 404 // Marks all bcis where a new basic block starts
 405 const BitMap ciMethod::bci_block_start() {
 406   check_is_loaded();
 407   if (_liveness == NULL) {
 408     // Create the liveness analyzer.
 409     Arena* arena = CURRENT_ENV->arena();
 410     _liveness = new (arena) MethodLiveness(arena, this);
 411     _liveness->compute_liveness();
 412   }
 413 
 414   return _liveness->get_bci_block_start();
 415 }
 416 #endif // COMPILER1
 417 
 418 
 419 // ------------------------------------------------------------------
 420 // ciMethod::call_profile_at_bci
 421 //
 422 // Get the ciCallProfile for the invocation of this method.
 423 // Also reports receiver types for non-call type checks (if TypeProfileCasts).
 424 ciCallProfile ciMethod::call_profile_at_bci(int bci) {
 425   ResourceMark rm;
 426   ciCallProfile result;
 427   if (method_data() != NULL && method_data()->is_mature()) {
 428     ciProfileData* data = method_data()->bci_to_data(bci);
 429     if (data != NULL && data->is_CounterData()) {
 430       // Every profiled call site has a counter.
 431       int count = data->as_CounterData()->count();
 432 
 433       if (!data->is_ReceiverTypeData()) {
 434         result._receiver_count[0] = 0;  // that's a definite zero
 435       } else { // ReceiverTypeData is a subclass of CounterData
 436         ciReceiverTypeData* call = (ciReceiverTypeData*)data->as_ReceiverTypeData();
 437         // In addition, virtual call sites have receiver type information
 438         int receivers_count_total = 0;
 439         int morphism = 0;
 440         for (uint i = 0; i < call->row_limit(); i++) {
 441           ciKlass* receiver = call->receiver(i);
 442           if (receiver == NULL)  continue;
 443           morphism += 1;
 444           int rcount = call->receiver_count(i);
 445           if (rcount == 0) rcount = 1; // Should be valid value
 446           receivers_count_total += rcount;
 447           // Add the receiver to result data.
 448           result.add_receiver(receiver, rcount);
 449           // If we extend profiling to record methods,
 450           // we will set result._method also.
 451         }
 452         // Determine call site's morphism.
 453         // The call site count is 0 with known morphism (onlt 1 or 2 receivers)
 454         // or < 0 in the case of a type check failured for checkcast, aastore, instanceof.
 455         // The call site count is > 0 in the case of a polymorphic virtual call.
 456         if (morphism > 0 && morphism == result._limit) {
 457            // The morphism <= MorphismLimit.
 458            if ((morphism <  ciCallProfile::MorphismLimit) ||
 459                (morphism == ciCallProfile::MorphismLimit && count == 0)) {
 460 #ifdef ASSERT
 461              if (count > 0) {
 462                this->print_short_name(tty);
 463                tty->print_cr(" @ bci:%d", bci);
 464                this->print_codes();
 465                assert(false, "this call site should not be polymorphic");
 466              }
 467 #endif
 468              result._morphism = morphism;
 469            }
 470         }
 471         // Make the count consistent if this is a call profile. If count is
 472         // zero or less, presume that this is a typecheck profile and
 473         // do nothing.  Otherwise, increase count to be the sum of all
 474         // receiver's counts.
 475         if (count >= 0) {
 476           count += receivers_count_total;
 477         }
 478       }
 479       result._count = count;
 480     }
 481   }
 482   return result;
 483 }
 484 
 485 // ------------------------------------------------------------------
 486 // Add new receiver and sort data by receiver's profile count.
 487 void ciCallProfile::add_receiver(ciKlass* receiver, int receiver_count) {
 488   // Add new receiver and sort data by receiver's counts when we have space
 489   // for it otherwise replace the less called receiver (less called receiver
 490   // is placed to the last array element which is not used).
 491   // First array's element contains most called receiver.
 492   int i = _limit;
 493   for (; i > 0 && receiver_count > _receiver_count[i-1]; i--) {
 494     _receiver[i] = _receiver[i-1];
 495     _receiver_count[i] = _receiver_count[i-1];
 496   }
 497   _receiver[i] = receiver;
 498   _receiver_count[i] = receiver_count;
 499   if (_limit < MorphismLimit) _limit++;
 500 }
 501 
 502 // ------------------------------------------------------------------
 503 // ciMethod::find_monomorphic_target
 504 //
 505 // Given a certain calling environment, find the monomorphic target
 506 // for the call.  Return NULL if the call is not monomorphic in
 507 // its calling environment, or if there are only abstract methods.
 508 // The returned method is never abstract.
 509 // Note: If caller uses a non-null result, it must inform dependencies
 510 // via assert_unique_concrete_method or assert_leaf_type.
 511 ciMethod* ciMethod::find_monomorphic_target(ciInstanceKlass* caller,
 512                                             ciInstanceKlass* callee_holder,
 513                                             ciInstanceKlass* actual_recv) {
 514   check_is_loaded();
 515 
 516   if (actual_recv->is_interface()) {
 517     // %%% We cannot trust interface types, yet.  See bug 6312651.
 518     return NULL;
 519   }
 520 
 521   ciMethod* root_m = resolve_invoke(caller, actual_recv);
 522   if (root_m == NULL) {
 523     // Something went wrong looking up the actual receiver method.
 524     return NULL;
 525   }
 526   assert(!root_m->is_abstract(), "resolve_invoke promise");
 527 
 528   // Make certain quick checks even if UseCHA is false.
 529 
 530   // Is it private or final?
 531   if (root_m->can_be_statically_bound()) {
 532     return root_m;
 533   }
 534 
 535   if (actual_recv->is_leaf_type() && actual_recv == root_m->holder()) {
 536     // Easy case.  There is no other place to put a method, so don't bother
 537     // to go through the VM_ENTRY_MARK and all the rest.
 538     return root_m;
 539   }
 540 
 541   // Array methods (clone, hashCode, etc.) are always statically bound.
 542   // If we were to see an array type here, we'd return root_m.
 543   // However, this method processes only ciInstanceKlasses.  (See 4962591.)
 544   // The inline_native_clone intrinsic narrows Object to T[] properly,
 545   // so there is no need to do the same job here.
 546 
 547   if (!UseCHA)  return NULL;
 548 
 549   VM_ENTRY_MARK;
 550 
 551   methodHandle target;
 552   {
 553     MutexLocker locker(Compile_lock);
 554     klassOop context = actual_recv->get_klassOop();
 555     target = Dependencies::find_unique_concrete_method(context,
 556                                                        root_m->get_methodOop());
 557     // %%% Should upgrade this ciMethod API to look for 1 or 2 concrete methods.
 558   }
 559 
 560 #ifndef PRODUCT
 561   if (TraceDependencies && target() != NULL && target() != root_m->get_methodOop()) {
 562     tty->print("found a non-root unique target method");
 563     tty->print_cr("  context = %s", instanceKlass::cast(actual_recv->get_klassOop())->external_name());
 564     tty->print("  method  = ");
 565     target->print_short_name(tty);
 566     tty->cr();
 567   }
 568 #endif //PRODUCT
 569 
 570   if (target() == NULL) {
 571     return NULL;
 572   }
 573   if (target() == root_m->get_methodOop()) {
 574     return root_m;
 575   }
 576   if (!root_m->is_public() &&
 577       !root_m->is_protected()) {
 578     // If we are going to reason about inheritance, it's easiest
 579     // if the method in question is public, protected, or private.
 580     // If the answer is not root_m, it is conservatively correct
 581     // to return NULL, even if the CHA encountered irrelevant
 582     // methods in other packages.
 583     // %%% TO DO: Work out logic for package-private methods
 584     // with the same name but different vtable indexes.
 585     return NULL;
 586   }
 587   return CURRENT_THREAD_ENV->get_object(target())->as_method();
 588 }
 589 
 590 // ------------------------------------------------------------------
 591 // ciMethod::resolve_invoke
 592 //
 593 // Given a known receiver klass, find the target for the call.
 594 // Return NULL if the call has no target or the target is abstract.
 595 ciMethod* ciMethod::resolve_invoke(ciKlass* caller, ciKlass* exact_receiver) {
 596    check_is_loaded();
 597    VM_ENTRY_MARK;
 598 
 599    KlassHandle caller_klass (THREAD, caller->get_klassOop());
 600    KlassHandle h_recv       (THREAD, exact_receiver->get_klassOop());
 601    KlassHandle h_resolved   (THREAD, holder()->get_klassOop());
 602    symbolHandle h_name      (THREAD, name()->get_symbolOop());
 603    symbolHandle h_signature (THREAD, signature()->get_symbolOop());
 604 
 605    methodHandle m;
 606    // Only do exact lookup if receiver klass has been linked.  Otherwise,
 607    // the vtable has not been setup, and the LinkResolver will fail.
 608    if (h_recv->oop_is_javaArray()
 609         ||
 610        instanceKlass::cast(h_recv())->is_linked() && !exact_receiver->is_interface()) {
 611      if (holder()->is_interface()) {
 612        m = LinkResolver::resolve_interface_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
 613      } else {
 614        m = LinkResolver::resolve_virtual_call_or_null(h_recv, h_resolved, h_name, h_signature, caller_klass);
 615      }
 616    }
 617 
 618    if (m.is_null()) {
 619      // Return NULL only if there was a problem with lookup (uninitialized class, etc.)
 620      return NULL;
 621    }
 622 
 623    ciMethod* result = this;
 624    if (m() != get_methodOop()) {
 625      result = CURRENT_THREAD_ENV->get_object(m())->as_method();
 626    }
 627 
 628    // Don't return abstract methods because they aren't
 629    // optimizable or interesting.
 630    if (result->is_abstract()) {
 631      return NULL;
 632    } else {
 633      return result;
 634    }
 635 }
 636 
 637 // ------------------------------------------------------------------
 638 // ciMethod::resolve_vtable_index
 639 //
 640 // Given a known receiver klass, find the vtable index for the call.
 641 // Return methodOopDesc::invalid_vtable_index if the vtable_index is unknown.
 642 int ciMethod::resolve_vtable_index(ciKlass* caller, ciKlass* receiver) {
 643    check_is_loaded();
 644 
 645    int vtable_index = methodOopDesc::invalid_vtable_index;
 646    // Only do lookup if receiver klass has been linked.  Otherwise,
 647    // the vtable has not been setup, and the LinkResolver will fail.
 648    if (!receiver->is_interface()
 649        && (!receiver->is_instance_klass() ||
 650            receiver->as_instance_klass()->is_linked())) {
 651      VM_ENTRY_MARK;
 652 
 653      KlassHandle caller_klass (THREAD, caller->get_klassOop());
 654      KlassHandle h_recv       (THREAD, receiver->get_klassOop());
 655      symbolHandle h_name      (THREAD, name()->get_symbolOop());
 656      symbolHandle h_signature (THREAD, signature()->get_symbolOop());
 657 
 658      vtable_index = LinkResolver::resolve_virtual_vtable_index(h_recv, h_recv, h_name, h_signature, caller_klass);
 659      if (vtable_index == methodOopDesc::nonvirtual_vtable_index) {
 660        // A statically bound method.  Return "no such index".
 661        vtable_index = methodOopDesc::invalid_vtable_index;
 662      }
 663    }
 664 
 665    return vtable_index;
 666 }
 667 
 668 // ------------------------------------------------------------------
 669 // ciMethod::interpreter_call_site_count
 670 int ciMethod::interpreter_call_site_count(int bci) {
 671   if (method_data() != NULL) {
 672     ResourceMark rm;
 673     ciProfileData* data = method_data()->bci_to_data(bci);
 674     if (data != NULL && data->is_CounterData()) {
 675       return scale_count(data->as_CounterData()->count());
 676     }
 677   }
 678   return -1;  // unknown
 679 }
 680 
 681 // ------------------------------------------------------------------
 682 // Adjust a CounterData count to be commensurate with
 683 // interpreter_invocation_count.  If the MDO exists for
 684 // only 25% of the time the method exists, then the
 685 // counts in the MDO should be scaled by 4X, so that
 686 // they can be usefully and stably compared against the
 687 // invocation counts in methods.
 688 int ciMethod::scale_count(int count, float prof_factor) {
 689   if (count > 0 && method_data() != NULL) {
 690     int current_mileage = method_data()->current_mileage();
 691     int creation_mileage = method_data()->creation_mileage();
 692     int counter_life = current_mileage - creation_mileage;
 693     int method_life = interpreter_invocation_count();
 694     // counter_life due to backedge_counter could be > method_life
 695     if (counter_life > method_life)
 696       counter_life = method_life;
 697     if (0 < counter_life && counter_life <= method_life) {
 698       count = (int)((double)count * prof_factor * method_life / counter_life + 0.5);
 699       count = (count > 0) ? count : 1;
 700     }
 701   }
 702   return count;
 703 }
 704 
 705 // ------------------------------------------------------------------
 706 // invokedynamic support
 707 
 708 // ------------------------------------------------------------------
 709 // ciMethod::is_method_handle_invoke
 710 //
 711 // Return true if the method is a MethodHandle target.
 712 bool ciMethod::is_method_handle_invoke() const {
 713   bool flag = (holder()->name() == ciSymbol::java_dyn_MethodHandle() &&
 714                methodOopDesc::is_method_handle_invoke_name(name()->sid()));
 715 #ifdef ASSERT
 716   if (is_loaded()) {
 717     bool flag2 = ((flags().as_int() & JVM_MH_INVOKE_BITS) == JVM_MH_INVOKE_BITS);
 718     {
 719       VM_ENTRY_MARK;
 720       bool flag3 = get_methodOop()->is_method_handle_invoke();
 721       assert(flag2 == flag3, "consistent");
 722       assert(flag  == flag3, "consistent");
 723     }
 724   }
 725 #endif //ASSERT
 726   return flag;
 727 }
 728 
 729 // ------------------------------------------------------------------
 730 // ciMethod::is_method_handle_adapter
 731 //
 732 // Return true if the method is a generated MethodHandle adapter.
 733 bool ciMethod::is_method_handle_adapter() const {
 734   check_is_loaded();
 735   VM_ENTRY_MARK;
 736   return get_methodOop()->is_method_handle_adapter();
 737 }
 738 
 739 ciInstance* ciMethod::method_handle_type() {
 740   check_is_loaded();
 741   VM_ENTRY_MARK;
 742   oop mtype = get_methodOop()->method_handle_type();
 743   return CURRENT_THREAD_ENV->get_object(mtype)->as_instance();
 744 }
 745 
 746 
 747 // ------------------------------------------------------------------
 748 // ciMethod::build_method_data
 749 //
 750 // Generate new methodDataOop objects at compile time.
 751 void ciMethod::build_method_data(methodHandle h_m) {
 752   EXCEPTION_CONTEXT;
 753   if (is_native() || is_abstract() || h_m()->is_accessor()) return;
 754   if (h_m()->method_data() == NULL) {
 755     methodOopDesc::build_interpreter_method_data(h_m, THREAD);
 756     if (HAS_PENDING_EXCEPTION) {
 757       CLEAR_PENDING_EXCEPTION;
 758     }
 759   }
 760   if (h_m()->method_data() != NULL) {
 761     _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
 762     _method_data->load_data();
 763   } else {
 764     _method_data = CURRENT_ENV->get_empty_methodData();
 765   }
 766 }
 767 
 768 // public, retroactive version
 769 void ciMethod::build_method_data() {
 770   if (_method_data == NULL || _method_data->is_empty()) {
 771     GUARDED_VM_ENTRY({
 772       build_method_data(get_methodOop());
 773     });
 774   }
 775 }
 776 
 777 
 778 // ------------------------------------------------------------------
 779 // ciMethod::method_data
 780 //
 781 ciMethodData* ciMethod::method_data() {
 782   if (_method_data != NULL) {
 783     return _method_data;
 784   }
 785   VM_ENTRY_MARK;
 786   ciEnv* env = CURRENT_ENV;
 787   Thread* my_thread = JavaThread::current();
 788   methodHandle h_m(my_thread, get_methodOop());
 789 
 790   if (Tier1UpdateMethodData && is_tier1_compile(env->comp_level())) {
 791     build_method_data(h_m);
 792   }
 793 
 794   if (h_m()->method_data() != NULL) {
 795     _method_data = CURRENT_ENV->get_object(h_m()->method_data())->as_method_data();
 796     _method_data->load_data();
 797   } else {
 798     _method_data = CURRENT_ENV->get_empty_methodData();
 799   }
 800   return _method_data;
 801 
 802 }
 803 
 804 
 805 // ------------------------------------------------------------------
 806 // ciMethod::will_link
 807 //
 808 // Will this method link in a specific calling context?
 809 bool ciMethod::will_link(ciKlass* accessing_klass,
 810                          ciKlass* declared_method_holder,
 811                          Bytecodes::Code bc) {
 812   if (!is_loaded()) {
 813     // Method lookup failed.
 814     return false;
 815   }
 816 
 817   // The link checks have been front-loaded into the get_method
 818   // call.  This method (ciMethod::will_link()) will be removed
 819   // in the future.
 820 
 821   return true;
 822 }
 823 
 824 // ------------------------------------------------------------------
 825 // ciMethod::should_exclude
 826 //
 827 // Should this method be excluded from compilation?
 828 bool ciMethod::should_exclude() {
 829   check_is_loaded();
 830   VM_ENTRY_MARK;
 831   methodHandle mh(THREAD, get_methodOop());
 832   bool ignore;
 833   return CompilerOracle::should_exclude(mh, ignore);
 834 }
 835 
 836 // ------------------------------------------------------------------
 837 // ciMethod::should_inline
 838 //
 839 // Should this method be inlined during compilation?
 840 bool ciMethod::should_inline() {
 841   check_is_loaded();
 842   VM_ENTRY_MARK;
 843   methodHandle mh(THREAD, get_methodOop());
 844   return CompilerOracle::should_inline(mh);
 845 }
 846 
 847 // ------------------------------------------------------------------
 848 // ciMethod::should_not_inline
 849 //
 850 // Should this method be disallowed from inlining during compilation?
 851 bool ciMethod::should_not_inline() {
 852   check_is_loaded();
 853   VM_ENTRY_MARK;
 854   methodHandle mh(THREAD, get_methodOop());
 855   return CompilerOracle::should_not_inline(mh);
 856 }
 857 
 858 // ------------------------------------------------------------------
 859 // ciMethod::should_print_assembly
 860 //
 861 // Should the compiler print the generated code for this method?
 862 bool ciMethod::should_print_assembly() {
 863   check_is_loaded();
 864   VM_ENTRY_MARK;
 865   methodHandle mh(THREAD, get_methodOop());
 866   return CompilerOracle::should_print(mh);
 867 }
 868 
 869 // ------------------------------------------------------------------
 870 // ciMethod::break_at_execute
 871 //
 872 // Should the compiler insert a breakpoint into the generated code
 873 // method?
 874 bool ciMethod::break_at_execute() {
 875   check_is_loaded();
 876   VM_ENTRY_MARK;
 877   methodHandle mh(THREAD, get_methodOop());
 878   return CompilerOracle::should_break_at(mh);
 879 }
 880 
 881 // ------------------------------------------------------------------
 882 // ciMethod::has_option
 883 //
 884 bool ciMethod::has_option(const char* option) {
 885   check_is_loaded();
 886   VM_ENTRY_MARK;
 887   methodHandle mh(THREAD, get_methodOop());
 888   return CompilerOracle::has_option_string(mh, option);
 889 }
 890 
 891 // ------------------------------------------------------------------
 892 // ciMethod::can_be_compiled
 893 //
 894 // Have previous compilations of this method succeeded?
 895 bool ciMethod::can_be_compiled() {
 896   check_is_loaded();
 897   return _is_compilable;
 898 }
 899 
 900 // ------------------------------------------------------------------
 901 // ciMethod::set_not_compilable
 902 //
 903 // Tell the VM that this method cannot be compiled at all.
 904 void ciMethod::set_not_compilable() {
 905   check_is_loaded();
 906   VM_ENTRY_MARK;
 907   _is_compilable = false;
 908   get_methodOop()->set_not_compilable();
 909 }
 910 
 911 // ------------------------------------------------------------------
 912 // ciMethod::can_be_osr_compiled
 913 //
 914 // Have previous compilations of this method succeeded?
 915 //
 916 // Implementation note: the VM does not currently keep track
 917 // of failed OSR compilations per bci.  The entry_bci parameter
 918 // is currently unused.
 919 bool ciMethod::can_be_osr_compiled(int entry_bci) {
 920   check_is_loaded();
 921   VM_ENTRY_MARK;
 922   return !get_methodOop()->access_flags().is_not_osr_compilable();
 923 }
 924 
 925 // ------------------------------------------------------------------
 926 // ciMethod::has_compiled_code
 927 bool ciMethod::has_compiled_code() {
 928   VM_ENTRY_MARK;
 929   return get_methodOop()->code() != NULL;
 930 }
 931 
 932 // ------------------------------------------------------------------
 933 // ciMethod::instructions_size
 934 // This is a rough metric for "fat" methods, compared
 935 // before inlining with InlineSmallCode.
 936 // The CodeBlob::instructions_size accessor includes
 937 // junk like exception handler, stubs, and constant table,
 938 // which are not highly relevant to an inlined method.
 939 // So we use the more specific accessor nmethod::code_size.
 940 int ciMethod::instructions_size() {
 941   GUARDED_VM_ENTRY(
 942     nmethod* code = get_methodOop()->code();
 943     // if there's no compiled code or the code was produced by the
 944     // tier1 profiler return 0 for the code size.  This should
 945     // probably be based on the compilation level of the nmethod but
 946     // that currently isn't properly recorded.
 947     if (code == NULL ||
 948         (TieredCompilation && code->compiler() != NULL && code->compiler()->is_c1())) {
 949       return 0;
 950     }
 951     return code->code_end() - code->verified_entry_point();
 952   )
 953 }
 954 
 955 // ------------------------------------------------------------------
 956 // ciMethod::log_nmethod_identity
 957 void ciMethod::log_nmethod_identity(xmlStream* log) {
 958   GUARDED_VM_ENTRY(
 959     nmethod* code = get_methodOop()->code();
 960     if (code != NULL) {
 961       code->log_identity(log);
 962     }
 963   )
 964 }
 965 
 966 // ------------------------------------------------------------------
 967 // ciMethod::is_not_reached
 968 bool ciMethod::is_not_reached(int bci) {
 969   check_is_loaded();
 970   VM_ENTRY_MARK;
 971   return Interpreter::is_not_reached(
 972                methodHandle(THREAD, get_methodOop()), bci);
 973 }
 974 
 975 // ------------------------------------------------------------------
 976 // ciMethod::was_never_executed
 977 bool ciMethod::was_executed_more_than(int times) {
 978   VM_ENTRY_MARK;
 979   return get_methodOop()->was_executed_more_than(times);
 980 }
 981 
 982 // ------------------------------------------------------------------
 983 // ciMethod::has_unloaded_classes_in_signature
 984 bool ciMethod::has_unloaded_classes_in_signature() {
 985   VM_ENTRY_MARK;
 986   {
 987     EXCEPTION_MARK;
 988     methodHandle m(THREAD, get_methodOop());
 989     bool has_unloaded = methodOopDesc::has_unloaded_classes_in_signature(m, (JavaThread *)THREAD);
 990     if( HAS_PENDING_EXCEPTION ) {
 991       CLEAR_PENDING_EXCEPTION;
 992       return true;     // Declare that we may have unloaded classes
 993     }
 994     return has_unloaded;
 995   }
 996 }
 997 
 998 // ------------------------------------------------------------------
 999 // ciMethod::is_klass_loaded
1000 bool ciMethod::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
1001   VM_ENTRY_MARK;
1002   return get_methodOop()->is_klass_loaded(refinfo_index, must_be_resolved);
1003 }
1004 
1005 // ------------------------------------------------------------------
1006 // ciMethod::check_call
1007 bool ciMethod::check_call(int refinfo_index, bool is_static) const {
1008   VM_ENTRY_MARK;
1009   {
1010     EXCEPTION_MARK;
1011     HandleMark hm(THREAD);
1012     constantPoolHandle pool (THREAD, get_methodOop()->constants());
1013     methodHandle spec_method;
1014     KlassHandle  spec_klass;
1015     LinkResolver::resolve_method(spec_method, spec_klass, pool, refinfo_index, THREAD);
1016     if (HAS_PENDING_EXCEPTION) {
1017       CLEAR_PENDING_EXCEPTION;
1018       return false;
1019     } else {
1020       return (spec_method->is_static() == is_static);
1021     }
1022   }
1023   return false;
1024 }
1025 
1026 // ------------------------------------------------------------------
1027 // ciMethod::print_codes
1028 //
1029 // Print the bytecodes for this method.
1030 void ciMethod::print_codes_on(outputStream* st) {
1031   check_is_loaded();
1032   GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(st);)
1033 }
1034 
1035 
1036 #define FETCH_FLAG_FROM_VM(flag_accessor) { \
1037   check_is_loaded(); \
1038   VM_ENTRY_MARK; \
1039   return get_methodOop()->flag_accessor(); \
1040 }
1041 
1042 bool ciMethod::is_empty_method() const {         FETCH_FLAG_FROM_VM(is_empty_method); }
1043 bool ciMethod::is_vanilla_constructor() const {  FETCH_FLAG_FROM_VM(is_vanilla_constructor); }
1044 bool ciMethod::has_loops      () const {         FETCH_FLAG_FROM_VM(has_loops); }
1045 bool ciMethod::has_jsrs       () const {         FETCH_FLAG_FROM_VM(has_jsrs);  }
1046 bool ciMethod::is_accessor    () const {         FETCH_FLAG_FROM_VM(is_accessor); }
1047 bool ciMethod::is_initializer () const {         FETCH_FLAG_FROM_VM(is_initializer); }
1048 
1049 BCEscapeAnalyzer  *ciMethod::get_bcea() {
1050   if (_bcea == NULL) {
1051     _bcea = new (CURRENT_ENV->arena()) BCEscapeAnalyzer(this, NULL);
1052   }
1053   return _bcea;
1054 }
1055 
1056 ciMethodBlocks  *ciMethod::get_method_blocks() {
1057   Arena *arena = CURRENT_ENV->arena();
1058   if (_method_blocks == NULL) {
1059     _method_blocks = new (arena) ciMethodBlocks(arena, this);
1060   }
1061   return _method_blocks;
1062 }
1063 
1064 #undef FETCH_FLAG_FROM_VM
1065 
1066 
1067 // ------------------------------------------------------------------
1068 // ciMethod::print_codes
1069 //
1070 // Print a range of the bytecodes for this method.
1071 void ciMethod::print_codes_on(int from, int to, outputStream* st) {
1072   check_is_loaded();
1073   GUARDED_VM_ENTRY(get_methodOop()->print_codes_on(from, to, st);)
1074 }
1075 
1076 // ------------------------------------------------------------------
1077 // ciMethod::print_name
1078 //
1079 // Print the name of this method, including signature and some flags.
1080 void ciMethod::print_name(outputStream* st) {
1081   check_is_loaded();
1082   GUARDED_VM_ENTRY(get_methodOop()->print_name(st);)
1083 }
1084 
1085 // ------------------------------------------------------------------
1086 // ciMethod::print_short_name
1087 //
1088 // Print the name of this method, without signature.
1089 void ciMethod::print_short_name(outputStream* st) {
1090   check_is_loaded();
1091   GUARDED_VM_ENTRY(get_methodOop()->print_short_name(st);)
1092 }
1093 
1094 // ------------------------------------------------------------------
1095 // ciMethod::print_impl
1096 //
1097 // Implementation of the print method.
1098 void ciMethod::print_impl(outputStream* st) {
1099   ciObject::print_impl(st);
1100   st->print(" name=");
1101   name()->print_symbol_on(st);
1102   st->print(" holder=");
1103   holder()->print_name_on(st);
1104   st->print(" signature=");
1105   signature()->as_symbol()->print_symbol_on(st);
1106   if (is_loaded()) {
1107     st->print(" loaded=true flags=");
1108     flags().print_member_flags(st);
1109   } else {
1110     st->print(" loaded=false");
1111   }
1112 }